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