-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtoindefarray_test.go
More file actions
331 lines (293 loc) · 9.69 KB
/
toindefarray_test.go
File metadata and controls
331 lines (293 loc) · 9.69 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
// Copyright (c) Faye Amacker. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
package cbor
import (
"bytes"
"reflect"
"strings"
"testing"
)
// allowToIndefArrayMode returns an EncMode with the toindefarray struct tag
// option allowed. It is a small helper to keep tests focused on behavior.
func allowToIndefArrayMode(t *testing.T) EncMode {
t.Helper()
em, err := EncOptions{ToIndefArrayStructTag: ToIndefArrayStructTagAllowed}.EncMode()
if err != nil {
t.Fatalf("EncMode() returned error: %v", err)
}
return em
}
// TestEncodeStructToIndefArrayBasic verifies the encoded bytes for a
// typical struct and that the data roundtrips through the default decoder.
func TestEncodeStructToIndefArrayBasic(t *testing.T) {
type S struct {
_ struct{} `cbor:",toindefarray"`
Data []byte
Count int
}
in := S{Data: []byte{0x73, 0xf7, 0x2d, 0xbe}, Count: 3}
em := allowToIndefArrayMode(t)
got, err := em.Marshal(in)
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [_ h'73f72dbe', 3]
want := []byte{0x9f, 0x44, 0x73, 0xf7, 0x2d, 0xbe, 0x03, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
// Roundtrip: decoder accepts both definite and indefinite arrays for
// toindefarray-tagged structs (the tag treats them the same way as
// `toarray` for decoding).
var out S
if err := Unmarshal(got, &out); err != nil {
t.Fatalf("Unmarshal returned error: %v", err)
}
if !reflect.DeepEqual(in, out) {
t.Errorf("roundtrip mismatch: got %+v, want %+v", out, in)
}
}
// TestEncodeStructToIndefArrayEmpty verifies that an empty struct produces
// exactly the two bytes 0x9f 0xff (no inner fields).
func TestEncodeStructToIndefArrayEmpty(t *testing.T) {
type Empty struct {
_ struct{} `cbor:",toindefarray"`
}
em := allowToIndefArrayMode(t)
in := Empty{}
got, err := em.Marshal(in)
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [_]
want := []byte{0x9f, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
var out Empty
if err := Unmarshal(got, &out); err != nil {
t.Fatalf("Unmarshal returned error: %v", err)
}
if !reflect.DeepEqual(in, out) {
t.Errorf("roundtrip mismatch: got %+v, want %+v", out, in)
}
}
// TestEncodeStructToIndefArrayNested verifies the byte layout and roundtrip
// of a struct with toindefarray containing another struct with toindefarray.
func TestEncodeStructToIndefArrayNested(t *testing.T) {
type Inner struct {
_ struct{} `cbor:",toindefarray"`
X []byte
Y int
}
type Outer struct {
_ struct{} `cbor:",toindefarray"`
Inner Inner
Z int
}
in := Outer{
Inner: Inner{X: []byte{0xab, 0xcd}, Y: 42},
Z: 7,
}
em := allowToIndefArrayMode(t)
got, err := em.Marshal(in)
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [_ [_ h'abcd', 42], 7]
want := []byte{0x9f, 0x9f, 0x42, 0xab, 0xcd, 0x18, 0x2a, 0xff, 0x07, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
var out Outer
if err := Unmarshal(got, &out); err != nil {
t.Fatalf("Unmarshal returned error: %v", err)
}
if !reflect.DeepEqual(in, out) {
t.Errorf("roundtrip mismatch: got %+v, want %+v", out, in)
}
}
// TestEncodeStructToIndefArrayForbiddenByDefault verifies that the default
// EncOptions (zero value) refuses to encode a struct tagged with
// `toindefarray`. The error message references the struct type and the
// option name to give callers a clear remediation path.
func TestEncodeStructToIndefArrayForbiddenByDefault(t *testing.T) {
type S struct {
_ struct{} `cbor:",toindefarray"`
A int
}
_, err := Marshal(S{A: 1})
if err == nil {
t.Fatal("expected error from default Marshal, got nil")
}
if !strings.Contains(err.Error(), "ToIndefArrayStructTag") {
t.Errorf("error should mention ToIndefArrayStructTag, got: %v", err)
}
}
// TestEncodeStructToIndefArrayMutuallyExclusive verifies that a struct
// declaring both `toarray` and `toindefarray` is rejected at encode time
// regardless of mode (the rejection comes from the type cache itself).
func TestEncodeStructToIndefArrayMutuallyExclusive(t *testing.T) {
type Bad struct {
_ struct{} `cbor:",toarray,toindefarray"`
A int
}
em := allowToIndefArrayMode(t)
_, err := em.Marshal(Bad{A: 1})
if err == nil {
t.Fatal("expected error for struct declaring both toarray and toindefarray, got nil")
}
if !strings.Contains(err.Error(), "toarray") || !strings.Contains(err.Error(), "toindefarray") {
t.Errorf("error should mention both options, got: %v", err)
}
}
// 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, err := opts.EncMode()
if err != nil {
t.Fatalf("EncMode() returned error: %v", err)
}
got := em.EncOptions()
if !reflect.DeepEqual(opts, got) {
t.Errorf("EncOptions round-trip mismatch:\n got: %#v\nwant: %#v", got, opts)
}
}
// TestInvalidToIndefArrayStructTagMode verifies that EncOptions rejects
// out-of-range values for ToIndefArrayStructTagMode at mode construction time.
func TestInvalidToIndefArrayStructTagMode(t *testing.T) {
for _, m := range []ToIndefArrayStructTagMode{-1, maxToIndefArrayStructTagMode, maxToIndefArrayStructTagMode + 1} {
_, err := EncOptions{ToIndefArrayStructTag: m}.EncMode()
if err == nil {
t.Errorf("expected error for mode %d, got nil", m)
continue
}
if !strings.Contains(err.Error(), "ToIndefArrayStructTag") {
t.Errorf("error for mode %d should mention ToIndefArrayStructTag, got: %v", m, err)
}
}
}
// TestToIndefArrayStructTagRejectsIndefLengthForbidden verifies that
// EncOptions rejects the combination of IndefLengthForbidden and
// ToIndefArrayStructTagAllowed at mode construction time, since the two
// settings are contradictory.
func TestToIndefArrayStructTagRejectsIndefLengthForbidden(t *testing.T) {
_, err := EncOptions{
IndefLength: IndefLengthForbidden,
ToIndefArrayStructTag: ToIndefArrayStructTagAllowed,
}.EncMode()
if err == nil {
t.Fatal("expected error, got nil")
}
if !strings.Contains(err.Error(), "ToIndefArrayStructTag") || !strings.Contains(err.Error(), "IndefLength") {
t.Errorf("error should mention both options, got: %v", err)
}
}
// TestEncodeStructToIndefArrayEmbeddedField verifies that fields promoted
// from an embedded struct are encoded as elements of the indefinite-length
// array, in the same order they would appear under toarray.
func TestEncodeStructToIndefArrayEmbeddedField(t *testing.T) {
type Inner struct {
A uint64
B uint64
}
type Outer struct {
_ struct{} `cbor:",toindefarray"`
Inner
C uint64
}
em := allowToIndefArrayMode(t)
got, err := em.Marshal(Outer{Inner: Inner{A: 1, B: 2}, C: 3})
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [_ 1, 2, 3] (promoted A, B, then C)
want := []byte{0x9f, 0x01, 0x02, 0x03, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
}
// TestEncodeStructToIndefArrayParentIndefChildArray verifies that a struct
// using toindefarray can contain a struct using toarray, and the inner
// struct still encodes as a definite-length array.
func TestEncodeStructToIndefArrayParentIndefChildArray(t *testing.T) {
type Inner struct {
_ struct{} `cbor:",toarray"`
A uint64
B uint64
}
type Outer struct {
_ struct{} `cbor:",toindefarray"`
Inner Inner
C uint64
}
em := allowToIndefArrayMode(t)
got, err := em.Marshal(Outer{Inner: Inner{A: 1, B: 2}, C: 3})
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [_ [1, 2], 3]
want := []byte{0x9f, 0x82, 0x01, 0x02, 0x03, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
}
// TestEncodeStructToIndefArrayParentArrayChildIndef verifies the symmetric
// case: a parent with toarray containing a child with toindefarray.
func TestEncodeStructToIndefArrayParentArrayChildIndef(t *testing.T) {
type Inner struct {
_ struct{} `cbor:",toindefarray"`
A uint64
B uint64
}
type Outer struct {
_ struct{} `cbor:",toarray"`
Inner Inner
C uint64
}
em := allowToIndefArrayMode(t)
got, err := em.Marshal(Outer{Inner: Inner{A: 1, B: 2}, C: 3})
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: [[_ 1, 2], 3]
want := []byte{0x82, 0x9f, 0x01, 0x02, 0xff, 0x03}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
}
// TestEncodeStructToIndefArrayWithCBORTag verifies that a struct registered
// with a CBOR tag through TagSet and tagged `toindefarray` produces the tag
// header followed by an indefinite-length array of its fields.
func TestEncodeStructToIndefArrayWithCBORTag(t *testing.T) {
type S struct {
_ struct{} `cbor:",toindefarray"`
Field1 uint64
Field2 uint64
}
tags := NewTagSet()
if err := tags.Add(
TagOptions{EncTag: EncTagRequired, DecTag: DecTagRequired},
reflect.TypeOf(S{}),
121,
); err != nil {
t.Fatalf("TagSet.Add returned error: %v", err)
}
em, err := EncOptions{ToIndefArrayStructTag: ToIndefArrayStructTagAllowed}.EncModeWithTags(tags)
if err != nil {
t.Fatalf("EncModeWithTags returned error: %v", err)
}
got, err := em.Marshal(S{Field1: 1, Field2: 2})
if err != nil {
t.Fatalf("Marshal returned error: %v", err)
}
// EDN: 121([_ 1, 2])
want := []byte{0xd8, 0x79, 0x9f, 0x01, 0x02, 0xff}
if !bytes.Equal(got, want) {
t.Errorf("got %x, want %x", got, want)
}
}