encode_test.go 26 KB

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