proto3_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2014 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "bytes"
  7. "testing"
  8. "github.com/golang/protobuf/proto"
  9. pb "github.com/golang/protobuf/proto/proto3_proto"
  10. tpb "github.com/golang/protobuf/proto/test_proto"
  11. )
  12. func TestProto3ZeroValues(t *testing.T) {
  13. tests := []struct {
  14. desc string
  15. m proto.Message
  16. }{
  17. {"zero message", &pb.Message{}},
  18. {"empty bytes field", &pb.Message{Data: []byte{}}},
  19. }
  20. for _, test := range tests {
  21. b, err := proto.Marshal(test.m)
  22. if err != nil {
  23. t.Errorf("%s: proto.Marshal: %v", test.desc, err)
  24. continue
  25. }
  26. if len(b) > 0 {
  27. t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
  28. }
  29. }
  30. }
  31. func TestRoundTripProto3(t *testing.T) {
  32. m := &pb.Message{
  33. Name: "David", // (2 | 1<<3): 0x0a 0x05 "David"
  34. Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01
  35. HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01
  36. Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
  37. ResultCount: 47, // (0 | 7<<3): 0x38 0x2f
  38. TrueScotsman: true, // (0 | 8<<3): 0x40 0x01
  39. Score: 8.1, // (5 | 9<<3): 0x4d <8.1>
  40. Key: []uint64{1, 0xdeadbeef},
  41. Nested: &pb.Nested{
  42. Bunny: "Monty",
  43. },
  44. }
  45. t.Logf(" m: %v", m)
  46. b, err := proto.Marshal(m)
  47. if err != nil {
  48. t.Fatalf("proto.Marshal: %v", err)
  49. }
  50. t.Logf(" b: %q", b)
  51. m2 := new(pb.Message)
  52. if err := proto.Unmarshal(b, m2); err != nil {
  53. t.Fatalf("proto.Unmarshal: %v", err)
  54. }
  55. t.Logf("m2: %v", m2)
  56. if !proto.Equal(m, m2) {
  57. t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
  58. }
  59. }
  60. func TestGettersForBasicTypesExist(t *testing.T) {
  61. var m pb.Message
  62. if got := m.GetNested().GetBunny(); got != "" {
  63. t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
  64. }
  65. if got := m.GetNested().GetCute(); got {
  66. t.Errorf("m.GetNested().GetCute() = %t, want false", got)
  67. }
  68. }
  69. func TestProto3SetDefaults(t *testing.T) {
  70. in := &pb.Message{
  71. Terrain: map[string]*pb.Nested{
  72. "meadow": new(pb.Nested),
  73. },
  74. Proto2Field: new(tpb.SubDefaults),
  75. Proto2Value: map[string]*tpb.SubDefaults{
  76. "badlands": new(tpb.SubDefaults),
  77. },
  78. }
  79. got := proto.Clone(in).(*pb.Message)
  80. proto.SetDefaults(got)
  81. // There are no defaults in proto3. Everything should be the zero value, but
  82. // we need to remember to set defaults for nested proto2 messages.
  83. want := &pb.Message{
  84. Terrain: map[string]*pb.Nested{
  85. "meadow": new(pb.Nested),
  86. },
  87. Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
  88. Proto2Value: map[string]*tpb.SubDefaults{
  89. "badlands": &tpb.SubDefaults{N: proto.Int64(7)},
  90. },
  91. }
  92. if !proto.Equal(got, want) {
  93. t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
  94. }
  95. }
  96. func TestUnknownFieldPreservation(t *testing.T) {
  97. b1 := "\x0a\x05David" // Known tag 1
  98. b2 := "\xc2\x0c\x06Google" // Unknown tag 200
  99. b := []byte(b1 + b2)
  100. m := new(pb.Message)
  101. if err := proto.Unmarshal(b, m); err != nil {
  102. t.Fatalf("proto.Unmarshal: %v", err)
  103. }
  104. if !bytes.Equal(m.XXX_unrecognized, []byte(b2)) {
  105. t.Fatalf("mismatching unknown fields:\ngot %q\nwant %q", m.XXX_unrecognized, b2)
  106. }
  107. }