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