1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2017 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.
37 "github.com/gogo/protobuf/proto"
38 proto3pb "github.com/gogo/protobuf/proto/proto3_proto"
39 pb "github.com/gogo/protobuf/proto/test_proto"
42 func TestDiscardUnknown(t *testing.T) {
45 in, want proto.Message
48 in: nil, want: nil, // Should not panic
51 in: (*proto3pb.Message)(nil), want: (*proto3pb.Message)(nil), // Should not panic
54 in: &proto3pb.Message{
56 Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte("blah")},
57 XXX_unrecognized: []byte("blah"),
59 want: &proto3pb.Message{
61 Nested: &proto3pb.Nested{Cute: true},
65 in: &proto3pb.Message{
67 Children: []*proto3pb.Message{
68 {Name: "Sarah", XXX_unrecognized: []byte("blah")},
69 {Name: "Abraham", XXX_unrecognized: []byte("blah")},
71 XXX_unrecognized: []byte("blah"),
73 want: &proto3pb.Message{
75 Children: []*proto3pb.Message{
83 Union: &pb.Communique_Msg{Msg: &pb.Strings{
84 StringField: proto.String("123"),
85 XXX_unrecognized: []byte("blah"),
87 XXX_unrecognized: []byte("blah"),
90 Union: &pb.Communique_Msg{Msg: &pb.Strings{StringField: proto.String("123")}},
94 in: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
96 Exact: proto.Bool(true),
97 XXX_unrecognized: []byte("blah"),
100 want: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
101 0x4002: {Exact: proto.Bool(true)},
105 in: func() proto.Message {
107 Count: proto.Int32(42),
108 Somegroup: &pb.MyMessage_SomeGroup{
109 GroupField: proto.Int32(6),
110 XXX_unrecognized: []byte("blah"),
112 XXX_unrecognized: []byte("blah"),
114 proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{
115 Data: proto.String("extension"),
116 XXX_unrecognized: []byte("blah"),
120 want: func() proto.Message {
122 Count: proto.Int32(42),
123 Somegroup: &pb.MyMessage_SomeGroup{GroupField: proto.Int32(6)},
125 proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{Data: proto.String("extension")})
130 // Test the legacy code path.
131 for _, tt := range tests {
132 // Clone the input so that we don't alter the original.
135 in = proto.Clone(tt.in)
139 m.Message, _ = in.(*proto3pb.Message)
140 m.Communique, _ = in.(*pb.Communique)
141 m.MessageWithMap, _ = in.(*pb.MessageWithMap)
142 m.MyMessage, _ = in.(*pb.MyMessage)
143 proto.DiscardUnknown(&m)
144 if !proto.Equal(in, tt.want) {
145 t.Errorf("test %s/Legacy, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, in, tt.want)
149 for _, tt := range tests {
150 proto.DiscardUnknown(tt.in)
151 if !proto.Equal(tt.in, tt.want) {
152 t.Errorf("test %s, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, tt.in, tt.want)
157 // LegacyMessage is a proto.Message that has several nested messages.
158 // This does not have the XXX_DiscardUnknown method and so forces DiscardUnknown
159 // to use the legacy fallback logic.
160 type LegacyMessage struct {
161 Message *proto3pb.Message
162 Communique *pb.Communique
163 MessageWithMap *pb.MessageWithMap
164 MyMessage *pb.MyMessage
167 func (m *LegacyMessage) Reset() { *m = LegacyMessage{} }
168 func (m *LegacyMessage) String() string { return proto.CompactTextString(m) }
169 func (*LegacyMessage) ProtoMessage() {}