encode_test.go 26 KB

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