blob: db5614fd1d0114d57d54321fbd9a13a86e257343 [file] [log] [blame]
David Symondsfc16c2c2012-11-07 23:41:17 +11001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2012 The Go Authors. All rights reserved.
David Symonds558f13f2014-11-24 10:28:53 +11004// https://github.com/golang/protobuf
David Symondsfc16c2c2012-11-07 23:41:17 +11005//
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 "log"
David Symondsa8323e22015-03-24 10:18:00 +110036 "strings"
David Symondsfc16c2c2012-11-07 23:41:17 +110037 "testing"
38
David Symonds558f13f2014-11-24 10:28:53 +110039 . "github.com/golang/protobuf/proto"
David Symondsa8de2842015-03-20 16:24:29 +110040 proto3pb "github.com/golang/protobuf/proto/proto3_proto"
41 pb "github.com/golang/protobuf/proto/testdata"
David Symondsfc16c2c2012-11-07 23:41:17 +110042)
43
44var messageWithExtension1 = &pb.MyMessage{Count: Int32(7)}
David Symondsbe02a4a2012-12-06 15:20:41 +110045
46// messageWithExtension2 is in equal_test.go.
David Symondsfc16c2c2012-11-07 23:41:17 +110047var messageWithExtension3 = &pb.MyMessage{Count: Int32(8)}
48
49func 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)
David Symonds20370902013-03-23 17:20:01 +110059
David Symondsfc16c2c2012-11-07 23:41:17 +110060}
61
62var SizeTests = []struct {
63 desc string
64 pb Message
65}{
66 {"empty", &pb.OtherMessage{}},
67 // Basic types.
68 {"bool", &pb.Defaults{F_Bool: Bool(true)}},
69 {"int32", &pb.Defaults{F_Int32: Int32(12)}},
David Symondsf054e842014-07-22 14:06:27 +100070 {"negative int32", &pb.Defaults{F_Int32: Int32(-1)}},
David Symondsfc16c2c2012-11-07 23:41:17 +110071 {"small int64", &pb.Defaults{F_Int64: Int64(1)}},
72 {"big int64", &pb.Defaults{F_Int64: Int64(1 << 20)}},
David Symondsf054e842014-07-22 14:06:27 +100073 {"negative int64", &pb.Defaults{F_Int64: Int64(-1)}},
David Symondsfc16c2c2012-11-07 23:41:17 +110074 {"fixed32", &pb.Defaults{F_Fixed32: Uint32(71)}},
75 {"fixed64", &pb.Defaults{F_Fixed64: Uint64(72)}},
76 {"uint32", &pb.Defaults{F_Uint32: Uint32(123)}},
77 {"uint64", &pb.Defaults{F_Uint64: Uint64(124)}},
78 {"float", &pb.Defaults{F_Float: Float32(12.6)}},
79 {"double", &pb.Defaults{F_Double: Float64(13.9)}},
80 {"string", &pb.Defaults{F_String: String("niles")}},
81 {"bytes", &pb.Defaults{F_Bytes: []byte("wowsa")}},
82 {"bytes, empty", &pb.Defaults{F_Bytes: []byte{}}},
83 {"sint32", &pb.Defaults{F_Sint32: Int32(65)}},
84 {"sint64", &pb.Defaults{F_Sint64: Int64(67)}},
85 {"enum", &pb.Defaults{F_Enum: pb.Defaults_BLUE.Enum()}},
86 // Repeated.
87 {"empty repeated bool", &pb.MoreRepeated{Bools: []bool{}}},
88 {"repeated bool", &pb.MoreRepeated{Bools: []bool{false, true, true, false}}},
89 {"packed repeated bool", &pb.MoreRepeated{BoolsPacked: []bool{false, true, true, false, true, true, true}}},
David Symonds0ec36a22014-08-12 13:21:46 +100090 {"repeated int32", &pb.MoreRepeated{Ints: []int32{1, 12203, 1729, -1}}},
David Symondsfc16c2c2012-11-07 23:41:17 +110091 {"repeated int32 packed", &pb.MoreRepeated{IntsPacked: []int32{1, 12203, 1729}}},
David Symonds9c8d8472013-06-24 10:56:58 +100092 {"repeated int64 packed", &pb.MoreRepeated{Int64SPacked: []int64{
93 // Need enough large numbers to verify that the header is counting the number of bytes
94 // for the field, not the number of elements.
95 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
96 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62, 1 << 62,
97 }}},
David Symondsfc16c2c2012-11-07 23:41:17 +110098 {"repeated string", &pb.MoreRepeated{Strings: []string{"r", "ken", "gri"}}},
David Symonds40877592013-06-08 09:17:00 +100099 {"repeated fixed", &pb.MoreRepeated{Fixeds: []uint32{1, 2, 3, 4}}},
David Symondsfc16c2c2012-11-07 23:41:17 +1100100 // Nested.
101 {"nested", &pb.OldMessage{Nested: &pb.OldMessage_Nested{Name: String("whatever")}}},
David Symonds6677c3e2013-06-11 08:48:47 +1000102 {"group", &pb.GroupOld{G: &pb.GroupOld_G{X: Int32(12345)}}},
David Symondsfc16c2c2012-11-07 23:41:17 +1100103 // Other things.
104 {"unrecognized", &pb.MoreRepeated{XXX_unrecognized: []byte{13<<3 | 0, 4}}},
105 {"extension (unencoded)", messageWithExtension1},
106 {"extension (encoded)", messageWithExtension3},
David Symondsabd3b412014-11-28 11:43:44 +1100107 // proto3 message
108 {"proto3 empty", &proto3pb.Message{}},
109 {"proto3 bool", &proto3pb.Message{TrueScotsman: true}},
110 {"proto3 int64", &proto3pb.Message{ResultCount: 1}},
111 {"proto3 uint32", &proto3pb.Message{HeightInCm: 123}},
112 {"proto3 float", &proto3pb.Message{Score: 12.6}},
113 {"proto3 string", &proto3pb.Message{Name: "Snezana"}},
114 {"proto3 bytes", &proto3pb.Message{Data: []byte("wowsa")}},
115 {"proto3 bytes, empty", &proto3pb.Message{Data: []byte{}}},
116 {"proto3 enum", &proto3pb.Message{Hilarity: proto3pb.Message_PUNS}},
David Symonds889ae492015-03-23 09:48:33 +1100117 {"proto3 map field with empty bytes", &proto3pb.MessageWithMap{ByteMapping: map[bool][]byte{false: []byte{}}}},
David Symonds3ea3e052014-12-22 16:15:28 +1100118
119 {"map field", &pb.MessageWithMap{NameMapping: map[int32]string{1: "Rob", 7: "Andrew"}}},
120 {"map field with message", &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{0x7001: &pb.FloatingPoint{F: Float64(2.0)}}}},
121 {"map field with bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte("this time for sure")}}},
David Symonds889ae492015-03-23 09:48:33 +1100122 {"map field with empty bytes", &pb.MessageWithMap{ByteMapping: map[bool][]byte{true: []byte{}}}},
David Symondsa8323e22015-03-24 10:18:00 +1100123
124 {"map field with big entry", &pb.MessageWithMap{NameMapping: map[int32]string{8: strings.Repeat("x", 125)}}},
125 {"map field with big key and val", &pb.MessageWithMap{StrToStr: map[string]string{strings.Repeat("x", 70): strings.Repeat("y", 70)}}},
126 {"map field with big numeric key", &pb.MessageWithMap{NameMapping: map[int32]string{0xf00d: "om nom nom"}}},
David Symondsfc16c2c2012-11-07 23:41:17 +1100127}
128
129func TestSize(t *testing.T) {
130 for _, tc := range SizeTests {
131 size := Size(tc.pb)
132 b, err := Marshal(tc.pb)
133 if err != nil {
134 t.Errorf("%v: Marshal failed: %v", tc.desc, err)
135 continue
136 }
137 if size != len(b) {
138 t.Errorf("%v: Size(%v) = %d, want %d", tc.desc, tc.pb, size, len(b))
139 t.Logf("%v: bytes: %#v", tc.desc, b)
140 }
141 }
142}