encode_test.go 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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. "github.com/google/go-cmp/cmp"
  9. "google.golang.org/protobuf/encoding/prototext"
  10. "google.golang.org/protobuf/internal/detrand"
  11. "google.golang.org/protobuf/internal/encoding/pack"
  12. "google.golang.org/protobuf/internal/flags"
  13. pimpl "google.golang.org/protobuf/internal/impl"
  14. "google.golang.org/protobuf/proto"
  15. preg "google.golang.org/protobuf/reflect/protoregistry"
  16. "google.golang.org/protobuf/encoding/testprotos/pb2"
  17. "google.golang.org/protobuf/encoding/testprotos/pb3"
  18. "google.golang.org/protobuf/types/known/anypb"
  19. )
  20. func init() {
  21. // Disable detrand to enable direct comparisons on outputs.
  22. detrand.Disable()
  23. }
  24. func TestMarshal(t *testing.T) {
  25. tests := []struct {
  26. desc string
  27. mo prototext.MarshalOptions
  28. input proto.Message
  29. want string
  30. wantErr bool // TODO: Verify error message content.
  31. skip bool
  32. }{{
  33. desc: "proto2 optional scalars not set",
  34. input: &pb2.Scalars{},
  35. want: "\n",
  36. }, {
  37. desc: "proto3 scalars not set",
  38. input: &pb3.Scalars{},
  39. want: "\n",
  40. }, {
  41. desc: "proto2 optional scalars set to zero values",
  42. input: &pb2.Scalars{
  43. OptBool: proto.Bool(false),
  44. OptInt32: proto.Int32(0),
  45. OptInt64: proto.Int64(0),
  46. OptUint32: proto.Uint32(0),
  47. OptUint64: proto.Uint64(0),
  48. OptSint32: proto.Int32(0),
  49. OptSint64: proto.Int64(0),
  50. OptFixed32: proto.Uint32(0),
  51. OptFixed64: proto.Uint64(0),
  52. OptSfixed32: proto.Int32(0),
  53. OptSfixed64: proto.Int64(0),
  54. OptFloat: proto.Float32(0),
  55. OptDouble: proto.Float64(0),
  56. OptBytes: []byte{},
  57. OptString: proto.String(""),
  58. },
  59. want: `opt_bool: false
  60. opt_int32: 0
  61. opt_int64: 0
  62. opt_uint32: 0
  63. opt_uint64: 0
  64. opt_sint32: 0
  65. opt_sint64: 0
  66. opt_fixed32: 0
  67. opt_fixed64: 0
  68. opt_sfixed32: 0
  69. opt_sfixed64: 0
  70. opt_float: 0
  71. opt_double: 0
  72. opt_bytes: ""
  73. opt_string: ""
  74. `,
  75. }, {
  76. desc: "proto3 scalars set to zero values",
  77. input: &pb3.Scalars{
  78. SBool: false,
  79. SInt32: 0,
  80. SInt64: 0,
  81. SUint32: 0,
  82. SUint64: 0,
  83. SSint32: 0,
  84. SSint64: 0,
  85. SFixed32: 0,
  86. SFixed64: 0,
  87. SSfixed32: 0,
  88. SSfixed64: 0,
  89. SFloat: 0,
  90. SDouble: 0,
  91. SBytes: []byte{},
  92. SString: "",
  93. },
  94. want: "\n",
  95. }, {
  96. desc: "proto2 optional scalars set to some values",
  97. input: &pb2.Scalars{
  98. OptBool: proto.Bool(true),
  99. OptInt32: proto.Int32(0xff),
  100. OptInt64: proto.Int64(0xdeadbeef),
  101. OptUint32: proto.Uint32(47),
  102. OptUint64: proto.Uint64(0xdeadbeef),
  103. OptSint32: proto.Int32(-1001),
  104. OptSint64: proto.Int64(-0xffff),
  105. OptFixed64: proto.Uint64(64),
  106. OptSfixed32: proto.Int32(-32),
  107. OptFloat: proto.Float32(1.02),
  108. OptDouble: proto.Float64(1.0199999809265137),
  109. OptBytes: []byte("\xe8\xb0\xb7\xe6\xad\x8c"),
  110. OptString: proto.String("谷歌"),
  111. },
  112. want: `opt_bool: true
  113. opt_int32: 255
  114. opt_int64: 3735928559
  115. opt_uint32: 47
  116. opt_uint64: 3735928559
  117. opt_sint32: -1001
  118. opt_sint64: -65535
  119. opt_fixed64: 64
  120. opt_sfixed32: -32
  121. opt_float: 1.02
  122. opt_double: 1.0199999809265137
  123. opt_bytes: "谷歌"
  124. opt_string: "谷歌"
  125. `,
  126. }, {
  127. desc: "string with invalid UTF-8",
  128. input: &pb3.Scalars{
  129. SString: "abc\xff",
  130. },
  131. wantErr: true,
  132. }, {
  133. desc: "float nan",
  134. input: &pb3.Scalars{
  135. SFloat: float32(math.NaN()),
  136. },
  137. want: "s_float: nan\n",
  138. }, {
  139. desc: "float positive infinity",
  140. input: &pb3.Scalars{
  141. SFloat: float32(math.Inf(1)),
  142. },
  143. want: "s_float: inf\n",
  144. }, {
  145. desc: "float negative infinity",
  146. input: &pb3.Scalars{
  147. SFloat: float32(math.Inf(-1)),
  148. },
  149. want: "s_float: -inf\n",
  150. }, {
  151. desc: "double nan",
  152. input: &pb3.Scalars{
  153. SDouble: math.NaN(),
  154. },
  155. want: "s_double: nan\n",
  156. }, {
  157. desc: "double positive infinity",
  158. input: &pb3.Scalars{
  159. SDouble: math.Inf(1),
  160. },
  161. want: "s_double: inf\n",
  162. }, {
  163. desc: "double negative infinity",
  164. input: &pb3.Scalars{
  165. SDouble: math.Inf(-1),
  166. },
  167. want: "s_double: -inf\n",
  168. }, {
  169. desc: "proto2 enum not set",
  170. input: &pb2.Enums{},
  171. want: "\n",
  172. }, {
  173. desc: "proto2 enum set to zero value",
  174. input: &pb2.Enums{
  175. OptEnum: pb2.Enum(0).Enum(),
  176. OptNestedEnum: pb2.Enums_NestedEnum(0).Enum(),
  177. },
  178. want: `opt_enum: 0
  179. opt_nested_enum: 0
  180. `,
  181. }, {
  182. desc: "proto2 enum",
  183. input: &pb2.Enums{
  184. OptEnum: pb2.Enum_ONE.Enum(),
  185. OptNestedEnum: pb2.Enums_UNO.Enum(),
  186. },
  187. want: `opt_enum: ONE
  188. opt_nested_enum: UNO
  189. `,
  190. }, {
  191. desc: "proto2 enum set to numeric values",
  192. input: &pb2.Enums{
  193. OptEnum: pb2.Enum(2).Enum(),
  194. OptNestedEnum: pb2.Enums_NestedEnum(2).Enum(),
  195. },
  196. want: `opt_enum: TWO
  197. opt_nested_enum: DOS
  198. `,
  199. }, {
  200. desc: "proto2 enum set to unnamed numeric values",
  201. input: &pb2.Enums{
  202. OptEnum: pb2.Enum(101).Enum(),
  203. OptNestedEnum: pb2.Enums_NestedEnum(-101).Enum(),
  204. },
  205. want: `opt_enum: 101
  206. opt_nested_enum: -101
  207. `,
  208. }, {
  209. desc: "proto3 enum not set",
  210. input: &pb3.Enums{},
  211. want: "\n",
  212. }, {
  213. desc: "proto3 enum set to zero value",
  214. input: &pb3.Enums{
  215. SEnum: pb3.Enum_ZERO,
  216. SNestedEnum: pb3.Enums_CERO,
  217. },
  218. want: "\n",
  219. }, {
  220. desc: "proto3 enum",
  221. input: &pb3.Enums{
  222. SEnum: pb3.Enum_ONE,
  223. SNestedEnum: pb3.Enums_UNO,
  224. },
  225. want: `s_enum: ONE
  226. s_nested_enum: UNO
  227. `,
  228. }, {
  229. desc: "proto3 enum set to numeric values",
  230. input: &pb3.Enums{
  231. SEnum: 2,
  232. SNestedEnum: 2,
  233. },
  234. want: `s_enum: TWO
  235. s_nested_enum: DOS
  236. `,
  237. }, {
  238. desc: "proto3 enum set to unnamed numeric values",
  239. input: &pb3.Enums{
  240. SEnum: -47,
  241. SNestedEnum: 47,
  242. },
  243. want: `s_enum: -47
  244. s_nested_enum: 47
  245. `,
  246. }, {
  247. desc: "proto2 nested message not set",
  248. input: &pb2.Nests{},
  249. want: "\n",
  250. }, {
  251. desc: "proto2 nested message set to empty",
  252. input: &pb2.Nests{
  253. OptNested: &pb2.Nested{},
  254. Optgroup: &pb2.Nests_OptGroup{},
  255. },
  256. want: `opt_nested: {}
  257. OptGroup: {}
  258. `,
  259. }, {
  260. desc: "proto2 nested messages",
  261. input: &pb2.Nests{
  262. OptNested: &pb2.Nested{
  263. OptString: proto.String("nested message"),
  264. OptNested: &pb2.Nested{
  265. OptString: proto.String("another nested message"),
  266. },
  267. },
  268. },
  269. want: `opt_nested: {
  270. opt_string: "nested message"
  271. opt_nested: {
  272. opt_string: "another nested message"
  273. }
  274. }
  275. `,
  276. }, {
  277. desc: "proto2 groups",
  278. input: &pb2.Nests{
  279. Optgroup: &pb2.Nests_OptGroup{
  280. OptString: proto.String("inside a group"),
  281. OptNested: &pb2.Nested{
  282. OptString: proto.String("nested message inside a group"),
  283. },
  284. Optnestedgroup: &pb2.Nests_OptGroup_OptNestedGroup{
  285. OptFixed32: proto.Uint32(47),
  286. },
  287. },
  288. },
  289. want: `OptGroup: {
  290. opt_string: "inside a group"
  291. opt_nested: {
  292. opt_string: "nested message inside a group"
  293. }
  294. OptNestedGroup: {
  295. opt_fixed32: 47
  296. }
  297. }
  298. `,
  299. }, {
  300. desc: "proto3 nested message not set",
  301. input: &pb3.Nests{},
  302. want: "\n",
  303. }, {
  304. desc: "proto3 nested message set to empty",
  305. input: &pb3.Nests{
  306. SNested: &pb3.Nested{},
  307. },
  308. want: "s_nested: {}\n",
  309. }, {
  310. desc: "proto3 nested message",
  311. input: &pb3.Nests{
  312. SNested: &pb3.Nested{
  313. SString: "nested message",
  314. SNested: &pb3.Nested{
  315. SString: "another nested message",
  316. },
  317. },
  318. },
  319. want: `s_nested: {
  320. s_string: "nested message"
  321. s_nested: {
  322. s_string: "another nested message"
  323. }
  324. }
  325. `,
  326. }, {
  327. desc: "proto3 nested message contains invalid UTF-8",
  328. input: &pb3.Nests{
  329. SNested: &pb3.Nested{
  330. SString: "abc\xff",
  331. },
  332. },
  333. wantErr: true,
  334. }, {
  335. desc: "oneof not set",
  336. input: &pb3.Oneofs{},
  337. want: "\n",
  338. }, {
  339. desc: "oneof set to empty string",
  340. input: &pb3.Oneofs{
  341. Union: &pb3.Oneofs_OneofString{},
  342. },
  343. want: `oneof_string: ""
  344. `,
  345. }, {
  346. desc: "oneof set to string",
  347. input: &pb3.Oneofs{
  348. Union: &pb3.Oneofs_OneofString{
  349. OneofString: "hello",
  350. },
  351. },
  352. want: `oneof_string: "hello"
  353. `,
  354. }, {
  355. desc: "oneof set to enum",
  356. input: &pb3.Oneofs{
  357. Union: &pb3.Oneofs_OneofEnum{
  358. OneofEnum: pb3.Enum_ZERO,
  359. },
  360. },
  361. want: `oneof_enum: ZERO
  362. `,
  363. }, {
  364. desc: "oneof set to empty message",
  365. input: &pb3.Oneofs{
  366. Union: &pb3.Oneofs_OneofNested{
  367. OneofNested: &pb3.Nested{},
  368. },
  369. },
  370. want: "oneof_nested: {}\n",
  371. }, {
  372. desc: "oneof set to message",
  373. input: &pb3.Oneofs{
  374. Union: &pb3.Oneofs_OneofNested{
  375. OneofNested: &pb3.Nested{
  376. SString: "nested message",
  377. },
  378. },
  379. },
  380. want: `oneof_nested: {
  381. s_string: "nested message"
  382. }
  383. `,
  384. }, {
  385. desc: "repeated fields not set",
  386. input: &pb2.Repeats{},
  387. want: "\n",
  388. }, {
  389. desc: "repeated fields set to empty slices",
  390. input: &pb2.Repeats{
  391. RptBool: []bool{},
  392. RptInt32: []int32{},
  393. RptInt64: []int64{},
  394. RptUint32: []uint32{},
  395. RptUint64: []uint64{},
  396. RptFloat: []float32{},
  397. RptDouble: []float64{},
  398. RptBytes: [][]byte{},
  399. },
  400. want: "\n",
  401. }, {
  402. desc: "repeated fields set to some values",
  403. input: &pb2.Repeats{
  404. RptBool: []bool{true, false, true, true},
  405. RptInt32: []int32{1, 6, 0, 0},
  406. RptInt64: []int64{-64, 47},
  407. RptUint32: []uint32{0xff, 0xffff},
  408. RptUint64: []uint64{0xdeadbeef},
  409. RptFloat: []float32{float32(math.NaN()), float32(math.Inf(1)), float32(math.Inf(-1)), 1.034},
  410. RptDouble: []float64{math.NaN(), math.Inf(1), math.Inf(-1), 1.23e-308},
  411. RptString: []string{"hello", "世界"},
  412. RptBytes: [][]byte{
  413. []byte("hello"),
  414. []byte("\xe4\xb8\x96\xe7\x95\x8c"),
  415. },
  416. },
  417. want: `rpt_bool: true
  418. rpt_bool: false
  419. rpt_bool: true
  420. rpt_bool: true
  421. rpt_int32: 1
  422. rpt_int32: 6
  423. rpt_int32: 0
  424. rpt_int32: 0
  425. rpt_int64: -64
  426. rpt_int64: 47
  427. rpt_uint32: 255
  428. rpt_uint32: 65535
  429. rpt_uint64: 3735928559
  430. rpt_float: nan
  431. rpt_float: inf
  432. rpt_float: -inf
  433. rpt_float: 1.034
  434. rpt_double: nan
  435. rpt_double: inf
  436. rpt_double: -inf
  437. rpt_double: 1.23e-308
  438. rpt_string: "hello"
  439. rpt_string: "世界"
  440. rpt_bytes: "hello"
  441. rpt_bytes: "世界"
  442. `,
  443. }, {
  444. desc: "repeated contains invalid UTF-8",
  445. input: &pb2.Repeats{
  446. RptString: []string{"abc\xff"},
  447. },
  448. wantErr: true,
  449. }, {
  450. desc: "repeated enums",
  451. input: &pb2.Enums{
  452. RptEnum: []pb2.Enum{pb2.Enum_ONE, 2, pb2.Enum_TEN, 42},
  453. RptNestedEnum: []pb2.Enums_NestedEnum{2, 47, 10},
  454. },
  455. want: `rpt_enum: ONE
  456. rpt_enum: TWO
  457. rpt_enum: TEN
  458. rpt_enum: 42
  459. rpt_nested_enum: DOS
  460. rpt_nested_enum: 47
  461. rpt_nested_enum: DIEZ
  462. `,
  463. }, {
  464. desc: "repeated messages set to empty",
  465. input: &pb2.Nests{
  466. RptNested: []*pb2.Nested{},
  467. Rptgroup: []*pb2.Nests_RptGroup{},
  468. },
  469. want: "\n",
  470. }, {
  471. desc: "repeated messages",
  472. input: &pb2.Nests{
  473. RptNested: []*pb2.Nested{
  474. {
  475. OptString: proto.String("repeat nested one"),
  476. },
  477. {
  478. OptString: proto.String("repeat nested two"),
  479. OptNested: &pb2.Nested{
  480. OptString: proto.String("inside repeat nested two"),
  481. },
  482. },
  483. {},
  484. },
  485. },
  486. want: `rpt_nested: {
  487. opt_string: "repeat nested one"
  488. }
  489. rpt_nested: {
  490. opt_string: "repeat nested two"
  491. opt_nested: {
  492. opt_string: "inside repeat nested two"
  493. }
  494. }
  495. rpt_nested: {}
  496. `,
  497. }, {
  498. desc: "repeated messages contains nil value",
  499. input: &pb2.Nests{
  500. RptNested: []*pb2.Nested{nil, {}},
  501. },
  502. want: `rpt_nested: {}
  503. rpt_nested: {}
  504. `,
  505. }, {
  506. desc: "repeated groups",
  507. input: &pb2.Nests{
  508. Rptgroup: []*pb2.Nests_RptGroup{
  509. {
  510. RptString: []string{"hello", "world"},
  511. },
  512. {},
  513. nil,
  514. },
  515. },
  516. want: `RptGroup: {
  517. rpt_string: "hello"
  518. rpt_string: "world"
  519. }
  520. RptGroup: {}
  521. RptGroup: {}
  522. `,
  523. }, {
  524. desc: "map fields not set",
  525. input: &pb3.Maps{},
  526. want: "\n",
  527. }, {
  528. desc: "map fields set to empty",
  529. input: &pb3.Maps{
  530. Int32ToStr: map[int32]string{},
  531. BoolToUint32: map[bool]uint32{},
  532. Uint64ToEnum: map[uint64]pb3.Enum{},
  533. StrToNested: map[string]*pb3.Nested{},
  534. StrToOneofs: map[string]*pb3.Oneofs{},
  535. },
  536. want: "\n",
  537. }, {
  538. desc: "map fields 1",
  539. input: &pb3.Maps{
  540. Int32ToStr: map[int32]string{
  541. -101: "-101",
  542. 0xff: "0xff",
  543. 0: "zero",
  544. },
  545. BoolToUint32: map[bool]uint32{
  546. true: 42,
  547. false: 101,
  548. },
  549. },
  550. want: `int32_to_str: {
  551. key: -101
  552. value: "-101"
  553. }
  554. int32_to_str: {
  555. key: 0
  556. value: "zero"
  557. }
  558. int32_to_str: {
  559. key: 255
  560. value: "0xff"
  561. }
  562. bool_to_uint32: {
  563. key: false
  564. value: 101
  565. }
  566. bool_to_uint32: {
  567. key: true
  568. value: 42
  569. }
  570. `,
  571. }, {
  572. desc: "map fields 2",
  573. input: &pb3.Maps{
  574. Uint64ToEnum: map[uint64]pb3.Enum{
  575. 1: pb3.Enum_ONE,
  576. 2: pb3.Enum_TWO,
  577. 10: pb3.Enum_TEN,
  578. 47: 47,
  579. },
  580. },
  581. want: `uint64_to_enum: {
  582. key: 1
  583. value: ONE
  584. }
  585. uint64_to_enum: {
  586. key: 2
  587. value: TWO
  588. }
  589. uint64_to_enum: {
  590. key: 10
  591. value: TEN
  592. }
  593. uint64_to_enum: {
  594. key: 47
  595. value: 47
  596. }
  597. `,
  598. }, {
  599. desc: "map fields 3",
  600. input: &pb3.Maps{
  601. StrToNested: map[string]*pb3.Nested{
  602. "nested": &pb3.Nested{
  603. SString: "nested in a map",
  604. },
  605. },
  606. },
  607. want: `str_to_nested: {
  608. key: "nested"
  609. value: {
  610. s_string: "nested in a map"
  611. }
  612. }
  613. `,
  614. }, {
  615. desc: "map fields 4",
  616. input: &pb3.Maps{
  617. StrToOneofs: map[string]*pb3.Oneofs{
  618. "string": &pb3.Oneofs{
  619. Union: &pb3.Oneofs_OneofString{
  620. OneofString: "hello",
  621. },
  622. },
  623. "nested": &pb3.Oneofs{
  624. Union: &pb3.Oneofs_OneofNested{
  625. OneofNested: &pb3.Nested{
  626. SString: "nested oneof in map field value",
  627. },
  628. },
  629. },
  630. },
  631. },
  632. want: `str_to_oneofs: {
  633. key: "nested"
  634. value: {
  635. oneof_nested: {
  636. s_string: "nested oneof in map field value"
  637. }
  638. }
  639. }
  640. str_to_oneofs: {
  641. key: "string"
  642. value: {
  643. oneof_string: "hello"
  644. }
  645. }
  646. `,
  647. }, {
  648. desc: "map field value contains invalid UTF-8",
  649. input: &pb3.Maps{
  650. Int32ToStr: map[int32]string{
  651. 101: "abc\xff",
  652. },
  653. },
  654. wantErr: true,
  655. }, {
  656. desc: "map field key contains invalid UTF-8",
  657. input: &pb3.Maps{
  658. StrToNested: map[string]*pb3.Nested{
  659. "abc\xff": {},
  660. },
  661. },
  662. wantErr: true,
  663. }, {
  664. desc: "map field contains nil value",
  665. input: &pb3.Maps{
  666. StrToNested: map[string]*pb3.Nested{
  667. "nil": nil,
  668. },
  669. },
  670. want: `str_to_nested: {
  671. key: "nil"
  672. value: {}
  673. }
  674. `,
  675. }, {
  676. desc: "required fields not set",
  677. input: &pb2.Requireds{},
  678. want: "\n",
  679. wantErr: true,
  680. }, {
  681. desc: "required fields partially set",
  682. input: &pb2.Requireds{
  683. ReqBool: proto.Bool(false),
  684. ReqSfixed64: proto.Int64(0xbeefcafe),
  685. ReqDouble: proto.Float64(math.NaN()),
  686. ReqString: proto.String("hello"),
  687. ReqEnum: pb2.Enum_ONE.Enum(),
  688. },
  689. want: `req_bool: false
  690. req_sfixed64: 3203386110
  691. req_double: nan
  692. req_string: "hello"
  693. req_enum: ONE
  694. `,
  695. wantErr: true,
  696. }, {
  697. desc: "required fields not set with AllowPartial",
  698. mo: prototext.MarshalOptions{AllowPartial: true},
  699. input: &pb2.Requireds{
  700. ReqBool: proto.Bool(false),
  701. ReqSfixed64: proto.Int64(0xbeefcafe),
  702. ReqDouble: proto.Float64(math.NaN()),
  703. ReqString: proto.String("hello"),
  704. ReqEnum: pb2.Enum_ONE.Enum(),
  705. },
  706. want: `req_bool: false
  707. req_sfixed64: 3203386110
  708. req_double: nan
  709. req_string: "hello"
  710. req_enum: ONE
  711. `,
  712. }, {
  713. desc: "required fields all set",
  714. input: &pb2.Requireds{
  715. ReqBool: proto.Bool(false),
  716. ReqSfixed64: proto.Int64(0),
  717. ReqDouble: proto.Float64(1.23),
  718. ReqString: proto.String(""),
  719. ReqEnum: pb2.Enum_ONE.Enum(),
  720. ReqNested: &pb2.Nested{},
  721. },
  722. want: `req_bool: false
  723. req_sfixed64: 0
  724. req_double: 1.23
  725. req_string: ""
  726. req_enum: ONE
  727. req_nested: {}
  728. `,
  729. }, {
  730. desc: "indirect required field",
  731. input: &pb2.IndirectRequired{
  732. OptNested: &pb2.NestedWithRequired{},
  733. },
  734. want: "opt_nested: {}\n",
  735. wantErr: true,
  736. }, {
  737. desc: "indirect required field with AllowPartial",
  738. mo: prototext.MarshalOptions{AllowPartial: true},
  739. input: &pb2.IndirectRequired{
  740. OptNested: &pb2.NestedWithRequired{},
  741. },
  742. want: "opt_nested: {}\n",
  743. }, {
  744. desc: "indirect required field in empty repeated",
  745. input: &pb2.IndirectRequired{
  746. RptNested: []*pb2.NestedWithRequired{},
  747. },
  748. want: "\n",
  749. }, {
  750. desc: "indirect required field in repeated",
  751. input: &pb2.IndirectRequired{
  752. RptNested: []*pb2.NestedWithRequired{
  753. &pb2.NestedWithRequired{},
  754. },
  755. },
  756. want: "rpt_nested: {}\n",
  757. wantErr: true,
  758. }, {
  759. desc: "indirect required field in repeated with AllowPartial",
  760. mo: prototext.MarshalOptions{AllowPartial: true},
  761. input: &pb2.IndirectRequired{
  762. RptNested: []*pb2.NestedWithRequired{
  763. &pb2.NestedWithRequired{},
  764. },
  765. },
  766. want: "rpt_nested: {}\n",
  767. }, {
  768. desc: "indirect required field in empty map",
  769. input: &pb2.IndirectRequired{
  770. StrToNested: map[string]*pb2.NestedWithRequired{},
  771. },
  772. want: "\n",
  773. }, {
  774. desc: "indirect required field in map",
  775. input: &pb2.IndirectRequired{
  776. StrToNested: map[string]*pb2.NestedWithRequired{
  777. "fail": &pb2.NestedWithRequired{},
  778. },
  779. },
  780. want: `str_to_nested: {
  781. key: "fail"
  782. value: {}
  783. }
  784. `,
  785. wantErr: true,
  786. }, {
  787. desc: "indirect required field in map with AllowPartial",
  788. mo: prototext.MarshalOptions{AllowPartial: true},
  789. input: &pb2.IndirectRequired{
  790. StrToNested: map[string]*pb2.NestedWithRequired{
  791. "fail": &pb2.NestedWithRequired{},
  792. },
  793. },
  794. want: `str_to_nested: {
  795. key: "fail"
  796. value: {}
  797. }
  798. `,
  799. }, {
  800. desc: "indirect required field in oneof",
  801. input: &pb2.IndirectRequired{
  802. Union: &pb2.IndirectRequired_OneofNested{
  803. OneofNested: &pb2.NestedWithRequired{},
  804. },
  805. },
  806. want: "oneof_nested: {}\n",
  807. wantErr: true,
  808. }, {
  809. desc: "indirect required field in oneof with AllowPartial",
  810. mo: prototext.MarshalOptions{AllowPartial: true},
  811. input: &pb2.IndirectRequired{
  812. Union: &pb2.IndirectRequired_OneofNested{
  813. OneofNested: &pb2.NestedWithRequired{},
  814. },
  815. },
  816. want: "oneof_nested: {}\n",
  817. }, {
  818. desc: "unknown varint and fixed types",
  819. input: func() proto.Message {
  820. m := &pb2.Scalars{
  821. OptString: proto.String("this message contains unknown fields"),
  822. }
  823. m.ProtoReflect().SetUnknown(pack.Message{
  824. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  825. pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
  826. pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
  827. pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
  828. }.Marshal())
  829. return m
  830. }(),
  831. want: `opt_string: "this message contains unknown fields"
  832. 101: 1
  833. 102: 255
  834. 103: 47
  835. 104: 3735928559
  836. `,
  837. }, {
  838. desc: "unknown length-delimited",
  839. input: func() proto.Message {
  840. m := new(pb2.Scalars)
  841. m.ProtoReflect().SetUnknown(pack.Message{
  842. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
  843. pack.Tag{102, pack.BytesType}, pack.String("hello world"),
  844. pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
  845. }.Marshal())
  846. return m
  847. }(),
  848. want: `101: "\x01\x00"
  849. 102: "hello world"
  850. 103: "世界"
  851. `,
  852. }, {
  853. desc: "unknown group type",
  854. input: func() proto.Message {
  855. m := new(pb2.Scalars)
  856. m.ProtoReflect().SetUnknown(pack.Message{
  857. pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
  858. pack.Tag{102, pack.StartGroupType},
  859. pack.Tag{101, pack.VarintType}, pack.Bool(false),
  860. pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
  861. pack.Tag{102, pack.EndGroupType},
  862. }.Marshal())
  863. return m
  864. }(),
  865. want: `101: {}
  866. 102: {
  867. 101: 0
  868. 102: "inside a group"
  869. }
  870. `,
  871. }, {
  872. desc: "unknown unpack repeated field",
  873. input: func() proto.Message {
  874. m := new(pb2.Scalars)
  875. m.ProtoReflect().SetUnknown(pack.Message{
  876. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
  877. pack.Tag{102, pack.BytesType}, pack.String("hello"),
  878. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  879. pack.Tag{102, pack.BytesType}, pack.String("世界"),
  880. }.Marshal())
  881. return m
  882. }(),
  883. want: `101: "\x01\x00\x01"
  884. 102: "hello"
  885. 101: 1
  886. 102: "世界"
  887. `,
  888. }, {
  889. desc: "extensions of non-repeated fields",
  890. input: func() proto.Message {
  891. m := &pb2.Extensions{
  892. OptString: proto.String("non-extension field"),
  893. OptBool: proto.Bool(true),
  894. OptInt32: proto.Int32(42),
  895. }
  896. proto.SetExtension(m, pb2.E_OptExtBool, true)
  897. proto.SetExtension(m, pb2.E_OptExtString, "extension field")
  898. proto.SetExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  899. proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  900. OptString: proto.String("nested in an extension"),
  901. OptNested: &pb2.Nested{
  902. OptString: proto.String("another nested in an extension"),
  903. },
  904. })
  905. return m
  906. }(),
  907. want: `opt_string: "non-extension field"
  908. opt_bool: true
  909. opt_int32: 42
  910. [pb2.opt_ext_bool]: true
  911. [pb2.opt_ext_enum]: TEN
  912. [pb2.opt_ext_nested]: {
  913. opt_string: "nested in an extension"
  914. opt_nested: {
  915. opt_string: "another nested in an extension"
  916. }
  917. }
  918. [pb2.opt_ext_string]: "extension field"
  919. `,
  920. }, {
  921. desc: "extension field contains invalid UTF-8",
  922. input: func() proto.Message {
  923. m := &pb2.Extensions{}
  924. proto.SetExtension(m, pb2.E_OptExtString, "abc\xff")
  925. return m
  926. }(),
  927. wantErr: true,
  928. }, {
  929. desc: "extension partial returns error",
  930. input: func() proto.Message {
  931. m := &pb2.Extensions{}
  932. proto.SetExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
  933. OptString: proto.String("partial1"),
  934. })
  935. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtPartial, &pb2.PartialRequired{
  936. OptString: proto.String("partial2"),
  937. })
  938. return m
  939. }(),
  940. want: `[pb2.ExtensionsContainer.opt_ext_partial]: {
  941. opt_string: "partial2"
  942. }
  943. [pb2.opt_ext_partial]: {
  944. opt_string: "partial1"
  945. }
  946. `,
  947. wantErr: true,
  948. }, {
  949. desc: "extension partial with AllowPartial",
  950. mo: prototext.MarshalOptions{AllowPartial: true},
  951. input: func() proto.Message {
  952. m := &pb2.Extensions{}
  953. proto.SetExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
  954. OptString: proto.String("partial1"),
  955. })
  956. return m
  957. }(),
  958. want: `[pb2.opt_ext_partial]: {
  959. opt_string: "partial1"
  960. }
  961. `,
  962. }, {
  963. desc: "extensions of repeated fields",
  964. input: func() proto.Message {
  965. m := &pb2.Extensions{}
  966. proto.SetExtension(m, pb2.E_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  967. proto.SetExtension(m, pb2.E_RptExtFixed32, &[]uint32{42, 47})
  968. proto.SetExtension(m, pb2.E_RptExtNested, &[]*pb2.Nested{
  969. &pb2.Nested{OptString: proto.String("one")},
  970. &pb2.Nested{OptString: proto.String("two")},
  971. &pb2.Nested{OptString: proto.String("three")},
  972. })
  973. return m
  974. }(),
  975. want: `[pb2.rpt_ext_enum]: TEN
  976. [pb2.rpt_ext_enum]: 101
  977. [pb2.rpt_ext_enum]: ONE
  978. [pb2.rpt_ext_fixed32]: 42
  979. [pb2.rpt_ext_fixed32]: 47
  980. [pb2.rpt_ext_nested]: {
  981. opt_string: "one"
  982. }
  983. [pb2.rpt_ext_nested]: {
  984. opt_string: "two"
  985. }
  986. [pb2.rpt_ext_nested]: {
  987. opt_string: "three"
  988. }
  989. `,
  990. }, {
  991. desc: "extensions of non-repeated fields in another message",
  992. input: func() proto.Message {
  993. m := &pb2.Extensions{}
  994. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  995. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  996. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  997. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  998. OptString: proto.String("nested in an extension"),
  999. OptNested: &pb2.Nested{
  1000. OptString: proto.String("another nested in an extension"),
  1001. },
  1002. })
  1003. return m
  1004. }(),
  1005. want: `[pb2.ExtensionsContainer.opt_ext_bool]: true
  1006. [pb2.ExtensionsContainer.opt_ext_enum]: TEN
  1007. [pb2.ExtensionsContainer.opt_ext_nested]: {
  1008. opt_string: "nested in an extension"
  1009. opt_nested: {
  1010. opt_string: "another nested in an extension"
  1011. }
  1012. }
  1013. [pb2.ExtensionsContainer.opt_ext_string]: "extension field"
  1014. `,
  1015. }, {
  1016. desc: "extensions of repeated fields in another message",
  1017. input: func() proto.Message {
  1018. m := &pb2.Extensions{
  1019. OptString: proto.String("non-extension field"),
  1020. OptBool: proto.Bool(true),
  1021. OptInt32: proto.Int32(42),
  1022. }
  1023. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, &[]pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1024. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtString, &[]string{"hello", "world"})
  1025. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtNested, &[]*pb2.Nested{
  1026. &pb2.Nested{OptString: proto.String("one")},
  1027. &pb2.Nested{OptString: proto.String("two")},
  1028. &pb2.Nested{OptString: proto.String("three")},
  1029. })
  1030. return m
  1031. }(),
  1032. want: `opt_string: "non-extension field"
  1033. opt_bool: true
  1034. opt_int32: 42
  1035. [pb2.ExtensionsContainer.rpt_ext_enum]: TEN
  1036. [pb2.ExtensionsContainer.rpt_ext_enum]: 101
  1037. [pb2.ExtensionsContainer.rpt_ext_enum]: ONE
  1038. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1039. opt_string: "one"
  1040. }
  1041. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1042. opt_string: "two"
  1043. }
  1044. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1045. opt_string: "three"
  1046. }
  1047. [pb2.ExtensionsContainer.rpt_ext_string]: "hello"
  1048. [pb2.ExtensionsContainer.rpt_ext_string]: "world"
  1049. `,
  1050. }, {
  1051. desc: "MessageSet",
  1052. input: func() proto.Message {
  1053. m := &pb2.MessageSet{}
  1054. proto.SetExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1055. OptString: proto.String("a messageset extension"),
  1056. })
  1057. proto.SetExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1058. OptString: proto.String("not a messageset extension"),
  1059. })
  1060. proto.SetExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1061. OptString: proto.String("just a regular extension"),
  1062. })
  1063. return m
  1064. }(),
  1065. want: `[pb2.MessageSetExtension]: {
  1066. opt_string: "a messageset extension"
  1067. }
  1068. [pb2.MessageSetExtension.ext_nested]: {
  1069. opt_string: "just a regular extension"
  1070. }
  1071. [pb2.MessageSetExtension.not_message_set_extension]: {
  1072. opt_string: "not a messageset extension"
  1073. }
  1074. `,
  1075. skip: !flags.ProtoLegacy,
  1076. }, {
  1077. desc: "not real MessageSet 1",
  1078. input: func() proto.Message {
  1079. m := &pb2.FakeMessageSet{}
  1080. proto.SetExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1081. OptString: proto.String("not a messageset extension"),
  1082. })
  1083. return m
  1084. }(),
  1085. want: `[pb2.FakeMessageSetExtension.message_set_extension]: {
  1086. opt_string: "not a messageset extension"
  1087. }
  1088. `,
  1089. skip: !flags.ProtoLegacy,
  1090. }, {
  1091. desc: "not real MessageSet 2",
  1092. input: func() proto.Message {
  1093. m := &pb2.MessageSet{}
  1094. proto.SetExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1095. OptString: proto.String("another not a messageset extension"),
  1096. })
  1097. return m
  1098. }(),
  1099. want: `[pb2.message_set_extension]: {
  1100. opt_string: "another not a messageset extension"
  1101. }
  1102. `,
  1103. skip: !flags.ProtoLegacy,
  1104. }, {
  1105. desc: "Any not expanded",
  1106. mo: prototext.MarshalOptions{
  1107. Resolver: preg.NewTypes(),
  1108. },
  1109. input: func() proto.Message {
  1110. m := &pb2.Nested{
  1111. OptString: proto.String("embedded inside Any"),
  1112. OptNested: &pb2.Nested{
  1113. OptString: proto.String("inception"),
  1114. },
  1115. }
  1116. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1117. if err != nil {
  1118. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1119. }
  1120. return &anypb.Any{
  1121. TypeUrl: "pb2.Nested",
  1122. Value: b,
  1123. }
  1124. }(),
  1125. want: `type_url: "pb2.Nested"
  1126. value: "\n\x13embedded inside Any\x12\x0b\n\tinception"
  1127. `,
  1128. }, {
  1129. desc: "Any expanded",
  1130. mo: prototext.MarshalOptions{
  1131. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1132. },
  1133. input: func() proto.Message {
  1134. m := &pb2.Nested{
  1135. OptString: proto.String("embedded inside Any"),
  1136. OptNested: &pb2.Nested{
  1137. OptString: proto.String("inception"),
  1138. },
  1139. }
  1140. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1141. if err != nil {
  1142. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1143. }
  1144. return &anypb.Any{
  1145. TypeUrl: "foo/pb2.Nested",
  1146. Value: b,
  1147. }
  1148. }(),
  1149. want: `[foo/pb2.Nested]: {
  1150. opt_string: "embedded inside Any"
  1151. opt_nested: {
  1152. opt_string: "inception"
  1153. }
  1154. }
  1155. `,
  1156. }, {
  1157. desc: "Any expanded with missing required",
  1158. mo: prototext.MarshalOptions{
  1159. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1160. },
  1161. input: func() proto.Message {
  1162. m := &pb2.PartialRequired{
  1163. OptString: proto.String("embedded inside Any"),
  1164. }
  1165. b, err := proto.MarshalOptions{
  1166. AllowPartial: true,
  1167. Deterministic: true,
  1168. }.Marshal(m)
  1169. if err != nil {
  1170. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1171. }
  1172. return &anypb.Any{
  1173. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1174. Value: b,
  1175. }
  1176. }(),
  1177. want: `[pb2.PartialRequired]: {
  1178. opt_string: "embedded inside Any"
  1179. }
  1180. `,
  1181. }, {
  1182. desc: "Any with invalid value",
  1183. mo: prototext.MarshalOptions{
  1184. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1185. },
  1186. input: &anypb.Any{
  1187. TypeUrl: "foo/pb2.Nested",
  1188. Value: []byte("\x80"),
  1189. },
  1190. want: `type_url: "foo/pb2.Nested"
  1191. value: "\x80"
  1192. `,
  1193. }}
  1194. for _, tt := range tests {
  1195. tt := tt
  1196. if tt.skip {
  1197. continue
  1198. }
  1199. t.Run(tt.desc, func(t *testing.T) {
  1200. // Use 2-space indentation on all MarshalOptions.
  1201. tt.mo.Indent = " "
  1202. b, err := tt.mo.Marshal(tt.input)
  1203. if err != nil && !tt.wantErr {
  1204. t.Errorf("Marshal() returned error: %v\n", err)
  1205. }
  1206. if err == nil && tt.wantErr {
  1207. t.Error("Marshal() got nil error, want error\n")
  1208. }
  1209. got := string(b)
  1210. if tt.want != "" && got != tt.want {
  1211. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1212. if diff := cmp.Diff(tt.want, got); diff != "" {
  1213. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1214. }
  1215. }
  1216. })
  1217. }
  1218. }