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