encode_test.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  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: "",
  36. }, {
  37. desc: "proto3 scalars not set",
  38. input: &pb3.Scalars{},
  39. want: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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: "",
  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 fields not printed",
  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. `,
  833. }, {
  834. desc: "unknown varint and fixed types",
  835. mo: prototext.MarshalOptions{EmitUnknown: true},
  836. input: func() proto.Message {
  837. m := &pb2.Scalars{
  838. OptString: proto.String("this message contains unknown fields"),
  839. }
  840. m.ProtoReflect().SetUnknown(pack.Message{
  841. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  842. pack.Tag{102, pack.VarintType}, pack.Varint(0xff),
  843. pack.Tag{103, pack.Fixed32Type}, pack.Uint32(47),
  844. pack.Tag{104, pack.Fixed64Type}, pack.Int64(0xdeadbeef),
  845. }.Marshal())
  846. return m
  847. }(),
  848. want: `opt_string: "this message contains unknown fields"
  849. 101: 1
  850. 102: 255
  851. 103: 47
  852. 104: 3735928559
  853. `,
  854. }, {
  855. desc: "unknown length-delimited",
  856. mo: prototext.MarshalOptions{EmitUnknown: true},
  857. input: func() proto.Message {
  858. m := new(pb2.Scalars)
  859. m.ProtoReflect().SetUnknown(pack.Message{
  860. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false)},
  861. pack.Tag{102, pack.BytesType}, pack.String("hello world"),
  862. pack.Tag{103, pack.BytesType}, pack.Bytes("\xe4\xb8\x96\xe7\x95\x8c"),
  863. }.Marshal())
  864. return m
  865. }(),
  866. want: `101: "\x01\x00"
  867. 102: "hello world"
  868. 103: "世界"
  869. `,
  870. }, {
  871. desc: "unknown group type",
  872. mo: prototext.MarshalOptions{EmitUnknown: true},
  873. input: func() proto.Message {
  874. m := new(pb2.Scalars)
  875. m.ProtoReflect().SetUnknown(pack.Message{
  876. pack.Tag{101, pack.StartGroupType}, pack.Tag{101, pack.EndGroupType},
  877. pack.Tag{102, pack.StartGroupType},
  878. pack.Tag{101, pack.VarintType}, pack.Bool(false),
  879. pack.Tag{102, pack.BytesType}, pack.String("inside a group"),
  880. pack.Tag{102, pack.EndGroupType},
  881. }.Marshal())
  882. return m
  883. }(),
  884. want: `101: {}
  885. 102: {
  886. 101: 0
  887. 102: "inside a group"
  888. }
  889. `,
  890. }, {
  891. desc: "unknown unpack repeated field",
  892. mo: prototext.MarshalOptions{EmitUnknown: true},
  893. input: func() proto.Message {
  894. m := new(pb2.Scalars)
  895. m.ProtoReflect().SetUnknown(pack.Message{
  896. pack.Tag{101, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Bool(true)},
  897. pack.Tag{102, pack.BytesType}, pack.String("hello"),
  898. pack.Tag{101, pack.VarintType}, pack.Bool(true),
  899. pack.Tag{102, pack.BytesType}, pack.String("世界"),
  900. }.Marshal())
  901. return m
  902. }(),
  903. want: `101: "\x01\x00\x01"
  904. 102: "hello"
  905. 101: 1
  906. 102: "世界"
  907. `,
  908. }, {
  909. desc: "extensions of non-repeated fields",
  910. input: func() proto.Message {
  911. m := &pb2.Extensions{
  912. OptString: proto.String("non-extension field"),
  913. OptBool: proto.Bool(true),
  914. OptInt32: proto.Int32(42),
  915. }
  916. proto.SetExtension(m, pb2.E_OptExtBool, true)
  917. proto.SetExtension(m, pb2.E_OptExtString, "extension field")
  918. proto.SetExtension(m, pb2.E_OptExtEnum, pb2.Enum_TEN)
  919. proto.SetExtension(m, pb2.E_OptExtNested, &pb2.Nested{
  920. OptString: proto.String("nested in an extension"),
  921. OptNested: &pb2.Nested{
  922. OptString: proto.String("another nested in an extension"),
  923. },
  924. })
  925. return m
  926. }(),
  927. want: `opt_string: "non-extension field"
  928. opt_bool: true
  929. opt_int32: 42
  930. [pb2.opt_ext_bool]: true
  931. [pb2.opt_ext_enum]: TEN
  932. [pb2.opt_ext_nested]: {
  933. opt_string: "nested in an extension"
  934. opt_nested: {
  935. opt_string: "another nested in an extension"
  936. }
  937. }
  938. [pb2.opt_ext_string]: "extension field"
  939. `,
  940. }, {
  941. desc: "extension field contains invalid UTF-8",
  942. input: func() proto.Message {
  943. m := &pb2.Extensions{}
  944. proto.SetExtension(m, pb2.E_OptExtString, "abc\xff")
  945. return m
  946. }(),
  947. wantErr: true,
  948. }, {
  949. desc: "extension partial returns error",
  950. input: func() proto.Message {
  951. m := &pb2.Extensions{}
  952. proto.SetExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
  953. OptString: proto.String("partial1"),
  954. })
  955. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtPartial, &pb2.PartialRequired{
  956. OptString: proto.String("partial2"),
  957. })
  958. return m
  959. }(),
  960. want: `[pb2.ExtensionsContainer.opt_ext_partial]: {
  961. opt_string: "partial2"
  962. }
  963. [pb2.opt_ext_partial]: {
  964. opt_string: "partial1"
  965. }
  966. `,
  967. wantErr: true,
  968. }, {
  969. desc: "extension partial with AllowPartial",
  970. mo: prototext.MarshalOptions{AllowPartial: true},
  971. input: func() proto.Message {
  972. m := &pb2.Extensions{}
  973. proto.SetExtension(m, pb2.E_OptExtPartial, &pb2.PartialRequired{
  974. OptString: proto.String("partial1"),
  975. })
  976. return m
  977. }(),
  978. want: `[pb2.opt_ext_partial]: {
  979. opt_string: "partial1"
  980. }
  981. `,
  982. }, {
  983. desc: "extensions of repeated fields",
  984. input: func() proto.Message {
  985. m := &pb2.Extensions{}
  986. proto.SetExtension(m, pb2.E_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  987. proto.SetExtension(m, pb2.E_RptExtFixed32, []uint32{42, 47})
  988. proto.SetExtension(m, pb2.E_RptExtNested, []*pb2.Nested{
  989. &pb2.Nested{OptString: proto.String("one")},
  990. &pb2.Nested{OptString: proto.String("two")},
  991. &pb2.Nested{OptString: proto.String("three")},
  992. })
  993. return m
  994. }(),
  995. want: `[pb2.rpt_ext_enum]: TEN
  996. [pb2.rpt_ext_enum]: 101
  997. [pb2.rpt_ext_enum]: ONE
  998. [pb2.rpt_ext_fixed32]: 42
  999. [pb2.rpt_ext_fixed32]: 47
  1000. [pb2.rpt_ext_nested]: {
  1001. opt_string: "one"
  1002. }
  1003. [pb2.rpt_ext_nested]: {
  1004. opt_string: "two"
  1005. }
  1006. [pb2.rpt_ext_nested]: {
  1007. opt_string: "three"
  1008. }
  1009. `,
  1010. }, {
  1011. desc: "extensions of non-repeated fields in another message",
  1012. input: func() proto.Message {
  1013. m := &pb2.Extensions{}
  1014. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtBool, true)
  1015. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtString, "extension field")
  1016. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtEnum, pb2.Enum_TEN)
  1017. proto.SetExtension(m, pb2.E_ExtensionsContainer_OptExtNested, &pb2.Nested{
  1018. OptString: proto.String("nested in an extension"),
  1019. OptNested: &pb2.Nested{
  1020. OptString: proto.String("another nested in an extension"),
  1021. },
  1022. })
  1023. return m
  1024. }(),
  1025. want: `[pb2.ExtensionsContainer.opt_ext_bool]: true
  1026. [pb2.ExtensionsContainer.opt_ext_enum]: TEN
  1027. [pb2.ExtensionsContainer.opt_ext_nested]: {
  1028. opt_string: "nested in an extension"
  1029. opt_nested: {
  1030. opt_string: "another nested in an extension"
  1031. }
  1032. }
  1033. [pb2.ExtensionsContainer.opt_ext_string]: "extension field"
  1034. `,
  1035. }, {
  1036. desc: "extensions of repeated fields in another message",
  1037. input: func() proto.Message {
  1038. m := &pb2.Extensions{
  1039. OptString: proto.String("non-extension field"),
  1040. OptBool: proto.Bool(true),
  1041. OptInt32: proto.Int32(42),
  1042. }
  1043. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtEnum, []pb2.Enum{pb2.Enum_TEN, 101, pb2.Enum_ONE})
  1044. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtString, []string{"hello", "world"})
  1045. proto.SetExtension(m, pb2.E_ExtensionsContainer_RptExtNested, []*pb2.Nested{
  1046. &pb2.Nested{OptString: proto.String("one")},
  1047. &pb2.Nested{OptString: proto.String("two")},
  1048. &pb2.Nested{OptString: proto.String("three")},
  1049. })
  1050. return m
  1051. }(),
  1052. want: `opt_string: "non-extension field"
  1053. opt_bool: true
  1054. opt_int32: 42
  1055. [pb2.ExtensionsContainer.rpt_ext_enum]: TEN
  1056. [pb2.ExtensionsContainer.rpt_ext_enum]: 101
  1057. [pb2.ExtensionsContainer.rpt_ext_enum]: ONE
  1058. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1059. opt_string: "one"
  1060. }
  1061. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1062. opt_string: "two"
  1063. }
  1064. [pb2.ExtensionsContainer.rpt_ext_nested]: {
  1065. opt_string: "three"
  1066. }
  1067. [pb2.ExtensionsContainer.rpt_ext_string]: "hello"
  1068. [pb2.ExtensionsContainer.rpt_ext_string]: "world"
  1069. `,
  1070. }, {
  1071. desc: "MessageSet",
  1072. input: func() proto.Message {
  1073. m := &pb2.MessageSet{}
  1074. proto.SetExtension(m, pb2.E_MessageSetExtension_MessageSetExtension, &pb2.MessageSetExtension{
  1075. OptString: proto.String("a messageset extension"),
  1076. })
  1077. proto.SetExtension(m, pb2.E_MessageSetExtension_NotMessageSetExtension, &pb2.MessageSetExtension{
  1078. OptString: proto.String("not a messageset extension"),
  1079. })
  1080. proto.SetExtension(m, pb2.E_MessageSetExtension_ExtNested, &pb2.Nested{
  1081. OptString: proto.String("just a regular extension"),
  1082. })
  1083. return m
  1084. }(),
  1085. want: `[pb2.MessageSetExtension]: {
  1086. opt_string: "a messageset extension"
  1087. }
  1088. [pb2.MessageSetExtension.ext_nested]: {
  1089. opt_string: "just a regular extension"
  1090. }
  1091. [pb2.MessageSetExtension.not_message_set_extension]: {
  1092. opt_string: "not a messageset extension"
  1093. }
  1094. `,
  1095. skip: !flags.ProtoLegacy,
  1096. }, {
  1097. desc: "not real MessageSet 1",
  1098. input: func() proto.Message {
  1099. m := &pb2.FakeMessageSet{}
  1100. proto.SetExtension(m, pb2.E_FakeMessageSetExtension_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1101. OptString: proto.String("not a messageset extension"),
  1102. })
  1103. return m
  1104. }(),
  1105. want: `[pb2.FakeMessageSetExtension.message_set_extension]: {
  1106. opt_string: "not a messageset extension"
  1107. }
  1108. `,
  1109. skip: !flags.ProtoLegacy,
  1110. }, {
  1111. desc: "not real MessageSet 2",
  1112. input: func() proto.Message {
  1113. m := &pb2.MessageSet{}
  1114. proto.SetExtension(m, pb2.E_MessageSetExtension, &pb2.FakeMessageSetExtension{
  1115. OptString: proto.String("another not a messageset extension"),
  1116. })
  1117. return m
  1118. }(),
  1119. want: `[pb2.message_set_extension]: {
  1120. opt_string: "another not a messageset extension"
  1121. }
  1122. `,
  1123. skip: !flags.ProtoLegacy,
  1124. }, {
  1125. desc: "Any not expanded",
  1126. mo: prototext.MarshalOptions{
  1127. Resolver: preg.NewTypes(),
  1128. },
  1129. input: func() proto.Message {
  1130. m := &pb2.Nested{
  1131. OptString: proto.String("embedded inside Any"),
  1132. OptNested: &pb2.Nested{
  1133. OptString: proto.String("inception"),
  1134. },
  1135. }
  1136. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1137. if err != nil {
  1138. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1139. }
  1140. return &anypb.Any{
  1141. TypeUrl: "pb2.Nested",
  1142. Value: b,
  1143. }
  1144. }(),
  1145. want: `type_url: "pb2.Nested"
  1146. value: "\n\x13embedded inside Any\x12\x0b\n\tinception"
  1147. `,
  1148. }, {
  1149. desc: "Any expanded",
  1150. mo: prototext.MarshalOptions{
  1151. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1152. },
  1153. input: func() proto.Message {
  1154. m := &pb2.Nested{
  1155. OptString: proto.String("embedded inside Any"),
  1156. OptNested: &pb2.Nested{
  1157. OptString: proto.String("inception"),
  1158. },
  1159. }
  1160. b, err := proto.MarshalOptions{Deterministic: true}.Marshal(m)
  1161. if err != nil {
  1162. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1163. }
  1164. return &anypb.Any{
  1165. TypeUrl: "foo/pb2.Nested",
  1166. Value: b,
  1167. }
  1168. }(),
  1169. want: `[foo/pb2.Nested]: {
  1170. opt_string: "embedded inside Any"
  1171. opt_nested: {
  1172. opt_string: "inception"
  1173. }
  1174. }
  1175. `,
  1176. }, {
  1177. desc: "Any expanded with missing required",
  1178. mo: prototext.MarshalOptions{
  1179. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.PartialRequired{})),
  1180. },
  1181. input: func() proto.Message {
  1182. m := &pb2.PartialRequired{
  1183. OptString: proto.String("embedded inside Any"),
  1184. }
  1185. b, err := proto.MarshalOptions{
  1186. AllowPartial: true,
  1187. Deterministic: true,
  1188. }.Marshal(m)
  1189. if err != nil {
  1190. t.Fatalf("error in binary marshaling message for Any.value: %v", err)
  1191. }
  1192. return &anypb.Any{
  1193. TypeUrl: string(m.ProtoReflect().Descriptor().FullName()),
  1194. Value: b,
  1195. }
  1196. }(),
  1197. want: `[pb2.PartialRequired]: {
  1198. opt_string: "embedded inside Any"
  1199. }
  1200. `,
  1201. }, {
  1202. desc: "Any with invalid value",
  1203. mo: prototext.MarshalOptions{
  1204. Resolver: preg.NewTypes(pimpl.Export{}.MessageTypeOf(&pb2.Nested{})),
  1205. },
  1206. input: &anypb.Any{
  1207. TypeUrl: "foo/pb2.Nested",
  1208. Value: []byte("\x80"),
  1209. },
  1210. want: `type_url: "foo/pb2.Nested"
  1211. value: "\x80"
  1212. `,
  1213. }}
  1214. for _, tt := range tests {
  1215. tt := tt
  1216. if tt.skip {
  1217. continue
  1218. }
  1219. t.Run(tt.desc, func(t *testing.T) {
  1220. // Use 2-space indentation on all MarshalOptions.
  1221. tt.mo.Indent = " "
  1222. b, err := tt.mo.Marshal(tt.input)
  1223. if err != nil && !tt.wantErr {
  1224. t.Errorf("Marshal() returned error: %v\n", err)
  1225. }
  1226. if err == nil && tt.wantErr {
  1227. t.Error("Marshal() got nil error, want error\n")
  1228. }
  1229. got := string(b)
  1230. if tt.want != "" && got != tt.want {
  1231. t.Errorf("Marshal()\n<got>\n%v\n<want>\n%v\n", got, tt.want)
  1232. if diff := cmp.Diff(tt.want, got); diff != "" {
  1233. t.Errorf("Marshal() diff -want +got\n%v\n", diff)
  1234. }
  1235. }
  1236. })
  1237. }
  1238. }