1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2010 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
32 // Test that we can use protocol buffers that use extensions.
43 "github.com/golang/protobuf/proto"
44 base "extension_base.pb"
45 user "extension_user.pb"
48 func TestSingleFieldExtension(t *testing.T) {
49 bm := &base.BaseMessage{
50 Height: proto.Int32(178),
53 // Use extension within scope of another type.
54 vol := proto.Uint32(11)
55 err := proto.SetExtension(bm, user.E_LoudMessage_Volume, vol)
57 t.Fatal("Failed setting extension:", err)
59 buf, err := proto.Marshal(bm)
61 t.Fatal("Failed encoding message with extension:", err)
63 bm_new := new(base.BaseMessage)
64 if err := proto.Unmarshal(buf, bm_new); err != nil {
65 t.Fatal("Failed decoding message with extension:", err)
67 if !proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
68 t.Fatal("Decoded message didn't contain extension.")
70 vol_out, err := proto.GetExtension(bm_new, user.E_LoudMessage_Volume)
72 t.Fatal("Failed getting extension:", err)
74 if v := vol_out.(*uint32); *v != *vol {
75 t.Errorf("vol_out = %v, expected %v", *v, *vol)
77 proto.ClearExtension(bm_new, user.E_LoudMessage_Volume)
78 if proto.HasExtension(bm_new, user.E_LoudMessage_Volume) {
79 t.Fatal("Failed clearing extension.")
83 func TestMessageExtension(t *testing.T) {
84 bm := &base.BaseMessage{
85 Height: proto.Int32(179),
88 // Use extension that is itself a message.
89 um := &user.UserMessage{
90 Name: proto.String("Dave"),
91 Rank: proto.String("Major"),
93 err := proto.SetExtension(bm, user.E_LoginMessage_UserMessage, um)
95 t.Fatal("Failed setting extension:", err)
97 buf, err := proto.Marshal(bm)
99 t.Fatal("Failed encoding message with extension:", err)
101 bm_new := new(base.BaseMessage)
102 if err := proto.Unmarshal(buf, bm_new); err != nil {
103 t.Fatal("Failed decoding message with extension:", err)
105 if !proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
106 t.Fatal("Decoded message didn't contain extension.")
108 um_out, err := proto.GetExtension(bm_new, user.E_LoginMessage_UserMessage)
110 t.Fatal("Failed getting extension:", err)
112 if n := um_out.(*user.UserMessage).Name; *n != *um.Name {
113 t.Errorf("um_out.Name = %q, expected %q", *n, *um.Name)
115 if r := um_out.(*user.UserMessage).Rank; *r != *um.Rank {
116 t.Errorf("um_out.Rank = %q, expected %q", *r, *um.Rank)
118 proto.ClearExtension(bm_new, user.E_LoginMessage_UserMessage)
119 if proto.HasExtension(bm_new, user.E_LoginMessage_UserMessage) {
120 t.Fatal("Failed clearing extension.")
124 func TestTopLevelExtension(t *testing.T) {
125 bm := &base.BaseMessage{
126 Height: proto.Int32(179),
129 width := proto.Int32(17)
130 err := proto.SetExtension(bm, user.E_Width, width)
132 t.Fatal("Failed setting extension:", err)
134 buf, err := proto.Marshal(bm)
136 t.Fatal("Failed encoding message with extension:", err)
138 bm_new := new(base.BaseMessage)
139 if err := proto.Unmarshal(buf, bm_new); err != nil {
140 t.Fatal("Failed decoding message with extension:", err)
142 if !proto.HasExtension(bm_new, user.E_Width) {
143 t.Fatal("Decoded message didn't contain extension.")
145 width_out, err := proto.GetExtension(bm_new, user.E_Width)
147 t.Fatal("Failed getting extension:", err)
149 if w := width_out.(*int32); *w != *width {
150 t.Errorf("width_out = %v, expected %v", *w, *width)
152 proto.ClearExtension(bm_new, user.E_Width)
153 if proto.HasExtension(bm_new, user.E_Width) {
154 t.Fatal("Failed clearing extension.")
158 func TestMessageSetWireFormat(t *testing.T) {
159 osm := new(base.OldStyleMessage)
160 osp := &user.OldStyleParcel{
161 Name: proto.String("Dave"),
162 Height: proto.Int32(178),
165 err := proto.SetExtension(osm, user.E_OldStyleParcel_MessageSetExtension, osp)
167 t.Fatal("Failed setting extension:", err)
170 buf, err := proto.Marshal(osm)
172 t.Fatal("Failed encoding message:", err)
175 // Data generated from Python implementation.
177 11, 16, 209, 15, 26, 9, 10, 4, 68, 97, 118, 101, 16, 178, 1, 12,
180 if !bytes.Equal(expected, buf) {
181 t.Errorf("Encoding mismatch.\nwant %+v\n got %+v", expected, buf)
184 // Check that it is restored correctly.
185 osm = new(base.OldStyleMessage)
186 if err := proto.Unmarshal(buf, osm); err != nil {
187 t.Fatal("Failed decoding message:", err)
189 osp_out, err := proto.GetExtension(osm, user.E_OldStyleParcel_MessageSetExtension)
191 t.Fatal("Failed getting extension:", err)
193 osp = osp_out.(*user.OldStyleParcel)
194 if *osp.Name != "Dave" || *osp.Height != 178 {
195 t.Errorf("Retrieved extension from decoded message is not correct: %+v", osp)
200 // simpler than rigging up gotest
201 testing.Main(regexp.MatchString, []testing.InternalTest{
202 {"TestSingleFieldExtension", TestSingleFieldExtension},
203 {"TestMessageExtension", TestMessageExtension},
204 {"TestTopLevelExtension", TestTopLevelExtension},
206 []testing.InternalBenchmark{},
207 []testing.InternalExample{})