decode_test.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318
  1. // Copyright 2018 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 textpb_test
  5. import (
  6. "math"
  7. "testing"
  8. protoV1 "github.com/golang/protobuf/proto"
  9. "github.com/golang/protobuf/protoapi"
  10. anypb "github.com/golang/protobuf/ptypes/any"
  11. "github.com/golang/protobuf/v2/encoding/textpb"
  12. "github.com/golang/protobuf/v2/internal/legacy"
  13. "github.com/golang/protobuf/v2/internal/scalar"
  14. "github.com/golang/protobuf/v2/proto"
  15. preg "github.com/golang/protobuf/v2/reflect/protoregistry"
  16. // The legacy package must be imported prior to use of any legacy messages.
  17. // TODO: Remove this when protoV1 registers these hooks for you.
  18. _ "github.com/golang/protobuf/v2/internal/legacy"
  19. "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb2"
  20. "github.com/golang/protobuf/v2/encoding/textpb/testprotos/pb3"
  21. )
  22. func init() {
  23. registerExtension(pb2.E_OptExtBool)
  24. registerExtension(pb2.E_OptExtString)
  25. registerExtension(pb2.E_OptExtEnum)
  26. registerExtension(pb2.E_OptExtNested)
  27. registerExtension(pb2.E_RptExtFixed32)
  28. registerExtension(pb2.E_RptExtEnum)
  29. registerExtension(pb2.E_RptExtNested)
  30. registerExtension(pb2.E_ExtensionsContainer_OptExtBool)
  31. registerExtension(pb2.E_ExtensionsContainer_OptExtString)
  32. registerExtension(pb2.E_ExtensionsContainer_OptExtEnum)
  33. registerExtension(pb2.E_ExtensionsContainer_OptExtNested)
  34. registerExtension(pb2.E_ExtensionsContainer_RptExtString)
  35. registerExtension(pb2.E_ExtensionsContainer_RptExtEnum)
  36. registerExtension(pb2.E_ExtensionsContainer_RptExtNested)
  37. }
  38. func registerExtension(xd *protoapi.ExtensionDesc) {
  39. xt := legacy.Export{}.ExtensionTypeFromDesc(xd)
  40. preg.GlobalTypes.Register(xt)
  41. }
  42. func TestUnmarshal(t *testing.T) {
  43. tests := []struct {
  44. desc string
  45. umo textpb.UnmarshalOptions
  46. inputMessage proto.Message
  47. inputText string
  48. wantMessage proto.Message
  49. wantErr bool
  50. }{{
  51. desc: "proto2 empty message",
  52. inputMessage: &pb2.Scalars{},
  53. wantMessage: &pb2.Scalars{},
  54. }, {
  55. desc: "proto2 optional scalar fields set to zero values",
  56. inputMessage: &pb2.Scalars{},
  57. inputText: `opt_bool: false
  58. opt_int32: 0
  59. opt_int64: 0
  60. opt_uint32: 0
  61. opt_uint64: 0
  62. opt_sint32: 0
  63. opt_sint64: 0
  64. opt_fixed32: 0
  65. opt_fixed64: 0
  66. opt_sfixed32: 0
  67. opt_sfixed64: 0
  68. opt_float: 0
  69. opt_double: 0
  70. opt_bytes: ""
  71. opt_string: ""
  72. `,
  73. wantMessage: &pb2.Scalars{
  74. OptBool: scalar.Bool(false),
  75. OptInt32: scalar.Int32(0),
  76. OptInt64: scalar.Int64(0),
  77. OptUint32: scalar.Uint32(0),
  78. OptUint64: scalar.Uint64(0),
  79. OptSint32: scalar.Int32(0),
  80. OptSint64: scalar.Int64(0),
  81. OptFixed32: scalar.Uint32(0),
  82. OptFixed64: scalar.Uint64(0),
  83. OptSfixed32: scalar.Int32(0),
  84. OptSfixed64: scalar.Int64(0),
  85. OptFloat: scalar.Float32(0),
  86. OptDouble: scalar.Float64(0),
  87. OptBytes: []byte{},
  88. OptString: scalar.String(""),
  89. },
  90. }, {
  91. desc: "proto3 scalar fields set to zero values",
  92. inputMessage: &pb3.Scalars{},
  93. inputText: `s_bool: false
  94. s_int32: 0
  95. s_int64: 0
  96. s_uint32: 0
  97. s_uint64: 0
  98. s_sint32: 0
  99. s_sint64: 0
  100. s_fixed32: 0
  101. s_fixed64: 0
  102. s_sfixed32: 0
  103. s_sfixed64: 0
  104. s_float: 0
  105. s_double: 0
  106. s_bytes: ""
  107. s_string: ""
  108. `,
  109. wantMessage: &pb3.Scalars{},
  110. }, {
  111. desc: "proto2 optional scalar fields",
  112. inputMessage: &pb2.Scalars{},
  113. inputText: `opt_bool: true
  114. opt_int32: 255
  115. opt_int64: 3735928559
  116. opt_uint32: 0xff
  117. opt_uint64: 0xdeadbeef
  118. opt_sint32: -1001
  119. opt_sint64: -0xffff
  120. opt_fixed64: 64
  121. opt_sfixed32: -32
  122. opt_float: 1.234
  123. opt_double: 1.23e+100
  124. opt_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
  125. opt_string: "谷歌"
  126. `,
  127. wantMessage: &pb2.Scalars{
  128. OptBool: scalar.Bool(true),
  129. OptInt32: scalar.Int32(0xff),
  130. OptInt64: scalar.Int64(0xdeadbeef),
  131. OptUint32: scalar.Uint32(0xff),
  132. OptUint64: scalar.Uint64(0xdeadbeef),
  133. OptSint32: scalar.Int32(-1001),
  134. OptSint64: scalar.Int64(-0xffff),
  135. OptFixed64: scalar.Uint64(64),
  136. OptSfixed32: scalar.Int32(-32),
  137. OptFloat: scalar.Float32(1.234),
  138. OptDouble: scalar.Float64(1.23e100),
  139. OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  140. OptString: scalar.String("谷歌"),
  141. },
  142. }, {
  143. desc: "proto3 scalar fields",
  144. inputMessage: &pb3.Scalars{},
  145. inputText: `s_bool: true
  146. s_int32: 255
  147. s_int64: 3735928559
  148. s_uint32: 0xff
  149. s_uint64: 0xdeadbeef
  150. s_sint32: -1001
  151. s_sint64: -0xffff
  152. s_fixed64: 64
  153. s_sfixed32: -32
  154. s_float: 1.234
  155. s_double: 1.23e+100
  156. s_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
  157. s_string: "谷歌"
  158. `,
  159. wantMessage: &pb3.Scalars{
  160. SBool: true,
  161. SInt32: 0xff,
  162. SInt64: 0xdeadbeef,
  163. SUint32: 0xff,
  164. SUint64: 0xdeadbeef,
  165. SSint32: -1001,
  166. SSint64: -0xffff,
  167. SFixed64: 64,
  168. SSfixed32: -32,
  169. SFloat: 1.234,
  170. SDouble: 1.23e100,
  171. SBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  172. SString: "谷歌",
  173. },
  174. }, {
  175. desc: "proto2 message contains unknown field",
  176. inputMessage: &pb2.Scalars{},
  177. inputText: "unknown_field: 123",
  178. wantErr: true,
  179. }, {
  180. desc: "proto3 message contains unknown field",
  181. inputMessage: &pb3.Scalars{},
  182. inputText: "unknown_field: 456",
  183. wantErr: true,
  184. }, {
  185. desc: "proto2 numeric key field",
  186. inputMessage: &pb2.Scalars{},
  187. inputText: "1: true",
  188. wantErr: true,
  189. }, {
  190. desc: "proto3 numeric key field",
  191. inputMessage: &pb3.Scalars{},
  192. inputText: "1: true",
  193. wantErr: true,
  194. }, {
  195. desc: "invalid bool value",
  196. inputMessage: &pb3.Scalars{},
  197. inputText: "s_bool: 123",
  198. wantErr: true,
  199. }, {
  200. desc: "invalid int32 value",
  201. inputMessage: &pb3.Scalars{},
  202. inputText: "s_int32: not_a_num",
  203. wantErr: true,
  204. }, {
  205. desc: "invalid int64 value",
  206. inputMessage: &pb3.Scalars{},
  207. inputText: "s_int64: 'not a num either'",
  208. wantErr: true,
  209. }, {
  210. desc: "invalid uint32 value",
  211. inputMessage: &pb3.Scalars{},
  212. inputText: "s_fixed32: -42",
  213. wantErr: true,
  214. }, {
  215. desc: "invalid uint64 value",
  216. inputMessage: &pb3.Scalars{},
  217. inputText: "s_uint64: -47",
  218. wantErr: true,
  219. }, {
  220. desc: "invalid sint32 value",
  221. inputMessage: &pb3.Scalars{},
  222. inputText: "s_sint32: '42'",
  223. wantErr: true,
  224. }, {
  225. desc: "invalid sint64 value",
  226. inputMessage: &pb3.Scalars{},
  227. inputText: "s_sint64: '-47'",
  228. wantErr: true,
  229. }, {
  230. desc: "invalid fixed32 value",
  231. inputMessage: &pb3.Scalars{},
  232. inputText: "s_fixed32: -42",
  233. wantErr: true,
  234. }, {
  235. desc: "invalid fixed64 value",
  236. inputMessage: &pb3.Scalars{},
  237. inputText: "s_fixed64: -42",
  238. wantErr: true,
  239. }, {
  240. desc: "invalid sfixed32 value",
  241. inputMessage: &pb3.Scalars{},
  242. inputText: "s_sfixed32: 'not valid'",
  243. wantErr: true,
  244. }, {
  245. desc: "invalid sfixed64 value",
  246. inputMessage: &pb3.Scalars{},
  247. inputText: "s_sfixed64: bad",
  248. wantErr: true,
  249. }, {
  250. desc: "float32 positive infinity",
  251. inputMessage: &pb3.Scalars{},
  252. inputText: "s_float: inf",
  253. wantMessage: &pb3.Scalars{
  254. SFloat: float32(math.Inf(1)),
  255. },
  256. }, {
  257. desc: "float32 negative infinity",
  258. inputMessage: &pb3.Scalars{},
  259. inputText: "s_float: -inf",
  260. wantMessage: &pb3.Scalars{
  261. SFloat: float32(math.Inf(-1)),
  262. },
  263. }, {
  264. desc: "float64 positive infinity",
  265. inputMessage: &pb3.Scalars{},
  266. inputText: "s_double: inf",
  267. wantMessage: &pb3.Scalars{
  268. SDouble: math.Inf(1),
  269. },
  270. }, {
  271. desc: "float64 negative infinity",
  272. inputMessage: &pb3.Scalars{},
  273. inputText: "s_double: -inf",
  274. wantMessage: &pb3.Scalars{
  275. SDouble: math.Inf(-1),
  276. },
  277. }, {
  278. desc: "invalid string value",
  279. inputMessage: &pb3.Scalars{},
  280. inputText: "s_string: invalid_string",
  281. wantErr: true,
  282. }, {
  283. desc: "proto2 bytes set to empty string",
  284. inputMessage: &pb2.Scalars{},
  285. inputText: "opt_bytes: ''",
  286. wantMessage: &pb2.Scalars{
  287. OptBytes: []byte(""),
  288. },
  289. }, {
  290. desc: "proto3 bytes set to empty string",
  291. inputMessage: &pb3.Scalars{},
  292. inputText: "s_bytes: ''",
  293. wantMessage: &pb3.Scalars{},
  294. }, {
  295. desc: "proto2 duplicate singular field",
  296. inputMessage: &pb2.Scalars{},
  297. inputText: `
  298. opt_bool: true
  299. opt_bool: false
  300. `,
  301. wantErr: true,
  302. }, {
  303. desc: "proto2 invalid singular field",
  304. inputMessage: &pb2.Scalars{},
  305. inputText: `
  306. opt_bool: [true, false]
  307. `,
  308. wantErr: true,
  309. }, {
  310. desc: "proto2 more duplicate singular field",
  311. inputMessage: &pb2.Scalars{},
  312. inputText: `
  313. opt_bool: true
  314. opt_string: "hello"
  315. opt_bool: false
  316. `,
  317. wantErr: true,
  318. }, {
  319. desc: "proto3 duplicate singular field",
  320. inputMessage: &pb3.Scalars{},
  321. inputText: `
  322. s_bool: false
  323. s_bool: true
  324. `,
  325. wantErr: true,
  326. }, {
  327. desc: "proto3 more duplicate singular field",
  328. inputMessage: &pb3.Scalars{},
  329. inputText: `
  330. s_bool: false
  331. s_string: ""
  332. s_bool: true
  333. `,
  334. wantErr: true,
  335. }, {
  336. desc: "proto2 enum",
  337. inputMessage: &pb2.Enums{},
  338. inputText: `
  339. opt_enum: FIRST
  340. opt_nested_enum: UNO
  341. `,
  342. wantMessage: &pb2.Enums{
  343. OptEnum: pb2.Enum_FIRST.Enum(),
  344. OptNestedEnum: pb2.Enums_UNO.Enum(),
  345. },
  346. }, {
  347. desc: "proto2 enum set to numeric values",
  348. inputMessage: &pb2.Enums{},
  349. inputText: `
  350. opt_enum: 1
  351. opt_nested_enum: 2
  352. `,
  353. wantMessage: &pb2.Enums{
  354. OptEnum: pb2.Enum_FIRST.Enum(),
  355. OptNestedEnum: pb2.Enums_DOS.Enum(),
  356. },
  357. }, {
  358. desc: "proto2 enum set to unnamed numeric values",
  359. inputMessage: &pb2.Enums{},
  360. inputText: `
  361. opt_enum: 101
  362. opt_nested_enum: -101
  363. `,
  364. wantMessage: &pb2.Enums{
  365. OptEnum: pb2Enum(101),
  366. OptNestedEnum: pb2Enums_NestedEnum(-101),
  367. },
  368. }, {
  369. desc: "proto2 enum set to invalid named",
  370. inputMessage: &pb2.Enums{},
  371. inputText: `
  372. opt_enum: UNNAMED
  373. opt_nested_enum: UNNAMED_TOO
  374. `,
  375. wantErr: true,
  376. }, {
  377. desc: "proto3 enum name value",
  378. inputMessage: &pb3.Enums{},
  379. inputText: `
  380. s_enum: ONE
  381. s_nested_enum: DIEZ
  382. `,
  383. wantMessage: &pb3.Enums{
  384. SEnum: pb3.Enum_ONE,
  385. SNestedEnum: pb3.Enums_DIEZ,
  386. },
  387. }, {
  388. desc: "proto3 enum numeric value",
  389. inputMessage: &pb3.Enums{},
  390. inputText: `
  391. s_enum: 2
  392. s_nested_enum: 1
  393. `,
  394. wantMessage: &pb3.Enums{
  395. SEnum: pb3.Enum_TWO,
  396. SNestedEnum: pb3.Enums_UNO,
  397. },
  398. }, {
  399. desc: "proto3 enum unnamed numeric value",
  400. inputMessage: &pb3.Enums{},
  401. inputText: `
  402. s_enum: 0x7fffffff
  403. s_nested_enum: -0x80000000
  404. `,
  405. wantMessage: &pb3.Enums{
  406. SEnum: 0x7fffffff,
  407. SNestedEnum: -0x80000000,
  408. },
  409. }, {
  410. desc: "proto2 nested empty messages",
  411. inputMessage: &pb2.Nests{},
  412. inputText: `
  413. opt_nested: {}
  414. OptGroup: {}
  415. `,
  416. wantMessage: &pb2.Nests{
  417. OptNested: &pb2.Nested{},
  418. Optgroup: &pb2.Nests_OptGroup{},
  419. },
  420. }, {
  421. desc: "proto2 nested messages",
  422. inputMessage: &pb2.Nests{},
  423. inputText: `
  424. opt_nested: {
  425. opt_string: "nested message"
  426. opt_nested: {
  427. opt_string: "another nested message"
  428. }
  429. }
  430. `,
  431. wantMessage: &pb2.Nests{
  432. OptNested: &pb2.Nested{
  433. OptString: scalar.String("nested message"),
  434. OptNested: &pb2.Nested{
  435. OptString: scalar.String("another nested message"),
  436. },
  437. },
  438. },
  439. }, {
  440. desc: "proto3 nested empty message",
  441. inputMessage: &pb3.Nests{},
  442. inputText: "s_nested: {}",
  443. wantMessage: &pb3.Nests{
  444. SNested: &pb3.Nested{},
  445. },
  446. }, {
  447. desc: "proto3 nested message",
  448. inputMessage: &pb3.Nests{},
  449. inputText: `
  450. s_nested: {
  451. s_string: "nested message"
  452. s_nested: {
  453. s_string: "another nested message"
  454. }
  455. }
  456. `,
  457. wantMessage: &pb3.Nests{
  458. SNested: &pb3.Nested{
  459. SString: "nested message",
  460. SNested: &pb3.Nested{
  461. SString: "another nested message",
  462. },
  463. },
  464. },
  465. }, {
  466. desc: "oneof field set to empty string",
  467. inputMessage: &pb2.Oneofs{},
  468. inputText: "str: ''",
  469. wantMessage: &pb2.Oneofs{
  470. Union: &pb2.Oneofs_Str{},
  471. },
  472. }, {
  473. desc: "oneof field set to string",
  474. inputMessage: &pb2.Oneofs{},
  475. inputText: "str: 'hello'",
  476. wantMessage: &pb2.Oneofs{
  477. Union: &pb2.Oneofs_Str{
  478. Str: "hello",
  479. },
  480. },
  481. }, {
  482. desc: "oneof field set to empty message",
  483. inputMessage: &pb2.Oneofs{},
  484. inputText: "msg: {}",
  485. wantMessage: &pb2.Oneofs{
  486. Union: &pb2.Oneofs_Msg{
  487. Msg: &pb2.Nested{},
  488. },
  489. },
  490. }, {
  491. desc: "oneof field set to message",
  492. inputMessage: &pb2.Oneofs{},
  493. inputText: `
  494. msg: {
  495. opt_string: "nested message"
  496. }
  497. `,
  498. wantMessage: &pb2.Oneofs{
  499. Union: &pb2.Oneofs_Msg{
  500. Msg: &pb2.Nested{
  501. OptString: scalar.String("nested message"),
  502. },
  503. },
  504. },
  505. }, {
  506. desc: "repeated scalar using same field name",
  507. inputMessage: &pb2.Repeats{},
  508. inputText: `
  509. rpt_string: "a"
  510. rpt_string: "b"
  511. rpt_int32: 0xff
  512. rpt_float: 1.23
  513. rpt_bytes: "bytes"
  514. `,
  515. wantMessage: &pb2.Repeats{
  516. RptString: []string{"a", "b"},
  517. RptInt32: []int32{0xff},
  518. RptFloat: []float32{1.23},
  519. RptBytes: [][]byte{[]byte("bytes")},
  520. },
  521. }, {
  522. desc: "repeated using mix of [] and repeated field name",
  523. inputMessage: &pb2.Repeats{},
  524. inputText: `
  525. rpt_string: "a"
  526. rpt_bool: true
  527. rpt_string: ["x", "y"]
  528. rpt_bool: [ false, true ]
  529. rpt_string: "b"
  530. `,
  531. wantMessage: &pb2.Repeats{
  532. RptString: []string{"a", "x", "y", "b"},
  533. RptBool: []bool{true, false, true},
  534. },
  535. }, {
  536. desc: "repeated enums",
  537. inputMessage: &pb2.Enums{},
  538. inputText: `
  539. rpt_enum: TENTH
  540. rpt_enum: 1
  541. rpt_nested_enum: [DOS, 2]
  542. rpt_enum: 42
  543. rpt_nested_enum: -47
  544. `,
  545. wantMessage: &pb2.Enums{
  546. RptEnum: []pb2.Enum{pb2.Enum_TENTH, pb2.Enum_FIRST, 42},
  547. RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47},
  548. },
  549. }, {
  550. desc: "repeated nested messages",
  551. inputMessage: &pb2.Nests{},
  552. inputText: `
  553. rpt_nested: {
  554. opt_string: "repeat nested one"
  555. }
  556. rpt_nested: {
  557. opt_string: "repeat nested two"
  558. opt_nested: {
  559. opt_string: "inside repeat nested two"
  560. }
  561. }
  562. rpt_nested: {}
  563. `,
  564. wantMessage: &pb2.Nests{
  565. RptNested: []*pb2.Nested{
  566. {
  567. OptString: scalar.String("repeat nested one"),
  568. },
  569. {
  570. OptString: scalar.String("repeat nested two"),
  571. OptNested: &pb2.Nested{
  572. OptString: scalar.String("inside repeat nested two"),
  573. },
  574. },
  575. {},
  576. },
  577. },
  578. }, {
  579. desc: "repeated group fields",
  580. inputMessage: &pb2.Nests{},
  581. inputText: `
  582. RptGroup: {
  583. rpt_bool: true
  584. rpt_bool: false
  585. }
  586. RptGroup: {}
  587. `,
  588. wantMessage: &pb2.Nests{
  589. Rptgroup: []*pb2.Nests_RptGroup{
  590. {
  591. RptBool: []bool{true, false},
  592. },
  593. {},
  594. },
  595. },
  596. }, {
  597. desc: "map fields 1",
  598. inputMessage: &pb2.Maps{},
  599. inputText: `
  600. int32_to_str: {
  601. key: -101
  602. value: "-101"
  603. }
  604. int32_to_str: {
  605. key: 0
  606. value: "zero"
  607. }
  608. sfixed64_to_bool: {
  609. key: 0
  610. value: false
  611. }
  612. int32_to_str: {
  613. key: 255
  614. value: "0xff"
  615. }
  616. bool_to_uint32: {
  617. key: false
  618. value: 101
  619. }
  620. sfixed64_to_bool: {
  621. key: 51966
  622. value: true
  623. }
  624. bool_to_uint32: {
  625. key: true
  626. value: 42
  627. }
  628. `,
  629. wantMessage: &pb2.Maps{
  630. Int32ToStr: map[int32]string{
  631. -101: "-101",
  632. 0xff: "0xff",
  633. 0: "zero",
  634. },
  635. Sfixed64ToBool: map[int64]bool{
  636. 0xcafe: true,
  637. 0: false,
  638. },
  639. BoolToUint32: map[bool]uint32{
  640. true: 42,
  641. false: 101,
  642. },
  643. },
  644. }, {
  645. desc: "map fields 2",
  646. inputMessage: &pb2.Maps{},
  647. inputText: `
  648. uint64_to_enum: {
  649. key: 1
  650. value: FIRST
  651. }
  652. uint64_to_enum: {
  653. key: 2
  654. value: SECOND
  655. }
  656. uint64_to_enum: {
  657. key: 10
  658. value: TENTH
  659. }
  660. `,
  661. wantMessage: &pb2.Maps{
  662. Uint64ToEnum: map[uint64]pb2.Enum{
  663. 1: pb2.Enum_FIRST,
  664. 2: pb2.Enum_SECOND,
  665. 10: pb2.Enum_TENTH,
  666. },
  667. },
  668. }, {
  669. desc: "map fields 3",
  670. inputMessage: &pb2.Maps{},
  671. inputText: `
  672. str_to_nested: {
  673. key: "nested_one"
  674. value: {
  675. opt_string: "nested in a map"
  676. }
  677. }
  678. `,
  679. wantMessage: &pb2.Maps{
  680. StrToNested: map[string]*pb2.Nested{
  681. "nested_one": &pb2.Nested{
  682. OptString: scalar.String("nested in a map"),
  683. },
  684. },
  685. },
  686. }, {
  687. desc: "map fields 4",
  688. inputMessage: &pb2.Maps{},
  689. inputText: `
  690. str_to_oneofs: {
  691. key: "nested"
  692. value: {
  693. msg: {
  694. opt_string: "nested oneof in map field value"
  695. }
  696. }
  697. }
  698. str_to_oneofs: {
  699. key: "string"
  700. value: {
  701. str: "hello"
  702. }
  703. }
  704. `,
  705. wantMessage: &pb2.Maps{
  706. StrToOneofs: map[string]*pb2.Oneofs{
  707. "string": &pb2.Oneofs{
  708. Union: &pb2.Oneofs_Str{
  709. Str: "hello",
  710. },
  711. },
  712. "nested": &pb2.Oneofs{
  713. Union: &pb2.Oneofs_Msg{
  714. Msg: &pb2.Nested{
  715. OptString: scalar.String("nested oneof in map field value"),
  716. },
  717. },
  718. },
  719. },
  720. },
  721. }, {
  722. desc: "map contains duplicate keys",
  723. inputMessage: &pb2.Maps{},
  724. inputText: `
  725. int32_to_str: {
  726. key: 0
  727. value: "cero"
  728. }
  729. int32_to_str: {
  730. key: 0
  731. value: "zero"
  732. }
  733. `,
  734. wantMessage: &pb2.Maps{
  735. Int32ToStr: map[int32]string{
  736. 0: "zero",
  737. },
  738. },
  739. }, {
  740. desc: "map contains duplicate key fields",
  741. inputMessage: &pb2.Maps{},
  742. inputText: `
  743. int32_to_str: {
  744. key: 0
  745. key: 1
  746. value: "cero"
  747. }
  748. `,
  749. wantErr: true,
  750. }, {
  751. desc: "map contains duplicate value fields",
  752. inputMessage: &pb2.Maps{},
  753. inputText: `
  754. int32_to_str: {
  755. key: 1
  756. value: "cero"
  757. value: "uno"
  758. }
  759. `,
  760. wantErr: true,
  761. }, {
  762. desc: "map contains missing key",
  763. inputMessage: &pb2.Maps{},
  764. inputText: `
  765. int32_to_str: {
  766. value: "zero"
  767. }
  768. `,
  769. wantMessage: &pb2.Maps{
  770. Int32ToStr: map[int32]string{
  771. 0: "zero",
  772. },
  773. },
  774. }, {
  775. desc: "map contains missing value",
  776. inputMessage: &pb2.Maps{},
  777. inputText: `
  778. int32_to_str: {
  779. key: 100
  780. }
  781. `,
  782. wantMessage: &pb2.Maps{
  783. Int32ToStr: map[int32]string{
  784. 100: "",
  785. },
  786. },
  787. }, {
  788. desc: "map contains missing key and value",
  789. inputMessage: &pb2.Maps{},
  790. inputText: `
  791. int32_to_str: {}
  792. `,
  793. wantMessage: &pb2.Maps{
  794. Int32ToStr: map[int32]string{
  795. 0: "",
  796. },
  797. },
  798. }, {
  799. desc: "map contains unknown field",
  800. inputMessage: &pb2.Maps{},
  801. inputText: `
  802. int32_to_str: {
  803. key: 0
  804. value: "cero"
  805. unknown: "bad"
  806. }
  807. `,
  808. wantErr: true,
  809. }, {
  810. desc: "map contains extension-like key field",
  811. inputMessage: &pb2.Maps{},
  812. inputText: `
  813. int32_to_str: {
  814. [key]: 10
  815. value: "ten"
  816. }
  817. `,
  818. wantErr: true,
  819. }, {
  820. desc: "map contains invalid key",
  821. inputMessage: &pb2.Maps{},
  822. inputText: `
  823. int32_to_str: {
  824. key: "invalid"
  825. value: "cero"
  826. }
  827. `,
  828. wantErr: true,
  829. }, {
  830. desc: "map contains invalid value",
  831. inputMessage: &pb2.Maps{},
  832. inputText: `
  833. int32_to_str: {
  834. key: 100
  835. value: 101
  836. }
  837. `,
  838. wantErr: true,
  839. }, {
  840. desc: "map using mix of [] and repeated",
  841. inputMessage: &pb2.Maps{},
  842. inputText: `
  843. int32_to_str: {
  844. key: 1
  845. value: "one"
  846. }
  847. int32_to_str: [
  848. {
  849. key: 2
  850. value: "not this"
  851. },
  852. {
  853. },
  854. {
  855. key: 3
  856. value: "three"
  857. }
  858. ]
  859. int32_to_str: {
  860. key: 2
  861. value: "two"
  862. }
  863. `,
  864. wantMessage: &pb2.Maps{
  865. Int32ToStr: map[int32]string{
  866. 0: "",
  867. 1: "one",
  868. 2: "two",
  869. 3: "three",
  870. },
  871. },
  872. }, {
  873. desc: "proto2 required fields not set",
  874. inputMessage: &pb2.Requireds{},
  875. wantErr: true,
  876. }, {
  877. desc: "proto2 required field set but not optional",
  878. inputMessage: &pb2.PartialRequired{},
  879. inputText: "req_string: 'this is required'",
  880. wantMessage: &pb2.PartialRequired{
  881. ReqString: scalar.String("this is required"),
  882. },
  883. }, {
  884. desc: "proto2 required fields partially set",
  885. inputMessage: &pb2.Requireds{},
  886. inputText: `
  887. req_bool: false
  888. req_fixed32: 47
  889. req_sfixed64: 3203386110
  890. req_string: "hello"
  891. req_enum: FIRST
  892. `,
  893. wantMessage: &pb2.Requireds{
  894. ReqBool: scalar.Bool(false),
  895. ReqFixed32: scalar.Uint32(47),
  896. ReqSfixed64: scalar.Int64(0xbeefcafe),
  897. ReqString: scalar.String("hello"),
  898. ReqEnum: pb2.Enum_FIRST.Enum(),
  899. },
  900. wantErr: true,
  901. }, {
  902. desc: "proto2 required fields all set",
  903. inputMessage: &pb2.Requireds{},
  904. inputText: `
  905. req_bool: false
  906. req_fixed32: 0
  907. req_fixed64: 0
  908. req_sfixed32: 0
  909. req_sfixed64: 0
  910. req_float: 0
  911. req_double: 0
  912. req_string: ""
  913. req_bytes: ""
  914. req_enum: UNKNOWN
  915. req_nested: {}
  916. `,
  917. wantMessage: &pb2.Requireds{
  918. ReqBool: scalar.Bool(false),
  919. ReqFixed32: scalar.Uint32(0),
  920. ReqFixed64: scalar.Uint64(0),
  921. ReqSfixed32: scalar.Int32(0),
  922. ReqSfixed64: scalar.Int64(0),
  923. ReqFloat: scalar.Float32(0),
  924. ReqDouble: scalar.Float64(0),
  925. ReqString: scalar.String(""),
  926. ReqEnum: pb2.Enum_UNKNOWN.Enum(),
  927. ReqBytes: []byte{},
  928. ReqNested: &pb2.Nested{},
  929. },
  930. }, {
  931. desc: "indirect required field",
  932. inputMessage: &pb2.IndirectRequired{},
  933. inputText: "opt_nested: {}",
  934. wantMessage: &pb2.IndirectRequired{
  935. OptNested: &pb2.NestedWithRequired{},
  936. },
  937. wantErr: true,
  938. }, {
  939. desc: "indirect required field in repeated",
  940. inputMessage: &pb2.IndirectRequired{},
  941. inputText: `
  942. rpt_nested: {
  943. req_string: "one"
  944. }
  945. rpt_nested: {}
  946. rpt_nested: {
  947. req_string: "three"
  948. }
  949. `,
  950. wantMessage: &pb2.IndirectRequired{
  951. RptNested: []*pb2.NestedWithRequired{
  952. {
  953. ReqString: scalar.String("one"),
  954. },
  955. {},
  956. {
  957. ReqString: scalar.String("three"),
  958. },
  959. },
  960. },
  961. wantErr: true,
  962. }, {
  963. desc: "indirect required field in map",
  964. inputMessage: &pb2.IndirectRequired{},
  965. inputText: `
  966. str_to_nested: {
  967. key: "missing"
  968. }
  969. str_to_nested: {
  970. key: "contains"
  971. value: {
  972. req_string: "here"
  973. }
  974. }
  975. `,
  976. wantMessage: &pb2.IndirectRequired{
  977. StrToNested: map[string]*pb2.NestedWithRequired{
  978. "missing": &pb2.NestedWithRequired{},
  979. "contains": &pb2.NestedWithRequired{
  980. ReqString: scalar.String("here"),
  981. },
  982. },
  983. },
  984. wantErr: true,
  985. }, {
  986. desc: "ignore reserved field",
  987. inputMessage: &pb2.Nests{},
  988. inputText: "reserved_field: 'ignore this'",
  989. wantMessage: &pb2.Nests{},
  990. }, {
  991. desc: "extensions of non-repeated fields",
  992. inputMessage: &pb2.Extensions{},
  993. inputText: `opt_string: "non-extension field"
  994. [pb2.opt_ext_bool]: true
  995. opt_bool: true
  996. [pb2.opt_ext_nested]: {
  997. opt_string: "nested in an extension"
  998. opt_nested: {
  999. opt_string: "another nested in an extension"
  1000. }
  1001. }
  1002. [pb2.opt_ext_string]: "extension field"
  1003. opt_int32: 42
  1004. [pb2.opt_ext_enum]: TENTH
  1005. `,
  1006. wantMessage: func() proto.Message {
  1007. m := &pb2.Extensions{
  1008. OptString: scalar.String("non-extension field"),
  1009. OptBool: scalar.Bool(true),
  1010. OptInt32: scalar.Int32(42),
  1011. }
  1012. setExtension(m, pb2.E_OptExtBool, true)
  1013. setExtension(m, pb2.E_OptExtString, "extension field")
  1014. setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TENTH)
  1015. setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  1016. OptString: scalar.String("nested in an extension"),
  1017. OptNested: &pb2.Nested{
  1018. OptString: scalar.String("another nested in an extension"),
  1019. },
  1020. })
  1021. return m
  1022. }(),
  1023. }, {
  1024. desc: "extensions of repeated fields",
  1025. inputMessage: &pb2.Extensions{},
  1026. inputText: `[pb2.rpt_ext_enum]: TENTH
  1027. [pb2.rpt_ext_enum]: 101
  1028. [pb2.rpt_ext_fixed32]: 42
  1029. [pb2.rpt_ext_enum]: FIRST
  1030. [pb2.rpt_ext_nested]: {
  1031. opt_string: "one"
  1032. }
  1033. [pb2.rpt_ext_nested]: {
  1034. opt_string: "two"
  1035. }
  1036. [pb2.rpt_ext_fixed32]: 47
  1037. [pb2.rpt_ext_nested]: {
  1038. opt_string: "three"
  1039. }
  1040. `,
  1041. wantMessage: func() proto.Message {
  1042. m := &pb2.Extensions{}
  1043. setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TENTH, 101, pb2.Enum_FIRST})
  1044. setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  1045. setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  1046. &pb2.Nested{OptString: scalar.String("one")},
  1047. &pb2.Nested{OptString: scalar.String("two")},
  1048. &pb2.Nested{OptString: scalar.String("three")},
  1049. })
  1050. return m
  1051. }(),
  1052. }, {
  1053. desc: "extensions of non-repeated fields in another message",
  1054. inputMessage: &pb2.Extensions{},
  1055. inputText: `[pb2.ExtensionsContainer.opt_ext_bool]: true
  1056. [pb2.ExtensionsContainer.opt_ext_enum]: TENTH
  1057. [pb2.ExtensionsContainer.opt_ext_nested]: {
  1058. opt_string: "nested in an extension"
  1059. opt_nested: {
  1060. opt_string: "another nested in an extension"
  1061. }
  1062. }
  1063. [pb2.ExtensionsContainer.opt_ext_string]: "extension field"
  1064. `,
  1065. wantMessage: func() proto.Message {
  1066. m := &pb2.Extensions{}
  1067. setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  1068. setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  1069. setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TENTH)
  1070. setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  1071. OptString: scalar.String("nested in an extension"),
  1072. OptNested: &pb2.Nested{
  1073. OptString: scalar.String("another nested in an extension"),
  1074. },
  1075. })
  1076. return m
  1077. }(),
  1078. }, {
  1079. desc: "extensions of repeated fields in another message",
  1080. inputMessage: &pb2.Extensions{},
  1081. inputText: `opt_string: "non-extension field"
  1082. opt_bool: true
  1083. opt_int32: 42
  1084. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1085. opt_string: "one"
  1086. }
  1087. [pb2.ExtensionsContainer.rpt_ext_enum]: TENTH
  1088. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1089. opt_string: "two"
  1090. }
  1091. [pb2.ExtensionsContainer.rpt_ext_enum]: 101
  1092. [pb2.ExtensionsContainer.rpt_ext_string]: "hello"
  1093. [pb2.ExtensionsContainer.rpt_ext_enum]: FIRST
  1094. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1095. opt_string: "three"
  1096. }
  1097. [pb2.ExtensionsContainer.rpt_ext_string]: "world"
  1098. `,
  1099. wantMessage: func() proto.Message {
  1100. m := &pb2.Extensions{
  1101. OptString: scalar.String("non-extension field"),
  1102. OptBool: scalar.Bool(true),
  1103. OptInt32: scalar.Int32(42),
  1104. }
  1105. setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TENTH, 101, pb2.Enum_FIRST})
  1106. setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1107. setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1108. &pb2.Nested{OptString: scalar.String("one")},
  1109. &pb2.Nested{OptString: scalar.String("two")},
  1110. &pb2.Nested{OptString: scalar.String("three")},
  1111. })
  1112. return m
  1113. }(),
  1114. }, {
  1115. desc: "invalid extension field name",
  1116. inputMessage: &pb2.Extensions{},
  1117. inputText: "[pb2.invalid_message_field]: true",
  1118. wantErr: true,
  1119. }, {
  1120. desc: "Any not expanded",
  1121. inputMessage: &pb2.KnownTypes{},
  1122. inputText: `opt_any: {
  1123. type_url: "pb2.Nested"
  1124. value: "some bytes"
  1125. }
  1126. `,
  1127. wantMessage: &pb2.KnownTypes{
  1128. OptAny: &anypb.Any{
  1129. TypeUrl: "pb2.Nested",
  1130. Value: []byte("some bytes"),
  1131. },
  1132. },
  1133. }, {
  1134. desc: "Any not expanded missing value",
  1135. inputMessage: &pb2.KnownTypes{},
  1136. inputText: `opt_any: {
  1137. type_url: "pb2.Nested"
  1138. }
  1139. `,
  1140. wantMessage: &pb2.KnownTypes{
  1141. OptAny: &anypb.Any{
  1142. TypeUrl: "pb2.Nested",
  1143. },
  1144. },
  1145. }, {
  1146. desc: "Any not expanded missing type_url",
  1147. inputMessage: &pb2.KnownTypes{},
  1148. inputText: `opt_any: {
  1149. value: "some bytes"
  1150. }
  1151. `,
  1152. wantMessage: &pb2.KnownTypes{
  1153. OptAny: &anypb.Any{
  1154. Value: []byte("some bytes"),
  1155. },
  1156. },
  1157. }, {
  1158. desc: "Any expanded",
  1159. umo: func() textpb.UnmarshalOptions {
  1160. m := &pb2.Nested{}
  1161. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1162. return textpb.UnmarshalOptions{Resolver: resolver}
  1163. }(),
  1164. inputMessage: &pb2.KnownTypes{},
  1165. inputText: `opt_any: {
  1166. [foobar/pb2.Nested]: {
  1167. opt_string: "embedded inside Any"
  1168. opt_nested: {
  1169. opt_string: "inception"
  1170. }
  1171. }
  1172. }
  1173. `,
  1174. wantMessage: func() proto.Message {
  1175. m := &pb2.Nested{
  1176. OptString: scalar.String("embedded inside Any"),
  1177. OptNested: &pb2.Nested{
  1178. OptString: scalar.String("inception"),
  1179. },
  1180. }
  1181. // TODO: Switch to V2 marshal when ready.
  1182. b, err := protoV1.Marshal(m)
  1183. if err != nil {
  1184. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1185. }
  1186. return &pb2.KnownTypes{
  1187. OptAny: &anypb.Any{
  1188. TypeUrl: "foobar/pb2.Nested",
  1189. Value: b,
  1190. },
  1191. }
  1192. }(),
  1193. }, {
  1194. desc: "Any expanded with empty value",
  1195. umo: func() textpb.UnmarshalOptions {
  1196. m := &pb2.Nested{}
  1197. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1198. return textpb.UnmarshalOptions{Resolver: resolver}
  1199. }(),
  1200. inputMessage: &pb2.KnownTypes{},
  1201. inputText: `opt_any: {
  1202. [foo.com/pb2.Nested]: {}
  1203. }
  1204. `,
  1205. wantMessage: &pb2.KnownTypes{
  1206. OptAny: &anypb.Any{
  1207. TypeUrl: "foo.com/pb2.Nested",
  1208. },
  1209. },
  1210. }, {
  1211. desc: "Any expanded with missing required error",
  1212. umo: func() textpb.UnmarshalOptions {
  1213. m := &pb2.PartialRequired{}
  1214. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1215. return textpb.UnmarshalOptions{Resolver: resolver}
  1216. }(),
  1217. inputMessage: &pb2.KnownTypes{},
  1218. inputText: `opt_any: {
  1219. [pb2.PartialRequired]: {
  1220. opt_string: "embedded inside Any"
  1221. }
  1222. }
  1223. `,
  1224. wantMessage: func() proto.Message {
  1225. m := &pb2.PartialRequired{
  1226. OptString: scalar.String("embedded inside Any"),
  1227. }
  1228. // TODO: Switch to V2 marshal when ready.
  1229. b, err := protoV1.Marshal(m)
  1230. // Ignore required not set error.
  1231. if _, ok := err.(*protoV1.RequiredNotSetError); !ok {
  1232. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1233. }
  1234. return &pb2.KnownTypes{
  1235. OptAny: &anypb.Any{
  1236. TypeUrl: "pb2.PartialRequired",
  1237. Value: b,
  1238. },
  1239. }
  1240. }(),
  1241. wantErr: true,
  1242. }, {
  1243. desc: "Any expanded with unregistered type",
  1244. umo: textpb.UnmarshalOptions{Resolver: preg.NewTypes()},
  1245. inputMessage: &pb2.KnownTypes{},
  1246. inputText: `opt_any: {
  1247. [SomeMessage]: {}
  1248. }
  1249. `,
  1250. wantErr: true,
  1251. }, {
  1252. desc: "Any expanded with invalid value",
  1253. umo: func() textpb.UnmarshalOptions {
  1254. m := &pb2.Nested{}
  1255. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1256. return textpb.UnmarshalOptions{Resolver: resolver}
  1257. }(),
  1258. inputMessage: &pb2.KnownTypes{},
  1259. inputText: `opt_any: {
  1260. [pb2.Nested]: 123
  1261. }
  1262. `,
  1263. wantErr: true,
  1264. }, {
  1265. desc: "Any expanded with unknown fields",
  1266. umo: func() textpb.UnmarshalOptions {
  1267. m := &pb2.Nested{}
  1268. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1269. return textpb.UnmarshalOptions{Resolver: resolver}
  1270. }(),
  1271. inputMessage: &pb2.KnownTypes{},
  1272. inputText: `opt_any: {
  1273. [pb2.Nested]: {}
  1274. unknown: ""
  1275. }
  1276. `,
  1277. wantErr: true,
  1278. }, {
  1279. desc: "Any contains expanded and unexpanded fields",
  1280. umo: func() textpb.UnmarshalOptions {
  1281. m := &pb2.Nested{}
  1282. resolver := preg.NewTypes(m.ProtoReflect().Type())
  1283. return textpb.UnmarshalOptions{Resolver: resolver}
  1284. }(),
  1285. inputMessage: &pb2.KnownTypes{},
  1286. inputText: `opt_any: {
  1287. [pb2.Nested]: {}
  1288. type_url: "pb2.Nested"
  1289. }
  1290. `,
  1291. wantErr: true,
  1292. }}
  1293. for _, tt := range tests {
  1294. tt := tt
  1295. t.Run(tt.desc, func(t *testing.T) {
  1296. t.Parallel()
  1297. err := tt.umo.Unmarshal(tt.inputMessage, []byte(tt.inputText))
  1298. if err != nil && !tt.wantErr {
  1299. t.Errorf("Unmarshal() returned error: %v\n\n", err)
  1300. }
  1301. if err == nil && tt.wantErr {
  1302. t.Error("Unmarshal() got nil error, want error\n\n")
  1303. }
  1304. if tt.wantMessage != nil && !protoV1.Equal(tt.inputMessage.(protoV1.Message), tt.wantMessage.(protoV1.Message)) {
  1305. t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
  1306. }
  1307. })
  1308. }
  1309. }