|
|
@@ -7,12 +7,15 @@ package proto_test
|
|
|
import (
|
|
|
"testing"
|
|
|
|
|
|
+ protoV1a "github.com/golang/protobuf/internal/proto"
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
|
proto3pb "github.com/golang/protobuf/proto/proto3_proto"
|
|
|
pb "github.com/golang/protobuf/proto/test_proto"
|
|
|
)
|
|
|
|
|
|
+const rawFields = "\x2d\xc3\xd2\xe1\xf0"
|
|
|
+
|
|
|
func TestDiscardUnknown(t *testing.T) {
|
|
|
tests := []struct {
|
|
|
desc string
|
|
|
@@ -27,8 +30,8 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
desc: "Nested",
|
|
|
in: &proto3pb.Message{
|
|
|
Name: "Aaron",
|
|
|
- Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte("blah")},
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte(rawFields)},
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
},
|
|
|
want: &proto3pb.Message{
|
|
|
Name: "Aaron",
|
|
|
@@ -39,10 +42,10 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
in: &proto3pb.Message{
|
|
|
Name: "Aaron",
|
|
|
Children: []*proto3pb.Message{
|
|
|
- {Name: "Sarah", XXX_unrecognized: []byte("blah")},
|
|
|
- {Name: "Abraham", XXX_unrecognized: []byte("blah")},
|
|
|
+ {Name: "Sarah", XXX_unrecognized: []byte(rawFields)},
|
|
|
+ {Name: "Abraham", XXX_unrecognized: []byte(rawFields)},
|
|
|
},
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
},
|
|
|
want: &proto3pb.Message{
|
|
|
Name: "Aaron",
|
|
|
@@ -56,9 +59,9 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
in: &pb.Communique{
|
|
|
Union: &pb.Communique_Msg{&pb.Strings{
|
|
|
StringField: proto.String("123"),
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
}},
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
},
|
|
|
want: &pb.Communique{
|
|
|
Union: &pb.Communique_Msg{&pb.Strings{StringField: proto.String("123")}},
|
|
|
@@ -68,7 +71,7 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
in: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
|
|
|
0x4002: &pb.FloatingPoint{
|
|
|
Exact: proto.Bool(true),
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
},
|
|
|
}},
|
|
|
want: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
|
|
|
@@ -81,13 +84,13 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
Count: proto.Int32(42),
|
|
|
Somegroup: &pb.MyMessage_SomeGroup{
|
|
|
GroupField: proto.Int32(6),
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
},
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
}
|
|
|
proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{
|
|
|
Data: proto.String("extension"),
|
|
|
- XXX_unrecognized: []byte("blah"),
|
|
|
+ XXX_unrecognized: []byte(rawFields),
|
|
|
})
|
|
|
return m
|
|
|
}(),
|
|
|
@@ -101,6 +104,20 @@ func TestDiscardUnknown(t *testing.T) {
|
|
|
}(),
|
|
|
}}
|
|
|
|
|
|
+ // Test the reflection code path.
|
|
|
+ for _, tt := range tests {
|
|
|
+ // Clone the input so that we don't alter the original.
|
|
|
+ in := tt.in
|
|
|
+ if in != nil {
|
|
|
+ in = proto.Clone(tt.in)
|
|
|
+ }
|
|
|
+
|
|
|
+ protoV1a.DiscardUnknown(tt.in)
|
|
|
+ if !proto.Equal(tt.in, tt.want) {
|
|
|
+ t.Errorf("test %s, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, tt.in, tt.want)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// Test the legacy code path.
|
|
|
for _, tt := range tests {
|
|
|
// Clone the input so that we don't alter the original.
|