encode_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. "reflect"
  9. "strings"
  10. "testing"
  11. )
  12. func TestEncoderTableSizeUpdate(t *testing.T) {
  13. tests := []struct {
  14. size1, size2 uint32
  15. wantHex string
  16. }{
  17. // Should emit 2 table size updates (2048 and 4096)
  18. {2048, 4096, "3fe10f 3fe11f 82"},
  19. // Should emit 1 table size update (2048)
  20. {16384, 2048, "3fe10f 82"},
  21. }
  22. for _, tt := range tests {
  23. var buf bytes.Buffer
  24. e := NewEncoder(&buf)
  25. e.SetMaxDynamicTableSize(tt.size1)
  26. e.SetMaxDynamicTableSize(tt.size2)
  27. if err := e.WriteField(pair(":method", "GET")); err != nil {
  28. t.Fatal(err)
  29. }
  30. want := removeSpace(tt.wantHex)
  31. if got := hex.EncodeToString(buf.Bytes()); got != want {
  32. t.Errorf("e.SetDynamicTableSize %v, %v = %q; want %q", tt.size1, tt.size2, got, want)
  33. }
  34. }
  35. }
  36. func TestEncoderWriteField(t *testing.T) {
  37. var buf bytes.Buffer
  38. e := NewEncoder(&buf)
  39. var got []HeaderField
  40. d := NewDecoder(4<<10, func(f HeaderField) {
  41. got = append(got, f)
  42. })
  43. tests := []struct {
  44. hdrs []HeaderField
  45. }{
  46. {[]HeaderField{
  47. pair(":method", "GET"),
  48. pair(":scheme", "http"),
  49. pair(":path", "/"),
  50. pair(":authority", "www.example.com"),
  51. }},
  52. {[]HeaderField{
  53. pair(":method", "GET"),
  54. pair(":scheme", "http"),
  55. pair(":path", "/"),
  56. pair(":authority", "www.example.com"),
  57. pair("cache-control", "no-cache"),
  58. }},
  59. {[]HeaderField{
  60. pair(":method", "GET"),
  61. pair(":scheme", "https"),
  62. pair(":path", "/index.html"),
  63. pair(":authority", "www.example.com"),
  64. pair("custom-key", "custom-value"),
  65. }},
  66. }
  67. for i, tt := range tests {
  68. buf.Reset()
  69. got = got[:0]
  70. for _, hf := range tt.hdrs {
  71. if err := e.WriteField(hf); err != nil {
  72. t.Fatal(err)
  73. }
  74. }
  75. _, err := d.Write(buf.Bytes())
  76. if err != nil {
  77. t.Errorf("%d. Decoder Write = %v", i, err)
  78. }
  79. if !reflect.DeepEqual(got, tt.hdrs) {
  80. t.Errorf("%d. Decoded %+v; want %+v", i, got, tt.hdrs)
  81. }
  82. }
  83. }
  84. func TestEncoderSearchTable(t *testing.T) {
  85. e := NewEncoder(nil)
  86. e.dynTab.add(pair("foo", "bar"))
  87. e.dynTab.add(pair("blake", "miz"))
  88. e.dynTab.add(pair(":method", "GET"))
  89. tests := []struct {
  90. hf HeaderField
  91. wantI uint64
  92. wantMatch bool
  93. }{
  94. // Name and Value match
  95. {pair("foo", "bar"), uint64(len(staticTable) + 3), true},
  96. {pair("blake", "miz"), uint64(len(staticTable) + 2), true},
  97. {pair(":method", "GET"), 2, true},
  98. // Only name match because Sensitive == true
  99. {HeaderField{":method", "GET", true}, 2, false},
  100. // Only Name matches
  101. {pair("foo", "..."), uint64(len(staticTable) + 3), false},
  102. {pair("blake", "..."), uint64(len(staticTable) + 2), false},
  103. {pair(":method", "..."), 2, false},
  104. // None match
  105. {pair("foo-", "bar"), 0, false},
  106. }
  107. for _, tt := range tests {
  108. if gotI, gotMatch := e.searchTable(tt.hf); gotI != tt.wantI || gotMatch != tt.wantMatch {
  109. t.Errorf("d.search(%+v) = %v, %v; want %v, %v", tt.hf, gotI, gotMatch, tt.wantI, tt.wantMatch)
  110. }
  111. }
  112. }
  113. func TestAppendVarInt(t *testing.T) {
  114. tests := []struct {
  115. n byte
  116. i uint64
  117. want []byte
  118. }{
  119. // Fits in a byte:
  120. {1, 0, []byte{0}},
  121. {2, 2, []byte{2}},
  122. {3, 6, []byte{6}},
  123. {4, 14, []byte{14}},
  124. {5, 30, []byte{30}},
  125. {6, 62, []byte{62}},
  126. {7, 126, []byte{126}},
  127. {8, 254, []byte{254}},
  128. // Multiple bytes:
  129. {5, 1337, []byte{31, 154, 10}},
  130. }
  131. for _, tt := range tests {
  132. got := appendVarInt(nil, tt.n, tt.i)
  133. if !bytes.Equal(got, tt.want) {
  134. t.Errorf("appendVarInt(nil, %v, %v) = %v; want %v", tt.n, tt.i, got, tt.want)
  135. }
  136. }
  137. }
  138. func TestAppendHpackString(t *testing.T) {
  139. tests := []struct {
  140. s, wantHex string
  141. }{
  142. // Huffman encoded
  143. {"www.example.com", "8c f1e3 c2e5 f23a 6ba0 ab90 f4ff"},
  144. // Not Huffman encoded
  145. {"a", "01 61"},
  146. // zero length
  147. {"", "00"},
  148. }
  149. for _, tt := range tests {
  150. want := removeSpace(tt.wantHex)
  151. buf := appendHpackString(nil, tt.s)
  152. if got := hex.EncodeToString(buf); want != got {
  153. t.Errorf("appendHpackString(nil, %q) = %q; want %q", tt.s, got, want)
  154. }
  155. }
  156. }
  157. func TestAppendIndexed(t *testing.T) {
  158. tests := []struct {
  159. i uint64
  160. wantHex string
  161. }{
  162. // 1 byte
  163. {1, "81"},
  164. {126, "fe"},
  165. // 2 bytes
  166. {127, "ff00"},
  167. {128, "ff01"},
  168. }
  169. for _, tt := range tests {
  170. want := removeSpace(tt.wantHex)
  171. buf := appendIndexed(nil, tt.i)
  172. if got := hex.EncodeToString(buf); want != got {
  173. t.Errorf("appendIndex(nil, %v) = %q; want %q", tt.i, got, want)
  174. }
  175. }
  176. }
  177. func TestAppendNewName(t *testing.T) {
  178. tests := []struct {
  179. f HeaderField
  180. indexing bool
  181. wantHex string
  182. }{
  183. // Incremental indexing
  184. {HeaderField{"custom-key", "custom-value", false}, true, "40 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
  185. // Without indexing
  186. {HeaderField{"custom-key", "custom-value", false}, false, "00 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
  187. // Never indexed
  188. {HeaderField{"custom-key", "custom-value", true}, true, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
  189. {HeaderField{"custom-key", "custom-value", true}, false, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
  190. }
  191. for _, tt := range tests {
  192. want := removeSpace(tt.wantHex)
  193. buf := appendNewName(nil, tt.f, tt.indexing)
  194. if got := hex.EncodeToString(buf); want != got {
  195. t.Errorf("appendNewName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
  196. }
  197. }
  198. }
  199. func TestAppendIndexedName(t *testing.T) {
  200. tests := []struct {
  201. f HeaderField
  202. i uint64
  203. indexing bool
  204. wantHex string
  205. }{
  206. // Incremental indexing
  207. {HeaderField{":status", "302", false}, 8, true, "48 82 6402"},
  208. // Without indexing
  209. {HeaderField{":status", "302", false}, 8, false, "08 82 6402"},
  210. // Never indexed
  211. {HeaderField{":status", "302", true}, 8, true, "18 82 6402"},
  212. {HeaderField{":status", "302", true}, 8, false, "18 82 6402"},
  213. }
  214. for _, tt := range tests {
  215. want := removeSpace(tt.wantHex)
  216. buf := appendIndexedName(nil, tt.f, tt.i, tt.indexing)
  217. if got := hex.EncodeToString(buf); want != got {
  218. t.Errorf("appendIndexedName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
  219. }
  220. }
  221. }
  222. func TestAppendTableSize(t *testing.T) {
  223. tests := []struct {
  224. i uint32
  225. wantHex string
  226. }{
  227. // Fits into 1 byte
  228. {30, "3e"},
  229. // Extra byte
  230. {31, "3f00"},
  231. {32, "3f01"},
  232. }
  233. for _, tt := range tests {
  234. want := removeSpace(tt.wantHex)
  235. buf := appendTableSize(nil, tt.i)
  236. if got := hex.EncodeToString(buf); want != got {
  237. t.Errorf("appendTableSize(nil, %v) = %q; want %q", tt.i, got, want)
  238. }
  239. }
  240. }
  241. func TestEncoderSetMaxDynamicTableSize(t *testing.T) {
  242. var buf bytes.Buffer
  243. e := NewEncoder(&buf)
  244. tests := []struct {
  245. v uint32
  246. wantUpdate bool
  247. wantMinSize uint32
  248. wantMaxSize uint32
  249. }{
  250. // Set new table size to 2048
  251. {2048, true, 2048, 2048},
  252. // Set new table size to 16384, but still limited to
  253. // 4096
  254. {16384, true, 2048, 4096},
  255. }
  256. for _, tt := range tests {
  257. e.SetMaxDynamicTableSize(tt.v)
  258. if got := e.tableSizeUpdate; tt.wantUpdate != got {
  259. t.Errorf("e.tableSizeUpdate = %v; want %v", got, tt.wantUpdate)
  260. }
  261. if got := e.minSize; tt.wantMinSize != got {
  262. t.Errorf("e.minSize = %v; want %v", got, tt.wantMinSize)
  263. }
  264. if got := e.dynTab.maxSize; tt.wantMaxSize != got {
  265. t.Errorf("e.maxSize = %v; want %v", got, tt.wantMaxSize)
  266. }
  267. }
  268. }
  269. func TestEncoderSetMaxDynamicTableSizeLimit(t *testing.T) {
  270. e := NewEncoder(nil)
  271. // 4095 < initialHeaderTableSize means maxSize is truncated to
  272. // 4095.
  273. e.SetMaxDynamicTableSizeLimit(4095)
  274. if got, want := e.dynTab.maxSize, uint32(4095); got != want {
  275. t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
  276. }
  277. if got, want := e.maxSizeLimit, uint32(4095); got != want {
  278. t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
  279. }
  280. if got, want := e.tableSizeUpdate, true; got != want {
  281. t.Errorf("e.tableSizeUpdate = %v; want %v", got, want)
  282. }
  283. // maxSize will be truncated to maxSizeLimit
  284. e.SetMaxDynamicTableSize(16384)
  285. if got, want := e.dynTab.maxSize, uint32(4095); got != want {
  286. t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
  287. }
  288. // 8192 > current maxSizeLimit, so maxSize does not change.
  289. e.SetMaxDynamicTableSizeLimit(8192)
  290. if got, want := e.dynTab.maxSize, uint32(4095); got != want {
  291. t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
  292. }
  293. if got, want := e.maxSizeLimit, uint32(8192); got != want {
  294. t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
  295. }
  296. }
  297. func removeSpace(s string) string {
  298. return strings.Replace(s, " ", "", -1)
  299. }