• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 The Go Authors.  All rights reserved.
4// https://github.com/golang/protobuf
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
10//     * Redistributions of source code must retain the above copyright
11// notice, this list of conditions and the following disclaimer.
12//     * Redistributions in binary form must reproduce the above
13// copyright notice, this list of conditions and the following disclaimer
14// in the documentation and/or other materials provided with the
15// distribution.
16//     * Neither the name of Google Inc. nor the names of its
17// contributors may be used to endorse or promote products derived from
18// this software without specific prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32package proto_test
33
34import (
35	"bytes"
36	"encoding/json"
37	"errors"
38	"fmt"
39	"math"
40	"math/rand"
41	"reflect"
42	"runtime/debug"
43	"strings"
44	"sync"
45	"testing"
46	"time"
47
48	. "github.com/golang/protobuf/proto"
49	pb3 "github.com/golang/protobuf/proto/proto3_proto"
50	. "github.com/golang/protobuf/proto/test_proto"
51)
52
53var globalO *Buffer
54
55func old() *Buffer {
56	if globalO == nil {
57		globalO = NewBuffer(nil)
58	}
59	globalO.Reset()
60	return globalO
61}
62
63func equalbytes(b1, b2 []byte, t *testing.T) {
64	if len(b1) != len(b2) {
65		t.Errorf("wrong lengths: 2*%d != %d", len(b1), len(b2))
66		return
67	}
68	for i := 0; i < len(b1); i++ {
69		if b1[i] != b2[i] {
70			t.Errorf("bad byte[%d]:%x %x: %s %s", i, b1[i], b2[i], b1, b2)
71		}
72	}
73}
74
75func initGoTestField() *GoTestField {
76	f := new(GoTestField)
77	f.Label = String("label")
78	f.Type = String("type")
79	return f
80}
81
82// These are all structurally equivalent but the tag numbers differ.
83// (It's remarkable that required, optional, and repeated all have
84// 8 letters.)
85func initGoTest_RequiredGroup() *GoTest_RequiredGroup {
86	return &GoTest_RequiredGroup{
87		RequiredField: String("required"),
88	}
89}
90
91func initGoTest_OptionalGroup() *GoTest_OptionalGroup {
92	return &GoTest_OptionalGroup{
93		RequiredField: String("optional"),
94	}
95}
96
97func initGoTest_RepeatedGroup() *GoTest_RepeatedGroup {
98	return &GoTest_RepeatedGroup{
99		RequiredField: String("repeated"),
100	}
101}
102
103func initGoTest(setdefaults bool) *GoTest {
104	pb := new(GoTest)
105	if setdefaults {
106		pb.F_BoolDefaulted = Bool(Default_GoTest_F_BoolDefaulted)
107		pb.F_Int32Defaulted = Int32(Default_GoTest_F_Int32Defaulted)
108		pb.F_Int64Defaulted = Int64(Default_GoTest_F_Int64Defaulted)
109		pb.F_Fixed32Defaulted = Uint32(Default_GoTest_F_Fixed32Defaulted)
110		pb.F_Fixed64Defaulted = Uint64(Default_GoTest_F_Fixed64Defaulted)
111		pb.F_Uint32Defaulted = Uint32(Default_GoTest_F_Uint32Defaulted)
112		pb.F_Uint64Defaulted = Uint64(Default_GoTest_F_Uint64Defaulted)
113		pb.F_FloatDefaulted = Float32(Default_GoTest_F_FloatDefaulted)
114		pb.F_DoubleDefaulted = Float64(Default_GoTest_F_DoubleDefaulted)
115		pb.F_StringDefaulted = String(Default_GoTest_F_StringDefaulted)
116		pb.F_BytesDefaulted = Default_GoTest_F_BytesDefaulted
117		pb.F_Sint32Defaulted = Int32(Default_GoTest_F_Sint32Defaulted)
118		pb.F_Sint64Defaulted = Int64(Default_GoTest_F_Sint64Defaulted)
119		pb.F_Sfixed32Defaulted = Int32(Default_GoTest_F_Sfixed32Defaulted)
120		pb.F_Sfixed64Defaulted = Int64(Default_GoTest_F_Sfixed64Defaulted)
121	}
122
123	pb.Kind = GoTest_TIME.Enum()
124	pb.RequiredField = initGoTestField()
125	pb.F_BoolRequired = Bool(true)
126	pb.F_Int32Required = Int32(3)
127	pb.F_Int64Required = Int64(6)
128	pb.F_Fixed32Required = Uint32(32)
129	pb.F_Fixed64Required = Uint64(64)
130	pb.F_Uint32Required = Uint32(3232)
131	pb.F_Uint64Required = Uint64(6464)
132	pb.F_FloatRequired = Float32(3232)
133	pb.F_DoubleRequired = Float64(6464)
134	pb.F_StringRequired = String("string")
135	pb.F_BytesRequired = []byte("bytes")
136	pb.F_Sint32Required = Int32(-32)
137	pb.F_Sint64Required = Int64(-64)
138	pb.F_Sfixed32Required = Int32(-32)
139	pb.F_Sfixed64Required = Int64(-64)
140	pb.Requiredgroup = initGoTest_RequiredGroup()
141
142	return pb
143}
144
145func hex(c uint8) uint8 {
146	if '0' <= c && c <= '9' {
147		return c - '0'
148	}
149	if 'a' <= c && c <= 'f' {
150		return 10 + c - 'a'
151	}
152	if 'A' <= c && c <= 'F' {
153		return 10 + c - 'A'
154	}
155	return 0
156}
157
158func equal(b []byte, s string, t *testing.T) bool {
159	if 2*len(b) != len(s) {
160		//		fail(fmt.Sprintf("wrong lengths: 2*%d != %d", len(b), len(s)), b, s, t)
161		fmt.Printf("wrong lengths: 2*%d != %d\n", len(b), len(s))
162		return false
163	}
164	for i, j := 0, 0; i < len(b); i, j = i+1, j+2 {
165		x := hex(s[j])*16 + hex(s[j+1])
166		if b[i] != x {
167			//			fail(fmt.Sprintf("bad byte[%d]:%x %x", i, b[i], x), b, s, t)
168			fmt.Printf("bad byte[%d]:%x %x", i, b[i], x)
169			return false
170		}
171	}
172	return true
173}
174
175func overify(t *testing.T, pb *GoTest, expected string) {
176	o := old()
177	err := o.Marshal(pb)
178	if err != nil {
179		fmt.Printf("overify marshal-1 err = %v", err)
180		o.DebugPrint("", o.Bytes())
181		t.Fatalf("expected = %s", expected)
182	}
183	if !equal(o.Bytes(), expected, t) {
184		o.DebugPrint("overify neq 1", o.Bytes())
185		t.Fatalf("expected = %s", expected)
186	}
187
188	// Now test Unmarshal by recreating the original buffer.
189	pbd := new(GoTest)
190	err = o.Unmarshal(pbd)
191	if err != nil {
192		t.Fatalf("overify unmarshal err = %v", err)
193		o.DebugPrint("", o.Bytes())
194		t.Fatalf("string = %s", expected)
195	}
196	o.Reset()
197	err = o.Marshal(pbd)
198	if err != nil {
199		t.Errorf("overify marshal-2 err = %v", err)
200		o.DebugPrint("", o.Bytes())
201		t.Fatalf("string = %s", expected)
202	}
203	if !equal(o.Bytes(), expected, t) {
204		o.DebugPrint("overify neq 2", o.Bytes())
205		t.Fatalf("string = %s", expected)
206	}
207}
208
209// Simple tests for numeric encode/decode primitives (varint, etc.)
210func TestNumericPrimitives(t *testing.T) {
211	for i := uint64(0); i < 1e6; i += 111 {
212		o := old()
213		if o.EncodeVarint(i) != nil {
214			t.Error("EncodeVarint")
215			break
216		}
217		x, e := o.DecodeVarint()
218		if e != nil {
219			t.Fatal("DecodeVarint")
220		}
221		if x != i {
222			t.Fatal("varint decode fail:", i, x)
223		}
224
225		o = old()
226		if o.EncodeFixed32(i) != nil {
227			t.Fatal("encFixed32")
228		}
229		x, e = o.DecodeFixed32()
230		if e != nil {
231			t.Fatal("decFixed32")
232		}
233		if x != i {
234			t.Fatal("fixed32 decode fail:", i, x)
235		}
236
237		o = old()
238		if o.EncodeFixed64(i*1234567) != nil {
239			t.Error("encFixed64")
240			break
241		}
242		x, e = o.DecodeFixed64()
243		if e != nil {
244			t.Error("decFixed64")
245			break
246		}
247		if x != i*1234567 {
248			t.Error("fixed64 decode fail:", i*1234567, x)
249			break
250		}
251
252		o = old()
253		i32 := int32(i - 12345)
254		if o.EncodeZigzag32(uint64(i32)) != nil {
255			t.Fatal("EncodeZigzag32")
256		}
257		x, e = o.DecodeZigzag32()
258		if e != nil {
259			t.Fatal("DecodeZigzag32")
260		}
261		if x != uint64(uint32(i32)) {
262			t.Fatal("zigzag32 decode fail:", i32, x)
263		}
264
265		o = old()
266		i64 := int64(i - 12345)
267		if o.EncodeZigzag64(uint64(i64)) != nil {
268			t.Fatal("EncodeZigzag64")
269		}
270		x, e = o.DecodeZigzag64()
271		if e != nil {
272			t.Fatal("DecodeZigzag64")
273		}
274		if x != uint64(i64) {
275			t.Fatal("zigzag64 decode fail:", i64, x)
276		}
277	}
278}
279
280// fakeMarshaler is a simple struct implementing Marshaler and Message interfaces.
281type fakeMarshaler struct {
282	b   []byte
283	err error
284}
285
286func (f *fakeMarshaler) Marshal() ([]byte, error) { return f.b, f.err }
287func (f *fakeMarshaler) String() string           { return fmt.Sprintf("Bytes: %v Error: %v", f.b, f.err) }
288func (f *fakeMarshaler) ProtoMessage()            {}
289func (f *fakeMarshaler) Reset()                   {}
290
291type msgWithFakeMarshaler struct {
292	M *fakeMarshaler `protobuf:"bytes,1,opt,name=fake"`
293}
294
295func (m *msgWithFakeMarshaler) String() string { return CompactTextString(m) }
296func (m *msgWithFakeMarshaler) ProtoMessage()  {}
297func (m *msgWithFakeMarshaler) Reset()         {}
298
299// Simple tests for proto messages that implement the Marshaler interface.
300func TestMarshalerEncoding(t *testing.T) {
301	tests := []struct {
302		name    string
303		m       Message
304		want    []byte
305		errType reflect.Type
306	}{
307		{
308			name: "Marshaler that fails",
309			m: &fakeMarshaler{
310				err: errors.New("some marshal err"),
311				b:   []byte{5, 6, 7},
312			},
313			// Since the Marshal method returned bytes, they should be written to the
314			// buffer.  (For efficiency, we assume that Marshal implementations are
315			// always correct w.r.t. RequiredNotSetError and output.)
316			want:    []byte{5, 6, 7},
317			errType: reflect.TypeOf(errors.New("some marshal err")),
318		},
319		{
320			name: "Marshaler that fails with RequiredNotSetError",
321			m: &msgWithFakeMarshaler{
322				M: &fakeMarshaler{
323					err: &RequiredNotSetError{},
324					b:   []byte{5, 6, 7},
325				},
326			},
327			// Since there's an error that can be continued after,
328			// the buffer should be written.
329			want: []byte{
330				10, 3, // for &msgWithFakeMarshaler
331				5, 6, 7, // for &fakeMarshaler
332			},
333			errType: reflect.TypeOf(&RequiredNotSetError{}),
334		},
335		{
336			name: "Marshaler that succeeds",
337			m: &fakeMarshaler{
338				b: []byte{0, 1, 2, 3, 4, 127, 255},
339			},
340			want: []byte{0, 1, 2, 3, 4, 127, 255},
341		},
342	}
343	for _, test := range tests {
344		b := NewBuffer(nil)
345		err := b.Marshal(test.m)
346		if reflect.TypeOf(err) != test.errType {
347			t.Errorf("%s: got err %T(%v) wanted %T", test.name, err, err, test.errType)
348		}
349		if !reflect.DeepEqual(test.want, b.Bytes()) {
350			t.Errorf("%s: got bytes %v wanted %v", test.name, b.Bytes(), test.want)
351		}
352		if size := Size(test.m); size != len(b.Bytes()) {
353			t.Errorf("%s: Size(_) = %v, but marshaled to %v bytes", test.name, size, len(b.Bytes()))
354		}
355
356		m, mErr := Marshal(test.m)
357		if !bytes.Equal(b.Bytes(), m) {
358			t.Errorf("%s: Marshal returned %v, but (*Buffer).Marshal wrote %v", test.name, m, b.Bytes())
359		}
360		if !reflect.DeepEqual(err, mErr) {
361			t.Errorf("%s: Marshal err = %q, but (*Buffer).Marshal returned %q",
362				test.name, fmt.Sprint(mErr), fmt.Sprint(err))
363		}
364	}
365}
366
367// Ensure that Buffer.Marshal uses O(N) memory for N messages
368func TestBufferMarshalAllocs(t *testing.T) {
369	value := &OtherMessage{Key: Int64(1)}
370	msg := &MyMessage{Count: Int32(1), Others: []*OtherMessage{value}}
371
372	reallocSize := func(t *testing.T, items int, prealloc int) (int64, int64) {
373		var b Buffer
374		b.SetBuf(make([]byte, 0, prealloc))
375
376		var allocSpace int64
377		prevCap := cap(b.Bytes())
378		for i := 0; i < items; i++ {
379			err := b.Marshal(msg)
380			if err != nil {
381				t.Errorf("Marshal err = %q", err)
382				break
383			}
384			if c := cap(b.Bytes()); prevCap != c {
385				allocSpace += int64(c)
386				prevCap = c
387			}
388		}
389		needSpace := int64(len(b.Bytes()))
390		return allocSpace, needSpace
391	}
392
393	for _, prealloc := range []int{0, 100, 10000} {
394		for _, items := range []int{1, 2, 5, 10, 20, 50, 100, 200, 500, 1000} {
395			runtimeSpace, need := reallocSize(t, items, prealloc)
396			totalSpace := int64(prealloc) + runtimeSpace
397
398			runtimeRatio := float64(runtimeSpace) / float64(need)
399			totalRatio := float64(totalSpace) / float64(need)
400
401			if totalRatio < 1 || runtimeRatio > 4 {
402				t.Errorf("needed %dB, allocated %dB total (ratio %.1f), allocated %dB at runtime (ratio %.1f)",
403					need, totalSpace, totalRatio, runtimeSpace, runtimeRatio)
404			}
405		}
406	}
407}
408
409// Simple tests for bytes
410func TestBytesPrimitives(t *testing.T) {
411	o := old()
412	bytes := []byte{'n', 'o', 'w', ' ', 'i', 's', ' ', 't', 'h', 'e', ' ', 't', 'i', 'm', 'e'}
413	if o.EncodeRawBytes(bytes) != nil {
414		t.Error("EncodeRawBytes")
415	}
416	decb, e := o.DecodeRawBytes(false)
417	if e != nil {
418		t.Error("DecodeRawBytes")
419	}
420	equalbytes(bytes, decb, t)
421}
422
423// Simple tests for strings
424func TestStringPrimitives(t *testing.T) {
425	o := old()
426	s := "now is the time"
427	if o.EncodeStringBytes(s) != nil {
428		t.Error("enc_string")
429	}
430	decs, e := o.DecodeStringBytes()
431	if e != nil {
432		t.Error("dec_string")
433	}
434	if s != decs {
435		t.Error("string encode/decode fail:", s, decs)
436	}
437}
438
439// Do we catch the "required bit not set" case?
440func TestRequiredBit(t *testing.T) {
441	o := old()
442	pb := new(GoTest)
443	err := o.Marshal(pb)
444	if err == nil {
445		t.Error("did not catch missing required fields")
446	} else if !strings.Contains(err.Error(), "Kind") {
447		t.Error("wrong error type:", err)
448	}
449}
450
451// Check that all fields are nil.
452// Clearly silly, and a residue from a more interesting test with an earlier,
453// different initialization property, but it once caught a compiler bug so
454// it lives.
455func checkInitialized(pb *GoTest, t *testing.T) {
456	if pb.F_BoolDefaulted != nil {
457		t.Error("New or Reset did not set boolean:", *pb.F_BoolDefaulted)
458	}
459	if pb.F_Int32Defaulted != nil {
460		t.Error("New or Reset did not set int32:", *pb.F_Int32Defaulted)
461	}
462	if pb.F_Int64Defaulted != nil {
463		t.Error("New or Reset did not set int64:", *pb.F_Int64Defaulted)
464	}
465	if pb.F_Fixed32Defaulted != nil {
466		t.Error("New or Reset did not set fixed32:", *pb.F_Fixed32Defaulted)
467	}
468	if pb.F_Fixed64Defaulted != nil {
469		t.Error("New or Reset did not set fixed64:", *pb.F_Fixed64Defaulted)
470	}
471	if pb.F_Uint32Defaulted != nil {
472		t.Error("New or Reset did not set uint32:", *pb.F_Uint32Defaulted)
473	}
474	if pb.F_Uint64Defaulted != nil {
475		t.Error("New or Reset did not set uint64:", *pb.F_Uint64Defaulted)
476	}
477	if pb.F_FloatDefaulted != nil {
478		t.Error("New or Reset did not set float:", *pb.F_FloatDefaulted)
479	}
480	if pb.F_DoubleDefaulted != nil {
481		t.Error("New or Reset did not set double:", *pb.F_DoubleDefaulted)
482	}
483	if pb.F_StringDefaulted != nil {
484		t.Error("New or Reset did not set string:", *pb.F_StringDefaulted)
485	}
486	if pb.F_BytesDefaulted != nil {
487		t.Error("New or Reset did not set bytes:", string(pb.F_BytesDefaulted))
488	}
489	if pb.F_Sint32Defaulted != nil {
490		t.Error("New or Reset did not set int32:", *pb.F_Sint32Defaulted)
491	}
492	if pb.F_Sint64Defaulted != nil {
493		t.Error("New or Reset did not set int64:", *pb.F_Sint64Defaulted)
494	}
495}
496
497// Does Reset() reset?
498func TestReset(t *testing.T) {
499	pb := initGoTest(true)
500	// muck with some values
501	pb.F_BoolDefaulted = Bool(false)
502	pb.F_Int32Defaulted = Int32(237)
503	pb.F_Int64Defaulted = Int64(12346)
504	pb.F_Fixed32Defaulted = Uint32(32000)
505	pb.F_Fixed64Defaulted = Uint64(666)
506	pb.F_Uint32Defaulted = Uint32(323232)
507	pb.F_Uint64Defaulted = nil
508	pb.F_FloatDefaulted = nil
509	pb.F_DoubleDefaulted = Float64(0)
510	pb.F_StringDefaulted = String("gotcha")
511	pb.F_BytesDefaulted = []byte("asdfasdf")
512	pb.F_Sint32Defaulted = Int32(123)
513	pb.F_Sint64Defaulted = Int64(789)
514	pb.Reset()
515	checkInitialized(pb, t)
516}
517
518// All required fields set, no defaults provided.
519func TestEncodeDecode1(t *testing.T) {
520	pb := initGoTest(false)
521	overify(t, pb,
522		"0807"+ // field 1, encoding 0, value 7
523			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
524			"5001"+ // field 10, encoding 0, value 1
525			"5803"+ // field 11, encoding 0, value 3
526			"6006"+ // field 12, encoding 0, value 6
527			"6d20000000"+ // field 13, encoding 5, value 0x20
528			"714000000000000000"+ // field 14, encoding 1, value 0x40
529			"78a019"+ // field 15, encoding 0, value 0xca0 = 3232
530			"8001c032"+ // field 16, encoding 0, value 0x1940 = 6464
531			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
532			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
533			"9a0106"+"737472696e67"+ // field 19, encoding 2, string "string"
534			"b304"+ // field 70, encoding 3, start group
535			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
536			"b404"+ // field 70, encoding 4, end group
537			"aa0605"+"6279746573"+ // field 101, encoding 2, string "bytes"
538			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
539			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
540			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
541			"c906c0ffffffffffffff") // field 105, encoding 1, -64 fixed64
542}
543
544// All required fields set, defaults provided.
545func TestEncodeDecode2(t *testing.T) {
546	pb := initGoTest(true)
547	overify(t, pb,
548		"0807"+ // field 1, encoding 0, value 7
549			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
550			"5001"+ // field 10, encoding 0, value 1
551			"5803"+ // field 11, encoding 0, value 3
552			"6006"+ // field 12, encoding 0, value 6
553			"6d20000000"+ // field 13, encoding 5, value 32
554			"714000000000000000"+ // field 14, encoding 1, value 64
555			"78a019"+ // field 15, encoding 0, value 3232
556			"8001c032"+ // field 16, encoding 0, value 6464
557			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
558			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
559			"9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
560			"c00201"+ // field 40, encoding 0, value 1
561			"c80220"+ // field 41, encoding 0, value 32
562			"d00240"+ // field 42, encoding 0, value 64
563			"dd0240010000"+ // field 43, encoding 5, value 320
564			"e1028002000000000000"+ // field 44, encoding 1, value 640
565			"e8028019"+ // field 45, encoding 0, value 3200
566			"f0028032"+ // field 46, encoding 0, value 6400
567			"fd02e0659948"+ // field 47, encoding 5, value 314159.0
568			"81030000000050971041"+ // field 48, encoding 1, value 271828.0
569			"8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
570			"b304"+ // start group field 70 level 1
571			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
572			"b404"+ // end group field 70 level 1
573			"aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
574			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
575			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
576			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
577			"c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
578			"8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
579			"90193f"+ // field 402, encoding 0, value 63
580			"98197f"+ // field 403, encoding 0, value 127
581			"a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
582			"a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
583
584}
585
586// All default fields set to their default value by hand
587func TestEncodeDecode3(t *testing.T) {
588	pb := initGoTest(false)
589	pb.F_BoolDefaulted = Bool(true)
590	pb.F_Int32Defaulted = Int32(32)
591	pb.F_Int64Defaulted = Int64(64)
592	pb.F_Fixed32Defaulted = Uint32(320)
593	pb.F_Fixed64Defaulted = Uint64(640)
594	pb.F_Uint32Defaulted = Uint32(3200)
595	pb.F_Uint64Defaulted = Uint64(6400)
596	pb.F_FloatDefaulted = Float32(314159)
597	pb.F_DoubleDefaulted = Float64(271828)
598	pb.F_StringDefaulted = String("hello, \"world!\"\n")
599	pb.F_BytesDefaulted = []byte("Bignose")
600	pb.F_Sint32Defaulted = Int32(-32)
601	pb.F_Sint64Defaulted = Int64(-64)
602	pb.F_Sfixed32Defaulted = Int32(-32)
603	pb.F_Sfixed64Defaulted = Int64(-64)
604
605	overify(t, pb,
606		"0807"+ // field 1, encoding 0, value 7
607			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
608			"5001"+ // field 10, encoding 0, value 1
609			"5803"+ // field 11, encoding 0, value 3
610			"6006"+ // field 12, encoding 0, value 6
611			"6d20000000"+ // field 13, encoding 5, value 32
612			"714000000000000000"+ // field 14, encoding 1, value 64
613			"78a019"+ // field 15, encoding 0, value 3232
614			"8001c032"+ // field 16, encoding 0, value 6464
615			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
616			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
617			"9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
618			"c00201"+ // field 40, encoding 0, value 1
619			"c80220"+ // field 41, encoding 0, value 32
620			"d00240"+ // field 42, encoding 0, value 64
621			"dd0240010000"+ // field 43, encoding 5, value 320
622			"e1028002000000000000"+ // field 44, encoding 1, value 640
623			"e8028019"+ // field 45, encoding 0, value 3200
624			"f0028032"+ // field 46, encoding 0, value 6400
625			"fd02e0659948"+ // field 47, encoding 5, value 314159.0
626			"81030000000050971041"+ // field 48, encoding 1, value 271828.0
627			"8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
628			"b304"+ // start group field 70 level 1
629			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
630			"b404"+ // end group field 70 level 1
631			"aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
632			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
633			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
634			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
635			"c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
636			"8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
637			"90193f"+ // field 402, encoding 0, value 63
638			"98197f"+ // field 403, encoding 0, value 127
639			"a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
640			"a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
641
642}
643
644// All required fields set, defaults provided, all non-defaulted optional fields have values.
645func TestEncodeDecode4(t *testing.T) {
646	pb := initGoTest(true)
647	pb.Table = String("hello")
648	pb.Param = Int32(7)
649	pb.OptionalField = initGoTestField()
650	pb.F_BoolOptional = Bool(true)
651	pb.F_Int32Optional = Int32(32)
652	pb.F_Int64Optional = Int64(64)
653	pb.F_Fixed32Optional = Uint32(3232)
654	pb.F_Fixed64Optional = Uint64(6464)
655	pb.F_Uint32Optional = Uint32(323232)
656	pb.F_Uint64Optional = Uint64(646464)
657	pb.F_FloatOptional = Float32(32.)
658	pb.F_DoubleOptional = Float64(64.)
659	pb.F_StringOptional = String("hello")
660	pb.F_BytesOptional = []byte("Bignose")
661	pb.F_Sint32Optional = Int32(-32)
662	pb.F_Sint64Optional = Int64(-64)
663	pb.F_Sfixed32Optional = Int32(-32)
664	pb.F_Sfixed64Optional = Int64(-64)
665	pb.Optionalgroup = initGoTest_OptionalGroup()
666
667	overify(t, pb,
668		"0807"+ // field 1, encoding 0, value 7
669			"1205"+"68656c6c6f"+ // field 2, encoding 2, string "hello"
670			"1807"+ // field 3, encoding 0, value 7
671			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
672			"320d"+"0a056c6162656c120474797065"+ // field 6, encoding 2 (GoTestField)
673			"5001"+ // field 10, encoding 0, value 1
674			"5803"+ // field 11, encoding 0, value 3
675			"6006"+ // field 12, encoding 0, value 6
676			"6d20000000"+ // field 13, encoding 5, value 32
677			"714000000000000000"+ // field 14, encoding 1, value 64
678			"78a019"+ // field 15, encoding 0, value 3232
679			"8001c032"+ // field 16, encoding 0, value 6464
680			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
681			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
682			"9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
683			"f00101"+ // field 30, encoding 0, value 1
684			"f80120"+ // field 31, encoding 0, value 32
685			"800240"+ // field 32, encoding 0, value 64
686			"8d02a00c0000"+ // field 33, encoding 5, value 3232
687			"91024019000000000000"+ // field 34, encoding 1, value 6464
688			"9802a0dd13"+ // field 35, encoding 0, value 323232
689			"a002c0ba27"+ // field 36, encoding 0, value 646464
690			"ad0200000042"+ // field 37, encoding 5, value 32.0
691			"b1020000000000005040"+ // field 38, encoding 1, value 64.0
692			"ba0205"+"68656c6c6f"+ // field 39, encoding 2, string "hello"
693			"c00201"+ // field 40, encoding 0, value 1
694			"c80220"+ // field 41, encoding 0, value 32
695			"d00240"+ // field 42, encoding 0, value 64
696			"dd0240010000"+ // field 43, encoding 5, value 320
697			"e1028002000000000000"+ // field 44, encoding 1, value 640
698			"e8028019"+ // field 45, encoding 0, value 3200
699			"f0028032"+ // field 46, encoding 0, value 6400
700			"fd02e0659948"+ // field 47, encoding 5, value 314159.0
701			"81030000000050971041"+ // field 48, encoding 1, value 271828.0
702			"8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
703			"b304"+ // start group field 70 level 1
704			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
705			"b404"+ // end group field 70 level 1
706			"d305"+ // start group field 90 level 1
707			"da0508"+"6f7074696f6e616c"+ // field 91, encoding 2, string "optional"
708			"d405"+ // end group field 90 level 1
709			"aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
710			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
711			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
712			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
713			"c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
714			"ea1207"+"4269676e6f7365"+ // field 301, encoding 2, string "Bignose"
715			"f0123f"+ // field 302, encoding 0, value 63
716			"f8127f"+ // field 303, encoding 0, value 127
717			"8513e0ffffff"+ // field 304, encoding 5, -32 fixed32
718			"8913c0ffffffffffffff"+ // field 305, encoding 1, -64 fixed64
719			"8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
720			"90193f"+ // field 402, encoding 0, value 63
721			"98197f"+ // field 403, encoding 0, value 127
722			"a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
723			"a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
724
725}
726
727// All required fields set, defaults provided, all repeated fields given two values.
728func TestEncodeDecode5(t *testing.T) {
729	pb := initGoTest(true)
730	pb.RepeatedField = []*GoTestField{initGoTestField(), initGoTestField()}
731	pb.F_BoolRepeated = []bool{false, true}
732	pb.F_Int32Repeated = []int32{32, 33}
733	pb.F_Int64Repeated = []int64{64, 65}
734	pb.F_Fixed32Repeated = []uint32{3232, 3333}
735	pb.F_Fixed64Repeated = []uint64{6464, 6565}
736	pb.F_Uint32Repeated = []uint32{323232, 333333}
737	pb.F_Uint64Repeated = []uint64{646464, 656565}
738	pb.F_FloatRepeated = []float32{32., 33.}
739	pb.F_DoubleRepeated = []float64{64., 65.}
740	pb.F_StringRepeated = []string{"hello", "sailor"}
741	pb.F_BytesRepeated = [][]byte{[]byte("big"), []byte("nose")}
742	pb.F_Sint32Repeated = []int32{32, -32}
743	pb.F_Sint64Repeated = []int64{64, -64}
744	pb.F_Sfixed32Repeated = []int32{32, -32}
745	pb.F_Sfixed64Repeated = []int64{64, -64}
746	pb.Repeatedgroup = []*GoTest_RepeatedGroup{initGoTest_RepeatedGroup(), initGoTest_RepeatedGroup()}
747
748	overify(t, pb,
749		"0807"+ // field 1, encoding 0, value 7
750			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
751			"2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
752			"2a0d"+"0a056c6162656c120474797065"+ // field 5, encoding 2 (GoTestField)
753			"5001"+ // field 10, encoding 0, value 1
754			"5803"+ // field 11, encoding 0, value 3
755			"6006"+ // field 12, encoding 0, value 6
756			"6d20000000"+ // field 13, encoding 5, value 32
757			"714000000000000000"+ // field 14, encoding 1, value 64
758			"78a019"+ // field 15, encoding 0, value 3232
759			"8001c032"+ // field 16, encoding 0, value 6464
760			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
761			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
762			"9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
763			"a00100"+ // field 20, encoding 0, value 0
764			"a00101"+ // field 20, encoding 0, value 1
765			"a80120"+ // field 21, encoding 0, value 32
766			"a80121"+ // field 21, encoding 0, value 33
767			"b00140"+ // field 22, encoding 0, value 64
768			"b00141"+ // field 22, encoding 0, value 65
769			"bd01a00c0000"+ // field 23, encoding 5, value 3232
770			"bd01050d0000"+ // field 23, encoding 5, value 3333
771			"c1014019000000000000"+ // field 24, encoding 1, value 6464
772			"c101a519000000000000"+ // field 24, encoding 1, value 6565
773			"c801a0dd13"+ // field 25, encoding 0, value 323232
774			"c80195ac14"+ // field 25, encoding 0, value 333333
775			"d001c0ba27"+ // field 26, encoding 0, value 646464
776			"d001b58928"+ // field 26, encoding 0, value 656565
777			"dd0100000042"+ // field 27, encoding 5, value 32.0
778			"dd0100000442"+ // field 27, encoding 5, value 33.0
779			"e1010000000000005040"+ // field 28, encoding 1, value 64.0
780			"e1010000000000405040"+ // field 28, encoding 1, value 65.0
781			"ea0105"+"68656c6c6f"+ // field 29, encoding 2, string "hello"
782			"ea0106"+"7361696c6f72"+ // field 29, encoding 2, string "sailor"
783			"c00201"+ // field 40, encoding 0, value 1
784			"c80220"+ // field 41, encoding 0, value 32
785			"d00240"+ // field 42, encoding 0, value 64
786			"dd0240010000"+ // field 43, encoding 5, value 320
787			"e1028002000000000000"+ // field 44, encoding 1, value 640
788			"e8028019"+ // field 45, encoding 0, value 3200
789			"f0028032"+ // field 46, encoding 0, value 6400
790			"fd02e0659948"+ // field 47, encoding 5, value 314159.0
791			"81030000000050971041"+ // field 48, encoding 1, value 271828.0
792			"8a0310"+"68656c6c6f2c2022776f726c6421220a"+ // field 49, encoding 2 string "hello, \"world!\"\n"
793			"b304"+ // start group field 70 level 1
794			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
795			"b404"+ // end group field 70 level 1
796			"8305"+ // start group field 80 level 1
797			"8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
798			"8405"+ // end group field 80 level 1
799			"8305"+ // start group field 80 level 1
800			"8a0508"+"7265706561746564"+ // field 81, encoding 2, string "repeated"
801			"8405"+ // end group field 80 level 1
802			"aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
803			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
804			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
805			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
806			"c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
807			"ca0c03"+"626967"+ // field 201, encoding 2, string "big"
808			"ca0c04"+"6e6f7365"+ // field 201, encoding 2, string "nose"
809			"d00c40"+ // field 202, encoding 0, value 32
810			"d00c3f"+ // field 202, encoding 0, value -32
811			"d80c8001"+ // field 203, encoding 0, value 64
812			"d80c7f"+ // field 203, encoding 0, value -64
813			"e50c20000000"+ // field 204, encoding 5, 32 fixed32
814			"e50ce0ffffff"+ // field 204, encoding 5, -32 fixed32
815			"e90c4000000000000000"+ // field 205, encoding 1, 64 fixed64
816			"e90cc0ffffffffffffff"+ // field 205, encoding 1, -64 fixed64
817			"8a1907"+"4269676e6f7365"+ // field 401, encoding 2, string "Bignose"
818			"90193f"+ // field 402, encoding 0, value 63
819			"98197f"+ // field 403, encoding 0, value 127
820			"a519e0ffffff"+ // field 404, encoding 5, -32 fixed32
821			"a919c0ffffffffffffff") // field 405, encoding 1, -64 fixed64
822
823}
824
825// All required fields set, all packed repeated fields given two values.
826func TestEncodeDecode6(t *testing.T) {
827	pb := initGoTest(false)
828	pb.F_BoolRepeatedPacked = []bool{false, true}
829	pb.F_Int32RepeatedPacked = []int32{32, 33}
830	pb.F_Int64RepeatedPacked = []int64{64, 65}
831	pb.F_Fixed32RepeatedPacked = []uint32{3232, 3333}
832	pb.F_Fixed64RepeatedPacked = []uint64{6464, 6565}
833	pb.F_Uint32RepeatedPacked = []uint32{323232, 333333}
834	pb.F_Uint64RepeatedPacked = []uint64{646464, 656565}
835	pb.F_FloatRepeatedPacked = []float32{32., 33.}
836	pb.F_DoubleRepeatedPacked = []float64{64., 65.}
837	pb.F_Sint32RepeatedPacked = []int32{32, -32}
838	pb.F_Sint64RepeatedPacked = []int64{64, -64}
839	pb.F_Sfixed32RepeatedPacked = []int32{32, -32}
840	pb.F_Sfixed64RepeatedPacked = []int64{64, -64}
841
842	overify(t, pb,
843		"0807"+ // field 1, encoding 0, value 7
844			"220d"+"0a056c6162656c120474797065"+ // field 4, encoding 2 (GoTestField)
845			"5001"+ // field 10, encoding 0, value 1
846			"5803"+ // field 11, encoding 0, value 3
847			"6006"+ // field 12, encoding 0, value 6
848			"6d20000000"+ // field 13, encoding 5, value 32
849			"714000000000000000"+ // field 14, encoding 1, value 64
850			"78a019"+ // field 15, encoding 0, value 3232
851			"8001c032"+ // field 16, encoding 0, value 6464
852			"8d0100004a45"+ // field 17, encoding 5, value 3232.0
853			"9101000000000040b940"+ // field 18, encoding 1, value 6464.0
854			"9a0106"+"737472696e67"+ // field 19, encoding 2 string "string"
855			"9203020001"+ // field 50, encoding 2, 2 bytes, value 0, value 1
856			"9a03022021"+ // field 51, encoding 2, 2 bytes, value 32, value 33
857			"a203024041"+ // field 52, encoding 2, 2 bytes, value 64, value 65
858			"aa0308"+ // field 53, encoding 2, 8 bytes
859			"a00c0000050d0000"+ // value 3232, value 3333
860			"b20310"+ // field 54, encoding 2, 16 bytes
861			"4019000000000000a519000000000000"+ // value 6464, value 6565
862			"ba0306"+ // field 55, encoding 2, 6 bytes
863			"a0dd1395ac14"+ // value 323232, value 333333
864			"c20306"+ // field 56, encoding 2, 6 bytes
865			"c0ba27b58928"+ // value 646464, value 656565
866			"ca0308"+ // field 57, encoding 2, 8 bytes
867			"0000004200000442"+ // value 32.0, value 33.0
868			"d20310"+ // field 58, encoding 2, 16 bytes
869			"00000000000050400000000000405040"+ // value 64.0, value 65.0
870			"b304"+ // start group field 70 level 1
871			"ba0408"+"7265717569726564"+ // field 71, encoding 2, string "required"
872			"b404"+ // end group field 70 level 1
873			"aa0605"+"6279746573"+ // field 101, encoding 2 string "bytes"
874			"b0063f"+ // field 102, encoding 0, 0x3f zigzag32
875			"b8067f"+ // field 103, encoding 0, 0x7f zigzag64
876			"c506e0ffffff"+ // field 104, encoding 5, -32 fixed32
877			"c906c0ffffffffffffff"+ // field 105, encoding 1, -64 fixed64
878			"b21f02"+ // field 502, encoding 2, 2 bytes
879			"403f"+ // value 32, value -32
880			"ba1f03"+ // field 503, encoding 2, 3 bytes
881			"80017f"+ // value 64, value -64
882			"c21f08"+ // field 504, encoding 2, 8 bytes
883			"20000000e0ffffff"+ // value 32, value -32
884			"ca1f10"+ // field 505, encoding 2, 16 bytes
885			"4000000000000000c0ffffffffffffff") // value 64, value -64
886
887}
888
889// Test that we can encode empty bytes fields.
890func TestEncodeDecodeBytes1(t *testing.T) {
891	pb := initGoTest(false)
892
893	// Create our bytes
894	pb.F_BytesRequired = []byte{}
895	pb.F_BytesRepeated = [][]byte{{}}
896	pb.F_BytesOptional = []byte{}
897
898	d, err := Marshal(pb)
899	if err != nil {
900		t.Error(err)
901	}
902
903	pbd := new(GoTest)
904	if err := Unmarshal(d, pbd); err != nil {
905		t.Error(err)
906	}
907
908	if pbd.F_BytesRequired == nil || len(pbd.F_BytesRequired) != 0 {
909		t.Error("required empty bytes field is incorrect")
910	}
911	if pbd.F_BytesRepeated == nil || len(pbd.F_BytesRepeated) == 1 && pbd.F_BytesRepeated[0] == nil {
912		t.Error("repeated empty bytes field is incorrect")
913	}
914	if pbd.F_BytesOptional == nil || len(pbd.F_BytesOptional) != 0 {
915		t.Error("optional empty bytes field is incorrect")
916	}
917}
918
919// Test that we encode nil-valued fields of a repeated bytes field correctly.
920// Since entries in a repeated field cannot be nil, nil must mean empty value.
921func TestEncodeDecodeBytes2(t *testing.T) {
922	pb := initGoTest(false)
923
924	// Create our bytes
925	pb.F_BytesRepeated = [][]byte{nil}
926
927	d, err := Marshal(pb)
928	if err != nil {
929		t.Error(err)
930	}
931
932	pbd := new(GoTest)
933	if err := Unmarshal(d, pbd); err != nil {
934		t.Error(err)
935	}
936
937	if len(pbd.F_BytesRepeated) != 1 || pbd.F_BytesRepeated[0] == nil {
938		t.Error("Unexpected value for repeated bytes field")
939	}
940}
941
942// All required fields set, defaults provided, all repeated fields given two values.
943func TestSkippingUnrecognizedFields(t *testing.T) {
944	o := old()
945	pb := initGoTestField()
946
947	// Marshal it normally.
948	o.Marshal(pb)
949
950	// Now new a GoSkipTest record.
951	skip := &GoSkipTest{
952		SkipInt32:   Int32(32),
953		SkipFixed32: Uint32(3232),
954		SkipFixed64: Uint64(6464),
955		SkipString:  String("skipper"),
956		Skipgroup: &GoSkipTest_SkipGroup{
957			GroupInt32:  Int32(75),
958			GroupString: String("wxyz"),
959		},
960	}
961
962	// Marshal it into same buffer.
963	o.Marshal(skip)
964
965	pbd := new(GoTestField)
966	o.Unmarshal(pbd)
967
968	// The __unrecognized field should be a marshaling of GoSkipTest
969	skipd := new(GoSkipTest)
970
971	o.SetBuf(pbd.XXX_unrecognized)
972	o.Unmarshal(skipd)
973
974	if *skipd.SkipInt32 != *skip.SkipInt32 {
975		t.Error("skip int32", skipd.SkipInt32)
976	}
977	if *skipd.SkipFixed32 != *skip.SkipFixed32 {
978		t.Error("skip fixed32", skipd.SkipFixed32)
979	}
980	if *skipd.SkipFixed64 != *skip.SkipFixed64 {
981		t.Error("skip fixed64", skipd.SkipFixed64)
982	}
983	if *skipd.SkipString != *skip.SkipString {
984		t.Error("skip string", *skipd.SkipString)
985	}
986	if *skipd.Skipgroup.GroupInt32 != *skip.Skipgroup.GroupInt32 {
987		t.Error("skip group int32", skipd.Skipgroup.GroupInt32)
988	}
989	if *skipd.Skipgroup.GroupString != *skip.Skipgroup.GroupString {
990		t.Error("skip group string", *skipd.Skipgroup.GroupString)
991	}
992}
993
994// Check that unrecognized fields of a submessage are preserved.
995func TestSubmessageUnrecognizedFields(t *testing.T) {
996	nm := &NewMessage{
997		Nested: &NewMessage_Nested{
998			Name:      String("Nigel"),
999			FoodGroup: String("carbs"),
1000		},
1001	}
1002	b, err := Marshal(nm)
1003	if err != nil {
1004		t.Fatalf("Marshal of NewMessage: %v", err)
1005	}
1006
1007	// Unmarshal into an OldMessage.
1008	om := new(OldMessage)
1009	if err := Unmarshal(b, om); err != nil {
1010		t.Fatalf("Unmarshal to OldMessage: %v", err)
1011	}
1012	exp := &OldMessage{
1013		Nested: &OldMessage_Nested{
1014			Name: String("Nigel"),
1015			// normal protocol buffer users should not do this
1016			XXX_unrecognized: []byte("\x12\x05carbs"),
1017		},
1018	}
1019	if !Equal(om, exp) {
1020		t.Errorf("om = %v, want %v", om, exp)
1021	}
1022
1023	// Clone the OldMessage.
1024	om = Clone(om).(*OldMessage)
1025	if !Equal(om, exp) {
1026		t.Errorf("Clone(om) = %v, want %v", om, exp)
1027	}
1028
1029	// Marshal the OldMessage, then unmarshal it into an empty NewMessage.
1030	if b, err = Marshal(om); err != nil {
1031		t.Fatalf("Marshal of OldMessage: %v", err)
1032	}
1033	t.Logf("Marshal(%v) -> %q", om, b)
1034	nm2 := new(NewMessage)
1035	if err := Unmarshal(b, nm2); err != nil {
1036		t.Fatalf("Unmarshal to NewMessage: %v", err)
1037	}
1038	if !Equal(nm, nm2) {
1039		t.Errorf("NewMessage round-trip: %v => %v", nm, nm2)
1040	}
1041}
1042
1043// Check that an int32 field can be upgraded to an int64 field.
1044func TestNegativeInt32(t *testing.T) {
1045	om := &OldMessage{
1046		Num: Int32(-1),
1047	}
1048	b, err := Marshal(om)
1049	if err != nil {
1050		t.Fatalf("Marshal of OldMessage: %v", err)
1051	}
1052
1053	// Check the size. It should be 11 bytes;
1054	// 1 for the field/wire type, and 10 for the negative number.
1055	if len(b) != 11 {
1056		t.Errorf("%v marshaled as %q, wanted 11 bytes", om, b)
1057	}
1058
1059	// Unmarshal into a NewMessage.
1060	nm := new(NewMessage)
1061	if err := Unmarshal(b, nm); err != nil {
1062		t.Fatalf("Unmarshal to NewMessage: %v", err)
1063	}
1064	want := &NewMessage{
1065		Num: Int64(-1),
1066	}
1067	if !Equal(nm, want) {
1068		t.Errorf("nm = %v, want %v", nm, want)
1069	}
1070}
1071
1072// Check that we can grow an array (repeated field) to have many elements.
1073// This test doesn't depend only on our encoding; for variety, it makes sure
1074// we create, encode, and decode the correct contents explicitly.  It's therefore
1075// a bit messier.
1076// This test also uses (and hence tests) the Marshal/Unmarshal functions
1077// instead of the methods.
1078func TestBigRepeated(t *testing.T) {
1079	pb := initGoTest(true)
1080
1081	// Create the arrays
1082	const N = 50 // Internally the library starts much smaller.
1083	pb.Repeatedgroup = make([]*GoTest_RepeatedGroup, N)
1084	pb.F_Sint64Repeated = make([]int64, N)
1085	pb.F_Sint32Repeated = make([]int32, N)
1086	pb.F_BytesRepeated = make([][]byte, N)
1087	pb.F_StringRepeated = make([]string, N)
1088	pb.F_DoubleRepeated = make([]float64, N)
1089	pb.F_FloatRepeated = make([]float32, N)
1090	pb.F_Uint64Repeated = make([]uint64, N)
1091	pb.F_Uint32Repeated = make([]uint32, N)
1092	pb.F_Fixed64Repeated = make([]uint64, N)
1093	pb.F_Fixed32Repeated = make([]uint32, N)
1094	pb.F_Int64Repeated = make([]int64, N)
1095	pb.F_Int32Repeated = make([]int32, N)
1096	pb.F_BoolRepeated = make([]bool, N)
1097	pb.RepeatedField = make([]*GoTestField, N)
1098
1099	// Fill in the arrays with checkable values.
1100	igtf := initGoTestField()
1101	igtrg := initGoTest_RepeatedGroup()
1102	for i := 0; i < N; i++ {
1103		pb.Repeatedgroup[i] = igtrg
1104		pb.F_Sint64Repeated[i] = int64(i)
1105		pb.F_Sint32Repeated[i] = int32(i)
1106		s := fmt.Sprint(i)
1107		pb.F_BytesRepeated[i] = []byte(s)
1108		pb.F_StringRepeated[i] = s
1109		pb.F_DoubleRepeated[i] = float64(i)
1110		pb.F_FloatRepeated[i] = float32(i)
1111		pb.F_Uint64Repeated[i] = uint64(i)
1112		pb.F_Uint32Repeated[i] = uint32(i)
1113		pb.F_Fixed64Repeated[i] = uint64(i)
1114		pb.F_Fixed32Repeated[i] = uint32(i)
1115		pb.F_Int64Repeated[i] = int64(i)
1116		pb.F_Int32Repeated[i] = int32(i)
1117		pb.F_BoolRepeated[i] = i%2 == 0
1118		pb.RepeatedField[i] = igtf
1119	}
1120
1121	// Marshal.
1122	buf, _ := Marshal(pb)
1123
1124	// Now test Unmarshal by recreating the original buffer.
1125	pbd := new(GoTest)
1126	Unmarshal(buf, pbd)
1127
1128	// Check the checkable values
1129	for i := uint64(0); i < N; i++ {
1130		if pbd.Repeatedgroup[i] == nil { // TODO: more checking?
1131			t.Error("pbd.Repeatedgroup bad")
1132		}
1133		if x := uint64(pbd.F_Sint64Repeated[i]); x != i {
1134			t.Error("pbd.F_Sint64Repeated bad", x, i)
1135		}
1136		if x := uint64(pbd.F_Sint32Repeated[i]); x != i {
1137			t.Error("pbd.F_Sint32Repeated bad", x, i)
1138		}
1139		s := fmt.Sprint(i)
1140		equalbytes(pbd.F_BytesRepeated[i], []byte(s), t)
1141		if pbd.F_StringRepeated[i] != s {
1142			t.Error("pbd.F_Sint32Repeated bad", pbd.F_StringRepeated[i], i)
1143		}
1144		if x := uint64(pbd.F_DoubleRepeated[i]); x != i {
1145			t.Error("pbd.F_DoubleRepeated bad", x, i)
1146		}
1147		if x := uint64(pbd.F_FloatRepeated[i]); x != i {
1148			t.Error("pbd.F_FloatRepeated bad", x, i)
1149		}
1150		if x := pbd.F_Uint64Repeated[i]; x != i {
1151			t.Error("pbd.F_Uint64Repeated bad", x, i)
1152		}
1153		if x := uint64(pbd.F_Uint32Repeated[i]); x != i {
1154			t.Error("pbd.F_Uint32Repeated bad", x, i)
1155		}
1156		if x := pbd.F_Fixed64Repeated[i]; x != i {
1157			t.Error("pbd.F_Fixed64Repeated bad", x, i)
1158		}
1159		if x := uint64(pbd.F_Fixed32Repeated[i]); x != i {
1160			t.Error("pbd.F_Fixed32Repeated bad", x, i)
1161		}
1162		if x := uint64(pbd.F_Int64Repeated[i]); x != i {
1163			t.Error("pbd.F_Int64Repeated bad", x, i)
1164		}
1165		if x := uint64(pbd.F_Int32Repeated[i]); x != i {
1166			t.Error("pbd.F_Int32Repeated bad", x, i)
1167		}
1168		if x := pbd.F_BoolRepeated[i]; x != (i%2 == 0) {
1169			t.Error("pbd.F_BoolRepeated bad", x, i)
1170		}
1171		if pbd.RepeatedField[i] == nil { // TODO: more checking?
1172			t.Error("pbd.RepeatedField bad")
1173		}
1174	}
1175}
1176
1177func TestBadWireTypeUnknown(t *testing.T) {
1178	var b []byte
1179	fmt.Sscanf("0a01780d00000000080b101612036161611521000000202c220362626225370000002203636363214200000000000000584d5a036464645900000000000056405d63000000", "%x", &b)
1180
1181	m := new(MyMessage)
1182	if err := Unmarshal(b, m); err != nil {
1183		t.Errorf("unexpected Unmarshal error: %v", err)
1184	}
1185
1186	var unknown []byte
1187	fmt.Sscanf("0a01780d0000000010161521000000202c2537000000214200000000000000584d5a036464645d63000000", "%x", &unknown)
1188	if !bytes.Equal(m.XXX_unrecognized, unknown) {
1189		t.Errorf("unknown bytes mismatch:\ngot  %x\nwant %x", m.XXX_unrecognized, unknown)
1190	}
1191	DiscardUnknown(m)
1192
1193	want := &MyMessage{Count: Int32(11), Name: String("aaa"), Pet: []string{"bbb", "ccc"}, Bigfloat: Float64(88)}
1194	if !Equal(m, want) {
1195		t.Errorf("message mismatch:\ngot  %v\nwant %v", m, want)
1196	}
1197}
1198
1199func encodeDecode(t *testing.T, in, out Message, msg string) {
1200	buf, err := Marshal(in)
1201	if err != nil {
1202		t.Fatalf("failed marshaling %v: %v", msg, err)
1203	}
1204	if err := Unmarshal(buf, out); err != nil {
1205		t.Fatalf("failed unmarshaling %v: %v", msg, err)
1206	}
1207}
1208
1209func TestPackedNonPackedDecoderSwitching(t *testing.T) {
1210	np, p := new(NonPackedTest), new(PackedTest)
1211
1212	// non-packed -> packed
1213	np.A = []int32{0, 1, 1, 2, 3, 5}
1214	encodeDecode(t, np, p, "non-packed -> packed")
1215	if !reflect.DeepEqual(np.A, p.B) {
1216		t.Errorf("failed non-packed -> packed; np.A=%+v, p.B=%+v", np.A, p.B)
1217	}
1218
1219	// packed -> non-packed
1220	np.Reset()
1221	p.B = []int32{3, 1, 4, 1, 5, 9}
1222	encodeDecode(t, p, np, "packed -> non-packed")
1223	if !reflect.DeepEqual(p.B, np.A) {
1224		t.Errorf("failed packed -> non-packed; p.B=%+v, np.A=%+v", p.B, np.A)
1225	}
1226}
1227
1228func TestProto1RepeatedGroup(t *testing.T) {
1229	pb := &MessageList{
1230		Message: []*MessageList_Message{
1231			{
1232				Name:  String("blah"),
1233				Count: Int32(7),
1234			},
1235			// NOTE: pb.Message[1] is a nil
1236			nil,
1237		},
1238	}
1239
1240	o := old()
1241	err := o.Marshal(pb)
1242	if err == nil || !strings.Contains(err.Error(), "repeated field Message has nil") {
1243		t.Fatalf("unexpected or no error when marshaling: %v", err)
1244	}
1245}
1246
1247// Test that enums work.  Checks for a bug introduced by making enums
1248// named types instead of int32: newInt32FromUint64 would crash with
1249// a type mismatch in reflect.PointTo.
1250func TestEnum(t *testing.T) {
1251	pb := new(GoEnum)
1252	pb.Foo = FOO_FOO1.Enum()
1253	o := old()
1254	if err := o.Marshal(pb); err != nil {
1255		t.Fatal("error encoding enum:", err)
1256	}
1257	pb1 := new(GoEnum)
1258	if err := o.Unmarshal(pb1); err != nil {
1259		t.Fatal("error decoding enum:", err)
1260	}
1261	if *pb1.Foo != FOO_FOO1 {
1262		t.Error("expected 7 but got ", *pb1.Foo)
1263	}
1264}
1265
1266// Enum types have String methods. Check that enum fields can be printed.
1267// We don't care what the value actually is, just as long as it doesn't crash.
1268func TestPrintingNilEnumFields(t *testing.T) {
1269	pb := new(GoEnum)
1270	_ = fmt.Sprintf("%+v", pb)
1271}
1272
1273// Verify that absent required fields cause Marshal/Unmarshal to return errors.
1274func TestRequiredFieldEnforcement(t *testing.T) {
1275	pb := new(GoTestField)
1276	_, err := Marshal(pb)
1277	if err == nil {
1278		t.Error("marshal: expected error, got nil")
1279	} else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Label") {
1280		t.Errorf("marshal: bad error type: %v", err)
1281	}
1282
1283	// A slightly sneaky, yet valid, proto. It encodes the same required field twice,
1284	// so simply counting the required fields is insufficient.
1285	// field 1, encoding 2, value "hi"
1286	buf := []byte("\x0A\x02hi\x0A\x02hi")
1287	err = Unmarshal(buf, pb)
1288	if err == nil {
1289		t.Error("unmarshal: expected error, got nil")
1290	} else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Type") && !strings.Contains(err.Error(), "{Unknown}") {
1291		// TODO: remove unknown cases once we commit to the new unmarshaler.
1292		t.Errorf("unmarshal: bad error type: %v", err)
1293	}
1294}
1295
1296// Verify that absent required fields in groups cause Marshal/Unmarshal to return errors.
1297func TestRequiredFieldEnforcementGroups(t *testing.T) {
1298	pb := &GoTestRequiredGroupField{Group: &GoTestRequiredGroupField_Group{}}
1299	if _, err := Marshal(pb); err == nil {
1300		t.Error("marshal: expected error, got nil")
1301	} else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Group.Field") {
1302		t.Errorf("marshal: bad error type: %v", err)
1303	}
1304
1305	buf := []byte{11, 12}
1306	if err := Unmarshal(buf, pb); err == nil {
1307		t.Error("unmarshal: expected error, got nil")
1308	} else if _, ok := err.(*RequiredNotSetError); !ok || !strings.Contains(err.Error(), "Group.Field") && !strings.Contains(err.Error(), "Group.{Unknown}") {
1309		t.Errorf("unmarshal: bad error type: %v", err)
1310	}
1311}
1312
1313func TestTypedNilMarshal(t *testing.T) {
1314	// A typed nil should return ErrNil and not crash.
1315	{
1316		var m *GoEnum
1317		if _, err := Marshal(m); err != ErrNil {
1318			t.Errorf("Marshal(%#v): got %v, want ErrNil", m, err)
1319		}
1320	}
1321
1322	{
1323		m := &Communique{Union: &Communique_Msg{nil}}
1324		if _, err := Marshal(m); err == nil || err == ErrNil {
1325			t.Errorf("Marshal(%#v): got %v, want errOneofHasNil", m, err)
1326		}
1327	}
1328}
1329
1330// A type that implements the Marshaler interface, but is not nillable.
1331type nonNillableInt uint64
1332
1333func (nni nonNillableInt) Marshal() ([]byte, error) {
1334	return EncodeVarint(uint64(nni)), nil
1335}
1336
1337type NNIMessage struct {
1338	nni nonNillableInt
1339}
1340
1341func (*NNIMessage) Reset()         {}
1342func (*NNIMessage) String() string { return "" }
1343func (*NNIMessage) ProtoMessage()  {}
1344
1345type NMMessage struct{}
1346
1347func (*NMMessage) Reset()         {}
1348func (*NMMessage) String() string { return "" }
1349func (*NMMessage) ProtoMessage()  {}
1350
1351// Verify a type that uses the Marshaler interface, but has a nil pointer.
1352func TestNilMarshaler(t *testing.T) {
1353	// Try a struct with a Marshaler field that is nil.
1354	// It should be directly marshable.
1355	nmm := new(NMMessage)
1356	if _, err := Marshal(nmm); err != nil {
1357		t.Error("unexpected error marshaling nmm: ", err)
1358	}
1359
1360	// Try a struct with a Marshaler field that is not nillable.
1361	nnim := new(NNIMessage)
1362	nnim.nni = 7
1363	var _ Marshaler = nnim.nni // verify it is truly a Marshaler
1364	if _, err := Marshal(nnim); err != nil {
1365		t.Error("unexpected error marshaling nnim: ", err)
1366	}
1367}
1368
1369func TestAllSetDefaults(t *testing.T) {
1370	// Exercise SetDefaults with all scalar field types.
1371	m := &Defaults{
1372		// NaN != NaN, so override that here.
1373		F_Nan: Float32(1.7),
1374	}
1375	expected := &Defaults{
1376		F_Bool:    Bool(true),
1377		F_Int32:   Int32(32),
1378		F_Int64:   Int64(64),
1379		F_Fixed32: Uint32(320),
1380		F_Fixed64: Uint64(640),
1381		F_Uint32:  Uint32(3200),
1382		F_Uint64:  Uint64(6400),
1383		F_Float:   Float32(314159),
1384		F_Double:  Float64(271828),
1385		F_String:  String(`hello, "world!"` + "\n"),
1386		F_Bytes:   []byte("Bignose"),
1387		F_Sint32:  Int32(-32),
1388		F_Sint64:  Int64(-64),
1389		F_Enum:    Defaults_GREEN.Enum(),
1390		F_Pinf:    Float32(float32(math.Inf(1))),
1391		F_Ninf:    Float32(float32(math.Inf(-1))),
1392		F_Nan:     Float32(1.7),
1393		StrZero:   String(""),
1394	}
1395	SetDefaults(m)
1396	if !Equal(m, expected) {
1397		t.Errorf("SetDefaults failed\n got %v\nwant %v", m, expected)
1398	}
1399}
1400
1401func TestSetDefaultsWithSetField(t *testing.T) {
1402	// Check that a set value is not overridden.
1403	m := &Defaults{
1404		F_Int32: Int32(12),
1405	}
1406	SetDefaults(m)
1407	if v := m.GetF_Int32(); v != 12 {
1408		t.Errorf("m.FInt32 = %v, want 12", v)
1409	}
1410}
1411
1412func TestSetDefaultsWithSubMessage(t *testing.T) {
1413	m := &OtherMessage{
1414		Key: Int64(123),
1415		Inner: &InnerMessage{
1416			Host: String("gopher"),
1417		},
1418	}
1419	expected := &OtherMessage{
1420		Key: Int64(123),
1421		Inner: &InnerMessage{
1422			Host: String("gopher"),
1423			Port: Int32(4000),
1424		},
1425	}
1426	SetDefaults(m)
1427	if !Equal(m, expected) {
1428		t.Errorf("\n got %v\nwant %v", m, expected)
1429	}
1430}
1431
1432func TestSetDefaultsWithRepeatedSubMessage(t *testing.T) {
1433	m := &MyMessage{
1434		RepInner: []*InnerMessage{{}},
1435	}
1436	expected := &MyMessage{
1437		RepInner: []*InnerMessage{{
1438			Port: Int32(4000),
1439		}},
1440	}
1441	SetDefaults(m)
1442	if !Equal(m, expected) {
1443		t.Errorf("\n got %v\nwant %v", m, expected)
1444	}
1445}
1446
1447func TestSetDefaultWithRepeatedNonMessage(t *testing.T) {
1448	m := &MyMessage{
1449		Pet: []string{"turtle", "wombat"},
1450	}
1451	expected := Clone(m)
1452	SetDefaults(m)
1453	if !Equal(m, expected) {
1454		t.Errorf("\n got %v\nwant %v", m, expected)
1455	}
1456}
1457
1458func TestMaximumTagNumber(t *testing.T) {
1459	m := &MaxTag{
1460		LastField: String("natural goat essence"),
1461	}
1462	buf, err := Marshal(m)
1463	if err != nil {
1464		t.Fatalf("proto.Marshal failed: %v", err)
1465	}
1466	m2 := new(MaxTag)
1467	if err := Unmarshal(buf, m2); err != nil {
1468		t.Fatalf("proto.Unmarshal failed: %v", err)
1469	}
1470	if got, want := m2.GetLastField(), *m.LastField; got != want {
1471		t.Errorf("got %q, want %q", got, want)
1472	}
1473}
1474
1475func TestJSON(t *testing.T) {
1476	m := &MyMessage{
1477		Count: Int32(4),
1478		Pet:   []string{"bunny", "kitty"},
1479		Inner: &InnerMessage{
1480			Host: String("cauchy"),
1481		},
1482		Bikeshed: MyMessage_GREEN.Enum(),
1483	}
1484	const expected = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":1}`
1485
1486	b, err := json.Marshal(m)
1487	if err != nil {
1488		t.Fatalf("json.Marshal failed: %v", err)
1489	}
1490	s := string(b)
1491	if s != expected {
1492		t.Errorf("got  %s\nwant %s", s, expected)
1493	}
1494
1495	received := new(MyMessage)
1496	if err := json.Unmarshal(b, received); err != nil {
1497		t.Fatalf("json.Unmarshal failed: %v", err)
1498	}
1499	if !Equal(received, m) {
1500		t.Fatalf("got %s, want %s", received, m)
1501	}
1502
1503	// Test unmarshalling of JSON with symbolic enum name.
1504	const old = `{"count":4,"pet":["bunny","kitty"],"inner":{"host":"cauchy"},"bikeshed":"GREEN"}`
1505	received.Reset()
1506	if err := json.Unmarshal([]byte(old), received); err != nil {
1507		t.Fatalf("json.Unmarshal failed: %v", err)
1508	}
1509	if !Equal(received, m) {
1510		t.Fatalf("got %s, want %s", received, m)
1511	}
1512}
1513
1514func TestBadWireType(t *testing.T) {
1515	b := []byte{7<<3 | 6} // field 7, wire type 6
1516	pb := new(OtherMessage)
1517	if err := Unmarshal(b, pb); err == nil {
1518		t.Errorf("Unmarshal did not fail")
1519	} else if !strings.Contains(err.Error(), "unknown wire type") {
1520		t.Errorf("wrong error: %v", err)
1521	}
1522}
1523
1524func TestBytesWithInvalidLength(t *testing.T) {
1525	// If a byte sequence has an invalid (negative) length, Unmarshal should not panic.
1526	b := []byte{2<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0}
1527	Unmarshal(b, new(MyMessage))
1528}
1529
1530func TestLengthOverflow(t *testing.T) {
1531	// Overflowing a length should not panic.
1532	b := []byte{2<<3 | WireBytes, 1, 1, 3<<3 | WireBytes, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01}
1533	Unmarshal(b, new(MyMessage))
1534}
1535
1536func TestVarintOverflow(t *testing.T) {
1537	// Overflowing a 64-bit length should not be allowed.
1538	b := []byte{1<<3 | WireVarint, 0x01, 3<<3 | WireBytes, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01}
1539	if err := Unmarshal(b, new(MyMessage)); err == nil {
1540		t.Fatalf("Overflowed uint64 length without error")
1541	}
1542}
1543
1544func TestBytesWithInvalidLengthInGroup(t *testing.T) {
1545	// Overflowing a 64-bit length should not be allowed.
1546	b := []byte{0xbb, 0x30, 0xb2, 0x30, 0xb0, 0xb2, 0x83, 0xf1, 0xb0, 0xb2, 0xef, 0xbf, 0xbd, 0x01}
1547	if err := Unmarshal(b, new(MyMessage)); err == nil {
1548		t.Fatalf("Overflowed uint64 length without error")
1549	}
1550}
1551
1552func TestUnmarshalFuzz(t *testing.T) {
1553	const N = 1000
1554	seed := time.Now().UnixNano()
1555	t.Logf("RNG seed is %d", seed)
1556	rng := rand.New(rand.NewSource(seed))
1557	buf := make([]byte, 20)
1558	for i := 0; i < N; i++ {
1559		for j := range buf {
1560			buf[j] = byte(rng.Intn(256))
1561		}
1562		fuzzUnmarshal(t, buf)
1563	}
1564}
1565
1566func TestMergeMessages(t *testing.T) {
1567	pb := &MessageList{Message: []*MessageList_Message{{Name: String("x"), Count: Int32(1)}}}
1568	data, err := Marshal(pb)
1569	if err != nil {
1570		t.Fatalf("Marshal: %v", err)
1571	}
1572
1573	pb1 := new(MessageList)
1574	if err := Unmarshal(data, pb1); err != nil {
1575		t.Fatalf("first Unmarshal: %v", err)
1576	}
1577	if err := Unmarshal(data, pb1); err != nil {
1578		t.Fatalf("second Unmarshal: %v", err)
1579	}
1580	if len(pb1.Message) != 1 {
1581		t.Errorf("two Unmarshals produced %d Messages, want 1", len(pb1.Message))
1582	}
1583
1584	pb2 := new(MessageList)
1585	if err := UnmarshalMerge(data, pb2); err != nil {
1586		t.Fatalf("first UnmarshalMerge: %v", err)
1587	}
1588	if err := UnmarshalMerge(data, pb2); err != nil {
1589		t.Fatalf("second UnmarshalMerge: %v", err)
1590	}
1591	if len(pb2.Message) != 2 {
1592		t.Errorf("two UnmarshalMerges produced %d Messages, want 2", len(pb2.Message))
1593	}
1594}
1595
1596func TestExtensionMarshalOrder(t *testing.T) {
1597	m := &MyMessage{Count: Int(123)}
1598	if err := SetExtension(m, E_Ext_More, &Ext{Data: String("alpha")}); err != nil {
1599		t.Fatalf("SetExtension: %v", err)
1600	}
1601	if err := SetExtension(m, E_Ext_Text, String("aleph")); err != nil {
1602		t.Fatalf("SetExtension: %v", err)
1603	}
1604	if err := SetExtension(m, E_Ext_Number, Int32(1)); err != nil {
1605		t.Fatalf("SetExtension: %v", err)
1606	}
1607
1608	// Serialize m several times, and check we get the same bytes each time.
1609	var orig []byte
1610	for i := 0; i < 100; i++ {
1611		b, err := Marshal(m)
1612		if err != nil {
1613			t.Fatalf("Marshal: %v", err)
1614		}
1615		if i == 0 {
1616			orig = b
1617			continue
1618		}
1619		if !bytes.Equal(b, orig) {
1620			t.Errorf("Bytes differ on attempt #%d", i)
1621		}
1622	}
1623}
1624
1625func TestExtensionMapFieldMarshalDeterministic(t *testing.T) {
1626	m := &MyMessage{Count: Int(123)}
1627	if err := SetExtension(m, E_Ext_More, &Ext{MapField: map[int32]int32{1: 1, 2: 2, 3: 3, 4: 4}}); err != nil {
1628		t.Fatalf("SetExtension: %v", err)
1629	}
1630	marshal := func(m Message) []byte {
1631		var b Buffer
1632		b.SetDeterministic(true)
1633		if err := b.Marshal(m); err != nil {
1634			t.Fatalf("Marshal failed: %v", err)
1635		}
1636		return b.Bytes()
1637	}
1638
1639	want := marshal(m)
1640	for i := 0; i < 100; i++ {
1641		if got := marshal(m); !bytes.Equal(got, want) {
1642			t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
1643		}
1644	}
1645}
1646
1647// Many extensions, because small maps might not iterate differently on each iteration.
1648var exts = []*ExtensionDesc{
1649	E_X201,
1650	E_X202,
1651	E_X203,
1652	E_X204,
1653	E_X205,
1654	E_X206,
1655	E_X207,
1656	E_X208,
1657	E_X209,
1658	E_X210,
1659	E_X211,
1660	E_X212,
1661	E_X213,
1662	E_X214,
1663	E_X215,
1664	E_X216,
1665	E_X217,
1666	E_X218,
1667	E_X219,
1668	E_X220,
1669	E_X221,
1670	E_X222,
1671	E_X223,
1672	E_X224,
1673	E_X225,
1674	E_X226,
1675	E_X227,
1676	E_X228,
1677	E_X229,
1678	E_X230,
1679	E_X231,
1680	E_X232,
1681	E_X233,
1682	E_X234,
1683	E_X235,
1684	E_X236,
1685	E_X237,
1686	E_X238,
1687	E_X239,
1688	E_X240,
1689	E_X241,
1690	E_X242,
1691	E_X243,
1692	E_X244,
1693	E_X245,
1694	E_X246,
1695	E_X247,
1696	E_X248,
1697	E_X249,
1698	E_X250,
1699}
1700
1701func TestMessageSetMarshalOrder(t *testing.T) {
1702	m := &MyMessageSet{}
1703	for _, x := range exts {
1704		if err := SetExtension(m, x, &Empty{}); err != nil {
1705			t.Fatalf("SetExtension: %v", err)
1706		}
1707	}
1708
1709	buf, err := Marshal(m)
1710	if err != nil {
1711		t.Fatalf("Marshal: %v", err)
1712	}
1713
1714	// Serialize m several times, and check we get the same bytes each time.
1715	for i := 0; i < 10; i++ {
1716		b1, err := Marshal(m)
1717		if err != nil {
1718			t.Fatalf("Marshal: %v", err)
1719		}
1720		if !bytes.Equal(b1, buf) {
1721			t.Errorf("Bytes differ on re-Marshal #%d", i)
1722		}
1723
1724		m2 := &MyMessageSet{}
1725		if err := Unmarshal(buf, m2); err != nil {
1726			t.Errorf("Unmarshal: %v", err)
1727		}
1728		b2, err := Marshal(m2)
1729		if err != nil {
1730			t.Errorf("re-Marshal: %v", err)
1731		}
1732		if !bytes.Equal(b2, buf) {
1733			t.Errorf("Bytes differ on round-trip #%d", i)
1734		}
1735	}
1736}
1737
1738func TestUnmarshalMergesMessages(t *testing.T) {
1739	// If a nested message occurs twice in the input,
1740	// the fields should be merged when decoding.
1741	a := &OtherMessage{
1742		Key: Int64(123),
1743		Inner: &InnerMessage{
1744			Host: String("polhode"),
1745			Port: Int32(1234),
1746		},
1747	}
1748	aData, err := Marshal(a)
1749	if err != nil {
1750		t.Fatalf("Marshal(a): %v", err)
1751	}
1752	b := &OtherMessage{
1753		Weight: Float32(1.2),
1754		Inner: &InnerMessage{
1755			Host:      String("herpolhode"),
1756			Connected: Bool(true),
1757		},
1758	}
1759	bData, err := Marshal(b)
1760	if err != nil {
1761		t.Fatalf("Marshal(b): %v", err)
1762	}
1763	want := &OtherMessage{
1764		Key:    Int64(123),
1765		Weight: Float32(1.2),
1766		Inner: &InnerMessage{
1767			Host:      String("herpolhode"),
1768			Port:      Int32(1234),
1769			Connected: Bool(true),
1770		},
1771	}
1772	got := new(OtherMessage)
1773	if err := Unmarshal(append(aData, bData...), got); err != nil {
1774		t.Fatalf("Unmarshal: %v", err)
1775	}
1776	if !Equal(got, want) {
1777		t.Errorf("\n got %v\nwant %v", got, want)
1778	}
1779}
1780
1781func TestUnmarshalMergesGroups(t *testing.T) {
1782	// If a nested group occurs twice in the input,
1783	// the fields should be merged when decoding.
1784	a := &GroupNew{
1785		G: &GroupNew_G{
1786			X: Int32(7),
1787			Y: Int32(8),
1788		},
1789	}
1790	aData, err := Marshal(a)
1791	if err != nil {
1792		t.Fatalf("Marshal(a): %v", err)
1793	}
1794	b := &GroupNew{
1795		G: &GroupNew_G{
1796			X: Int32(9),
1797		},
1798	}
1799	bData, err := Marshal(b)
1800	if err != nil {
1801		t.Fatalf("Marshal(b): %v", err)
1802	}
1803	want := &GroupNew{
1804		G: &GroupNew_G{
1805			X: Int32(9),
1806			Y: Int32(8),
1807		},
1808	}
1809	got := new(GroupNew)
1810	if err := Unmarshal(append(aData, bData...), got); err != nil {
1811		t.Fatalf("Unmarshal: %v", err)
1812	}
1813	if !Equal(got, want) {
1814		t.Errorf("\n got %v\nwant %v", got, want)
1815	}
1816}
1817
1818func TestEncodingSizes(t *testing.T) {
1819	tests := []struct {
1820		m Message
1821		n int
1822	}{
1823		{&Defaults{F_Int32: Int32(math.MaxInt32)}, 6},
1824		{&Defaults{F_Int32: Int32(math.MinInt32)}, 11},
1825		{&Defaults{F_Uint32: Uint32(uint32(math.MaxInt32) + 1)}, 6},
1826		{&Defaults{F_Uint32: Uint32(math.MaxUint32)}, 6},
1827	}
1828	for _, test := range tests {
1829		b, err := Marshal(test.m)
1830		if err != nil {
1831			t.Errorf("Marshal(%v): %v", test.m, err)
1832			continue
1833		}
1834		if len(b) != test.n {
1835			t.Errorf("Marshal(%v) yielded %d bytes, want %d bytes", test.m, len(b), test.n)
1836		}
1837	}
1838}
1839
1840func TestRequiredNotSetError(t *testing.T) {
1841	pb := initGoTest(false)
1842	pb.RequiredField.Label = nil
1843	pb.F_Int32Required = nil
1844	pb.F_Int64Required = nil
1845
1846	expected := "0807" + // field 1, encoding 0, value 7
1847		"2206" + "120474797065" + // field 4, encoding 2 (GoTestField)
1848		"5001" + // field 10, encoding 0, value 1
1849		"6d20000000" + // field 13, encoding 5, value 0x20
1850		"714000000000000000" + // field 14, encoding 1, value 0x40
1851		"78a019" + // field 15, encoding 0, value 0xca0 = 3232
1852		"8001c032" + // field 16, encoding 0, value 0x1940 = 6464
1853		"8d0100004a45" + // field 17, encoding 5, value 3232.0
1854		"9101000000000040b940" + // field 18, encoding 1, value 6464.0
1855		"9a0106" + "737472696e67" + // field 19, encoding 2, string "string"
1856		"b304" + // field 70, encoding 3, start group
1857		"ba0408" + "7265717569726564" + // field 71, encoding 2, string "required"
1858		"b404" + // field 70, encoding 4, end group
1859		"aa0605" + "6279746573" + // field 101, encoding 2, string "bytes"
1860		"b0063f" + // field 102, encoding 0, 0x3f zigzag32
1861		"b8067f" + // field 103, encoding 0, 0x7f zigzag64
1862		"c506e0ffffff" + // field 104, encoding 5, -32 fixed32
1863		"c906c0ffffffffffffff" // field 105, encoding 1, -64 fixed64
1864
1865	o := old()
1866	bytes, err := Marshal(pb)
1867	if _, ok := err.(*RequiredNotSetError); !ok {
1868		fmt.Printf("marshal-1 err = %v, want *RequiredNotSetError", err)
1869		o.DebugPrint("", bytes)
1870		t.Fatalf("expected = %s", expected)
1871	}
1872	if !strings.Contains(err.Error(), "RequiredField.Label") {
1873		t.Errorf("marshal-1 wrong err msg: %v", err)
1874	}
1875	if !equal(bytes, expected, t) {
1876		o.DebugPrint("neq 1", bytes)
1877		t.Fatalf("expected = %s", expected)
1878	}
1879
1880	// Now test Unmarshal by recreating the original buffer.
1881	pbd := new(GoTest)
1882	err = Unmarshal(bytes, pbd)
1883	if _, ok := err.(*RequiredNotSetError); !ok {
1884		t.Fatalf("unmarshal err = %v, want *RequiredNotSetError", err)
1885		o.DebugPrint("", bytes)
1886		t.Fatalf("string = %s", expected)
1887	}
1888	if !strings.Contains(err.Error(), "RequiredField.Label") && !strings.Contains(err.Error(), "RequiredField.{Unknown}") {
1889		t.Errorf("unmarshal wrong err msg: %v", err)
1890	}
1891	bytes, err = Marshal(pbd)
1892	if _, ok := err.(*RequiredNotSetError); !ok {
1893		t.Errorf("marshal-2 err = %v, want *RequiredNotSetError", err)
1894		o.DebugPrint("", bytes)
1895		t.Fatalf("string = %s", expected)
1896	}
1897	if !strings.Contains(err.Error(), "RequiredField.Label") {
1898		t.Errorf("marshal-2 wrong err msg: %v", err)
1899	}
1900	if !equal(bytes, expected, t) {
1901		o.DebugPrint("neq 2", bytes)
1902		t.Fatalf("string = %s", expected)
1903	}
1904}
1905
1906func TestRequiredNotSetErrorWithBadWireTypes(t *testing.T) {
1907	// Required field expects a varint, and properly found a varint.
1908	if err := Unmarshal([]byte{0x08, 0x00}, new(GoEnum)); err != nil {
1909		t.Errorf("Unmarshal = %v, want nil", err)
1910	}
1911	// Required field expects a varint, but found a fixed32 instead.
1912	if err := Unmarshal([]byte{0x0d, 0x00, 0x00, 0x00, 0x00}, new(GoEnum)); err == nil {
1913		t.Errorf("Unmarshal = nil, want RequiredNotSetError")
1914	}
1915	// Required field expects a varint, and found both a varint and fixed32 (ignored).
1916	m := new(GoEnum)
1917	if err := Unmarshal([]byte{0x08, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00}, m); err != nil {
1918		t.Errorf("Unmarshal = %v, want nil", err)
1919	}
1920	if !bytes.Equal(m.XXX_unrecognized, []byte{0x0d, 0x00, 0x00, 0x00, 0x00}) {
1921		t.Errorf("expected fixed32 to appear as unknown bytes: %x", m.XXX_unrecognized)
1922	}
1923}
1924
1925func fuzzUnmarshal(t *testing.T, data []byte) {
1926	defer func() {
1927		if e := recover(); e != nil {
1928			t.Errorf("These bytes caused a panic: %+v", data)
1929			t.Logf("Stack:\n%s", debug.Stack())
1930			t.FailNow()
1931		}
1932	}()
1933
1934	pb := new(MyMessage)
1935	Unmarshal(data, pb)
1936}
1937
1938func TestMapFieldMarshal(t *testing.T) {
1939	m := &MessageWithMap{
1940		NameMapping: map[int32]string{
1941			1: "Rob",
1942			4: "Ian",
1943			8: "Dave",
1944		},
1945	}
1946	b, err := Marshal(m)
1947	if err != nil {
1948		t.Fatalf("Marshal: %v", err)
1949	}
1950
1951	// b should be the concatenation of these three byte sequences in some order.
1952	parts := []string{
1953		"\n\a\b\x01\x12\x03Rob",
1954		"\n\a\b\x04\x12\x03Ian",
1955		"\n\b\b\x08\x12\x04Dave",
1956	}
1957	ok := false
1958	for i := range parts {
1959		for j := range parts {
1960			if j == i {
1961				continue
1962			}
1963			for k := range parts {
1964				if k == i || k == j {
1965					continue
1966				}
1967				try := parts[i] + parts[j] + parts[k]
1968				if bytes.Equal(b, []byte(try)) {
1969					ok = true
1970					break
1971				}
1972			}
1973		}
1974	}
1975	if !ok {
1976		t.Fatalf("Incorrect Marshal output.\n got %q\nwant %q (or a permutation of that)", b, parts[0]+parts[1]+parts[2])
1977	}
1978	t.Logf("FYI b: %q", b)
1979
1980	(new(Buffer)).DebugPrint("Dump of b", b)
1981}
1982
1983func TestMapFieldDeterministicMarshal(t *testing.T) {
1984	m := &MessageWithMap{
1985		NameMapping: map[int32]string{
1986			1: "Rob",
1987			4: "Ian",
1988			8: "Dave",
1989		},
1990	}
1991
1992	marshal := func(m Message) []byte {
1993		var b Buffer
1994		b.SetDeterministic(true)
1995		if err := b.Marshal(m); err != nil {
1996			t.Fatalf("Marshal failed: %v", err)
1997		}
1998		return b.Bytes()
1999	}
2000
2001	want := marshal(m)
2002	for i := 0; i < 10; i++ {
2003		if got := marshal(m); !bytes.Equal(got, want) {
2004			t.Errorf("Marshal produced inconsistent output with determinism enabled (pass %d).\n got %v\nwant %v", i, got, want)
2005		}
2006	}
2007}
2008
2009func TestMapFieldRoundTrips(t *testing.T) {
2010	m := &MessageWithMap{
2011		NameMapping: map[int32]string{
2012			1: "Rob",
2013			4: "Ian",
2014			8: "Dave",
2015		},
2016		MsgMapping: map[int64]*FloatingPoint{
2017			0x7001: {F: Float64(2.0)},
2018		},
2019		ByteMapping: map[bool][]byte{
2020			false: []byte("that's not right!"),
2021			true:  []byte("aye, 'tis true!"),
2022		},
2023	}
2024	b, err := Marshal(m)
2025	if err != nil {
2026		t.Fatalf("Marshal: %v", err)
2027	}
2028	t.Logf("FYI b: %q", b)
2029	m2 := new(MessageWithMap)
2030	if err := Unmarshal(b, m2); err != nil {
2031		t.Fatalf("Unmarshal: %v", err)
2032	}
2033	if !Equal(m, m2) {
2034		t.Errorf("Map did not survive a round trip.\ninitial: %v\n  final: %v", m, m2)
2035	}
2036}
2037
2038func TestMapFieldWithNil(t *testing.T) {
2039	m1 := &MessageWithMap{
2040		MsgMapping: map[int64]*FloatingPoint{
2041			1: nil,
2042		},
2043	}
2044	b, err := Marshal(m1)
2045	if err != nil {
2046		t.Fatalf("Marshal: %v", err)
2047	}
2048	m2 := new(MessageWithMap)
2049	if err := Unmarshal(b, m2); err != nil {
2050		t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
2051	}
2052	if v, ok := m2.MsgMapping[1]; !ok {
2053		t.Error("msg_mapping[1] not present")
2054	} else if v != nil {
2055		t.Errorf("msg_mapping[1] not nil: %v", v)
2056	}
2057}
2058
2059func TestMapFieldWithNilBytes(t *testing.T) {
2060	m1 := &MessageWithMap{
2061		ByteMapping: map[bool][]byte{
2062			false: {},
2063			true:  nil,
2064		},
2065	}
2066	n := Size(m1)
2067	b, err := Marshal(m1)
2068	if err != nil {
2069		t.Fatalf("Marshal: %v", err)
2070	}
2071	if n != len(b) {
2072		t.Errorf("Size(m1) = %d; want len(Marshal(m1)) = %d", n, len(b))
2073	}
2074	m2 := new(MessageWithMap)
2075	if err := Unmarshal(b, m2); err != nil {
2076		t.Fatalf("Unmarshal: %v, got these bytes: %v", err, b)
2077	}
2078	if v, ok := m2.ByteMapping[false]; !ok {
2079		t.Error("byte_mapping[false] not present")
2080	} else if len(v) != 0 {
2081		t.Errorf("byte_mapping[false] not empty: %#v", v)
2082	}
2083	if v, ok := m2.ByteMapping[true]; !ok {
2084		t.Error("byte_mapping[true] not present")
2085	} else if len(v) != 0 {
2086		t.Errorf("byte_mapping[true] not empty: %#v", v)
2087	}
2088}
2089
2090func TestDecodeMapFieldMissingKey(t *testing.T) {
2091	b := []byte{
2092		0x0A, 0x03, // message, tag 1 (name_mapping), of length 3 bytes
2093		// no key
2094		0x12, 0x01, 0x6D, // string value of length 1 byte, value "m"
2095	}
2096	got := &MessageWithMap{}
2097	err := Unmarshal(b, got)
2098	if err != nil {
2099		t.Fatalf("failed to marshal map with missing key: %v", err)
2100	}
2101	want := &MessageWithMap{NameMapping: map[int32]string{0: "m"}}
2102	if !Equal(got, want) {
2103		t.Errorf("Unmarshaled map with no key was not as expected. got: %v, want %v", got, want)
2104	}
2105}
2106
2107func TestDecodeMapFieldMissingValue(t *testing.T) {
2108	b := []byte{
2109		0x0A, 0x02, // message, tag 1 (name_mapping), of length 2 bytes
2110		0x08, 0x01, // varint key, value 1
2111		// no value
2112	}
2113	got := &MessageWithMap{}
2114	err := Unmarshal(b, got)
2115	if err != nil {
2116		t.Fatalf("failed to marshal map with missing value: %v", err)
2117	}
2118	want := &MessageWithMap{NameMapping: map[int32]string{1: ""}}
2119	if !Equal(got, want) {
2120		t.Errorf("Unmarshaled map with no value was not as expected. got: %v, want %v", got, want)
2121	}
2122}
2123
2124func TestOneof(t *testing.T) {
2125	m := &Communique{}
2126	b, err := Marshal(m)
2127	if err != nil {
2128		t.Fatalf("Marshal of empty message with oneof: %v", err)
2129	}
2130	if len(b) != 0 {
2131		t.Errorf("Marshal of empty message yielded too many bytes: %v", b)
2132	}
2133
2134	m = &Communique{
2135		Union: &Communique_Name{"Barry"},
2136	}
2137
2138	// Round-trip.
2139	b, err = Marshal(m)
2140	if err != nil {
2141		t.Fatalf("Marshal of message with oneof: %v", err)
2142	}
2143	if len(b) != 7 { // name tag/wire (1) + name len (1) + name (5)
2144		t.Errorf("Incorrect marshal of message with oneof: %v", b)
2145	}
2146	m.Reset()
2147	if err := Unmarshal(b, m); err != nil {
2148		t.Fatalf("Unmarshal of message with oneof: %v", err)
2149	}
2150	if x, ok := m.Union.(*Communique_Name); !ok || x.Name != "Barry" {
2151		t.Errorf("After round trip, Union = %+v", m.Union)
2152	}
2153	if name := m.GetName(); name != "Barry" {
2154		t.Errorf("After round trip, GetName = %q, want %q", name, "Barry")
2155	}
2156
2157	// Let's try with a message in the oneof.
2158	m.Union = &Communique_Msg{&Strings{StringField: String("deep deep string")}}
2159	b, err = Marshal(m)
2160	if err != nil {
2161		t.Fatalf("Marshal of message with oneof set to message: %v", err)
2162	}
2163	if len(b) != 20 { // msg tag/wire (1) + msg len (1) + msg (1 + 1 + 16)
2164		t.Errorf("Incorrect marshal of message with oneof set to message: %v", b)
2165	}
2166	m.Reset()
2167	if err := Unmarshal(b, m); err != nil {
2168		t.Fatalf("Unmarshal of message with oneof set to message: %v", err)
2169	}
2170	ss, ok := m.Union.(*Communique_Msg)
2171	if !ok || ss.Msg.GetStringField() != "deep deep string" {
2172		t.Errorf("After round trip with oneof set to message, Union = %+v", m.Union)
2173	}
2174}
2175
2176func TestOneofNilBytes(t *testing.T) {
2177	// A oneof with nil byte slice should marshal to tag + 0 (size), with no error.
2178	m := &Communique{Union: &Communique_Data{Data: nil}}
2179	b, err := Marshal(m)
2180	if err != nil {
2181		t.Fatalf("Marshal failed: %v", err)
2182	}
2183	want := []byte{
2184		7<<3 | 2, // tag 7, wire type 2
2185		0,        // size
2186	}
2187	if !bytes.Equal(b, want) {
2188		t.Errorf("Wrong result of Marshal: got %x, want %x", b, want)
2189	}
2190}
2191
2192func TestInefficientPackedBool(t *testing.T) {
2193	// https://github.com/golang/protobuf/issues/76
2194	inp := []byte{
2195		0x12, 0x02, // 0x12 = 2<<3|2; 2 bytes
2196		// Usually a bool should take a single byte,
2197		// but it is permitted to be any varint.
2198		0xb9, 0x30,
2199	}
2200	if err := Unmarshal(inp, new(MoreRepeated)); err != nil {
2201		t.Error(err)
2202	}
2203}
2204
2205// Make sure pure-reflect-based implementation handles
2206// []int32-[]enum conversion correctly.
2207func TestRepeatedEnum2(t *testing.T) {
2208	pb := &RepeatedEnum{
2209		Color: []RepeatedEnum_Color{RepeatedEnum_RED},
2210	}
2211	b, err := Marshal(pb)
2212	if err != nil {
2213		t.Fatalf("Marshal failed: %v", err)
2214	}
2215	x := new(RepeatedEnum)
2216	err = Unmarshal(b, x)
2217	if err != nil {
2218		t.Fatalf("Unmarshal failed: %v", err)
2219	}
2220	if !Equal(pb, x) {
2221		t.Errorf("Incorrect result: want: %v got: %v", pb, x)
2222	}
2223}
2224
2225// TestConcurrentMarshal makes sure that it is safe to marshal
2226// same message in multiple goroutines concurrently.
2227func TestConcurrentMarshal(t *testing.T) {
2228	pb := initGoTest(true)
2229	const N = 100
2230	b := make([][]byte, N)
2231
2232	var wg sync.WaitGroup
2233	for i := 0; i < N; i++ {
2234		wg.Add(1)
2235		go func(i int) {
2236			defer wg.Done()
2237			var err error
2238			b[i], err = Marshal(pb)
2239			if err != nil {
2240				t.Errorf("marshal error: %v", err)
2241			}
2242		}(i)
2243	}
2244
2245	wg.Wait()
2246	for i := 1; i < N; i++ {
2247		if !bytes.Equal(b[0], b[i]) {
2248			t.Errorf("concurrent marshal result not same: b[0] = %v, b[%d] = %v", b[0], i, b[i])
2249		}
2250	}
2251}
2252
2253func TestInvalidUTF8(t *testing.T) {
2254	const invalidUTF8 = "\xde\xad\xbe\xef\x80\x00\xff"
2255	tests := []struct {
2256		label  string
2257		proto2 Message
2258		proto3 Message
2259		want   []byte
2260	}{{
2261		label:  "Scalar",
2262		proto2: &TestUTF8{Scalar: String(invalidUTF8)},
2263		proto3: &pb3.TestUTF8{Scalar: invalidUTF8},
2264		want:   []byte{0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2265	}, {
2266		label:  "Vector",
2267		proto2: &TestUTF8{Vector: []string{invalidUTF8}},
2268		proto3: &pb3.TestUTF8{Vector: []string{invalidUTF8}},
2269		want:   []byte{0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2270	}, {
2271		label:  "Oneof",
2272		proto2: &TestUTF8{Oneof: &TestUTF8_Field{invalidUTF8}},
2273		proto3: &pb3.TestUTF8{Oneof: &pb3.TestUTF8_Field{invalidUTF8}},
2274		want:   []byte{0x1a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2275	}, {
2276		label:  "MapKey",
2277		proto2: &TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2278		proto3: &pb3.TestUTF8{MapKey: map[string]int64{invalidUTF8: 0}},
2279		want:   []byte{0x22, 0x0b, 0x0a, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff, 0x10, 0x00},
2280	}, {
2281		label:  "MapValue",
2282		proto2: &TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2283		proto3: &pb3.TestUTF8{MapValue: map[int64]string{0: invalidUTF8}},
2284		want:   []byte{0x2a, 0x0b, 0x08, 0x00, 0x12, 0x07, 0xde, 0xad, 0xbe, 0xef, 0x80, 0x00, 0xff},
2285	}}
2286
2287	for _, tt := range tests {
2288		// Proto2 should not validate UTF-8.
2289		b, err := Marshal(tt.proto2)
2290		if err != nil {
2291			t.Errorf("Marshal(proto2.%s) = %v, want nil", tt.label, err)
2292		}
2293		if !bytes.Equal(b, tt.want) {
2294			t.Errorf("Marshal(proto2.%s) = %x, want %x", tt.label, b, tt.want)
2295		}
2296
2297		m := Clone(tt.proto2)
2298		m.Reset()
2299		if err = Unmarshal(tt.want, m); err != nil {
2300			t.Errorf("Unmarshal(proto2.%s) = %v, want nil", tt.label, err)
2301		}
2302		if !Equal(m, tt.proto2) {
2303			t.Errorf("proto2.%s: output mismatch:\ngot  %v\nwant %v", tt.label, m, tt.proto2)
2304		}
2305
2306		// Proto3 should validate UTF-8.
2307		b, err = Marshal(tt.proto3)
2308		if err == nil {
2309			t.Errorf("Marshal(proto3.%s) = %v, want non-nil", tt.label, err)
2310		}
2311		if !bytes.Equal(b, tt.want) {
2312			t.Errorf("Marshal(proto3.%s) = %x, want %x", tt.label, b, tt.want)
2313		}
2314
2315		m = Clone(tt.proto3)
2316		m.Reset()
2317		err = Unmarshal(tt.want, m)
2318		if err == nil {
2319			t.Errorf("Unmarshal(proto3.%s) = %v, want non-nil", tt.label, err)
2320		}
2321		if !Equal(m, tt.proto3) {
2322			t.Errorf("proto3.%s: output mismatch:\ngot  %v\nwant %v", tt.label, m, tt.proto2)
2323		}
2324	}
2325}
2326
2327func TestRequired(t *testing.T) {
2328	// The F_BoolRequired field appears after all of the required fields.
2329	// It should still be handled even after multiple required field violations.
2330	m := &GoTest{F_BoolRequired: Bool(true)}
2331	got, err := Marshal(m)
2332	if _, ok := err.(*RequiredNotSetError); !ok {
2333		t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
2334	}
2335	if want := []byte{0x50, 0x01}; !bytes.Equal(got, want) {
2336		t.Errorf("Marshal() = %x, want %x", got, want)
2337	}
2338
2339	m = new(GoTest)
2340	err = Unmarshal(got, m)
2341	if _, ok := err.(*RequiredNotSetError); !ok {
2342		t.Errorf("Marshal() = %v, want RequiredNotSetError error", err)
2343	}
2344	if !m.GetF_BoolRequired() {
2345		t.Error("m.F_BoolRequired = false, want true")
2346	}
2347}
2348
2349// Benchmarks
2350
2351func testMsg() *GoTest {
2352	pb := initGoTest(true)
2353	const N = 1000 // Internally the library starts much smaller.
2354	pb.F_Int32Repeated = make([]int32, N)
2355	pb.F_DoubleRepeated = make([]float64, N)
2356	for i := 0; i < N; i++ {
2357		pb.F_Int32Repeated[i] = int32(i)
2358		pb.F_DoubleRepeated[i] = float64(i)
2359	}
2360	return pb
2361}
2362
2363func bytesMsg() *GoTest {
2364	pb := initGoTest(true)
2365	buf := make([]byte, 4000)
2366	for i := range buf {
2367		buf[i] = byte(i)
2368	}
2369	pb.F_BytesDefaulted = buf
2370	return pb
2371}
2372
2373func benchmarkMarshal(b *testing.B, pb Message, marshal func(Message) ([]byte, error)) {
2374	d, _ := marshal(pb)
2375	b.SetBytes(int64(len(d)))
2376	b.ResetTimer()
2377	for i := 0; i < b.N; i++ {
2378		marshal(pb)
2379	}
2380}
2381
2382func benchmarkBufferMarshal(b *testing.B, pb Message) {
2383	p := NewBuffer(nil)
2384	benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
2385		p.Reset()
2386		err := p.Marshal(pb0)
2387		return p.Bytes(), err
2388	})
2389}
2390
2391func benchmarkSize(b *testing.B, pb Message) {
2392	benchmarkMarshal(b, pb, func(pb0 Message) ([]byte, error) {
2393		Size(pb)
2394		return nil, nil
2395	})
2396}
2397
2398func newOf(pb Message) Message {
2399	in := reflect.ValueOf(pb)
2400	if in.IsNil() {
2401		return pb
2402	}
2403	return reflect.New(in.Type().Elem()).Interface().(Message)
2404}
2405
2406func benchmarkUnmarshal(b *testing.B, pb Message, unmarshal func([]byte, Message) error) {
2407	d, _ := Marshal(pb)
2408	b.SetBytes(int64(len(d)))
2409	pbd := newOf(pb)
2410
2411	b.ResetTimer()
2412	for i := 0; i < b.N; i++ {
2413		unmarshal(d, pbd)
2414	}
2415}
2416
2417func benchmarkBufferUnmarshal(b *testing.B, pb Message) {
2418	p := NewBuffer(nil)
2419	benchmarkUnmarshal(b, pb, func(d []byte, pb0 Message) error {
2420		p.SetBuf(d)
2421		return p.Unmarshal(pb0)
2422	})
2423}
2424
2425// Benchmark{Marshal,BufferMarshal,Size,Unmarshal,BufferUnmarshal}{,Bytes}
2426
2427func BenchmarkMarshal(b *testing.B) {
2428	benchmarkMarshal(b, testMsg(), Marshal)
2429}
2430
2431func BenchmarkBufferMarshal(b *testing.B) {
2432	benchmarkBufferMarshal(b, testMsg())
2433}
2434
2435func BenchmarkSize(b *testing.B) {
2436	benchmarkSize(b, testMsg())
2437}
2438
2439func BenchmarkUnmarshal(b *testing.B) {
2440	benchmarkUnmarshal(b, testMsg(), Unmarshal)
2441}
2442
2443func BenchmarkBufferUnmarshal(b *testing.B) {
2444	benchmarkBufferUnmarshal(b, testMsg())
2445}
2446
2447func BenchmarkMarshalBytes(b *testing.B) {
2448	benchmarkMarshal(b, bytesMsg(), Marshal)
2449}
2450
2451func BenchmarkBufferMarshalBytes(b *testing.B) {
2452	benchmarkBufferMarshal(b, bytesMsg())
2453}
2454
2455func BenchmarkSizeBytes(b *testing.B) {
2456	benchmarkSize(b, bytesMsg())
2457}
2458
2459func BenchmarkUnmarshalBytes(b *testing.B) {
2460	benchmarkUnmarshal(b, bytesMsg(), Unmarshal)
2461}
2462
2463func BenchmarkBufferUnmarshalBytes(b *testing.B) {
2464	benchmarkBufferUnmarshal(b, bytesMsg())
2465}
2466
2467func BenchmarkUnmarshalUnrecognizedFields(b *testing.B) {
2468	b.StopTimer()
2469	pb := initGoTestField()
2470	skip := &GoSkipTest{
2471		SkipInt32:   Int32(32),
2472		SkipFixed32: Uint32(3232),
2473		SkipFixed64: Uint64(6464),
2474		SkipString:  String("skipper"),
2475		Skipgroup: &GoSkipTest_SkipGroup{
2476			GroupInt32:  Int32(75),
2477			GroupString: String("wxyz"),
2478		},
2479	}
2480
2481	pbd := new(GoTestField)
2482	p := NewBuffer(nil)
2483	p.Marshal(pb)
2484	p.Marshal(skip)
2485	p2 := NewBuffer(nil)
2486
2487	b.StartTimer()
2488	for i := 0; i < b.N; i++ {
2489		p2.SetBuf(p.Bytes())
2490		p2.Unmarshal(pbd)
2491	}
2492}
2493