pack_test.go 14 KB

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