Repository navigation
Expand file tree
/
Copy pathencode_opts_test.go
More file actions
417 lines (404 loc) · 14.7 KB
/
Copy pathencode_opts_test.go
File metadata and controls
417 lines (404 loc) · 14.7 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
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
// Copyright (c) Faye Amacker. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
package cbor
import (
"io"
"reflect"
"testing"
)
func TestEncModeInvalidOptions(t *testing.T) {
tagsForbiddenErrorMsg := "cbor: cannot set TagsMd to TagsForbidden when ByteSliceLaterFormat is not ByteSliceLaterFormatNone"
for _, tc := range []struct {
name string
opts EncOptions
wantErrorMsg string
}{
{
name: "Sort below range of valid modes",
opts: EncOptions{Sort: -1},
wantErrorMsg: "cbor: invalid Sort -1",
},
{
name: "Sort above range of valid modes",
opts: EncOptions{Sort: 101},
wantErrorMsg: "cbor: invalid Sort 101",
},
{
name: "ShortestFloat below range of valid modes",
opts: EncOptions{ShortestFloat: -1},
wantErrorMsg: "cbor: invalid ShortestFloat -1",
},
{
name: "ShortestFloat above range of valid modes",
opts: EncOptions{ShortestFloat: 101},
wantErrorMsg: "cbor: invalid ShortestFloat 101",
},
{
name: "NaNConvert below range of valid modes",
opts: EncOptions{NaNConvert: -1},
wantErrorMsg: "cbor: invalid NaNConvert -1",
},
{
name: "NaNConvert above range of valid modes",
opts: EncOptions{NaNConvert: 101},
wantErrorMsg: "cbor: invalid NaNConvert 101",
},
{
name: "InfConvert below range of valid modes",
opts: EncOptions{InfConvert: -1},
wantErrorMsg: "cbor: invalid InfConvert -1",
},
{
name: "InfConvert above range of valid modes",
opts: EncOptions{InfConvert: 101},
wantErrorMsg: "cbor: invalid InfConvert 101",
},
{
name: "BigIntConvert below range of valid modes",
opts: EncOptions{BigIntConvert: -1},
wantErrorMsg: "cbor: invalid BigIntConvert -1",
},
{
name: "BigIntConvert above range of valid modes",
opts: EncOptions{BigIntConvert: 101},
wantErrorMsg: "cbor: invalid BigIntConvert 101",
},
{
name: "BigIntConvert above range of valid modes with TagsForbidden",
opts: EncOptions{BigIntConvert: 101, TagsMd: TagsForbidden},
wantErrorMsg: "cbor: invalid BigIntConvert 101",
},
{
name: "BigIntConvert BigIntConvertNone with TagsForbidden",
opts: EncOptions{BigIntConvert: BigIntConvertNone, TagsMd: TagsForbidden},
wantErrorMsg: "cbor: cannot set TagsMd to TagsForbidden when BigIntConvert is BigIntConvertNone",
},
{
name: "Time below range of valid modes",
opts: EncOptions{Time: -1},
wantErrorMsg: "cbor: invalid Time -1",
},
{
name: "Time above range of valid modes",
opts: EncOptions{Time: 101},
wantErrorMsg: "cbor: invalid Time 101",
},
{
name: "TimeTag below range of valid modes",
opts: EncOptions{TimeTag: -1},
wantErrorMsg: "cbor: invalid TimeTag -1",
},
{
name: "TimeTag above range of valid modes",
opts: EncOptions{TimeTag: 101},
wantErrorMsg: "cbor: invalid TimeTag 101",
},
{
name: "IndefLength below range of valid modes",
opts: EncOptions{IndefLength: -1},
wantErrorMsg: "cbor: invalid IndefLength -1",
},
{
name: "IndefLength above range of valid modes",
opts: EncOptions{IndefLength: 101},
wantErrorMsg: "cbor: invalid IndefLength 101",
},
{
name: "NilContainers below range of valid modes",
opts: EncOptions{NilContainers: -1},
wantErrorMsg: "cbor: invalid NilContainers -1",
},
{
name: "NilContainers above range of valid modes",
opts: EncOptions{NilContainers: 101},
wantErrorMsg: "cbor: invalid NilContainers 101",
},
{
name: "TagsMd below range of valid modes",
opts: EncOptions{TagsMd: -1},
wantErrorMsg: "cbor: invalid TagsMd -1",
},
{
name: "TagsMd above range of valid modes",
opts: EncOptions{TagsMd: 101},
wantErrorMsg: "cbor: invalid TagsMd 101",
},
{
name: "OmitEmpty below range of valid modes",
opts: EncOptions{OmitEmpty: -1},
wantErrorMsg: "cbor: invalid OmitEmpty -1",
},
{
name: "OmitEmpty above range of valid modes",
opts: EncOptions{OmitEmpty: 101},
wantErrorMsg: "cbor: invalid OmitEmpty 101",
},
{
name: "String invalid mode",
opts: EncOptions{String: -1},
wantErrorMsg: "cbor: invalid String -1",
},
{
name: "FieldName below range of valid modes",
opts: EncOptions{FieldName: -1},
wantErrorMsg: "cbor: invalid FieldName -1",
},
{
name: "FieldName above range of valid modes",
opts: EncOptions{FieldName: 101},
wantErrorMsg: "cbor: invalid FieldName 101",
},
{
name: "ByteSliceLaterFormat below range of valid modes",
opts: EncOptions{ByteSliceLaterFormat: -1},
wantErrorMsg: "cbor: invalid ByteSliceLaterFormat -1",
},
{
name: "ByteSliceLaterFormat above range of valid modes",
opts: EncOptions{ByteSliceLaterFormat: 101},
wantErrorMsg: "cbor: invalid ByteSliceLaterFormat 101",
},
{
name: "ByteSliceLaterFormat above range of valid modes with TagsForbidden",
opts: EncOptions{ByteSliceLaterFormat: 101, TagsMd: TagsForbidden},
wantErrorMsg: "cbor: invalid ByteSliceLaterFormat 101",
},
{
name: "ByteSliceLaterFormat ByteSliceLaterFormatBase64URL with TagsForbidden",
opts: EncOptions{ByteSliceLaterFormat: ByteSliceLaterFormatBase64URL, TagsMd: TagsForbidden},
wantErrorMsg: tagsForbiddenErrorMsg,
},
{
name: "ByteSliceLaterFormat ByteSliceLaterFormatBase64 with TagsForbidden",
opts: EncOptions{ByteSliceLaterFormat: ByteSliceLaterFormatBase64, TagsMd: TagsForbidden},
wantErrorMsg: tagsForbiddenErrorMsg,
},
{
name: "ByteSliceLaterFormat ByteSliceLaterFormatBase16 with TagsForbidden",
opts: EncOptions{ByteSliceLaterFormat: ByteSliceLaterFormatBase16, TagsMd: TagsForbidden},
wantErrorMsg: tagsForbiddenErrorMsg,
},
{
name: "ByteArray below range of valid modes",
opts: EncOptions{ByteArray: -1},
wantErrorMsg: "cbor: invalid ByteArray -1",
},
{
name: "ByteArray above range of valid modes",
opts: EncOptions{ByteArray: 101},
wantErrorMsg: "cbor: invalid ByteArray 101",
},
{
name: "BinaryMarshaler below range of valid modes",
opts: EncOptions{BinaryMarshaler: -1},
wantErrorMsg: "cbor: invalid BinaryMarshaler -1",
},
{
name: "BinaryMarshaler above range of valid modes",
opts: EncOptions{BinaryMarshaler: 101},
wantErrorMsg: "cbor: invalid BinaryMarshaler 101",
},
{
name: "TextMarshaler below range of valid modes",
opts: EncOptions{TextMarshaler: -1},
wantErrorMsg: "cbor: invalid TextMarshaler -1",
},
{
name: "TextMarshaler above range of valid modes",
opts: EncOptions{TextMarshaler: 101},
wantErrorMsg: "cbor: invalid TextMarshaler 101",
},
{
name: "ToIndefArrayStructTag below range of valid modes",
opts: EncOptions{ToIndefArrayStructTag: -1},
wantErrorMsg: "cbor: invalid ToIndefArrayStructTag -1",
},
{
name: "ToIndefArrayStructTag above range of valid modes",
opts: EncOptions{ToIndefArrayStructTag: maxToIndefArrayStructTagMode},
wantErrorMsg: "cbor: invalid ToIndefArrayStructTag 2",
},
{
name: "ToIndefArrayStructTagAllowed with IndefLengthForbidden",
opts: EncOptions{IndefLength: IndefLengthForbidden, ToIndefArrayStructTag: ToIndefArrayStructTagAllowed},
wantErrorMsg: "cbor: cannot set ToIndefArrayStructTag to ToIndefArrayStructTagAllowed when IndefLength is IndefLengthForbidden",
},
} {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.opts.EncMode()
checkError[error](t, "EncMode", err, tc.wantErrorMsg)
})
}
}
// TestToIndefArrayStructTagOptionsRoundTrip verifies that an EncOptions
// value containing the non-zero value of ToIndefArrayStructTag round-trips
// through EncOptions -> EncMode -> EncOptions with all fields preserved.
// TestEncOptions cannot cover this case because the non-zero values of
// IndefLength and ToIndefArrayStructTag are mutually exclusive.
func TestToIndefArrayStructTagOptionsRoundTrip(t *testing.T) {
opts := EncOptions{ToIndefArrayStructTag: ToIndefArrayStructTagAllowed}
em := mustEncMode(t, opts)
got := em.EncOptions()
if !reflect.DeepEqual(opts, got) {
t.Errorf("em.EncOptions() = %#v, want %#v", got, opts)
}
}
func TestEncOptionsTagsForbidden(t *testing.T) {
// It's not valid to set both TagsMd and TimeTag to a non-zero value in the same EncOptions,
// so this exercises the options-mode-options roundtrip for non-zero TagsMd.
opts1 := EncOptions{
TagsMd: TagsForbidden,
}
em := mustEncMode(t, opts1)
opts2 := em.EncOptions()
if !reflect.DeepEqual(opts1, opts2) {
t.Errorf("em.EncOptions() = %#v, want %#v", opts2, opts1)
}
}
func TestCanonicalEncOptions(t *testing.T) { //nolint:dupl
wantSortMode := SortCanonical
wantShortestFloat := ShortestFloat16
wantNaNConvert := NaNConvert7e00
wantInfConvert := InfConvertFloat16
wantErrorMsg := "cbor: indefinite-length array isn't allowed"
em := mustEncMode(t, CanonicalEncOptions())
opts := em.EncOptions()
if opts.Sort != wantSortMode {
t.Errorf("em.EncOptions().Sort = %d, want %d", opts.Sort, wantSortMode)
}
if opts.ShortestFloat != wantShortestFloat {
t.Errorf("em.EncOptions().ShortestFloat = %d, want %d", opts.ShortestFloat, wantShortestFloat)
}
if opts.NaNConvert != wantNaNConvert {
t.Errorf("em.EncOptions().NaNConvert = %d, want %d", opts.NaNConvert, wantNaNConvert)
}
if opts.InfConvert != wantInfConvert {
t.Errorf("em.EncOptions().InfConvert = %d, want %d", opts.InfConvert, wantInfConvert)
}
enc := em.NewEncoder(io.Discard)
err := enc.StartIndefiniteArray()
checkError[error](t, "Encoder.StartIndefiniteArray", err, wantErrorMsg)
}
func TestCTAP2EncOptions(t *testing.T) { //nolint:dupl
wantSortMode := SortCTAP2
wantShortestFloat := ShortestFloatNone
wantNaNConvert := NaNConvertNone
wantInfConvert := InfConvertNone
wantErrorMsg := "cbor: indefinite-length array isn't allowed"
em := mustEncMode(t, CTAP2EncOptions())
opts := em.EncOptions()
if opts.Sort != wantSortMode {
t.Errorf("em.EncOptions().Sort = %d, want %d", opts.Sort, wantSortMode)
}
if opts.ShortestFloat != wantShortestFloat {
t.Errorf("em.EncOptions().ShortestFloat = %d, want %d", opts.ShortestFloat, wantShortestFloat)
}
if opts.NaNConvert != wantNaNConvert {
t.Errorf("em.EncOptions().NaNConvert = %d, want %d", opts.NaNConvert, wantNaNConvert)
}
if opts.InfConvert != wantInfConvert {
t.Errorf("em.EncOptions().InfConvert = %d, want %d", opts.InfConvert, wantInfConvert)
}
enc := em.NewEncoder(io.Discard)
err := enc.StartIndefiniteArray()
checkError[error](t, "Encoder.StartIndefiniteArray", err, wantErrorMsg)
}
func TestCoreDetEncOptions(t *testing.T) { //nolint:dupl
wantSortMode := SortCoreDeterministic
wantShortestFloat := ShortestFloat16
wantNaNConvert := NaNConvert7e00
wantInfConvert := InfConvertFloat16
wantErrorMsg := "cbor: indefinite-length array isn't allowed"
em := mustEncMode(t, CoreDetEncOptions())
opts := em.EncOptions()
if opts.Sort != wantSortMode {
t.Errorf("em.EncOptions().Sort = %d, want %d", opts.Sort, wantSortMode)
}
if opts.ShortestFloat != wantShortestFloat {
t.Errorf("em.EncOptions().ShortestFloat = %d, want %d", opts.ShortestFloat, wantShortestFloat)
}
if opts.NaNConvert != wantNaNConvert {
t.Errorf("em.EncOptions().NaNConvert = %d, want %d", opts.NaNConvert, wantNaNConvert)
}
if opts.InfConvert != wantInfConvert {
t.Errorf("em.EncOptions().InfConvert = %d, want %d", opts.InfConvert, wantInfConvert)
}
enc := em.NewEncoder(io.Discard)
err := enc.StartIndefiniteArray()
checkError[error](t, "Encoder.StartIndefiniteArray", err, wantErrorMsg)
}
func TestPreferredUnsortedEncOptions(t *testing.T) {
wantSortMode := SortNone
wantShortestFloat := ShortestFloat16
wantNaNConvert := NaNConvert7e00
wantInfConvert := InfConvertFloat16
em := mustEncMode(t, PreferredUnsortedEncOptions())
opts := em.EncOptions()
if opts.Sort != wantSortMode {
t.Errorf("em.EncOptions().Sort = %d, want %d", opts.Sort, wantSortMode)
}
if opts.ShortestFloat != wantShortestFloat {
t.Errorf("em.EncOptions().ShortestFloat = %d, want %d", opts.ShortestFloat, wantShortestFloat)
}
if opts.NaNConvert != wantNaNConvert {
t.Errorf("em.EncOptions().NaNConvert = %d, want %d", opts.NaNConvert, wantNaNConvert)
}
if opts.InfConvert != wantInfConvert {
t.Errorf("em.EncOptions().InfConvert = %d, want %d", opts.InfConvert, wantInfConvert)
}
enc := em.NewEncoder(io.Discard)
err := enc.StartIndefiniteArray()
checkNoError(t, "Encoder.StartIndefiniteArray", err)
}
type stubTranscoder struct{}
func (stubTranscoder) Transcode(io.Writer, io.Reader) error {
return nil
}
func TestEncOptions(t *testing.T) {
opts1 := EncOptions{
Sort: SortBytewiseLexical,
ShortestFloat: ShortestFloat16,
NaNConvert: NaNConvertPreserveSignal,
InfConvert: InfConvertNone,
BigIntConvert: BigIntConvertNone,
Time: TimeRFC3339Nano,
TimeTag: EncTagRequired,
IndefLength: IndefLengthForbidden,
NilContainers: NilContainerAsEmpty,
TagsMd: TagsAllowed,
OmitEmpty: OmitEmptyGoValue,
String: StringToByteString,
FieldName: FieldNameToByteString,
ByteSliceLaterFormat: ByteSliceLaterFormatBase16,
ByteArray: ByteArrayToArray,
BinaryMarshaler: BinaryMarshalerNone,
TextMarshaler: TextMarshalerTextString,
JSONMarshalerTranscoder: stubTranscoder{},
}
ov := reflect.ValueOf(opts1)
for i := range ov.NumField() {
fv := ov.Field(i)
if fv.IsZero() {
fn := ov.Type().Field(i).Name
if fn == "TagsMd" {
// Roundtripping non-zero values for TagsMd is tested separately
// since the non-zero value (TagsForbidden) is incompatible with the
// non-zero value for other options (e.g. TimeTag).
continue
}
if fn == "ToIndefArrayStructTag" {
// Roundtripping non-zero values for ToIndefArrayStructTag is tested
// separately since the non-zero value (ToIndefArrayStructTagAllowed)
// is incompatible with IndefLengthForbidden, which is the non-zero
// value used by IndefLength above.
continue
}
t.Errorf("options field %q is unset or set to the zero value for its type", fn)
}
}
em := mustEncMode(t, opts1)
opts2 := em.EncOptions()
if !reflect.DeepEqual(opts1, opts2) {
t.Errorf("em.EncOptions() = %#v, want %#v", opts2, opts1)
}
}