Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

171 rader
5.4 KiB

  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2017 The Go Authors. All rights reserved.
  4. // https://github.com/golang/protobuf
  5. //
  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. package proto_test
  32. import (
  33. "testing"
  34. "github.com/golang/protobuf/proto"
  35. proto3pb "github.com/golang/protobuf/proto/proto3_proto"
  36. pb "github.com/golang/protobuf/proto/test_proto"
  37. )
  38. func TestDiscardUnknown(t *testing.T) {
  39. tests := []struct {
  40. desc string
  41. in, want proto.Message
  42. }{{
  43. desc: "Nil",
  44. in: nil, want: nil, // Should not panic
  45. }, {
  46. desc: "NilPtr",
  47. in: (*proto3pb.Message)(nil), want: (*proto3pb.Message)(nil), // Should not panic
  48. }, {
  49. desc: "Nested",
  50. in: &proto3pb.Message{
  51. Name: "Aaron",
  52. Nested: &proto3pb.Nested{Cute: true, XXX_unrecognized: []byte("blah")},
  53. XXX_unrecognized: []byte("blah"),
  54. },
  55. want: &proto3pb.Message{
  56. Name: "Aaron",
  57. Nested: &proto3pb.Nested{Cute: true},
  58. },
  59. }, {
  60. desc: "Slice",
  61. in: &proto3pb.Message{
  62. Name: "Aaron",
  63. Children: []*proto3pb.Message{
  64. {Name: "Sarah", XXX_unrecognized: []byte("blah")},
  65. {Name: "Abraham", XXX_unrecognized: []byte("blah")},
  66. },
  67. XXX_unrecognized: []byte("blah"),
  68. },
  69. want: &proto3pb.Message{
  70. Name: "Aaron",
  71. Children: []*proto3pb.Message{
  72. {Name: "Sarah"},
  73. {Name: "Abraham"},
  74. },
  75. },
  76. }, {
  77. desc: "OneOf",
  78. in: &pb.Communique{
  79. Union: &pb.Communique_Msg{&pb.Strings{
  80. StringField: proto.String("123"),
  81. XXX_unrecognized: []byte("blah"),
  82. }},
  83. XXX_unrecognized: []byte("blah"),
  84. },
  85. want: &pb.Communique{
  86. Union: &pb.Communique_Msg{&pb.Strings{StringField: proto.String("123")}},
  87. },
  88. }, {
  89. desc: "Map",
  90. in: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
  91. 0x4002: &pb.FloatingPoint{
  92. Exact: proto.Bool(true),
  93. XXX_unrecognized: []byte("blah"),
  94. },
  95. }},
  96. want: &pb.MessageWithMap{MsgMapping: map[int64]*pb.FloatingPoint{
  97. 0x4002: &pb.FloatingPoint{Exact: proto.Bool(true)},
  98. }},
  99. }, {
  100. desc: "Extension",
  101. in: func() proto.Message {
  102. m := &pb.MyMessage{
  103. Count: proto.Int32(42),
  104. Somegroup: &pb.MyMessage_SomeGroup{
  105. GroupField: proto.Int32(6),
  106. XXX_unrecognized: []byte("blah"),
  107. },
  108. XXX_unrecognized: []byte("blah"),
  109. }
  110. proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{
  111. Data: proto.String("extension"),
  112. XXX_unrecognized: []byte("blah"),
  113. })
  114. return m
  115. }(),
  116. want: func() proto.Message {
  117. m := &pb.MyMessage{
  118. Count: proto.Int32(42),
  119. Somegroup: &pb.MyMessage_SomeGroup{GroupField: proto.Int32(6)},
  120. }
  121. proto.SetExtension(m, pb.E_Ext_More, &pb.Ext{Data: proto.String("extension")})
  122. return m
  123. }(),
  124. }}
  125. // Test the legacy code path.
  126. for _, tt := range tests {
  127. // Clone the input so that we don't alter the original.
  128. in := tt.in
  129. if in != nil {
  130. in = proto.Clone(tt.in)
  131. }
  132. var m LegacyMessage
  133. m.Message, _ = in.(*proto3pb.Message)
  134. m.Communique, _ = in.(*pb.Communique)
  135. m.MessageWithMap, _ = in.(*pb.MessageWithMap)
  136. m.MyMessage, _ = in.(*pb.MyMessage)
  137. proto.DiscardUnknown(&m)
  138. if !proto.Equal(in, tt.want) {
  139. t.Errorf("test %s/Legacy, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, in, tt.want)
  140. }
  141. }
  142. for _, tt := range tests {
  143. proto.DiscardUnknown(tt.in)
  144. if !proto.Equal(tt.in, tt.want) {
  145. t.Errorf("test %s, expected unknown fields to be discarded\ngot %v\nwant %v", tt.desc, tt.in, tt.want)
  146. }
  147. }
  148. }
  149. // LegacyMessage is a proto.Message that has several nested messages.
  150. // This does not have the XXX_DiscardUnknown method and so forces DiscardUnknown
  151. // to use the legacy fallback logic.
  152. type LegacyMessage struct {
  153. Message *proto3pb.Message
  154. Communique *pb.Communique
  155. MessageWithMap *pb.MessageWithMap
  156. MyMessage *pb.MyMessage
  157. }
  158. func (m *LegacyMessage) Reset() { *m = LegacyMessage{} }
  159. func (m *LegacyMessage) String() string { return proto.CompactTextString(m) }
  160. func (*LegacyMessage) ProtoMessage() {}