github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/golang/protobuf/proto/all_test.go (about)

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