hpack_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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) = %q; want %q", 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) = %q; want %q", got, want)
  131. }
  132. if got, want := at(len(staticTable)+2), (pair("foo", "bar")); got != want {
  133. t.Errorf("at(dyn 2) = %q; want %q", got, want)
  134. }
  135. if got, want := at(3), (pair(":method", "POST")); got != want {
  136. t.Errorf("at(3) = %q; want %q", got, want)
  137. }
  138. }
  139. func TestDynamicTableSizeEvict(t *testing.T) {
  140. d := NewDecoder(4096, nil)
  141. if want := uint32(0); d.dynTab.size != want {
  142. t.Fatalf("size = %d; want %d", d.dynTab.size, want)
  143. }
  144. add := d.dynTab.add
  145. add(pair("blake", "eats pizza"))
  146. if want := uint32(15 + 32); d.dynTab.size != want {
  147. t.Fatalf("after pizza, size = %d; want %d", d.dynTab.size, want)
  148. }
  149. add(pair("foo", "bar"))
  150. if want := uint32(15 + 32 + 6 + 32); d.dynTab.size != want {
  151. t.Fatalf("after foo bar, size = %d; want %d", d.dynTab.size, want)
  152. }
  153. d.dynTab.setMaxSize(15 + 32 + 1 /* slop */)
  154. if want := uint32(6 + 32); d.dynTab.size != want {
  155. t.Fatalf("after setMaxSize, size = %d; want %d", d.dynTab.size, want)
  156. }
  157. if got, want := d.mustAt(len(staticTable)+1), (pair("foo", "bar")); got != want {
  158. t.Errorf("at(dyn 1) = %q; want %q", got, want)
  159. }
  160. add(pair("long", strings.Repeat("x", 500)))
  161. if want := uint32(0); d.dynTab.size != want {
  162. t.Fatalf("after big one, size = %d; want %d", d.dynTab.size, want)
  163. }
  164. }
  165. func TestDecoderDecode(t *testing.T) {
  166. tests := []struct {
  167. name string
  168. in []byte
  169. want []HeaderField
  170. wantDynTab []HeaderField // newest entry first
  171. }{
  172. // C.2.1 Literal Header Field with Indexing
  173. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.1
  174. {"C.2.1", dehex("400a 6375 7374 6f6d 2d6b 6579 0d63 7573 746f 6d2d 6865 6164 6572"),
  175. []HeaderField{pair("custom-key", "custom-header")},
  176. []HeaderField{pair("custom-key", "custom-header")},
  177. },
  178. // C.2.2 Literal Header Field without Indexing
  179. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.2
  180. {"C.2.2", dehex("040c 2f73 616d 706c 652f 7061 7468"),
  181. []HeaderField{pair(":path", "/sample/path")},
  182. []HeaderField{}},
  183. // C.2.3 Literal Header Field never Indexed
  184. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.3
  185. {"C.2.3", dehex("1008 7061 7373 776f 7264 0673 6563 7265 74"),
  186. []HeaderField{{"password", "secret", true}},
  187. []HeaderField{}},
  188. // C.2.4 Indexed Header Field
  189. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.4
  190. {"C.2.4", []byte("\x82"),
  191. []HeaderField{pair(":method", "GET")},
  192. []HeaderField{}},
  193. }
  194. for _, tt := range tests {
  195. d := NewDecoder(4096, nil)
  196. hf, err := d.DecodeFull(tt.in)
  197. if err != nil {
  198. t.Errorf("%s: %v", tt.name, err)
  199. continue
  200. }
  201. if !reflect.DeepEqual(hf, tt.want) {
  202. t.Errorf("%s: Got %v; want %v", tt.name, hf, tt.want)
  203. }
  204. gotDynTab := make([]HeaderField, len(d.dynTab.ents))
  205. for i := range gotDynTab {
  206. gotDynTab[i] = d.dynTab.ents[len(d.dynTab.ents)-1-i]
  207. }
  208. if !reflect.DeepEqual(gotDynTab, tt.wantDynTab) {
  209. t.Errorf("%s: dynamic table after = %v; want %v", tt.name, gotDynTab, tt.wantDynTab)
  210. }
  211. }
  212. }
  213. type encAndWant struct {
  214. enc []byte
  215. want []HeaderField
  216. }
  217. // C.3 Request Examples without Huffman Coding
  218. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.3
  219. func TestDecodeC3_NoHuffman(t *testing.T) {
  220. testDecodeSeries(t, []encAndWant{
  221. {dehex("8286 8441 0f77 7777 2e65 7861 6d70 6c65 2e63 6f6d"),
  222. []HeaderField{
  223. pair(":method", "GET"),
  224. pair(":scheme", "http"),
  225. pair(":path", "/"),
  226. pair(":authority", "www.example.com"),
  227. },
  228. },
  229. {dehex("8286 84be 5808 6e6f 2d63 6163 6865"),
  230. []HeaderField{
  231. pair(":method", "GET"),
  232. pair(":scheme", "http"),
  233. pair(":path", "/"),
  234. pair(":authority", "www.example.com"),
  235. pair("cache-control", "no-cache"),
  236. },
  237. },
  238. {dehex("8287 85bf 400a 6375 7374 6f6d 2d6b 6579 0c63 7573 746f 6d2d 7661 6c75 65"),
  239. []HeaderField{
  240. pair(":method", "GET"),
  241. pair(":scheme", "https"),
  242. pair(":path", "/index.html"),
  243. pair(":authority", "www.example.com"),
  244. pair("custom-key", "custom-value"),
  245. },
  246. },
  247. })
  248. }
  249. // C.4 Request Examples with Huffman Coding
  250. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.4
  251. func TestDecodeC4_Huffman(t *testing.T) {
  252. testDecodeSeries(t, []encAndWant{
  253. {dehex("8286 8441 8cf1 e3c2 e5f2 3a6b a0ab 90f4 ff"),
  254. []HeaderField{
  255. pair(":method", "GET"),
  256. pair(":scheme", "http"),
  257. pair(":path", "/"),
  258. pair(":authority", "www.example.com"),
  259. },
  260. },
  261. {dehex("8286 84be 5886 a8eb 1064 9cbf"),
  262. []HeaderField{
  263. pair(":method", "GET"),
  264. pair(":scheme", "http"),
  265. pair(":path", "/"),
  266. pair(":authority", "www.example.com"),
  267. pair("cache-control", "no-cache"),
  268. },
  269. },
  270. {dehex("8287 85bf 4088 25a8 49e9 5ba9 7d7f 8925 a849 e95b b8e8 b4bf"),
  271. []HeaderField{
  272. pair(":method", "GET"),
  273. pair(":scheme", "https"),
  274. pair(":path", "/index.html"),
  275. pair(":authority", "www.example.com"),
  276. pair("custom-key", "custom-value"),
  277. },
  278. },
  279. })
  280. }
  281. func testDecodeSeries(t *testing.T, steps []encAndWant) {
  282. d := NewDecoder(4096, nil)
  283. for i, step := range steps {
  284. hf, err := d.DecodeFull(step.enc)
  285. if err != nil {
  286. t.Fatalf("Error at step index %d: %v", i, err)
  287. }
  288. if !reflect.DeepEqual(hf, step.want) {
  289. t.Fatalf("Error at step index %d: Got %v; want %v", i, hf, step.want)
  290. }
  291. }
  292. }
  293. func TestHuffmanDecode(t *testing.T) {
  294. tests := []struct {
  295. inHex, want string
  296. }{
  297. {"f1e3 c2e5 f23a 6ba0 ab90 f4ff", "www.example.com"},
  298. {"a8eb 1064 9cbf", "no-cache"},
  299. {"25a8 49e9 5ba9 7d7f", "custom-key"},
  300. {"25a8 49e9 5bb8 e8b4 bf", "custom-value"},
  301. {"6402", "302"},
  302. {"aec3 771a 4b", "private"},
  303. {"d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff", "Mon, 21 Oct 2013 20:13:21 GMT"},
  304. {"9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3", "https://www.example.com"},
  305. {"9bd9 ab", "gzip"},
  306. {"94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07",
  307. "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
  308. }
  309. for i, tt := range tests {
  310. var buf bytes.Buffer
  311. in, err := hex.DecodeString(strings.Replace(tt.inHex, " ", "", -1))
  312. if err != nil {
  313. t.Errorf("%d. hex input error: %v", i, err)
  314. continue
  315. }
  316. if _, err := HuffmanDecode(&buf, in); err != nil {
  317. t.Errorf("%d. decode error: %v", i, err)
  318. continue
  319. }
  320. if got := buf.String(); tt.want != got {
  321. t.Errorf("%d. decode = %q; want %q", i, got, tt.want)
  322. }
  323. }
  324. }
  325. func TestReadVarInt(t *testing.T) {
  326. type res struct {
  327. i uint64
  328. consumed int
  329. err error
  330. }
  331. tests := []struct {
  332. n byte
  333. p []byte
  334. want res
  335. }{
  336. // Fits in a byte:
  337. {1, []byte{0}, res{0, 1, nil}},
  338. {2, []byte{2}, res{2, 1, nil}},
  339. {3, []byte{6}, res{6, 1, nil}},
  340. {4, []byte{14}, res{14, 1, nil}},
  341. {5, []byte{30}, res{30, 1, nil}},
  342. {6, []byte{62}, res{62, 1, nil}},
  343. {7, []byte{126}, res{126, 1, nil}},
  344. {8, []byte{254}, res{254, 1, nil}},
  345. // Doesn't fit in a byte:
  346. {1, []byte{1}, res{0, 0, errNeedMore}},
  347. {2, []byte{3}, res{0, 0, errNeedMore}},
  348. {3, []byte{7}, res{0, 0, errNeedMore}},
  349. {4, []byte{15}, res{0, 0, errNeedMore}},
  350. {5, []byte{31}, res{0, 0, errNeedMore}},
  351. {6, []byte{63}, res{0, 0, errNeedMore}},
  352. {7, []byte{127}, res{0, 0, errNeedMore}},
  353. {8, []byte{255}, res{0, 0, errNeedMore}},
  354. // Ignoring top bits:
  355. {5, []byte{255, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 111
  356. {5, []byte{159, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 100
  357. {5, []byte{191, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 101
  358. // Extra byte:
  359. {5, []byte{191, 154, 10, 2}, res{1337, 3, nil}}, // extra byte
  360. // Short a byte:
  361. {5, []byte{191, 154}, res{0, 0, errNeedMore}},
  362. // integer overflow:
  363. {1, []byte{255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, res{0, 0, errVarintOverflow}},
  364. }
  365. for _, tt := range tests {
  366. i, remain, err := readVarInt(tt.n, tt.p)
  367. consumed := len(tt.p) - len(remain)
  368. got := res{i, consumed, err}
  369. if got != tt.want {
  370. t.Errorf("readVarInt(%d, %v ~ %x) = %+v; want %+v", tt.n, tt.p, tt.p, got, tt.want)
  371. }
  372. }
  373. }
  374. func dehex(s string) []byte {
  375. s = strings.Replace(s, " ", "", -1)
  376. b, err := hex.DecodeString(s)
  377. if err != nil {
  378. panic(err)
  379. }
  380. return b
  381. }