decode_test.go 36 KB

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