hpack_test.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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(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), (HeaderField{":method", "GET"}); got != want {
  125. t.Errorf("at(2) = %q; want %q", got, want)
  126. }
  127. d.dynTab.add(HeaderField{"foo", "bar"})
  128. d.dynTab.add(HeaderField{"blake", "miz"})
  129. if got, want := at(len(staticTable)+1), (HeaderField{"blake", "miz"}); got != want {
  130. t.Errorf("at(dyn 1) = %q; want %q", got, want)
  131. }
  132. if got, want := at(len(staticTable)+2), (HeaderField{"foo", "bar"}); got != want {
  133. t.Errorf("at(dyn 2) = %q; want %q", got, want)
  134. }
  135. if got, want := at(3), (HeaderField{":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(HeaderField{"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(HeaderField{"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), (HeaderField{"foo", "bar"}); got != want {
  158. t.Errorf("at(dyn 1) = %q; want %q", got, want)
  159. }
  160. add(HeaderField{"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. d := NewDecoder(4096, nil)
  167. // Indexed Header Field
  168. // http://http2.github.io/http2-spec/compression.html#rfc.section.C.2.4
  169. hf, err := d.Decode([]byte("\x82"))
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. want := []HeaderField{{":method", "GET"}}
  174. if !reflect.DeepEqual(hf, want) {
  175. t.Errorf("Got %v; want %v", hf, want)
  176. }
  177. }
  178. func TestHuffmanDecode(t *testing.T) {
  179. tests := []struct {
  180. inHex, want string
  181. }{
  182. {"f1e3 c2e5 f23a 6ba0 ab90 f4ff", "www.example.com"},
  183. {"a8eb 1064 9cbf", "no-cache"},
  184. {"25a8 49e9 5ba9 7d7f", "custom-key"},
  185. {"25a8 49e9 5bb8 e8b4 bf", "custom-value"},
  186. {"6402", "302"},
  187. {"aec3 771a 4b", "private"},
  188. {"d07a be94 1054 d444 a820 0595 040b 8166 e082 a62d 1bff", "Mon, 21 Oct 2013 20:13:21 GMT"},
  189. {"9d29 ad17 1863 c78f 0b97 c8e9 ae82 ae43 d3", "https://www.example.com"},
  190. {"9bd9 ab", "gzip"},
  191. {"94e7 821d d7f2 e6c7 b335 dfdf cd5b 3960 d5af 2708 7f36 72c1 ab27 0fb5 291f 9587 3160 65c0 03ed 4ee5 b106 3d50 07",
  192. "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"},
  193. }
  194. for i, tt := range tests {
  195. var buf bytes.Buffer
  196. in, err := hex.DecodeString(strings.Replace(tt.inHex, " ", "", -1))
  197. if err != nil {
  198. t.Errorf("%d. hex input error: %v", i, err)
  199. continue
  200. }
  201. if _, err := HuffmanDecode(&buf, in); err != nil {
  202. t.Errorf("%d. decode error: %v", i, err)
  203. continue
  204. }
  205. if got := buf.String(); tt.want != got {
  206. t.Errorf("%d. decode = %q; want %q", i, got, tt.want)
  207. }
  208. }
  209. }
  210. func TestReadVarInt(t *testing.T) {
  211. type res struct {
  212. i uint64
  213. consumed int
  214. err error
  215. }
  216. tests := []struct {
  217. n byte
  218. p []byte
  219. want res
  220. }{
  221. // Fits in a byte:
  222. {1, []byte{0}, res{0, 1, nil}},
  223. {2, []byte{2}, res{2, 1, nil}},
  224. {3, []byte{6}, res{6, 1, nil}},
  225. {4, []byte{14}, res{14, 1, nil}},
  226. {5, []byte{30}, res{30, 1, nil}},
  227. {6, []byte{62}, res{62, 1, nil}},
  228. {7, []byte{126}, res{126, 1, nil}},
  229. {8, []byte{254}, res{254, 1, nil}},
  230. // Doesn't fit in a byte:
  231. {1, []byte{1}, res{0, 0, nil}},
  232. {2, []byte{3}, res{0, 0, nil}},
  233. {3, []byte{7}, res{0, 0, nil}},
  234. {4, []byte{15}, res{0, 0, nil}},
  235. {5, []byte{31}, res{0, 0, nil}},
  236. {6, []byte{63}, res{0, 0, nil}},
  237. {7, []byte{127}, res{0, 0, nil}},
  238. {8, []byte{255}, res{0, 0, nil}},
  239. // Ignoring top bits:
  240. {5, []byte{255, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 111
  241. {5, []byte{159, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 100
  242. {5, []byte{191, 154, 10}, res{1337, 3, nil}}, // high dummy three bits: 101
  243. // Extra byte:
  244. {5, []byte{191, 154, 10, 2}, res{1337, 3, nil}}, // extra byte
  245. // Short a byte:
  246. {5, []byte{191, 154}, res{0, 0, nil}},
  247. // integer overflow:
  248. {1, []byte{255, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128}, res{0, 0, errVarintOverflow}},
  249. }
  250. for _, tt := range tests {
  251. i, consumed, err := readVarInt(tt.n, tt.p)
  252. got := res{i, consumed, err}
  253. if got != tt.want {
  254. t.Errorf("readVarInt(%d, %v ~ %x) = %+v; want %+v", tt.n, tt.p, tt.p, got, tt.want)
  255. }
  256. }
  257. }