clone_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // Go support for Protocol Buffers - Google's data interchange format
  2. //
  3. // Copyright 2011 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. var cloneTestMessage = &pb.MyMessage{
  39. Count: proto.Int32(42),
  40. Name: proto.String("Dave"),
  41. Pet: []string{"bunny", "kitty", "horsey"},
  42. Inner: &pb.InnerMessage{
  43. Host: proto.String("niles"),
  44. Port: proto.Int32(9099),
  45. Connected: proto.Bool(true),
  46. },
  47. Others: []*pb.OtherMessage{
  48. {
  49. Value: []byte("some bytes"),
  50. },
  51. },
  52. Somegroup: &pb.MyMessage_SomeGroup{
  53. GroupField: proto.Int32(6),
  54. },
  55. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  56. }
  57. func init() {
  58. ext := &pb.Ext{
  59. Data: proto.String("extension"),
  60. }
  61. if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_More, ext); err != nil {
  62. panic("SetExtension: " + err.Error())
  63. }
  64. if err := proto.SetExtension(cloneTestMessage, pb.E_Ext_Text, proto.String("hello")); err != nil {
  65. panic("SetExtension: " + err.Error())
  66. }
  67. if err := proto.SetExtension(cloneTestMessage, pb.E_Greeting, []string{"one", "two"}); err != nil {
  68. panic("SetExtension: " + err.Error())
  69. }
  70. }
  71. func TestClone(t *testing.T) {
  72. // Create a clone using a marshal/unmarshal roundtrip.
  73. vanilla := new(pb.MyMessage)
  74. b, err := proto.Marshal(cloneTestMessage)
  75. if err != nil {
  76. t.Errorf("unexpected Marshal error: %v", err)
  77. }
  78. if err := proto.Unmarshal(b, vanilla); err != nil {
  79. t.Errorf("unexpected Unarshal error: %v", err)
  80. }
  81. // Create a clone using Clone and verify that it is equal to the original.
  82. m := proto.Clone(cloneTestMessage).(*pb.MyMessage)
  83. if !proto.Equal(m, cloneTestMessage) {
  84. t.Fatalf("Clone(%v) = %v", cloneTestMessage, m)
  85. }
  86. // Mutate the clone, which should not affect the original.
  87. x1, err := proto.GetExtension(m, pb.E_Ext_More)
  88. if err != nil {
  89. t.Errorf("unexpected GetExtension(%v) error: %v", pb.E_Ext_More.Name, err)
  90. }
  91. x2, err := proto.GetExtension(m, pb.E_Ext_Text)
  92. if err != nil {
  93. t.Errorf("unexpected GetExtension(%v) error: %v", pb.E_Ext_Text.Name, err)
  94. }
  95. x3, err := proto.GetExtension(m, pb.E_Greeting)
  96. if err != nil {
  97. t.Errorf("unexpected GetExtension(%v) error: %v", pb.E_Greeting.Name, err)
  98. }
  99. *m.Inner.Port++
  100. *(x1.(*pb.Ext)).Data = "blah blah"
  101. *(x2.(*string)) = "goodbye"
  102. x3.([]string)[0] = "zero"
  103. if !proto.Equal(cloneTestMessage, vanilla) {
  104. t.Fatalf("mutation on original detected:\ngot %v\nwant %v", cloneTestMessage, vanilla)
  105. }
  106. }
  107. func TestCloneNil(t *testing.T) {
  108. var m *pb.MyMessage
  109. if c := proto.Clone(m); !proto.Equal(m, c) {
  110. t.Errorf("Clone(%v) = %v", m, c)
  111. }
  112. }
  113. var mergeTests = []struct {
  114. src, dst, want proto.Message
  115. }{
  116. {
  117. src: &pb.MyMessage{
  118. Count: proto.Int32(42),
  119. },
  120. dst: &pb.MyMessage{
  121. Name: proto.String("Dave"),
  122. },
  123. want: &pb.MyMessage{
  124. Count: proto.Int32(42),
  125. Name: proto.String("Dave"),
  126. },
  127. },
  128. {
  129. src: &pb.MyMessage{
  130. Inner: &pb.InnerMessage{
  131. Host: proto.String("hey"),
  132. Connected: proto.Bool(true),
  133. },
  134. Pet: []string{"horsey"},
  135. Others: []*pb.OtherMessage{
  136. {
  137. Value: []byte("some bytes"),
  138. },
  139. },
  140. },
  141. dst: &pb.MyMessage{
  142. Inner: &pb.InnerMessage{
  143. Host: proto.String("niles"),
  144. Port: proto.Int32(9099),
  145. },
  146. Pet: []string{"bunny", "kitty"},
  147. Others: []*pb.OtherMessage{
  148. {
  149. Key: proto.Int64(31415926535),
  150. },
  151. {
  152. // Explicitly test a src=nil field
  153. Inner: nil,
  154. },
  155. },
  156. },
  157. want: &pb.MyMessage{
  158. Inner: &pb.InnerMessage{
  159. Host: proto.String("hey"),
  160. Connected: proto.Bool(true),
  161. Port: proto.Int32(9099),
  162. },
  163. Pet: []string{"bunny", "kitty", "horsey"},
  164. Others: []*pb.OtherMessage{
  165. {
  166. Key: proto.Int64(31415926535),
  167. },
  168. {},
  169. {
  170. Value: []byte("some bytes"),
  171. },
  172. },
  173. },
  174. },
  175. {
  176. src: &pb.MyMessage{
  177. RepBytes: [][]byte{[]byte("wow")},
  178. },
  179. dst: &pb.MyMessage{
  180. Somegroup: &pb.MyMessage_SomeGroup{
  181. GroupField: proto.Int32(6),
  182. },
  183. RepBytes: [][]byte{[]byte("sham")},
  184. },
  185. want: &pb.MyMessage{
  186. Somegroup: &pb.MyMessage_SomeGroup{
  187. GroupField: proto.Int32(6),
  188. },
  189. RepBytes: [][]byte{[]byte("sham"), []byte("wow")},
  190. },
  191. },
  192. // Check that a scalar bytes field replaces rather than appends.
  193. {
  194. src: &pb.OtherMessage{Value: []byte("foo")},
  195. dst: &pb.OtherMessage{Value: []byte("bar")},
  196. want: &pb.OtherMessage{Value: []byte("foo")},
  197. },
  198. {
  199. src: &pb.MessageWithMap{
  200. NameMapping: map[int32]string{6: "Nigel"},
  201. MsgMapping: map[int64]*pb.FloatingPoint{
  202. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  203. 0x4002: &pb.FloatingPoint{
  204. F: proto.Float64(2.0),
  205. },
  206. },
  207. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  208. },
  209. dst: &pb.MessageWithMap{
  210. NameMapping: map[int32]string{
  211. 6: "Bruce", // should be overwritten
  212. 7: "Andrew",
  213. },
  214. MsgMapping: map[int64]*pb.FloatingPoint{
  215. 0x4002: &pb.FloatingPoint{
  216. F: proto.Float64(3.0),
  217. Exact: proto.Bool(true),
  218. }, // the entire message should be overwritten
  219. },
  220. },
  221. want: &pb.MessageWithMap{
  222. NameMapping: map[int32]string{
  223. 6: "Nigel",
  224. 7: "Andrew",
  225. },
  226. MsgMapping: map[int64]*pb.FloatingPoint{
  227. 0x4001: &pb.FloatingPoint{F: proto.Float64(2.0)},
  228. 0x4002: &pb.FloatingPoint{
  229. F: proto.Float64(2.0),
  230. },
  231. },
  232. ByteMapping: map[bool][]byte{true: []byte("wowsa")},
  233. },
  234. },
  235. // proto3 shouldn't merge zero values,
  236. // in the same way that proto2 shouldn't merge nils.
  237. {
  238. src: &proto3pb.Message{
  239. Name: "Aaron",
  240. Data: []byte(""), // zero value, but not nil
  241. },
  242. dst: &proto3pb.Message{
  243. HeightInCm: 176,
  244. Data: []byte("texas!"),
  245. },
  246. want: &proto3pb.Message{
  247. Name: "Aaron",
  248. HeightInCm: 176,
  249. Data: []byte("texas!"),
  250. },
  251. },
  252. { // Oneof fields should merge by assignment.
  253. src: &pb.Communique{Union: &pb.Communique_Number{41}},
  254. dst: &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  255. want: &pb.Communique{Union: &pb.Communique_Number{41}},
  256. },
  257. { // Oneof nil is the same as not set.
  258. src: &pb.Communique{},
  259. dst: &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  260. want: &pb.Communique{Union: &pb.Communique_Name{"Bobby Tables"}},
  261. },
  262. {
  263. src: &pb.Communique{Union: &pb.Communique_Number{1337}},
  264. dst: &pb.Communique{},
  265. want: &pb.Communique{Union: &pb.Communique_Number{1337}},
  266. },
  267. {
  268. src: &pb.Communique{Union: &pb.Communique_Col{pb.MyMessage_RED}},
  269. dst: &pb.Communique{},
  270. want: &pb.Communique{Union: &pb.Communique_Col{pb.MyMessage_RED}},
  271. },
  272. {
  273. src: &pb.Communique{Union: &pb.Communique_Data{[]byte("hello")}},
  274. dst: &pb.Communique{},
  275. want: &pb.Communique{Union: &pb.Communique_Data{[]byte("hello")}},
  276. },
  277. {
  278. src: &pb.Communique{Union: &pb.Communique_Msg{&pb.Strings{BytesField: []byte{1, 2, 3}}}},
  279. dst: &pb.Communique{},
  280. want: &pb.Communique{Union: &pb.Communique_Msg{&pb.Strings{BytesField: []byte{1, 2, 3}}}},
  281. },
  282. {
  283. src: &pb.Communique{Union: &pb.Communique_Msg{}},
  284. dst: &pb.Communique{},
  285. want: &pb.Communique{Union: &pb.Communique_Msg{}},
  286. },
  287. {
  288. src: &pb.Communique{Union: &pb.Communique_Msg{&pb.Strings{StringField: proto.String("123")}}},
  289. dst: &pb.Communique{Union: &pb.Communique_Msg{&pb.Strings{BytesField: []byte{1, 2, 3}}}},
  290. want: &pb.Communique{Union: &pb.Communique_Msg{&pb.Strings{StringField: proto.String("123"), BytesField: []byte{1, 2, 3}}}},
  291. },
  292. {
  293. src: &proto3pb.Message{
  294. Terrain: map[string]*proto3pb.Nested{
  295. "kay_a": &proto3pb.Nested{Cute: true}, // replace
  296. "kay_b": &proto3pb.Nested{Bunny: "rabbit"}, // insert
  297. },
  298. },
  299. dst: &proto3pb.Message{
  300. Terrain: map[string]*proto3pb.Nested{
  301. "kay_a": &proto3pb.Nested{Bunny: "lost"}, // replaced
  302. "kay_c": &proto3pb.Nested{Bunny: "bunny"}, // keep
  303. },
  304. },
  305. want: &proto3pb.Message{
  306. Terrain: map[string]*proto3pb.Nested{
  307. "kay_a": &proto3pb.Nested{Cute: true},
  308. "kay_b": &proto3pb.Nested{Bunny: "rabbit"},
  309. "kay_c": &proto3pb.Nested{Bunny: "bunny"},
  310. },
  311. },
  312. },
  313. {
  314. src: &pb.GoTest{
  315. F_BoolRepeated: []bool{},
  316. F_Int32Repeated: []int32{},
  317. F_Int64Repeated: []int64{},
  318. F_Uint32Repeated: []uint32{},
  319. F_Uint64Repeated: []uint64{},
  320. F_FloatRepeated: []float32{},
  321. F_DoubleRepeated: []float64{},
  322. F_StringRepeated: []string{},
  323. F_BytesRepeated: [][]byte{},
  324. },
  325. dst: &pb.GoTest{},
  326. want: &pb.GoTest{
  327. F_BoolRepeated: []bool{},
  328. F_Int32Repeated: []int32{},
  329. F_Int64Repeated: []int64{},
  330. F_Uint32Repeated: []uint32{},
  331. F_Uint64Repeated: []uint64{},
  332. F_FloatRepeated: []float32{},
  333. F_DoubleRepeated: []float64{},
  334. F_StringRepeated: []string{},
  335. F_BytesRepeated: [][]byte{},
  336. },
  337. },
  338. {
  339. src: &pb.GoTest{},
  340. dst: &pb.GoTest{
  341. F_BoolRepeated: []bool{},
  342. F_Int32Repeated: []int32{},
  343. F_Int64Repeated: []int64{},
  344. F_Uint32Repeated: []uint32{},
  345. F_Uint64Repeated: []uint64{},
  346. F_FloatRepeated: []float32{},
  347. F_DoubleRepeated: []float64{},
  348. F_StringRepeated: []string{},
  349. F_BytesRepeated: [][]byte{},
  350. },
  351. want: &pb.GoTest{
  352. F_BoolRepeated: []bool{},
  353. F_Int32Repeated: []int32{},
  354. F_Int64Repeated: []int64{},
  355. F_Uint32Repeated: []uint32{},
  356. F_Uint64Repeated: []uint64{},
  357. F_FloatRepeated: []float32{},
  358. F_DoubleRepeated: []float64{},
  359. F_StringRepeated: []string{},
  360. F_BytesRepeated: [][]byte{},
  361. },
  362. },
  363. {
  364. src: &pb.GoTest{
  365. F_BytesRepeated: [][]byte{nil, []byte{}, []byte{0}},
  366. },
  367. dst: &pb.GoTest{},
  368. want: &pb.GoTest{
  369. F_BytesRepeated: [][]byte{nil, []byte{}, []byte{0}},
  370. },
  371. },
  372. {
  373. src: &pb.MyMessage{
  374. Others: []*pb.OtherMessage{},
  375. },
  376. dst: &pb.MyMessage{},
  377. want: &pb.MyMessage{
  378. Others: []*pb.OtherMessage{},
  379. },
  380. },
  381. }
  382. func TestMerge(t *testing.T) {
  383. for _, m := range mergeTests {
  384. got := proto.Clone(m.dst)
  385. if !proto.Equal(got, m.dst) {
  386. t.Errorf("Clone()\ngot %v\nwant %v", got, m.dst)
  387. continue
  388. }
  389. proto.Merge(got, m.src)
  390. if !proto.Equal(got, m.want) {
  391. t.Errorf("Merge(%v, %v)\ngot %v\nwant %v", m.dst, m.src, got, m.want)
  392. }
  393. }
  394. }