-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathbad_data_test.go
More file actions
256 lines (235 loc) · 7.11 KB
/
Copy pathbad_data_test.go
File metadata and controls
256 lines (235 loc) · 7.11 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package maxminddb
import (
"math/bits"
"net/netip"
"testing"
"github.com/stretchr/testify/require"
)
func TestBadDataFixtures(t *testing.T) {
tests := []struct {
name string
openError string
verifyError string
lookupIP string
lookupError string
decodeError string
iterateCount int
iterateError string
iterateDecodeErr string
}{
{
name: "libmaxminddb/libmaxminddb-corrupt-search-tree.mmdb",
verifyError: "description - Expected: non-empty map",
iterateCount: 2,
},
{
name: "libmaxminddb/libmaxminddb-deep-array-nesting.mmdb",
verifyError: "description - Expected: non-empty map",
lookupIP: "1.1.1.1",
decodeError: "exceeded maximum data structure depth",
iterateCount: 2,
iterateDecodeErr: "exceeded maximum data structure depth",
},
{
name: "libmaxminddb/libmaxminddb-deep-nesting.mmdb",
verifyError: "description - Expected: non-empty map",
lookupIP: "1.1.1.1",
decodeError: "exceeded maximum data structure depth",
iterateCount: 2,
iterateDecodeErr: "exceeded maximum data structure depth",
},
{
name: "libmaxminddb/libmaxminddb-empty-array-last-in-metadata.mmdb",
verifyError: "description - Expected: non-empty map",
iterateCount: 2,
},
{
name: "libmaxminddb/libmaxminddb-empty-map-last-in-metadata.mmdb",
verifyError: "description - Expected: non-empty map",
iterateCount: 2,
},
{
name: "libmaxminddb/libmaxminddb-offset-integer-overflow.mmdb",
openError: "unexpected end of database",
},
{
name: "libmaxminddb/libmaxminddb-oversized-array.mmdb",
verifyError: "description - Expected: non-empty map",
lookupIP: "1.1.1.1",
decodeError: "unexpected end of database",
iterateCount: 1,
iterateDecodeErr: "unexpected end of database",
},
{
name: "libmaxminddb/libmaxminddb-oversized-map.mmdb",
verifyError: "description - Expected: non-empty map",
lookupIP: "1.1.1.1",
decodeError: "unexpected end of database",
iterateCount: 1,
iterateDecodeErr: "unexpected end of database",
},
{
name: "libmaxminddb/libmaxminddb-uint64-max-epoch.mmdb",
verifyError: "description - Expected: non-empty map",
iterateCount: 2,
},
{
name: "maxminddb-golang/cyclic-data-structure.mmdb",
openError: "path /record_size: unexpected end of database",
},
{
name: "maxminddb-golang/invalid-bytes-length.mmdb",
openError: "path /description/en: unexpected end of database",
},
{
name: "maxminddb-golang/invalid-data-record-offset.mmdb",
openError: "unexpected end of database",
},
{
name: "maxminddb-golang/invalid-map-key-length.mmdb",
openError: "path /record_size: unexpected end of database",
},
{
name: "maxminddb-golang/invalid-string-length.mmdb",
openError: "path /description: unexpected end of database",
},
{
name: "maxminddb-golang/metadata-is-an-uint128.mmdb",
openError: "unexpected end of database",
},
{
name: "maxminddb-golang/unexpected-bytes.mmdb",
openError: "cannot unmarshal [] ([]uint8) into type []string",
},
{
name: "maxminddb-python/bad-unicode-in-map-key.mmdb",
verifyError: "search tree is corrupt",
lookupIP: "0.0.0.0",
lookupError: "search tree is corrupt",
decodeError: "search tree is corrupt",
iterateCount: 1,
iterateError: "search tree is corrupt",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Skip uint64-max-epoch test on 32-bit architectures: the test fails as
// BuildEpoch is defined as a uint type (ie. 32 bits), leading to error:
// cannot unmarshal 18446744073709551615 (uint64) into type uint
if bits.UintSize == 32 {
if tt.name == "libmaxminddb/libmaxminddb-uint64-max-epoch.mmdb" {
t.Skip("Skipping on 32-bit architectures")
}
}
reader, err := Open(badDataFile(tt.name))
if tt.openError != "" {
require.ErrorContains(t, err, tt.openError)
return
}
require.NoError(t, err)
t.Cleanup(func() {
require.NoError(t, reader.Close())
})
if tt.verifyError != "" {
require.ErrorContains(t, reader.Verify(), tt.verifyError)
}
if tt.lookupIP == "" {
return
}
result := reader.Lookup(netip.MustParseAddr(tt.lookupIP))
if tt.lookupError != "" {
require.ErrorContains(t, result.Err(), tt.lookupError)
} else {
require.NoError(t, result.Err())
}
var value any
if tt.decodeError != "" {
require.ErrorContains(t, result.Decode(&value), tt.decodeError)
} else {
require.NoError(t, result.Decode(&value))
}
if tt.iterateCount == 0 {
return
}
count := 0
sawIterError := false
sawIterDecodeError := false
for iterResult := range reader.Networks(IncludeNetworksWithoutData()) {
count++
if tt.iterateError != "" && iterResult.Err() != nil {
require.ErrorContains(t, iterResult.Err(), tt.iterateError)
sawIterError = true
break
}
require.NoError(t, iterResult.Err())
var iterValue any
if tt.iterateDecodeErr != "" {
err = iterResult.Decode(&iterValue)
if err != nil {
require.ErrorContains(t, err, tt.iterateDecodeErr)
sawIterDecodeError = true
break
}
continue
}
require.NoError(t, iterResult.Decode(&iterValue))
}
require.Equal(t, tt.iterateCount, count)
if tt.iterateError != "" {
require.True(t, sawIterError)
}
if tt.iterateDecodeErr != "" {
require.True(t, sawIterDecodeError)
}
})
}
}
func TestReadNodePairBySizeRejectsShortBuffers(t *testing.T) {
tests := []struct {
name string
buffer []byte
recordSize uint
}{
{name: "24-bit", buffer: []byte{0x01, 0x02, 0x03, 0x04, 0x05}, recordSize: 24},
{name: "28-bit", buffer: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, recordSize: 28},
{name: "32-bit", buffer: []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, recordSize: 32},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, _, err := readNodePairBySize(tt.buffer, 0, tt.recordSize)
require.ErrorContains(t, err, "bounds check failed")
})
}
}
func TestTraverseTreeRejectsShortBuffers(t *testing.T) {
tests := []struct {
name string
buffer []byte
recordSize uint
}{
{name: "24-bit", buffer: []byte{0x01, 0x02}, recordSize: 24},
{name: "28-bit", buffer: []byte{0x01, 0x02, 0x03}, recordSize: 28},
{name: "32-bit", buffer: []byte{0x01, 0x02, 0x03}, recordSize: 32},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
reader := &Reader{
buffer: tt.buffer,
Metadata: Metadata{NodeCount: 1, RecordSize: tt.recordSize},
}
ip := netip.MustParseAddr("1.1.1.1")
var err error
switch tt.recordSize {
case 24:
_, _, err = reader.traverseTree24(ip, 0, 1)
case 28:
_, _, err = reader.traverseTree28(ip, 0, 1)
case 32:
_, _, err = reader.traverseTree32(ip, 0, 1)
default:
t.Fatalf("unexpected record size %d", tt.recordSize)
}
require.ErrorContains(t, err, "bounds check failed")
})
}
}