| 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_encoder.h" | 5 #include "net/spdy/hpack_encoder.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "testing/gmock/include/gmock/gmock.h" | 10 #include "testing/gmock/include/gmock/gmock.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 void ExpectNonIndexedLiteral(StringPiece name, StringPiece value) { | 138 void ExpectNonIndexedLiteral(StringPiece name, StringPiece value) { |
| 139 expected_.AppendPrefix(kLiteralNoIndexOpcode); | 139 expected_.AppendPrefix(kLiteralNoIndexOpcode); |
| 140 expected_.AppendUint32(0); | 140 expected_.AppendUint32(0); |
| 141 expected_.AppendPrefix(kStringLiteralIdentityEncoded); | 141 expected_.AppendPrefix(kStringLiteralIdentityEncoded); |
| 142 expected_.AppendUint32(name.size()); | 142 expected_.AppendUint32(name.size()); |
| 143 expected_.AppendBytes(name); | 143 expected_.AppendBytes(name); |
| 144 expected_.AppendPrefix(kStringLiteralIdentityEncoded); | 144 expected_.AppendPrefix(kStringLiteralIdentityEncoded); |
| 145 expected_.AppendUint32(value.size()); | 145 expected_.AppendUint32(value.size()); |
| 146 expected_.AppendBytes(value); | 146 expected_.AppendBytes(value); |
| 147 } | 147 } |
| 148 void CompareWithExpectedEncoding(const map<string, string>& header_set) { | 148 void CompareWithExpectedEncoding(const SpdyHeaderBlock& header_set) { |
| 149 string expected_out, actual_out; | 149 string expected_out, actual_out; |
| 150 expected_.TakeString(&expected_out); | 150 expected_.TakeString(&expected_out); |
| 151 EXPECT_TRUE(encoder_.EncodeHeaderSet(header_set, &actual_out)); | 151 EXPECT_TRUE(encoder_.EncodeHeaderSet(header_set, &actual_out)); |
| 152 EXPECT_EQ(expected_out, actual_out); | 152 EXPECT_EQ(expected_out, actual_out); |
| 153 } | 153 } |
| 154 size_t IndexOf(const HpackEntry* entry) { | 154 size_t IndexOf(const HpackEntry* entry) { |
| 155 return peer_.table()->IndexOf(entry); | 155 return peer_.table()->IndexOf(entry); |
| 156 } | 156 } |
| 157 | 157 |
| 158 HpackEncoder encoder_; | 158 HpackEncoder encoder_; |
| 159 test::HpackEncoderPeer peer_; | 159 test::HpackEncoderPeer peer_; |
| 160 | 160 |
| 161 const HpackEntry* static_; | 161 const HpackEntry* static_; |
| 162 const HpackEntry* key_1_; | 162 const HpackEntry* key_1_; |
| 163 const HpackEntry* key_2_; | 163 const HpackEntry* key_2_; |
| 164 const HpackEntry* cookie_a_; | 164 const HpackEntry* cookie_a_; |
| 165 const HpackEntry* cookie_c_; | 165 const HpackEntry* cookie_c_; |
| 166 | 166 |
| 167 HpackOutputStream expected_; | 167 HpackOutputStream expected_; |
| 168 }; | 168 }; |
| 169 | 169 |
| 170 TEST_F(HpackEncoderTest, SingleDynamicIndex) { | 170 TEST_F(HpackEncoderTest, SingleDynamicIndex) { |
| 171 ExpectIndex(IndexOf(key_2_)); | 171 ExpectIndex(IndexOf(key_2_)); |
| 172 | 172 |
| 173 map<string, string> headers; | 173 SpdyHeaderBlock headers; |
| 174 headers[key_2_->name()] = key_2_->value(); | 174 headers[key_2_->name()] = key_2_->value(); |
| 175 CompareWithExpectedEncoding(headers); | 175 CompareWithExpectedEncoding(headers); |
| 176 } | 176 } |
| 177 | 177 |
| 178 TEST_F(HpackEncoderTest, SingleStaticIndex) { | 178 TEST_F(HpackEncoderTest, SingleStaticIndex) { |
| 179 ExpectIndex(IndexOf(static_)); | 179 ExpectIndex(IndexOf(static_)); |
| 180 | 180 |
| 181 map<string, string> headers; | 181 SpdyHeaderBlock headers; |
| 182 headers[static_->name()] = static_->value(); | 182 headers[static_->name()] = static_->value(); |
| 183 CompareWithExpectedEncoding(headers); | 183 CompareWithExpectedEncoding(headers); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(HpackEncoderTest, SingleStaticIndexTooLarge) { | 186 TEST_F(HpackEncoderTest, SingleStaticIndexTooLarge) { |
| 187 peer_.table()->SetMaxSize(1); // Also evicts all fixtures. | 187 peer_.table()->SetMaxSize(1); // Also evicts all fixtures. |
| 188 ExpectIndex(IndexOf(static_)); | 188 ExpectIndex(IndexOf(static_)); |
| 189 | 189 |
| 190 map<string, string> headers; | 190 SpdyHeaderBlock headers; |
| 191 headers[static_->name()] = static_->value(); | 191 headers[static_->name()] = static_->value(); |
| 192 CompareWithExpectedEncoding(headers); | 192 CompareWithExpectedEncoding(headers); |
| 193 | 193 |
| 194 EXPECT_EQ(0u, peer_.table_peer().dynamic_entries()->size()); | 194 EXPECT_EQ(0u, peer_.table_peer().dynamic_entries()->size()); |
| 195 } | 195 } |
| 196 | 196 |
| 197 TEST_F(HpackEncoderTest, SingleLiteralWithIndexName) { | 197 TEST_F(HpackEncoderTest, SingleLiteralWithIndexName) { |
| 198 ExpectIndexedLiteral(key_2_, "value3"); | 198 ExpectIndexedLiteral(key_2_, "value3"); |
| 199 | 199 |
| 200 map<string, string> headers; | 200 SpdyHeaderBlock headers; |
| 201 headers[key_2_->name()] = "value3"; | 201 headers[key_2_->name()] = "value3"; |
| 202 CompareWithExpectedEncoding(headers); | 202 CompareWithExpectedEncoding(headers); |
| 203 | 203 |
| 204 // A new entry was inserted and added to the reference set. | 204 // A new entry was inserted and added to the reference set. |
| 205 HpackEntry* new_entry = &peer_.table_peer().dynamic_entries()->front(); | 205 HpackEntry* new_entry = &peer_.table_peer().dynamic_entries()->front(); |
| 206 EXPECT_EQ(new_entry->name(), key_2_->name()); | 206 EXPECT_EQ(new_entry->name(), key_2_->name()); |
| 207 EXPECT_EQ(new_entry->value(), "value3"); | 207 EXPECT_EQ(new_entry->value(), "value3"); |
| 208 } | 208 } |
| 209 | 209 |
| 210 TEST_F(HpackEncoderTest, SingleLiteralWithLiteralName) { | 210 TEST_F(HpackEncoderTest, SingleLiteralWithLiteralName) { |
| 211 ExpectIndexedLiteral("key3", "value3"); | 211 ExpectIndexedLiteral("key3", "value3"); |
| 212 | 212 |
| 213 map<string, string> headers; | 213 SpdyHeaderBlock headers; |
| 214 headers["key3"] = "value3"; | 214 headers["key3"] = "value3"; |
| 215 CompareWithExpectedEncoding(headers); | 215 CompareWithExpectedEncoding(headers); |
| 216 | 216 |
| 217 HpackEntry* new_entry = &peer_.table_peer().dynamic_entries()->front(); | 217 HpackEntry* new_entry = &peer_.table_peer().dynamic_entries()->front(); |
| 218 EXPECT_EQ(new_entry->name(), "key3"); | 218 EXPECT_EQ(new_entry->name(), "key3"); |
| 219 EXPECT_EQ(new_entry->value(), "value3"); | 219 EXPECT_EQ(new_entry->value(), "value3"); |
| 220 } | 220 } |
| 221 | 221 |
| 222 TEST_F(HpackEncoderTest, SingleLiteralTooLarge) { | 222 TEST_F(HpackEncoderTest, SingleLiteralTooLarge) { |
| 223 peer_.table()->SetMaxSize(1); // Also evicts all fixtures. | 223 peer_.table()->SetMaxSize(1); // Also evicts all fixtures. |
| 224 | 224 |
| 225 ExpectIndexedLiteral("key3", "value3"); | 225 ExpectIndexedLiteral("key3", "value3"); |
| 226 | 226 |
| 227 // A header overflowing the header table is still emitted. | 227 // A header overflowing the header table is still emitted. |
| 228 // The header table is empty. | 228 // The header table is empty. |
| 229 map<string, string> headers; | 229 SpdyHeaderBlock headers; |
| 230 headers["key3"] = "value3"; | 230 headers["key3"] = "value3"; |
| 231 CompareWithExpectedEncoding(headers); | 231 CompareWithExpectedEncoding(headers); |
| 232 | 232 |
| 233 EXPECT_EQ(0u, peer_.table_peer().dynamic_entries()->size()); | 233 EXPECT_EQ(0u, peer_.table_peer().dynamic_entries()->size()); |
| 234 } | 234 } |
| 235 | 235 |
| 236 TEST_F(HpackEncoderTest, EmitThanEvict) { | 236 TEST_F(HpackEncoderTest, EmitThanEvict) { |
| 237 // |key_1_| is toggled and placed into the reference set, | 237 // |key_1_| is toggled and placed into the reference set, |
| 238 // and then immediately evicted by "key3". | 238 // and then immediately evicted by "key3". |
| 239 ExpectIndex(IndexOf(key_1_)); | 239 ExpectIndex(IndexOf(key_1_)); |
| 240 ExpectIndexedLiteral("key3", "value3"); | 240 ExpectIndexedLiteral("key3", "value3"); |
| 241 | 241 |
| 242 map<string, string> headers; | 242 SpdyHeaderBlock headers; |
| 243 headers[key_1_->name()] = key_1_->value(); | 243 headers[key_1_->name()] = key_1_->value(); |
| 244 headers["key3"] = "value3"; | 244 headers["key3"] = "value3"; |
| 245 CompareWithExpectedEncoding(headers); | 245 CompareWithExpectedEncoding(headers); |
| 246 } | 246 } |
| 247 | 247 |
| 248 TEST_F(HpackEncoderTest, CookieHeaderIsCrumbled) { | 248 TEST_F(HpackEncoderTest, CookieHeaderIsCrumbled) { |
| 249 ExpectIndex(IndexOf(cookie_a_)); | 249 ExpectIndex(IndexOf(cookie_a_)); |
| 250 ExpectIndex(IndexOf(cookie_c_)); | 250 ExpectIndex(IndexOf(cookie_c_)); |
| 251 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "e=ff"); | 251 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "e=ff"); |
| 252 | 252 |
| 253 map<string, string> headers; | 253 SpdyHeaderBlock headers; |
| 254 headers["cookie"] = "e=ff; a=bb; c=dd"; | 254 headers["cookie"] = "e=ff; a=bb; c=dd"; |
| 255 CompareWithExpectedEncoding(headers); | 255 CompareWithExpectedEncoding(headers); |
| 256 } | 256 } |
| 257 | 257 |
| 258 TEST_F(HpackEncoderTest, StringsDynamicallySelectHuffmanCoding) { | 258 TEST_F(HpackEncoderTest, StringsDynamicallySelectHuffmanCoding) { |
| 259 peer_.set_allow_huffman_compression(true); | 259 peer_.set_allow_huffman_compression(true); |
| 260 | 260 |
| 261 // Compactable string. Uses Huffman coding. | 261 // Compactable string. Uses Huffman coding. |
| 262 peer_.EmitString("feedbeef"); | 262 peer_.EmitString("feedbeef"); |
| 263 expected_.AppendPrefix(kStringLiteralHuffmanEncoded); | 263 expected_.AppendPrefix(kStringLiteralHuffmanEncoded); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 277 } | 277 } |
| 278 | 278 |
| 279 TEST_F(HpackEncoderTest, EncodingWithoutCompression) { | 279 TEST_F(HpackEncoderTest, EncodingWithoutCompression) { |
| 280 // Implementation should internally disable. | 280 // Implementation should internally disable. |
| 281 peer_.set_allow_huffman_compression(true); | 281 peer_.set_allow_huffman_compression(true); |
| 282 | 282 |
| 283 ExpectNonIndexedLiteral(":path", "/index.html"); | 283 ExpectNonIndexedLiteral(":path", "/index.html"); |
| 284 ExpectNonIndexedLiteral("cookie", "foo=bar; baz=bing"); | 284 ExpectNonIndexedLiteral("cookie", "foo=bar; baz=bing"); |
| 285 ExpectNonIndexedLiteral("hello", "goodbye"); | 285 ExpectNonIndexedLiteral("hello", "goodbye"); |
| 286 | 286 |
| 287 map<string, string> headers; | 287 SpdyHeaderBlock headers; |
| 288 headers[":path"] = "/index.html"; | 288 headers[":path"] = "/index.html"; |
| 289 headers["cookie"] = "foo=bar; baz=bing"; | 289 headers["cookie"] = "foo=bar; baz=bing"; |
| 290 headers["hello"] = "goodbye"; | 290 headers["hello"] = "goodbye"; |
| 291 | 291 |
| 292 string expected_out, actual_out; | 292 string expected_out, actual_out; |
| 293 expected_.TakeString(&expected_out); | 293 expected_.TakeString(&expected_out); |
| 294 encoder_.EncodeHeaderSetWithoutCompression(headers, &actual_out); | 294 encoder_.EncodeHeaderSetWithoutCompression(headers, &actual_out); |
| 295 EXPECT_EQ(expected_out, actual_out); | 295 EXPECT_EQ(expected_out, actual_out); |
| 296 } | 296 } |
| 297 | 297 |
| 298 TEST_F(HpackEncoderTest, MultipleEncodingPasses) { | 298 TEST_F(HpackEncoderTest, MultipleEncodingPasses) { |
| 299 // Pass 1. | 299 // Pass 1. |
| 300 { | 300 { |
| 301 map<string, string> headers; | 301 SpdyHeaderBlock headers; |
| 302 headers["key1"] = "value1"; | 302 headers["key1"] = "value1"; |
| 303 headers["cookie"] = "a=bb"; | 303 headers["cookie"] = "a=bb"; |
| 304 | 304 |
| 305 ExpectIndex(IndexOf(cookie_a_)); | 305 ExpectIndex(IndexOf(cookie_a_)); |
| 306 ExpectIndex(IndexOf(key_1_)); | 306 ExpectIndex(IndexOf(key_1_)); |
| 307 CompareWithExpectedEncoding(headers); | 307 CompareWithExpectedEncoding(headers); |
| 308 } | 308 } |
| 309 // Header table is: | 309 // Header table is: |
| 310 // 65: key1: value1 | 310 // 65: key1: value1 |
| 311 // 64: key2: value2 | 311 // 64: key2: value2 |
| 312 // 63: cookie: a=bb | 312 // 63: cookie: a=bb |
| 313 // 62: cookie: c=dd | 313 // 62: cookie: c=dd |
| 314 // Pass 2. | 314 // Pass 2. |
| 315 { | 315 { |
| 316 map<string, string> headers; | 316 SpdyHeaderBlock headers; |
| 317 headers["key2"] = "value2"; | 317 headers["key2"] = "value2"; |
| 318 headers["cookie"] = "c=dd; e=ff"; | 318 headers["cookie"] = "c=dd; e=ff"; |
| 319 | 319 |
| 320 ExpectIndex(IndexOf(cookie_c_)); | 320 ExpectIndex(IndexOf(cookie_c_)); |
| 321 // This cookie evicts |key1| from the dynamic table. | 321 // This cookie evicts |key1| from the dynamic table. |
| 322 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "e=ff"); | 322 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "e=ff"); |
| 323 // "key2: value2" | 323 // "key2: value2" |
| 324 ExpectIndex(65); | 324 ExpectIndex(65); |
| 325 | 325 |
| 326 CompareWithExpectedEncoding(headers); | 326 CompareWithExpectedEncoding(headers); |
| 327 } | 327 } |
| 328 // Header table is: | 328 // Header table is: |
| 329 // 65: key2: value2 | 329 // 65: key2: value2 |
| 330 // 64: cookie: a=bb | 330 // 64: cookie: a=bb |
| 331 // 63: cookie: c=dd | 331 // 63: cookie: c=dd |
| 332 // 62: cookie: e=ff | 332 // 62: cookie: e=ff |
| 333 // Pass 3. | 333 // Pass 3. |
| 334 { | 334 { |
| 335 map<string, string> headers; | 335 SpdyHeaderBlock headers; |
| 336 headers["key2"] = "value2"; | 336 headers["key2"] = "value2"; |
| 337 headers["cookie"] = "a=bb; b=cc; c=dd"; | 337 headers["cookie"] = "a=bb; b=cc; c=dd"; |
| 338 | 338 |
| 339 // "cookie: a=bb" | 339 // "cookie: a=bb" |
| 340 ExpectIndex(64); | 340 ExpectIndex(64); |
| 341 // This cookie evicts |key2| from the dynamic table. | 341 // This cookie evicts |key2| from the dynamic table. |
| 342 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "b=cc"); | 342 ExpectIndexedLiteral(peer_.table()->GetByName("cookie"), "b=cc"); |
| 343 // "cookie: c=dd" | 343 // "cookie: c=dd" |
| 344 ExpectIndex(64); | 344 ExpectIndex(64); |
| 345 // "key2: value2" | 345 // "key2: value2" |
| 346 ExpectIndexedLiteral("key2", "value2"); | 346 ExpectIndexedLiteral("key2", "value2"); |
| 347 | 347 |
| 348 CompareWithExpectedEncoding(headers); | 348 CompareWithExpectedEncoding(headers); |
| 349 } | 349 } |
| 350 } | 350 } |
| 351 | 351 |
| 352 TEST_F(HpackEncoderTest, PseudoHeadersFirst) { | 352 TEST_F(HpackEncoderTest, PseudoHeadersFirst) { |
| 353 map<string, string> headers; | 353 SpdyHeaderBlock headers; |
| 354 // A pseudo-header to be indexed. | 354 // A pseudo-header to be indexed. |
| 355 headers[":authority"] = "www.example.com"; | 355 headers[":authority"] = "www.example.com"; |
| 356 // A pseudo-header that should not be indexed. | 356 // A pseudo-header that should not be indexed. |
| 357 headers[":path"] = "/spam/eggs.html"; | 357 headers[":path"] = "/spam/eggs.html"; |
| 358 // A regular header which precedes ":" alphabetically, should still be encoded | 358 // A regular header which precedes ":" alphabetically, should still be encoded |
| 359 // after pseudo-headers. | 359 // after pseudo-headers. |
| 360 headers["-foo"] = "bar"; | 360 headers["-foo"] = "bar"; |
| 361 headers["foo"] = "bar"; | 361 headers["foo"] = "bar"; |
| 362 headers["cookie"] = "c=dd"; | 362 headers["cookie"] = "c=dd"; |
| 363 | 363 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 peer.DecomposeRepresentation(StringPiece("foo\0bar\0", 8), &out); | 441 peer.DecomposeRepresentation(StringPiece("foo\0bar\0", 8), &out); |
| 442 EXPECT_THAT(out, ElementsAre("foo", "bar", "")); | 442 EXPECT_THAT(out, ElementsAre("foo", "bar", "")); |
| 443 | 443 |
| 444 peer.DecomposeRepresentation(StringPiece("\0foo\0bar\0", 9), &out); | 444 peer.DecomposeRepresentation(StringPiece("\0foo\0bar\0", 9), &out); |
| 445 EXPECT_THAT(out, ElementsAre("", "foo", "bar", "")); | 445 EXPECT_THAT(out, ElementsAre("", "foo", "bar", "")); |
| 446 } | 446 } |
| 447 | 447 |
| 448 // Test that encoded headers do not have \0-delimited multiple values, as this | 448 // Test that encoded headers do not have \0-delimited multiple values, as this |
| 449 // became disallowed in HTTP/2 draft-14. | 449 // became disallowed in HTTP/2 draft-14. |
| 450 TEST_F(HpackEncoderTest, CrumbleNullByteDelimitedValue) { | 450 TEST_F(HpackEncoderTest, CrumbleNullByteDelimitedValue) { |
| 451 map<string, string> headers; | 451 SpdyHeaderBlock headers; |
| 452 // A header field to be crumbled: "spam: foo\0bar". | 452 // A header field to be crumbled: "spam: foo\0bar". |
| 453 headers["spam"] = string("foo\0bar", 7); | 453 headers["spam"] = string("foo\0bar", 7); |
| 454 | 454 |
| 455 ExpectIndexedLiteral("spam", "foo"); | 455 ExpectIndexedLiteral("spam", "foo"); |
| 456 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); | 456 expected_.AppendPrefix(kLiteralIncrementalIndexOpcode); |
| 457 expected_.AppendUint32(62); | 457 expected_.AppendUint32(62); |
| 458 expected_.AppendPrefix(kStringLiteralIdentityEncoded); | 458 expected_.AppendPrefix(kStringLiteralIdentityEncoded); |
| 459 expected_.AppendUint32(3); | 459 expected_.AppendUint32(3); |
| 460 expected_.AppendBytes("bar"); | 460 expected_.AppendBytes("bar"); |
| 461 CompareWithExpectedEncoding(headers); | 461 CompareWithExpectedEncoding(headers); |
| 462 } | 462 } |
| 463 | 463 |
| 464 } // namespace | 464 } // namespace |
| 465 | 465 |
| 466 } // namespace net | 466 } // namespace net |
| OLD | NEW |