pack_test.go 14 KB

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