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