You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

152 lines
4.7 KiB

  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2014 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. package proto_test
  32. import (
  33. "bytes"
  34. "testing"
  35. "github.com/golang/protobuf/proto"
  36. pb "github.com/golang/protobuf/proto/proto3_proto"
  37. tpb "github.com/golang/protobuf/proto/test_proto"
  38. )
  39. func TestProto3ZeroValues(t *testing.T) {
  40. tests := []struct {
  41. desc string
  42. m proto.Message
  43. }{
  44. {"zero message", &pb.Message{}},
  45. {"empty bytes field", &pb.Message{Data: []byte{}}},
  46. }
  47. for _, test := range tests {
  48. b, err := proto.Marshal(test.m)
  49. if err != nil {
  50. t.Errorf("%s: proto.Marshal: %v", test.desc, err)
  51. continue
  52. }
  53. if len(b) > 0 {
  54. t.Errorf("%s: Encoding is non-empty: %q", test.desc, b)
  55. }
  56. }
  57. }
  58. func TestRoundTripProto3(t *testing.T) {
  59. m := &pb.Message{
  60. Name: "David", // (2 | 1<<3): 0x0a 0x05 "David"
  61. Hilarity: pb.Message_PUNS, // (0 | 2<<3): 0x10 0x01
  62. HeightInCm: 178, // (0 | 3<<3): 0x18 0xb2 0x01
  63. Data: []byte("roboto"), // (2 | 4<<3): 0x20 0x06 "roboto"
  64. ResultCount: 47, // (0 | 7<<3): 0x38 0x2f
  65. TrueScotsman: true, // (0 | 8<<3): 0x40 0x01
  66. Score: 8.1, // (5 | 9<<3): 0x4d <8.1>
  67. Key: []uint64{1, 0xdeadbeef},
  68. Nested: &pb.Nested{
  69. Bunny: "Monty",
  70. },
  71. }
  72. t.Logf(" m: %v", m)
  73. b, err := proto.Marshal(m)
  74. if err != nil {
  75. t.Fatalf("proto.Marshal: %v", err)
  76. }
  77. t.Logf(" b: %q", b)
  78. m2 := new(pb.Message)
  79. if err := proto.Unmarshal(b, m2); err != nil {
  80. t.Fatalf("proto.Unmarshal: %v", err)
  81. }
  82. t.Logf("m2: %v", m2)
  83. if !proto.Equal(m, m2) {
  84. t.Errorf("proto.Equal returned false:\n m: %v\nm2: %v", m, m2)
  85. }
  86. }
  87. func TestGettersForBasicTypesExist(t *testing.T) {
  88. var m pb.Message
  89. if got := m.GetNested().GetBunny(); got != "" {
  90. t.Errorf("m.GetNested().GetBunny() = %q, want empty string", got)
  91. }
  92. if got := m.GetNested().GetCute(); got {
  93. t.Errorf("m.GetNested().GetCute() = %t, want false", got)
  94. }
  95. }
  96. func TestProto3SetDefaults(t *testing.T) {
  97. in := &pb.Message{
  98. Terrain: map[string]*pb.Nested{
  99. "meadow": new(pb.Nested),
  100. },
  101. Proto2Field: new(tpb.SubDefaults),
  102. Proto2Value: map[string]*tpb.SubDefaults{
  103. "badlands": new(tpb.SubDefaults),
  104. },
  105. }
  106. got := proto.Clone(in).(*pb.Message)
  107. proto.SetDefaults(got)
  108. // There are no defaults in proto3. Everything should be the zero value, but
  109. // we need to remember to set defaults for nested proto2 messages.
  110. want := &pb.Message{
  111. Terrain: map[string]*pb.Nested{
  112. "meadow": new(pb.Nested),
  113. },
  114. Proto2Field: &tpb.SubDefaults{N: proto.Int64(7)},
  115. Proto2Value: map[string]*tpb.SubDefaults{
  116. "badlands": &tpb.SubDefaults{N: proto.Int64(7)},
  117. },
  118. }
  119. if !proto.Equal(got, want) {
  120. t.Errorf("with in = %v\nproto.SetDefaults(in) =>\ngot %v\nwant %v", in, got, want)
  121. }
  122. }
  123. func TestUnknownFieldPreservation(t *testing.T) {
  124. b1 := "\x0a\x05David" // Known tag 1
  125. b2 := "\xc2\x0c\x06Google" // Unknown tag 200
  126. b := []byte(b1 + b2)
  127. m := new(pb.Message)
  128. if err := proto.Unmarshal(b, m); err != nil {
  129. t.Fatalf("proto.Unmarshal: %v", err)
  130. }
  131. if !bytes.Equal(m.XXX_unrecognized, []byte(b2)) {
  132. t.Fatalf("mismatching unknown fields:\ngot %q\nwant %q", m.XXX_unrecognized, b2)
  133. }
  134. }