hpack_test.go 21 KB

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