decode_test.go 41 KB

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