-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
180 lines (154 loc) · 5.13 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"github.com/maxmind/mmdbinspect/pkg/mmdbinspect"
"github.com/maxmind/mmdbwriter/mmdbtype"
"github.com/oschwald/maxminddb-golang"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"fmt"
"os"
"testing"
)
const (
CityDBPathIn = "./testdata/GeoLite2-City.testmmdb"
CityDBPathOut = "./testdata/GeoLite2-City-mod.mmdb"
TestDataset = "./testdata/dataset.json"
)
func TestToMMDBType(t *testing.T) {
tests := []struct {
key string
value any
expected mmdbtype.DataType
expectedErr string
}{
{
key: "bool_key",
value: true,
expected: mmdbtype.Bool(true),
},
{
key: "string_key",
value: "test",
expected: mmdbtype.String("test"),
},
{
key: "map_key",
value: map[string]any{
"inner_key": "inner_value",
},
expected: mmdbtype.Map{
mmdbtype.String("inner_key"): mmdbtype.String("inner_value"),
},
},
{
key: "slice_key",
value: []any{
"slice_value",
},
expected: mmdbtype.Slice{
mmdbtype.String("slice_value"),
},
},
{
key: "accuracy_radius",
value: 1234.0,
expected: mmdbtype.Uint16(1234),
},
{
key: "latitude",
value: 52.5164,
expected: mmdbtype.Float64(52.5164),
},
{
key: "unsupported_key",
value: 1234.0,
expectedErr: "unsupported numeric type for key \"unsupported_key\": float64",
},
{
key: "unsupported_type_key",
value: struct{}{},
expectedErr: "unsupported type for key \"unsupported_type_key\": struct {}",
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("key=%s,value=%v", tt.key, tt.value), func(t *testing.T) {
result, err := toMMDBType(tt.key, tt.value)
if tt.expectedErr != "" {
assert.Error(t, err)
assert.Contains(t, err.Error(), tt.expectedErr)
} else {
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
}
})
}
}
func TestMain(t *testing.T) {
os.Args = []string{"mmdb-editor",
"-i", CityDBPathIn,
"-o", CityDBPathOut,
"-d", TestDataset}
main()
}
func TestOpenDB(t *testing.T) {
a := assert.New(t)
a.FileExists(CityDBPathIn, "database exists")
reader, err := mmdbinspect.OpenDB(CityDBPathIn)
a.NoError(err, "no open error")
a.IsType(maxminddb.Reader{}, *reader)
reader, err = mmdbinspect.OpenDB("foo/bar/baz")
a.Error(err, "open error when file does not exist")
a.Nil(reader)
a.Equal(
"foo/bar/baz does not exist",
err.Error(),
)
reader, err = mmdbinspect.OpenDB(TestDataset)
a.Error(err)
a.Contains(err.Error(), "could not be opened: error opening database: invalid MaxMind DB file")
a.Nil(reader)
if reader != nil {
require.NoError(t, reader.Close())
}
}
func TestRecordsForNetwork(t *testing.T) {
a := assert.New(t)
reader, err := mmdbinspect.OpenDB(CityDBPathOut) // ipv6 database
a.NoError(err, "no open error")
records, err := mmdbinspect.RecordsForNetwork(*reader, true, "123.125.71.29")
a.NoError(err, "no error on lookup of 123.125.71.29")
a.NotNil(records, "records returned")
records, err = mmdbinspect.RecordsForNetwork(*reader, true, "127.0.0.1/32")
a.NoError(err, "no error on lookup of 127.0.0.1/32")
a.NotNil(records, "records returned")
records, err = mmdbinspect.RecordsForNetwork(*reader, true, "10.200.0.33")
a.NoError(err, "no error on lookup of 10.200.0.33")
a.NotNil(records, "records returned")
records, err = mmdbinspect.RecordsForNetwork(*reader, true, "192.168.33.13/30")
a.NoError(err, "no error on lookup of 192.168.33.13/30")
a.NotNil(records, "records returned")
records, err = mmdbinspect.RecordsForNetwork(*reader, true, "1.1.1.1/29")
a.NoError(err, "got no error when IP not found")
a.Nil(records, "no records returned for 1.1.1.1/29")
records, err = mmdbinspect.RecordsForNetwork(*reader, true, "X.X.Y.Z")
a.Error(err, "got an error")
a.Nil(records, "no records returned for X.X.Y.Z")
a.Equal("X.X.Y.Z is not a valid IP address", err.Error())
require.NoError(t, reader.Close())
}
func TestRecordToString(t *testing.T) {
a := assert.New(t)
ips := []string{"10.200.0.0/24", "10.200.0.211/24", "10.200.0.89", "192.168.33.13/30", "192.168.33.14", "127.0.0.1/32"}
reader, err := mmdbinspect.OpenDB(CityDBPathOut)
a.NoError(err, "no open error")
for _, ip := range ips {
records, err := mmdbinspect.RecordsForNetwork(*reader, true, ip)
a.NoError(err, "no RecordsForNetwork error")
prettyJSON, err := mmdbinspect.RecordToString(records)
a.NoError(err, "no error on stringification")
a.NotNil(prettyJSON, "records stringified")
a.Contains(prettyJSON, "Iglov's property")
a.Contains(prettyJSON, "6255148")
}
require.NoError(t, reader.Close())
}