You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

189 lines
8.0 KiB

  1. // Copyright 2017 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. "bufio"
  7. "regexp"
  8. "strconv"
  9. "strings"
  10. "testing"
  11. )
  12. func TestHeaderFieldTable(t *testing.T) {
  13. table := &headerFieldTable{}
  14. table.init()
  15. table.addEntry(pair("key1", "value1-1"))
  16. table.addEntry(pair("key2", "value2-1"))
  17. table.addEntry(pair("key1", "value1-2"))
  18. table.addEntry(pair("key3", "value3-1"))
  19. table.addEntry(pair("key4", "value4-1"))
  20. table.addEntry(pair("key2", "value2-2"))
  21. // Tests will be run twice: once before evicting anything, and
  22. // again after evicting the three oldest entries.
  23. tests := []struct {
  24. f HeaderField
  25. beforeWantStaticI uint64
  26. beforeWantMatch bool
  27. afterWantStaticI uint64
  28. afterWantMatch bool
  29. }{
  30. {HeaderField{"key1", "value1-1", false}, 1, true, 0, false},
  31. {HeaderField{"key1", "value1-2", false}, 3, true, 0, false},
  32. {HeaderField{"key1", "value1-3", false}, 3, false, 0, false},
  33. {HeaderField{"key2", "value2-1", false}, 2, true, 3, false},
  34. {HeaderField{"key2", "value2-2", false}, 6, true, 3, true},
  35. {HeaderField{"key2", "value2-3", false}, 6, false, 3, false},
  36. {HeaderField{"key4", "value4-1", false}, 5, true, 2, true},
  37. // Name match only, because sensitive.
  38. {HeaderField{"key4", "value4-1", true}, 5, false, 2, false},
  39. // Key not found.
  40. {HeaderField{"key5", "value5-x", false}, 0, false, 0, false},
  41. }
  42. staticToDynamic := func(i uint64) uint64 {
  43. if i == 0 {
  44. return 0
  45. }
  46. return uint64(table.len()) - i + 1 // dynamic is the reversed table
  47. }
  48. searchStatic := func(f HeaderField) (uint64, bool) {
  49. old := staticTable
  50. staticTable = table
  51. defer func() { staticTable = old }()
  52. return staticTable.search(f)
  53. }
  54. searchDynamic := func(f HeaderField) (uint64, bool) {
  55. return table.search(f)
  56. }
  57. for _, test := range tests {
  58. gotI, gotMatch := searchStatic(test.f)
  59. if wantI, wantMatch := test.beforeWantStaticI, test.beforeWantMatch; gotI != wantI || gotMatch != wantMatch {
  60. t.Errorf("before evictions: searchStatic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
  61. }
  62. gotI, gotMatch = searchDynamic(test.f)
  63. wantDynamicI := staticToDynamic(test.beforeWantStaticI)
  64. if wantI, wantMatch := wantDynamicI, test.beforeWantMatch; gotI != wantI || gotMatch != wantMatch {
  65. t.Errorf("before evictions: searchDynamic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
  66. }
  67. }
  68. table.evictOldest(3)
  69. for _, test := range tests {
  70. gotI, gotMatch := searchStatic(test.f)
  71. if wantI, wantMatch := test.afterWantStaticI, test.afterWantMatch; gotI != wantI || gotMatch != wantMatch {
  72. t.Errorf("after evictions: searchStatic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
  73. }
  74. gotI, gotMatch = searchDynamic(test.f)
  75. wantDynamicI := staticToDynamic(test.afterWantStaticI)
  76. if wantI, wantMatch := wantDynamicI, test.afterWantMatch; gotI != wantI || gotMatch != wantMatch {
  77. t.Errorf("after evictions: searchDynamic(%+v)=%v,%v want %v,%v", test.f, gotI, gotMatch, wantI, wantMatch)
  78. }
  79. }
  80. }
  81. func TestStaticTable(t *testing.T) {
  82. fromSpec := `
  83. +-------+-----------------------------+---------------+
  84. | 1 | :authority | |
  85. | 2 | :method | GET |
  86. | 3 | :method | POST |
  87. | 4 | :path | / |
  88. | 5 | :path | /index.html |
  89. | 6 | :scheme | http |
  90. | 7 | :scheme | https |
  91. | 8 | :status | 200 |
  92. | 9 | :status | 204 |
  93. | 10 | :status | 206 |
  94. | 11 | :status | 304 |
  95. | 12 | :status | 400 |
  96. | 13 | :status | 404 |
  97. | 14 | :status | 500 |
  98. | 15 | accept-charset | |
  99. | 16 | accept-encoding | gzip, deflate |
  100. | 17 | accept-language | |
  101. | 18 | accept-ranges | |
  102. | 19 | accept | |
  103. | 20 | access-control-allow-origin | |
  104. | 21 | age | |
  105. | 22 | allow | |
  106. | 23 | authorization | |
  107. | 24 | cache-control | |
  108. | 25 | content-disposition | |
  109. | 26 | content-encoding | |
  110. | 27 | content-language | |
  111. | 28 | content-length | |
  112. | 29 | content-location | |
  113. | 30 | content-range | |
  114. | 31 | content-type | |
  115. | 32 | cookie | |
  116. | 33 | date | |
  117. | 34 | etag | |
  118. | 35 | expect | |
  119. | 36 | expires | |
  120. | 37 | from | |
  121. | 38 | host | |
  122. | 39 | if-match | |
  123. | 40 | if-modified-since | |
  124. | 41 | if-none-match | |
  125. | 42 | if-range | |
  126. | 43 | if-unmodified-since | |
  127. | 44 | last-modified | |
  128. | 45 | link | |
  129. | 46 | location | |
  130. | 47 | max-forwards | |
  131. | 48 | proxy-authenticate | |
  132. | 49 | proxy-authorization | |
  133. | 50 | range | |
  134. | 51 | referer | |
  135. | 52 | refresh | |
  136. | 53 | retry-after | |
  137. | 54 | server | |
  138. | 55 | set-cookie | |
  139. | 56 | strict-transport-security | |
  140. | 57 | transfer-encoding | |
  141. | 58 | user-agent | |
  142. | 59 | vary | |
  143. | 60 | via | |
  144. | 61 | www-authenticate | |
  145. +-------+-----------------------------+---------------+
  146. `
  147. bs := bufio.NewScanner(strings.NewReader(fromSpec))
  148. re := regexp.MustCompile(`\| (\d+)\s+\| (\S+)\s*\| (\S(.*\S)?)?\s+\|`)
  149. for bs.Scan() {
  150. l := bs.Text()
  151. if !strings.Contains(l, "|") {
  152. continue
  153. }
  154. m := re.FindStringSubmatch(l)
  155. if m == nil {
  156. continue
  157. }
  158. i, err := strconv.Atoi(m[1])
  159. if err != nil {
  160. t.Errorf("Bogus integer on line %q", l)
  161. continue
  162. }
  163. if i < 1 || i > staticTable.len() {
  164. t.Errorf("Bogus index %d on line %q", i, l)
  165. continue
  166. }
  167. if got, want := staticTable.ents[i-1].Name, m[2]; got != want {
  168. t.Errorf("header index %d name = %q; want %q", i, got, want)
  169. }
  170. if got, want := staticTable.ents[i-1].Value, m[3]; got != want {
  171. t.Errorf("header index %d value = %q; want %q", i, got, want)
  172. }
  173. }
  174. if err := bs.Err(); err != nil {
  175. t.Error(err)
  176. }
  177. }