decode_test.go 35 KB

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