1
2
3
4
5 package proto_test
6
7 import (
8 "bytes"
9 "encoding/json"
10 "errors"
11 "fmt"
12 "log"
13 "math"
14 "math/rand"
15 "reflect"
16 "runtime/debug"
17 "strings"
18 "sync"
19 "testing"
20 "time"
21
22 "github.com/golang/protobuf/proto"
23 "google.golang.org/protobuf/testing/protopack"
24
25 pb2 "github.com/golang/protobuf/internal/testprotos/proto2_proto"
26 pb3 "github.com/golang/protobuf/internal/testprotos/proto3_proto"
27 tspb "github.com/golang/protobuf/ptypes/timestamp"
28 )
29
30 func initGoTestField() *pb2.GoTestField {
31 f := new(pb2.GoTestField)
32 f.Label = proto.String("label")
33 f.Type = proto.String("type")
34 return f
35 }
36
37
38
39
40 func initGoTest_RequiredGroup() *pb2.GoTest_RequiredGroup {
41 return &pb2.GoTest_RequiredGroup{
42 RequiredField: proto.String("required"),
43 }
44 }
45
46 func initGoTest_OptionalGroup() *pb2.GoTest_OptionalGroup {
47 return &pb2.GoTest_OptionalGroup{
48 RequiredField: proto.String("optional"),
49 }
50 }
51
52 func initGoTest_RepeatedGroup() *pb2.GoTest_RepeatedGroup {
53 return &pb2.GoTest_RepeatedGroup{
54 RequiredField: proto.String("repeated"),
55 }
56 }
57
58 func initGoTest(setdefaults bool) *pb2.GoTest {
59 pb := new(pb2.GoTest)
60 if setdefaults {
61 pb.F_BoolDefaulted = proto.Bool(pb2.Default_GoTest_F_BoolDefaulted)
62 pb.F_Int32Defaulted = proto.Int32(pb2.Default_GoTest_F_Int32Defaulted)
63 pb.F_Int64Defaulted = proto.Int64(pb2.Default_GoTest_F_Int64Defaulted)
64 pb.F_Fixed32Defaulted = proto.Uint32(pb2.Default_GoTest_F_Fixed32Defaulted)
65 pb.F_Fixed64Defaulted = proto.Uint64(pb2.Default_GoTest_F_Fixed64Defaulted)
66 pb.F_Uint32Defaulted = proto.Uint32(pb2.Default_GoTest_F_Uint32Defaulted)
67 pb.F_Uint64Defaulted = proto.Uint64(pb2.Default_GoTest_F_Uint64Defaulted)
68 pb.F_FloatDefaulted = proto.Float32(pb2.Default_GoTest_F_FloatDefaulted)
69 pb.F_DoubleDefaulted = proto.Float64(pb2.Default_GoTest_F_DoubleDefaulted)
70 pb.F_StringDefaulted = proto.String(pb2.Default_GoTest_F_StringDefaulted)
71 pb.F_BytesDefaulted = pb2.Default_GoTest_F_BytesDefaulted
72 pb.F_Sint32Defaulted = proto.Int32(pb2.Default_GoTest_F_Sint32Defaulted)
73 pb.F_Sint64Defaulted = proto.Int64(pb2.Default_GoTest_F_Sint64Defaulted)
74 pb.F_Sfixed32Defaulted = proto.Int32(pb2.Default_GoTest_F_Sfixed32Defaulted)
75 pb.F_Sfixed64Defaulted = proto.Int64(pb2.Default_GoTest_F_Sfixed64Defaulted)
76 }
77
78 pb.Kind = pb2.GoTest_TIME.Enum()
79 pb.RequiredField = initGoTestField()
80 pb.F_BoolRequired = proto.Bool(true)
81 pb.F_Int32Required = proto.Int32(3)
82 pb.F_Int64Required = proto.Int64(6)
83 pb.F_Fixed32Required = proto.Uint32(32)
84 pb.F_Fixed64Required = proto.Uint64(64)
85 pb.F_Uint32Required = proto.Uint32(3232)
86 pb.F_Uint64Required = proto.Uint64(6464)
87 pb.F_FloatRequired = proto.Float32(3232)
88 pb.F_DoubleRequired = proto.Float64(6464)
89 pb.F_StringRequired = proto.String("string")
90 pb.F_BytesRequired = []byte("bytes")
91 pb.F_Sint32Required = proto.Int32(-32)
92 pb.F_Sint64Required = proto.Int64(-64)
93 pb.F_Sfixed32Required = proto.Int32(-32)
94 pb.F_Sfixed64Required = proto.Int64(-64)
95 pb.Requiredgroup = initGoTest_RequiredGroup()
96
97 return pb
98 }
99
100 func overify(t *testing.T, pb *pb2.GoTest, want []byte) {
101 bb := new(proto.Buffer)
102 err := bb.Marshal(pb)
103 got := bb.Bytes()
104 if err != nil {
105 t.Logf("overify marshal-1 err = %v", err)
106 }
107 if !bytes.Equal(got, want) {
108 t.Fatalf("got %q\nwant %q", got, want)
109 }
110
111
112 pbd := new(pb2.GoTest)
113 err = bb.Unmarshal(pbd)
114 if err != nil {
115 t.Fatalf("overify unmarshal err = %v", err)
116 }
117 bb.Reset()
118 err = bb.Marshal(pbd)
119 got = bb.Bytes()
120 if err != nil {
121 t.Fatalf("overify marshal-2 err = %v", err)
122 }
123 if !bytes.Equal(got, want) {
124 t.Fatalf("got %q\nwant %q", got, want)
125 }
126 }
127
128
129
130
131
132 func isRequiredNotSetError(err error) bool {
133 e, ok := err.(interface{ RequiredNotSet() bool })
134 return ok && e.RequiredNotSet()
135 }
136
137
138 func TestNumericPrimitives(t *testing.T) {
139 for i := uint64(0); i < 1e6; i += 111 {
140 o := new(proto.Buffer)
141 if o.EncodeVarint(i) != nil {
142 t.Error("EncodeVarint")
143 break
144 }
145 x, e := o.DecodeVarint()
146 if e != nil {
147 t.Fatal("DecodeVarint")
148 }
149 if x != i {
150 t.Fatal("varint decode fail:", i, x)
151 }
152
153 o.Reset()
154 if o.EncodeFixed32(i) != nil {
155 t.Fatal("encFixed32")
156 }
157 x, e = o.DecodeFixed32()
158 if e != nil {
159 t.Fatal("decFixed32")
160 }
161 if x != i {
162 t.Fatal("fixed32 decode fail:", i, x)
163 }
164
165 o.Reset()
166 if o.EncodeFixed64(i*1234567) != nil {
167 t.Error("encFixed64")
168 break
169 }
170 x, e = o.DecodeFixed64()
171 if e != nil {
172 t.Error("decFixed64")
173 break
174 }
175 if x != i*1234567 {
176 t.Error("fixed64 decode fail:", i*1234567, x)
177 break
178 }
179
180 o.Reset()
181 i32 := int32(i - 12345)
182 if o.EncodeZigzag32(uint64(i32)) != nil {
183 t.Fatal("EncodeZigzag32")
184 }
185 x, e = o.DecodeZigzag32()
186 if e != nil {
187 t.Fatal("DecodeZigzag32")
188 }
189 if x != uint64(uint32(i32)) {
190 t.Fatal("zigzag32 decode fail:", i32, x)
191 }
192
193 o.Reset()
194 i64 := int64(i - 12345)
195 if o.EncodeZigzag64(uint64(i64)) != nil {
196 t.Fatal("EncodeZigzag64")
197 }
198 x, e = o.DecodeZigzag64()
199 if e != nil {
200 t.Fatal("DecodeZigzag64")
201 }
202 if x != uint64(i64) {
203 t.Fatal("zigzag64 decode fail:", i64, x)
204 }
205 }
206 }
207
208
209 type fakeMarshaler struct {
210 b []byte
211 err error
212 }
213
214 func (f *fakeMarshaler) Marshal() ([]byte, error) { return f.b, f.err }
215 func (f *fakeMarshaler) String() string { return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) }
216 func (f *fakeMarshaler) ProtoMessage() {}
217 func (f *fakeMarshaler) Reset() {}
218
219 type msgWithFakeMarshaler struct {
220 M *fakeMarshaler `protobuf:"bytes,1,opt,name=fake"`
221 }
222
223 func (m *msgWithFakeMarshaler) String() string { return proto.CompactTextString(m) }
224 func (m *msgWithFakeMarshaler) ProtoMessage() {}
225 func (m *msgWithFakeMarshaler) Reset() {}
226
227
228 func TestMarshalerEncoding(t *testing.T) {
229 tests := []struct {
230 name string
231 m proto.Message
232 want []byte
233 errType reflect.Type
234 }{
235 {
236 name: "Marshaler that fails",
237 m: &fakeMarshaler{
238 err: errors.New("some marshal err"),
239 b: []byte{5, 6, 7},
240 },
241 errType: reflect.TypeOf(errors.New("some marshal err")),
242 },
243 {
244 name: "Marshaler that fails with RequiredNotSetError",
245 m: &msgWithFakeMarshaler{
246 M: &fakeMarshaler{
247 err: &proto.RequiredNotSetError{},
248 b: []byte{5, 6, 7},
249 },
250 },
251 errType: reflect.TypeOf(&proto.RequiredNotSetError{}),
252 },
253 {
254 name: "Marshaler that succeeds",
255 m: &fakeMarshaler{
256 b: []byte{0, 1, 2, 3, 4, 127, 255},
257 },
258 want: []byte{0, 1, 2, 3, 4, 127, 255},
259 },
260 }
261 for _, test := range tests {
262 t.Run(test.name, func(t *testing.T) {
263 b := proto.NewBuffer(nil)
264 err := b.Marshal(test.m)
265 if reflect.TypeOf(err) != test.errType {
266 t.Errorf("got err %T(%v) wanted %T", err, err, test.errType)
267 }
268 if err != nil {
269 return
270 }
271 if !reflect.DeepEqual(test.want, b.Bytes()) {
272 t.Errorf("got bytes %v wanted %v", b.Bytes(), test.want)
273 }
274 if size := proto.Size(test.m); size != len(b.Bytes()) {
275 t.Errorf("Size(_) = %v, but marshaled to %v bytes", size, len(b.Bytes()))
276 }
277
278 m, mErr := proto.Marshal(test.m)
279 if !bytes.Equal(b.Bytes(), m) {
280 t.Errorf("Marshal returned %v, but (*Buffer).Marshal wrote %v", m, b.Bytes())
281 }
282 if !reflect.DeepEqual(err, mErr) {
283 t.Errorf("Marshal err = %v, but (*Buffer).Marshal returned %v", mErr, err)
284 }
285 })
286 }
287 }
288
289
290 func TestBufferMarshalAllocs(t *testing.T) {
291 value := &pb2.OtherMessage{Key: proto.Int64(1)}
292 msg := &pb2.MyMessage{Count: proto.Int32(1), Others: []*pb2.OtherMessage{value}}
293
294 for _, prealloc := range []int{0, 100, 10000} {
295 const count = 1000
296 var b proto.Buffer
297 s := make([]byte, 0, proto.Size(msg))
298 marshalAllocs := testing.AllocsPerRun(count, func() {
299 b.SetBuf(s)
300 err := b.Marshal(msg)
301 if err != nil {
302 t.Errorf("Marshal err = %q", err)
303 }
304 })
305
306 b.SetBuf(make([]byte, 0, prealloc))
307 bufferAllocs := testing.AllocsPerRun(count, func() {
308 err := b.Marshal(msg)
309 if err != nil {
310 t.Errorf("Marshal err = %q", err)
311 }
312 })
313
314 if marshalAllocs != bufferAllocs {
315 t.Errorf("%v allocs/op when writing to a preallocated buffer", marshalAllocs)
316 t.Errorf("%v allocs/op when repeatedly appending to a buffer", bufferAllocs)
317 t.Errorf("expect amortized allocs/op to be identical")
318 }
319 }
320 }
321
322
323 func TestBytesPrimitives(t *testing.T) {
324 bb := new(proto.Buffer)
325 want := []byte("now is the time")
326 if err := bb.EncodeRawBytes(want); err != nil {
327 t.Errorf("EncodeRawBytes error: %v", err)
328 }
329 got, err := bb.DecodeRawBytes(false)
330 if err != nil {
331 t.Errorf("DecodeRawBytes error: %v", err)
332 }
333 if !bytes.Equal(got, want) {
334 t.Errorf("got %q\nwant %q", got, want)
335 }
336 }
337
338
339 func TestStringPrimitives(t *testing.T) {
340 bb := new(proto.Buffer)
341 want := "now is the time"
342 if err := bb.EncodeStringBytes(want); err != nil {
343 t.Errorf("EncodeStringBytes error: %v", err)
344 }
345 got, err := bb.DecodeStringBytes()
346 if err != nil {
347 t.Errorf("DecodeStringBytes error: %v", err)
348 }
349 if got != want {
350 t.Errorf("got %q\nwant %q", got, want)
351 }
352 }
353
354
355 func TestRequiredBit(t *testing.T) {
356 o := new(proto.Buffer)
357 pb := new(pb2.GoTest)
358 err := o.Marshal(pb)
359 if err == nil {
360 t.Error("did not catch missing required fields")
361 } else if !strings.Contains(err.Error(), "Kind") {
362 t.Error("wrong error type:", err)
363 }
364 }
365
366
367
368
369
370 func checkInitialized(pb *pb2.GoTest, t *testing.T) {
371 switch {
372 case pb.F_BoolDefaulted != nil:
373 t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
374 case pb.F_Int32Defaulted != nil:
375 t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
376 case pb.F_Int64Defaulted != nil:
377 t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
378 case pb.F_Fixed32Defaulted != nil:
379 t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
380 case pb.F_Fixed64Defaulted != nil:
381 t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
382 case pb.F_Uint32Defaulted != nil:
383 t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
384 case pb.F_Uint64Defaulted != nil:
385 t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
386 case pb.F_FloatDefaulted != nil:
387 t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
388 case pb.F_DoubleDefaulted != nil:
389 t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
390 case pb.F_StringDefaulted != nil:
391 t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
392 case pb.F_BytesDefaulted != nil:
393 t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
394 case pb.F_Sint32Defaulted != nil:
395 t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
396 case pb.F_Sint64Defaulted != nil:
397 t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
398 }
399 }
400
401
402 func TestReset(t *testing.T) {
403 pb := initGoTest(true)
404
405 pb.F_BoolDefaulted = proto.Bool(false)
406 pb.F_Int32Defaulted = proto.Int32(237)
407 pb.F_Int64Defaulted = proto.Int64(12346)
408 pb.F_Fixed32Defaulted = proto.Uint32(32000)
409 pb.F_Fixed64Defaulted = proto.Uint64(666)
410 pb.F_Uint32Defaulted = proto.Uint32(323232)
411 pb.F_Uint64Defaulted = nil
412 pb.F_FloatDefaulted = nil
413 pb.F_DoubleDefaulted = proto.Float64(0)
414 pb.F_StringDefaulted = proto.String("gotcha")
415 pb.F_BytesDefaulted = []byte("asdfasdf")
416 pb.F_Sint32Defaulted = proto.Int32(123)
417 pb.F_Sint64Defaulted = proto.Int64(789)
418 pb.Reset()
419 checkInitialized(pb, t)
420 }
421
422
423 func TestEncodeDecode1(t *testing.T) {
424 pb := initGoTest(false)
425 overify(t, pb,
426 protopack.Message{
427 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
428 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
429 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
430 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
431 }),
432 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
433 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
434 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
435 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
436 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
437 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
438 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
439 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
440 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
441 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
442 protopack.Tag{70, protopack.StartGroupType},
443 protopack.Message{
444 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
445 },
446 protopack.Tag{70, protopack.EndGroupType},
447 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
448 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
449 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
450 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
451 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
452 }.Marshal())
453 }
454
455
456 func TestEncodeDecode2(t *testing.T) {
457 pb := initGoTest(true)
458 overify(t, pb,
459 protopack.Message{
460 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
461 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
462 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
463 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
464 }),
465 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
466 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
467 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
468 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
469 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
470 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
471 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
472 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
473 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
474 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
475 protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
476 protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
477 protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
478 protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
479 protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
480 protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
481 protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
482 protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
483 protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
484 protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
485 protopack.Tag{70, protopack.StartGroupType},
486 protopack.Message{
487 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
488 },
489 protopack.Tag{70, protopack.EndGroupType},
490 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
491 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
492 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
493 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
494 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
495 protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
496 protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
497 protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
498 protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
499 protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
500 }.Marshal())
501 }
502
503
504 func TestEncodeDecode3(t *testing.T) {
505 pb := initGoTest(false)
506 pb.F_BoolDefaulted = proto.Bool(true)
507 pb.F_Int32Defaulted = proto.Int32(32)
508 pb.F_Int64Defaulted = proto.Int64(64)
509 pb.F_Fixed32Defaulted = proto.Uint32(320)
510 pb.F_Fixed64Defaulted = proto.Uint64(640)
511 pb.F_Uint32Defaulted = proto.Uint32(3200)
512 pb.F_Uint64Defaulted = proto.Uint64(6400)
513 pb.F_FloatDefaulted = proto.Float32(314159)
514 pb.F_DoubleDefaulted = proto.Float64(271828)
515 pb.F_StringDefaulted = proto.String("hello, \"world!\"\n")
516 pb.F_BytesDefaulted = []byte("Bignose")
517 pb.F_Sint32Defaulted = proto.Int32(-32)
518 pb.F_Sint64Defaulted = proto.Int64(-64)
519 pb.F_Sfixed32Defaulted = proto.Int32(-32)
520 pb.F_Sfixed64Defaulted = proto.Int64(-64)
521
522 overify(t, pb,
523 protopack.Message{
524 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
525 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
526 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
527 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
528 }),
529 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
530 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
531 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
532 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
533 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
534 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
535 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
536 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
537 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
538 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
539 protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
540 protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
541 protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
542 protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
543 protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
544 protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
545 protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
546 protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
547 protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
548 protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
549 protopack.Tag{70, protopack.StartGroupType},
550 protopack.Message{
551 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
552 },
553 protopack.Tag{70, protopack.EndGroupType},
554 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
555 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
556 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
557 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
558 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
559 protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
560 protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
561 protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
562 protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
563 protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
564 }.Marshal())
565 }
566
567
568 func TestEncodeDecode4(t *testing.T) {
569 pb := initGoTest(true)
570 pb.Table = proto.String("hello")
571 pb.Param = proto.Int32(7)
572 pb.OptionalField = initGoTestField()
573 pb.F_BoolOptional = proto.Bool(true)
574 pb.F_Int32Optional = proto.Int32(32)
575 pb.F_Int64Optional = proto.Int64(64)
576 pb.F_Fixed32Optional = proto.Uint32(3232)
577 pb.F_Fixed64Optional = proto.Uint64(6464)
578 pb.F_Uint32Optional = proto.Uint32(323232)
579 pb.F_Uint64Optional = proto.Uint64(646464)
580 pb.F_FloatOptional = proto.Float32(32.)
581 pb.F_DoubleOptional = proto.Float64(64.)
582 pb.F_StringOptional = proto.String("hello")
583 pb.F_BytesOptional = []byte("Bignose")
584 pb.F_Sint32Optional = proto.Int32(-32)
585 pb.F_Sint64Optional = proto.Int64(-64)
586 pb.F_Sfixed32Optional = proto.Int32(-32)
587 pb.F_Sfixed64Optional = proto.Int64(-64)
588 pb.Optionalgroup = initGoTest_OptionalGroup()
589
590 overify(t, pb,
591 protopack.Message{
592 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
593 protopack.Tag{2, protopack.BytesType}, protopack.String("hello"),
594 protopack.Tag{3, protopack.VarintType}, protopack.Varint(7),
595 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
596 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
597 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
598 }),
599 protopack.Tag{6, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
600 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
601 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
602 }),
603 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
604 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
605 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
606 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
607 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
608 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
609 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
610 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
611 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
612 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
613 protopack.Tag{30, protopack.VarintType}, protopack.Bool(true),
614 protopack.Tag{31, protopack.VarintType}, protopack.Varint(32),
615 protopack.Tag{32, protopack.VarintType}, protopack.Varint(64),
616 protopack.Tag{33, protopack.Fixed32Type}, protopack.Uint32(3232),
617 protopack.Tag{34, protopack.Fixed64Type}, protopack.Uint64(6464),
618 protopack.Tag{35, protopack.VarintType}, protopack.Uvarint(323232),
619 protopack.Tag{36, protopack.VarintType}, protopack.Uvarint(646464),
620 protopack.Tag{37, protopack.Fixed32Type}, protopack.Float32(32),
621 protopack.Tag{38, protopack.Fixed64Type}, protopack.Float64(64),
622 protopack.Tag{39, protopack.BytesType}, protopack.String("hello"),
623 protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
624 protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
625 protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
626 protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
627 protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
628 protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
629 protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
630 protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
631 protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
632 protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
633 protopack.Tag{70, protopack.StartGroupType},
634 protopack.Message{
635 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
636 },
637 protopack.Tag{70, protopack.EndGroupType},
638 protopack.Tag{90, protopack.StartGroupType},
639 protopack.Message{
640 protopack.Tag{91, protopack.BytesType}, protopack.String("optional"),
641 },
642 protopack.Tag{90, protopack.EndGroupType},
643 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
644 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
645 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
646 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
647 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
648 protopack.Tag{301, protopack.BytesType}, protopack.Bytes("Bignose"),
649 protopack.Tag{302, protopack.VarintType}, protopack.Svarint(-32),
650 protopack.Tag{303, protopack.VarintType}, protopack.Svarint(-64),
651 protopack.Tag{304, protopack.Fixed32Type}, protopack.Int32(-32),
652 protopack.Tag{305, protopack.Fixed64Type}, protopack.Int64(-64),
653 protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
654 protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
655 protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
656 protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
657 protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
658 }.Marshal())
659 }
660
661
662 func TestEncodeDecode5(t *testing.T) {
663 pb := initGoTest(true)
664 pb.RepeatedField = []*pb2.GoTestField{initGoTestField(), initGoTestField()}
665 pb.F_BoolRepeated = []bool{false, true}
666 pb.F_Int32Repeated = []int32{32, 33}
667 pb.F_Int64Repeated = []int64{64, 65}
668 pb.F_Fixed32Repeated = []uint32{3232, 3333}
669 pb.F_Fixed64Repeated = []uint64{6464, 6565}
670 pb.F_Uint32Repeated = []uint32{323232, 333333}
671 pb.F_Uint64Repeated = []uint64{646464, 656565}
672 pb.F_FloatRepeated = []float32{32., 33.}
673 pb.F_DoubleRepeated = []float64{64., 65.}
674 pb.F_StringRepeated = []string{"hello", "sailor"}
675 pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
676 pb.F_Sint32Repeated = []int32{32, -32}
677 pb.F_Sint64Repeated = []int64{64, -64}
678 pb.F_Sfixed32Repeated = []int32{32, -32}
679 pb.F_Sfixed64Repeated = []int64{64, -64}
680 pb.Repeatedgroup = []*pb2.GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
681
682 overify(t, pb,
683 protopack.Message{
684 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
685 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
686 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
687 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
688 }),
689 protopack.Tag{5, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
690 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
691 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
692 }),
693 protopack.Tag{5, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
694 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
695 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
696 }),
697 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
698 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
699 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
700 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
701 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
702 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
703 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
704 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
705 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
706 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
707 protopack.Tag{20, protopack.VarintType}, protopack.Bool(false),
708 protopack.Tag{20, protopack.VarintType}, protopack.Bool(true),
709 protopack.Tag{21, protopack.VarintType}, protopack.Varint(32),
710 protopack.Tag{21, protopack.VarintType}, protopack.Varint(33),
711 protopack.Tag{22, protopack.VarintType}, protopack.Varint(64),
712 protopack.Tag{22, protopack.VarintType}, protopack.Varint(65),
713 protopack.Tag{23, protopack.Fixed32Type}, protopack.Uint32(3232),
714 protopack.Tag{23, protopack.Fixed32Type}, protopack.Uint32(3333),
715 protopack.Tag{24, protopack.Fixed64Type}, protopack.Uint64(6464),
716 protopack.Tag{24, protopack.Fixed64Type}, protopack.Uint64(6565),
717 protopack.Tag{25, protopack.VarintType}, protopack.Uvarint(323232),
718 protopack.Tag{25, protopack.VarintType}, protopack.Uvarint(333333),
719 protopack.Tag{26, protopack.VarintType}, protopack.Uvarint(646464),
720 protopack.Tag{26, protopack.VarintType}, protopack.Uvarint(656565),
721 protopack.Tag{27, protopack.Fixed32Type}, protopack.Float32(32),
722 protopack.Tag{27, protopack.Fixed32Type}, protopack.Float32(33),
723 protopack.Tag{28, protopack.Fixed64Type}, protopack.Float64(64),
724 protopack.Tag{28, protopack.Fixed64Type}, protopack.Float64(65),
725 protopack.Tag{29, protopack.BytesType}, protopack.String("hello"),
726 protopack.Tag{29, protopack.BytesType}, protopack.String("sailor"),
727 protopack.Tag{40, protopack.VarintType}, protopack.Bool(true),
728 protopack.Tag{41, protopack.VarintType}, protopack.Varint(32),
729 protopack.Tag{42, protopack.VarintType}, protopack.Varint(64),
730 protopack.Tag{43, protopack.Fixed32Type}, protopack.Uint32(320),
731 protopack.Tag{44, protopack.Fixed64Type}, protopack.Uint64(640),
732 protopack.Tag{45, protopack.VarintType}, protopack.Uvarint(3200),
733 protopack.Tag{46, protopack.VarintType}, protopack.Uvarint(6400),
734 protopack.Tag{47, protopack.Fixed32Type}, protopack.Float32(314159),
735 protopack.Tag{48, protopack.Fixed64Type}, protopack.Float64(271828),
736 protopack.Tag{49, protopack.BytesType}, protopack.String("hello, \"world!\"\n"),
737 protopack.Tag{70, protopack.StartGroupType},
738 protopack.Message{
739 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
740 },
741 protopack.Tag{70, protopack.EndGroupType},
742 protopack.Tag{80, protopack.StartGroupType},
743 protopack.Message{
744 protopack.Tag{81, protopack.BytesType}, protopack.String("repeated"),
745 },
746 protopack.Tag{80, protopack.EndGroupType},
747 protopack.Tag{80, protopack.StartGroupType},
748 protopack.Message{
749 protopack.Tag{81, protopack.BytesType}, protopack.String("repeated"),
750 },
751 protopack.Tag{80, protopack.EndGroupType},
752 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
753 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
754 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
755 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
756 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
757 protopack.Tag{201, protopack.BytesType}, protopack.Bytes("big"),
758 protopack.Tag{201, protopack.BytesType}, protopack.Bytes("nose"),
759 protopack.Tag{202, protopack.VarintType}, protopack.Svarint(32),
760 protopack.Tag{202, protopack.VarintType}, protopack.Svarint(-32),
761 protopack.Tag{203, protopack.VarintType}, protopack.Svarint(64),
762 protopack.Tag{203, protopack.VarintType}, protopack.Svarint(-64),
763 protopack.Tag{204, protopack.Fixed32Type}, protopack.Int32(32),
764 protopack.Tag{204, protopack.Fixed32Type}, protopack.Int32(-32),
765 protopack.Tag{205, protopack.Fixed64Type}, protopack.Int64(64),
766 protopack.Tag{205, protopack.Fixed64Type}, protopack.Int64(-64),
767 protopack.Tag{401, protopack.BytesType}, protopack.Bytes("Bignose"),
768 protopack.Tag{402, protopack.VarintType}, protopack.Svarint(-32),
769 protopack.Tag{403, protopack.VarintType}, protopack.Svarint(-64),
770 protopack.Tag{404, protopack.Fixed32Type}, protopack.Int32(-32),
771 protopack.Tag{405, protopack.Fixed64Type}, protopack.Int64(-64),
772 }.Marshal())
773 }
774
775
776 func TestEncodeDecode6(t *testing.T) {
777 pb := initGoTest(false)
778 pb.F_BoolRepeatedPacked = []bool{false, true}
779 pb.F_Int32RepeatedPacked = []int32{32, 33}
780 pb.F_Int64RepeatedPacked = []int64{64, 65}
781 pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
782 pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
783 pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
784 pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
785 pb.F_FloatRepeatedPacked = []float32{32., 33.}
786 pb.F_DoubleRepeatedPacked = []float64{64., 65.}
787 pb.F_Sint32RepeatedPacked = []int32{32, -32}
788 pb.F_Sint64RepeatedPacked = []int64{64, -64}
789 pb.F_Sfixed32RepeatedPacked = []int32{32, -32}
790 pb.F_Sfixed64RepeatedPacked = []int64{64, -64}
791
792 overify(t, pb,
793 protopack.Message{
794 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
795 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
796 protopack.Tag{1, protopack.BytesType}, protopack.String("label"),
797 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
798 }),
799 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
800 protopack.Tag{11, protopack.VarintType}, protopack.Varint(3),
801 protopack.Tag{12, protopack.VarintType}, protopack.Varint(6),
802 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
803 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
804 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
805 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
806 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
807 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
808 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
809 protopack.Tag{50, protopack.BytesType}, protopack.LengthPrefix{protopack.Bool(false), protopack.Bool(true)},
810 protopack.Tag{51, protopack.BytesType}, protopack.LengthPrefix{protopack.Varint(32), protopack.Varint(33)},
811 protopack.Tag{52, protopack.BytesType}, protopack.LengthPrefix{protopack.Varint(64), protopack.Varint(65)},
812 protopack.Tag{53, protopack.BytesType}, protopack.LengthPrefix{protopack.Uint32(3232), protopack.Uint32(3333)},
813 protopack.Tag{54, protopack.BytesType}, protopack.LengthPrefix{protopack.Uint64(6464), protopack.Uint64(6565)},
814 protopack.Tag{55, protopack.BytesType}, protopack.LengthPrefix{protopack.Uvarint(323232), protopack.Uvarint(333333)},
815 protopack.Tag{56, protopack.BytesType}, protopack.LengthPrefix{protopack.Uvarint(646464), protopack.Uvarint(656565)},
816 protopack.Tag{57, protopack.BytesType}, protopack.LengthPrefix{protopack.Float32(32), protopack.Float32(33)},
817 protopack.Tag{58, protopack.BytesType}, protopack.LengthPrefix{protopack.Float64(64), protopack.Float64(65)},
818 protopack.Tag{70, protopack.StartGroupType},
819 protopack.Message{
820 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
821 },
822 protopack.Tag{70, protopack.EndGroupType},
823 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
824 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
825 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
826 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
827 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
828 protopack.Tag{502, protopack.BytesType}, protopack.LengthPrefix{protopack.Svarint(32), protopack.Svarint(-32)},
829 protopack.Tag{503, protopack.BytesType}, protopack.LengthPrefix{protopack.Svarint(64), protopack.Svarint(-64)},
830 protopack.Tag{504, protopack.BytesType}, protopack.LengthPrefix{protopack.Int32(32), protopack.Int32(-32)},
831 protopack.Tag{505, protopack.BytesType}, protopack.LengthPrefix{protopack.Int64(64), protopack.Int64(-64)},
832 }.Marshal())
833 }
834
835
836 func TestEncodeDecodeBytes1(t *testing.T) {
837 pb := initGoTest(false)
838
839
840 pb.F_BytesRequired = []byte{}
841 pb.F_BytesRepeated = [][]byte{{}}
842 pb.F_BytesOptional = []byte{}
843
844 d, err := proto.Marshal(pb)
845 if err != nil {
846 t.Error(err)
847 }
848
849 pbd := new(pb2.GoTest)
850 if err := proto.Unmarshal(d, pbd); err != nil {
851 t.Error(err)
852 }
853
854 if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
855 t.Error("required empty bytes field is incorrect")
856 }
857 if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
858 t.Error("repeated empty bytes field is incorrect")
859 }
860 if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
861 t.Error("optional empty bytes field is incorrect")
862 }
863 }
864
865
866
867 func TestEncodeDecodeBytes2(t *testing.T) {
868 pb := initGoTest(false)
869
870
871 pb.F_BytesRepeated = [][]byte{nil}
872
873 d, err := proto.Marshal(pb)
874 if err != nil {
875 t.Error(err)
876 }
877
878 pbd := new(pb2.GoTest)
879 if err := proto.Unmarshal(d, pbd); err != nil {
880 t.Error(err)
881 }
882
883 if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
884 t.Error("Unexpected value for repeated bytes field")
885 }
886 }
887
888
889 func TestSkippingUnrecognizedFields(t *testing.T) {
890 o := new(proto.Buffer)
891 pb := initGoTestField()
892
893
894 o.Marshal(pb)
895
896
897 skip := &pb2.GoSkipTest{
898 SkipInt32: proto.Int32(32),
899 SkipFixed32: proto.Uint32(3232),
900 SkipFixed64: proto.Uint64(6464),
901 SkipString: proto.String("skipper"),
902 Skipgroup: &pb2.GoSkipTest_SkipGroup{
903 GroupInt32: proto.Int32(75),
904 GroupString: proto.String("wxyz"),
905 },
906 }
907
908
909 o.Marshal(skip)
910
911 pbd := new(pb2.GoTestField)
912 o.Unmarshal(pbd)
913
914
915 skipd := new(pb2.GoSkipTest)
916
917 o.SetBuf(pbd.XXX_unrecognized)
918 o.Unmarshal(skipd)
919
920 switch {
921 case *skipd.SkipInt32 != *skip.SkipInt32:
922 t.Error("skip int32", skipd.SkipInt32)
923 case *skipd.SkipFixed32 != *skip.SkipFixed32:
924 t.Error("skip fixed32", skipd.SkipFixed32)
925 case *skipd.SkipFixed64 != *skip.SkipFixed64:
926 t.Error("skip fixed64", skipd.SkipFixed64)
927 case *skipd.SkipString != *skip.SkipString:
928 t.Error("skip string", *skipd.SkipString)
929 case *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32:
930 t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
931 case *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString:
932 t.Error("skip group string", *skipd.Skipgroup.GroupString)
933 }
934 }
935
936
937 func TestSubmessageUnrecognizedFields(t *testing.T) {
938 nm := &pb2.NewMessage{
939 Nested: &pb2.NewMessage_Nested{
940 Name: proto.String("Nigel"),
941 FoodGroup: proto.String("carbs"),
942 },
943 }
944 b, err := proto.Marshal(nm)
945 if err != nil {
946 t.Fatalf("Marshal of NewMessage: %v", err)
947 }
948
949
950 om := new(pb2.OldMessage)
951 if err := proto.Unmarshal(b, om); err != nil {
952 t.Fatalf("Unmarshal to OldMessage: %v", err)
953 }
954 exp := &pb2.OldMessage{
955 Nested: &pb2.OldMessage_Nested{
956 Name: proto.String("Nigel"),
957
958 XXX_unrecognized: []byte("\x12\x05carbs"),
959 },
960 }
961 if !proto.Equal(om, exp) {
962 t.Errorf("om = %v, want %v", om, exp)
963 }
964
965
966 om = proto.Clone(om).(*pb2.OldMessage)
967 if !proto.Equal(om, exp) {
968 t.Errorf("Clone(om) = %v, want %v", om, exp)
969 }
970
971
972 if b, err = proto.Marshal(om); err != nil {
973 t.Fatalf("Marshal of OldMessage: %v", err)
974 }
975 t.Logf("Marshal(%v) -> %q", om, b)
976 nm2 := new(pb2.NewMessage)
977 if err := proto.Unmarshal(b, nm2); err != nil {
978 t.Fatalf("Unmarshal to NewMessage: %v", err)
979 }
980 if !proto.Equal(nm, nm2) {
981 t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
982 }
983 }
984
985
986 func TestNegativeInt32(t *testing.T) {
987 om := &pb2.OldMessage{
988 Num: proto.Int32(-1),
989 }
990 b, err := proto.Marshal(om)
991 if err != nil {
992 t.Fatalf("Marshal of OldMessage: %v", err)
993 }
994
995
996
997 if len(b) != 11 {
998 t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
999 }
1000
1001
1002 nm := new(pb2.NewMessage)
1003 if err := proto.Unmarshal(b, nm); err != nil {
1004 t.Fatalf("Unmarshal to NewMessage: %v", err)
1005 }
1006 want := &pb2.NewMessage{
1007 Num: proto.Int64(-1),
1008 }
1009 if !proto.Equal(nm, want) {
1010 t.Errorf("nm = %v, want %v", nm, want)
1011 }
1012 }
1013
1014
1015
1016
1017
1018
1019
1020 func TestBigRepeated(t *testing.T) {
1021 pb := initGoTest(true)
1022
1023
1024 const N = 50
1025 pb.Repeatedgroup = make([]*pb2.GoTest_RepeatedGroup, N)
1026 pb.F_Sint64Repeated = make([]int64, N)
1027 pb.F_Sint32Repeated = make([]int32, N)
1028 pb.F_BytesRepeated = make([][]byte, N)
1029 pb.F_StringRepeated = make([]string, N)
1030 pb.F_DoubleRepeated = make([]float64, N)
1031 pb.F_FloatRepeated = make([]float32, N)
1032 pb.F_Uint64Repeated = make([]uint64, N)
1033 pb.F_Uint32Repeated = make([]uint32, N)
1034 pb.F_Fixed64Repeated = make([]uint64, N)
1035 pb.F_Fixed32Repeated = make([]uint32, N)
1036 pb.F_Int64Repeated = make([]int64, N)
1037 pb.F_Int32Repeated = make([]int32, N)
1038 pb.F_BoolRepeated = make([]bool, N)
1039 pb.RepeatedField = make([]*pb2.GoTestField, N)
1040
1041
1042 igtf := initGoTestField()
1043 igtrg := initGoTest_RepeatedGroup()
1044 for i := 0; i < N; i++ {
1045 pb.Repeatedgroup[i] = igtrg
1046 pb.F_Sint64Repeated[i] = int64(i)
1047 pb.F_Sint32Repeated[i] = int32(i)
1048 s := fmt.Sprint(i)
1049 pb.F_BytesRepeated[i] = []byte(s)
1050 pb.F_StringRepeated[i] = s
1051 pb.F_DoubleRepeated[i] = float64(i)
1052 pb.F_FloatRepeated[i] = float32(i)
1053 pb.F_Uint64Repeated[i] = uint64(i)
1054 pb.F_Uint32Repeated[i] = uint32(i)
1055 pb.F_Fixed64Repeated[i] = uint64(i)
1056 pb.F_Fixed32Repeated[i] = uint32(i)
1057 pb.F_Int64Repeated[i] = int64(i)
1058 pb.F_Int32Repeated[i] = int32(i)
1059 pb.F_BoolRepeated[i] = i%2 == 0
1060 pb.RepeatedField[i] = igtf
1061 }
1062
1063
1064 buf, _ := proto.Marshal(pb)
1065
1066
1067 pbd := new(pb2.GoTest)
1068 proto.Unmarshal(buf, pbd)
1069
1070
1071 for i := uint64(0); i < N; i++ {
1072 switch {
1073 case pbd.Repeatedgroup[i] == nil:
1074 t.Error("pbd.Repeatedgroup bad")
1075 case uint64(pbd.F_Sint64Repeated[i]) != i:
1076 t.Error("pbd.F_Sint64Repeated bad", uint64(pbd.F_Sint64Repeated[i]), i)
1077 case uint64(pbd.F_Sint32Repeated[i]) != i:
1078 t.Error("pbd.F_Sint32Repeated bad", uint64(pbd.F_Sint32Repeated[i]), i)
1079 case !bytes.Equal(pbd.F_BytesRepeated[i], []byte(fmt.Sprint(i))):
1080 t.Error("pbd.F_BytesRepeated bad", pbd.F_BytesRepeated[i], i)
1081 case pbd.F_StringRepeated[i] != string(fmt.Sprint(i)):
1082 t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
1083 case uint64(pbd.F_DoubleRepeated[i]) != i:
1084 t.Error("pbd.F_DoubleRepeated bad", uint64(pbd.F_DoubleRepeated[i]), i)
1085 case uint64(pbd.F_FloatRepeated[i]) != i:
1086 t.Error("pbd.F_FloatRepeated bad", uint64(pbd.F_FloatRepeated[i]), i)
1087 case pbd.F_Uint64Repeated[i] != i:
1088 t.Error("pbd.F_Uint64Repeated bad", pbd.F_Uint64Repeated[i], i)
1089 case uint64(pbd.F_Uint32Repeated[i]) != i:
1090 t.Error("pbd.F_Uint32Repeated bad", uint64(pbd.F_Uint32Repeated[i]), i)
1091 case pbd.F_Fixed64Repeated[i] != i:
1092 t.Error("pbd.F_Fixed64Repeated bad", pbd.F_Fixed64Repeated[i], i)
1093 case uint64(pbd.F_Fixed32Repeated[i]) != i:
1094 t.Error("pbd.F_Fixed32Repeated bad", uint64(pbd.F_Fixed32Repeated[i]), i)
1095 case uint64(pbd.F_Int64Repeated[i]) != i:
1096 t.Error("pbd.F_Int64Repeated bad", uint64(pbd.F_Int64Repeated[i]), i)
1097 case uint64(pbd.F_Int32Repeated[i]) != i:
1098 t.Error("pbd.F_Int32Repeated bad", uint64(pbd.F_Int32Repeated[i]), i)
1099 case pbd.F_BoolRepeated[i] != (i%2 == 0):
1100 t.Error("pbd.F_BoolRepeated bad", pbd.F_BoolRepeated[i], i)
1101 case pbd.RepeatedField[i] == nil:
1102 t.Error("pbd.RepeatedField bad")
1103 }
1104 }
1105 }
1106
1107 func TestBadWireTypeUnknown(t *testing.T) {
1108 b := protopack.Message{
1109 protopack.Tag{1, protopack.BytesType}, protopack.Bytes("x"),
1110 protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(0),
1111 protopack.Tag{1, protopack.VarintType}, protopack.Varint(11),
1112 protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(22),
1113 protopack.Tag{2, protopack.BytesType}, protopack.String("aaa"),
1114 protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(33),
1115 protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(44),
1116 protopack.Tag{4, protopack.BytesType}, protopack.String("bbb"),
1117 protopack.Tag{4, protopack.Fixed32Type}, protopack.Uint32(55),
1118 protopack.Tag{4, protopack.BytesType}, protopack.String("ccc"),
1119 protopack.Tag{4, protopack.Fixed64Type}, protopack.Uint64(66),
1120 protopack.Tag{11, protopack.VarintType}, protopack.Uvarint(77),
1121 protopack.Tag{11, protopack.BytesType}, protopack.Bytes("ddd"),
1122 protopack.Tag{11, protopack.Fixed64Type}, protopack.Float64(88),
1123 protopack.Tag{11, protopack.Fixed32Type}, protopack.Uint32(99),
1124 }.Marshal()
1125
1126 m := new(pb2.MyMessage)
1127 if err := proto.Unmarshal(b, m); err != nil {
1128 t.Errorf("unexpected Unmarshal error: %v", err)
1129 }
1130
1131 unknown := protopack.Message{
1132 protopack.Tag{1, protopack.BytesType}, protopack.Bytes("x"),
1133 protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(0),
1134 protopack.Tag{2, protopack.VarintType}, protopack.Uvarint(22),
1135 protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(33),
1136 protopack.Tag{4, protopack.VarintType}, protopack.Uvarint(44),
1137 protopack.Tag{4, protopack.Fixed32Type}, protopack.Uint32(55),
1138 protopack.Tag{4, protopack.Fixed64Type}, protopack.Uint64(66),
1139 protopack.Tag{11, protopack.VarintType}, protopack.Uvarint(77),
1140 protopack.Tag{11, protopack.BytesType}, protopack.Bytes("ddd"),
1141 protopack.Tag{11, protopack.Fixed32Type}, protopack.Uint32(99),
1142 }.Marshal()
1143 if !bytes.Equal(m.XXX_unrecognized, unknown) {
1144 t.Errorf("unknown bytes mismatch:\ngot %x\nwant %x", m.XXX_unrecognized, unknown)
1145 }
1146 proto.DiscardUnknown(m)
1147
1148 want := &pb2.MyMessage{Count: proto.Int32(11), Name: proto.String("aaa"), Pet: []string{"bbb", "ccc"}, Bigfloat: proto.Float64(88)}
1149 if !proto.Equal(m, want) {
1150 t.Errorf("message mismatch:\ngot %v\nwant %v", m, want)
1151 }
1152 }
1153
1154 func encodeDecode(t *testing.T, in, out proto.Message, msg string) {
1155 buf, err := proto.Marshal(in)
1156 if err != nil {
1157 t.Fatalf("failed marshaling %v: %v", msg, err)
1158 }
1159 if err := proto.Unmarshal(buf, out); err != nil {
1160 t.Fatalf("failed unmarshaling %v: %v", msg, err)
1161 }
1162 }
1163
1164 func TestPackedNonPackedDecoderSwitching(t *testing.T) {
1165 np, p := new(pb2.NonPackedTest), new(pb2.PackedTest)
1166
1167
1168 np.A = []int32{0, 1, 1, 2, 3, 5}
1169 encodeDecode(t, np, p, "non-packed -> packed")
1170 if !reflect.DeepEqual(np.A, p.B) {
1171 t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
1172 }
1173
1174
1175 np.Reset()
1176 p.B = []int32{3, 1, 4, 1, 5, 9}
1177 encodeDecode(t, p, np, "packed -> non-packed")
1178 if !reflect.DeepEqual(p.B, np.A) {
1179 t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
1180 }
1181 }
1182
1183 func TestProto1RepeatedGroup(t *testing.T) {
1184 pb := &pb2.MessageList{
1185 Message: []*pb2.MessageList_Message{
1186 {
1187 Name: proto.String("blah"),
1188 Count: proto.Int32(7),
1189 },
1190
1191 nil,
1192 },
1193 }
1194
1195 o := new(proto.Buffer)
1196 err := o.Marshal(pb)
1197 if err == nil {
1198 t.Fatalf("expected error when marshaling repeted nil MessageList.Message")
1199 }
1200 if _, ok := err.(*proto.RequiredNotSetError); !ok {
1201 t.Fatalf("unexpected error when marshaling: %v", err)
1202 }
1203 }
1204
1205
1206
1207
1208 func TestEnum(t *testing.T) {
1209 pb := new(pb2.GoEnum)
1210 pb.Foo = pb2.FOO_FOO1.Enum()
1211 o := new(proto.Buffer)
1212 if err := o.Marshal(pb); err != nil {
1213 t.Fatal("error encoding enum:", err)
1214 }
1215 pb1 := new(pb2.GoEnum)
1216 if err := o.Unmarshal(pb1); err != nil {
1217 t.Fatal("error decoding enum:", err)
1218 }
1219 if *pb1.Foo != pb2.FOO_FOO1 {
1220 t.Error("expected 7 but got ", *pb1.Foo)
1221 }
1222 }
1223
1224
1225
1226 func TestPrintingNilEnumFields(t *testing.T) {
1227 pb := new(pb2.GoEnum)
1228 _ = fmt.Sprintf("%+v", pb)
1229 }
1230
1231
1232 func TestRequiredFieldEnforcement(t *testing.T) {
1233 pb := new(pb2.GoTestField)
1234 _, err := proto.Marshal(pb)
1235 if err == nil {
1236 t.Error("marshal: expected error, got nil")
1237 } else if !isRequiredNotSetError(err) {
1238 t.Errorf("marshal: bad error type: %v", err)
1239 }
1240
1241
1242
1243
1244 buf := []byte("\x0A\x02hi\x0A\x02hi")
1245 err = proto.Unmarshal(buf, pb)
1246 if err == nil {
1247 t.Error("unmarshal: expected error, got nil")
1248 } else if !isRequiredNotSetError(err) {
1249 t.Errorf("unmarshal: bad error type: %v", err)
1250 }
1251 }
1252
1253
1254 func TestRequiredFieldEnforcementGroups(t *testing.T) {
1255 pb := &pb2.GoTestRequiredGroupField{Group: &pb2.GoTestRequiredGroupField_Group{}}
1256 if _, err := proto.Marshal(pb); err == nil {
1257 t.Error("marshal: expected error, got nil")
1258 } else if !isRequiredNotSetError(err) {
1259 t.Errorf("marshal: bad error type: %v", err)
1260 }
1261
1262 buf := []byte{11, 12}
1263 if err := proto.Unmarshal(buf, pb); err == nil {
1264 t.Error("unmarshal: expected error, got nil")
1265 } else if !isRequiredNotSetError(err) {
1266 t.Errorf("unmarshal: bad error type: %v", err)
1267 }
1268 }
1269
1270 func TestTypedNilMarshal(t *testing.T) {
1271
1272 var m *pb2.GoEnum
1273 if _, err := proto.Marshal(m); err != proto.ErrNil {
1274 t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err)
1275 }
1276 }
1277
1278 func TestTypedNilMarshalInOneof(t *testing.T) {
1279
1280 m := &pb2.Communique{Union: &pb2.Communique_Msg{nil}}
1281 if _, err := proto.Marshal(m); err == proto.ErrNil {
1282 t.Errorf("Marshal(%#v): got %v, want nil or errOneofHasNil", m, err)
1283 }
1284 }
1285
1286
1287 type nonNillableInt uint64
1288
1289 func (nni nonNillableInt) Marshal() ([]byte, error) {
1290 return proto.EncodeVarint(uint64(nni)), nil
1291 }
1292
1293 type NNIMessage struct {
1294 nni nonNillableInt
1295 }
1296
1297 func (*NNIMessage) Reset() {}
1298 func (*NNIMessage) String() string { return "" }
1299 func (*NNIMessage) ProtoMessage() {}
1300
1301 type NMMessage struct{}
1302
1303 func (*NMMessage) Reset() {}
1304 func (*NMMessage) String() string { return "" }
1305 func (*NMMessage) ProtoMessage() {}
1306
1307
1308 func TestNilMarshaler(t *testing.T) {
1309
1310
1311 nmm := new(NMMessage)
1312 if _, err := proto.Marshal(nmm); err != nil {
1313 t.Error("unexpected error marshaling nmm: ", err)
1314 }
1315
1316
1317 nnim := new(NNIMessage)
1318 nnim.nni = 7
1319 var _ proto.Marshaler = nnim.nni
1320 if _, err := proto.Marshal(nnim); err != nil {
1321 t.Error("unexpected error marshaling nnim: ", err)
1322 }
1323 }
1324
1325 func TestAllSetDefaults(t *testing.T) {
1326
1327 got := &pb2.Defaults{
1328
1329 F_Nan: proto.Float32(1.7),
1330 }
1331 want := &pb2.Defaults{
1332 F_Bool: proto.Bool(true),
1333 F_Int32: proto.Int32(32),
1334 F_Int64: proto.Int64(64),
1335 F_Fixed32: proto.Uint32(320),
1336 F_Fixed64: proto.Uint64(640),
1337 F_Uint32: proto.Uint32(3200),
1338 F_Uint64: proto.Uint64(6400),
1339 F_Float: proto.Float32(314159),
1340 F_Double: proto.Float64(271828),
1341 F_String: proto.String(`hello, "world!"` + "\n"),
1342 F_Bytes: []byte("Bignose"),
1343 F_Sint32: proto.Int32(-32),
1344 F_Sint64: proto.Int64(-64),
1345 F_Enum: pb2.Defaults_GREEN.Enum(),
1346 F_Pinf: proto.Float32(float32(math.Inf(1))),
1347 F_Ninf: proto.Float32(float32(math.Inf(-1))),
1348 F_Nan: proto.Float32(1.7),
1349 StrZero: proto.String(""),
1350 }
1351 proto.SetDefaults(got)
1352 if !proto.Equal(got, want) {
1353 t.Errorf("SetDefaults failed\n got %v\nwant %v", got, want)
1354 }
1355 }
1356
1357 func TestSetDefaultsWithSetField(t *testing.T) {
1358
1359 m := &pb2.Defaults{
1360 F_Int32: proto.Int32(12),
1361 }
1362 proto.SetDefaults(m)
1363 if v := m.GetF_Int32(); v != 12 {
1364 t.Errorf("m.FInt32 = %v, want 12", v)
1365 }
1366 }
1367
1368 func TestSetDefaultsWithSubMessage(t *testing.T) {
1369 got := &pb2.OtherMessage{
1370 Key: proto.Int64(123),
1371 Inner: &pb2.InnerMessage{
1372 Host: proto.String("gopher"),
1373 },
1374 }
1375 want := &pb2.OtherMessage{
1376 Key: proto.Int64(123),
1377 Inner: &pb2.InnerMessage{
1378 Host: proto.String("gopher"),
1379 Port: proto.Int32(4000),
1380 },
1381 }
1382 proto.SetDefaults(got)
1383 if !proto.Equal(got, want) {
1384 t.Errorf("\n got %v\nwant %v", got, want)
1385 }
1386 }
1387
1388 func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
1389 got := &pb2.MyMessage{
1390 RepInner: []*pb2.InnerMessage{{}},
1391 }
1392 want := &pb2.MyMessage{
1393 RepInner: []*pb2.InnerMessage{{
1394 Port: proto.Int32(4000),
1395 }},
1396 }
1397 proto.SetDefaults(got)
1398 if !proto.Equal(got, want) {
1399 t.Errorf("\n got %v\nwant %v", got, want)
1400 }
1401 }
1402
1403 func TestSetDefaultWithRepeatedNonMessage(t *testing.T) {
1404 got := &pb2.MyMessage{
1405 Pet: []string{"turtle", "wombat"},
1406 }
1407 want := proto.Clone(got)
1408 proto.SetDefaults(got)
1409 if !proto.Equal(got, want) {
1410 t.Errorf("\n got %v\nwant %v", got, want)
1411 }
1412 }
1413
1414 func TestMaximumTagNumber(t *testing.T) {
1415 m := &pb2.MaxTag{
1416 LastField: proto.String("natural goat essence"),
1417 }
1418 buf, err := proto.Marshal(m)
1419 if err != nil {
1420 t.Fatalf("proto.Marshal failed: %v", err)
1421 }
1422 m2 := new(pb2.MaxTag)
1423 if err := proto.Unmarshal(buf, m2); err != nil {
1424 t.Fatalf("proto.Unmarshal failed: %v", err)
1425 }
1426 if got, want := m2.GetLastField(), *m.LastField; got != want {
1427 t.Errorf("got %q, want %q", got, want)
1428 }
1429 }
1430
1431 func TestJSON(t *testing.T) {
1432 m := &pb2.MyMessage{
1433 Count: proto.Int32(4),
1434 Pet: []string{"bunny", "kitty"},
1435 Inner: &pb2.InnerMessage{
1436 Host: proto.String("cauchy"),
1437 },
1438 Bikeshed: pb2.MyMessage_GREEN.Enum(),
1439 }
1440 const want = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
1441
1442 b, err := json.Marshal(m)
1443 if err != nil {
1444 t.Fatalf("json.Marshal failed: %v", err)
1445 }
1446 s := string(b)
1447 if s != want {
1448 t.Errorf("got %s\nwant %s", s, want)
1449 }
1450
1451 received := new(pb2.MyMessage)
1452 if err := json.Unmarshal(b, received); err != nil {
1453 t.Fatalf("json.Unmarshal failed: %v", err)
1454 }
1455 if !proto.Equal(received, m) {
1456 t.Fatalf("got %s, want %s", received, m)
1457 }
1458
1459
1460 const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
1461 received.Reset()
1462 if err := json.Unmarshal([]byte(old), received); err != nil {
1463 t.Fatalf("json.Unmarshal failed: %v", err)
1464 }
1465 if !proto.Equal(received, m) {
1466 t.Fatalf("got %s, want %s", received, m)
1467 }
1468 }
1469
1470 func TestBadWireType(t *testing.T) {
1471 b := []byte{7<<3 | 6}
1472 pb := new(pb2.OtherMessage)
1473 if err := proto.Unmarshal(b, pb); err == nil {
1474 t.Errorf("Unmarshal did not fail")
1475 }
1476 }
1477
1478 func TestBytesWithInvalidLength(t *testing.T) {
1479
1480 b := protopack.Message{
1481 protopack.Tag{2, protopack.BytesType}, protopack.Denormalized{+1, protopack.Uvarint(34359738367)},
1482 }.Marshal()
1483 proto.Unmarshal(b, new(pb2.MyMessage))
1484 }
1485
1486 func TestLengthOverflow(t *testing.T) {
1487
1488 b := protopack.Message{
1489 protopack.Tag{2, protopack.BytesType}, protopack.String("\x01"),
1490 protopack.Tag{3, protopack.BytesType}, protopack.Uvarint(9223372036854775807),
1491 protopack.Raw("\x01"),
1492 }.Marshal()
1493 proto.Unmarshal(b, new(pb2.MyMessage))
1494 }
1495
1496 func TestVarintOverflow(t *testing.T) {
1497
1498 b := protopack.Message{
1499 protopack.Tag{1, protopack.VarintType}, protopack.Varint(1),
1500 protopack.Tag{3, protopack.BytesType},
1501 protopack.Raw("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01"),
1502 }.Marshal()
1503 if err := proto.Unmarshal(b, new(pb2.MyMessage)); err == nil {
1504 t.Fatalf("Overflowed uint64 length without error")
1505 }
1506 }
1507
1508 func TestBytesWithInvalidLengthInGroup(t *testing.T) {
1509
1510 b := protopack.Message{
1511 protopack.Tag{775, protopack.StartGroupType},
1512 protopack.Message{
1513 protopack.Tag{774, protopack.BytesType}, protopack.Uvarint(13654841034505509168),
1514 protopack.Raw(""),
1515 },
1516 }.Marshal()
1517 if err := proto.Unmarshal(b, new(pb2.MyMessage)); err == nil {
1518 t.Fatalf("Overflowed uint64 length without error")
1519 }
1520 }
1521
1522 func TestUnmarshalFuzz(t *testing.T) {
1523 const N = 1000
1524 seed := time.Now().UnixNano()
1525 t.Logf("RNG seed is %d", seed)
1526 rng := rand.New(rand.NewSource(seed))
1527 buf := make([]byte, 20)
1528 for i := 0; i < N; i++ {
1529 for j := range buf {
1530 buf[j] = byte(rng.Intn(256))
1531 }
1532 fuzzUnmarshal(t, buf)
1533 }
1534 }
1535
1536 func TestMergeMessages(t *testing.T) {
1537 pb := &pb2.MessageList{Message: []*pb2.MessageList_Message{{Name: proto.String("x"), Count: proto.Int32(1)}}}
1538 data, err := proto.Marshal(pb)
1539 if err != nil {
1540 t.Fatalf("Marshal: %v", err)
1541 }
1542
1543 pb1 := new(pb2.MessageList)
1544 if err := proto.Unmarshal(data, pb1); err != nil {
1545 t.Fatalf("first Unmarshal: %v", err)
1546 }
1547 if err := proto.Unmarshal(data, pb1); err != nil {
1548 t.Fatalf("second Unmarshal: %v", err)
1549 }
1550 if len(pb1.Message) != 1 {
1551 t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
1552 }
1553
1554 pb2 := new(pb2.MessageList)
1555 if err := proto.UnmarshalMerge(data, pb2); err != nil {
1556 t.Fatalf("first UnmarshalMerge: %v", err)
1557 }
1558 if err := proto.UnmarshalMerge(data, pb2); err != nil {
1559 t.Fatalf("second UnmarshalMerge: %v", err)
1560 }
1561 if len(pb2.Message) != 2 {
1562 t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
1563 }
1564 }
1565
1566 func TestExtensionMarshalOrder(t *testing.T) {
1567 m := &pb2.MyMessage{Count: proto.Int(123)}
1568 if err := proto.SetExtension(m, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("alpha")}); err != nil {
1569 t.Fatalf("SetExtension: %v", err)
1570 }
1571 if err := proto.SetExtension(m, pb2.E_Ext_Text, proto.String("aleph")); err != nil {
1572 t.Fatalf("SetExtension: %v", err)
1573 }
1574 if err := proto.SetExtension(m, pb2.E_Ext_Number, proto.Int32(1)); err != nil {
1575 t.Fatalf("SetExtension: %v", err)
1576 }
1577
1578
1579 var orig []byte
1580 for i := 0; i < 100; i++ {
1581 b, err := proto.Marshal(m)
1582 if err != nil {
1583 t.Fatalf("Marshal: %v", err)
1584 }
1585 if i == 0 {
1586 orig = b
1587 continue
1588 }
1589 if !bytes.Equal(b, orig) {
1590 t.Errorf("Bytes differ on attempt #%d", i)
1591 }
1592 }
1593 }
1594
1595 func TestExtensionMapFieldMarshalDeterministic(t *testing.T) {
1596 m := &pb2.MyMessage{Count: proto.Int(123)}
1597 if err := proto.SetExtension(m, pb2.E_Ext_More, &pb2.Ext{MapField: map[int32]int32{1: 1, 2: 2, 3: 3, 4: 4}}); err != nil {
1598 t.Fatalf("SetExtension: %v", err)
1599 }
1600 marshal := func(m proto.Message) []byte {
1601 var b proto.Buffer
1602 b.SetDeterministic(true)
1603 if err := b.Marshal(m); err != nil {
1604 t.Fatalf("Marshal failed: %v", err)
1605 }
1606 return b.Bytes()
1607 }
1608
1609 want := marshal(m)
1610 for i := 0; i < 100; i++ {
1611 if got := marshal(m); !bytes.Equal(got, want) {
1612 t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
1613 }
1614 }
1615 }
1616
1617 func TestUnmarshalMergesMessages(t *testing.T) {
1618
1619
1620 a := &pb2.OtherMessage{
1621 Key: proto.Int64(123),
1622 Inner: &pb2.InnerMessage{
1623 Host: proto.String("polhode"),
1624 Port: proto.Int32(1234),
1625 },
1626 }
1627 aData, err := proto.Marshal(a)
1628 if err != nil {
1629 t.Fatalf("Marshal(a): %v", err)
1630 }
1631 b := &pb2.OtherMessage{
1632 Weight: proto.Float32(1.2),
1633 Inner: &pb2.InnerMessage{
1634 Host: proto.String("herpolhode"),
1635 Connected: proto.Bool(true),
1636 },
1637 }
1638 bData, err := proto.Marshal(b)
1639 if err != nil {
1640 t.Fatalf("Marshal(b): %v", err)
1641 }
1642 want := &pb2.OtherMessage{
1643 Key: proto.Int64(123),
1644 Weight: proto.Float32(1.2),
1645 Inner: &pb2.InnerMessage{
1646 Host: proto.String("herpolhode"),
1647 Port: proto.Int32(1234),
1648 Connected: proto.Bool(true),
1649 },
1650 }
1651 got := new(pb2.OtherMessage)
1652 if err := proto.Unmarshal(append(aData, bData...), got); err != nil {
1653 t.Fatalf("Unmarshal: %v", err)
1654 }
1655 if !proto.Equal(got, want) {
1656 t.Errorf("\n got %v\nwant %v", got, want)
1657 }
1658 }
1659
1660 func TestUnmarshalMergesGroups(t *testing.T) {
1661
1662
1663 a := &pb2.GroupNew{
1664 G: &pb2.GroupNew_G{
1665 X: proto.Int32(7),
1666 Y: proto.Int32(8),
1667 },
1668 }
1669 aData, err := proto.Marshal(a)
1670 if err != nil {
1671 t.Fatalf("Marshal(a): %v", err)
1672 }
1673 b := &pb2.GroupNew{
1674 G: &pb2.GroupNew_G{
1675 X: proto.Int32(9),
1676 },
1677 }
1678 bData, err := proto.Marshal(b)
1679 if err != nil {
1680 t.Fatalf("Marshal(b): %v", err)
1681 }
1682 want := &pb2.GroupNew{
1683 G: &pb2.GroupNew_G{
1684 X: proto.Int32(9),
1685 Y: proto.Int32(8),
1686 },
1687 }
1688 got := new(pb2.GroupNew)
1689 if err := proto.Unmarshal(append(aData, bData...), got); err != nil {
1690 t.Fatalf("Unmarshal: %v", err)
1691 }
1692 if !proto.Equal(got, want) {
1693 t.Errorf("\n got %v\nwant %v", got, want)
1694 }
1695 }
1696
1697 func TestEncodingSizes(t *testing.T) {
1698 tests := []struct {
1699 m proto.Message
1700 n int
1701 }{
1702 {&pb2.Defaults{F_Int32: proto.Int32(math.MaxInt32)}, 6},
1703 {&pb2.Defaults{F_Int32: proto.Int32(math.MinInt32)}, 11},
1704 {&pb2.Defaults{F_Uint32: proto.Uint32(uint32(math.MaxInt32) + 1)}, 6},
1705 {&pb2.Defaults{F_Uint32: proto.Uint32(math.MaxUint32)}, 6},
1706 }
1707 for _, test := range tests {
1708 b, err := proto.Marshal(test.m)
1709 if err != nil {
1710 t.Errorf("Marshal(%v): %v", test.m, err)
1711 continue
1712 }
1713 if len(b) != test.n {
1714 t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
1715 }
1716 }
1717 }
1718
1719 func TestRequiredNotSetError(t *testing.T) {
1720 pb := initGoTest(false)
1721 pb.RequiredField.Label = nil
1722 pb.F_Int32Required = nil
1723 pb.F_Int64Required = nil
1724
1725 want := protopack.Message{
1726 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(7),
1727 protopack.Tag{4, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1728 protopack.Tag{2, protopack.BytesType}, protopack.String("type"),
1729 }),
1730 protopack.Tag{10, protopack.VarintType}, protopack.Bool(true),
1731 protopack.Tag{13, protopack.Fixed32Type}, protopack.Uint32(32),
1732 protopack.Tag{14, protopack.Fixed64Type}, protopack.Uint64(64),
1733 protopack.Tag{15, protopack.VarintType}, protopack.Uvarint(3232),
1734 protopack.Tag{16, protopack.VarintType}, protopack.Uvarint(6464),
1735 protopack.Tag{17, protopack.Fixed32Type}, protopack.Float32(3232),
1736 protopack.Tag{18, protopack.Fixed64Type}, protopack.Float64(6464),
1737 protopack.Tag{19, protopack.BytesType}, protopack.String("string"),
1738 protopack.Tag{70, protopack.StartGroupType},
1739 protopack.Message{
1740 protopack.Tag{71, protopack.BytesType}, protopack.String("required"),
1741 },
1742 protopack.Tag{70, protopack.EndGroupType},
1743 protopack.Tag{101, protopack.BytesType}, protopack.Bytes("bytes"),
1744 protopack.Tag{102, protopack.VarintType}, protopack.Svarint(-32),
1745 protopack.Tag{103, protopack.VarintType}, protopack.Svarint(-64),
1746 protopack.Tag{104, protopack.Fixed32Type}, protopack.Int32(-32),
1747 protopack.Tag{105, protopack.Fixed64Type}, protopack.Int64(-64),
1748 }.Marshal()
1749
1750 got, err := proto.Marshal(pb)
1751 if !isRequiredNotSetError(err) {
1752 t.Logf("marshal-1 err = %v, want *RequiredNotSetError", err)
1753 t.Fatalf("got %q\nwant %q", got, want)
1754 }
1755 if !bytes.Equal(got, want) {
1756 t.Fatalf("got %q\nwant %q", got, want)
1757 }
1758
1759
1760 pbd := new(pb2.GoTest)
1761 err = proto.Unmarshal(got, pbd)
1762 if !isRequiredNotSetError(err) {
1763 t.Errorf("unmarshal err = %v, want *RequiredNotSetError", err)
1764 t.Fatalf("got %q\nwant %q", got, want)
1765 }
1766 got, err = proto.Marshal(pbd)
1767 if !isRequiredNotSetError(err) {
1768 t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
1769 t.Fatalf("got %q\nwant %q", got, want)
1770 }
1771 if !bytes.Equal(got, want) {
1772 t.Fatalf("got %q\nwant %q", got, want)
1773 }
1774 }
1775
1776 func TestRequiredNotSetErrorWithBadWireTypes(t *testing.T) {
1777
1778 if err := proto.Unmarshal([]byte{0x08, 0x00}, new(pb2.GoEnum)); err != nil {
1779 t.Errorf("Unmarshal = %v, want nil", err)
1780 }
1781
1782 if err := proto.Unmarshal([]byte{0x0d, 0x00, 0x00, 0x00, 0x00}, new(pb2.GoEnum)); err == nil {
1783 t.Errorf("Unmarshal = nil, want RequiredNotSetError")
1784 }
1785
1786 m := new(pb2.GoEnum)
1787 if err := proto.Unmarshal([]byte{0x08, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00}, m); err != nil {
1788 t.Errorf("Unmarshal = %v, want nil", err)
1789 }
1790 if !bytes.Equal(m.XXX_unrecognized, []byte{0x0d, 0x00, 0x00, 0x00, 0x00}) {
1791 t.Errorf("expected fixed32 to appear as unknown bytes: %x", m.XXX_unrecognized)
1792 }
1793 }
1794
1795 func fuzzUnmarshal(t *testing.T, data []byte) {
1796 defer func() {
1797 if e := recover(); e != nil {
1798 t.Errorf("These bytes caused a panic: %+v", data)
1799 t.Logf("Stack:\n%s", debug.Stack())
1800 t.FailNow()
1801 }
1802 }()
1803
1804 pb := new(pb2.MyMessage)
1805 proto.Unmarshal(data, pb)
1806 }
1807
1808 func TestMapFieldMarshal(t *testing.T) {
1809 m := &pb2.MessageWithMap{
1810 NameMapping: map[int32]string{
1811 1: "Rob",
1812 4: "Ian",
1813 8: "Dave",
1814 },
1815 }
1816 b, err := proto.Marshal(m)
1817 if err != nil {
1818 t.Fatalf("Marshal: %v", err)
1819 }
1820
1821
1822 parts := []string{
1823 "\n\a\b\x01\x12\x03Rob",
1824 "\n\a\b\x04\x12\x03Ian",
1825 "\n\b\b\x08\x12\x04Dave",
1826 }
1827 ok := false
1828 for i := range parts {
1829 for j := range parts {
1830 if j == i {
1831 continue
1832 }
1833 for k := range parts {
1834 if k == i || k == j {
1835 continue
1836 }
1837 try := parts[i] + parts[j] + parts[k]
1838 if bytes.Equal(b, []byte(try)) {
1839 ok = true
1840 break
1841 }
1842 }
1843 }
1844 }
1845 if !ok {
1846 t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
1847 }
1848 t.Logf("FYI b: %q", b)
1849 }
1850
1851 func TestMapFieldDeterministicMarshal(t *testing.T) {
1852 m := &pb2.MessageWithMap{
1853 NameMapping: map[int32]string{
1854 1: "Rob",
1855 4: "Ian",
1856 8: "Dave",
1857 },
1858 }
1859
1860 marshal := func(m proto.Message) []byte {
1861 var b proto.Buffer
1862 b.SetDeterministic(true)
1863 if err := b.Marshal(m); err != nil {
1864 t.Fatalf("Marshal failed: %v", err)
1865 }
1866 return b.Bytes()
1867 }
1868
1869 want := marshal(m)
1870 for i := 0; i < 10; i++ {
1871 if got := marshal(m); !bytes.Equal(got, want) {
1872 t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
1873 }
1874 }
1875 }
1876
1877 func TestMapFieldRoundTrips(t *testing.T) {
1878 m := &pb2.MessageWithMap{
1879 NameMapping: map[int32]string{
1880 1: "Rob",
1881 4: "Ian",
1882 8: "Dave",
1883 },
1884 MsgMapping: map[int64]*pb2.FloatingPoint{
1885 0x7001: {F: proto.Float64(2.0)},
1886 },
1887 ByteMapping: map[bool][]byte{
1888 false: []byte("that's not right!"),
1889 true: []byte("aye, 'tis true!"),
1890 },
1891 }
1892 b, err := proto.Marshal(m)
1893 if err != nil {
1894 t.Fatalf("Marshal: %v", err)
1895 }
1896 t.Logf("FYI b: %q", b)
1897 m2 := new(pb2.MessageWithMap)
1898 if err := proto.Unmarshal(b, m2); err != nil {
1899 t.Fatalf("Unmarshal: %v", err)
1900 }
1901 if !proto.Equal(m, m2) {
1902 t.Errorf("Map did not survive a round trip.\ninitial: %v\n final: %v", m, m2)
1903 }
1904 }
1905
1906 func TestMapFieldWithNil(t *testing.T) {
1907 m1 := &pb2.MessageWithMap{
1908 MsgMapping: map[int64]*pb2.FloatingPoint{
1909 1: nil,
1910 },
1911 }
1912 b, err := proto.Marshal(m1)
1913 if _, ok := err.(*proto.RequiredNotSetError); !ok {
1914 t.Fatalf("Marshal(%v): err=%v, want RequiredNotSet", m1, err)
1915 }
1916 m2 := new(pb2.MessageWithMap)
1917 err = proto.Unmarshal(b, m2)
1918 if _, ok := err.(*proto.RequiredNotSetError); !ok {
1919 t.Fatalf("Unmarshal(%v): err=%v, want RequiredNotSet", m1, err)
1920 }
1921 if !proto.Equal(m1, m2) {
1922 t.Fatalf("roundtrip marshal/unmarshal changed message; got:\n%v\nwant:\n%v", m2, m1)
1923 }
1924 }
1925
1926 func TestMapFieldWithNilBytes(t *testing.T) {
1927 m1 := &pb2.MessageWithMap{
1928 ByteMapping: map[bool][]byte{
1929 false: {},
1930 true: nil,
1931 },
1932 }
1933 n := proto.Size(m1)
1934 b, err := proto.Marshal(m1)
1935 if err != nil {
1936 t.Fatalf("Marshal: %v", err)
1937 }
1938 if n != len(b) {
1939 t.Errorf("Size(m1) = %d; want len(Marshal(m1)) = %d", n, len(b))
1940 }
1941 m2 := new(pb2.MessageWithMap)
1942 if err := proto.Unmarshal(b, m2); err != nil {
1943 t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
1944 }
1945 if v, ok := m2.ByteMapping[false]; !ok {
1946 t.Error("byte_mapping[false] not present")
1947 } else if len(v) != 0 {
1948 t.Errorf("byte_mapping[false] not empty: %#v", v)
1949 }
1950 if v, ok := m2.ByteMapping[true]; !ok {
1951 t.Error("byte_mapping[true] not present")
1952 } else if len(v) != 0 {
1953 t.Errorf("byte_mapping[true] not empty: %#v", v)
1954 }
1955 }
1956
1957 func TestDecodeMapFieldMissingKey(t *testing.T) {
1958 b := []byte{
1959 0x0A, 0x03,
1960
1961 0x12, 0x01, 0x6D,
1962 }
1963 got := &pb2.MessageWithMap{}
1964 err := proto.Unmarshal(b, got)
1965 if err != nil {
1966 t.Fatalf("failed to marshal map with missing key: %v", err)
1967 }
1968 want := &pb2.MessageWithMap{NameMapping: map[int32]string{0: "m"}}
1969 if !proto.Equal(got, want) {
1970 t.Errorf("Unmarshaled map with no key was not as expected. got: %v, want %v", got, want)
1971 }
1972 }
1973
1974 func TestDecodeMapFieldMissingValue(t *testing.T) {
1975 b := protopack.Message{
1976 protopack.Tag{1, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
1977 protopack.Tag{1, protopack.VarintType}, protopack.Uvarint(1),
1978 }),
1979 }.Marshal()
1980 got := &pb2.MessageWithMap{}
1981 err := proto.Unmarshal(b, got)
1982 if err != nil {
1983 t.Fatalf("failed to marshal map with missing value: %v", err)
1984 }
1985 want := &pb2.MessageWithMap{NameMapping: map[int32]string{1: ""}}
1986 if !proto.Equal(got, want) {
1987 t.Errorf("Unmarshaled map with no value was not as expected. got: %v, want %v", got, want)
1988 }
1989 }
1990
1991 func TestOneof(t *testing.T) {
1992 m := &pb2.Communique{}
1993 b, err := proto.Marshal(m)
1994 if err != nil {
1995 t.Fatalf("Marshal of empty message with oneof: %v", err)
1996 }
1997 if len(b) != 0 {
1998 t.Errorf("Marshal of empty message yielded too many bytes: %v", b)
1999 }
2000
2001 m = &pb2.Communique{
2002 Union: &pb2.Communique_Name{"Barry"},
2003 }
2004
2005
2006 b, err = proto.Marshal(m)
2007 if err != nil {
2008 t.Fatalf("Marshal of message with oneof: %v", err)
2009 }
2010 if len(b) != 7 {
2011 t.Errorf("Incorrect marshal of message with oneof: %v", b)
2012 }
2013 m.Reset()
2014 if err := proto.Unmarshal(b, m); err != nil {
2015 t.Fatalf("Unmarshal of message with oneof: %v", err)
2016 }
2017 if x, ok := m.Union.(*pb2.Communique_Name); !ok || x.Name != "Barry" {
2018 t.Errorf("After round trip, Union = %+v", m.Union)
2019 }
2020 if name := m.GetName(); name != "Barry" {
2021 t.Errorf("After round trip, GetName = %q, want %q", name, "Barry")
2022 }
2023
2024
2025 m.Union = &pb2.Communique_Msg{&pb2.Strings{StringField: proto.String("deep deep string")}}
2026 b, err = proto.Marshal(m)
2027 if err != nil {
2028 t.Fatalf("Marshal of message with oneof set to message: %v", err)
2029 }
2030 if len(b) != 20 {
2031 t.Errorf("Incorrect marshal of message with oneof set to message: %v", b)
2032 }
2033 m.Reset()
2034 if err := proto.Unmarshal(b, m); err != nil {
2035 t.Fatalf("Unmarshal of message with oneof set to message: %v", err)
2036 }
2037 ss, ok := m.Union.(*pb2.Communique_Msg)
2038 if !ok || ss.Msg.GetStringField() != "deep deep string" {
2039 t.Errorf("After round trip with oneof set to message, Union = %+v", m.Union)
2040 }
2041 }
2042
2043 func TestOneofNilBytes(t *testing.T) {
2044
2045 m := &pb2.Communique{Union: &pb2.Communique_Data{Data: nil}}
2046 b, err := proto.Marshal(m)
2047 if err != nil {
2048 t.Fatalf("Marshal failed: %v", err)
2049 }
2050 want := protopack.Message{
2051 protopack.Tag{7, protopack.BytesType}, protopack.Bytes(""),
2052 }.Marshal()
2053 if !bytes.Equal(b, want) {
2054 t.Errorf("Wrong result of Marshal: got %x, want %x", b, want)
2055 }
2056 }
2057
2058 func TestInefficientPackedBool(t *testing.T) {
2059
2060 inp := protopack.Message{
2061 protopack.Tag{2, protopack.BytesType}, protopack.Bytes("\xb90"),
2062 }.Marshal()
2063 if err := proto.Unmarshal(inp, new(pb2.MoreRepeated)); err != nil {
2064 t.Error(err)
2065 }
2066 }
2067
2068
2069
2070 func TestRepeatedEnum2(t *testing.T) {
2071 pb := &pb2.RepeatedEnum{
2072 Color: []pb2.RepeatedEnum_Color{pb2.RepeatedEnum_RED},
2073 }
2074 b, err := proto.Marshal(pb)
2075 if err != nil {
2076 t.Fatalf("Marshal failed: %v", err)
2077 }
2078 x := new(pb2.RepeatedEnum)
2079 err = proto.Unmarshal(b, x)
2080 if err != nil {
2081 t.Fatalf("Unmarshal failed: %v", err)
2082 }
2083 if !proto.Equal(pb, x) {
2084 t.Errorf("Incorrect result: want: %v got: %v", pb, x)
2085 }
2086 }
2087
2088
2089
2090 func TestConcurrentMarshal(t *testing.T) {
2091 pb := initGoTest(true)
2092 const N = 100
2093 b := make([][]byte, N)
2094
2095 var wg sync.WaitGroup
2096 for i := 0; i < N; i++ {
2097 wg.Add(1)
2098 go func(i int) {
2099 defer wg.Done()
2100 var err error
2101 b[i], err = proto.Marshal(pb)
2102 if err != nil {
2103 t.Errorf("marshal error: %v", err)
2104 }
2105 }(i)
2106 }
2107
2108 wg.Wait()
2109 for i := 1; i < N; i++ {
2110 if !bytes.Equal(b[0], b[i]) {
2111 t.Errorf("concurrent marshal result not same: b[0] = %v, b[%d] = %v", b[0], i, b[i])
2112 }
2113 }
2114 }
2115
2116 func TestInvalidUTF8(t *testing.T) {
2117 const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"
2118 tests := []struct {
2119 label string
2120 proto2 proto.Message
2121 proto3 proto.Message
2122 want []byte
2123 }{{
2124 label: "Scalar",
2125 proto2: &pb2.TestUTF8{Scalar: proto.String(invalidUTF8)},
2126 proto3: &pb3.TestUTF8{Scalar: invalidUTF8},
2127 want: []byte{0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2128 }, {
2129 label: "Vector",
2130 proto2: &pb2.TestUTF8{Vector: []string{invalidUTF8}},
2131 proto3: &pb3.TestUTF8{Vector: []string{invalidUTF8}},
2132 want: []byte{0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2133 }, {
2134 label: "Oneof",
2135 proto2: &pb2.TestUTF8{Oneof: &pb2.TestUTF8_Field{invalidUTF8}},
2136 proto3: &pb3.TestUTF8{Oneof: &pb3.TestUTF8_Field{invalidUTF8}},
2137 want: []byte{0x1a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2138 }, {
2139 label: "MapKey",
2140 proto2: &pb2.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2141 proto3: &pb3.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2142 want: []byte{0x22, 0x0b, 0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff, 0x10, 0x00},
2143 }, {
2144 label: "MapValue",
2145 proto2: &pb2.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2146 proto3: &pb3.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2147 want: []byte{0x2a, 0x0b, 0x08, 0x00, 0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2148 }}
2149
2150 for _, tt := range tests {
2151
2152 b, err := proto.Marshal(tt.proto2)
2153 if err != nil {
2154 t.Errorf("Marshal(proto2.%s) = %v, want nil", tt.label, err)
2155 }
2156 if !bytes.Equal(b, tt.want) {
2157 t.Errorf("Marshal(proto2.%s) = %x, want %x", tt.label, b, tt.want)
2158 }
2159
2160 m := proto.Clone(tt.proto2)
2161 m.Reset()
2162 if err = proto.Unmarshal(tt.want, m); err != nil {
2163 t.Errorf("Unmarshal(proto2.%s) = %v, want nil", tt.label, err)
2164 }
2165 if !proto.Equal(m, tt.proto2) {
2166 t.Errorf("proto2.%s: output mismatch:\ngot %v\nwant %v", tt.label, m, tt.proto2)
2167 }
2168
2169
2170 if _, err := proto.Marshal(tt.proto3); err == nil {
2171 t.Errorf("Marshal(proto3.%s) = %v, want non-nil", tt.label, err)
2172 }
2173
2174 m = proto.Clone(tt.proto3)
2175 m.Reset()
2176 if err := proto.Unmarshal(tt.want, m); err == nil {
2177 t.Errorf("Unmarshal(proto3.%s) = %v, want non-nil", tt.label, err)
2178 }
2179 }
2180 }
2181
2182 func TestRequired(t *testing.T) {
2183
2184
2185 m := &pb2.GoTest{F_BoolRequired: proto.Bool(true)}
2186 got, err := proto.Marshal(m)
2187 if !isRequiredNotSetError(err) {
2188 t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
2189 }
2190 if want := []byte{0x50, 0x01}; !bytes.Equal(got, want) {
2191 t.Errorf("Marshal() = %x, want %x", got, want)
2192 }
2193
2194 m = new(pb2.GoTest)
2195 err = proto.Unmarshal(got, m)
2196 if !isRequiredNotSetError(err) {
2197 t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
2198 }
2199 if !m.GetF_BoolRequired() {
2200 t.Error("m.F_BoolRequired = false, want true")
2201 }
2202 }
2203
2204 func TestUnknownV2(t *testing.T) {
2205 m := new(tspb.Timestamp)
2206 m.ProtoReflect().SetUnknown([]byte("\x92\x4d\x12unknown field 1234"))
2207 got := proto.CompactTextString(m)
2208 if !strings.Contains(got, "unknown field 1234") {
2209 t.Errorf("got %q, want contains %q", got, "unknown field 1234")
2210 }
2211 }
2212
2213 func testMsg() *pb2.GoTest {
2214 pb := initGoTest(true)
2215 const N = 1000
2216 pb.F_Int32Repeated = make([]int32, N)
2217 pb.F_DoubleRepeated = make([]float64, N)
2218 for i := 0; i < N; i++ {
2219 pb.F_Int32Repeated[i] = int32(i)
2220 pb.F_DoubleRepeated[i] = float64(i)
2221 }
2222 return pb
2223 }
2224
2225 func bytesMsg() *pb2.GoTest {
2226 pb := initGoTest(true)
2227 buf := make([]byte, 4000)
2228 for i := range buf {
2229 buf[i] = byte(i)
2230 }
2231 pb.F_BytesDefaulted = buf
2232 return pb
2233 }
2234
2235 func benchmarkMarshal(b *testing.B, pb proto.Message, marshal func(proto.Message) ([]byte, error)) {
2236 d, _ := marshal(pb)
2237 b.SetBytes(int64(len(d)))
2238 b.ResetTimer()
2239 for i := 0; i < b.N; i++ {
2240 marshal(pb)
2241 }
2242 }
2243
2244 func benchmarkBufferMarshal(b *testing.B, pb proto.Message) {
2245 p := proto.NewBuffer(nil)
2246 benchmarkMarshal(b, pb, func(pb0 proto.Message) ([]byte, error) {
2247 p.Reset()
2248 err := p.Marshal(pb0)
2249 return p.Bytes(), err
2250 })
2251 }
2252
2253 func benchmarkSize(b *testing.B, pb proto.Message) {
2254 benchmarkMarshal(b, pb, func(pb0 proto.Message) ([]byte, error) {
2255 proto.Size(pb)
2256 return nil, nil
2257 })
2258 }
2259
2260 func TestProto3ZeroValues(t *testing.T) {
2261 tests := []struct {
2262 desc string
2263 m proto.Message
2264 }{
2265 {"zero message", &pb3.Message{}},
2266 {"empty bytes field", &pb3.Message{Data: []byte{}}},
2267 }
2268 for _, test := range tests {
2269 b, err := proto.Marshal(test.m)
2270 if err != nil {
2271 t.Errorf("%s: proto.Marshal: %v", test.desc, err)
2272 continue
2273 }
2274 if len(b) > 0 {
2275 t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
2276 }
2277 }
2278 }
2279
2280 func TestRoundTripProto3(t *testing.T) {
2281 m := &pb3.Message{
2282 Name: "David",
2283 Hilarity: pb3.Message_PUNS,
2284 HeightInCm: 178,
2285 Data: []byte("roboto"),
2286 ResultCount: 47,
2287 TrueScotsman: true,
2288 Score: 8.1,
2289
2290 Key: []uint64{1, 0xdeadbeef},
2291 Nested: &pb3.Nested{
2292 Bunny: "Monty",
2293 },
2294 }
2295 t.Logf(" m: %v", m)
2296
2297 b, err := proto.Marshal(m)
2298 if err != nil {
2299 t.Fatalf("proto.Marshal: %v", err)
2300 }
2301 t.Logf(" b: %q", b)
2302
2303 m2 := new(pb3.Message)
2304 if err := proto.Unmarshal(b, m2); err != nil {
2305 t.Fatalf("proto.Unmarshal: %v", err)
2306 }
2307 t.Logf("m2: %v", m2)
2308
2309 if !proto.Equal(m, m2) {
2310 t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
2311 }
2312 }
2313
2314 func TestGettersForBasicTypesExist(t *testing.T) {
2315 var m pb3.Message
2316 if got := m.GetNested().GetBunny(); got != "" {
2317 t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
2318 }
2319 if got := m.GetNested().GetCute(); got {
2320 t.Errorf("m.GetNested().GetCute() = %t, want false", got)
2321 }
2322 }
2323
2324 func TestProto3SetDefaults(t *testing.T) {
2325 in := &pb3.Message{
2326 Terrain: map[string]*pb3.Nested{
2327 "meadow": new(pb3.Nested),
2328 },
2329 Proto2Field: new(pb2.SubDefaults),
2330 Proto2Value: map[string]*pb2.SubDefaults{
2331 "badlands": new(pb2.SubDefaults),
2332 },
2333 }
2334
2335 got := proto.Clone(in).(*pb3.Message)
2336 proto.SetDefaults(got)
2337
2338
2339
2340 want := &pb3.Message{
2341 Terrain: map[string]*pb3.Nested{
2342 "meadow": new(pb3.Nested),
2343 },
2344 Proto2Field: &pb2.SubDefaults{N: proto.Int64(7)},
2345 Proto2Value: map[string]*pb2.SubDefaults{
2346 "badlands": &pb2.SubDefaults{N: proto.Int64(7)},
2347 },
2348 }
2349
2350 if !proto.Equal(got, want) {
2351 t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
2352 }
2353 }
2354
2355 func TestUnknownFieldPreservation(t *testing.T) {
2356 b1 := "\x0a\x05David"
2357 b2 := "\xc2\x0c\x06Google"
2358 b := []byte(b1 + b2)
2359
2360 m := new(pb3.Message)
2361 if err := proto.Unmarshal(b, m); err != nil {
2362 t.Fatalf("proto.Unmarshal: %v", err)
2363 }
2364
2365 if !bytes.Equal(m.XXX_unrecognized, []byte(b2)) {
2366 t.Fatalf("mismatching unknown fields:\ngot %q\nwant %q", m.XXX_unrecognized, b2)
2367 }
2368 }
2369
2370 func TestMap(t *testing.T) {
2371 b := protopack.Message{
2372 protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
2373 protopack.Tag{1, protopack.BytesType}, protopack.String("Key1"),
2374 protopack.Tag{2, protopack.BytesType}, protopack.String("Val1"),
2375 }),
2376 protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
2377 protopack.Tag{1, protopack.BytesType}, protopack.String("Key2"),
2378 protopack.Tag{2, protopack.BytesType}, protopack.String("Val2a"),
2379 protopack.Tag{2, protopack.BytesType}, protopack.String("Val2"),
2380 }),
2381 protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
2382 protopack.Tag{1, protopack.BytesType}, protopack.String("Key3"),
2383 protopack.Tag{1, protopack.Fixed32Type}, protopack.Uint32(5),
2384 protopack.Tag{2, protopack.BytesType}, protopack.String("Val3b"),
2385 protopack.Tag{3, protopack.BytesType}, protopack.Bytes("Val3a"),
2386 protopack.Tag{2, protopack.BytesType}, protopack.String("Val3"),
2387 protopack.Tag{2, protopack.Fixed32Type}, protopack.Uint32(5),
2388 }),
2389 protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix{},
2390 protopack.Tag{20, protopack.BytesType}, protopack.LengthPrefix(protopack.Message{
2391 protopack.Tag{1, protopack.BytesType}, protopack.String("Key4"),
2392 protopack.Tag{2, protopack.StartGroupType},
2393 protopack.Message{
2394 protopack.Tag{1, protopack.BytesType}, protopack.Bytes("SomeURL"),
2395 protopack.Tag{2, protopack.BytesType}, protopack.Bytes("SomeTitle"),
2396 protopack.Tag{3, protopack.BytesType}, protopack.Bytes("Snippet1"),
2397 },
2398 protopack.Tag{2, protopack.EndGroupType},
2399 }),
2400 }.Marshal()
2401
2402 var m pb3.Message
2403 if err := proto.Unmarshal(b, &m); err != nil {
2404 t.Fatalf("proto.Unmarshal error: %v", err)
2405 }
2406
2407 got := m.StringMap
2408 want := map[string]string{
2409 "": "",
2410 "Key1": "Val1",
2411 "Key2": "Val2",
2412 "Key3": "Val3",
2413 "Key4": "",
2414 }
2415
2416 if !reflect.DeepEqual(got, want) {
2417 t.Errorf("maps differ:\ngot %#v\nwant %#v", got, want)
2418 }
2419 }
2420
2421 func marshalled() []byte {
2422 m := &pb3.IntMaps{}
2423 for i := 0; i < 1000; i++ {
2424 m.Maps = append(m.Maps, &pb3.IntMap{
2425 Rtt: map[int32]int32{1: 2},
2426 })
2427 }
2428 b, err := proto.Marshal(m)
2429 if err != nil {
2430 panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
2431 }
2432 return b
2433 }
2434
2435 var messageWithExtension1 = &pb2.MyMessage{Count: proto.Int32(7)}
2436
2437
2438 var messageWithExtension3 = &pb2.MyMessage{Count: proto.Int32(8)}
2439
2440 func init() {
2441 if err := proto.SetExtension(messageWithExtension1, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("Abbott")}); err != nil {
2442 log.Panicf("proto.SetExtension: %v", err)
2443 }
2444 if err := proto.SetExtension(messageWithExtension3, pb2.E_Ext_More, &pb2.Ext{Data: proto.String("Costello")}); err != nil {
2445 log.Panicf("proto.SetExtension: %v", err)
2446 }
2447
2448
2449 proto.Marshal(messageWithExtension3)
2450
2451 }
2452
2453
2454 type nonptrMessage struct{}
2455
2456 func (m nonptrMessage) ProtoMessage() {}
2457 func (m nonptrMessage) Reset() {}
2458 func (m nonptrMessage) String() string { return "" }
2459
2460 func (m nonptrMessage) Marshal() ([]byte, error) {
2461 return []byte{42}, nil
2462 }
2463
2464 var SizeTests = []struct {
2465 desc string
2466 pb proto.Message
2467 }{
2468 {"empty", &pb2.OtherMessage{}},
2469
2470 {"bool", &pb2.Defaults{F_Bool: proto.Bool(true)}},
2471 {"int32", &pb2.Defaults{F_Int32: proto.Int32(12)}},
2472 {"negative int32", &pb2.Defaults{F_Int32: proto.Int32(-1)}},
2473 {"small int64", &pb2.Defaults{F_Int64: proto.Int64(1)}},
2474 {"big int64", &pb2.Defaults{F_Int64: proto.Int64(1 << 20)}},
2475 {"negative int64", &pb2.Defaults{F_Int64: proto.Int64(-1)}},
2476 {"fixed32", &pb2.Defaults{F_Fixed32: proto.Uint32(71)}},
2477 {"fixed64", &pb2.Defaults{F_Fixed64: proto.Uint64(72)}},
2478 {"uint32", &pb2.Defaults{F_Uint32: proto.Uint32(123)}},
2479 {"uint64", &pb2.Defaults{F_Uint64: proto.Uint64(124)}},
2480 {"float", &pb2.Defaults{F_Float: proto.Float32(12.6)}},
2481 {"double", &pb2.Defaults{F_Double: proto.Float64(13.9)}},
2482 {"string", &pb2.Defaults{F_String: proto.String("niles")}},
2483 {"bytes", &pb2.Defaults{F_Bytes: []byte("wowsa")}},
2484 {"bytes, empty", &pb2.Defaults{F_Bytes: []byte{}}},
2485 {"sint32", &pb2.Defaults{F_Sint32: proto.Int32(65)}},
2486 {"sint64", &pb2.Defaults{F_Sint64: proto.Int64(67)}},
2487 {"enum", &pb2.Defaults{F_Enum: pb2.Defaults_BLUE.Enum()}},
2488
2489 {"empty repeated bool", &pb2.MoreRepeated{Bools: []bool{}}},
2490 {"repeated bool", &pb2.MoreRepeated{Bools: []bool{false, true, true, false}}},
2491 {"packed repeated bool", &pb2.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
2492 {"repeated int32", &pb2.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
2493 {"repeated int32 packed", &pb2.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
2494 {"repeated int64 packed", &pb2.MoreRepeated{Int64SPacked: []int64{
2495
2496
2497 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
2498 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
2499 }}},
2500 {"repeated string", &pb2.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
2501 {"repeated fixed", &pb2.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
2502
2503 {"nested", &pb2.OldMessage{Nested: &pb2.OldMessage_Nested{Name: proto.String("whatever")}}},
2504 {"group", &pb2.GroupOld{G: &pb2.GroupOld_G{X: proto.Int32(12345)}}},
2505
2506 {"unrecognized", &pb2.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
2507 {"extension (unencoded)", messageWithExtension1},
2508 {"extension (encoded)", messageWithExtension3},
2509
2510 {"proto3 empty", &pb3.Message{}},
2511 {"proto3 bool", &pb3.Message{TrueScotsman: true}},
2512 {"proto3 int64", &pb3.Message{ResultCount: 1}},
2513 {"proto3 uint32", &pb3.Message{HeightInCm: 123}},
2514 {"proto3 float", &pb3.Message{Score: 12.6}},
2515 {"proto3 string", &pb3.Message{Name: "Snezana"}},
2516 {"proto3 bytes", &pb3.Message{Data: []byte("wowsa")}},
2517 {"proto3 bytes, empty", &pb3.Message{Data: []byte{}}},
2518 {"proto3 enum", &pb3.Message{Hilarity: pb3.Message_PUNS}},
2519 {"proto3 map field with empty bytes", &pb3.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},
2520
2521 {"map field", &pb2.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
2522 {"map field with message", &pb2.MessageWithMap{MsgMapping: map[int64]*pb2.FloatingPoint{0x7001: &pb2.FloatingPoint{F: proto.Float64(2.0)}}}},
2523 {"map field with bytes", &pb2.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
2524 {"map field with empty bytes", &pb2.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},
2525
2526 {"map field with big entry", &pb2.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
2527 {"map field with big key and val", &pb2.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
2528 {"map field with big numeric key", &pb2.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},
2529
2530 {"oneof not set", &pb2.Oneof{}},
2531 {"oneof bool", &pb2.Oneof{Union: &pb2.Oneof_F_Bool{true}}},
2532 {"oneof zero int32", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{0}}},
2533 {"oneof big int32", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{1 << 20}}},
2534 {"oneof int64", &pb2.Oneof{Union: &pb2.Oneof_F_Int64{42}}},
2535 {"oneof fixed32", &pb2.Oneof{Union: &pb2.Oneof_F_Fixed32{43}}},
2536 {"oneof fixed64", &pb2.Oneof{Union: &pb2.Oneof_F_Fixed64{44}}},
2537 {"oneof uint32", &pb2.Oneof{Union: &pb2.Oneof_F_Uint32{45}}},
2538 {"oneof uint64", &pb2.Oneof{Union: &pb2.Oneof_F_Uint64{46}}},
2539 {"oneof float", &pb2.Oneof{Union: &pb2.Oneof_F_Float{47.1}}},
2540 {"oneof double", &pb2.Oneof{Union: &pb2.Oneof_F_Double{48.9}}},
2541 {"oneof string", &pb2.Oneof{Union: &pb2.Oneof_F_String{"Rhythmic Fman"}}},
2542 {"oneof bytes", &pb2.Oneof{Union: &pb2.Oneof_F_Bytes{[]byte("let go")}}},
2543 {"oneof sint32", &pb2.Oneof{Union: &pb2.Oneof_F_Sint32{50}}},
2544 {"oneof sint64", &pb2.Oneof{Union: &pb2.Oneof_F_Sint64{51}}},
2545 {"oneof enum", &pb2.Oneof{Union: &pb2.Oneof_F_Enum{pb2.MyMessage_BLUE}}},
2546 {"message for oneof", &pb2.GoTestField{Label: proto.String("k"), Type: proto.String("v")}},
2547 {"oneof message", &pb2.Oneof{Union: &pb2.Oneof_F_Message{&pb2.GoTestField{Label: proto.String("k"), Type: proto.String("v")}}}},
2548 {"oneof group", &pb2.Oneof{Union: &pb2.Oneof_FGroup{&pb2.Oneof_F_Group{X: proto.Int32(52)}}}},
2549 {"oneof largest tag", &pb2.Oneof{Union: &pb2.Oneof_F_Largest_Tag{1}}},
2550 {"multiple oneofs", &pb2.Oneof{Union: &pb2.Oneof_F_Int32{1}, Tormato: &pb2.Oneof_Value{2}}},
2551
2552 {"non-pointer message", nonptrMessage{}},
2553 }
2554
2555 func TestSize(t *testing.T) {
2556 for _, tc := range SizeTests {
2557 t.Run(tc.desc, func(t *testing.T) {
2558 size := proto.Size(tc.pb)
2559 b, err := proto.Marshal(tc.pb)
2560 if err != nil {
2561 t.Errorf("%v: Marshal failed: %v", tc.desc, err)
2562 return
2563 }
2564 if size != len(b) {
2565 t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
2566 t.Logf("%v: bytes: %#v", tc.desc, b)
2567 }
2568 })
2569 }
2570 }
2571
2572 func TestVarintSize(t *testing.T) {
2573
2574 testCases := []struct {
2575 n uint64
2576 size int
2577 }{
2578 {0, 1},
2579 {1, 1},
2580 {127, 1},
2581 {128, 2},
2582 {16383, 2},
2583 {16384, 3},
2584 {math.MaxInt64, 9},
2585 {math.MaxInt64 + 1, 10},
2586 }
2587 for _, tc := range testCases {
2588 size := proto.SizeVarint(tc.n)
2589 if size != tc.size {
2590 t.Errorf("sizeVarint(%d) = %d, want %d", tc.n, size, tc.size)
2591 }
2592 }
2593 }
2594
View as plain text