decode_test.go 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 prototext_test
  5. import (
  6. "math"
  7. "testing"
  8. "google.golang.org/protobuf/encoding/prototext"
  9. pimpl "google.golang.org/protobuf/internal/impl"
  10. "google.golang.org/protobuf/proto"
  11. preg "google.golang.org/protobuf/reflect/protoregistry"
  12. "google.golang.org/protobuf/encoding/testprotos/pb2"
  13. "google.golang.org/protobuf/encoding/testprotos/pb3"
  14. "google.golang.org/protobuf/types/known/anypb"
  15. )
  16. func TestUnmarshal(t *testing.T) {
  17. tests := []struct {
  18. desc string
  19. umo prototext.UnmarshalOptions
  20. inputMessage proto.Message
  21. inputText string
  22. wantMessage proto.Message
  23. wantErr bool // TODO: Verify error message content.
  24. }{{
  25. desc: "proto2 empty message",
  26. inputMessage: &pb2.Scalars{},
  27. wantMessage: &pb2.Scalars{},
  28. }, {
  29. desc: "proto2 optional scalars set to zero values",
  30. inputMessage: &pb2.Scalars{},
  31. inputText: `opt_bool: false
  32. opt_int32: 0
  33. opt_int64: 0
  34. opt_uint32: 0
  35. opt_uint64: 0
  36. opt_sint32: 0
  37. opt_sint64: 0
  38. opt_fixed32: 0
  39. opt_fixed64: 0
  40. opt_sfixed32: 0
  41. opt_sfixed64: 0
  42. opt_float: 0
  43. opt_double: 0
  44. opt_bytes: ""
  45. opt_string: ""
  46. `,
  47. wantMessage: &pb2.Scalars{
  48. OptBool: proto.Bool(false),
  49. OptInt32: proto.Int32(0),
  50. OptInt64: proto.Int64(0),
  51. OptUint32: proto.Uint32(0),
  52. OptUint64: proto.Uint64(0),
  53. OptSint32: proto.Int32(0),
  54. OptSint64: proto.Int64(0),
  55. OptFixed32: proto.Uint32(0),
  56. OptFixed64: proto.Uint64(0),
  57. OptSfixed32: proto.Int32(0),
  58. OptSfixed64: proto.Int64(0),
  59. OptFloat: proto.Float32(0),
  60. OptDouble: proto.Float64(0),
  61. OptBytes: []byte{},
  62. OptString: proto.String(""),
  63. },
  64. }, {
  65. desc: "proto3 scalars set to zero values",
  66. inputMessage: &pb3.Scalars{},
  67. inputText: `s_bool: false
  68. s_int32: 0
  69. s_int64: 0
  70. s_uint32: 0
  71. s_uint64: 0
  72. s_sint32: 0
  73. s_sint64: 0
  74. s_fixed32: 0
  75. s_fixed64: 0
  76. s_sfixed32: 0
  77. s_sfixed64: 0
  78. s_float: 0
  79. s_double: 0
  80. s_bytes: ""
  81. s_string: ""
  82. `,
  83. wantMessage: &pb3.Scalars{},
  84. }, {
  85. desc: "proto2 optional scalars",
  86. inputMessage: &pb2.Scalars{},
  87. inputText: `opt_bool: true
  88. opt_int32: 255
  89. opt_int64: 3735928559
  90. opt_uint32: 0xff
  91. opt_uint64: 0xdeadbeef
  92. opt_sint32: -1001
  93. opt_sint64: -0xffff
  94. opt_fixed64: 64
  95. opt_sfixed32: -32
  96. opt_float: 1.234
  97. opt_double: 1.23e+100
  98. opt_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
  99. opt_string: "谷歌"
  100. `,
  101. wantMessage: &pb2.Scalars{
  102. OptBool: proto.Bool(true),
  103. OptInt32: proto.Int32(0xff),
  104. OptInt64: proto.Int64(0xdeadbeef),
  105. OptUint32: proto.Uint32(0xff),
  106. OptUint64: proto.Uint64(0xdeadbeef),
  107. OptSint32: proto.Int32(-1001),
  108. OptSint64: proto.Int64(-0xffff),
  109. OptFixed64: proto.Uint64(64),
  110. OptSfixed32: proto.Int32(-32),
  111. OptFloat: proto.Float32(1.234),
  112. OptDouble: proto.Float64(1.23e100),
  113. OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  114. OptString: proto.String("谷歌"),
  115. },
  116. }, {
  117. desc: "case sensitive",
  118. inputMessage: &pb3.Scalars{},
  119. inputText: `S_BOOL: true`,
  120. wantErr: true,
  121. }, {
  122. desc: "proto3 scalars",
  123. inputMessage: &pb3.Scalars{},
  124. inputText: `s_bool: true
  125. s_int32: 255
  126. s_int64: 3735928559
  127. s_uint32: 0xff
  128. s_uint64: 0xdeadbeef
  129. s_sint32: -1001
  130. s_sint64: -0xffff
  131. s_fixed64: 64
  132. s_sfixed32: -32
  133. s_float: 1.234
  134. s_double: 1.23e+100
  135. s_bytes: "\xe8\xb0\xb7\xe6\xad\x8c"
  136. s_string: "谷歌"
  137. `,
  138. wantMessage: &pb3.Scalars{
  139. SBool: true,
  140. SInt32: 0xff,
  141. SInt64: 0xdeadbeef,
  142. SUint32: 0xff,
  143. SUint64: 0xdeadbeef,
  144. SSint32: -1001,
  145. SSint64: -0xffff,
  146. SFixed64: 64,
  147. SSfixed32: -32,
  148. SFloat: 1.234,
  149. SDouble: 1.23e100,
  150. SBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  151. SString: "谷歌",
  152. },
  153. }, {
  154. desc: "string with invalid UTF-8",
  155. inputMessage: &pb3.Scalars{},
  156. inputText: `s_string: "abc\xff"`,
  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: pb2.Enum(101).Enum(),
  350. OptNestedEnum: pb2.Enums_NestedEnum(-101).Enum(),
  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: "group field name",
  406. inputMessage: &pb2.Nests{},
  407. inputText: `optgroup: {}`,
  408. wantErr: true,
  409. }, {
  410. desc: "proto2 nested messages",
  411. inputMessage: &pb2.Nests{},
  412. inputText: `
  413. opt_nested: {
  414. opt_string: "nested message"
  415. opt_nested: {
  416. opt_string: "another nested message"
  417. }
  418. }
  419. `,
  420. wantMessage: &pb2.Nests{
  421. OptNested: &pb2.Nested{
  422. OptString: proto.String("nested message"),
  423. OptNested: &pb2.Nested{
  424. OptString: proto.String("another nested message"),
  425. },
  426. },
  427. },
  428. }, {
  429. desc: "proto3 nested empty message",
  430. inputMessage: &pb3.Nests{},
  431. inputText: "s_nested: {}",
  432. wantMessage: &pb3.Nests{
  433. SNested: &pb3.Nested{},
  434. },
  435. }, {
  436. desc: "proto3 nested message",
  437. inputMessage: &pb3.Nests{},
  438. inputText: `
  439. s_nested: {
  440. s_string: "nested message"
  441. s_nested: {
  442. s_string: "another nested message"
  443. }
  444. }
  445. `,
  446. wantMessage: &pb3.Nests{
  447. SNested: &pb3.Nested{
  448. SString: "nested message",
  449. SNested: &pb3.Nested{
  450. SString: "another nested message",
  451. },
  452. },
  453. },
  454. }, {
  455. desc: "proto3 nested message contains invalid UTF-8",
  456. inputMessage: &pb3.Nests{},
  457. inputText: `s_nested: {
  458. s_string: "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. wantErr: true,
  554. }, {
  555. desc: "repeated enums",
  556. inputMessage: &pb2.Enums{},
  557. inputText: `
  558. rpt_enum: TEN
  559. rpt_enum: 1
  560. rpt_nested_enum: [DOS, 2]
  561. rpt_enum: 42
  562. rpt_nested_enum: -47
  563. `,
  564. wantMessage: &pb2.Enums{
  565. RptEnum: []pb2.Enum{pb2.Enum_TEN, pb2.Enum_ONE, 42},
  566. RptNestedEnum: []pb2.Enums_NestedEnum{pb2.Enums_DOS, pb2.Enums_DOS, -47},
  567. },
  568. }, {
  569. desc: "repeated nested messages",
  570. inputMessage: &pb2.Nests{},
  571. inputText: `
  572. rpt_nested: {
  573. opt_string: "repeat nested one"
  574. }
  575. rpt_nested: {
  576. opt_string: "repeat nested two"
  577. opt_nested: {
  578. opt_string: "inside repeat nested two"
  579. }
  580. }
  581. rpt_nested: {}
  582. `,
  583. wantMessage: &pb2.Nests{
  584. RptNested: []*pb2.Nested{
  585. {
  586. OptString: proto.String("repeat nested one"),
  587. },
  588. {
  589. OptString: proto.String("repeat nested two"),
  590. OptNested: &pb2.Nested{
  591. OptString: proto.String("inside repeat nested two"),
  592. },
  593. },
  594. {},
  595. },
  596. },
  597. }, {
  598. desc: "repeated group fields",
  599. inputMessage: &pb2.Nests{},
  600. inputText: `
  601. RptGroup: {
  602. rpt_string: "hello"
  603. rpt_string: "world"
  604. }
  605. RptGroup: {}
  606. `,
  607. wantMessage: &pb2.Nests{
  608. Rptgroup: []*pb2.Nests_RptGroup{
  609. {
  610. RptString: []string{"hello", "world"},
  611. },
  612. {},
  613. },
  614. },
  615. }, {
  616. desc: "map fields 1",
  617. inputMessage: &pb3.Maps{},
  618. inputText: `
  619. int32_to_str: {
  620. key: -101
  621. value: "-101"
  622. }
  623. int32_to_str: {
  624. key: 0
  625. value: "zero"
  626. }
  627. bool_to_uint32: {
  628. key: false
  629. value: 101
  630. }
  631. int32_to_str: {
  632. key: 255
  633. value: "0xff"
  634. }
  635. bool_to_uint32: {
  636. key: true
  637. value: 42
  638. }
  639. `,
  640. wantMessage: &pb3.Maps{
  641. Int32ToStr: map[int32]string{
  642. -101: "-101",
  643. 0xff: "0xff",
  644. 0: "zero",
  645. },
  646. BoolToUint32: map[bool]uint32{
  647. true: 42,
  648. false: 101,
  649. },
  650. },
  651. }, {
  652. desc: "map fields 2",
  653. inputMessage: &pb3.Maps{},
  654. inputText: `
  655. uint64_to_enum: {
  656. key: 1
  657. value: ONE
  658. }
  659. uint64_to_enum: {
  660. key: 2
  661. value: 2
  662. }
  663. uint64_to_enum: {
  664. key: 10
  665. value: 101
  666. }
  667. `,
  668. wantMessage: &pb3.Maps{
  669. Uint64ToEnum: map[uint64]pb3.Enum{
  670. 1: pb3.Enum_ONE,
  671. 2: pb3.Enum_TWO,
  672. 10: 101,
  673. },
  674. },
  675. }, {
  676. desc: "map fields 3",
  677. inputMessage: &pb3.Maps{},
  678. inputText: `
  679. str_to_nested: {
  680. key: "nested_one"
  681. value: {
  682. s_string: "nested in a map"
  683. }
  684. }
  685. `,
  686. wantMessage: &pb3.Maps{
  687. StrToNested: map[string]*pb3.Nested{
  688. "nested_one": &pb3.Nested{
  689. SString: "nested in a map",
  690. },
  691. },
  692. },
  693. }, {
  694. desc: "map fields 4",
  695. inputMessage: &pb3.Maps{},
  696. inputText: `
  697. str_to_oneofs: {
  698. key: "nested"
  699. value: {
  700. oneof_nested: {
  701. s_string: "nested oneof in map field value"
  702. }
  703. }
  704. }
  705. str_to_oneofs: {
  706. key: "string"
  707. value: {
  708. oneof_string: "hello"
  709. }
  710. }
  711. `,
  712. wantMessage: &pb3.Maps{
  713. StrToOneofs: map[string]*pb3.Oneofs{
  714. "string": &pb3.Oneofs{
  715. Union: &pb3.Oneofs_OneofString{
  716. OneofString: "hello",
  717. },
  718. },
  719. "nested": &pb3.Oneofs{
  720. Union: &pb3.Oneofs_OneofNested{
  721. OneofNested: &pb3.Nested{
  722. SString: "nested oneof in map field value",
  723. },
  724. },
  725. },
  726. },
  727. },
  728. }, {
  729. desc: "map contains duplicate keys",
  730. inputMessage: &pb3.Maps{},
  731. inputText: `
  732. int32_to_str: {
  733. key: 0
  734. value: "cero"
  735. }
  736. int32_to_str: {
  737. key: 0
  738. value: "zero"
  739. }
  740. `,
  741. wantMessage: &pb3.Maps{
  742. Int32ToStr: map[int32]string{
  743. 0: "zero",
  744. },
  745. },
  746. }, {
  747. desc: "map contains duplicate key fields",
  748. inputMessage: &pb3.Maps{},
  749. inputText: `
  750. int32_to_str: {
  751. key: 0
  752. key: 1
  753. value: "cero"
  754. }
  755. `,
  756. wantErr: true,
  757. }, {
  758. desc: "map contains duplicate value fields",
  759. inputMessage: &pb3.Maps{},
  760. inputText: `
  761. int32_to_str: {
  762. key: 1
  763. value: "cero"
  764. value: "uno"
  765. }
  766. `,
  767. wantErr: true,
  768. }, {
  769. desc: "map contains missing key",
  770. inputMessage: &pb3.Maps{},
  771. inputText: `
  772. int32_to_str: {
  773. value: "zero"
  774. }
  775. bool_to_uint32: {
  776. value: 47
  777. }
  778. str_to_nested: {
  779. value: {}
  780. }
  781. `,
  782. wantMessage: &pb3.Maps{
  783. Int32ToStr: map[int32]string{
  784. 0: "zero",
  785. },
  786. BoolToUint32: map[bool]uint32{
  787. false: 47,
  788. },
  789. StrToNested: map[string]*pb3.Nested{
  790. "": {},
  791. },
  792. },
  793. }, {
  794. desc: "map contains missing value",
  795. inputMessage: &pb3.Maps{},
  796. inputText: `
  797. int32_to_str: {
  798. key: 100
  799. }
  800. bool_to_uint32: {
  801. key: true
  802. }
  803. uint64_to_enum: {
  804. key: 101
  805. }
  806. str_to_nested: {
  807. key: "hello"
  808. }
  809. `,
  810. wantMessage: &pb3.Maps{
  811. Int32ToStr: map[int32]string{
  812. 100: "",
  813. },
  814. BoolToUint32: map[bool]uint32{
  815. true: 0,
  816. },
  817. Uint64ToEnum: map[uint64]pb3.Enum{
  818. 101: pb3.Enum_ZERO,
  819. },
  820. StrToNested: map[string]*pb3.Nested{
  821. "hello": {},
  822. },
  823. },
  824. }, {
  825. desc: "map contains missing key and value",
  826. inputMessage: &pb3.Maps{},
  827. inputText: `
  828. int32_to_str: {}
  829. bool_to_uint32: {}
  830. uint64_to_enum: {}
  831. str_to_nested: {}
  832. `,
  833. wantMessage: &pb3.Maps{
  834. Int32ToStr: map[int32]string{
  835. 0: "",
  836. },
  837. BoolToUint32: map[bool]uint32{
  838. false: 0,
  839. },
  840. Uint64ToEnum: map[uint64]pb3.Enum{
  841. 0: pb3.Enum_ZERO,
  842. },
  843. StrToNested: map[string]*pb3.Nested{
  844. "": {},
  845. },
  846. },
  847. }, {
  848. desc: "map contains overriding entries",
  849. inputMessage: &pb3.Maps{},
  850. inputText: `
  851. int32_to_str: {
  852. key: 0
  853. }
  854. int32_to_str: {
  855. value: "empty"
  856. }
  857. int32_to_str: {}
  858. `,
  859. wantMessage: &pb3.Maps{
  860. Int32ToStr: map[int32]string{
  861. 0: "",
  862. },
  863. },
  864. }, {
  865. desc: "map field value contains invalid UTF-8",
  866. inputMessage: &pb3.Maps{},
  867. inputText: `int32_to_str: {
  868. key: 101
  869. value: "abc\xff"
  870. }
  871. `,
  872. wantErr: true,
  873. }, {
  874. desc: "map field key contains invalid UTF-8",
  875. inputMessage: &pb3.Maps{},
  876. inputText: `str_to_nested: {
  877. key: "abc\xff"
  878. value: {}
  879. }
  880. `,
  881. wantErr: true,
  882. }, {
  883. desc: "map contains unknown field",
  884. inputMessage: &pb3.Maps{},
  885. inputText: `
  886. int32_to_str: {
  887. key: 0
  888. value: "cero"
  889. unknown: "bad"
  890. }
  891. `,
  892. wantErr: true,
  893. }, {
  894. desc: "map contains extension-like key field",
  895. inputMessage: &pb3.Maps{},
  896. inputText: `
  897. int32_to_str: {
  898. [key]: 10
  899. value: "ten"
  900. }
  901. `,
  902. wantErr: true,
  903. }, {
  904. desc: "map contains invalid key",
  905. inputMessage: &pb3.Maps{},
  906. inputText: `
  907. int32_to_str: {
  908. key: "invalid"
  909. value: "cero"
  910. }
  911. `,
  912. wantErr: true,
  913. }, {
  914. desc: "map contains invalid value",
  915. inputMessage: &pb3.Maps{},
  916. inputText: `
  917. int32_to_str: {
  918. key: 100
  919. value: 101
  920. }
  921. `,
  922. wantErr: true,
  923. }, {
  924. desc: "map using mix of [] and repeated",
  925. inputMessage: &pb3.Maps{},
  926. inputText: `
  927. int32_to_str: {
  928. key: 1
  929. value: "one"
  930. }
  931. int32_to_str: [
  932. {
  933. key: 2
  934. value: "not this"
  935. },
  936. {
  937. },
  938. {
  939. key: 3
  940. value: "three"
  941. }
  942. ]
  943. int32_to_str: {
  944. key: 2
  945. value: "two"
  946. }
  947. `,
  948. wantMessage: &pb3.Maps{
  949. Int32ToStr: map[int32]string{
  950. 0: "",
  951. 1: "one",
  952. 2: "two",
  953. 3: "three",
  954. },
  955. },
  956. }, {
  957. desc: "required fields not set",
  958. inputMessage: &pb2.Requireds{},
  959. wantErr: true,
  960. }, {
  961. desc: "required field set",
  962. inputMessage: &pb2.PartialRequired{},
  963. inputText: "req_string: 'this is required'",
  964. wantMessage: &pb2.PartialRequired{
  965. ReqString: proto.String("this is required"),
  966. },
  967. }, {
  968. desc: "required fields partially set",
  969. inputMessage: &pb2.Requireds{},
  970. inputText: `
  971. req_bool: false
  972. req_sfixed64: 3203386110
  973. req_string: "hello"
  974. req_enum: ONE
  975. `,
  976. wantMessage: &pb2.Requireds{
  977. ReqBool: proto.Bool(false),
  978. ReqSfixed64: proto.Int64(0xbeefcafe),
  979. ReqString: proto.String("hello"),
  980. ReqEnum: pb2.Enum_ONE.Enum(),
  981. },
  982. wantErr: true,
  983. }, {
  984. desc: "required fields partially set with AllowPartial",
  985. umo: prototext.UnmarshalOptions{AllowPartial: true},
  986. inputMessage: &pb2.Requireds{},
  987. inputText: `
  988. req_bool: false
  989. req_sfixed64: 3203386110
  990. req_string: "hello"
  991. req_enum: ONE
  992. `,
  993. wantMessage: &pb2.Requireds{
  994. ReqBool: proto.Bool(false),
  995. ReqSfixed64: proto.Int64(0xbeefcafe),
  996. ReqString: proto.String("hello"),
  997. ReqEnum: pb2.Enum_ONE.Enum(),
  998. },
  999. }, {
  1000. desc: "required fields all set",
  1001. inputMessage: &pb2.Requireds{},
  1002. inputText: `
  1003. req_bool: false
  1004. req_sfixed64: 0
  1005. req_double: 0
  1006. req_string: ""
  1007. req_enum: ONE
  1008. req_nested: {}
  1009. `,
  1010. wantMessage: &pb2.Requireds{
  1011. ReqBool: proto.Bool(false),
  1012. ReqSfixed64: proto.Int64(0),
  1013. ReqDouble: proto.Float64(0),
  1014. ReqString: proto.String(""),
  1015. ReqEnum: pb2.Enum_ONE.Enum(),
  1016. ReqNested: &pb2.Nested{},
  1017. },
  1018. }, {
  1019. desc: "indirect required field",
  1020. inputMessage: &pb2.IndirectRequired{},
  1021. inputText: "opt_nested: {}",
  1022. wantMessage: &pb2.IndirectRequired{
  1023. OptNested: &pb2.NestedWithRequired{},
  1024. },
  1025. wantErr: true,
  1026. }, {
  1027. desc: "indirect required field with AllowPartial",
  1028. umo: prototext.UnmarshalOptions{AllowPartial: true},
  1029. inputMessage: &pb2.IndirectRequired{},
  1030. inputText: "opt_nested: {}",
  1031. wantMessage: &pb2.IndirectRequired{
  1032. OptNested: &pb2.NestedWithRequired{},
  1033. },
  1034. }, {
  1035. desc: "indirect required field in repeated",
  1036. inputMessage: &pb2.IndirectRequired{},
  1037. inputText: `
  1038. rpt_nested: {
  1039. req_string: "one"
  1040. }
  1041. rpt_nested: {}
  1042. `,
  1043. wantMessage: &pb2.IndirectRequired{
  1044. RptNested: []*pb2.NestedWithRequired{
  1045. {
  1046. ReqString: proto.String("one"),
  1047. },
  1048. {},
  1049. },
  1050. },
  1051. wantErr: true,
  1052. }, {
  1053. desc: "indirect required field in repeated with AllowPartial",
  1054. umo: prototext.UnmarshalOptions{AllowPartial: true},
  1055. inputMessage: &pb2.IndirectRequired{},
  1056. inputText: `
  1057. rpt_nested: {
  1058. req_string: "one"
  1059. }
  1060. rpt_nested: {}
  1061. `,
  1062. wantMessage: &pb2.IndirectRequired{
  1063. RptNested: []*pb2.NestedWithRequired{
  1064. {
  1065. ReqString: proto.String("one"),
  1066. },
  1067. {},
  1068. },
  1069. },
  1070. }, {
  1071. desc: "indirect required field in map",
  1072. inputMessage: &pb2.IndirectRequired{},
  1073. inputText: `
  1074. str_to_nested: {
  1075. key: "missing"
  1076. }
  1077. str_to_nested: {
  1078. key: "contains"
  1079. value: {
  1080. req_string: "here"
  1081. }
  1082. }
  1083. `,
  1084. wantMessage: &pb2.IndirectRequired{
  1085. StrToNested: map[string]*pb2.NestedWithRequired{
  1086. "missing": &pb2.NestedWithRequired{},
  1087. "contains": &pb2.NestedWithRequired{
  1088. ReqString: proto.String("here"),
  1089. },
  1090. },
  1091. },
  1092. wantErr: true,
  1093. }, {
  1094. desc: "indirect required field in map with AllowPartial",
  1095. umo: prototext.UnmarshalOptions{AllowPartial: true},
  1096. inputMessage: &pb2.IndirectRequired{},
  1097. inputText: `
  1098. str_to_nested: {
  1099. key: "missing"
  1100. }
  1101. str_to_nested: {
  1102. key: "contains"
  1103. value: {
  1104. req_string: "here"
  1105. }
  1106. }
  1107. `,
  1108. wantMessage: &pb2.IndirectRequired{
  1109. StrToNested: map[string]*pb2.NestedWithRequired{
  1110. "missing": &pb2.NestedWithRequired{},
  1111. "contains": &pb2.NestedWithRequired{
  1112. ReqString: proto.String("here"),
  1113. },
  1114. },
  1115. },
  1116. }, {
  1117. desc: "indirect required field in oneof",
  1118. inputMessage: &pb2.IndirectRequired{},
  1119. inputText: `oneof_nested: {}
  1120. `,
  1121. wantMessage: &pb2.IndirectRequired{
  1122. Union: &pb2.IndirectRequired_OneofNested{
  1123. OneofNested: &pb2.NestedWithRequired{},
  1124. },
  1125. },
  1126. wantErr: true,
  1127. }, {
  1128. desc: "indirect required field in oneof with AllowPartial",
  1129. umo: prototext.UnmarshalOptions{AllowPartial: true},
  1130. inputMessage: &pb2.IndirectRequired{},
  1131. inputText: `oneof_nested: {}
  1132. `,
  1133. wantMessage: &pb2.IndirectRequired{
  1134. Union: &pb2.IndirectRequired_OneofNested{
  1135. OneofNested: &pb2.NestedWithRequired{},
  1136. },
  1137. },
  1138. }, {
  1139. desc: "ignore reserved field",
  1140. inputMessage: &pb2.Nests{},
  1141. inputText: "reserved_field: 'ignore this'",
  1142. wantMessage: &pb2.Nests{},
  1143. }, {
  1144. desc: "extensions of non-repeated fields",
  1145. inputMessage: &pb2.Extensions{},
  1146. inputText: `opt_string: "non-extension field"
  1147. [pb2.opt_ext_bool]: true
  1148. opt_bool: true
  1149. [pb2.opt_ext_nested]: {
  1150. opt_string: "nested in an extension"
  1151. opt_nested: {
  1152. opt_string: "another nested in an extension"
  1153. }
  1154. }
  1155. [pb2.opt_ext_string]: "extension field"
  1156. opt_int32: 42
  1157. [pb2.opt_ext_enum]: TEN
  1158. `,
  1159. wantMessage: func() proto.Message {
  1160. m := &pb2.Extensions{
  1161. OptString: proto.String("non-extension field"),
  1162. OptBool: proto.Bool(true),
  1163. OptInt32: proto.Int32(42),
  1164. }
  1165. setExtension(m, pb2.E_OptExtBool, true)
  1166. setExtension(m, pb2.E_OptExtString, "extension field")
  1167. setExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  1168. setExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  1169. OptString: proto.String("nested in an extension"),
  1170. OptNested: &pb2.Nested{
  1171. OptString: proto.String("another nested in an extension"),
  1172. },
  1173. })
  1174. return m
  1175. }(),
  1176. }, {
  1177. desc: "extension field contains invalid UTF-8",
  1178. inputMessage: &pb2.Extensions{},
  1179. inputText: `[pb2.opt_ext_string]: "abc\xff"`,
  1180. wantErr: true,
  1181. }, {
  1182. desc: "extensions of repeated fields",
  1183. inputMessage: &pb2.Extensions{},
  1184. inputText: `[pb2.rpt_ext_enum]: TEN
  1185. [pb2.rpt_ext_enum]: 101
  1186. [pb2.rpt_ext_fixed32]: 42
  1187. [pb2.rpt_ext_enum]: ONE
  1188. [pb2.rpt_ext_nested]: {
  1189. opt_string: "one"
  1190. }
  1191. [pb2.rpt_ext_nested]: {
  1192. opt_string: "two"
  1193. }
  1194. [pb2.rpt_ext_fixed32]: 47
  1195. [pb2.rpt_ext_nested]: {
  1196. opt_string: "three"
  1197. }
  1198. `,
  1199. wantMessage: func() proto.Message {
  1200. m := &pb2.Extensions{}
  1201. setExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1202. setExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  1203. setExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  1204. &pb2.Nested{OptString: proto.String("one")},
  1205. &pb2.Nested{OptString: proto.String("two")},
  1206. &pb2.Nested{OptString: proto.String("three")},
  1207. })
  1208. return m
  1209. }(),
  1210. }, {
  1211. desc: "extensions of non-repeated fields in another message",
  1212. inputMessage: &pb2.Extensions{},
  1213. inputText: `[pb2.ExtensionsContainer.opt_ext_bool]: true
  1214. [pb2.ExtensionsContainer.opt_ext_enum]: TEN
  1215. [pb2.ExtensionsContainer.opt_ext_nested]: {
  1216. opt_string: "nested in an extension"
  1217. opt_nested: {
  1218. opt_string: "another nested in an extension"
  1219. }
  1220. }
  1221. [pb2.ExtensionsContainer.opt_ext_string]: "extension field"
  1222. `,
  1223. wantMessage: func() proto.Message {
  1224. m := &pb2.Extensions{}
  1225. setExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  1226. setExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  1227. setExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  1228. setExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  1229. OptString: proto.String("nested in an extension"),
  1230. OptNested: &pb2.Nested{
  1231. OptString: proto.String("another nested in an extension"),
  1232. },
  1233. })
  1234. return m
  1235. }(),
  1236. }, {
  1237. desc: "extensions of repeated fields in another message",
  1238. inputMessage: &pb2.Extensions{},
  1239. inputText: `opt_string: "non-extension field"
  1240. opt_bool: true
  1241. opt_int32: 42
  1242. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1243. opt_string: "one"
  1244. }
  1245. [pb2.ExtensionsContainer.rpt_ext_enum]: TEN
  1246. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1247. opt_string: "two"
  1248. }
  1249. [pb2.ExtensionsContainer.rpt_ext_enum]: 101
  1250. [pb2.ExtensionsContainer.rpt_ext_string]: "hello"
  1251. [pb2.ExtensionsContainer.rpt_ext_enum]: ONE
  1252. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1253. opt_string: "three"
  1254. }
  1255. [pb2.ExtensionsContainer.rpt_ext_string]: "world"
  1256. `,
  1257. wantMessage: func() proto.Message {
  1258. m := &pb2.Extensions{
  1259. OptString: proto.String("non-extension field"),
  1260. OptBool: proto.Bool(true),
  1261. OptInt32: proto.Int32(42),
  1262. }
  1263. setExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1264. setExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1265. setExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1266. &pb2.Nested{OptString: proto.String("one")},
  1267. &pb2.Nested{OptString: proto.String("two")},
  1268. &pb2.Nested{OptString: proto.String("three")},
  1269. })
  1270. return m
  1271. }(),
  1272. }, {
  1273. desc: "invalid extension field name",
  1274. inputMessage: &pb2.Extensions{},
  1275. inputText: "[pb2.invalid_message_field]: true",
  1276. wantErr: true,
  1277. }, {
  1278. desc: "MessageSet",
  1279. inputMessage: &pb2.MessageSet{},
  1280. inputText: `
  1281. [pb2.MessageSetExtension]: {
  1282. opt_string: "a messageset extension"
  1283. }
  1284. [pb2.MessageSetExtension.ext_nested]: {
  1285. opt_string: "just a regular extension"
  1286. }
  1287. [pb2.MessageSetExtension.not_message_set_extension]: {
  1288. opt_string: "not a messageset extension"
  1289. }
  1290. `,
  1291. wantMessage: func() proto.Message {
  1292. m := &pb2.MessageSet{}
  1293. setExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1294. OptString: proto.String("a messageset extension"),
  1295. })
  1296. setExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1297. OptString: proto.String("not a messageset extension"),
  1298. })
  1299. setExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1300. OptString: proto.String("just a regular extension"),
  1301. })
  1302. return m
  1303. }(),
  1304. }, {
  1305. desc: "not real MessageSet 1",
  1306. inputMessage: &pb2.FakeMessageSet{},
  1307. inputText: `
  1308. [pb2.FakeMessageSetExtension.message_set_extension]: {
  1309. opt_string: "not a messageset extension"
  1310. }
  1311. `,
  1312. wantMessage: func() proto.Message {
  1313. m := &pb2.FakeMessageSet{}
  1314. setExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1315. OptString: proto.String("not a messageset extension"),
  1316. })
  1317. return m
  1318. }(),
  1319. }, {
  1320. desc: "not real MessageSet 2",
  1321. inputMessage: &pb2.FakeMessageSet{},
  1322. inputText: `
  1323. [pb2.FakeMessageSetExtension]: {
  1324. opt_string: "not a messageset extension"
  1325. }
  1326. `,
  1327. wantErr: true,
  1328. }, {
  1329. desc: "not real MessageSet 3",
  1330. inputMessage: &pb2.MessageSet{},
  1331. inputText: `
  1332. [pb2.message_set_extension]: {
  1333. opt_string: "another not a messageset extension"
  1334. }`,
  1335. wantMessage: func() proto.Message {
  1336. m := &pb2.MessageSet{}
  1337. setExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1338. OptString: proto.String("another not a messageset extension"),
  1339. })
  1340. return m
  1341. }(),
  1342. }, {
  1343. desc: "Any not expanded",
  1344. inputMessage: &anypb.Any{},
  1345. inputText: `
  1346. type_url: "pb2.Nested"
  1347. value: "some bytes"
  1348. `,
  1349. wantMessage: &anypb.Any{
  1350. TypeUrl: "pb2.Nested",
  1351. Value: []byte("some bytes"),
  1352. },
  1353. }, {
  1354. desc: "Any not expanded missing value",
  1355. inputMessage: &anypb.Any{},
  1356. inputText: `type_url: "pb2.Nested"`,
  1357. wantMessage: &anypb.Any{
  1358. TypeUrl: "pb2.Nested",
  1359. },
  1360. }, {
  1361. desc: "Any not expanded missing type_url",
  1362. inputMessage: &anypb.Any{},
  1363. inputText: `value: "some bytes"`,
  1364. wantMessage: &anypb.Any{
  1365. Value: []byte("some bytes"),
  1366. },
  1367. }, {
  1368. desc: "Any expanded",
  1369. umo: prototext.UnmarshalOptions{
  1370. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1371. },
  1372. inputMessage: &anypb.Any{},
  1373. inputText: `
  1374. [foobar/pb2.Nested]: {
  1375. opt_string: "embedded inside Any"
  1376. opt_nested: {
  1377. opt_string: "inception"
  1378. }
  1379. }
  1380. `,
  1381. wantMessage: func() proto.Message {
  1382. m := &pb2.Nested{
  1383. OptString: proto.String("embedded inside Any"),
  1384. OptNested: &pb2.Nested{
  1385. OptString: proto.String("inception"),
  1386. },
  1387. }
  1388. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1389. if err != nil {
  1390. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1391. }
  1392. return &anypb.Any{
  1393. TypeUrl: "foobar/pb2.Nested",
  1394. Value: b,
  1395. }
  1396. }(),
  1397. }, {
  1398. desc: "Any expanded with empty value",
  1399. umo: prototext.UnmarshalOptions{
  1400. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1401. },
  1402. inputMessage: &anypb.Any{},
  1403. inputText: `[foo.com/pb2.Nested]: {}`,
  1404. wantMessage: &anypb.Any{
  1405. TypeUrl: "foo.com/pb2.Nested",
  1406. },
  1407. }, {
  1408. desc: "Any expanded with missing required",
  1409. umo: prototext.UnmarshalOptions{
  1410. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1411. },
  1412. inputMessage: &anypb.Any{},
  1413. inputText: `
  1414. [pb2.PartialRequired]: {
  1415. opt_string: "embedded inside Any"
  1416. }
  1417. `,
  1418. wantMessage: func() proto.Message {
  1419. m := &pb2.PartialRequired{
  1420. OptString: proto.String("embedded inside Any"),
  1421. }
  1422. b, err := proto.MarshalOptions{
  1423. AllowPartial: true,
  1424. Deterministic: true,
  1425. }.Marshal(m)
  1426. if err != nil {
  1427. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1428. }
  1429. return &anypb.Any{
  1430. TypeUrl: "pb2.PartialRequired",
  1431. Value: b,
  1432. }
  1433. }(),
  1434. }, {
  1435. desc: "Any with invalid UTF-8",
  1436. umo: prototext.UnmarshalOptions{
  1437. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb3.Nested{})),
  1438. },
  1439. inputMessage: &anypb.Any{},
  1440. inputText: `
  1441. [pb3.Nested]: {
  1442. s_string: "abc\xff"
  1443. }
  1444. `,
  1445. wantErr: true,
  1446. }, {
  1447. desc: "Any expanded with unregistered type",
  1448. umo: prototext.UnmarshalOptions{Resolver: preg.NewTypes()},
  1449. inputMessage: &anypb.Any{},
  1450. inputText: `[SomeMessage]: {}`,
  1451. wantErr: true,
  1452. }, {
  1453. desc: "Any expanded with invalid value",
  1454. umo: prototext.UnmarshalOptions{
  1455. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1456. },
  1457. inputMessage: &anypb.Any{},
  1458. inputText: `[pb2.Nested]: 123`,
  1459. wantErr: true,
  1460. }, {
  1461. desc: "Any expanded with unknown fields",
  1462. umo: prototext.UnmarshalOptions{
  1463. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1464. },
  1465. inputMessage: &anypb.Any{},
  1466. inputText: `
  1467. [pb2.Nested]: {}
  1468. unknown: ""
  1469. `,
  1470. wantErr: true,
  1471. }, {
  1472. desc: "Any contains expanded and unexpanded fields",
  1473. umo: prototext.UnmarshalOptions{
  1474. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1475. },
  1476. inputMessage: &anypb.Any{},
  1477. inputText: `
  1478. [pb2.Nested]: {}
  1479. type_url: "pb2.Nested"
  1480. `,
  1481. wantErr: true,
  1482. }}
  1483. for _, tt := range tests {
  1484. tt := tt
  1485. t.Run(tt.desc, func(t *testing.T) {
  1486. err := tt.umo.Unmarshal([]byte(tt.inputText), tt.inputMessage)
  1487. if err != nil && !tt.wantErr {
  1488. t.Errorf("Unmarshal() returned error: %v\n\n", err)
  1489. }
  1490. if err == nil && tt.wantErr {
  1491. t.Error("Unmarshal() got nil error, want error\n\n")
  1492. }
  1493. if tt.wantMessage != nil && !proto.Equal(tt.inputMessage, tt.wantMessage) {
  1494. t.Errorf("Unmarshal()\n<got>\n%v\n<want>\n%v\n", tt.inputMessage, tt.wantMessage)
  1495. }
  1496. })
  1497. }
  1498. }