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.
17 func TestEncoderTableSizeUpdate(t *testing.T) {
22 // Should emit 2 table size updates (2048 and 4096)
23 {2048, 4096, "3fe10f 3fe11f 82"},
25 // Should emit 1 table size update (2048)
26 {16384, 2048, "3fe10f 82"},
28 for _, tt := range tests {
31 e.SetMaxDynamicTableSize(tt.size1)
32 e.SetMaxDynamicTableSize(tt.size2)
33 if err := e.WriteField(pair(":method", "GET")); err != nil {
36 want := removeSpace(tt.wantHex)
37 if got := hex.EncodeToString(buf.Bytes()); got != want {
38 t.Errorf("e.SetDynamicTableSize %v, %v = %q; want %q", tt.size1, tt.size2, got, want)
43 func TestEncoderWriteField(t *testing.T) {
47 d := NewDecoder(4<<10, func(f HeaderField) {
55 pair(":method", "GET"),
56 pair(":scheme", "http"),
58 pair(":authority", "www.example.com"),
61 pair(":method", "GET"),
62 pair(":scheme", "http"),
64 pair(":authority", "www.example.com"),
65 pair("cache-control", "no-cache"),
68 pair(":method", "GET"),
69 pair(":scheme", "https"),
70 pair(":path", "/index.html"),
71 pair(":authority", "www.example.com"),
72 pair("custom-key", "custom-value"),
75 for i, tt := range tests {
78 for _, hf := range tt.hdrs {
79 if err := e.WriteField(hf); err != nil {
83 _, err := d.Write(buf.Bytes())
85 t.Errorf("%d. Decoder Write = %v", i, err)
87 if !reflect.DeepEqual(got, tt.hdrs) {
88 t.Errorf("%d. Decoded %+v; want %+v", i, got, tt.hdrs)
93 func TestEncoderSearchTable(t *testing.T) {
96 e.dynTab.add(pair("foo", "bar"))
97 e.dynTab.add(pair("blake", "miz"))
98 e.dynTab.add(pair(":method", "GET"))
105 // Name and Value match
106 {pair("foo", "bar"), uint64(staticTable.len()) + 3, true},
107 {pair("blake", "miz"), uint64(staticTable.len()) + 2, true},
108 {pair(":method", "GET"), 2, true},
110 // Only name match because Sensitive == true. This is allowed to match
111 // any ":method" entry. The current implementation uses the last entry
112 // added in newStaticTable.
113 {HeaderField{":method", "GET", true}, 3, false},
116 {pair("foo", "..."), uint64(staticTable.len()) + 3, false},
117 {pair("blake", "..."), uint64(staticTable.len()) + 2, false},
118 // As before, this is allowed to match any ":method" entry.
119 {pair(":method", "..."), 3, false},
122 {pair("foo-", "bar"), 0, false},
124 for _, tt := range tests {
125 if gotI, gotMatch := e.searchTable(tt.hf); gotI != tt.wantI || gotMatch != tt.wantMatch {
126 t.Errorf("d.search(%+v) = %v, %v; want %v, %v", tt.hf, gotI, gotMatch, tt.wantI, tt.wantMatch)
131 func TestAppendVarInt(t *testing.T) {
144 {7, 126, []byte{126}},
145 {8, 254, []byte{254}},
148 {5, 1337, []byte{31, 154, 10}},
150 for _, tt := range tests {
151 got := appendVarInt(nil, tt.n, tt.i)
152 if !bytes.Equal(got, tt.want) {
153 t.Errorf("appendVarInt(nil, %v, %v) = %v; want %v", tt.n, tt.i, got, tt.want)
158 func TestAppendHpackString(t *testing.T) {
163 {"www.example.com", "8c f1e3 c2e5 f23a 6ba0 ab90 f4ff"},
165 // Not Huffman encoded
171 for _, tt := range tests {
172 want := removeSpace(tt.wantHex)
173 buf := appendHpackString(nil, tt.s)
174 if got := hex.EncodeToString(buf); want != got {
175 t.Errorf("appendHpackString(nil, %q) = %q; want %q", tt.s, got, want)
180 func TestAppendIndexed(t *testing.T) {
193 for _, tt := range tests {
194 want := removeSpace(tt.wantHex)
195 buf := appendIndexed(nil, tt.i)
196 if got := hex.EncodeToString(buf); want != got {
197 t.Errorf("appendIndex(nil, %v) = %q; want %q", tt.i, got, want)
202 func TestAppendNewName(t *testing.T) {
208 // Incremental indexing
209 {HeaderField{"custom-key", "custom-value", false}, true, "40 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
212 {HeaderField{"custom-key", "custom-value", false}, false, "00 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
215 {HeaderField{"custom-key", "custom-value", true}, true, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
216 {HeaderField{"custom-key", "custom-value", true}, false, "10 88 25a8 49e9 5ba9 7d7f 89 25a8 49e9 5bb8 e8b4 bf"},
218 for _, tt := range tests {
219 want := removeSpace(tt.wantHex)
220 buf := appendNewName(nil, tt.f, tt.indexing)
221 if got := hex.EncodeToString(buf); want != got {
222 t.Errorf("appendNewName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
227 func TestAppendIndexedName(t *testing.T) {
234 // Incremental indexing
235 {HeaderField{":status", "302", false}, 8, true, "48 82 6402"},
238 {HeaderField{":status", "302", false}, 8, false, "08 82 6402"},
241 {HeaderField{":status", "302", true}, 8, true, "18 82 6402"},
242 {HeaderField{":status", "302", true}, 8, false, "18 82 6402"},
244 for _, tt := range tests {
245 want := removeSpace(tt.wantHex)
246 buf := appendIndexedName(nil, tt.f, tt.i, tt.indexing)
247 if got := hex.EncodeToString(buf); want != got {
248 t.Errorf("appendIndexedName(nil, %+v, %v) = %q; want %q", tt.f, tt.indexing, got, want)
253 func TestAppendTableSize(t *testing.T) {
265 for _, tt := range tests {
266 want := removeSpace(tt.wantHex)
267 buf := appendTableSize(nil, tt.i)
268 if got := hex.EncodeToString(buf); want != got {
269 t.Errorf("appendTableSize(nil, %v) = %q; want %q", tt.i, got, want)
274 func TestEncoderSetMaxDynamicTableSize(t *testing.T) {
276 e := NewEncoder(&buf)
283 // Set new table size to 2048
284 {2048, true, 2048, 2048},
286 // Set new table size to 16384, but still limited to
288 {16384, true, 2048, 4096},
290 for _, tt := range tests {
291 e.SetMaxDynamicTableSize(tt.v)
292 if got := e.tableSizeUpdate; tt.wantUpdate != got {
293 t.Errorf("e.tableSizeUpdate = %v; want %v", got, tt.wantUpdate)
295 if got := e.minSize; tt.wantMinSize != got {
296 t.Errorf("e.minSize = %v; want %v", got, tt.wantMinSize)
298 if got := e.dynTab.maxSize; tt.wantMaxSize != got {
299 t.Errorf("e.maxSize = %v; want %v", got, tt.wantMaxSize)
304 func TestEncoderSetMaxDynamicTableSizeLimit(t *testing.T) {
306 // 4095 < initialHeaderTableSize means maxSize is truncated to
308 e.SetMaxDynamicTableSizeLimit(4095)
309 if got, want := e.dynTab.maxSize, uint32(4095); got != want {
310 t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
312 if got, want := e.maxSizeLimit, uint32(4095); got != want {
313 t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
315 if got, want := e.tableSizeUpdate, true; got != want {
316 t.Errorf("e.tableSizeUpdate = %v; want %v", got, want)
318 // maxSize will be truncated to maxSizeLimit
319 e.SetMaxDynamicTableSize(16384)
320 if got, want := e.dynTab.maxSize, uint32(4095); got != want {
321 t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
323 // 8192 > current maxSizeLimit, so maxSize does not change.
324 e.SetMaxDynamicTableSizeLimit(8192)
325 if got, want := e.dynTab.maxSize, uint32(4095); got != want {
326 t.Errorf("e.dynTab.maxSize = %v; want %v", got, want)
328 if got, want := e.maxSizeLimit, uint32(8192); got != want {
329 t.Errorf("e.maxSizeLimit = %v; want %v", got, want)
333 func removeSpace(s string) string {
334 return strings.Replace(s, " ", "", -1)
337 func BenchmarkEncoderSearchTable(b *testing.B) {
340 // A sample of possible header fields.
341 // This is not based on any actual data from HTTP/2 traces.
342 var possible []HeaderField
343 for _, f := range staticTable.ents {
345 possible = append(possible, f)
348 // Generate 5 random values, except for cookie and set-cookie,
349 // which we know can have many values in practice.
351 if f.Name == "cookie" || f.Name == "set-cookie" {
354 for i := 0; i < num; i++ {
355 f.Value = fmt.Sprintf("%s-%d", f.Name, i)
356 possible = append(possible, f)
359 for k := 0; k < 10; k++ {
361 Name: fmt.Sprintf("x-header-%d", k),
362 Sensitive: rand.Int()%2 == 0,
364 for i := 0; i < 5; i++ {
365 f.Value = fmt.Sprintf("%s-%d", f.Name, i)
366 possible = append(possible, f)
370 // Add a random sample to the dynamic table. This very loosely simulates
371 // a history of 100 requests with 20 header fields per request.
372 for r := 0; r < 100*20; r++ {
373 f := possible[rand.Int31n(int32(len(possible)))]
374 // Skip if this is in the staticTable verbatim.
375 if _, has := staticTable.search(f); !has {
381 for n := 0; n < b.N; n++ {
382 for _, f := range possible {