blob: 2e76b514c2c066da9b21de09af1aa649eb191d74 [file] [log] [blame]
Joe Tsai3ab648c2018-08-15 14:41:30 -07001// Copyright 2018 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package pack
6
7import (
8 "bytes"
9 "encoding/hex"
10 "fmt"
11 "math"
12 "testing"
13
Joe Tsaid8881392019-06-06 13:01:53 -070014 "github.com/google/go-cmp/cmp"
15 "google.golang.org/protobuf/encoding/prototext"
16 pdesc "google.golang.org/protobuf/reflect/protodesc"
Damien Neile89e6242019-05-13 23:55:40 -070017 pref "google.golang.org/protobuf/reflect/protoreflect"
Joe Tsaid8881392019-06-06 13:01:53 -070018
19 "google.golang.org/protobuf/types/descriptorpb"
Joe Tsai3ab648c2018-08-15 14:41:30 -070020)
21
22var msgDesc = func() pref.MessageDescriptor {
Joe Tsaid8881392019-06-06 13:01:53 -070023 const s = `
24 name: "test.proto"
25 syntax: "proto2"
26 message_type: [{
27 name: "Message"
28 field: [
Joe Tsai15076352019-07-02 15:19:08 -070029 {name:"f1" number:1 label:LABEL_REPEATED type:TYPE_BOOL options:{packed:true}},
30 {name:"f2" number:2 label:LABEL_REPEATED type:TYPE_INT64 options:{packed:true}},
31 {name:"f3" number:3 label:LABEL_REPEATED type:TYPE_SINT64 options:{packed:true}},
32 {name:"f4" number:4 label:LABEL_REPEATED type:TYPE_UINT64 options:{packed:true}},
33 {name:"f5" number:5 label:LABEL_REPEATED type:TYPE_FIXED32 options:{packed:true}},
34 {name:"f6" number:6 label:LABEL_REPEATED type:TYPE_SFIXED32 options:{packed:true}},
35 {name:"f7" number:7 label:LABEL_REPEATED type:TYPE_FLOAT options:{packed:true}},
36 {name:"f8" number:8 label:LABEL_REPEATED type:TYPE_FIXED64 options:{packed:true}},
37 {name:"f9" number:9 label:LABEL_REPEATED type:TYPE_SFIXED64 options:{packed:true}},
38 {name:"f10" number:10 label:LABEL_REPEATED type:TYPE_DOUBLE options:{packed:true}},
39 {name:"f11" number:11 label:LABEL_OPTIONAL type:TYPE_STRING},
40 {name:"f12" number:12 label:LABEL_OPTIONAL type:TYPE_BYTES},
41 {name:"f13" number:13 label:LABEL_OPTIONAL type:TYPE_MESSAGE type_name:".Message"},
42 {name:"f14" number:14 label:LABEL_OPTIONAL type:TYPE_GROUP type_name:".Message.F14"}
Joe Tsaid8881392019-06-06 13:01:53 -070043 ]
Joe Tsai15076352019-07-02 15:19:08 -070044 nested_type: [{name: "F14"}]
Joe Tsaid8881392019-06-06 13:01:53 -070045 }]
46 `
47 pb := new(descriptorpb.FileDescriptorProto)
48 if err := prototext.Unmarshal([]byte(s), pb); err != nil {
49 panic(err)
50 }
51 fd, err := pdesc.NewFile(pb, nil)
Joe Tsai3ab648c2018-08-15 14:41:30 -070052 if err != nil {
53 panic(err)
54 }
Joe Tsaid8881392019-06-06 13:01:53 -070055 return fd.Messages().Get(0)
Joe Tsai3ab648c2018-08-15 14:41:30 -070056}()
57
Joe Tsai3ab648c2018-08-15 14:41:30 -070058// dhex decodes a hex-string and returns the bytes and panics if s is invalid.
59func dhex(s string) []byte {
60 b, err := hex.DecodeString(s)
61 if err != nil {
62 panic(err)
63 }
64 return b
65}
66
67func TestPack(t *testing.T) {
68 tests := []struct {
69 raw []byte
70 msg Message
71
72 wantOutCompact string
73 wantOutMulti string
74 wantOutSource string
75 }{{
76 raw: dhex("080088808080800002088280808080000a09010002828080808000"),
77 msg: Message{
78 Tag{1, VarintType}, Bool(false),
79 Denormalized{5, Tag{1, VarintType}}, Uvarint(2),
80 Tag{1, VarintType}, Denormalized{5, Uvarint(2)},
81 Tag{1, BytesType}, LengthPrefix{Bool(true), Bool(false), Uvarint(2), Denormalized{5, Uvarint(2)}},
82 },
83 wantOutSource: `pack.Message{
84 pack.Tag{1, pack.VarintType}, pack.Bool(false),
85 pack.Denormalized{+5, pack.Tag{1, pack.VarintType}}, pack.Uvarint(2),
86 pack.Tag{1, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(2)},
87 pack.Tag{1, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Uvarint(2), pack.Denormalized{+5, pack.Uvarint(2)}},
88}`,
89 }, {
90 raw: dhex("100010828080808000121980808080808080808001ffffffffffffffff7f828080808000"),
91 msg: Message{
92 Tag{2, VarintType}, Varint(0),
93 Tag{2, VarintType}, Denormalized{5, Varint(2)},
94 Tag{2, BytesType}, LengthPrefix{Varint(math.MinInt64), Varint(math.MaxInt64), Denormalized{5, Varint(2)}},
95 },
96 wantOutCompact: `Message{Tag{2, Varint}, Varint(0), Tag{2, Varint}, Denormalized{+5, Varint(2)}, Tag{2, Bytes}, LengthPrefix{Varint(-9223372036854775808), Varint(9223372036854775807), Denormalized{+5, Varint(2)}}}`,
97 }, {
98 raw: dhex("1801188180808080001a1affffffffffffffffff01feffffffffffffffff01818080808000"),
99 msg: Message{
100 Tag{3, VarintType}, Svarint(-1),
101 Tag{3, VarintType}, Denormalized{5, Svarint(-1)},
102 Tag{3, BytesType}, LengthPrefix{Svarint(math.MinInt64), Svarint(math.MaxInt64), Denormalized{5, Svarint(-1)}},
103 },
104 wantOutMulti: `Message{
105 Tag{3, Varint}, Svarint(-1),
106 Tag{3, Varint}, Denormalized{+5, Svarint(-1)},
107 Tag{3, Bytes}, LengthPrefix{Svarint(-9223372036854775808), Svarint(9223372036854775807), Denormalized{+5, Svarint(-1)}},
108}`,
109 }, {
110 raw: dhex("200120818080808000221100ffffffffffffffffff01818080808000"),
111 msg: Message{
112 Tag{4, VarintType}, Uvarint(+1),
113 Tag{4, VarintType}, Denormalized{5, Uvarint(+1)},
114 Tag{4, BytesType}, LengthPrefix{Uvarint(0), Uvarint(math.MaxUint64), Denormalized{5, Uvarint(+1)}},
115 },
116 wantOutSource: `pack.Message{
117 pack.Tag{4, pack.VarintType}, pack.Uvarint(1),
118 pack.Tag{4, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(1)},
119 pack.Tag{4, pack.BytesType}, pack.LengthPrefix{pack.Uvarint(0), pack.Uvarint(18446744073709551615), pack.Denormalized{+5, pack.Uvarint(1)}},
120}`,
121 }, {
122 raw: dhex("2d010000002a0800000000ffffffff"),
123 msg: Message{
124 Tag{5, Fixed32Type}, Uint32(+1),
125 Tag{5, BytesType}, LengthPrefix{Uint32(0), Uint32(math.MaxUint32)},
126 },
127 wantOutCompact: `Message{Tag{5, Fixed32}, Uint32(1), Tag{5, Bytes}, LengthPrefix{Uint32(0), Uint32(4294967295)}}`,
128 }, {
129 raw: dhex("35ffffffff320800000080ffffff7f"),
130 msg: Message{
131 Tag{6, Fixed32Type}, Int32(-1),
132 Tag{6, BytesType}, LengthPrefix{Int32(math.MinInt32), Int32(math.MaxInt32)},
133 },
134 wantOutMulti: `Message{
135 Tag{6, Fixed32}, Int32(-1),
136 Tag{6, Bytes}, LengthPrefix{Int32(-2147483648), Int32(2147483647)},
137}`,
138 }, {
Damien Neil94293922019-07-17 16:53:14 -0700139 raw: dhex("3ddb0f49403a1001000000ffff7f7f0000807f000080ff"),
Joe Tsai3ab648c2018-08-15 14:41:30 -0700140 msg: Message{
141 Tag{7, Fixed32Type}, Float32(math.Pi),
Damien Neil94293922019-07-17 16:53:14 -0700142 Tag{7, BytesType}, LengthPrefix{Float32(math.SmallestNonzeroFloat32), Float32(math.MaxFloat32), Float32(math.Inf(+1)), Float32(math.Inf(-1))},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700143 },
144 wantOutSource: `pack.Message{
145 pack.Tag{7, pack.Fixed32Type}, pack.Float32(3.1415927),
Damien Neil94293922019-07-17 16:53:14 -0700146 pack.Tag{7, pack.BytesType}, pack.LengthPrefix{pack.Float32(1e-45), pack.Float32(3.4028235e+38), pack.Float32(math.Inf(+1)), pack.Float32(math.Inf(-1))},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700147}`,
148 }, {
149 raw: dhex("41010000000000000042100000000000000000ffffffffffffffff"),
150 msg: Message{
151 Tag{8, Fixed64Type}, Uint64(+1),
152 Tag{8, BytesType}, LengthPrefix{Uint64(0), Uint64(math.MaxUint64)},
153 },
154 wantOutCompact: `Message{Tag{8, Fixed64}, Uint64(1), Tag{8, Bytes}, LengthPrefix{Uint64(0), Uint64(18446744073709551615)}}`,
155 }, {
156 raw: dhex("49ffffffffffffffff4a100000000000000080ffffffffffffff7f"),
157 msg: Message{
158 Tag{9, Fixed64Type}, Int64(-1),
159 Tag{9, BytesType}, LengthPrefix{Int64(math.MinInt64), Int64(math.MaxInt64)},
160 },
161 wantOutMulti: `Message{
162 Tag{9, Fixed64}, Int64(-1),
163 Tag{9, Bytes}, LengthPrefix{Int64(-9223372036854775808), Int64(9223372036854775807)},
164}`,
165 }, {
Damien Neil94293922019-07-17 16:53:14 -0700166 raw: dhex("51182d4454fb21094052200100000000000000ffffffffffffef7f000000000000f07f000000000000f0ff"),
Joe Tsai3ab648c2018-08-15 14:41:30 -0700167 msg: Message{
168 Tag{10, Fixed64Type}, Float64(math.Pi),
Damien Neil94293922019-07-17 16:53:14 -0700169 Tag{10, BytesType}, LengthPrefix{Float64(math.SmallestNonzeroFloat64), Float64(math.MaxFloat64), Float64(math.Inf(+1)), Float64(math.Inf(-1))},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700170 },
171 wantOutMulti: `Message{
172 Tag{10, Fixed64}, Float64(3.141592653589793),
Damien Neil94293922019-07-17 16:53:14 -0700173 Tag{10, Bytes}, LengthPrefix{Float64(5e-324), Float64(1.7976931348623157e+308), Float64(+Inf), Float64(-Inf)},
Joe Tsai3ab648c2018-08-15 14:41:30 -0700174}`,
175 }, {
176 raw: dhex("5a06737472696e675a868080808000737472696e67"),
177 msg: Message{
178 Tag{11, BytesType}, String("string"),
179 Tag{11, BytesType}, Denormalized{+5, String("string")},
180 },
181 wantOutCompact: `Message{Tag{11, Bytes}, String("string"), Tag{11, Bytes}, Denormalized{+5, String("string")}}`,
182 }, {
183 raw: dhex("62056279746573628580808080006279746573"),
184 msg: Message{
185 Tag{12, BytesType}, Bytes("bytes"),
186 Tag{12, BytesType}, Denormalized{+5, Bytes("bytes")},
187 },
188 wantOutMulti: `Message{
189 Tag{12, Bytes}, Bytes("bytes"),
190 Tag{12, Bytes}, Denormalized{+5, Bytes("bytes")},
191}`,
192 }, {
193 raw: dhex("6a28a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
194 msg: Message{
195 Tag{13, BytesType}, LengthPrefix(Message{
196 Tag{100, VarintType}, Uvarint(math.MaxUint64),
197 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
198 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
199 Tag{100, BytesType}, Bytes("bytes"),
200 Tag{100, StartGroupType}, Tag{100, EndGroupType},
201 }),
202 },
203 wantOutSource: `pack.Message{
204 pack.Tag{13, pack.BytesType}, pack.LengthPrefix(pack.Message{
205 pack.Tag{100, pack.VarintType}, pack.Uvarint(18446744073709551615),
206 pack.Tag{100, pack.Fixed32Type}, pack.Uint32(4294967295),
207 pack.Tag{100, pack.Fixed64Type}, pack.Uint64(18446744073709551615),
208 pack.Tag{100, pack.BytesType}, pack.Bytes("bytes"),
209 pack.Tag{100, pack.StartGroupType},
210 pack.Tag{100, pack.EndGroupType},
211 }),
212}`,
213 }, {
214 raw: dhex("6aa88080808000a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
215 msg: Message{
216 Tag{13, BytesType}, Denormalized{5, LengthPrefix(Message{
217 Tag{100, VarintType}, Uvarint(math.MaxUint64),
218 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
219 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
220 Tag{100, BytesType}, Bytes("bytes"),
221 Tag{100, StartGroupType}, Tag{100, EndGroupType},
222 })},
223 },
224 wantOutCompact: `Message{Tag{13, Bytes}, Denormalized{+5, LengthPrefix(Message{Tag{100, Varint}, Uvarint(18446744073709551615), Tag{100, Fixed32}, Uint32(4294967295), Tag{100, Fixed64}, Uint64(18446744073709551615), Tag{100, Bytes}, Bytes("bytes"), Tag{100, StartGroup}, Tag{100, EndGroup}})}}`,
225 }, {
226 raw: dhex("73a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a40674"),
227 msg: Message{
228 Tag{14, StartGroupType}, Message{
229 Tag{100, VarintType}, Uvarint(math.MaxUint64),
230 Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
231 Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
232 Tag{100, BytesType}, Bytes("bytes"),
233 Tag{100, StartGroupType}, Tag{100, EndGroupType},
234 },
235 Tag{14, EndGroupType},
236 },
237 wantOutMulti: `Message{
238 Tag{14, StartGroup},
239 Message{
240 Tag{100, Varint}, Uvarint(18446744073709551615),
241 Tag{100, Fixed32}, Uint32(4294967295),
242 Tag{100, Fixed64}, Uint64(18446744073709551615),
243 Tag{100, Bytes}, Bytes("bytes"),
244 Tag{100, StartGroup},
245 Tag{100, EndGroup},
246 },
247 Tag{14, EndGroup},
248}`,
249 }, {
250 raw: dhex("d0faa972cd02a5f09051c2d8aa0d6a26a89c311eddef024b423c0f6f47b64227a1600db56e3f73d4113096c9a88e2b99f2d847516853d76a1a6e9811c85a2ab3"),
251 msg: Message{
252 Tag{29970346, VarintType}, Uvarint(333),
253 Tag{21268228, Fixed32Type}, Uint32(229300418),
254 Tag{13, BytesType}, LengthPrefix(Message{
255 Tag{100805, VarintType}, Uvarint(30),
256 Tag{5883, Fixed32Type}, Uint32(255607371),
257 Tag{13, Type(7)},
258 Raw("G\xb6B'\xa1`\r\xb5n?s\xd4\x110\x96ɨ\x8e+\x99\xf2\xd8GQhS"),
259 }),
260 Tag{1706, Type(7)},
261 Raw("\x1an\x98\x11\xc8Z*\xb3"),
262 },
263 }, {
264 raw: dhex("3d08d0e57f"),
265 msg: Message{
266 Tag{7, Fixed32Type}, Float32(math.Float32frombits(
267 // TODO: Remove workaround for compiler bug (see https://golang.org/issues/27193).
268 func() uint32 { return 0x7fe5d008 }(),
269 )),
270 },
271 wantOutSource: `pack.Message{
272 pack.Tag{7, pack.Fixed32Type}, pack.Float32(math.Float32frombits(0x7fe5d008)),
273}`,
274 }, {
275 raw: dhex("51a8d65110771bf97f"),
276 msg: Message{
277 Tag{10, Fixed64Type}, Float64(math.Float64frombits(0x7ff91b771051d6a8)),
278 },
279 wantOutSource: `pack.Message{
280 pack.Tag{10, pack.Fixed64Type}, pack.Float64(math.Float64frombits(0x7ff91b771051d6a8)),
281}`,
282 }, {
283 raw: dhex("ab2c14481ab3e9a76d937fb4dd5e6c616ef311f62b7fe888785fca5609ffe81c1064e50dd7a9edb408d317e2891c0d54c719446938d41ab0ccf8e61dc28b0ebb"),
284 msg: Message{
285 Tag{709, StartGroupType},
286 Tag{2, EndGroupType},
287 Tag{9, VarintType}, Uvarint(26),
288 Tag{28655254, StartGroupType},
289 Message{
290 Tag{2034, StartGroupType},
291 Tag{194006, EndGroupType},
292 },
293 Tag{13, EndGroupType},
294 Tag{12, Fixed64Type}, Uint64(9865274810543764334),
295 Tag{15, VarintType}, Uvarint(95),
296 Tag{1385, BytesType}, Bytes("\xff\xe8\x1c\x10d\xe5\rש"),
297 Tag{17229, Fixed32Type}, Uint32(2313295827),
298 Tag{3, EndGroupType},
299 Tag{1, Fixed32Type}, Uint32(1142540116),
300 Tag{13, Fixed64Type}, Uint64(2154683029754926136),
301 Tag{28856, BytesType},
302 Raw("\xbb"),
303 },
304 }, {
305 raw: dhex("29baa4ac1c1e0a20183393bac434b8d3559337ec940050038770eaa9937f98e4"),
306 msg: Message{
307 Tag{5, Fixed64Type}, Uint64(1738400580611384506),
308 Tag{6, StartGroupType},
309 Message{
310 Tag{13771682, StartGroupType},
311 Message{
312 Tag{175415, VarintType}, Uvarint(7059),
313 },
314 Denormalized{+1, Tag{333, EndGroupType}},
315 Tag{10, VarintType}, Uvarint(3),
316 Tag{1792, Type(7)},
317 Raw("꩓\u007f\x98\xe4"),
318 },
319 },
320 }}
321
322 equateFloatBits := cmp.Options{
323 cmp.Comparer(func(x, y Float32) bool {
324 return math.Float32bits(float32(x)) == math.Float32bits(float32(y))
325 }),
326 cmp.Comparer(func(x, y Float64) bool {
327 return math.Float64bits(float64(x)) == math.Float64bits(float64(y))
328 }),
329 }
330 for _, tt := range tests {
331 t.Run("", func(t *testing.T) {
332 var msg Message
333 raw := tt.msg.Marshal()
334 msg.UnmarshalDescriptor(tt.raw, msgDesc)
335
336 if !bytes.Equal(raw, tt.raw) {
337 t.Errorf("Marshal() mismatch:\ngot %x\nwant %x", raw, tt.raw)
338 }
339 if !cmp.Equal(msg, tt.msg, equateFloatBits) {
340 t.Errorf("Unmarshal() mismatch:\ngot %+v\nwant %+v", msg, tt.msg)
341 }
342 if got, want := tt.msg.Size(), len(tt.raw); got != want {
343 t.Errorf("Size() = %v, want %v", got, want)
344 }
345 if tt.wantOutCompact != "" {
346 gotOut := fmt.Sprintf("%v", tt.msg)
347 if string(gotOut) != tt.wantOutCompact {
348 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%v", gotOut, tt.wantOutCompact)
349 }
350 }
351 if tt.wantOutMulti != "" {
352 gotOut := fmt.Sprintf("%+v", tt.msg)
353 if string(gotOut) != tt.wantOutMulti {
354 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%+v", gotOut, tt.wantOutMulti)
355 }
356 }
357 if tt.wantOutSource != "" {
358 gotOut := fmt.Sprintf("%#v", tt.msg)
359 if string(gotOut) != tt.wantOutSource {
360 t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%#v", gotOut, tt.wantOutSource)
361 }
362 }
363 })
364 }
365}