hpack_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  1. // Copyright 2014 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 hpack
  5. import (
  6. "bytes"
  7. "encoding/hex"
  8. "fmt"
  9. "math/rand"
  10. "reflect"
  11. "strings"
  12. "testing"
  13. "time"
  14. )
  15. func (d *Decoder) mustAt(idx int) HeaderField {
  16. if hf, ok := d.at(uint64(idx)); !ok {
  17. panic(fmt.Sprintf("bogus index %d", idx))
  18. } else {
  19. return hf
  20. }
  21. }
  22. func TestDynamicTableAt(t *testing.T) {
  23. d := NewDecoder(4096, nil)
  24. at := d.mustAt
  25. if got, want := at(2), (pair(":method", "GET")); got != want {
  26. t.Errorf("at(2) = %v; want %v", got, want)
  27. }
  28. d.dynTab.add(pair("foo", "bar"))
  29. d.dynTab.add(pair("blake", "miz"))
  30. if got, want := at(staticTable.len()+1), (pair("blake", "miz")); got != want {
  31. t.Errorf("at(dyn 1) = %v; want %v", got, want)
  32. }
  33. if got, want := at(staticTable.len()+2), (pair("foo", "bar")); got != want {
  34. t.Errorf("at(dyn 2) = %v; want %v", got, want)
  35. }
  36. if got, want := at(3), (pair(":method", "POST")); got != want {
  37. t.Errorf("at(3) = %v; want %v", got, want)
  38. }
  39. }
  40. func TestDynamicTableSizeEvict(t *testing.T) {
  41. d := NewDecoder(4096, nil)
  42. if want := uint32(0); d.dynTab.size != want {
  43. t.Fatalf("size = %d; want %d", d.dynTab.size, want)
  44. }
  45. add := d.dynTab.add
  46. add(pair("blake", "eats pizza"))
  47. if want := uint32(15 + 32); d.dynTab.size != want {
  48. t.Fatalf("after pizza, size = %d; want %d", d.dynTab.size, want)
  49. }
  50. add(pair("foo", "bar"))
  51. if want := uint32(15 + 32 + 6 + 32); d.dynTab.size != want {
  52. t.Fatalf("after foo bar, size = %d; want %d", d.dynTab.size, want)
  53. }
  54. d.dynTab.setMaxSize(15 + 32 + 1 /* slop */)
  55. if want := uint32(6 + 32); d.dynTab.size != want {
  56. t.Fatalf("after setMaxSize, size = %d; want %d", d.dynTab.size, want)
  57. }
  58. if got, want := d.mustAt(staticTable.len()+1), (pair("foo", "bar")); got != want {
  59. t.Errorf("at(dyn 1) = %v; want %v", got, want)
  60. }
  61. add(pair("long", strings.Repeat("x", 500)))
  62. if want := uint32(0); d.dynTab.size != want {
  63. t.Fatalf("after big one, size = %d; want %d", d.dynTab.size, want)
  64. }
  65. }
  66. func TestDecoderDecode(t *testing.T) {
  67. tests := []struct {
  68. name string
  69. in []byte
  70. want []HeaderField
  71. wantDynTab []HeaderField // newest entry first
  72. }{
  73. // C.2.1 Literal Header Field with Indexing
  74. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.1
  75. {"C.2.1", dehex("400a 6375 7374 6f6d 2d6b 6579 0d63 7573 746f 6d2d 6865 6164 6572"),
  76. []HeaderField{pair("custom-key", "custom-header")},
  77. []HeaderField{pair("custom-key", "custom-header")},
  78. },
  79. // C.2.2 Literal Header Field without Indexing
  80. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.2
  81. {"C.2.2", dehex("040c 2f73 616d 706c 652f 7061 7468"),
  82. []HeaderField{pair(":path", "/sample/path")},
  83. []HeaderField{}},
  84. // C.2.3 Literal Header Field never Indexed
  85. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.3
  86. {"C.2.3", dehex("1008 7061 7373 776f 7264 0673 6563 7265 74"),
  87. []HeaderField{{"password", "secret", true}},
  88. []HeaderField{}},
  89. // C.2.4 Indexed Header Field
  90. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.4
  91. {"C.2.4", []byte("\x82"),
  92. []HeaderField{pair(":method", "GET")},
  93. []HeaderField{}},
  94. }
  95. for _, tt := range tests {
  96. d := NewDecoder(4096, nil)
  97. hf, err := d.DecodeFull(tt.in)
  98. if err != nil {
  99. t.Errorf("%s: %v", tt.name, err)
  100. continue
  101. }
  102. if !reflect.DeepEqual(hf, tt.want) {
  103. t.Errorf("%s: Got %v; want %v", tt.name, hf, tt.want)
  104. }
  105. gotDynTab := d.dynTab.reverseCopy()
  106. if !reflect.DeepEqual(gotDynTab, tt.wantDynTab) {
  107. t.Errorf("%s: dynamic table after = %v; want %v", tt.name, gotDynTab, tt.wantDynTab)
  108. }
  109. }
  110. }
  111. func (dt *dynamicTable) reverseCopy() (hf []HeaderField) {
  112. hf = make([]HeaderField, len(dt.table.ents))
  113. for i := range hf {
  114. hf[i] = dt.table.ents[len(dt.table.ents)-1-i]
  115. }
  116. return
  117. }
  118. type encAndWant struct {
  119. enc []byte
  120. want []HeaderField
  121. wantDynTab []HeaderField
  122. wantDynSize uint32
  123. }
  124. // C.3 Request Examples without Huffman Coding
  125. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.3
  126. func TestDecodeC3_NoHuffman(t *testing.T) {
  127. testDecodeSeries(t, 4096, []encAndWant{
  128. {dehex("8286 8441 0f77 7777 2e65 7861 6d70 6c65 2e63 6f6d"),
  129. []HeaderField{
  130. pair(":method", "GET"),
  131. pair(":scheme", "http"),
  132. pair(":path", "/"),
  133. pair(":authority", "www.example.com"),
  134. },
  135. []HeaderField{
  136. pair(":authority", "www.example.com"),
  137. },
  138. 57,
  139. },
  140. {dehex("8286 84be 5808 6e6f 2d63 6163 6865"),
  141. []HeaderField{
  142. pair(":method", "GET"),
  143. pair(":scheme", "http"),
  144. pair(":path", "/"),
  145. pair(":authority", "www.example.com"),
  146. pair("cache-control", "no-cache"),
  147. },
  148. []HeaderField{
  149. pair("cache-control", "no-cache"),
  150. pair(":authority", "www.example.com"),
  151. },
  152. 110,
  153. },
  154. {dehex("8287 85bf 400a 6375 7374 6f6d 2d6b 6579 0c63 7573 746f 6d2d 7661 6c75 65"),
  155. []HeaderField{
  156. pair(":method", "GET"),
  157. pair(":scheme", "https"),
  158. pair(":path", "/index.html"),
  159. pair(":authority", "www.example.com"),
  160. pair("custom-key", "custom-value"),
  161. },
  162. []HeaderField{
  163. pair("custom-key", "custom-value"),
  164. pair("cache-control", "no-cache"),
  165. pair(":authority", "www.example.com"),
  166. },
  167. 164,
  168. },
  169. })
  170. }
  171. // C.4 Request Examples with Huffman Coding
  172. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.4
  173. func TestDecodeC4_Huffman(t *testing.T) {
  174. testDecodeSeries(t, 4096, []encAndWant{
  175. {dehex("8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ff"),
  176. []HeaderField{
  177. pair(":method", "GET"),
  178. pair(":scheme", "http"),
  179. pair(":path", "/"),
  180. pair(":authority", "www.example.com"),
  181. },
  182. []HeaderField{
  183. pair(":authority", "www.example.com"),
  184. },
  185. 57,
  186. },
  187. {dehex("8286 84be 5886 a8eb 1064 9cbf"),
  188. []HeaderField{
  189. pair(":method", "GET"),
  190. pair(":scheme", "http"),
  191. pair(":path", "/"),
  192. pair(":authority", "www.example.com"),
  193. pair("cache-control", "no-cache"),
  194. },
  195. []HeaderField{
  196. pair("cache-control", "no-cache"),
  197. pair(":authority", "www.example.com"),
  198. },
  199. 110,
  200. },
  201. {dehex("8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925 a849 e95b b8e8 b4bf"),
  202. []HeaderField{
  203. pair(":method", "GET"),
  204. pair(":scheme", "https"),
  205. pair(":path", "/index.html"),
  206. pair(":authority", "www.example.com"),
  207. pair("custom-key", "custom-value"),
  208. },
  209. []HeaderField{
  210. pair("custom-key", "custom-value"),
  211. pair("cache-control", "no-cache"),
  212. pair(":authority", "www.example.com"),
  213. },
  214. 164,
  215. },
  216. })
  217. }
  218. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.5
  219. // "This section shows several consecutive header lists, corresponding
  220. // to HTTP responses, on the same connection. The HTTP/2 setting
  221. // parameter SETTINGS_HEADER_TABLE_SIZE is set to the value of 256
  222. // octets, causing some evictions to occur."
  223. func TestDecodeC5_ResponsesNoHuff(t *testing.T) {
  224. testDecodeSeries(t, 256, []encAndWant{
  225. {dehex(`
  226. 4803 3330 3258 0770 7269 7661 7465 611d
  227. 4d6f 6e2c 2032 3120 4f63 7420 3230 3133
  228. 2032 303a 3133 3a32 3120 474d 546e 1768
  229. 7474 7073 3a2f 2f77 7777 2e65 7861 6d70
  230. 6c65 2e63 6f6d
  231. `),
  232. []HeaderField{
  233. pair(":status", "302"),
  234. pair("cache-control", "private"),
  235. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  236. pair("location", "https://www.example.com"),
  237. },
  238. []HeaderField{
  239. pair("location", "https://www.example.com"),
  240. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  241. pair("cache-control", "private"),
  242. pair(":status", "302"),
  243. },
  244. 222,
  245. },
  246. {dehex("4803 3330 37c1 c0bf"),
  247. []HeaderField{
  248. pair(":status", "307"),
  249. pair("cache-control", "private"),
  250. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  251. pair("location", "https://www.example.com"),
  252. },
  253. []HeaderField{
  254. pair(":status", "307"),
  255. pair("location", "https://www.example.com"),
  256. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  257. pair("cache-control", "private"),
  258. },
  259. 222,
  260. },
  261. {dehex(`
  262. 88c1 611d 4d6f 6e2c 2032 3120 4f63 7420
  263. 3230 3133 2032 303a 3133 3a32 3220 474d
  264. 54c0 5a04 677a 6970 7738 666f 6f3d 4153
  265. 444a 4b48 514b 425a 584f 5157 454f 5049
  266. 5541 5851 5745 4f49 553b 206d 6178 2d61
  267. 6765 3d33 3630 303b 2076 6572 7369 6f6e
  268. 3d31
  269. `),
  270. []HeaderField{
  271. pair(":status", "200"),
  272. pair("cache-control", "private"),
  273. pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
  274. pair("location", "https://www.example.com"),
  275. pair("content-encoding", "gzip"),
  276. pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
  277. },
  278. []HeaderField{
  279. pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
  280. pair("content-encoding", "gzip"),
  281. pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
  282. },
  283. 215,
  284. },
  285. })
  286. }
  287. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.6
  288. // "This section shows the same examples as the previous section, but
  289. // using Huffman encoding for the literal values. The HTTP/2 setting
  290. // parameter SETTINGS_HEADER_TABLE_SIZE is set to the value of 256
  291. // octets, causing some evictions to occur. The eviction mechanism
  292. // uses the length of the decoded literal values, so the same
  293. // evictions occurs as in the previous section."
  294. func TestDecodeC6_ResponsesHuffman(t *testing.T) {
  295. testDecodeSeries(t, 256, []encAndWant{
  296. {dehex(`
  297. 4882 6402 5885 aec3 771a 4b61 96d0 7abe
  298. 9410 54d4 44a8 2005 9504 0b81 66e0 82a6
  299. 2d1b ff6e 919d 29ad 1718 63c7 8f0b 97c8
  300. e9ae 82ae 43d3
  301. `),
  302. []HeaderField{
  303. pair(":status", "302"),
  304. pair("cache-control", "private"),
  305. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  306. pair("location", "https://www.example.com"),
  307. },
  308. []HeaderField{
  309. pair("location", "https://www.example.com"),
  310. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  311. pair("cache-control", "private"),
  312. pair(":status", "302"),
  313. },
  314. 222,
  315. },
  316. {dehex("4883 640e ffc1 c0bf"),
  317. []HeaderField{
  318. pair(":status", "307"),
  319. pair("cache-control", "private"),
  320. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  321. pair("location", "https://www.example.com"),
  322. },
  323. []HeaderField{
  324. pair(":status", "307"),
  325. pair("location", "https://www.example.com"),
  326. pair("date", "Mon, 21 Oct 2013 20:13:21 GMT"),
  327. pair("cache-control", "private"),
  328. },
  329. 222,
  330. },
  331. {dehex(`
  332. 88c1 6196 d07a be94 1054 d444 a820 0595
  333. 040b 8166 e084 a62d 1bff c05a 839b d9ab
  334. 77ad 94e7 821d d7f2 e6c7 b335 dfdf cd5b
  335. 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f
  336. 9587 3160 65c0 03ed 4ee5 b106 3d50 07
  337. `),
  338. []HeaderField{
  339. pair(":status", "200"),
  340. pair("cache-control", "private"),
  341. pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
  342. pair("location", "https://www.example.com"),
  343. pair("content-encoding", "gzip"),
  344. pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
  345. },
  346. []HeaderField{
  347. pair("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
  348. pair("content-encoding", "gzip"),
  349. pair("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
  350. },
  351. 215,
  352. },
  353. })
  354. }
  355. func testDecodeSeries(t *testing.T, size uint32, steps []encAndWant) {
  356. d := NewDecoder(size, nil)
  357. for i, step := range steps {
  358. hf, err := d.DecodeFull(step.enc)
  359. if err != nil {
  360. t.Fatalf("Error at step index %d: %v", i, err)
  361. }
  362. if !reflect.DeepEqual(hf, step.want) {
  363. t.Fatalf("At step index %d: Got headers %v; want %v", i, hf, step.want)
  364. }
  365. gotDynTab := d.dynTab.reverseCopy()
  366. if !reflect.DeepEqual(gotDynTab, step.wantDynTab) {
  367. t.Errorf("After step index %d, dynamic table = %v; want %v", i, gotDynTab, step.wantDynTab)
  368. }
  369. if d.dynTab.size != step.wantDynSize {
  370. t.Errorf("After step index %d, dynamic table size = %v; want %v", i, d.dynTab.size, step.wantDynSize)
  371. }
  372. }
  373. }
  374. func TestHuffmanDecodeExcessPadding(t *testing.T) {
  375. tests := [][]byte{
  376. {0xff}, // Padding Exceeds 7 bits
  377. {0x1f, 0xff}, // {"a", 1 byte excess padding}
  378. {0x1f, 0xff, 0xff}, // {"a", 2 byte excess padding}
  379. {0x1f, 0xff, 0xff, 0xff}, // {"a", 3 byte excess padding}
  380. {0xff, 0x9f, 0xff, 0xff, 0xff}, // {"a", 29 bit excess padding}
  381. {'R', 0xbc, '0', 0xff, 0xff, 0xff, 0xff}, // Padding ends on partial symbol.
  382. }
  383. for i, in := range tests {
  384. var buf bytes.Buffer
  385. if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
  386. t.Errorf("test-%d: decode(%q) = %v; want ErrInvalidHuffman", i, in, err)
  387. }
  388. }
  389. }
  390. func TestHuffmanDecodeEOS(t *testing.T) {
  391. in := []byte{0xff, 0xff, 0xff, 0xff, 0xfc} // {EOS, "?"}
  392. var buf bytes.Buffer
  393. if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
  394. t.Errorf("error = %v; want ErrInvalidHuffman", err)
  395. }
  396. }
  397. func TestHuffmanDecodeMaxLengthOnTrailingByte(t *testing.T) {
  398. in := []byte{0x00, 0x01} // {"0", "0", "0"}
  399. var buf bytes.Buffer
  400. if err := huffmanDecode(&buf, 2, in); err != ErrStringLength {
  401. t.Errorf("error = %v; want ErrStringLength", err)
  402. }
  403. }
  404. func TestHuffmanDecodeCorruptPadding(t *testing.T) {
  405. in := []byte{0x00}
  406. var buf bytes.Buffer
  407. if _, err := HuffmanDecode(&buf, in); err != ErrInvalidHuffman {
  408. t.Errorf("error = %v; want ErrInvalidHuffman", err)
  409. }
  410. }
  411. func TestHuffmanDecode(t *testing.T) {
  412. tests := []struct {
  413. inHex, want string
  414. }{
  415. {"f1e3 c2e5 f23a 6ba0 ab90 f4ff", "www.example.com"},
  416. {"a8eb 1064 9cbf", "no-cache"},
  417. {"25a8 49e9 5ba9 7d7f", "custom-key"},
  418. {"25a8 49e9 5bb8 e8b4 bf", "custom-value"},
  419. {"6402", "302"},
  420. {"aec3 771a 4b", "private"},
  421. {"d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff", "Mon, 21 Oct 2013 20:13:21 GMT"},
  422. {"9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3", "https://www.example.com"},
  423. {"9bd9 ab", "gzip"},
  424. {"94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07",
  425. "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
  426. }
  427. for i, tt := range tests {
  428. var buf bytes.Buffer
  429. in, err := hex.DecodeString(strings.Replace(tt.inHex, " ", "", -1))
  430. if err != nil {
  431. t.Errorf("%d. hex input error: %v", i, err)
  432. continue
  433. }
  434. if _, err := HuffmanDecode(&buf, in); err != nil {
  435. t.Errorf("%d. decode error: %v", i, err)
  436. continue
  437. }
  438. if got := buf.String(); tt.want != got {
  439. t.Errorf("%d. decode = %q; want %q", i, got, tt.want)
  440. }
  441. }
  442. }
  443. func TestAppendHuffmanString(t *testing.T) {
  444. tests := []struct {
  445. in, want string
  446. }{
  447. {"www.example.com", "f1e3 c2e5 f23a 6ba0 ab90 f4ff"},
  448. {"no-cache", "a8eb 1064 9cbf"},
  449. {"custom-key", "25a8 49e9 5ba9 7d7f"},
  450. {"custom-value", "25a8 49e9 5bb8 e8b4 bf"},
  451. {"302", "6402"},
  452. {"private", "aec3 771a 4b"},
  453. {"Mon, 21 Oct 2013 20:13:21 GMT", "d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff"},
  454. {"https://www.example.com", "9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3"},
  455. {"gzip", "9bd9 ab"},
  456. {"foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1",
  457. "94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07"},
  458. }
  459. for i, tt := range tests {
  460. buf := []byte{}
  461. want := strings.Replace(tt.want, " ", "", -1)
  462. buf = AppendHuffmanString(buf, tt.in)
  463. if got := hex.EncodeToString(buf); want != got {
  464. t.Errorf("%d. encode = %q; want %q", i, got, want)
  465. }
  466. }
  467. }
  468. func TestHuffmanMaxStrLen(t *testing.T) {
  469. const msg = "Some string"
  470. huff := AppendHuffmanString(nil, msg)
  471. testGood := func(max int) {
  472. var out bytes.Buffer
  473. if err := huffmanDecode(&out, max, huff); err != nil {
  474. t.Errorf("For maxLen=%d, unexpected error: %v", max, err)
  475. }
  476. if out.String() != msg {
  477. t.Errorf("For maxLen=%d, out = %q; want %q", max, out.String(), msg)
  478. }
  479. }
  480. testGood(0)
  481. testGood(len(msg))
  482. testGood(len(msg) + 1)
  483. var out bytes.Buffer
  484. if err := huffmanDecode(&out, len(msg)-1, huff); err != ErrStringLength {
  485. t.Errorf("err = %v; want ErrStringLength", err)
  486. }
  487. }
  488. func TestHuffmanRoundtripStress(t *testing.T) {
  489. const Len = 50 // of uncompressed string
  490. input := make([]byte, Len)
  491. var output bytes.Buffer
  492. var huff []byte
  493. n := 5000
  494. if testing.Short() {
  495. n = 100
  496. }
  497. seed := time.Now().UnixNano()
  498. t.Logf("Seed = %v", seed)
  499. src := rand.New(rand.NewSource(seed))
  500. var encSize int64
  501. for i := 0; i < n; i++ {
  502. for l := range input {
  503. input[l] = byte(src.Intn(256))
  504. }
  505. huff = AppendHuffmanString(huff[:0], string(input))
  506. encSize += int64(len(huff))
  507. output.Reset()
  508. if err := huffmanDecode(&output, 0, huff); err != nil {
  509. t.Errorf("Failed to decode %q -> %q -> error %v", input, huff, err)
  510. continue
  511. }
  512. if !bytes.Equal(output.Bytes(), input) {
  513. t.Errorf("Roundtrip failure on %q -> %q -> %q", input, huff, output.Bytes())
  514. }
  515. }
  516. t.Logf("Compressed size of original: %0.02f%% (%v -> %v)", 100*(float64(encSize)/(Len*float64(n))), Len*n, encSize)
  517. }
  518. func TestHuffmanDecodeFuzz(t *testing.T) {
  519. const Len = 50 // of compressed
  520. var buf, zbuf bytes.Buffer
  521. n := 5000
  522. if testing.Short() {
  523. n = 100
  524. }
  525. seed := time.Now().UnixNano()
  526. t.Logf("Seed = %v", seed)
  527. src := rand.New(rand.NewSource(seed))
  528. numFail := 0
  529. for i := 0; i < n; i++ {
  530. zbuf.Reset()
  531. if i == 0 {
  532. // Start with at least one invalid one.
  533. zbuf.WriteString("00\x91\xff\xff\xff\xff\xc8")
  534. } else {
  535. for l := 0; l < Len; l++ {
  536. zbuf.WriteByte(byte(src.Intn(256)))
  537. }
  538. }
  539. buf.Reset()
  540. if err := huffmanDecode(&buf, 0, zbuf.Bytes()); err != nil {
  541. if err == ErrInvalidHuffman {
  542. numFail++
  543. continue
  544. }
  545. t.Errorf("Failed to decode %q: %v", zbuf.Bytes(), err)
  546. continue
  547. }
  548. }
  549. t.Logf("%0.02f%% are invalid (%d / %d)", 100*float64(numFail)/float64(n), numFail, n)
  550. if numFail < 1 {
  551. t.Error("expected at least one invalid huffman encoding (test starts with one)")
  552. }
  553. }
  554. func TestReadVarInt(t *testing.T) {
  555. type res struct {
  556. i uint64
  557. consumed int
  558. err error
  559. }
  560. tests := []struct {
  561. n byte
  562. p []byte
  563. want res
  564. }{
  565. // Fits in a byte:
  566. {1, []byte{0}, res{0, 1, nil}},
  567. {2, []byte{2}, res{2, 1, nil}},
  568. {3, []byte{6}, res{6, 1, nil}},
  569. {4, []byte{14}, res{14, 1, nil}},
  570. {5, []byte{30}, res{30, 1, nil}},
  571. {6, []byte{62}, res{62, 1, nil}},
  572. {7, []byte{126}, res{126, 1, nil}},
  573. {8, []byte{254}, res{254, 1, nil}},
  574. // Doesn't fit in a byte:
  575. {1, []byte{1}, res{0, 0, errNeedMore}},
  576. {2, []byte{3}, res{0, 0, errNeedMore}},
  577. {3, []byte{7}, res{0, 0, errNeedMore}},
  578. {4, []byte{15}, res{0, 0, errNeedMore}},
  579. {5, []byte{31}, res{0, 0, errNeedMore}},
  580. {6, []byte{63}, res{0, 0, errNeedMore}},
  581. {7, []byte{127}, res{0, 0, errNeedMore}},
  582. {8, []byte{255}, res{0, 0, errNeedMore}},
  583. // Ignoring top bits:
  584. {5, []byte{255, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 111
  585. {5, []byte{159, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 100
  586. {5, []byte{191, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 101
  587. // Extra byte:
  588. {5, []byte{191, 154, 10, 2}, res{1337, 3, nil}}, // extra byte
  589. // Short a byte:
  590. {5, []byte{191, 154}, res{0, 0, errNeedMore}},
  591. // integer overflow:
  592. {1, []byte{255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, res{0, 0, errVarintOverflow}},
  593. }
  594. for _, tt := range tests {
  595. i, remain, err := readVarInt(tt.n, tt.p)
  596. consumed := len(tt.p) - len(remain)
  597. got := res{i, consumed, err}
  598. if got != tt.want {
  599. t.Errorf("readVarInt(%d, %v ~ %x) = %+v; want %+v", tt.n, tt.p, tt.p, got, tt.want)
  600. }
  601. }
  602. }
  603. // Fuzz crash, originally reported at https://github.com/bradfitz/http2/issues/56
  604. func TestHuffmanFuzzCrash(t *testing.T) {
  605. got, err := HuffmanDecodeToString([]byte("00\x91\xff\xff\xff\xff\xc8"))
  606. if got != "" {
  607. t.Errorf("Got %q; want empty string", got)
  608. }
  609. if err != ErrInvalidHuffman {
  610. t.Errorf("Err = %v; want ErrInvalidHuffman", err)
  611. }
  612. }
  613. func pair(name, value string) HeaderField {
  614. return HeaderField{Name: name, Value: value}
  615. }
  616. func dehex(s string) []byte {
  617. s = strings.Replace(s, " ", "", -1)
  618. s = strings.Replace(s, "\n", "", -1)
  619. b, err := hex.DecodeString(s)
  620. if err != nil {
  621. panic(err)
  622. }
  623. return b
  624. }
  625. func TestEmitEnabled(t *testing.T) {
  626. var buf bytes.Buffer
  627. enc := NewEncoder(&buf)
  628. enc.WriteField(HeaderField{Name: "foo", Value: "bar"})
  629. enc.WriteField(HeaderField{Name: "foo", Value: "bar"})
  630. numCallback := 0
  631. var dec *Decoder
  632. dec = NewDecoder(8<<20, func(HeaderField) {
  633. numCallback++
  634. dec.SetEmitEnabled(false)
  635. })
  636. if !dec.EmitEnabled() {
  637. t.Errorf("initial emit enabled = false; want true")
  638. }
  639. if _, err := dec.Write(buf.Bytes()); err != nil {
  640. t.Error(err)
  641. }
  642. if numCallback != 1 {
  643. t.Errorf("num callbacks = %d; want 1", numCallback)
  644. }
  645. if dec.EmitEnabled() {
  646. t.Errorf("emit enabled = true; want false")
  647. }
  648. }
  649. func TestSaveBufLimit(t *testing.T) {
  650. const maxStr = 1 << 10
  651. var got []HeaderField
  652. dec := NewDecoder(initialHeaderTableSize, func(hf HeaderField) {
  653. got = append(got, hf)
  654. })
  655. dec.SetMaxStringLength(maxStr)
  656. var frag []byte
  657. frag = append(frag[:0], encodeTypeByte(false, false))
  658. frag = appendVarInt(frag, 7, 3)
  659. frag = append(frag, "foo"...)
  660. frag = appendVarInt(frag, 7, 3)
  661. frag = append(frag, "bar"...)
  662. if _, err := dec.Write(frag); err != nil {
  663. t.Fatal(err)
  664. }
  665. want := []HeaderField{{Name: "foo", Value: "bar"}}
  666. if !reflect.DeepEqual(got, want) {
  667. t.Errorf("After small writes, got %v; want %v", got, want)
  668. }
  669. frag = append(frag[:0], encodeTypeByte(false, false))
  670. frag = appendVarInt(frag, 7, maxStr*3)
  671. frag = append(frag, make([]byte, maxStr*3)...)
  672. _, err := dec.Write(frag)
  673. if err != ErrStringLength {
  674. t.Fatalf("Write error = %v; want ErrStringLength", err)
  675. }
  676. }