| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/spdy/hpack_header_table.h" | 5 #include "net/spdy/hpack_header_table.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 public: | 27 public: |
| 28 explicit HpackHeaderTablePeer(HpackHeaderTable* table) | 28 explicit HpackHeaderTablePeer(HpackHeaderTable* table) |
| 29 : table_(table) {} | 29 : table_(table) {} |
| 30 | 30 |
| 31 const HpackHeaderTable::EntryTable& dynamic_entries() { | 31 const HpackHeaderTable::EntryTable& dynamic_entries() { |
| 32 return table_->dynamic_entries_; | 32 return table_->dynamic_entries_; |
| 33 } | 33 } |
| 34 const HpackHeaderTable::EntryTable& static_entries() { | 34 const HpackHeaderTable::EntryTable& static_entries() { |
| 35 return table_->static_entries_; | 35 return table_->static_entries_; |
| 36 } | 36 } |
| 37 const HpackHeaderTable::OrderedEntrySet& index() { | 37 size_t index_size() { |
| 38 return table_->index_; | 38 return table_->static_index_.size() + table_->dynamic_index_.size(); |
| 39 } | 39 } |
| 40 std::vector<HpackEntry*> EvictionSet(StringPiece name, StringPiece value) { | 40 std::vector<HpackEntry*> EvictionSet(StringPiece name, StringPiece value) { |
| 41 HpackHeaderTable::EntryTable::iterator begin, end; | 41 HpackHeaderTable::EntryTable::iterator begin, end; |
| 42 table_->EvictionSet(name, value, &begin, &end); | 42 table_->EvictionSet(name, value, &begin, &end); |
| 43 std::vector<HpackEntry*> result; | 43 std::vector<HpackEntry*> result; |
| 44 for (; begin != end; ++begin) { | 44 for (; begin != end; ++begin) { |
| 45 result.push_back(&(*begin)); | 45 result.push_back(&(*begin)); |
| 46 } | 46 } |
| 47 return result; | 47 return result; |
| 48 } | 48 } |
| 49 size_t total_insertions() { | 49 size_t total_insertions() { |
| 50 return table_->total_insertions_; | 50 return table_->total_insertions_; |
| 51 } | 51 } |
| 52 size_t dynamic_entries_count() { | 52 size_t dynamic_entries_count() { |
| 53 return table_->dynamic_entries_.size(); | 53 return table_->dynamic_entries_.size(); |
| 54 } | 54 } |
| 55 size_t EvictionCountForEntry(StringPiece name, StringPiece value) { | 55 size_t EvictionCountForEntry(StringPiece name, StringPiece value) { |
| 56 return table_->EvictionCountForEntry(name, value); | 56 return table_->EvictionCountForEntry(name, value); |
| 57 } | 57 } |
| 58 size_t EvictionCountToReclaim(size_t reclaim_size) { | 58 size_t EvictionCountToReclaim(size_t reclaim_size) { |
| 59 return table_->EvictionCountToReclaim(reclaim_size); | 59 return table_->EvictionCountToReclaim(reclaim_size); |
| 60 } | 60 } |
| 61 void Evict(size_t count) { | 61 void Evict(size_t count) { |
| 62 return table_->Evict(count); | 62 return table_->Evict(count); |
| 63 } | 63 } |
| 64 | 64 |
| 65 void AddStaticEntry(StringPiece name, StringPiece value) { | |
| 66 table_->static_entries_.push_back( | |
| 67 HpackEntry(name, value, true, table_->total_insertions_++)); | |
| 68 } | |
| 69 | |
| 70 void AddDynamicEntry(StringPiece name, StringPiece value) { | 65 void AddDynamicEntry(StringPiece name, StringPiece value) { |
| 71 table_->dynamic_entries_.push_back( | 66 table_->dynamic_entries_.push_back( |
| 72 HpackEntry(name, value, false, table_->total_insertions_++)); | 67 HpackEntry(name, value, false, table_->total_insertions_++)); |
| 73 } | 68 } |
| 74 | 69 |
| 75 private: | 70 private: |
| 76 HpackHeaderTable* table_; | 71 HpackHeaderTable* table_; |
| 77 }; | 72 }; |
| 78 | 73 |
| 79 } // namespace test | 74 } // namespace test |
| 80 | 75 |
| 81 namespace { | 76 namespace { |
| 82 | 77 |
| 83 class HpackHeaderTableTest : public ::testing::Test { | 78 class HpackHeaderTableTest : public ::testing::Test { |
| 84 protected: | 79 protected: |
| 85 typedef std::vector<HpackEntry> HpackEntryVector; | 80 typedef std::vector<HpackEntry> HpackEntryVector; |
| 86 | 81 |
| 87 HpackHeaderTableTest() | 82 HpackHeaderTableTest() : table_(), peer_(&table_) {} |
| 88 : table_(), | |
| 89 peer_(&table_), | |
| 90 name_("header-name"), | |
| 91 value_("header value") {} | |
| 92 | 83 |
| 93 // Returns an entry whose Size() is equal to the given one. | 84 // Returns an entry whose Size() is equal to the given one. |
| 94 static HpackEntry MakeEntryOfSize(uint32 size) { | 85 static HpackEntry MakeEntryOfSize(uint32 size) { |
| 95 EXPECT_GE(size, HpackEntry::kSizeOverhead); | 86 EXPECT_GE(size, HpackEntry::kSizeOverhead); |
| 96 string name((size - HpackEntry::kSizeOverhead) / 2, 'n'); | 87 string name((size - HpackEntry::kSizeOverhead) / 2, 'n'); |
| 97 string value(size - HpackEntry::kSizeOverhead - name.size(), 'v'); | 88 string value(size - HpackEntry::kSizeOverhead - name.size(), 'v'); |
| 98 HpackEntry entry(name, value); | 89 HpackEntry entry(name, value); |
| 99 EXPECT_EQ(size, entry.Size()); | 90 EXPECT_EQ(size, entry.Size()); |
| 100 return entry; | 91 return entry; |
| 101 } | 92 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 119 // Adds the given vector of entries to the given header table, | 110 // Adds the given vector of entries to the given header table, |
| 120 // expecting no eviction to happen. | 111 // expecting no eviction to happen. |
| 121 void AddEntriesExpectNoEviction(const HpackEntryVector& entries) { | 112 void AddEntriesExpectNoEviction(const HpackEntryVector& entries) { |
| 122 for (HpackEntryVector::const_iterator it = entries.begin(); | 113 for (HpackEntryVector::const_iterator it = entries.begin(); |
| 123 it != entries.end(); ++it) { | 114 it != entries.end(); ++it) { |
| 124 HpackHeaderTable::EntryTable::iterator begin, end; | 115 HpackHeaderTable::EntryTable::iterator begin, end; |
| 125 | 116 |
| 126 table_.EvictionSet(it->name(), it->value(), &begin, &end); | 117 table_.EvictionSet(it->name(), it->value(), &begin, &end); |
| 127 EXPECT_EQ(0, distance(begin, end)); | 118 EXPECT_EQ(0, distance(begin, end)); |
| 128 | 119 |
| 129 HpackEntry* entry = table_.TryAddEntry(it->name(), it->value()); | 120 const HpackEntry* entry = table_.TryAddEntry(it->name(), it->value()); |
| 130 EXPECT_NE(entry, static_cast<HpackEntry*>(NULL)); | 121 EXPECT_NE(entry, static_cast<HpackEntry*>(NULL)); |
| 131 } | 122 } |
| 132 | 123 |
| 133 for (size_t i = 0; i != entries.size(); ++i) { | 124 for (size_t i = 0; i != entries.size(); ++i) { |
| 134 // Static table has 61 entries, dynamic entries follow those. | 125 // Static table has 61 entries, dynamic entries follow those. |
| 135 size_t index = 61 + entries.size() - i; | 126 size_t index = 61 + entries.size() - i; |
| 136 HpackEntry* entry = table_.GetByIndex(index); | 127 const HpackEntry* entry = table_.GetByIndex(index); |
| 137 EXPECT_EQ(entries[i].name(), entry->name()); | 128 EXPECT_EQ(entries[i].name(), entry->name()); |
| 138 EXPECT_EQ(entries[i].value(), entry->value()); | 129 EXPECT_EQ(entries[i].value(), entry->value()); |
| 139 EXPECT_EQ(index, table_.IndexOf(entry)); | 130 EXPECT_EQ(index, table_.IndexOf(entry)); |
| 140 } | 131 } |
| 141 } | 132 } |
| 142 | 133 |
| 143 HpackEntry StaticEntry() { | 134 HpackEntry DynamicEntry(string name, string value) { |
| 144 peer_.AddStaticEntry(name_, value_); | 135 peer_.AddDynamicEntry(name, value); |
| 145 return peer_.static_entries().back(); | |
| 146 } | |
| 147 HpackEntry DynamicEntry() { | |
| 148 peer_.AddDynamicEntry(name_, value_); | |
| 149 return peer_.dynamic_entries().back(); | 136 return peer_.dynamic_entries().back(); |
| 150 } | 137 } |
| 151 | 138 |
| 152 HpackHeaderTable table_; | 139 HpackHeaderTable table_; |
| 153 test::HpackHeaderTablePeer peer_; | 140 test::HpackHeaderTablePeer peer_; |
| 154 string name_, value_; | |
| 155 }; | 141 }; |
| 156 | 142 |
| 157 TEST_F(HpackHeaderTableTest, StaticTableInitialization) { | 143 TEST_F(HpackHeaderTableTest, StaticTableInitialization) { |
| 158 EXPECT_EQ(0u, table_.size()); | 144 EXPECT_EQ(0u, table_.size()); |
| 159 EXPECT_EQ(kDefaultHeaderTableSizeSetting, table_.max_size()); | 145 EXPECT_EQ(kDefaultHeaderTableSizeSetting, table_.max_size()); |
| 160 EXPECT_EQ(kDefaultHeaderTableSizeSetting, table_.settings_size_bound()); | 146 EXPECT_EQ(kDefaultHeaderTableSizeSetting, table_.settings_size_bound()); |
| 161 | 147 |
| 162 EXPECT_EQ(0u, peer_.dynamic_entries_count()); | 148 EXPECT_EQ(0u, peer_.dynamic_entries_count()); |
| 163 EXPECT_EQ(peer_.static_entries().size(), peer_.total_insertions()); | 149 EXPECT_EQ(peer_.static_entries().size(), peer_.total_insertions()); |
| 164 | 150 |
| 165 // Static entries have been populated and inserted into the table & index. | 151 // Static entries have been populated and inserted into the table & index. |
| 166 EXPECT_NE(0u, peer_.static_entries().size()); | 152 EXPECT_NE(0u, peer_.static_entries().size()); |
| 167 EXPECT_EQ(peer_.index().size(), peer_.static_entries().size()); | 153 EXPECT_EQ(peer_.index_size(), peer_.static_entries().size()); |
| 168 for (size_t i = 0; i != peer_.static_entries().size(); ++i) { | 154 for (size_t i = 0; i != peer_.static_entries().size(); ++i) { |
| 169 const HpackEntry* entry = &peer_.static_entries()[i]; | 155 const HpackEntry* entry = &peer_.static_entries()[i]; |
| 170 | 156 |
| 171 EXPECT_TRUE(entry->IsStatic()); | 157 EXPECT_TRUE(entry->IsStatic()); |
| 172 EXPECT_EQ(entry, table_.GetByIndex(i + 1)); | 158 EXPECT_EQ(entry, table_.GetByIndex(i + 1)); |
| 173 EXPECT_EQ(entry, table_.GetByNameAndValue(entry->name(), entry->value())); | 159 EXPECT_EQ(entry, table_.GetByNameAndValue(entry->name(), entry->value())); |
| 174 } | 160 } |
| 175 } | 161 } |
| 176 | 162 |
| 177 TEST_F(HpackHeaderTableTest, BasicDynamicEntryInsertionAndEviction) { | 163 TEST_F(HpackHeaderTableTest, BasicDynamicEntryInsertionAndEviction) { |
| 178 size_t static_count = peer_.total_insertions(); | 164 size_t static_count = peer_.total_insertions(); |
| 179 HpackEntry* first_static_entry = table_.GetByIndex(1); | 165 const HpackEntry* first_static_entry = table_.GetByIndex(1); |
| 180 | 166 |
| 181 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); | 167 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); |
| 182 | 168 |
| 183 HpackEntry* entry = table_.TryAddEntry("header-key", "Header Value"); | 169 const HpackEntry* entry = table_.TryAddEntry("header-key", "Header Value"); |
| 184 EXPECT_EQ("header-key", entry->name()); | 170 EXPECT_EQ("header-key", entry->name()); |
| 185 EXPECT_EQ("Header Value", entry->value()); | 171 EXPECT_EQ("Header Value", entry->value()); |
| 186 EXPECT_FALSE(entry->IsStatic()); | 172 EXPECT_FALSE(entry->IsStatic()); |
| 187 | 173 |
| 188 // Table counts were updated appropriately. | 174 // Table counts were updated appropriately. |
| 189 EXPECT_EQ(entry->Size(), table_.size()); | 175 EXPECT_EQ(entry->Size(), table_.size()); |
| 190 EXPECT_EQ(1u, peer_.dynamic_entries_count()); | 176 EXPECT_EQ(1u, peer_.dynamic_entries_count()); |
| 191 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.dynamic_entries_count()); | 177 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.dynamic_entries_count()); |
| 192 EXPECT_EQ(static_count + 1, peer_.total_insertions()); | 178 EXPECT_EQ(static_count + 1, peer_.total_insertions()); |
| 193 EXPECT_EQ(static_count + 1, peer_.index().size()); | 179 EXPECT_EQ(static_count + 1, peer_.index_size()); |
| 194 | 180 |
| 195 // Index() of entries reflects the insertion. | 181 // Index() of entries reflects the insertion. |
| 196 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); | 182 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); |
| 197 // Static table has 61 entries. | 183 // Static table has 61 entries. |
| 198 EXPECT_EQ(62u, table_.IndexOf(entry)); | 184 EXPECT_EQ(62u, table_.IndexOf(entry)); |
| 199 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); | 185 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); |
| 200 EXPECT_EQ(entry, table_.GetByIndex(62)); | 186 EXPECT_EQ(entry, table_.GetByIndex(62)); |
| 201 | 187 |
| 202 // Evict |entry|. Table counts are again updated appropriately. | 188 // Evict |entry|. Table counts are again updated appropriately. |
| 203 peer_.Evict(1); | 189 peer_.Evict(1); |
| 204 EXPECT_EQ(0u, table_.size()); | 190 EXPECT_EQ(0u, table_.size()); |
| 205 EXPECT_EQ(0u, peer_.dynamic_entries_count()); | 191 EXPECT_EQ(0u, peer_.dynamic_entries_count()); |
| 206 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.dynamic_entries_count()); | 192 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.dynamic_entries_count()); |
| 207 EXPECT_EQ(static_count + 1, peer_.total_insertions()); | 193 EXPECT_EQ(static_count + 1, peer_.total_insertions()); |
| 208 EXPECT_EQ(static_count, peer_.index().size()); | 194 EXPECT_EQ(static_count, peer_.index_size()); |
| 209 | 195 |
| 210 // Index() of |first_static_entry| reflects the eviction. | 196 // Index() of |first_static_entry| reflects the eviction. |
| 211 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); | 197 EXPECT_EQ(1u, table_.IndexOf(first_static_entry)); |
| 212 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); | 198 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); |
| 213 } | 199 } |
| 214 | 200 |
| 215 TEST_F(HpackHeaderTableTest, EntryIndexing) { | 201 TEST_F(HpackHeaderTableTest, EntryIndexing) { |
| 216 HpackEntry* first_static_entry = table_.GetByIndex(1); | 202 const HpackEntry* first_static_entry = table_.GetByIndex(1); |
| 217 | 203 |
| 218 // Static entries are queryable by name & value. | 204 // Static entries are queryable by name & value. |
| 219 EXPECT_EQ(first_static_entry, table_.GetByName(first_static_entry->name())); | 205 EXPECT_EQ(first_static_entry, table_.GetByName(first_static_entry->name())); |
| 220 EXPECT_EQ(first_static_entry, table_.GetByNameAndValue( | 206 EXPECT_EQ(first_static_entry, table_.GetByNameAndValue( |
| 221 first_static_entry->name(), first_static_entry->value())); | 207 first_static_entry->name(), first_static_entry->value())); |
| 222 | 208 |
| 223 // Create a mix of entries which duplicate names, and names & values of both | 209 // Create a mix of entries which duplicate names, and names & values of both |
| 224 // dynamic and static entries. | 210 // dynamic and static entries. |
| 225 HpackEntry* entry1 = table_.TryAddEntry(first_static_entry->name(), | 211 const HpackEntry* entry1 = table_.TryAddEntry(first_static_entry->name(), |
| 226 first_static_entry->value()); | 212 first_static_entry->value()); |
| 227 HpackEntry* entry2 = table_.TryAddEntry(first_static_entry->name(), | 213 const HpackEntry* entry2 = |
| 228 "Value Four"); | 214 table_.TryAddEntry(first_static_entry->name(), "Value Four"); |
| 229 HpackEntry* entry3 = table_.TryAddEntry("key-1", "Value One"); | 215 const HpackEntry* entry3 = table_.TryAddEntry("key-1", "Value One"); |
| 230 HpackEntry* entry4 = table_.TryAddEntry("key-2", "Value Three"); | 216 const HpackEntry* entry4 = table_.TryAddEntry("key-2", "Value Three"); |
| 231 HpackEntry* entry5 = table_.TryAddEntry("key-1", "Value Two"); | 217 const HpackEntry* entry5 = table_.TryAddEntry("key-1", "Value Two"); |
| 232 HpackEntry* entry6 = table_.TryAddEntry("key-2", "Value Three"); | 218 const HpackEntry* entry6 = table_.TryAddEntry("key-2", "Value Three"); |
| 233 HpackEntry* entry7 = table_.TryAddEntry("key-2", "Value Four"); | 219 const HpackEntry* entry7 = table_.TryAddEntry("key-2", "Value Four"); |
| 234 | 220 |
| 235 // Entries are queryable under their current index. | 221 // Entries are queryable under their current index. |
| 236 EXPECT_EQ(entry7, table_.GetByIndex(62)); | 222 EXPECT_EQ(entry7, table_.GetByIndex(62)); |
| 237 EXPECT_EQ(entry6, table_.GetByIndex(63)); | 223 EXPECT_EQ(entry6, table_.GetByIndex(63)); |
| 238 EXPECT_EQ(entry5, table_.GetByIndex(64)); | 224 EXPECT_EQ(entry5, table_.GetByIndex(64)); |
| 239 EXPECT_EQ(entry4, table_.GetByIndex(65)); | 225 EXPECT_EQ(entry4, table_.GetByIndex(65)); |
| 240 EXPECT_EQ(entry3, table_.GetByIndex(66)); | 226 EXPECT_EQ(entry3, table_.GetByIndex(66)); |
| 241 EXPECT_EQ(entry2, table_.GetByIndex(67)); | 227 EXPECT_EQ(entry2, table_.GetByIndex(67)); |
| 242 EXPECT_EQ(entry1, table_.GetByIndex(68)); | 228 EXPECT_EQ(entry1, table_.GetByIndex(68)); |
| 243 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); | 229 EXPECT_EQ(first_static_entry, table_.GetByIndex(1)); |
| 244 | 230 |
| 245 // Querying by name returns the lowest-value matching entry. | 231 // Querying by name returns the lowest-value matching entry. |
| 246 EXPECT_EQ(entry3, table_.GetByName("key-1")); | 232 EXPECT_EQ(entry3, table_.GetByName("key-1")); |
| 247 EXPECT_EQ(entry7, table_.GetByName("key-2")); | 233 EXPECT_EQ(entry7, table_.GetByName("key-2")); |
| 248 EXPECT_EQ(entry2->name(), | 234 EXPECT_EQ(entry2->name(), |
| 249 table_.GetByName(first_static_entry->name())->name()); | 235 table_.GetByName(first_static_entry->name())->name()); |
| 250 EXPECT_EQ(NULL, table_.GetByName("not-present")); | 236 EXPECT_EQ(NULL, table_.GetByName("not-present")); |
| 251 | 237 |
| 252 // Querying by name & value returns the lowest-index matching entry. | 238 // Querying by name & value returns the lowest-index matching entry among |
| 239 // static entries, and the highest-index one among dynamic entries. |
| 253 EXPECT_EQ(entry3, table_.GetByNameAndValue("key-1", "Value One")); | 240 EXPECT_EQ(entry3, table_.GetByNameAndValue("key-1", "Value One")); |
| 254 EXPECT_EQ(entry5, table_.GetByNameAndValue("key-1", "Value Two")); | 241 EXPECT_EQ(entry5, table_.GetByNameAndValue("key-1", "Value Two")); |
| 255 EXPECT_EQ(entry6, table_.GetByNameAndValue("key-2", "Value Three")); | 242 EXPECT_EQ(entry4, table_.GetByNameAndValue("key-2", "Value Three")); |
| 256 EXPECT_EQ(entry7, table_.GetByNameAndValue("key-2", "Value Four")); | 243 EXPECT_EQ(entry7, table_.GetByNameAndValue("key-2", "Value Four")); |
| 257 EXPECT_EQ(first_static_entry, | 244 EXPECT_EQ(first_static_entry, |
| 258 table_.GetByNameAndValue(first_static_entry->name(), | 245 table_.GetByNameAndValue(first_static_entry->name(), |
| 259 first_static_entry->value())); | 246 first_static_entry->value())); |
| 260 EXPECT_EQ(entry2, table_.GetByNameAndValue(first_static_entry->name(), | 247 EXPECT_EQ(entry2, table_.GetByNameAndValue(first_static_entry->name(), |
| 261 "Value Four")); | 248 "Value Four")); |
| 262 EXPECT_EQ(NULL, table_.GetByNameAndValue("key-1", "Not Present")); | 249 EXPECT_EQ(NULL, table_.GetByNameAndValue("key-1", "Not Present")); |
| 263 EXPECT_EQ(NULL, table_.GetByNameAndValue("not-present", "Value One")); | 250 EXPECT_EQ(NULL, table_.GetByNameAndValue("not-present", "Value One")); |
| 264 | 251 |
| 265 // Evict |entry1|. Queries for its name & value now return the static entry. | 252 // Evict |entry1|. Queries for its name & value now return the static entry. |
| 266 // |entry2| remains queryable. | 253 // |entry2| remains queryable. |
| 267 peer_.Evict(1); | 254 peer_.Evict(1); |
| 268 EXPECT_EQ(first_static_entry, | 255 EXPECT_EQ(first_static_entry, |
| 269 table_.GetByNameAndValue(first_static_entry->name(), | 256 table_.GetByNameAndValue(first_static_entry->name(), |
| 270 first_static_entry->value())); | 257 first_static_entry->value())); |
| 271 EXPECT_EQ(entry2, table_.GetByNameAndValue(first_static_entry->name(), | 258 EXPECT_EQ(entry2, table_.GetByNameAndValue(first_static_entry->name(), |
| 272 "Value Four")); | 259 "Value Four")); |
| 273 | 260 |
| 274 // Evict |entry2|. Queries by its name & value are not found. | 261 // Evict |entry2|. Queries by its name & value are not found. |
| 275 peer_.Evict(1); | 262 peer_.Evict(1); |
| 276 EXPECT_EQ(NULL, table_.GetByNameAndValue(first_static_entry->name(), | 263 EXPECT_EQ(NULL, table_.GetByNameAndValue(first_static_entry->name(), |
| 277 "Value Four")); | 264 "Value Four")); |
| 278 } | 265 } |
| 279 | 266 |
| 280 TEST_F(HpackHeaderTableTest, SetSizes) { | 267 TEST_F(HpackHeaderTableTest, SetSizes) { |
| 281 string key = "key", value = "value"; | 268 string key = "key", value = "value"; |
| 282 HpackEntry* entry1 = table_.TryAddEntry(key, value); | 269 const HpackEntry* entry1 = table_.TryAddEntry(key, value); |
| 283 HpackEntry* entry2 = table_.TryAddEntry(key, value); | 270 const HpackEntry* entry2 = table_.TryAddEntry(key, value); |
| 284 HpackEntry* entry3 = table_.TryAddEntry(key, value); | 271 const HpackEntry* entry3 = table_.TryAddEntry(key, value); |
| 285 | 272 |
| 286 // Set exactly large enough. No Evictions. | 273 // Set exactly large enough. No Evictions. |
| 287 size_t max_size = entry1->Size() + entry2->Size() + entry3->Size(); | 274 size_t max_size = entry1->Size() + entry2->Size() + entry3->Size(); |
| 288 table_.SetMaxSize(max_size); | 275 table_.SetMaxSize(max_size); |
| 289 EXPECT_EQ(3u, peer_.dynamic_entries().size()); | 276 EXPECT_EQ(3u, peer_.dynamic_entries().size()); |
| 290 | 277 |
| 291 // Set just too small. One eviction. | 278 // Set just too small. One eviction. |
| 292 max_size = entry1->Size() + entry2->Size() + entry3->Size() - 1; | 279 max_size = entry1->Size() + entry2->Size() + entry3->Size() - 1; |
| 293 table_.SetMaxSize(max_size); | 280 table_.SetMaxSize(max_size); |
| 294 EXPECT_EQ(2u, peer_.dynamic_entries().size()); | 281 EXPECT_EQ(2u, peer_.dynamic_entries().size()); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 306 // and will force evictions. | 293 // and will force evictions. |
| 307 max_size = entry3->Size() - 1; | 294 max_size = entry3->Size() - 1; |
| 308 table_.SetSettingsHeaderTableSize(max_size); | 295 table_.SetSettingsHeaderTableSize(max_size); |
| 309 EXPECT_EQ(max_size, table_.max_size()); | 296 EXPECT_EQ(max_size, table_.max_size()); |
| 310 EXPECT_EQ(max_size, table_.settings_size_bound()); | 297 EXPECT_EQ(max_size, table_.settings_size_bound()); |
| 311 EXPECT_EQ(0u, peer_.dynamic_entries().size()); | 298 EXPECT_EQ(0u, peer_.dynamic_entries().size()); |
| 312 } | 299 } |
| 313 | 300 |
| 314 TEST_F(HpackHeaderTableTest, EvictionCountForEntry) { | 301 TEST_F(HpackHeaderTableTest, EvictionCountForEntry) { |
| 315 string key = "key", value = "value"; | 302 string key = "key", value = "value"; |
| 316 HpackEntry* entry1 = table_.TryAddEntry(key, value); | 303 const HpackEntry* entry1 = table_.TryAddEntry(key, value); |
| 317 HpackEntry* entry2 = table_.TryAddEntry(key, value); | 304 const HpackEntry* entry2 = table_.TryAddEntry(key, value); |
| 318 size_t entry3_size = HpackEntry::Size(key, value); | 305 size_t entry3_size = HpackEntry::Size(key, value); |
| 319 | 306 |
| 320 // Just enough capacity for third entry. | 307 // Just enough capacity for third entry. |
| 321 table_.SetMaxSize(entry1->Size() + entry2->Size() + entry3_size); | 308 table_.SetMaxSize(entry1->Size() + entry2->Size() + entry3_size); |
| 322 EXPECT_EQ(0u, peer_.EvictionCountForEntry(key, value)); | 309 EXPECT_EQ(0u, peer_.EvictionCountForEntry(key, value)); |
| 323 EXPECT_EQ(1u, peer_.EvictionCountForEntry(key, value + "x")); | 310 EXPECT_EQ(1u, peer_.EvictionCountForEntry(key, value + "x")); |
| 324 | 311 |
| 325 // No extra capacity. Third entry would force evictions. | 312 // No extra capacity. Third entry would force evictions. |
| 326 table_.SetMaxSize(entry1->Size() + entry2->Size()); | 313 table_.SetMaxSize(entry1->Size() + entry2->Size()); |
| 327 EXPECT_EQ(1u, peer_.EvictionCountForEntry(key, value)); | 314 EXPECT_EQ(1u, peer_.EvictionCountForEntry(key, value)); |
| 328 EXPECT_EQ(2u, peer_.EvictionCountForEntry(key, value + "x")); | 315 EXPECT_EQ(2u, peer_.EvictionCountForEntry(key, value + "x")); |
| 329 } | 316 } |
| 330 | 317 |
| 331 TEST_F(HpackHeaderTableTest, EvictionCountToReclaim) { | 318 TEST_F(HpackHeaderTableTest, EvictionCountToReclaim) { |
| 332 string key = "key", value = "value"; | 319 string key = "key", value = "value"; |
| 333 HpackEntry* entry1 = table_.TryAddEntry(key, value); | 320 const HpackEntry* entry1 = table_.TryAddEntry(key, value); |
| 334 HpackEntry* entry2 = table_.TryAddEntry(key, value); | 321 const HpackEntry* entry2 = table_.TryAddEntry(key, value); |
| 335 | 322 |
| 336 EXPECT_EQ(1u, peer_.EvictionCountToReclaim(1)); | 323 EXPECT_EQ(1u, peer_.EvictionCountToReclaim(1)); |
| 337 EXPECT_EQ(1u, peer_.EvictionCountToReclaim(entry1->Size())); | 324 EXPECT_EQ(1u, peer_.EvictionCountToReclaim(entry1->Size())); |
| 338 EXPECT_EQ(2u, peer_.EvictionCountToReclaim(entry1->Size() + 1)); | 325 EXPECT_EQ(2u, peer_.EvictionCountToReclaim(entry1->Size() + 1)); |
| 339 EXPECT_EQ(2u, peer_.EvictionCountToReclaim(entry1->Size() + entry2->Size())); | 326 EXPECT_EQ(2u, peer_.EvictionCountToReclaim(entry1->Size() + entry2->Size())); |
| 340 } | 327 } |
| 341 | 328 |
| 342 // Fill a header table with entries. Make sure the entries are in | 329 // Fill a header table with entries. Make sure the entries are in |
| 343 // reverse order in the header table. | 330 // reverse order in the header table. |
| 344 TEST_F(HpackHeaderTableTest, TryAddEntryBasic) { | 331 TEST_F(HpackHeaderTableTest, TryAddEntryBasic) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 380 } | 367 } |
| 381 | 368 |
| 382 // Fill a header table with entries, and then add an entry just big | 369 // Fill a header table with entries, and then add an entry just big |
| 383 // enough to cause eviction of all but one entry. Make sure the | 370 // enough to cause eviction of all but one entry. Make sure the |
| 384 // eviction happens as expected and the long entry is inserted into | 371 // eviction happens as expected and the long entry is inserted into |
| 385 // the table. | 372 // the table. |
| 386 TEST_F(HpackHeaderTableTest, TryAddEntryEviction) { | 373 TEST_F(HpackHeaderTableTest, TryAddEntryEviction) { |
| 387 HpackEntryVector entries = MakeEntriesOfTotalSize(table_.max_size()); | 374 HpackEntryVector entries = MakeEntriesOfTotalSize(table_.max_size()); |
| 388 AddEntriesExpectNoEviction(entries); | 375 AddEntriesExpectNoEviction(entries); |
| 389 | 376 |
| 390 HpackEntry* survivor_entry = table_.GetByIndex(61 + 1); | 377 const HpackEntry* survivor_entry = table_.GetByIndex(61 + 1); |
| 391 HpackEntry long_entry = | 378 HpackEntry long_entry = |
| 392 MakeEntryOfSize(table_.max_size() - survivor_entry->Size()); | 379 MakeEntryOfSize(table_.max_size() - survivor_entry->Size()); |
| 393 | 380 |
| 394 // All dynamic entries but the first are to be evicted. | 381 // All dynamic entries but the first are to be evicted. |
| 395 EXPECT_EQ(peer_.dynamic_entries().size() - 1, peer_.EvictionSet( | 382 EXPECT_EQ(peer_.dynamic_entries().size() - 1, peer_.EvictionSet( |
| 396 long_entry.name(), long_entry.value()).size()); | 383 long_entry.name(), long_entry.value()).size()); |
| 397 | 384 |
| 398 HpackEntry* new_entry = table_.TryAddEntry(long_entry.name(), | 385 const HpackEntry* new_entry = |
| 399 long_entry.value()); | 386 table_.TryAddEntry(long_entry.name(), long_entry.value()); |
| 400 EXPECT_EQ(62u, table_.IndexOf(new_entry)); | 387 EXPECT_EQ(62u, table_.IndexOf(new_entry)); |
| 401 EXPECT_EQ(2u, peer_.dynamic_entries().size()); | 388 EXPECT_EQ(2u, peer_.dynamic_entries().size()); |
| 402 EXPECT_EQ(table_.GetByIndex(63), survivor_entry); | 389 EXPECT_EQ(table_.GetByIndex(63), survivor_entry); |
| 403 EXPECT_EQ(table_.GetByIndex(62), new_entry); | 390 EXPECT_EQ(table_.GetByIndex(62), new_entry); |
| 404 } | 391 } |
| 405 | 392 |
| 406 // Fill a header table with entries, and then add an entry bigger than | 393 // Fill a header table with entries, and then add an entry bigger than |
| 407 // the entire table. Make sure no entry remains in the table. | 394 // the entire table. Make sure no entry remains in the table. |
| 408 TEST_F(HpackHeaderTableTest, TryAddTooLargeEntry) { | 395 TEST_F(HpackHeaderTableTest, TryAddTooLargeEntry) { |
| 409 HpackEntryVector entries = MakeEntriesOfTotalSize(table_.max_size()); | 396 HpackEntryVector entries = MakeEntriesOfTotalSize(table_.max_size()); |
| 410 AddEntriesExpectNoEviction(entries); | 397 AddEntriesExpectNoEviction(entries); |
| 411 | 398 |
| 412 HpackEntry long_entry = MakeEntryOfSize(table_.max_size() + 1); | 399 const HpackEntry long_entry = MakeEntryOfSize(table_.max_size() + 1); |
| 413 | 400 |
| 414 // All entries are to be evicted. | 401 // All entries are to be evicted. |
| 415 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.EvictionSet( | 402 EXPECT_EQ(peer_.dynamic_entries().size(), peer_.EvictionSet( |
| 416 long_entry.name(), long_entry.value()).size()); | 403 long_entry.name(), long_entry.value()).size()); |
| 417 | 404 |
| 418 HpackEntry* new_entry = table_.TryAddEntry(long_entry.name(), | 405 const HpackEntry* new_entry = |
| 419 long_entry.value()); | 406 table_.TryAddEntry(long_entry.name(), long_entry.value()); |
| 420 EXPECT_EQ(new_entry, static_cast<HpackEntry*>(NULL)); | 407 EXPECT_EQ(new_entry, static_cast<HpackEntry*>(NULL)); |
| 421 EXPECT_EQ(0u, peer_.dynamic_entries().size()); | 408 EXPECT_EQ(0u, peer_.dynamic_entries().size()); |
| 422 } | 409 } |
| 423 | 410 |
| 424 TEST_F(HpackHeaderTableTest, ComparatorNameOrdering) { | 411 TEST_F(HpackHeaderTableTest, ComparatorNameOrdering) { |
| 425 HpackEntry entry1(StaticEntry()); | 412 HpackEntry entry1("header", "value"); |
| 426 name_[0]--; | 413 HpackEntry entry2("HEADER", "value"); |
| 427 HpackEntry entry2(StaticEntry()); | |
| 428 | 414 |
| 429 HpackHeaderTable::EntryComparator comparator(&table_); | 415 HpackHeaderTable::EntryComparator comparator; |
| 430 EXPECT_FALSE(comparator(&entry1, &entry2)); | 416 EXPECT_FALSE(comparator(&entry1, &entry2)); |
| 431 EXPECT_TRUE(comparator(&entry2, &entry1)); | 417 EXPECT_TRUE(comparator(&entry2, &entry1)); |
| 432 } | 418 } |
| 433 | 419 |
| 434 TEST_F(HpackHeaderTableTest, ComparatorValueOrdering) { | 420 TEST_F(HpackHeaderTableTest, ComparatorValueOrdering) { |
| 435 HpackEntry entry1(StaticEntry()); | 421 HpackEntry entry1("header", "value"); |
| 436 value_[0]--; | 422 HpackEntry entry2("header", "VALUE"); |
| 437 HpackEntry entry2(StaticEntry()); | |
| 438 | 423 |
| 439 HpackHeaderTable::EntryComparator comparator(&table_); | 424 HpackHeaderTable::EntryComparator comparator; |
| 440 EXPECT_FALSE(comparator(&entry1, &entry2)); | 425 EXPECT_FALSE(comparator(&entry1, &entry2)); |
| 441 EXPECT_TRUE(comparator(&entry2, &entry1)); | 426 EXPECT_TRUE(comparator(&entry2, &entry1)); |
| 442 } | 427 } |
| 443 | 428 |
| 444 TEST_F(HpackHeaderTableTest, ComparatorIndexOrdering) { | 429 TEST_F(HpackHeaderTableTest, ComparatorIndexOrdering) { |
| 445 HpackEntry entry1(StaticEntry()); | 430 HpackHeaderTable::EntryComparator comparator; |
| 446 HpackEntry entry2(StaticEntry()); | 431 HpackEntry entry1(DynamicEntry("name", "value")); |
| 432 HpackEntry entry2(DynamicEntry("name", "value")); |
| 447 | 433 |
| 448 HpackHeaderTable::EntryComparator comparator(&table_); | 434 // |entry1| has lower insertion index than |entry2|. |
| 449 EXPECT_TRUE(comparator(&entry1, &entry2)); | |
| 450 EXPECT_FALSE(comparator(&entry2, &entry1)); | |
| 451 | |
| 452 HpackEntry entry3(DynamicEntry()); | |
| 453 HpackEntry entry4(DynamicEntry()); | |
| 454 | |
| 455 // |entry4| has lower index than |entry3|. | |
| 456 EXPECT_TRUE(comparator(&entry4, &entry3)); | |
| 457 EXPECT_FALSE(comparator(&entry3, &entry4)); | |
| 458 | |
| 459 // |entry1| has lower index than |entry3|. | |
| 460 EXPECT_TRUE(comparator(&entry1, &entry3)); | |
| 461 EXPECT_FALSE(comparator(&entry3, &entry1)); | |
| 462 | |
| 463 // |entry1| & |entry2| ordering is preserved, though each Index() has changed. | |
| 464 EXPECT_TRUE(comparator(&entry1, &entry2)); | 435 EXPECT_TRUE(comparator(&entry1, &entry2)); |
| 465 EXPECT_FALSE(comparator(&entry2, &entry1)); | 436 EXPECT_FALSE(comparator(&entry2, &entry1)); |
| 466 } | 437 } |
| 467 | 438 |
| 468 TEST_F(HpackHeaderTableTest, ComparatorEqualityOrdering) { | 439 TEST_F(HpackHeaderTableTest, ComparatorEqualityOrdering) { |
| 469 HpackEntry entry1(StaticEntry()); | 440 HpackEntry entry1("name", "value"); |
| 470 HpackEntry entry2(DynamicEntry()); | 441 HpackEntry entry2(DynamicEntry("name", "value")); |
| 471 | 442 |
| 472 HpackHeaderTable::EntryComparator comparator(&table_); | 443 HpackHeaderTable::EntryComparator comparator; |
| 473 EXPECT_FALSE(comparator(&entry1, &entry1)); | 444 EXPECT_FALSE(comparator(&entry1, &entry1)); |
| 474 EXPECT_FALSE(comparator(&entry2, &entry2)); | 445 EXPECT_FALSE(comparator(&entry2, &entry2)); |
| 475 } | 446 } |
| 476 | 447 |
| 477 } // namespace | 448 } // namespace |
| 478 | 449 |
| 479 } // namespace net | 450 } // namespace net |
| OLD | NEW |