size_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2012 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. "log"
  34. "strings"
  35. "testing"
  36. . "github.com/golang/protobuf/proto"
  37. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  38. pb "github.com/golang/protobuf/proto/test_proto"
  39. )
  40. var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
  41. // messageWithExtension2 is in equal_test.go.
  42. var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
  43. func init() {
  44. if err := SetExtension(messageWithExtension1, pb.E_Ext_More, &pb.Ext{Data: String("Abbott")}); err != nil {
  45. log.Panicf("SetExtension: %v", err)
  46. }
  47. if err := SetExtension(messageWithExtension3, pb.E_Ext_More, &pb.Ext{Data: String("Costello")}); err != nil {
  48. log.Panicf("SetExtension: %v", err)
  49. }
  50. // Force messageWithExtension3 to have the extension encoded.
  51. Marshal(messageWithExtension3)
  52. }
  53. // non-pointer custom message
  54. type nonptrMessage struct{}
  55. func (m nonptrMessage) ProtoMessage() {}
  56. func (m nonptrMessage) Reset() {}
  57. func (m nonptrMessage) String() string { return "" }
  58. func (m nonptrMessage) Marshal() ([]byte, error) {
  59. return []byte{42}, nil
  60. }
  61. // custom message embedding a proto.Message
  62. type messageWithEmbedding struct {
  63. *pb.OtherMessage
  64. }
  65. func (m *messageWithEmbedding) ProtoMessage() {}
  66. func (m *messageWithEmbedding) Reset() {}
  67. func (m *messageWithEmbedding) String() string { return "" }
  68. func (m *messageWithEmbedding) Marshal() ([]byte, error) {
  69. return []byte{42}, nil
  70. }
  71. var SizeTests = []struct {
  72. desc string
  73. pb Message
  74. }{
  75. {"empty", &pb.OtherMessage{}},
  76. // Basic types.
  77. {"bool", &pb.Defaults{F_Bool: Bool(true)}},
  78. {"int32", &pb.Defaults{F_Int32: Int32(12)}},
  79. {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}},
  80. {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
  81. {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
  82. {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}},
  83. {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
  84. {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
  85. {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
  86. {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
  87. {"float", &pb.Defaults{F_Float: Float32(12.6)}},
  88. {"double", &pb.Defaults{F_Double: Float64(13.9)}},
  89. {"string", &pb.Defaults{F_String: String("niles")}},
  90. {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
  91. {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
  92. {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
  93. {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
  94. {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
  95. // Repeated.
  96. {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
  97. {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
  98. {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
  99. {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
  100. {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
  101. {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{
  102. // Need enough large numbers to verify that the header is counting the number of bytes
  103. // for the field, not the number of elements.
  104. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  105. 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
  106. }}},
  107. {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
  108. {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
  109. // Nested.
  110. {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
  111. {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
  112. // Other things.
  113. {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
  114. {"extension (unencoded)", messageWithExtension1},
  115. {"extension (encoded)", messageWithExtension3},
  116. // proto3 message
  117. {"proto3 empty", &proto3pb.Message{}},
  118. {"proto3 bool", &proto3pb.Message{TrueScotsman: true}},
  119. {"proto3 int64", &proto3pb.Message{ResultCount: 1}},
  120. {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}},
  121. {"proto3 float", &proto3pb.Message{Score: 12.6}},
  122. {"proto3 string", &proto3pb.Message{Name: "Snezana"}},
  123. {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}},
  124. {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}},
  125. {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
  126. {"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},
  127. {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
  128. {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}},
  129. {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
  130. {"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},
  131. {"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
  132. {"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
  133. {"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},
  134. {"oneof not set", &pb.Oneof{}},
  135. {"oneof bool", &pb.Oneof{Union: &pb.Oneof_F_Bool{true}}},
  136. {"oneof zero int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{0}}},
  137. {"oneof big int32", &pb.Oneof{Union: &pb.Oneof_F_Int32{1 << 20}}},
  138. {"oneof int64", &pb.Oneof{Union: &pb.Oneof_F_Int64{42}}},
  139. {"oneof fixed32", &pb.Oneof{Union: &pb.Oneof_F_Fixed32{43}}},
  140. {"oneof fixed64", &pb.Oneof{Union: &pb.Oneof_F_Fixed64{44}}},
  141. {"oneof uint32", &pb.Oneof{Union: &pb.Oneof_F_Uint32{45}}},
  142. {"oneof uint64", &pb.Oneof{Union: &pb.Oneof_F_Uint64{46}}},
  143. {"oneof float", &pb.Oneof{Union: &pb.Oneof_F_Float{47.1}}},
  144. {"oneof double", &pb.Oneof{Union: &pb.Oneof_F_Double{48.9}}},
  145. {"oneof string", &pb.Oneof{Union: &pb.Oneof_F_String{"Rhythmic Fman"}}},
  146. {"oneof bytes", &pb.Oneof{Union: &pb.Oneof_F_Bytes{[]byte("let go")}}},
  147. {"oneof sint32", &pb.Oneof{Union: &pb.Oneof_F_Sint32{50}}},
  148. {"oneof sint64", &pb.Oneof{Union: &pb.Oneof_F_Sint64{51}}},
  149. {"oneof enum", &pb.Oneof{Union: &pb.Oneof_F_Enum{pb.MyMessage_BLUE}}},
  150. {"message for oneof", &pb.GoTestField{Label: String("k"), Type: String("v")}},
  151. {"oneof message", &pb.Oneof{Union: &pb.Oneof_F_Message{&pb.GoTestField{Label: String("k"), Type: String("v")}}}},
  152. {"oneof group", &pb.Oneof{Union: &pb.Oneof_FGroup{&pb.Oneof_F_Group{X: Int32(52)}}}},
  153. {"oneof largest tag", &pb.Oneof{Union: &pb.Oneof_F_Largest_Tag{1}}},
  154. {"multiple oneofs", &pb.Oneof{Union: &pb.Oneof_F_Int32{1}, Tormato: &pb.Oneof_Value{2}}},
  155. {"non-pointer message", nonptrMessage{}},
  156. {"custom message with embedding", &messageWithEmbedding{&pb.OtherMessage{}}},
  157. }
  158. func TestSize(t *testing.T) {
  159. for _, tc := range SizeTests {
  160. size := Size(tc.pb)
  161. b, err := Marshal(tc.pb)
  162. if err != nil {
  163. t.Errorf("%v: Marshal failed: %v", tc.desc, err)
  164. continue
  165. }
  166. if size != len(b) {
  167. t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
  168. t.Logf("%v: bytes: %#v", tc.desc, b)
  169. }
  170. }
  171. }