OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/http/http_cache.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <list> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/format_macros.h" |
11 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
12 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
16 #include "base/test/simple_test_clock.h" | 18 #include "base/test/simple_test_clock.h" |
17 #include "net/base/cache_type.h" | 19 #include "net/base/cache_type.h" |
18 #include "net/base/elements_upload_data_stream.h" | 20 #include "net/base/elements_upload_data_stream.h" |
19 #include "net/base/host_port_pair.h" | 21 #include "net/base/host_port_pair.h" |
20 #include "net/base/load_flags.h" | 22 #include "net/base/load_flags.h" |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 "", | 271 "", |
270 LOAD_VALIDATE_CACHE, | 272 LOAD_VALIDATE_CACHE, |
271 "HTTP/1.1 200 OK", | 273 "HTTP/1.1 200 OK", |
272 "Cache-Control: max-age=10000\n", | 274 "Cache-Control: max-age=10000\n", |
273 base::Time(), | 275 base::Time(), |
274 "<html><body>Google Blah Blah</body></html>", | 276 "<html><body>Google Blah Blah</body></html>", |
275 TEST_MODE_SYNC_NET_START, | 277 TEST_MODE_SYNC_NET_START, |
276 &FastTransactionServer::FastNoStoreHandler, | 278 &FastTransactionServer::FastNoStoreHandler, |
277 nullptr, | 279 nullptr, |
278 0, | 280 0, |
279 0, | |
280 OK}; | 281 OK}; |
281 | 282 |
282 // This class provides a handler for kRangeGET_TransactionOK so that the range | 283 // This class provides a handler for kRangeGET_TransactionOK so that the range |
283 // request can be served on demand. | 284 // request can be served on demand. |
284 class RangeTransactionServer { | 285 class RangeTransactionServer { |
285 public: | 286 public: |
286 RangeTransactionServer() { | 287 RangeTransactionServer() { |
287 not_modified_ = false; | 288 not_modified_ = false; |
288 modified_ = false; | 289 modified_ = false; |
289 bad_200_ = false; | 290 bad_200_ = false; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
328 // A dummy extra header that must be preserved on a given request. | 329 // A dummy extra header that must be preserved on a given request. |
329 | 330 |
330 // EXTRA_HEADER_LINE doesn't include a line terminator because it | 331 // EXTRA_HEADER_LINE doesn't include a line terminator because it |
331 // will be passed to AddHeaderFromString() which doesn't accept them. | 332 // will be passed to AddHeaderFromString() which doesn't accept them. |
332 #define EXTRA_HEADER_LINE "Extra: header" | 333 #define EXTRA_HEADER_LINE "Extra: header" |
333 | 334 |
334 // EXTRA_HEADER contains a line terminator, as expected by | 335 // EXTRA_HEADER contains a line terminator, as expected by |
335 // AddHeadersFromString() (_not_ AddHeaderFromString()). | 336 // AddHeadersFromString() (_not_ AddHeaderFromString()). |
336 #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n" | 337 #define EXTRA_HEADER EXTRA_HEADER_LINE "\r\n" |
337 | 338 |
338 static const char kExtraHeaderKey[] = "Extra"; | 339 #define RETURN_DEFAULT_RANGE_HEADER "X-Return-Default-Range: 1\r\n" |
| 340 |
| 341 #define INDICATE_NO_RANGES_HEADER "X-Indicate-No-Ranges: 1\r\n" |
| 342 |
| 343 const char kExtraHeaderKey[] = "Extra"; |
| 344 |
| 345 const char kFullRangeData[] = |
| 346 "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 " |
| 347 "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 "; |
339 | 348 |
340 // Static. | 349 // Static. |
341 void RangeTransactionServer::RangeHandler(const HttpRequestInfo* request, | 350 void RangeTransactionServer::RangeHandler(const HttpRequestInfo* request, |
342 std::string* response_status, | 351 std::string* response_status, |
343 std::string* response_headers, | 352 std::string* response_headers, |
344 std::string* response_data) { | 353 std::string* response_data) { |
| 354 SCOPED_TRACE(testing::Message() << "Request headers: \n" |
| 355 << request->extra_headers.ToString()); |
345 if (request->extra_headers.IsEmpty()) { | 356 if (request->extra_headers.IsEmpty()) { |
346 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | 357 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
347 response_data->clear(); | 358 response_data->clear(); |
348 return; | 359 return; |
349 } | 360 } |
350 | 361 |
351 // We want to make sure we don't delete extra headers. | 362 // We want to make sure we don't delete extra headers. |
352 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); | 363 EXPECT_TRUE(request->extra_headers.HasHeader(kExtraHeaderKey)); |
353 | 364 |
354 bool require_auth = | 365 bool require_auth = |
(...skipping 13 matching lines...) Expand all Loading... |
368 } | 379 } |
369 | 380 |
370 std::vector<HttpByteRange> ranges; | 381 std::vector<HttpByteRange> ranges; |
371 std::string range_header; | 382 std::string range_header; |
372 if (!request->extra_headers.GetHeader(HttpRequestHeaders::kRange, | 383 if (!request->extra_headers.GetHeader(HttpRequestHeaders::kRange, |
373 &range_header) || | 384 &range_header) || |
374 !HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ || | 385 !HttpUtil::ParseRangeHeader(range_header, &ranges) || bad_200_ || |
375 ranges.size() != 1) { | 386 ranges.size() != 1) { |
376 // This is not a byte range request. We return 200. | 387 // This is not a byte range request. We return 200. |
377 response_status->assign("HTTP/1.1 200 OK"); | 388 response_status->assign("HTTP/1.1 200 OK"); |
378 response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT"); | 389 response_headers->assign("Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); |
379 response_data->assign("Not a range"); | 390 if (request->extra_headers.HasHeader("X-Indicate-No-Ranges")) { |
| 391 response_data->assign("Not a range"); |
| 392 return; |
| 393 } |
| 394 response_headers->append( |
| 395 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 396 "ETag: \"foo\"\n" |
| 397 "Accept-Ranges: bytes\n" |
| 398 "Content-Length: 80\n"); |
| 399 response_data->assign(kFullRangeData); |
380 return; | 400 return; |
381 } | 401 } |
382 | 402 |
383 // We can handle this range request. | 403 // We can handle this range request. |
384 HttpByteRange byte_range = ranges[0]; | 404 HttpByteRange byte_range = ranges[0]; |
385 | 405 |
386 if (request->extra_headers.HasHeader("X-Return-Default-Range")) { | 406 if (request->extra_headers.HasHeader("X-Return-Default-Range")) { |
387 byte_range.set_first_byte_position(40); | 407 byte_range.set_first_byte_position(40); |
388 byte_range.set_last_byte_position(49); | 408 byte_range.set_last_byte_position(49); |
389 } | 409 } |
390 | 410 |
391 if (byte_range.first_byte_position() > 79) { | 411 if (byte_range.first_byte_position() > 79) { |
392 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); | 412 response_status->assign("HTTP/1.1 416 Requested Range Not Satisfiable"); |
393 response_data->clear(); | 413 response_data->clear(); |
394 return; | 414 return; |
395 } | 415 } |
| 416 response_status->assign("HTTP/1.1 206 Partial"); |
396 | 417 |
397 EXPECT_TRUE(byte_range.ComputeBounds(80)); | 418 EXPECT_TRUE(byte_range.ComputeBounds(80)); |
398 int start = static_cast<int>(byte_range.first_byte_position()); | 419 int start = static_cast<int>(byte_range.first_byte_position()); |
399 int end = static_cast<int>(byte_range.last_byte_position()); | 420 int end = static_cast<int>(byte_range.last_byte_position()); |
400 | 421 |
401 EXPECT_LT(end, 80); | 422 EXPECT_LT(end, 80); |
402 | 423 |
403 std::string content_range = base::StringPrintf( | 424 std::string content_range = base::StringPrintf( |
404 "Content-Range: bytes %d-%d/80\n", start, end); | 425 "Content-Range: bytes %d-%d/80\n", start, end); |
405 response_headers->append(content_range); | 426 response_headers->append(content_range); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
442 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 463 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
443 "ETag: \"foo\"\n" | 464 "ETag: \"foo\"\n" |
444 "Accept-Ranges: bytes\n" | 465 "Accept-Ranges: bytes\n" |
445 "Content-Length: 10\n", | 466 "Content-Length: 10\n", |
446 base::Time(), | 467 base::Time(), |
447 "rg: 40-49 ", | 468 "rg: 40-49 ", |
448 TEST_MODE_NORMAL, | 469 TEST_MODE_NORMAL, |
449 &RangeTransactionServer::RangeHandler, | 470 &RangeTransactionServer::RangeHandler, |
450 nullptr, | 471 nullptr, |
451 0, | 472 0, |
452 0, | |
453 OK}; | 473 OK}; |
454 | 474 |
455 const char kFullRangeData[] = | |
456 "rg: 00-09 rg: 10-19 rg: 20-29 rg: 30-39 " | |
457 "rg: 40-49 rg: 50-59 rg: 60-69 rg: 70-79 "; | |
458 | |
459 // Verifies the response headers (|response|) match a partial content | 475 // Verifies the response headers (|response|) match a partial content |
460 // response for the range starting at |start| and ending at |end|. | 476 // response for the range starting at |start| and ending at |end|. |
461 void Verify206Response(std::string response, int start, int end) { | 477 void Verify206Response(std::string response, int start, int end) { |
462 std::string raw_headers( | 478 std::string raw_headers( |
463 HttpUtil::AssembleRawHeaders(response.data(), response.size())); | 479 HttpUtil::AssembleRawHeaders(response.data(), response.size())); |
464 scoped_refptr<HttpResponseHeaders> headers( | 480 scoped_refptr<HttpResponseHeaders> headers( |
465 new HttpResponseHeaders(raw_headers)); | 481 new HttpResponseHeaders(raw_headers)); |
466 | 482 |
467 ASSERT_EQ(206, headers->response_code()); | 483 ASSERT_EQ(206, headers->response_code()); |
468 | 484 |
(...skipping 1619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2088 // Write to the cache. | 2104 // Write to the cache. |
2089 MockTransaction transaction(kTypicalGET_Transaction); | 2105 MockTransaction transaction(kTypicalGET_Transaction); |
2090 transaction.request_headers = "Foo: bar\r\n"; | 2106 transaction.request_headers = "Foo: bar\r\n"; |
2091 transaction.response_headers = | 2107 transaction.response_headers = |
2092 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" | 2108 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n" |
2093 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 2109 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
2094 "Cache-Control: max-age=0\n" | 2110 "Cache-Control: max-age=0\n" |
2095 "Vary: Foo\n"; | 2111 "Vary: Foo\n"; |
2096 AddMockTransaction(&transaction); | 2112 AddMockTransaction(&transaction); |
2097 RunTransactionTest(cache.http_cache(), transaction); | 2113 RunTransactionTest(cache.http_cache(), transaction); |
2098 | |
2099 // Read from the cache and don't revalidate the entry. | 2114 // Read from the cache and don't revalidate the entry. |
2100 RevalidationServer server; | 2115 RevalidationServer server; |
2101 transaction.handler = server.Handler; | 2116 transaction.handler = server.Handler; |
2102 transaction.request_headers = "Foo: none\r\n"; | 2117 transaction.request_headers = "Foo: none\r\n"; |
| 2118 |
2103 BoundTestNetLog log; | 2119 BoundTestNetLog log; |
2104 LoadTimingInfo load_timing_info; | 2120 LoadTimingInfo load_timing_info; |
2105 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), | 2121 RunTransactionTestAndGetTiming(cache.http_cache(), transaction, log.bound(), |
2106 &load_timing_info); | 2122 &load_timing_info); |
2107 | 2123 |
2108 EXPECT_FALSE(server.EtagUsed()); | 2124 EXPECT_FALSE(server.EtagUsed()); |
2109 EXPECT_FALSE(server.LastModifiedUsed()); | 2125 EXPECT_FALSE(server.LastModifiedUsed()); |
2110 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2126 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
2111 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 2127 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
2112 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 2128 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
(...skipping 1778 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3891 | 3907 |
3892 Verify206Response(headers, 40, 49); | 3908 Verify206Response(headers, 40, 49); |
3893 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3909 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
3894 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3910 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
3895 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3911 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
3896 | 3912 |
3897 // Now verify that the cached data is not used. | 3913 // Now verify that the cached data is not used. |
3898 // Don't ask for a range. The cache will attempt to use the cached data but | 3914 // Don't ask for a range. The cache will attempt to use the cached data but |
3899 // should discard it as it cannot be validated. A regular request should go | 3915 // should discard it as it cannot be validated. A regular request should go |
3900 // to the server and a new entry should be created. | 3916 // to the server and a new entry should be created. |
3901 transaction.request_headers = EXTRA_HEADER; | 3917 transaction.request_headers = INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
3902 transaction.data = "Not a range"; | 3918 transaction.data = "Not a range"; |
3903 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 3919 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
3904 | 3920 |
3905 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | 3921 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
3906 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3922 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
3907 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3923 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
3908 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 3924 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
3909 | 3925 |
3910 // The last response was saved. | 3926 // The last response was saved. |
3911 RunTransactionTest(cache.http_cache(), transaction); | 3927 RunTransactionTest(cache.http_cache(), transaction); |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4359 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; | 4375 transaction.request_headers = "Range: bytes = 70-79\r\n" EXTRA_HEADER; |
4360 transaction.data = "rg: 70-79 "; | 4376 transaction.data = "rg: 70-79 "; |
4361 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 4377 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
4362 Verify206Response(headers, 70, 79); | 4378 Verify206Response(headers, 70, 79); |
4363 | 4379 |
4364 // Don't ask for a range. The cache will ask the server for 0-69. | 4380 // Don't ask for a range. The cache will ask the server for 0-69. |
4365 // The server returns 40-49. The cache should consider the server confused and | 4381 // The server returns 40-49. The cache should consider the server confused and |
4366 // abort caching, restarting the request. | 4382 // abort caching, restarting the request. |
4367 // The second network request should not be a byte range request so the server | 4383 // The second network request should not be a byte range request so the server |
4368 // should return 200 + "Not a range" | 4384 // should return 200 + "Not a range" |
4369 transaction.request_headers = "X-Return-Default-Range:\r\n" EXTRA_HEADER; | 4385 transaction.request_headers = |
| 4386 RETURN_DEFAULT_RANGE_HEADER INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
4370 transaction.data = "Not a range"; | 4387 transaction.data = "Not a range"; |
4371 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 4388 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
4372 | 4389 |
4373 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); | 4390 EXPECT_EQ(0U, headers.find("HTTP/1.1 200 OK\n")); |
4374 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4391 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4375 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 4392 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
4376 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4393 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4377 | 4394 |
4378 // The entry was deleted. | 4395 // The entry was deleted. |
4379 RunTransactionTest(cache.http_cache(), transaction); | 4396 RunTransactionTest(cache.http_cache(), transaction); |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4751 Verify206Response(headers, 0, 9); | 4768 Verify206Response(headers, 0, 9); |
4752 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 4769 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
4753 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 4770 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
4754 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4771 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4755 | 4772 |
4756 // Now we'll issue a request without any range that should result first in a | 4773 // Now we'll issue a request without any range that should result first in a |
4757 // 206 (when revalidating), and then in a weird standard answer: the test | 4774 // 206 (when revalidating), and then in a weird standard answer: the test |
4758 // server will not modify the response so we'll get the default range... a | 4775 // server will not modify the response so we'll get the default range... a |
4759 // real server will answer with 200. | 4776 // real server will answer with 200. |
4760 MockTransaction transaction2(kRangeGET_TransactionOK); | 4777 MockTransaction transaction2(kRangeGET_TransactionOK); |
4761 transaction2.request_headers = EXTRA_HEADER; | 4778 transaction2.request_headers = INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
4762 transaction2.load_flags |= LOAD_VALIDATE_CACHE; | 4779 transaction2.load_flags |= LOAD_VALIDATE_CACHE; |
4763 transaction2.data = "Not a range"; | 4780 transaction2.data = "Not a range"; |
4764 RangeTransactionServer handler; | 4781 RangeTransactionServer handler; |
4765 handler.set_modified(true); | 4782 handler.set_modified(true); |
4766 BoundTestNetLog log; | 4783 BoundTestNetLog log; |
4767 LoadTimingInfo load_timing_info; | 4784 LoadTimingInfo load_timing_info; |
4768 RunTransactionTestWithResponseAndGetTiming( | 4785 RunTransactionTestWithResponseAndGetTiming( |
4769 cache.http_cache(), transaction2, &headers, log.bound(), | 4786 cache.http_cache(), transaction2, &headers, log.bound(), |
4770 &load_timing_info); | 4787 &load_timing_info); |
4771 | 4788 |
(...skipping 589 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5361 | 5378 |
5362 RemoveMockTransaction(&kRangeGET_TransactionOK); | 5379 RemoveMockTransaction(&kRangeGET_TransactionOK); |
5363 } | 5380 } |
5364 | 5381 |
5365 // Tests that we don't crash when after reading from the cache we issue a | 5382 // Tests that we don't crash when after reading from the cache we issue a |
5366 // request for the next range and the server gives us a 200 synchronously. | 5383 // request for the next range and the server gives us a 200 synchronously. |
5367 TEST(HttpCache, RangeGET_FastFlakyServer) { | 5384 TEST(HttpCache, RangeGET_FastFlakyServer) { |
5368 MockHttpCache cache; | 5385 MockHttpCache cache; |
5369 | 5386 |
5370 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 5387 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
5371 transaction.request_headers = "Range: bytes = 40-\r\n" EXTRA_HEADER; | 5388 transaction.request_headers = |
| 5389 "Range: bytes = 40-\r\n" INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
5372 transaction.test_mode = TEST_MODE_SYNC_NET_START; | 5390 transaction.test_mode = TEST_MODE_SYNC_NET_START; |
5373 transaction.load_flags |= LOAD_VALIDATE_CACHE; | 5391 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
5374 | 5392 |
5375 // Write to the cache. | 5393 // Write to the cache. |
5376 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); | 5394 RunTransactionTest(cache.http_cache(), kRangeGET_TransactionOK); |
5377 | 5395 |
5378 // And now read from the cache and the network. | 5396 // And now read from the cache and the network. |
5379 RangeTransactionServer handler; | 5397 RangeTransactionServer handler; |
5380 handler.set_bad_200(true); | 5398 handler.set_bad_200(true); |
5381 transaction.data = "Not a range"; | 5399 transaction.data = "Not a range"; |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
5889 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 5907 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
5890 "ETag: \"foo\"\n" | 5908 "ETag: \"foo\"\n" |
5891 "Accept-Ranges: bytes\n" | 5909 "Accept-Ranges: bytes\n" |
5892 "Content-Length: 50\n"); | 5910 "Content-Length: 50\n"); |
5893 CreateTruncatedEntry(raw_headers, &cache); | 5911 CreateTruncatedEntry(raw_headers, &cache); |
5894 | 5912 |
5895 // Now make a regular request. We expect the code to fail the validation and | 5913 // Now make a regular request. We expect the code to fail the validation and |
5896 // retry the request without using byte ranges. | 5914 // retry the request without using byte ranges. |
5897 std::string headers; | 5915 std::string headers; |
5898 MockTransaction transaction(kRangeGET_TransactionOK); | 5916 MockTransaction transaction(kRangeGET_TransactionOK); |
5899 transaction.request_headers = EXTRA_HEADER; | 5917 transaction.request_headers = INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
5900 transaction.data = "Not a range"; | 5918 transaction.data = "Not a range"; |
5901 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 5919 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
5902 | 5920 |
5903 // The server will return 200 instead of a byte range. | 5921 // The server will return 200 instead of a byte range. |
5904 std::string expected_headers( | 5922 std::string expected_headers( |
5905 "HTTP/1.1 200 OK\n" | 5923 "HTTP/1.1 200 OK\n" |
5906 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); | 5924 "Date: Wed, 28 Nov 2007 09:40:09 GMT\n"); |
5907 | 5925 |
5908 EXPECT_EQ(expected_headers, headers); | 5926 EXPECT_EQ(expected_headers, headers); |
5909 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 5927 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6044 std::string raw_headers("HTTP/1.1 200 OK\n" | 6062 std::string raw_headers("HTTP/1.1 200 OK\n" |
6045 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" | 6063 "Last-Modified: Sat, 18 Apr 2009 01:10:43 GMT\n" |
6046 "ETag: \"foo\"\n" | 6064 "ETag: \"foo\"\n" |
6047 "Accept-Ranges: bytes\n" | 6065 "Accept-Ranges: bytes\n" |
6048 "Content-Length: 80\n"); | 6066 "Content-Length: 80\n"); |
6049 CreateTruncatedEntry(raw_headers, &cache); | 6067 CreateTruncatedEntry(raw_headers, &cache); |
6050 | 6068 |
6051 // Now make a regular request. | 6069 // Now make a regular request. |
6052 std::string headers; | 6070 std::string headers; |
6053 MockTransaction transaction(kRangeGET_TransactionOK); | 6071 MockTransaction transaction(kRangeGET_TransactionOK); |
6054 transaction.request_headers = EXTRA_HEADER; | 6072 transaction.request_headers = INDICATE_NO_RANGES_HEADER EXTRA_HEADER; |
6055 transaction.data = "Not a range"; | 6073 transaction.data = "Not a range"; |
6056 RangeTransactionServer handler; | 6074 RangeTransactionServer handler; |
6057 handler.set_bad_200(true); | 6075 handler.set_bad_200(true); |
6058 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); | 6076 RunTransactionTestWithResponse(cache.http_cache(), transaction, &headers); |
6059 | 6077 |
6060 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6078 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6061 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6079 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
6062 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6080 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6063 | 6081 |
6064 // Verify that the disk entry was updated. | 6082 // Verify that the disk entry was updated. |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6739 base::MessageLoop::current()->RunUntilIdle(); | 6757 base::MessageLoop::current()->RunUntilIdle(); |
6740 | 6758 |
6741 // Read from the cache. This should not deadlock. | 6759 // Read from the cache. This should not deadlock. |
6742 RunTransactionTest(cache.http_cache(), transaction); | 6760 RunTransactionTest(cache.http_cache(), transaction); |
6743 | 6761 |
6744 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 6762 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6745 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 6763 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
6746 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 6764 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
6747 } | 6765 } |
6748 | 6766 |
6749 // Tests that we stop caching when told. | 6767 // Tests that we stop caching when told, even if DoneReading is called after |
6750 TEST(HttpCache, StopCachingDeletesEntry) { | 6768 // StopCaching. |
6751 MockHttpCache cache; | |
6752 TestCompletionCallback callback; | |
6753 MockHttpRequest request(kSimpleGET_Transaction); | |
6754 | |
6755 { | |
6756 scoped_ptr<HttpTransaction> trans; | |
6757 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | |
6758 | |
6759 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | |
6760 EXPECT_EQ(OK, callback.GetResult(rv)); | |
6761 | |
6762 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | |
6763 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6764 EXPECT_EQ(10, callback.GetResult(rv)); | |
6765 | |
6766 trans->StopCaching(); | |
6767 | |
6768 // We should be able to keep reading. | |
6769 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6770 EXPECT_GT(callback.GetResult(rv), 0); | |
6771 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6772 EXPECT_EQ(0, callback.GetResult(rv)); | |
6773 } | |
6774 | |
6775 // Make sure that the ActiveEntry is gone. | |
6776 base::MessageLoop::current()->RunUntilIdle(); | |
6777 | |
6778 // Verify that the entry is gone. | |
6779 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | |
6780 | |
6781 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | |
6782 EXPECT_EQ(0, cache.disk_cache()->open_count()); | |
6783 EXPECT_EQ(2, cache.disk_cache()->create_count()); | |
6784 } | |
6785 | |
6786 // Tests that we stop caching when told, even if DoneReading is called | |
6787 // after StopCaching. | |
6788 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { | 6769 TEST(HttpCache, StopCachingThenDoneReadingDeletesEntry) { |
6789 MockHttpCache cache; | 6770 MockHttpCache cache; |
6790 TestCompletionCallback callback; | 6771 TestCompletionCallback callback; |
6791 MockHttpRequest request(kSimpleGET_Transaction); | 6772 MockHttpRequest request(kSimpleGET_Transaction); |
6792 | 6773 |
6793 { | 6774 { |
6794 scoped_ptr<HttpTransaction> trans; | 6775 scoped_ptr<HttpTransaction> trans; |
6795 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | 6776 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); |
6796 | 6777 |
6797 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 6778 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6852 base::MessageLoop::current()->RunUntilIdle(); | 6833 base::MessageLoop::current()->RunUntilIdle(); |
6853 | 6834 |
6854 // Verify that the entry is gone. | 6835 // Verify that the entry is gone. |
6855 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 6836 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
6856 | 6837 |
6857 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 6838 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
6858 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 6839 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
6859 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 6840 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
6860 } | 6841 } |
6861 | 6842 |
6862 // Tests that when we are told to stop caching we don't throw away valid data. | 6843 // Even if the request is resumable, the cache is still going to throw away the |
6863 TEST(HttpCache, StopCachingSavesEntry) { | 6844 // partial entry if StopCaching() is called. This is done on the assumption that |
| 6845 // such an entry is unlikely to be consumed from the cache again. |
| 6846 TEST(HttpCache, StopCachingDeletesNewEntry) { |
6864 MockHttpCache cache; | 6847 MockHttpCache cache; |
6865 TestCompletionCallback callback; | 6848 TestCompletionCallback callback; |
6866 MockHttpRequest request(kSimpleGET_Transaction); | 6849 MockHttpRequest request(kSimpleGET_Transaction); |
6867 | 6850 |
6868 { | 6851 scoped_ptr<HttpTransaction> transaction; |
6869 scoped_ptr<HttpTransaction> trans; | 6852 ASSERT_EQ(OK, cache.CreateTransaction(&transaction)); |
6870 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | |
6871 | 6853 |
6872 // Force a response that can be resumed. | 6854 MockTransaction mock_transaction(kSimpleGET_Transaction); |
6873 MockTransaction mock_transaction(kSimpleGET_Transaction); | 6855 AddMockTransaction(&mock_transaction); |
6874 AddMockTransaction(&mock_transaction); | 6856 mock_transaction.response_headers = |
6875 mock_transaction.response_headers = "Cache-Control: max-age=10000\n" | 6857 "Cache-Control: max-age=10000\n" |
6876 "Content-Length: 42\n" | 6858 "Content-Length: 42\n" |
6877 "Etag: \"foo\"\n"; | 6859 "Accept-Ranges: bytes\n" |
| 6860 "Etag: \"foo\"\n"; |
| 6861 int rv = transaction->Start(&request, callback.callback(), BoundNetLog()); |
| 6862 ASSERT_EQ(OK, callback.GetResult(rv)); |
6878 | 6863 |
6879 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 6864 const size_t kBufferSize = 256; |
6880 EXPECT_EQ(OK, callback.GetResult(rv)); | 6865 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufferSize)); |
| 6866 rv = transaction->Read(buf.get(), 10, callback.callback()); |
| 6867 EXPECT_EQ(callback.GetResult(rv), 10); |
6881 | 6868 |
6882 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 6869 transaction->StopCaching(); |
6883 rv = trans->Read(buf.get(), 10, callback.callback()); | |
6884 EXPECT_EQ(callback.GetResult(rv), 10); | |
6885 | 6870 |
6886 trans->StopCaching(); | 6871 // Should still be able to read the remainder of the resource. Data should |
| 6872 // still be coming from the same network request, which as asserted by the |
| 6873 // network_layer()->transaction_count() test below. |
| 6874 rv = transaction->Read(buf.get(), kBufferSize, callback.callback()); |
| 6875 EXPECT_EQ(32, callback.GetResult(rv)); |
| 6876 rv = transaction->Read(buf.get(), kBufferSize, callback.callback()); |
| 6877 EXPECT_EQ(0, callback.GetResult(rv)); |
6887 | 6878 |
6888 // We should be able to keep reading. | 6879 RemoveMockTransaction(&mock_transaction); |
6889 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6890 EXPECT_GT(callback.GetResult(rv), 0); | |
6891 rv = trans->Read(buf.get(), 256, callback.callback()); | |
6892 EXPECT_EQ(callback.GetResult(rv), 0); | |
6893 | 6880 |
6894 RemoveMockTransaction(&mock_transaction); | 6881 // At this point, the cache entry should be gone. It should've been doomed and |
6895 } | 6882 // released during the previous Read() cycles. |
| 6883 disk_cache::Entry* cache_entry = nullptr; |
| 6884 ASSERT_FALSE( |
| 6885 cache.OpenBackendEntry(kSimpleGET_Transaction.url, &cache_entry)); |
| 6886 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 6887 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
6896 | 6888 |
6897 // Verify that the entry is marked as incomplete. | 6889 // StopCaching shouldn't cause any additional network requests. |
6898 disk_cache::Entry* entry; | 6890 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6899 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | |
6900 HttpResponseInfo response; | |
6901 bool truncated = false; | |
6902 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | |
6903 EXPECT_TRUE(truncated); | |
6904 entry->Close(); | |
6905 } | 6891 } |
6906 | 6892 |
6907 // Tests that we handle truncated enries when StopCaching is called. | 6893 // If there request was being fulfilled via a truncated cache entry, and the |
6908 TEST(HttpCache, StopCachingTruncatedEntry) { | 6894 // client calls StopCaching(), the transaction should remove the truncated cache |
| 6895 // entry. |
| 6896 TEST(HttpCache, StopCachingRemovesPreviousTruncatedEntry) { |
6909 MockHttpCache cache; | 6897 MockHttpCache cache; |
6910 TestCompletionCallback callback; | 6898 TestCompletionCallback callback; |
6911 MockHttpRequest request(kRangeGET_TransactionOK); | 6899 MockHttpRequest request(kRangeGET_TransactionOK); |
6912 request.extra_headers.Clear(); | 6900 request.extra_headers.Clear(); |
6913 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); | 6901 request.extra_headers.AddHeaderFromString(EXTRA_HEADER_LINE); |
6914 AddMockTransaction(&kRangeGET_TransactionOK); | 6902 AddMockTransaction(&kRangeGET_TransactionOK); |
6915 | 6903 |
6916 std::string raw_headers("HTTP/1.1 200 OK\n" | 6904 std::string raw_headers("HTTP/1.1 200 OK\n" |
6917 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" | 6905 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
6918 "ETag: \"foo\"\n" | 6906 "ETag: \"foo\"\n" |
6919 "Accept-Ranges: bytes\n" | 6907 "Accept-Ranges: bytes\n" |
6920 "Content-Length: 80\n"); | 6908 "Content-Length: 80\n"); |
6921 CreateTruncatedEntry(raw_headers, &cache); | 6909 CreateTruncatedEntry(raw_headers, &cache); |
6922 | 6910 cache.disk_cache()->ResetCounts(); |
6923 { | 6911 cache.network_layer()->ResetTransactionCount(); |
6924 // Now make a regular request. | 6912 |
6925 scoped_ptr<HttpTransaction> trans; | 6913 // Now make a regular request. |
6926 ASSERT_EQ(OK, cache.CreateTransaction(&trans)); | 6914 scoped_ptr<HttpTransaction> transaction; |
6927 | 6915 ASSERT_EQ(OK, cache.CreateTransaction(&transaction)); |
6928 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); | 6916 |
6929 EXPECT_EQ(OK, callback.GetResult(rv)); | 6917 int rv = transaction->Start(&request, callback.callback(), BoundNetLog()); |
6930 | 6918 ASSERT_EQ(OK, callback.GetResult(rv)); |
6931 scoped_refptr<IOBuffer> buf(new IOBuffer(256)); | 6919 // Start() should send a single byte range request to validate the truncated |
6932 rv = trans->Read(buf.get(), 10, callback.callback()); | 6920 // cache entry. |
6933 EXPECT_EQ(callback.GetResult(rv), 10); | 6921 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
6934 | 6922 |
6935 // This is actually going to do nothing. | 6923 const size_t kBufferSize = 256; |
6936 trans->StopCaching(); | 6924 scoped_refptr<IOBuffer> buf(new IOBuffer(kBufferSize)); |
6937 | 6925 |
6938 // We should be able to keep reading. | 6926 // The size of the resource is 80 bytes. The first 20 should be available in |
6939 rv = trans->Read(buf.get(), 256, callback.callback()); | 6927 // the truncated cache entry. Let's read the first 10 bytes which should all |
6940 EXPECT_GT(callback.GetResult(rv), 0); | 6928 // come from the cache. |
6941 rv = trans->Read(buf.get(), 256, callback.callback()); | 6929 rv = transaction->Read(buf.get(), 10, callback.callback()); |
6942 EXPECT_GT(callback.GetResult(rv), 0); | 6930 EXPECT_EQ(callback.GetResult(rv), 10); |
6943 rv = trans->Read(buf.get(), 256, callback.callback()); | 6931 |
6944 EXPECT_EQ(callback.GetResult(rv), 0); | 6932 transaction->StopCaching(); |
| 6933 |
| 6934 // We should be able to keep reading. The previous StopCaching() call switches |
| 6935 // the transaction over to a network read. Hence the next read should slurp in |
| 6936 // the remainder of the resource from the network. |
| 6937 rv = transaction->Read(buf.get(), kBufferSize, callback.callback()); |
| 6938 EXPECT_EQ(callback.GetResult(rv), 70); |
| 6939 rv = transaction->Read(buf.get(), kBufferSize, callback.callback()); |
| 6940 EXPECT_EQ(callback.GetResult(rv), 0); |
| 6941 |
| 6942 // The cache entry should be gone now. |
| 6943 disk_cache::Entry* entry = nullptr; |
| 6944 ASSERT_FALSE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); |
| 6945 |
| 6946 // Transaction opens existing entry, but doesn't create any new entries. |
| 6947 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 6948 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 6949 |
| 6950 // Two network requests: |
| 6951 // - 1 request for validation. |
| 6952 // - 1 request for the range 10-. |
| 6953 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 6954 RemoveMockTransaction(&kRangeGET_TransactionOK); |
| 6955 } |
| 6956 |
| 6957 TEST(HttpCache, StopCachingKeepsSparseEntry) { |
| 6958 MockHttpCache cache; |
| 6959 TestCompletionCallback callback; |
| 6960 |
| 6961 // Create a sparse entry which contains 10 bytes at offset 20 out of an 80 |
| 6962 // byte resource. |
| 6963 MockTransaction mock_transaction(kRangeGET_TransactionOK); |
| 6964 mock_transaction.handler = nullptr; |
| 6965 mock_transaction.request_headers = "Range: bytes = 20-29\r\n" EXTRA_HEADER; |
| 6966 mock_transaction.response_headers = |
| 6967 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 6968 "ETag: \"foo\"\n" |
| 6969 "Accept-Ranges: bytes\n" |
| 6970 "Content-Range: bytes 20-29/80\n" |
| 6971 "Content-Length: 10\n"; |
| 6972 mock_transaction.data = "rg: 20-29 "; |
| 6973 AddMockTransaction(&mock_transaction); |
| 6974 std::string headers; |
| 6975 RunTransactionTestWithResponse(cache.http_cache(), mock_transaction, |
| 6976 &headers); |
| 6977 |
| 6978 // Verify our assumptions. There should be sparse entry here. |
| 6979 disk_cache::Entry* cache_entry; |
| 6980 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 6981 EXPECT_TRUE(cache_entry->CouldBeSparse()); |
| 6982 cache_entry->Close(); |
| 6983 RemoveMockTransaction(&mock_transaction); |
| 6984 |
| 6985 cache.network_layer()->ResetTransactionCount(); |
| 6986 cache.disk_cache()->ResetCounts(); |
| 6987 |
| 6988 // Prepare a request to read the entire contents of the resource. This should |
| 6989 // usually hope between network -> cache -> network. But not today. |
| 6990 mock_transaction.request_headers = EXTRA_HEADER; |
| 6991 mock_transaction.handler = kRangeGET_TransactionOK.handler; |
| 6992 mock_transaction.response_headers = |
| 6993 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 6994 "ETag: \"foo\"\n" |
| 6995 "Accept-Ranges: bytes\n" |
| 6996 "Content-Length: 80\n"; |
| 6997 AddMockTransaction(&mock_transaction); |
| 6998 MockHttpRequest request(mock_transaction); |
| 6999 |
| 7000 scoped_ptr<HttpTransaction> transaction; |
| 7001 ASSERT_EQ(OK, cache.CreateTransaction(&transaction)); |
| 7002 int rv = transaction->Start(&request, callback.callback(), BoundNetLog()); |
| 7003 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7004 |
| 7005 const size_t kBufferSize = 256; |
| 7006 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 7007 |
| 7008 rv = transaction->Read(buffer.get(), 10, callback.callback()); |
| 7009 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7010 |
| 7011 // This should've caused a network read of 10 bytes. |
| 7012 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| 7013 |
| 7014 // StopCaching() should cause the transaction to switch entirely to the |
| 7015 // network for the remainder of the response. Since the existing network |
| 7016 // transaction doesn't cover the entirety of the resource, the cache creates a |
| 7017 // new network transaction. |
| 7018 transaction->StopCaching(); |
| 7019 |
| 7020 // Since it's coming from a single network request, we can slurp the remainder |
| 7021 // of the resource in one request. |
| 7022 rv = transaction->Read(buffer.get(), kBufferSize, callback.callback()); |
| 7023 EXPECT_EQ(70, callback.GetResult(rv)); |
| 7024 |
| 7025 // And we hit EOF. |
| 7026 rv = transaction->Read(buffer.get(), kBufferSize, callback.callback()); |
| 7027 EXPECT_EQ(0, callback.GetResult(rv)); |
| 7028 |
| 7029 // Another network transaction is issued for the remainder of the resource |
| 7030 // following the StopCaching call. |
| 7031 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7032 |
| 7033 // And the sparse entry is still here. |
| 7034 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 7035 cache_entry->Close(); |
| 7036 RemoveMockTransaction(&mock_transaction); |
| 7037 } |
| 7038 |
| 7039 // If StopCaching() is called while serving the entire request out of cache, the |
| 7040 // transaction is expected to ignore the StopCaching() call and continue serving |
| 7041 // the remainder of the resource out of the cache. |
| 7042 TEST(HttpCache, StopCachingKeepsFullEntry) { |
| 7043 MockHttpCache cache; |
| 7044 TestCompletionCallback callback; |
| 7045 |
| 7046 MockTransaction mock_transaction(kRangeGET_TransactionOK); |
| 7047 mock_transaction.handler = nullptr; |
| 7048 mock_transaction.request_headers = EXTRA_HEADER; |
| 7049 mock_transaction.status = "HTTP/1.1 200 OK"; |
| 7050 mock_transaction.response_headers = |
| 7051 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7052 "ETag: \"foo\"\n" |
| 7053 "Content-Length: 80\n"; |
| 7054 mock_transaction.data = kFullRangeData; |
| 7055 AddMockTransaction(&mock_transaction); |
| 7056 std::string headers; |
| 7057 RunTransactionTestWithResponse(cache.http_cache(), mock_transaction, |
| 7058 &headers); |
| 7059 |
| 7060 cache.network_layer()->ResetTransactionCount(); |
| 7061 cache.disk_cache()->ResetCounts(); |
| 7062 |
| 7063 mock_transaction.request_headers = EXTRA_HEADER; |
| 7064 mock_transaction.handler = kRangeGET_TransactionOK.handler; |
| 7065 mock_transaction.response_headers = |
| 7066 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7067 "ETag: \"foo\"\n" |
| 7068 "Accept-Ranges: bytes\n" |
| 7069 "Content-Length: 80\n"; |
| 7070 AddMockTransaction(&mock_transaction); |
| 7071 MockHttpRequest request(mock_transaction); |
| 7072 |
| 7073 scoped_ptr<HttpTransaction> transaction; |
| 7074 ASSERT_EQ(OK, cache.CreateTransaction(&transaction)); |
| 7075 int rv = transaction->Start(&request, callback.callback(), BoundNetLog()); |
| 7076 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7077 |
| 7078 const size_t kBufferSize = 256; |
| 7079 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 7080 |
| 7081 rv = transaction->Read(buffer.get(), 10, callback.callback()); |
| 7082 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7083 |
| 7084 // This should've caused a cache read of 10 bytes. No new network transactions |
| 7085 // are expected. |
| 7086 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 7087 |
| 7088 // StopCaching() doesn't do anything because the entire resource is cached |
| 7089 // already. We proceed with the cache read. |
| 7090 transaction->StopCaching(); |
| 7091 |
| 7092 // Since it's all coming from a single network request, we can slurp the |
| 7093 // remainder of the resource in one request. |
| 7094 rv = transaction->Read(buffer.get(), kBufferSize, callback.callback()); |
| 7095 EXPECT_EQ(70, callback.GetResult(rv)); |
| 7096 |
| 7097 // And we hit EOF. |
| 7098 rv = transaction->Read(buffer.get(), kBufferSize, callback.callback()); |
| 7099 EXPECT_EQ(0, callback.GetResult(rv)); |
| 7100 |
| 7101 // We should've only seen one network transactions at this point. |
| 7102 EXPECT_EQ(0, cache.network_layer()->transaction_count()); |
| 7103 |
| 7104 // And the entry is still here. |
| 7105 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7106 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 7107 disk_cache::Entry* cache_entry; |
| 7108 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 7109 cache_entry->Close(); |
| 7110 |
| 7111 RemoveMockTransaction(&mock_transaction); |
| 7112 } |
| 7113 |
| 7114 namespace { |
| 7115 |
| 7116 const MockTransaction kResumableGET_Transaction = { |
| 7117 kRangeGET_TransactionOK.url, "GET", base::Time(), EXTRA_HEADER, 0, |
| 7118 "HTTP/1.1 200 OK", |
| 7119 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7120 "Date: Sat, 18 Apr 2015 01:10:43 GMT\n" |
| 7121 "ETag: \"foo\"\n" |
| 7122 "Accept-Ranges: bytes\n" |
| 7123 "Content-Length: 80\n", |
| 7124 base::Time(), kFullRangeData, TEST_MODE_NORMAL, |
| 7125 &RangeTransactionServer::RangeHandler}; |
| 7126 |
| 7127 } // namespace |
| 7128 |
| 7129 TEST(HttpCache, StopCachingReleasesCacheLockForNewCacheEntry) { |
| 7130 MockHttpCache cache; |
| 7131 TestCompletionCallback callback; |
| 7132 ScopedMockTransaction mock_transaction(kResumableGET_Transaction); |
| 7133 MockHttpRequest mock_request(mock_transaction); |
| 7134 |
| 7135 scoped_ptr<HttpTransaction> first_transaction; |
| 7136 ASSERT_EQ(OK, cache.CreateTransaction(&first_transaction)); |
| 7137 |
| 7138 int rv = first_transaction->Start(&mock_request, callback.callback(), |
| 7139 BoundNetLog()); |
| 7140 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7141 |
| 7142 const size_t kBufferSize = 1024; |
| 7143 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
| 7144 rv = first_transaction->Read(buf.get(), 10, callback.callback()); |
| 7145 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7146 |
| 7147 first_transaction->StopCaching(); |
| 7148 |
| 7149 rv = first_transaction->Read(buf.get(), 10, callback.callback()); |
| 7150 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7151 |
| 7152 // Since the cache entry is no longer locked by |first_transaction|, we can |
| 7153 // start another transaction without blocking. The test should time out if |
| 7154 // this is not the case (i.e. the Start() call has to wait indefinitely[*] |
| 7155 // for the previous transaction to release the lock). |
| 7156 // |
| 7157 // [*]: The wait isn't really indefinite since the Start() call times out |
| 7158 // (currently after 20 seconds) and proceeds without the cache entry. Failure |
| 7159 // to release the cahe entry is accounted for by the previous |
| 7160 // disk_cache()->GetEntryCount() expectation. |
| 7161 scoped_ptr<HttpTransaction> second_transaction; |
| 7162 ASSERT_EQ(OK, cache.CreateTransaction(&second_transaction)); |
| 7163 rv = second_transaction->Start(&mock_request, callback.callback(), |
| 7164 BoundNetLog()); |
| 7165 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7166 |
| 7167 // The first cache entry was gone by the time second_transaction started. |
| 7168 // Hence each transaction creates a cache entry. |
| 7169 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 7170 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 7171 |
| 7172 // The cache entry from second_transaction is still around. |
| 7173 disk_cache::Entry* cache_entry = nullptr; |
| 7174 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 7175 cache_entry->Close(); |
| 7176 |
| 7177 // One network request for each cache transaction. |
| 7178 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 7179 } |
| 7180 |
| 7181 TEST(HttpCache, StopCachingReleasesCacheLockForTruncatedEntry) { |
| 7182 MockHttpCache cache; |
| 7183 TestCompletionCallback callback; |
| 7184 ScopedMockTransaction mock_transaction(kResumableGET_Transaction); |
| 7185 MockHttpRequest mock_request(kResumableGET_Transaction); |
| 7186 std::string raw_headers( |
| 7187 "HTTP/1.1 200 OK\n" |
| 7188 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7189 "Date: Sat, 18 Apr 2015 01:10:43 GMT\n" |
| 7190 "ETag: \"foo\"\n" |
| 7191 "Accept-Ranges: bytes\n" |
| 7192 "Content-Length: 80\n"); |
| 7193 CreateTruncatedEntry(raw_headers, &cache); |
| 7194 cache.disk_cache()->ResetCounts(); |
| 7195 cache.network_layer()->ResetTransactionCount(); |
| 7196 |
| 7197 scoped_ptr<HttpTransaction> first_transaction; |
| 7198 int rv = cache.CreateTransaction(&first_transaction); |
| 7199 ASSERT_EQ(OK, rv); |
| 7200 ASSERT_TRUE(first_transaction.get()); |
| 7201 |
| 7202 rv = first_transaction->Start(&mock_request, callback.callback(), |
| 7203 BoundNetLog()); |
| 7204 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7205 |
| 7206 first_transaction->StopCaching(); |
| 7207 |
| 7208 const int kBufferSize = 1024; |
| 7209 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
| 7210 rv = first_transaction->Read(buf.get(), 10, callback.callback()); |
| 7211 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7212 |
| 7213 // Now that the cache lock is released, another transaction can begin. The |
| 7214 // Start() call should complete without delay. The test should timeout if this |
| 7215 // is not the case (i.e. the Start() call has to wait indefinitely[*] for the |
| 7216 // previous transaction to release the lock). |
| 7217 // |
| 7218 // [*]: The wait isn't really indefinite since the Start() call times out |
| 7219 // (currently after 20 seconds) and proceeds without the cache entry. This |
| 7220 // case is accounted for by the network_layer()->transaction_count() |
| 7221 // expectation below. |
| 7222 scoped_ptr<HttpTransaction> second_transaction; |
| 7223 rv = cache.CreateTransaction(&second_transaction); |
| 7224 ASSERT_EQ(OK, rv); |
| 7225 ASSERT_TRUE(second_transaction); |
| 7226 rv = second_transaction->Start(&mock_request, callback.callback(), |
| 7227 BoundNetLog()); |
| 7228 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7229 |
| 7230 // first_transaction: 1 open cache entry + 2 network fetches. |
| 7231 // second_transaction: 1 create cache entry + 1 network fetch. |
| 7232 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 7233 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7234 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 7235 } |
| 7236 |
| 7237 TEST(HttpCache, StopCachingReleasesCacheLockForSparseEntry) { |
| 7238 MockHttpCache cache; |
| 7239 TestCompletionCallback callback; |
| 7240 |
| 7241 // Create a sparse entry which contains 10 bytes at offset 20 out of an 80 |
| 7242 // byte resource. |
| 7243 MockTransaction mock_transaction(kRangeGET_TransactionOK); |
| 7244 mock_transaction.handler = nullptr; |
| 7245 mock_transaction.request_headers = "Range: bytes = 20-29\r\n" EXTRA_HEADER; |
| 7246 mock_transaction.response_headers = |
| 7247 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7248 "ETag: \"foo\"\n" |
| 7249 "Accept-Ranges: bytes\n" |
| 7250 "Content-Range: bytes 20-29/80\n" |
| 7251 "Content-Length: 10\n"; |
| 7252 mock_transaction.data = "rg: 20-29 "; |
| 7253 AddMockTransaction(&mock_transaction); |
| 7254 std::string headers; |
| 7255 RunTransactionTestWithResponse(cache.http_cache(), mock_transaction, |
| 7256 &headers); |
| 7257 |
| 7258 // Verify our assumptions. There should be sparse entry here. |
| 7259 disk_cache::Entry* cache_entry; |
| 7260 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 7261 EXPECT_TRUE(cache_entry->CouldBeSparse()); |
| 7262 cache_entry->Close(); |
| 7263 RemoveMockTransaction(&mock_transaction); |
| 7264 |
| 7265 cache.network_layer()->ResetTransactionCount(); |
| 7266 cache.disk_cache()->ResetCounts(); |
| 7267 |
| 7268 // Prepare a request to read the entire contents of the resource. This should |
| 7269 // usually hope between network -> cache -> network. But not today. |
| 7270 mock_transaction.request_headers = EXTRA_HEADER; |
| 7271 mock_transaction.handler = kRangeGET_TransactionOK.handler; |
| 7272 mock_transaction.response_headers = |
| 7273 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7274 "ETag: \"foo\"\n" |
| 7275 "Accept-Ranges: bytes\n" |
| 7276 "Content-Length: 10\n"; |
| 7277 AddMockTransaction(&mock_transaction); |
| 7278 MockHttpRequest request(mock_transaction); |
| 7279 |
| 7280 scoped_ptr<HttpTransaction> first_transaction; |
| 7281 ASSERT_EQ(OK, cache.CreateTransaction(&first_transaction)); |
| 7282 int rv = |
| 7283 first_transaction->Start(&request, callback.callback(), BoundNetLog()); |
| 7284 ASSERT_EQ(OK, callback.GetResult(rv)); |
| 7285 |
| 7286 const size_t kBufferSize = 256; |
| 7287 scoped_refptr<IOBuffer> buffer(new IOBuffer(kBufferSize)); |
| 7288 |
| 7289 rv = first_transaction->Read(buffer.get(), 10, callback.callback()); |
| 7290 EXPECT_EQ(10, callback.GetResult(rv)); |
| 7291 |
| 7292 // Create another cache transaction. This one is blocked on the first |
| 7293 // transaction's cache lock. |
| 7294 scoped_ptr<HttpTransaction> second_transaction; |
| 7295 TestCompletionCallback second_callback; |
| 7296 ASSERT_EQ(OK, cache.CreateTransaction(&second_transaction)); |
| 7297 rv = second_transaction->Start(&request, second_callback.callback(), |
| 7298 BoundNetLog()); |
| 7299 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 7300 |
| 7301 // StopCaching() should cause the transaction to switch entirely to the |
| 7302 // network for the remainder of the response. Since the existing network |
| 7303 // transaction doesn't cover the entirety of the resource, the cache creates a |
| 7304 // new network transaction. |
| 7305 first_transaction->StopCaching(); |
| 7306 |
| 7307 // Since it's coming from a single network request, we can slurp the remaining |
| 7308 // 70 bytes of the resource in one request. Let's leave a little bit on the |
| 7309 // pipe so that the stream doesn't hit EOF. |
| 7310 rv = first_transaction->Read(buffer.get(), 60, callback.callback()); |
| 7311 EXPECT_EQ(60, callback.GetResult(rv)); |
| 7312 |
| 7313 // The second transaction should become unblocked at this point. |
| 7314 EXPECT_EQ(OK, second_callback.GetResult(ERR_IO_PENDING)); |
| 7315 |
| 7316 // And it can read the entire resource too, but needs to alternate between |
| 7317 // cache and network.. |
| 7318 // 0-9 from cache (due to the first Read() on first_transaction). |
| 7319 EXPECT_EQ(10, second_callback.GetResult(second_transaction->Read( |
| 7320 buffer.get(), kBufferSize, second_callback.callback()))); |
| 7321 |
| 7322 // 10-19 From network: |
| 7323 EXPECT_EQ(10, second_callback.GetResult(second_transaction->Read( |
| 7324 buffer.get(), kBufferSize, second_callback.callback()))); |
| 7325 |
| 7326 // 20-29 From cache (due to test setup): |
| 7327 EXPECT_EQ(10, second_callback.GetResult(second_transaction->Read( |
| 7328 buffer.get(), kBufferSize, second_callback.callback()))); |
| 7329 |
| 7330 // 30-79 From network again: |
| 7331 EXPECT_EQ(50, second_callback.GetResult(second_transaction->Read( |
| 7332 buffer.get(), kBufferSize, second_callback.callback()))); |
| 7333 EXPECT_EQ(0, second_callback.GetResult(second_transaction->Read( |
| 7334 buffer.get(), kBufferSize, second_callback.callback()))); |
| 7335 |
| 7336 // Now drain first_transaction as well. |
| 7337 EXPECT_EQ(10, callback.GetResult(first_transaction->Read( |
| 7338 buffer.get(), kBufferSize, callback.callback()))); |
| 7339 EXPECT_EQ(0, callback.GetResult(first_transaction->Read( |
| 7340 buffer.get(), kBufferSize, callback.callback()))); |
| 7341 |
| 7342 // first_transaction: |
| 7343 // 1 open cache entry + 2 network fetches (0-19 and 10-79). |
| 7344 // second_transaction: |
| 7345 // 1 open cache entry + 2 network fetches (10-19 and 30-79). |
| 7346 EXPECT_EQ(0, cache.disk_cache()->create_count()); |
| 7347 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
| 7348 |
| 7349 // Even though there are two HttpCache::OpenEntry() calls, there's only one |
| 7350 // DiskCache::OpenEntry call because HttpCache already has an active entry. |
| 7351 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 7352 |
| 7353 // And the sparse entry is still here. |
| 7354 ASSERT_TRUE(cache.OpenBackendEntry(mock_transaction.url, &cache_entry)); |
| 7355 cache_entry->Close(); |
| 7356 RemoveMockTransaction(&mock_transaction); |
| 7357 } |
| 7358 |
| 7359 namespace { |
| 7360 |
| 7361 enum class TransactionPhase { |
| 7362 BEFORE_FIRST_READ, |
| 7363 AFTER_FIRST_READ, |
| 7364 AFTER_NETWORK_READ |
| 7365 }; |
| 7366 |
| 7367 using CacheInitializer = void (*)(MockHttpCache*); |
| 7368 using HugeCacheTestConfiguration = |
| 7369 std::pair<TransactionPhase, CacheInitializer>; |
| 7370 |
| 7371 class HttpCacheHugeResourceTest |
| 7372 : public ::testing::TestWithParam<HugeCacheTestConfiguration> { |
| 7373 public: |
| 7374 static std::list<HugeCacheTestConfiguration> GetTestModes(); |
| 7375 static std::list<HugeCacheTestConfiguration> kTestModes; |
| 7376 |
| 7377 // CacheInitializer callbacks. These are used to initialize the cache |
| 7378 // depending on the test run configuration. |
| 7379 |
| 7380 // Initializes a cache containing a truncated entry containing the first 20 |
| 7381 // bytes of the reponse body. |
| 7382 static void SetupTruncatedCacheEntry(MockHttpCache* cache); |
| 7383 |
| 7384 // Initializes a cache containing a sparse entry. The first 10 bytes are |
| 7385 // present in the cache. |
| 7386 static void SetupPrefixSparseCacheEntry(MockHttpCache* cache); |
| 7387 |
| 7388 // Initializes a cache containing a sparse entry. The 10 bytes at offset |
| 7389 // 99999990 are present in the cache. |
| 7390 static void SetupInfixSparseCacheEntry(MockHttpCache* cache); |
| 7391 |
| 7392 protected: |
| 7393 static void ExpectByteRangeTransactionHandler( |
| 7394 const net::HttpRequestInfo* request, |
| 7395 std::string* response_status, |
| 7396 std::string* response_headers, |
| 7397 std::string* response_data); |
| 7398 static int LargeBufferReader(int64 content_length, |
| 7399 int64 offset, |
| 7400 net::IOBuffer* buf, |
| 7401 int buf_len); |
| 7402 |
| 7403 static void SetFlagOnBeforeNetworkStart(bool* started, bool* /* defer */); |
| 7404 static void StopCachingOnBeforeNetworkStart(HttpTransaction* transaction, |
| 7405 bool* /* defer */); |
| 7406 |
| 7407 // Size of resource to be tested. |
| 7408 static const int64 kTotalSize = |
| 7409 5000000000LL; // Five beeeeeelllliooonn bytes! |
| 7410 }; |
| 7411 |
| 7412 const int64 HttpCacheHugeResourceTest::kTotalSize; |
| 7413 |
| 7414 // static |
| 7415 void HttpCacheHugeResourceTest::ExpectByteRangeTransactionHandler( |
| 7416 const net::HttpRequestInfo* request, |
| 7417 std::string* response_status, |
| 7418 std::string* response_headers, |
| 7419 std::string* response_data) { |
| 7420 std::string if_range; |
| 7421 EXPECT_TRUE(request->extra_headers.GetHeader( |
| 7422 net::HttpRequestHeaders::kIfRange, &if_range)); |
| 7423 EXPECT_EQ("\"foo\"", if_range); |
| 7424 |
| 7425 std::string range_header; |
| 7426 EXPECT_TRUE(request->extra_headers.GetHeader(net::HttpRequestHeaders::kRange, |
| 7427 &range_header)); |
| 7428 std::vector<net::HttpByteRange> ranges; |
| 7429 |
| 7430 EXPECT_TRUE(net::HttpUtil::ParseRangeHeader(range_header, &ranges)); |
| 7431 ASSERT_EQ(1u, ranges.size()); |
| 7432 |
| 7433 net::HttpByteRange range = ranges[0]; |
| 7434 EXPECT_TRUE(range.HasFirstBytePosition()); |
| 7435 int64 last_byte_position = |
| 7436 range.HasLastBytePosition() ? range.last_byte_position() : kTotalSize - 1; |
| 7437 |
| 7438 response_status->assign("HTTP/1.1 206 Partial"); |
| 7439 response_headers->assign(base::StringPrintf( |
| 7440 "Content-Range: bytes %" PRId64 "-%" PRId64 "/%" PRId64 |
| 7441 "\n" |
| 7442 "Content-Length: %" PRId64 "\n", |
| 7443 range.first_byte_position(), last_byte_position, kTotalSize, |
| 7444 last_byte_position - range.first_byte_position() + 1)); |
| 7445 } |
| 7446 |
| 7447 // static |
| 7448 int HttpCacheHugeResourceTest::LargeBufferReader(int64 content_length, |
| 7449 int64 offset, |
| 7450 net::IOBuffer* buf, |
| 7451 int buf_len) { |
| 7452 // This test involves reading multiple gigabytes of data. To make it run in a |
| 7453 // reasonable amount of time, we are going to skip filling the buffer with |
| 7454 // data. Instead the test relies on verifying that the count of bytes expected |
| 7455 // at the end is correct. |
| 7456 EXPECT_LT(0, content_length); |
| 7457 EXPECT_LE(offset, content_length); |
| 7458 int num = std::min(static_cast<int64>(buf_len), content_length - offset); |
| 7459 return num; |
| 7460 } |
| 7461 |
| 7462 // static |
| 7463 void HttpCacheHugeResourceTest::SetFlagOnBeforeNetworkStart(bool* started, |
| 7464 bool* /* defer */) { |
| 7465 *started = true; |
| 7466 } |
| 7467 |
| 7468 // static |
| 7469 void HttpCacheHugeResourceTest::StopCachingOnBeforeNetworkStart( |
| 7470 HttpTransaction* transaction, |
| 7471 bool* /* defer */) { |
| 7472 transaction->StopCaching(); |
| 7473 } |
| 7474 |
| 7475 // static |
| 7476 void HttpCacheHugeResourceTest::SetupTruncatedCacheEntry(MockHttpCache* cache) { |
| 7477 ScopedMockTransaction scoped_transaction(kRangeGET_TransactionOK); |
| 7478 std::string cached_headers = base::StringPrintf( |
| 7479 "HTTP/1.1 200 OK\n" |
| 7480 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7481 "ETag: \"foo\"\n" |
| 7482 "Accept-Ranges: bytes\n" |
| 7483 "Content-Length: %" PRId64 "\n", |
| 7484 kTotalSize); |
| 7485 CreateTruncatedEntry(cached_headers, cache); |
| 7486 } |
| 7487 |
| 7488 // static |
| 7489 void HttpCacheHugeResourceTest::SetupPrefixSparseCacheEntry( |
| 7490 MockHttpCache* cache) { |
| 7491 MockTransaction transaction(kRangeGET_TransactionOK); |
| 7492 transaction.handler = nullptr; |
| 7493 transaction.request_headers = "Range: bytes = 0-9\r\n" EXTRA_HEADER; |
| 7494 transaction.response_headers = |
| 7495 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7496 "ETag: \"foo\"\n" |
| 7497 "Accept-Ranges: bytes\n" |
| 7498 "Content-Range: bytes 0-9/5000000000\n" |
| 7499 "Content-Length: 10\n"; |
| 7500 AddMockTransaction(&transaction); |
| 7501 std::string headers; |
| 7502 RunTransactionTestWithResponse(cache->http_cache(), transaction, &headers); |
| 7503 RemoveMockTransaction(&transaction); |
| 7504 } |
| 7505 |
| 7506 // static |
| 7507 void HttpCacheHugeResourceTest::SetupInfixSparseCacheEntry( |
| 7508 MockHttpCache* cache) { |
| 7509 MockTransaction transaction(kRangeGET_TransactionOK); |
| 7510 transaction.handler = nullptr; |
| 7511 transaction.request_headers = |
| 7512 "Range: bytes = 99999990-99999999\r\n" EXTRA_HEADER; |
| 7513 transaction.response_headers = |
| 7514 "Last-Modified: Sat, 18 Apr 2007 01:10:43 GMT\n" |
| 7515 "ETag: \"foo\"\n" |
| 7516 "Accept-Ranges: bytes\n" |
| 7517 "Content-Range: bytes 99999990-99999999/5000000000\n" |
| 7518 "Content-Length: 10\n"; |
| 7519 AddMockTransaction(&transaction); |
| 7520 std::string headers; |
| 7521 RunTransactionTestWithResponse(cache->http_cache(), transaction, &headers); |
| 7522 RemoveMockTransaction(&transaction); |
| 7523 } |
| 7524 |
| 7525 // static |
| 7526 std::list<HugeCacheTestConfiguration> |
| 7527 HttpCacheHugeResourceTest::GetTestModes() { |
| 7528 std::list<HugeCacheTestConfiguration> test_modes; |
| 7529 const TransactionPhase kTransactionPhases[] = { |
| 7530 TransactionPhase::BEFORE_FIRST_READ, TransactionPhase::AFTER_FIRST_READ, |
| 7531 TransactionPhase::AFTER_NETWORK_READ}; |
| 7532 const CacheInitializer kInitializers[] = {&SetupTruncatedCacheEntry, |
| 7533 &SetupPrefixSparseCacheEntry, |
| 7534 &SetupInfixSparseCacheEntry}; |
| 7535 |
| 7536 for (const auto phase : kTransactionPhases) |
| 7537 for (const auto initializer : kInitializers) |
| 7538 test_modes.push_back(std::make_pair(phase, initializer)); |
| 7539 |
| 7540 return test_modes; |
| 7541 } |
| 7542 |
| 7543 // static |
| 7544 std::list<HugeCacheTestConfiguration> HttpCacheHugeResourceTest::kTestModes = |
| 7545 HttpCacheHugeResourceTest::GetTestModes(); |
| 7546 |
| 7547 INSTANTIATE_TEST_CASE_P( |
| 7548 _, |
| 7549 HttpCacheHugeResourceTest, |
| 7550 ::testing::ValuesIn(HttpCacheHugeResourceTest::kTestModes)); |
| 7551 |
| 7552 } // namespace |
| 7553 |
| 7554 // Test what happens when StopCaching() is called while reading a huge resource |
| 7555 // fetched via GET. Various combinations of cache state and when StopCaching() |
| 7556 // is called is controlled by the parameter passed into the test via the |
| 7557 // INSTANTIATE_TEST_CASE_P invocation above. |
| 7558 TEST_P(HttpCacheHugeResourceTest, |
| 7559 StopCachingFollowedByReadForHugeTruncatedResource) { |
| 7560 // This test is going to be repeated for all combinations of TransactionPhase |
| 7561 // and CacheInitializers returned by GetTestModes(). |
| 7562 const TransactionPhase stop_caching_phase = GetParam().first; |
| 7563 const CacheInitializer cache_initializer = GetParam().second; |
| 7564 |
| 7565 MockHttpCache cache; |
| 7566 (*cache_initializer)(&cache); |
| 7567 |
| 7568 MockTransaction transaction(kSimpleGET_Transaction); |
| 7569 transaction.url = kRangeGET_TransactionOK.url; |
| 7570 transaction.handler = &ExpectByteRangeTransactionHandler; |
| 7571 transaction.read_handler = &LargeBufferReader; |
| 7572 ScopedMockTransaction scoped_transaction(transaction); |
| 7573 |
| 7574 MockHttpRequest request(transaction); |
| 7575 net::TestCompletionCallback callback; |
| 7576 scoped_ptr<net::HttpTransaction> http_transaction; |
| 7577 int rv = cache.http_cache()->CreateTransaction(net::DEFAULT_PRIORITY, |
| 7578 &http_transaction); |
| 7579 ASSERT_EQ(net::OK, rv); |
| 7580 ASSERT_TRUE(http_transaction.get()); |
| 7581 |
| 7582 bool network_transaction_started = false; |
| 7583 if (stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7584 http_transaction->SetBeforeNetworkStartCallback( |
| 7585 base::Bind(&SetFlagOnBeforeNetworkStart, &network_transaction_started)); |
6945 } | 7586 } |
6946 | 7587 |
6947 // Verify that the disk entry was updated. | 7588 rv = http_transaction->Start(&request, callback.callback(), |
6948 disk_cache::Entry* entry; | 7589 net::BoundNetLog()); |
6949 ASSERT_TRUE(cache.OpenBackendEntry(kRangeGET_TransactionOK.url, &entry)); | 7590 rv = callback.GetResult(rv); |
6950 EXPECT_EQ(80, entry->GetDataSize(1)); | 7591 ASSERT_EQ(net::OK, rv); |
6951 bool truncated = true; | 7592 |
6952 HttpResponseInfo response; | 7593 if (stop_caching_phase == TransactionPhase::BEFORE_FIRST_READ) |
6953 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | 7594 http_transaction->StopCaching(); |
6954 EXPECT_FALSE(truncated); | 7595 |
6955 entry->Close(); | 7596 int64 total_bytes_received = 0; |
6956 | 7597 |
6957 RemoveMockTransaction(&kRangeGET_TransactionOK); | 7598 EXPECT_EQ(kTotalSize, |
| 7599 http_transaction->GetResponseInfo()->headers->GetContentLength()); |
| 7600 do { |
| 7601 // This test simulates reading Gigabytes of data. Buffer size is set to 1MB |
| 7602 // to reduce the number of reads and speedup the test. |
| 7603 const int kBufferSize = 1024 * 1024; |
| 7604 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kBufferSize)); |
| 7605 rv = http_transaction->Read(buf.get(), kBufferSize, callback.callback()); |
| 7606 rv = callback.GetResult(rv); |
| 7607 |
| 7608 if (stop_caching_phase == TransactionPhase::AFTER_FIRST_READ && |
| 7609 total_bytes_received == 0) { |
| 7610 http_transaction->StopCaching(); |
| 7611 } |
| 7612 |
| 7613 if (rv > 0) |
| 7614 total_bytes_received += rv; |
| 7615 |
| 7616 if (network_transaction_started && |
| 7617 stop_caching_phase == TransactionPhase::AFTER_NETWORK_READ) { |
| 7618 http_transaction->StopCaching(); |
| 7619 network_transaction_started = false; |
| 7620 } |
| 7621 } while (rv > 0); |
| 7622 |
| 7623 // The only verification we are going to do is that the received resource has |
| 7624 // the correct size. This is sufficient to verify that the state machine |
| 7625 // didn't terminate abruptly due to the StopCaching() call. |
| 7626 EXPECT_EQ(kTotalSize, total_bytes_received); |
6958 } | 7627 } |
6959 | 7628 |
6960 // Tests that we detect truncated resources from the net when there is | 7629 // Tests that we detect truncated resources from the net when there is |
6961 // a Content-Length header. | 7630 // a Content-Length header. |
6962 TEST(HttpCache, TruncatedByContentLength) { | 7631 TEST(HttpCache, TruncatedByContentLength) { |
6963 MockHttpCache cache; | 7632 MockHttpCache cache; |
6964 TestCompletionCallback callback; | 7633 TestCompletionCallback callback; |
6965 | 7634 |
6966 MockTransaction transaction(kSimpleGET_Transaction); | 7635 MockTransaction transaction(kSimpleGET_Transaction); |
6967 AddMockTransaction(&transaction); | 7636 AddMockTransaction(&transaction); |
(...skipping 586 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7554 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 8223 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
7555 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 8224 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
7556 EXPECT_TRUE(response_info.was_cached); | 8225 EXPECT_TRUE(response_info.was_cached); |
7557 | 8226 |
7558 // The new SSL state is reported. | 8227 // The new SSL state is reported. |
7559 EXPECT_EQ(status2, response_info.ssl_info.connection_status); | 8228 EXPECT_EQ(status2, response_info.ssl_info.connection_status); |
7560 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); | 8229 EXPECT_TRUE(cert2->Equals(response_info.ssl_info.cert.get())); |
7561 } | 8230 } |
7562 | 8231 |
7563 } // namespace net | 8232 } // namespace net |
OLD | NEW |