Debian Codegraph @

unmarshal_test.go

raw symbols SCIP
1 package bare
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 )
8
9 func TestUnmarshalValue(t *testing.T) {
10 var (
11 err error
12
13 u8 uint8
14 u16 uint16
15 u32 uint32
16 u64 uint64
17 u uint
18 vu Uint
19 i8 int8
20 i16 int16
21 i32 int32
22 i64 int64
23 i int
24 vi Int
25 f32 float32
26 f64 float64
27 b bool
28 str string
29 )
30
31 payloads := [][]byte{
32 []byte{0x42},
33 []byte{0xFE, 0xCA},
34 []byte{0xEF, 0xBE, 0xAD, 0xDE},
35 []byte{0xEF, 0xBE, 0xAD, 0xDE, 0xBE, 0xBA, 0xFE, 0xCA},
36 []byte{0xEF, 0xFD, 0xB6, 0xF5, 0x0D},
37 []byte{0xD6},
38 []byte{0x2E, 0xFB},
39 []byte{0xB2, 0x9E, 0x43, 0xFF},
40 []byte{0x4F, 0x0B, 0x6E, 0x9D, 0xAB, 0x23, 0xD4, 0xFF},
41 []byte{0x9B, 0x85, 0xE3, 0x0B},
42 []byte{0x71, 0x2D, 0xA7, 0x44},
43 []byte{0x9B, 0x6C, 0xC9, 0x20, 0xF0, 0x21, 0x3F, 0x42},
44 []byte{0x00, 0x01, 0x02},
45 []byte{0x1B, 0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81, 0xAB,
46 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0xE3, 0x80, 0x81, 0xE4, 0xB8,
47 0x96, 0xE7, 0x95, 0x8C, 0xEF, 0xBC, 0x81},
48 }
49
50 err = Unmarshal(payloads[0], &u8)
51 assert.Nil(t, err, "Expected Unmarshal to return without error")
52 assert.Equal(t, uint8(0x42), u8, "Expected Unmarshal to read 0x42")
53
54 err = Unmarshal(payloads[1], &u16)
55 assert.Nil(t, err, "Expected Unmarshal to return without error")
56 assert.Equal(t, uint16(0xCAFE), u16, "Expected Unmarshal to read 0xCAFE")
57
58 err = Unmarshal(payloads[2], &u32)
59 assert.Nil(t, err, "Expected Unmarshal to return without error")
60 assert.Equal(t, uint32(0xDEADBEEF), u32, "Expected Unmarshal to read 0xDEADBEEF")
61
62 err = Unmarshal(payloads[3], &u64)
63 assert.Nil(t, err, "Expected Unmarshal to return without error")
64 assert.Equal(t, uint64(0xCAFEBABEDEADBEEF), u64, "Expected Unmarshal to read 0xCAFEBABEDEADBEEF")
65
66 err = Unmarshal(payloads[4], &u)
67 assert.Nil(t, err, "Expected Unmarshal to return without error")
68 assert.Equal(t, uint(0xDEADBEEF), u, "Expected Unmarshal to read 0xDEADBEEF")
69
70 err = Unmarshal(payloads[4], &vu)
71 assert.Nil(t, err, "Expected Unmarshal to return without error")
72 assert.Equal(t, Uint(0xDEADBEEF), vu, "Expected Unmarshal to read 0xDEADBEEF")
73
74 err = Unmarshal(payloads[5], &i8)
75 assert.Nil(t, err, "Expected Unmarshal to return without error")
76 assert.Equal(t, int8(-42), i8, "Expected Unmarshal to read -42")
77
78 err = Unmarshal(payloads[6], &i16)
79 assert.Nil(t, err, "Expected Unmarshal to return without error")
80 assert.Equal(t, int16(-1234), i16, "Expected Unmarshal to read -1234")
81
82 err = Unmarshal(payloads[7], &i32)
83 assert.Nil(t, err, "Expected Unmarshal to return without error")
84 assert.Equal(t, int32(-12345678), i32, "Expected Unmarshal to read -12345678")
85
86 err = Unmarshal(payloads[8], &i64)
87 assert.Nil(t, err, "Expected Unmarshal to return without error")
88 assert.Equal(t, int64(-12345678987654321), i64, "Expected Unmarshal to read -12345678987654321")
89
90 err = Unmarshal(payloads[9], &i)
91 assert.Nil(t, err, "Expected Unmarshal to return without error")
92 assert.Equal(t, int(-12345678), i, "Expected Unmarshal to read -12345678")
93
94 err = Unmarshal(payloads[9], &vi)
95 assert.Nil(t, err, "Expected Unmarshal to return without error")
96 assert.Equal(t, Int(-12345678), vi, "Expected Unmarshal to read -12345678")
97
98 err = Unmarshal(payloads[10], &f32)
99 assert.Nil(t, err, "Expected Unmarshal to return without error")
100 assert.Equal(t, float32(1337.42), f32, "Expected Unmarshal to read 1337.42")
101
102 err = Unmarshal(payloads[11], &f64)
103 assert.Nil(t, err, "Expected Unmarshal to return without error")
104 assert.Equal(t, float64(133713371337.42424242), f64, "Expected Unmarshal to read 133713371337.42424242")
105
106 err = Unmarshal(payloads[12][0:], &b)
107 assert.Nil(t, err, "Expected Unmarshal to return without error")
108 assert.False(t, b, "Expected Unmarshal to read false")
109
110 err = Unmarshal(payloads[12][1:], &b)
111 assert.Nil(t, err, "Expected Unmarshal to return without error")
112 assert.True(t, b, "Expected Unmarshal to read true")
113
114 err = Unmarshal(payloads[12][2:], &b)
115 assert.EqualError(t, err, "Invalid bool value: 0x2")
116
117 err = Unmarshal(payloads[13], &str)
118 assert.Nil(t, err, "Expected Unmarshal to return without error")
119 assert.Equal(t, "こんにちは、世界!", str, "Expected Unmarshal to read 'こんにちは、世界!'")
120 }
121
122 func TestUnmarshalOptional(t *testing.T) {
123 var val *uint32
124 err := Unmarshal([]byte{0x00}, &val)
125 assert.Nil(t, err, "Expected Unmarshal to return without error")
126 assert.Nil(t, val, "Expected Unmarshal to read nil")
127
128 err = Unmarshal([]byte{0x01, 0xEF, 0xBE, 0xAD, 0xDE}, &val)
129 assert.Nil(t, err, "Expected Unmarshal to return without error")
130 assert.NotNil(t, val, "Expected Unmarshal to read non-nil value")
131 assert.Equal(t, uint32(0xDEADBEEF), *val, "Expected Unmarshal to read 0xDEADBEEF")
132
133 err = Unmarshal([]byte{0x02}, &val)
134 assert.EqualError(t, err, "Invalid optional value: 0x2")
135 }
136
137 func TestUnmarshalStruct(t *testing.T) {
138 type Coordinates struct{ X, Y, Z uint }
139 payload := []byte{0x01, 0x02, 0x03}
140 var coords Coordinates
141 err := Unmarshal(payload, &coords)
142 assert.Nil(t, err, "Expected Unmarshal to return without error")
143 assert.Equal(t, uint(1), coords.X, "Expected Unmarshal to read {1, 2, 3}")
144 assert.Equal(t, uint(2), coords.Y, "Expected Unmarshal to read {1, 2, 3}")
145 assert.Equal(t, uint(3), coords.Z, "Expected Unmarshal to read {1, 2, 3}")
146 }
147
148 func TestUnmarshalOmittedFields(t *testing.T) {
149 type Coordinates struct {
150 X uint
151 Y uint
152 Z uint `bare:"-"`
153 }
154 payload := []byte{0x01, 0x02}
155 var coords Coordinates
156 err := Unmarshal(payload, &coords)
157 assert.Nil(t, err, "Expected Unmarshal to return without error")
158 assert.Equal(t, uint(1), coords.X, "Expected Unmarshal to read {1, 2}")
159 assert.Equal(t, uint(2), coords.Y, "Expected Unmarshal to read {1, 2}")
160 assert.Equal(t, uint(0), coords.Z, "Expected Unmarshal to ignore field")
161 }
162
163 func TestUnmarshalArray(t *testing.T) {
164 var val [4]uint8
165 err := Unmarshal([]byte{0x11, 0x22, 0x33, 0x44}, &val)
166 assert.Nil(t, err, "Expected Unmarshal to return without error")
167 assert.Equal(t, uint8(0x11), val[0], "Expected Unmarshal to read 0x11")
168 assert.Equal(t, uint8(0x22), val[1], "Expected Unmarshal to read 0x22")
169 assert.Equal(t, uint8(0x33), val[2], "Expected Unmarshal to read 0x33")
170 assert.Equal(t, uint8(0x44), val[3], "Expected Unmarshal to read 0x44")
171 }
172
173 func TestUnmarshalSlice(t *testing.T) {
174 var val []uint8
175 err := Unmarshal([]byte{0x04, 0x11, 0x22, 0x33, 0x44}, &val)
176 assert.Nil(t, err, "Expected Unmarshal to return without error")
177 assert.Equal(t, 4, len(val), "Expected Unmarshal to read slice of length 4")
178 assert.Equal(t, uint8(0x11), val[0], "Expected Unmarshal to read 0x11")
179 assert.Equal(t, uint8(0x22), val[1], "Expected Unmarshal to read 0x22")
180 assert.Equal(t, uint8(0x33), val[2], "Expected Unmarshal to read 0x33")
181 assert.Equal(t, uint8(0x44), val[3], "Expected Unmarshal to read 0x44")
182
183 MaxArrayLength(64)
184 err = Unmarshal([]byte{100}, &val)
185 assert.EqualError(t, err, "Array length 100 exceeds configured limit of 64")
186 }
187
188 func TestUnmarshalMap(t *testing.T) {
189 var val map[uint8]uint8
190 payload := []byte{
191 0x03,
192 0x01, 0x11,
193 0x02, 0x22,
194 0x03, 0x33,
195 }
196 err := Unmarshal(payload, &val)
197 assert.Nil(t, err, "Expected Unmarshal to return without error")
198 assert.Equal(t, 3, len(val), "Expected Unmarshal to read map of length 3")
199 assert.Equal(t, uint8(0x11), val[uint8(0x01)], "Expected Unmarshal to read 0x01 -> 0x11")
200 assert.Equal(t, uint8(0x22), val[uint8(0x02)], "Expected Unmarshal to read 0x02 -> 0x22")
201 assert.Equal(t, uint8(0x33), val[uint8(0x03)], "Expected Unmarshal to read 0x03 -> 0x33")
202
203 t.Run("rejects duplicate keys", func(t *testing.T) {
204 payload = []byte{
205 0x02,
206 0x01, 0x13,
207 0x01, 0x37,
208 }
209 err = Unmarshal(payload, &val)
210 assert.EqualError(t, err, "Encountered duplicate map key: 1")
211 })
212
213 t.Run("respects size limits", func(t *testing.T) {
214 MaxMapSize(64)
215 payload = []byte{100}
216 err = Unmarshal(payload, &val)
217 assert.EqualError(t, err, "Map size 100 exceeds configured limit of 64")
218 })
219 }
220
221 func TestUnmarshalUnion(t *testing.T) {
222 var val NameAge
223 payload := []byte{0x00, 0x04, 0x4d, 0x61, 0x72, 0x79}
224 err := Unmarshal(payload, &val)
225 assert.Nil(t, err, "Expected Unmarshal to return without error")
226
227 name, ok := val.(*Name)
228 assert.True(t, ok)
229 assert.Equal(t, Name("Mary"), *name)
230
231 payload = []byte{0x01, 0x30}
232 err = Unmarshal(payload, &val)
233 assert.Nil(t, err, "Expected Unmarshal to return without error")
234
235 age, ok := val.(*Age)
236 assert.True(t, ok)
237 assert.Equal(t, Age(24), *age)
238
239 payload = []byte{0x13, 0x37}
240 err = Unmarshal(payload, &val)
241 assert.EqualError(t, err, "Invalid union tag 19 for type NameAge")
242 }
243
244 func TestUnmarshalCustom(t *testing.T) {
245 var val Custom
246 payload := []byte{0x0, 0x42}
247
248 err := Unmarshal(payload, &val)
249 assert.Nil(t, err, "Expected Unmarshal to return without error")
250 assert.Equal(t, Custom(0x42), val)
251 }