decode_test.go 35 KB

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