discard_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "testing"
  7. "github.com/golang/protobuf/proto"
  8. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  9. pb "github.com/golang/protobuf/proto/test_proto"
  10. )
  11. const rawFields = "\x2d\xc3\xd2\xe1\xf0"
  12. func TestDiscardUnknown(t *testing.T) {
  13. tests := []struct {
  14. desc string
  15. in, want proto.Message
  16. }{{
  17. desc: "Nil",
  18. in: nil, want: nil, // Should not panic
  19. }, {
  20. desc: "NilPtr",
  21. in: (*proto3pb.Message)(nil), want: (*proto3pb.Message)(nil), // Should not panic
  22. }, {
  23. desc: "Nested",
  24. in: &proto3pb.Message{
  25. Name: "Aaron",
  26. Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte(rawFields)},
  27. XXX_unrecognized: []byte(rawFields),
  28. },
  29. want: &proto3pb.Message{
  30. Name: "Aaron",
  31. Nested: &proto3pb.Nested{Cute: true},
  32. },
  33. }, {
  34. desc: "Slice",
  35. in: &proto3pb.Message{
  36. Name: "Aaron",
  37. Children: []*proto3pb.Message{
  38. {Name: "Sarah", XXX_unrecognized: []byte(rawFields)},
  39. {Name: "Abraham", XXX_unrecognized: []byte(rawFields)},
  40. },
  41. XXX_unrecognized: []byte(rawFields),
  42. },
  43. want: &proto3pb.Message{
  44. Name: "Aaron",
  45. Children: []*proto3pb.Message{
  46. {Name: "Sarah"},
  47. {Name: "Abraham"},
  48. },
  49. },
  50. }, {
  51. desc: "OneOf",
  52. in: &pb.Communique{
  53. Union: &pb.Communique_Msg{&pb.Strings{
  54. StringField: proto.String("123"),
  55. XXX_unrecognized: []byte(rawFields),
  56. }},
  57. XXX_unrecognized: []byte(rawFields),
  58. },
  59. want: &pb.Communique{
  60. Union: &pb.Communique_Msg{&pb.Strings{StringField: proto.String("123")}},
  61. },
  62. }, {
  63. desc: "Map",
  64. in: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
  65. 0x4002: &pb.FloatingPoint{
  66. Exact: proto.Bool(true),
  67. XXX_unrecognized: []byte(rawFields),
  68. },
  69. }},
  70. want: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
  71. 0x4002: &pb.FloatingPoint{Exact: proto.Bool(true)},
  72. }},
  73. }, {
  74. desc: "Extension",
  75. in: func() proto.Message {
  76. m := &pb.MyMessage{
  77. Count: proto.Int32(42),
  78. Somegroup: &pb.MyMessage_SomeGroup{
  79. GroupField: proto.Int32(6),
  80. XXX_unrecognized: []byte(rawFields),
  81. },
  82. XXX_unrecognized: []byte(rawFields),
  83. }
  84. proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{
  85. Data: proto.String("extension"),
  86. XXX_unrecognized: []byte(rawFields),
  87. })
  88. return m
  89. }(),
  90. want: func() proto.Message {
  91. m := &pb.MyMessage{
  92. Count: proto.Int32(42),
  93. Somegroup: &pb.MyMessage_SomeGroup{GroupField: proto.Int32(6)},
  94. }
  95. proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{Data: proto.String("extension")})
  96. return m
  97. }(),
  98. }}
  99. for _, tt := range tests {
  100. proto.DiscardUnknown(tt.in)
  101. if !proto.Equal(tt.in, tt.want) {
  102. t.Errorf("test %s, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, tt.in, tt.want)
  103. }
  104. }
  105. }