-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmax_len_nested_map_value_struct1_skyencoder_test_test.go
416 lines (358 loc) · 11.9 KB
/
max_len_nested_map_value_struct1_skyencoder_test_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
// Code generated by github.com/skycoin/skyencoder. DO NOT EDIT.
package tests
import (
"fmt"
mathrand "math/rand"
"reflect"
"strings"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/skycoin/encodertest"
"github.com/skycoin/skycoin/src/cipher/encoder"
)
func newEmptyMaxLenNestedMapValueStruct1ForEncodeTest() *MaxLenNestedMapValueStruct1 {
var obj MaxLenNestedMapValueStruct1
return &obj
}
func newRandomMaxLenNestedMapValueStruct1ForEncodeTest(t *testing.T, rand *mathrand.Rand) *MaxLenNestedMapValueStruct1 {
var obj MaxLenNestedMapValueStruct1
err := encodertest.PopulateRandom(&obj, rand, encodertest.PopulateRandomOptions{
MaxRandLen: 4,
MinRandLen: 1,
})
if err != nil {
t.Fatalf("encodertest.PopulateRandom failed: %v", err)
}
return &obj
}
func newRandomZeroLenMaxLenNestedMapValueStruct1ForEncodeTest(t *testing.T, rand *mathrand.Rand) *MaxLenNestedMapValueStruct1 {
var obj MaxLenNestedMapValueStruct1
err := encodertest.PopulateRandom(&obj, rand, encodertest.PopulateRandomOptions{
MaxRandLen: 0,
MinRandLen: 0,
EmptySliceNil: false,
EmptyMapNil: false,
})
if err != nil {
t.Fatalf("encodertest.PopulateRandom failed: %v", err)
}
return &obj
}
func newRandomZeroLenNilMaxLenNestedMapValueStruct1ForEncodeTest(t *testing.T, rand *mathrand.Rand) *MaxLenNestedMapValueStruct1 {
var obj MaxLenNestedMapValueStruct1
err := encodertest.PopulateRandom(&obj, rand, encodertest.PopulateRandomOptions{
MaxRandLen: 0,
MinRandLen: 0,
EmptySliceNil: true,
EmptyMapNil: true,
})
if err != nil {
t.Fatalf("encodertest.PopulateRandom failed: %v", err)
}
return &obj
}
func testSkyencoderMaxLenNestedMapValueStruct1(t *testing.T, obj *MaxLenNestedMapValueStruct1) {
isEncodableField := func(f reflect.StructField) bool {
// Skip unexported fields
if f.PkgPath != "" {
return false
}
// Skip fields disabled with and enc:"- struct tag
tag := f.Tag.Get("enc")
return !strings.HasPrefix(tag, "-,") && tag != "-"
}
hasOmitEmptyField := func(obj interface{}) bool {
v := reflect.ValueOf(obj)
switch v.Kind() {
case reflect.Ptr:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
t := v.Type()
n := v.NumField()
f := t.Field(n - 1)
tag := f.Tag.Get("enc")
return isEncodableField(f) && strings.Contains(tag, ",omitempty")
default:
return false
}
}
// returns the number of bytes encoded by an omitempty field on a given object
omitEmptyLen := func(obj interface{}) uint64 {
if !hasOmitEmptyField(obj) {
return 0
}
v := reflect.ValueOf(obj)
switch v.Kind() {
case reflect.Ptr:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
n := v.NumField()
f := v.Field(n - 1)
if f.Len() == 0 {
return 0
}
return uint64(4 + f.Len())
default:
return 0
}
}
// EncodeSize
n1 := encoder.Size(obj)
n2 := EncodeSizeMaxLenNestedMapValueStruct1(obj)
if uint64(n1) != n2 {
t.Fatalf("encoder.Size() != EncodeSizeMaxLenNestedMapValueStruct1() (%d != %d)", n1, n2)
}
// Encode
// encoder.Serialize
data1 := encoder.Serialize(obj)
// Encode
data2, err := EncodeMaxLenNestedMapValueStruct1(obj)
if err != nil {
t.Fatalf("EncodeMaxLenNestedMapValueStruct1 failed: %v", err)
}
if uint64(len(data2)) != n2 {
t.Fatal("EncodeMaxLenNestedMapValueStruct1 produced bytes of unexpected length")
}
if len(data1) != len(data2) {
t.Fatalf("len(encoder.Serialize()) != len(EncodeMaxLenNestedMapValueStruct1()) (%d != %d)", len(data1), len(data2))
}
// EncodeToBuffer
data3 := make([]byte, n2+5)
if err := EncodeMaxLenNestedMapValueStruct1ToBuffer(data3, obj); err != nil {
t.Fatalf("EncodeMaxLenNestedMapValueStruct1ToBuffer failed: %v", err)
}
// Decode
// encoder.DeserializeRaw
var obj2 MaxLenNestedMapValueStruct1
if n, err := encoder.DeserializeRaw(data1, &obj2); err != nil {
t.Fatalf("encoder.DeserializeRaw failed: %v", err)
} else if n != uint64(len(data1)) {
t.Fatalf("encoder.DeserializeRaw failed: %v", encoder.ErrRemainingBytes)
}
if !cmp.Equal(*obj, obj2, cmpopts.EquateEmpty(), encodertest.IgnoreAllUnexported()) {
t.Fatal("encoder.DeserializeRaw result wrong")
}
// Decode
var obj3 MaxLenNestedMapValueStruct1
if n, err := DecodeMaxLenNestedMapValueStruct1(data2, &obj3); err != nil {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 failed: %v", err)
} else if n != uint64(len(data2)) {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 bytes read length should be %d, is %d", len(data2), n)
}
if !cmp.Equal(obj2, obj3, cmpopts.EquateEmpty(), encodertest.IgnoreAllUnexported()) {
t.Fatal("encoder.DeserializeRaw() != DecodeMaxLenNestedMapValueStruct1()")
}
// Decode, excess buffer
var obj4 MaxLenNestedMapValueStruct1
n, err := DecodeMaxLenNestedMapValueStruct1(data3, &obj4)
if err != nil {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 failed: %v", err)
}
if hasOmitEmptyField(&obj4) && omitEmptyLen(&obj4) == 0 {
// 4 bytes read for the omitEmpty length, which should be zero (see the 5 bytes added above)
if n != n2+4 {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 bytes read length should be %d, is %d", n2+4, n)
}
} else {
if n != n2 {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 bytes read length should be %d, is %d", n2, n)
}
}
if !cmp.Equal(obj2, obj4, cmpopts.EquateEmpty(), encodertest.IgnoreAllUnexported()) {
t.Fatal("encoder.DeserializeRaw() != DecodeMaxLenNestedMapValueStruct1()")
}
// DecodeExact
var obj5 MaxLenNestedMapValueStruct1
if err := DecodeMaxLenNestedMapValueStruct1Exact(data2, &obj5); err != nil {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 failed: %v", err)
}
if !cmp.Equal(obj2, obj5, cmpopts.EquateEmpty(), encodertest.IgnoreAllUnexported()) {
t.Fatal("encoder.DeserializeRaw() != DecodeMaxLenNestedMapValueStruct1()")
}
// Check that the bytes read value is correct when providing an extended buffer
if !hasOmitEmptyField(&obj3) || omitEmptyLen(&obj3) > 0 {
padding := []byte{0xFF, 0xFE, 0xFD, 0xFC}
data4 := append(data2[:], padding...)
if n, err := DecodeMaxLenNestedMapValueStruct1(data4, &obj3); err != nil {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 failed: %v", err)
} else if n != uint64(len(data2)) {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1 bytes read length should be %d, is %d", len(data2), n)
}
}
}
func TestSkyencoderMaxLenNestedMapValueStruct1(t *testing.T) {
rand := mathrand.New(mathrand.NewSource(time.Now().Unix()))
type testCase struct {
name string
obj *MaxLenNestedMapValueStruct1
}
cases := []testCase{
{
name: "empty object",
obj: newEmptyMaxLenNestedMapValueStruct1ForEncodeTest(),
},
}
nRandom := 10
for i := 0; i < nRandom; i++ {
cases = append(cases, testCase{
name: fmt.Sprintf("randomly populated object %d", i),
obj: newRandomMaxLenNestedMapValueStruct1ForEncodeTest(t, rand),
})
cases = append(cases, testCase{
name: fmt.Sprintf("randomly populated object %d with zero length variable length contents", i),
obj: newRandomZeroLenMaxLenNestedMapValueStruct1ForEncodeTest(t, rand),
})
cases = append(cases, testCase{
name: fmt.Sprintf("randomly populated object %d with zero length variable length contents set to nil", i),
obj: newRandomZeroLenNilMaxLenNestedMapValueStruct1ForEncodeTest(t, rand),
})
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
testSkyencoderMaxLenNestedMapValueStruct1(t, tc.obj)
})
}
}
func decodeMaxLenNestedMapValueStruct1ExpectError(t *testing.T, buf []byte, expectedErr error) {
var obj MaxLenNestedMapValueStruct1
if _, err := DecodeMaxLenNestedMapValueStruct1(buf, &obj); err == nil {
t.Fatal("DecodeMaxLenNestedMapValueStruct1: expected error, got nil")
} else if err != expectedErr {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1: expected error %q, got %q", expectedErr, err)
}
}
func decodeMaxLenNestedMapValueStruct1ExactExpectError(t *testing.T, buf []byte, expectedErr error) {
var obj MaxLenNestedMapValueStruct1
if err := DecodeMaxLenNestedMapValueStruct1Exact(buf, &obj); err == nil {
t.Fatal("DecodeMaxLenNestedMapValueStruct1Exact: expected error, got nil")
} else if err != expectedErr {
t.Fatalf("DecodeMaxLenNestedMapValueStruct1Exact: expected error %q, got %q", expectedErr, err)
}
}
func testSkyencoderMaxLenNestedMapValueStruct1DecodeErrors(t *testing.T, k int, tag string, obj *MaxLenNestedMapValueStruct1) {
isEncodableField := func(f reflect.StructField) bool {
// Skip unexported fields
if f.PkgPath != "" {
return false
}
// Skip fields disabled with and enc:"- struct tag
tag := f.Tag.Get("enc")
return !strings.HasPrefix(tag, "-,") && tag != "-"
}
numEncodableFields := func(obj interface{}) int {
v := reflect.ValueOf(obj)
switch v.Kind() {
case reflect.Ptr:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
t := v.Type()
n := 0
for i := 0; i < v.NumField(); i++ {
f := t.Field(i)
if !isEncodableField(f) {
continue
}
n++
}
return n
default:
return 0
}
}
hasOmitEmptyField := func(obj interface{}) bool {
v := reflect.ValueOf(obj)
switch v.Kind() {
case reflect.Ptr:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
t := v.Type()
n := v.NumField()
f := t.Field(n - 1)
tag := f.Tag.Get("enc")
return isEncodableField(f) && strings.Contains(tag, ",omitempty")
default:
return false
}
}
// returns the number of bytes encoded by an omitempty field on a given object
omitEmptyLen := func(obj interface{}) uint64 {
if !hasOmitEmptyField(obj) {
return 0
}
v := reflect.ValueOf(obj)
switch v.Kind() {
case reflect.Ptr:
v = v.Elem()
}
switch v.Kind() {
case reflect.Struct:
n := v.NumField()
f := v.Field(n - 1)
if f.Len() == 0 {
return 0
}
return uint64(4 + f.Len())
default:
return 0
}
}
n := EncodeSizeMaxLenNestedMapValueStruct1(obj)
buf, err := EncodeMaxLenNestedMapValueStruct1(obj)
if err != nil {
t.Fatalf("EncodeMaxLenNestedMapValueStruct1 failed: %v", err)
}
// A nil buffer cannot decode, unless the object is a struct with a single omitempty field
if hasOmitEmptyField(obj) && numEncodableFields(obj) > 1 {
t.Run(fmt.Sprintf("%d %s buffer underflow nil", k, tag), func(t *testing.T) {
decodeMaxLenNestedMapValueStruct1ExpectError(t, nil, encoder.ErrBufferUnderflow)
})
t.Run(fmt.Sprintf("%d %s exact buffer underflow nil", k, tag), func(t *testing.T) {
decodeMaxLenNestedMapValueStruct1ExactExpectError(t, nil, encoder.ErrBufferUnderflow)
})
}
// Test all possible truncations of the encoded byte array, but skip
// a truncation that would be valid where omitempty is removed
skipN := n - omitEmptyLen(obj)
for i := uint64(0); i < n; i++ {
if i == skipN {
continue
}
t.Run(fmt.Sprintf("%d %s buffer underflow bytes=%d", k, tag, i), func(t *testing.T) {
decodeMaxLenNestedMapValueStruct1ExpectError(t, buf[:i], encoder.ErrBufferUnderflow)
})
t.Run(fmt.Sprintf("%d %s exact buffer underflow bytes=%d", k, tag, i), func(t *testing.T) {
decodeMaxLenNestedMapValueStruct1ExactExpectError(t, buf[:i], encoder.ErrBufferUnderflow)
})
}
// Append 5 bytes for omit empty with a 0 length prefix, to cause an ErrRemainingBytes.
// If only 1 byte is appended, the decoder will try to read the 4-byte length prefix,
// and return an ErrBufferUnderflow instead
if hasOmitEmptyField(obj) {
buf = append(buf, []byte{0, 0, 0, 0, 0}...)
} else {
buf = append(buf, 0)
}
t.Run(fmt.Sprintf("%d %s exact buffer remaining bytes", k, tag), func(t *testing.T) {
decodeMaxLenNestedMapValueStruct1ExactExpectError(t, buf, encoder.ErrRemainingBytes)
})
}
func TestSkyencoderMaxLenNestedMapValueStruct1DecodeErrors(t *testing.T) {
rand := mathrand.New(mathrand.NewSource(time.Now().Unix()))
n := 10
for i := 0; i < n; i++ {
emptyObj := newEmptyMaxLenNestedMapValueStruct1ForEncodeTest()
fullObj := newRandomMaxLenNestedMapValueStruct1ForEncodeTest(t, rand)
testSkyencoderMaxLenNestedMapValueStruct1DecodeErrors(t, i, "empty", emptyObj)
testSkyencoderMaxLenNestedMapValueStruct1DecodeErrors(t, i, "full", fullObj)
}
}