encode_test.go 29 KB

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