decode_test.go 36 KB

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