The reconcile package is used for DOM reconcilation in Isomorphic Go web applications.

encode_test.go 9.9KB

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