pack_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 pack
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "fmt"
  9. "math"
  10. "testing"
  11. pref "github.com/golang/protobuf/v2/reflect/protoreflect"
  12. ptype "github.com/golang/protobuf/v2/reflect/prototype"
  13. cmp "github.com/google/go-cmp/cmp"
  14. )
  15. var msgDesc = func() pref.MessageDescriptor {
  16. mtyp, err := ptype.NewMessage(&ptype.StandaloneMessage{
  17. Syntax: pref.Proto2,
  18. FullName: "Message",
  19. Fields: []ptype.Field{
  20. {Name: "F1", Number: 1, Cardinality: pref.Repeated, Kind: pref.BoolKind, IsPacked: ptype.True},
  21. {Name: "F2", Number: 2, Cardinality: pref.Repeated, Kind: pref.Int64Kind, IsPacked: ptype.True},
  22. {Name: "F3", Number: 3, Cardinality: pref.Repeated, Kind: pref.Sint64Kind, IsPacked: ptype.True},
  23. {Name: "F4", Number: 4, Cardinality: pref.Repeated, Kind: pref.Uint64Kind, IsPacked: ptype.True},
  24. {Name: "F5", Number: 5, Cardinality: pref.Repeated, Kind: pref.Fixed32Kind, IsPacked: ptype.True},
  25. {Name: "F6", Number: 6, Cardinality: pref.Repeated, Kind: pref.Sfixed32Kind, IsPacked: ptype.True},
  26. {Name: "F7", Number: 7, Cardinality: pref.Repeated, Kind: pref.FloatKind, IsPacked: ptype.True},
  27. {Name: "F8", Number: 8, Cardinality: pref.Repeated, Kind: pref.Fixed64Kind, IsPacked: ptype.True},
  28. {Name: "F9", Number: 9, Cardinality: pref.Repeated, Kind: pref.Sfixed64Kind, IsPacked: ptype.True},
  29. {Name: "F10", Number: 10, Cardinality: pref.Repeated, Kind: pref.DoubleKind, IsPacked: ptype.True},
  30. {Name: "F11", Number: 11, Cardinality: pref.Optional, Kind: pref.StringKind},
  31. {Name: "F12", Number: 12, Cardinality: pref.Optional, Kind: pref.BytesKind},
  32. {Name: "F13", Number: 13, Cardinality: pref.Optional, Kind: pref.MessageKind, MessageType: ptype.PlaceholderMessage("Message")},
  33. {Name: "F14", Number: 14, Cardinality: pref.Optional, Kind: pref.GroupKind, MessageType: ptype.PlaceholderMessage("Message")}},
  34. })
  35. if err != nil {
  36. panic(err)
  37. }
  38. return mtyp
  39. }()
  40. // dhex decodes a hex-string and returns the bytes and panics if s is invalid.
  41. func dhex(s string) []byte {
  42. b, err := hex.DecodeString(s)
  43. if err != nil {
  44. panic(err)
  45. }
  46. return b
  47. }
  48. func TestPack(t *testing.T) {
  49. tests := []struct {
  50. raw []byte
  51. msg Message
  52. wantOutCompact string
  53. wantOutMulti string
  54. wantOutSource string
  55. }{{
  56. raw: dhex("080088808080800002088280808080000a09010002828080808000"),
  57. msg: Message{
  58. Tag{1, VarintType}, Bool(false),
  59. Denormalized{5, Tag{1, VarintType}}, Uvarint(2),
  60. Tag{1, VarintType}, Denormalized{5, Uvarint(2)},
  61. Tag{1, BytesType}, LengthPrefix{Bool(true), Bool(false), Uvarint(2), Denormalized{5, Uvarint(2)}},
  62. },
  63. wantOutSource: `pack.Message{
  64. pack.Tag{1, pack.VarintType}, pack.Bool(false),
  65. pack.Denormalized{+5, pack.Tag{1, pack.VarintType}}, pack.Uvarint(2),
  66. pack.Tag{1, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(2)},
  67. pack.Tag{1, pack.BytesType}, pack.LengthPrefix{pack.Bool(true), pack.Bool(false), pack.Uvarint(2), pack.Denormalized{+5, pack.Uvarint(2)}},
  68. }`,
  69. }, {
  70. raw: dhex("100010828080808000121980808080808080808001ffffffffffffffff7f828080808000"),
  71. msg: Message{
  72. Tag{2, VarintType}, Varint(0),
  73. Tag{2, VarintType}, Denormalized{5, Varint(2)},
  74. Tag{2, BytesType}, LengthPrefix{Varint(math.MinInt64), Varint(math.MaxInt64), Denormalized{5, Varint(2)}},
  75. },
  76. wantOutCompact: `Message{Tag{2, Varint}, Varint(0), Tag{2, Varint}, Denormalized{+5, Varint(2)}, Tag{2, Bytes}, LengthPrefix{Varint(-9223372036854775808), Varint(9223372036854775807), Denormalized{+5, Varint(2)}}}`,
  77. }, {
  78. raw: dhex("1801188180808080001a1affffffffffffffffff01feffffffffffffffff01818080808000"),
  79. msg: Message{
  80. Tag{3, VarintType}, Svarint(-1),
  81. Tag{3, VarintType}, Denormalized{5, Svarint(-1)},
  82. Tag{3, BytesType}, LengthPrefix{Svarint(math.MinInt64), Svarint(math.MaxInt64), Denormalized{5, Svarint(-1)}},
  83. },
  84. wantOutMulti: `Message{
  85. Tag{3, Varint}, Svarint(-1),
  86. Tag{3, Varint}, Denormalized{+5, Svarint(-1)},
  87. Tag{3, Bytes}, LengthPrefix{Svarint(-9223372036854775808), Svarint(9223372036854775807), Denormalized{+5, Svarint(-1)}},
  88. }`,
  89. }, {
  90. raw: dhex("200120818080808000221100ffffffffffffffffff01818080808000"),
  91. msg: Message{
  92. Tag{4, VarintType}, Uvarint(+1),
  93. Tag{4, VarintType}, Denormalized{5, Uvarint(+1)},
  94. Tag{4, BytesType}, LengthPrefix{Uvarint(0), Uvarint(math.MaxUint64), Denormalized{5, Uvarint(+1)}},
  95. },
  96. wantOutSource: `pack.Message{
  97. pack.Tag{4, pack.VarintType}, pack.Uvarint(1),
  98. pack.Tag{4, pack.VarintType}, pack.Denormalized{+5, pack.Uvarint(1)},
  99. pack.Tag{4, pack.BytesType}, pack.LengthPrefix{pack.Uvarint(0), pack.Uvarint(18446744073709551615), pack.Denormalized{+5, pack.Uvarint(1)}},
  100. }`,
  101. }, {
  102. raw: dhex("2d010000002a0800000000ffffffff"),
  103. msg: Message{
  104. Tag{5, Fixed32Type}, Uint32(+1),
  105. Tag{5, BytesType}, LengthPrefix{Uint32(0), Uint32(math.MaxUint32)},
  106. },
  107. wantOutCompact: `Message{Tag{5, Fixed32}, Uint32(1), Tag{5, Bytes}, LengthPrefix{Uint32(0), Uint32(4294967295)}}`,
  108. }, {
  109. raw: dhex("35ffffffff320800000080ffffff7f"),
  110. msg: Message{
  111. Tag{6, Fixed32Type}, Int32(-1),
  112. Tag{6, BytesType}, LengthPrefix{Int32(math.MinInt32), Int32(math.MaxInt32)},
  113. },
  114. wantOutMulti: `Message{
  115. Tag{6, Fixed32}, Int32(-1),
  116. Tag{6, Bytes}, LengthPrefix{Int32(-2147483648), Int32(2147483647)},
  117. }`,
  118. }, {
  119. raw: dhex("3ddb0f49403a1401000000ffff7f7f0000c07f0000807f000080ff"),
  120. msg: Message{
  121. Tag{7, Fixed32Type}, Float32(math.Pi),
  122. Tag{7, BytesType}, LengthPrefix{Float32(math.SmallestNonzeroFloat32), Float32(math.MaxFloat32), Float32(math.NaN()), Float32(math.Inf(+1)), Float32(math.Inf(-1))},
  123. },
  124. wantOutSource: `pack.Message{
  125. pack.Tag{7, pack.Fixed32Type}, pack.Float32(3.1415927),
  126. pack.Tag{7, pack.BytesType}, pack.LengthPrefix{pack.Float32(1e-45), pack.Float32(3.4028235e+38), pack.Float32(math.NaN()), pack.Float32(math.Inf(+1)), pack.Float32(math.Inf(-1))},
  127. }`,
  128. }, {
  129. raw: dhex("41010000000000000042100000000000000000ffffffffffffffff"),
  130. msg: Message{
  131. Tag{8, Fixed64Type}, Uint64(+1),
  132. Tag{8, BytesType}, LengthPrefix{Uint64(0), Uint64(math.MaxUint64)},
  133. },
  134. wantOutCompact: `Message{Tag{8, Fixed64}, Uint64(1), Tag{8, Bytes}, LengthPrefix{Uint64(0), Uint64(18446744073709551615)}}`,
  135. }, {
  136. raw: dhex("49ffffffffffffffff4a100000000000000080ffffffffffffff7f"),
  137. msg: Message{
  138. Tag{9, Fixed64Type}, Int64(-1),
  139. Tag{9, BytesType}, LengthPrefix{Int64(math.MinInt64), Int64(math.MaxInt64)},
  140. },
  141. wantOutMulti: `Message{
  142. Tag{9, Fixed64}, Int64(-1),
  143. Tag{9, Bytes}, LengthPrefix{Int64(-9223372036854775808), Int64(9223372036854775807)},
  144. }`,
  145. }, {
  146. raw: dhex("51182d4454fb21094052280100000000000000ffffffffffffef7f010000000000f87f000000000000f07f000000000000f0ff"),
  147. msg: Message{
  148. Tag{10, Fixed64Type}, Float64(math.Pi),
  149. Tag{10, BytesType}, LengthPrefix{Float64(math.SmallestNonzeroFloat64), Float64(math.MaxFloat64), Float64(math.NaN()), Float64(math.Inf(+1)), Float64(math.Inf(-1))},
  150. },
  151. wantOutMulti: `Message{
  152. Tag{10, Fixed64}, Float64(3.141592653589793),
  153. Tag{10, Bytes}, LengthPrefix{Float64(5e-324), Float64(1.7976931348623157e+308), Float64(NaN), Float64(+Inf), Float64(-Inf)},
  154. }`,
  155. }, {
  156. raw: dhex("5a06737472696e675a868080808000737472696e67"),
  157. msg: Message{
  158. Tag{11, BytesType}, String("string"),
  159. Tag{11, BytesType}, Denormalized{+5, String("string")},
  160. },
  161. wantOutCompact: `Message{Tag{11, Bytes}, String("string"), Tag{11, Bytes}, Denormalized{+5, String("string")}}`,
  162. }, {
  163. raw: dhex("62056279746573628580808080006279746573"),
  164. msg: Message{
  165. Tag{12, BytesType}, Bytes("bytes"),
  166. Tag{12, BytesType}, Denormalized{+5, Bytes("bytes")},
  167. },
  168. wantOutMulti: `Message{
  169. Tag{12, Bytes}, Bytes("bytes"),
  170. Tag{12, Bytes}, Denormalized{+5, Bytes("bytes")},
  171. }`,
  172. }, {
  173. raw: dhex("6a28a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
  174. msg: Message{
  175. Tag{13, BytesType}, LengthPrefix(Message{
  176. Tag{100, VarintType}, Uvarint(math.MaxUint64),
  177. Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
  178. Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
  179. Tag{100, BytesType}, Bytes("bytes"),
  180. Tag{100, StartGroupType}, Tag{100, EndGroupType},
  181. }),
  182. },
  183. wantOutSource: `pack.Message{
  184. pack.Tag{13, pack.BytesType}, pack.LengthPrefix(pack.Message{
  185. pack.Tag{100, pack.VarintType}, pack.Uvarint(18446744073709551615),
  186. pack.Tag{100, pack.Fixed32Type}, pack.Uint32(4294967295),
  187. pack.Tag{100, pack.Fixed64Type}, pack.Uint64(18446744073709551615),
  188. pack.Tag{100, pack.BytesType}, pack.Bytes("bytes"),
  189. pack.Tag{100, pack.StartGroupType},
  190. pack.Tag{100, pack.EndGroupType},
  191. }),
  192. }`,
  193. }, {
  194. raw: dhex("6aa88080808000a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a406"),
  195. msg: Message{
  196. Tag{13, BytesType}, Denormalized{5, LengthPrefix(Message{
  197. Tag{100, VarintType}, Uvarint(math.MaxUint64),
  198. Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
  199. Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
  200. Tag{100, BytesType}, Bytes("bytes"),
  201. Tag{100, StartGroupType}, Tag{100, EndGroupType},
  202. })},
  203. },
  204. wantOutCompact: `Message{Tag{13, Bytes}, Denormalized{+5, LengthPrefix(Message{Tag{100, Varint}, Uvarint(18446744073709551615), Tag{100, Fixed32}, Uint32(4294967295), Tag{100, Fixed64}, Uint64(18446744073709551615), Tag{100, Bytes}, Bytes("bytes"), Tag{100, StartGroup}, Tag{100, EndGroup}})}}`,
  205. }, {
  206. raw: dhex("73a006ffffffffffffffffff01a506ffffffffa106ffffffffffffffffa206056279746573a306a40674"),
  207. msg: Message{
  208. Tag{14, StartGroupType}, Message{
  209. Tag{100, VarintType}, Uvarint(math.MaxUint64),
  210. Tag{100, Fixed32Type}, Uint32(math.MaxUint32),
  211. Tag{100, Fixed64Type}, Uint64(math.MaxUint64),
  212. Tag{100, BytesType}, Bytes("bytes"),
  213. Tag{100, StartGroupType}, Tag{100, EndGroupType},
  214. },
  215. Tag{14, EndGroupType},
  216. },
  217. wantOutMulti: `Message{
  218. Tag{14, StartGroup},
  219. Message{
  220. Tag{100, Varint}, Uvarint(18446744073709551615),
  221. Tag{100, Fixed32}, Uint32(4294967295),
  222. Tag{100, Fixed64}, Uint64(18446744073709551615),
  223. Tag{100, Bytes}, Bytes("bytes"),
  224. Tag{100, StartGroup},
  225. Tag{100, EndGroup},
  226. },
  227. Tag{14, EndGroup},
  228. }`,
  229. }, {
  230. raw: dhex("d0faa972cd02a5f09051c2d8aa0d6a26a89c311eddef024b423c0f6f47b64227a1600db56e3f73d4113096c9a88e2b99f2d847516853d76a1a6e9811c85a2ab3"),
  231. msg: Message{
  232. Tag{29970346, VarintType}, Uvarint(333),
  233. Tag{21268228, Fixed32Type}, Uint32(229300418),
  234. Tag{13, BytesType}, LengthPrefix(Message{
  235. Tag{100805, VarintType}, Uvarint(30),
  236. Tag{5883, Fixed32Type}, Uint32(255607371),
  237. Tag{13, Type(7)},
  238. Raw("G\xb6B'\xa1`\r\xb5n?s\xd4\x110\x96ɨ\x8e+\x99\xf2\xd8GQhS"),
  239. }),
  240. Tag{1706, Type(7)},
  241. Raw("\x1an\x98\x11\xc8Z*\xb3"),
  242. },
  243. }, {
  244. raw: dhex("3d08d0e57f"),
  245. msg: Message{
  246. Tag{7, Fixed32Type}, Float32(math.Float32frombits(
  247. // TODO: Remove workaround for compiler bug (see https://golang.org/issues/27193).
  248. func() uint32 { return 0x7fe5d008 }(),
  249. )),
  250. },
  251. wantOutSource: `pack.Message{
  252. pack.Tag{7, pack.Fixed32Type}, pack.Float32(math.Float32frombits(0x7fe5d008)),
  253. }`,
  254. }, {
  255. raw: dhex("51a8d65110771bf97f"),
  256. msg: Message{
  257. Tag{10, Fixed64Type}, Float64(math.Float64frombits(0x7ff91b771051d6a8)),
  258. },
  259. wantOutSource: `pack.Message{
  260. pack.Tag{10, pack.Fixed64Type}, pack.Float64(math.Float64frombits(0x7ff91b771051d6a8)),
  261. }`,
  262. }, {
  263. raw: dhex("ab2c14481ab3e9a76d937fb4dd5e6c616ef311f62b7fe888785fca5609ffe81c1064e50dd7a9edb408d317e2891c0d54c719446938d41ab0ccf8e61dc28b0ebb"),
  264. msg: Message{
  265. Tag{709, StartGroupType},
  266. Tag{2, EndGroupType},
  267. Tag{9, VarintType}, Uvarint(26),
  268. Tag{28655254, StartGroupType},
  269. Message{
  270. Tag{2034, StartGroupType},
  271. Tag{194006, EndGroupType},
  272. },
  273. Tag{13, EndGroupType},
  274. Tag{12, Fixed64Type}, Uint64(9865274810543764334),
  275. Tag{15, VarintType}, Uvarint(95),
  276. Tag{1385, BytesType}, Bytes("\xff\xe8\x1c\x10d\xe5\rש"),
  277. Tag{17229, Fixed32Type}, Uint32(2313295827),
  278. Tag{3, EndGroupType},
  279. Tag{1, Fixed32Type}, Uint32(1142540116),
  280. Tag{13, Fixed64Type}, Uint64(2154683029754926136),
  281. Tag{28856, BytesType},
  282. Raw("\xbb"),
  283. },
  284. }, {
  285. raw: dhex("29baa4ac1c1e0a20183393bac434b8d3559337ec940050038770eaa9937f98e4"),
  286. msg: Message{
  287. Tag{5, Fixed64Type}, Uint64(1738400580611384506),
  288. Tag{6, StartGroupType},
  289. Message{
  290. Tag{13771682, StartGroupType},
  291. Message{
  292. Tag{175415, VarintType}, Uvarint(7059),
  293. },
  294. Denormalized{+1, Tag{333, EndGroupType}},
  295. Tag{10, VarintType}, Uvarint(3),
  296. Tag{1792, Type(7)},
  297. Raw("꩓\u007f\x98\xe4"),
  298. },
  299. },
  300. }}
  301. equateFloatBits := cmp.Options{
  302. cmp.Comparer(func(x, y Float32) bool {
  303. return math.Float32bits(float32(x)) == math.Float32bits(float32(y))
  304. }),
  305. cmp.Comparer(func(x, y Float64) bool {
  306. return math.Float64bits(float64(x)) == math.Float64bits(float64(y))
  307. }),
  308. }
  309. for _, tt := range tests {
  310. t.Run("", func(t *testing.T) {
  311. var msg Message
  312. raw := tt.msg.Marshal()
  313. msg.UnmarshalDescriptor(tt.raw, msgDesc)
  314. if !bytes.Equal(raw, tt.raw) {
  315. t.Errorf("Marshal() mismatch:\ngot %x\nwant %x", raw, tt.raw)
  316. }
  317. if !cmp.Equal(msg, tt.msg, equateFloatBits) {
  318. t.Errorf("Unmarshal() mismatch:\ngot %+v\nwant %+v", msg, tt.msg)
  319. }
  320. if got, want := tt.msg.Size(), len(tt.raw); got != want {
  321. t.Errorf("Size() = %v, want %v", got, want)
  322. }
  323. if tt.wantOutCompact != "" {
  324. gotOut := fmt.Sprintf("%v", tt.msg)
  325. if string(gotOut) != tt.wantOutCompact {
  326. t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%v", gotOut, tt.wantOutCompact)
  327. }
  328. }
  329. if tt.wantOutMulti != "" {
  330. gotOut := fmt.Sprintf("%+v", tt.msg)
  331. if string(gotOut) != tt.wantOutMulti {
  332. t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%+v", gotOut, tt.wantOutMulti)
  333. }
  334. }
  335. if tt.wantOutSource != "" {
  336. gotOut := fmt.Sprintf("%#v", tt.msg)
  337. if string(gotOut) != tt.wantOutSource {
  338. t.Errorf("fmt.Sprintf(%q, msg):\ngot: %s\nwant: %s", "%#v", gotOut, tt.wantOutSource)
  339. }
  340. }
  341. })
  342. }
  343. }