clone_test.go 11 KB

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