forked from uber-go/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder_test.go
553 lines (517 loc) · 13.9 KB
/
decoder_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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config
import (
"fmt"
"math"
"testing"
"github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNumericConversion(t *testing.T) {
t.Parallel()
providers := map[string]Provider{
"int": newValueProvider(int8(1)),
"uint": newValueProvider(uint(1)),
"int8": newValueProvider(int8(1)),
"uint8": newValueProvider(uint8(1)),
"int16": newValueProvider(int16(1)),
"uint16": newValueProvider(uint16(1)),
"int32": newValueProvider(int32(1)),
"uint32": newValueProvider(uint32(1)),
"int64": newValueProvider(int64(1)),
"uint64": newValueProvider(uint64(1)),
"float32": newValueProvider(float32(1)),
"float64": newValueProvider(float64(1)),
"uintptr": newValueProvider(uintptr(1)),
"string": newValueProvider("1"),
}
conversions := map[string]func(p Provider, t *testing.T){
"int": func(p Provider, t *testing.T) {
var x int
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, int(1), x)
},
"uint": func(p Provider, t *testing.T) {
var x uint
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uint(1), x)
},
"int8": func(p Provider, t *testing.T) {
var x int8
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, int8(1), x)
},
"uint8": func(p Provider, t *testing.T) {
var x uint8
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uint8(1), x)
},
"int16": func(p Provider, t *testing.T) {
var x int16
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, int16(1), x)
},
"uint16": func(p Provider, t *testing.T) {
var x uint16
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uint16(1), x)
},
"int32": func(p Provider, t *testing.T) {
var x int32
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, int32(1), x)
},
"uint32": func(p Provider, t *testing.T) {
var x uint32
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uint32(1), x)
},
"int64": func(p Provider, t *testing.T) {
var x int64
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, int64(1), x)
},
"uint64": func(p Provider, t *testing.T) {
var x uint64
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uint64(1), x)
},
"float32": func(p Provider, t *testing.T) {
var x float32
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, float32(1), x)
},
"float64": func(p Provider, t *testing.T) {
var x float64
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, float64(1), x)
},
"uintptr": func(p Provider, t *testing.T) {
var x uintptr
require.NoError(t, p.Get(Root).Populate(&x))
assert.Equal(t, uintptr(1), x)
},
}
for from, provider := range providers {
for to, test := range conversions {
t.Run(fmt.Sprintf("From %q to %q", from, to),
func(t *testing.T) {
test(provider, t)
})
}
}
}
func TestNumericOverflows(t *testing.T) {
p := newValueProvider(math.MaxFloat64)
conversions := map[string]func(p Provider) error{
"int": func(p Provider) error {
var x int
return p.Get(Root).Populate(&x)
},
"uint": func(p Provider) error {
var x uint
return p.Get(Root).Populate(&x)
},
"int8": func(p Provider) error {
var x int8
return p.Get(Root).Populate(&x)
},
"uint8": func(p Provider) error {
var x uint8
return p.Get(Root).Populate(&x)
},
"int16": func(p Provider) error {
var x int16
return p.Get(Root).Populate(&x)
},
"uint16": func(p Provider) error {
var x uint16
return p.Get(Root).Populate(&x)
},
"int32": func(p Provider) error {
var x int32
return p.Get(Root).Populate(&x)
},
"uint32": func(p Provider) error {
var x uint32
return p.Get(Root).Populate(&x)
},
"int64": func(p Provider) error {
var x int64
return p.Get(Root).Populate(&x)
},
"uint64": func(p Provider) error {
var x uint64
return p.Get(Root).Populate(&x)
},
"float32": func(p Provider) error {
var x float32
return p.Get(Root).Populate(&x)
},
"uintptr": func(p Provider) error {
var x uintptr
return p.Get(Root).Populate(&x)
},
}
for to, f := range conversions {
t.Run(fmt.Sprintf("%q overflow", to), func(t *testing.T) {
err := f(p)
require.Error(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf(`can't convert %q`, fmt.Sprint(math.MaxFloat64)))
assert.Contains(t, err.Error(), to)
})
}
}
func TestUnsignedNumericDecodingNegatives(t *testing.T) {
p := newValueProvider(-1)
conversions := map[string]func(p Provider) error{
"uint": func(p Provider) error {
var x uint
return p.Get(Root).Populate(&x)
},
"uint8": func(p Provider) error {
var x uint8
return p.Get(Root).Populate(&x)
},
"uint16": func(p Provider) error {
var x uint16
return p.Get(Root).Populate(&x)
},
"uint32": func(p Provider) error {
var x uint32
return p.Get(Root).Populate(&x)
},
"uint64": func(p Provider) error {
var x uint64
return p.Get(Root).Populate(&x)
},
"uintptr": func(p Provider) error {
var x uintptr
return p.Get(Root).Populate(&x)
},
}
for to, f := range conversions {
t.Run(fmt.Sprintf("%q convert negative", to), func(t *testing.T) {
err := f(p)
require.Error(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("can't convert \"-1\" to unsigned integer type %q", to))
})
}
}
func TestIdenticalFuzzing(t *testing.T) {
t.Parallel()
type S struct {
ii int
ui uint
i8 int8
u8 uint8
i16 int16
u16 uint16
i32 int32
u32 uint32
i64 int64
u64 uint64
f32 float32
f64 float64
uPtr uintptr
iiPtr *int
uiPtr *uint
i8Ptr *int8
u8Ptr *uint8
i16Ptr *int16
u16Ptr *uint16
i32Ptr *int32
u32Ptr *uint32
i64Ptr *int64
u64Ptr *uint64
f32Ptr *float32
f64Ptr *float64
uPtrPtr *uintptr
s string
sPtr *string
b bool
bPtr *bool
}
f := fuzz.New()
var a, b S
for i := 1; i < 1000; i++ {
f.Fuzz(&a)
p, err := NewStaticProvider(a)
require.NoError(t, err, "Can't create a static provider")
require.NoError(t, p.Get(Root).Populate(&b))
require.Equal(t, a, b)
}
}
// Floating points have 23/52 bits for accuracy and we expect some accuracy loss when provider returns integers.
func TestFloatInAccuracy(t *testing.T) {
t.Parallel()
i32 := 1 << 24
p := newValueProvider(i32)
var f32 float32
require.NoError(t, p.Get(Root).Populate(&f32))
require.Equal(t, f32, float32(i32))
require.Equal(t, f32, float32(i32+1))
var i64 int64 = 1 << 53
p = newValueProvider(i64)
var f64 float64
require.NoError(t, p.Get(Root).Populate(&f64))
require.Equal(t, f64, float64(i64))
require.Equal(t, f64, float64(i64+1))
}
func TestNumericParsingErrors(t *testing.T) {
t.Parallel()
providerErrors := map[string]Provider{
"This can't be parsed as a number": newValueProvider("This can't be parsed as a number"),
//1.797693134862315708145274237317043567981e+308 is the math.MaxFloat64
"1.797693134862315708145274237317043567981e+309": newValueProvider("1.797693134862315708145274237317043567981e+309"),
}
conversions := map[string]func(p Provider) error{
"int": func(p Provider) error {
var x int
return p.Get(Root).Populate(&x)
},
"uint": func(p Provider) error {
var x uint
return p.Get(Root).Populate(&x)
},
"int8": func(p Provider) error {
var x int8
return p.Get(Root).Populate(&x)
},
"uint8": func(p Provider) error {
var x uint8
return p.Get(Root).Populate(&x)
},
"int16": func(p Provider) error {
var x int16
return p.Get(Root).Populate(&x)
},
"uint16": func(p Provider) error {
var x uint16
return p.Get(Root).Populate(&x)
},
"int32": func(p Provider) error {
var x int32
return p.Get(Root).Populate(&x)
},
"uint32": func(p Provider) error {
var x uint32
return p.Get(Root).Populate(&x)
},
"int64": func(p Provider) error {
var x int64
return p.Get(Root).Populate(&x)
},
"uint64": func(p Provider) error {
var x uint64
return p.Get(Root).Populate(&x)
},
"float32": func(p Provider) error {
var x float32
return p.Get(Root).Populate(&x)
},
"float64": func(p Provider) error {
var x float64
return p.Get(Root).Populate(&x)
},
"uintptr": func(p Provider) error {
var x uintptr
return p.Get(Root).Populate(&x)
},
"int alias": func(p Provider) error {
type intAlias int
var x intAlias
return p.Get(Root).Populate(&x)
},
"uint alias": func(p Provider) error {
type uintAlias uint
var x uintAlias
return p.Get(Root).Populate(&x)
},
"int8 alias": func(p Provider) error {
type int8Alias int8
var x int8Alias
return p.Get(Root).Populate(&x)
},
"uint8 alias": func(p Provider) error {
type uint8Alias uint8
var x uint8Alias
return p.Get(Root).Populate(&x)
},
"int16 alias": func(p Provider) error {
type uint16Alias uint16
var x uint16Alias
return p.Get(Root).Populate(&x)
},
"uint16 alias": func(p Provider) error {
type uint16Alias uint16
var x uint16Alias
return p.Get(Root).Populate(&x)
},
"int32 alias": func(p Provider) error {
type int32Alias int32
var x int32Alias
return p.Get(Root).Populate(&x)
},
"uint32 alias": func(p Provider) error {
type uint32Alias uint32
var x uint32Alias
return p.Get(Root).Populate(&x)
},
"int64 alias": func(p Provider) error {
type int64Alias int64
var x int64Alias
return p.Get(Root).Populate(&x)
},
"uint64 alias": func(p Provider) error {
type uint64Alias uint64
var x uint64Alias
return p.Get(Root).Populate(&x)
},
"float32 alias": func(p Provider) error {
type float32Alias float32
var x float32Alias
return p.Get(Root).Populate(&x)
},
"float64 alias": func(p Provider) error {
type float64Alias float64
var x float64Alias
return p.Get(Root).Populate(&x)
},
"uintptr alias": func(p Provider) error {
type uintptrAlias uintptr
var x uintptrAlias
return p.Get(Root).Populate(&x)
},
}
for to, f := range conversions {
for errMsg, provider := range providerErrors {
t.Run(fmt.Sprintf("%q parse error for value %q", to, errMsg), func(t *testing.T) {
err := f(provider)
require.Error(t, err)
assert.Contains(t, err.Error(), errMsg)
})
}
}
}
func TestIntegerOverflowParsingErrors(t *testing.T) {
t.Parallel()
// Can't use math.MaxUint64 directly because numerical constants in Go has to have type int
maxUint64 := fmt.Sprint(uint64(math.MaxUint64))
p := newValueProvider(maxUint64)
conversions := map[string]func(p Provider) error{
"int": func(p Provider) error {
var x int
return p.Get(Root).Populate(&x)
},
"int8": func(p Provider) error {
var x int8
return p.Get(Root).Populate(&x)
},
"uint8": func(p Provider) error {
var x uint8
return p.Get(Root).Populate(&x)
},
"int16": func(p Provider) error {
var x int16
return p.Get(Root).Populate(&x)
},
"uint16": func(p Provider) error {
var x uint16
return p.Get(Root).Populate(&x)
},
"int32": func(p Provider) error {
var x int32
return p.Get(Root).Populate(&x)
},
"uint32": func(p Provider) error {
var x uint32
return p.Get(Root).Populate(&x)
},
"int64": func(p Provider) error {
var x int64
return p.Get(Root).Populate(&x)
},
"int alias": func(p Provider) error {
type intAlias int
var x intAlias
return p.Get(Root).Populate(&x)
},
"int8 alias": func(p Provider) error {
type int8Alias int8
var x int8Alias
return p.Get(Root).Populate(&x)
},
"uint8 alias": func(p Provider) error {
type uint8Alias uint8
var x uint8Alias
return p.Get(Root).Populate(&x)
},
"int16 alias": func(p Provider) error {
type uint16Alias uint16
var x uint16Alias
return p.Get(Root).Populate(&x)
},
"uint16 alias": func(p Provider) error {
type uint16Alias uint16
var x uint16Alias
return p.Get(Root).Populate(&x)
},
"int32 alias": func(p Provider) error {
type int32Alias int32
var x int32Alias
return p.Get(Root).Populate(&x)
},
"uint32 alias": func(p Provider) error {
type uint32Alias uint32
var x uint32Alias
return p.Get(Root).Populate(&x)
},
"int64 alias": func(p Provider) error {
type int64Alias int64
var x int64Alias
return p.Get(Root).Populate(&x)
},
}
for to, f := range conversions {
t.Run(fmt.Sprintf("%q parse error for value %q", to, maxUint64), func(t *testing.T) {
err := f(p)
require.Error(t, err)
assert.Contains(t, err.Error(), maxUint64)
})
}
}
func TestBoolParsing(t *testing.T) {
t.Parallel()
p := newValueProvider("true")
var b bool
require.NoError(t, p.Get(Root).Populate(&b))
assert.True(t, b)
type boolAlias bool
var alias boolAlias
require.NoError(t, p.Get(Root).Populate(&alias))
assert.Equal(t, boolAlias(true), alias)
}
func TestAddKeyToErrorReturnsNilForNilErrors(t *testing.T) {
t.Parallel()
assert.NoError(t, errorWithKey(nil, "key"))
}