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 <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 // write to the cache | 211 // write to the cache |
212 | 212 |
213 std::unique_ptr<HttpTransaction> trans; | 213 std::unique_ptr<HttpTransaction> trans; |
214 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); | 214 int rv = cache->CreateTransaction(DEFAULT_PRIORITY, &trans); |
215 EXPECT_THAT(rv, IsOk()); | 215 EXPECT_THAT(rv, IsOk()); |
216 ASSERT_TRUE(trans.get()); | 216 ASSERT_TRUE(trans.get()); |
217 | 217 |
218 rv = trans->Start(&request, callback.callback(), net_log); | 218 rv = trans->Start(&request, callback.callback(), net_log); |
219 if (rv == ERR_IO_PENDING) | 219 if (rv == ERR_IO_PENDING) |
220 rv = callback.WaitForResult(); | 220 rv = callback.WaitForResult(); |
221 ASSERT_EQ(trans_info.return_code, rv); | 221 ASSERT_EQ(trans_info.start_return_code, rv); |
222 | 222 |
223 if (OK != rv) | 223 if (OK != rv) |
224 return; | 224 return; |
225 | 225 |
226 const HttpResponseInfo* response = trans->GetResponseInfo(); | 226 const HttpResponseInfo* response = trans->GetResponseInfo(); |
227 ASSERT_TRUE(response); | 227 ASSERT_TRUE(response); |
228 | 228 |
229 if (response_info) | 229 if (response_info) |
230 *response_info = *response; | 230 *response_info = *response; |
231 | 231 |
(...skipping 837 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1069 MockTransaction transaction(kSimpleGET_Transaction); | 1069 MockTransaction transaction(kSimpleGET_Transaction); |
1070 transaction.response_headers = "Cache-Control: no-cache\n"; | 1070 transaction.response_headers = "Cache-Control: no-cache\n"; |
1071 | 1071 |
1072 AddMockTransaction(&transaction); | 1072 AddMockTransaction(&transaction); |
1073 RunTransactionTest(cache.http_cache(), transaction); | 1073 RunTransactionTest(cache.http_cache(), transaction); |
1074 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 1074 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 1075 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
1076 RemoveMockTransaction(&transaction); | 1076 RemoveMockTransaction(&transaction); |
1077 | 1077 |
1078 // Network failure with error; should fail but have was_cached set. | 1078 // Network failure with error; should fail but have was_cached set. |
1079 transaction.return_code = ERR_FAILED; | 1079 transaction.start_return_code = ERR_FAILED; |
1080 AddMockTransaction(&transaction); | 1080 AddMockTransaction(&transaction); |
1081 | 1081 |
1082 MockHttpRequest request(transaction); | 1082 MockHttpRequest request(transaction); |
1083 TestCompletionCallback callback; | 1083 TestCompletionCallback callback; |
1084 std::unique_ptr<HttpTransaction> trans; | 1084 std::unique_ptr<HttpTransaction> trans; |
1085 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); | 1085 int rv = cache.http_cache()->CreateTransaction(DEFAULT_PRIORITY, &trans); |
1086 EXPECT_THAT(rv, IsOk()); | 1086 EXPECT_THAT(rv, IsOk()); |
1087 ASSERT_TRUE(trans.get()); | 1087 ASSERT_TRUE(trans.get()); |
1088 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); | 1088 rv = trans->Start(&request, callback.callback(), NetLogWithSource()); |
1089 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); | 1089 EXPECT_THAT(callback.GetResult(rv), IsError(ERR_FAILED)); |
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1880 } | 1880 } |
1881 | 1881 |
1882 // Allow all requests to move from the Create queue to the active entry. | 1882 // Allow all requests to move from the Create queue to the active entry. |
1883 base::RunLoop().RunUntilIdle(); | 1883 base::RunLoop().RunUntilIdle(); |
1884 | 1884 |
1885 // The first request should be a writer at this point, and the subsequent | 1885 // The first request should be a writer at this point, and the subsequent |
1886 // requests should have passed the validation phase and created their own | 1886 // requests should have passed the validation phase and created their own |
1887 // entries since none of them matched the headers of the earlier one. | 1887 // entries since none of them matched the headers of the earlier one. |
1888 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 1888 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
1889 | 1889 |
1890 // Note that there are only 3 entries created and not 5 since every other | |
1891 // transaction would have gone to the network. | |
1892 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1890 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
1893 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1891 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1894 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1892 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
1895 | 1893 |
1896 // All requests depend on the writer, and the writer is between Start and | 1894 // All requests depend on the writer, and the writer is between Start and |
1897 // Read, i.e. idle. | 1895 // Read, i.e. idle. |
1898 for (auto& context : context_list) { | 1896 for (auto& context : context_list) { |
1899 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); | 1897 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
1900 } | 1898 } |
1901 | 1899 |
1902 for (auto& context : context_list) { | 1900 for (auto& context : context_list) { |
1903 if (context->result == ERR_IO_PENDING) | 1901 if (context->result == ERR_IO_PENDING) |
1904 context->result = context->callback.WaitForResult(); | 1902 context->result = context->callback.WaitForResult(); |
1905 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 1903 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
1906 } | 1904 } |
1907 | 1905 |
1908 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1906 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
1909 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1907 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
1910 EXPECT_EQ(3, cache.disk_cache()->create_count()); | 1908 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1909 } |
| 1910 |
| 1911 // Parallel validation results in 200 for 1 transaction and validation matches |
| 1912 // for subsequent transactions. |
| 1913 TEST(HttpCache, SimpleGET_ParallelValidationNoMatch1) { |
| 1914 MockHttpCache cache; |
| 1915 MockHttpRequest request(kSimpleGET_Transaction); |
| 1916 |
| 1917 MockTransaction transaction(kSimpleGET_Transaction); |
| 1918 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
| 1919 MockHttpRequest validate_request(transaction); |
| 1920 std::vector<std::unique_ptr<Context>> context_list; |
| 1921 const int kNumTransactions = 5; |
| 1922 for (int i = 0; i < kNumTransactions; ++i) { |
| 1923 context_list.push_back(base::MakeUnique<Context>()); |
| 1924 auto& c = context_list[i]; |
| 1925 c->result = cache.CreateTransaction(&c->trans); |
| 1926 ASSERT_THAT(c->result, IsOk()); |
| 1927 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1928 |
| 1929 MockHttpRequest* this_request = &request; |
| 1930 if (i == 1) |
| 1931 this_request = &validate_request; |
| 1932 |
| 1933 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1934 NetLogWithSource()); |
| 1935 } |
| 1936 |
| 1937 // All requests are waiting for the active entry. |
| 1938 for (auto& context : context_list) { |
| 1939 EXPECT_EQ(LOAD_STATE_WAITING_FOR_CACHE, context->trans->GetLoadState()); |
| 1940 } |
| 1941 |
| 1942 // Allow all requests to move from the Create queue to the active entry. |
| 1943 base::RunLoop().RunUntilIdle(); |
| 1944 |
| 1945 // The first request should be a writer at this point, and the second |
| 1946 // request should have passed the validation phase and created a new |
| 1947 // entry. Rest of them should be added to the new entry. |
| 1948 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
| 1949 EXPECT_EQ(3, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
| 1950 |
| 1951 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1952 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1953 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1954 |
| 1955 // All requests depend on the writer, and the writer is between Start and |
| 1956 // Read, i.e. idle. |
| 1957 for (auto& context : context_list) { |
| 1958 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1959 } |
| 1960 |
| 1961 EXPECT_EQ(1, cache.disk_cache()->doomed_count()); |
| 1962 |
| 1963 for (size_t i = 0; i < context_list.size(); i++) { |
| 1964 if (context_list[i]->result == ERR_IO_PENDING) |
| 1965 context_list[i]->result = context_list[i]->callback.WaitForResult(); |
| 1966 |
| 1967 ReadAndVerifyTransaction(context_list[i]->trans.get(), |
| 1968 kSimpleGET_Transaction); |
| 1969 } |
| 1970 |
| 1971 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 1972 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1973 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
1911 } | 1974 } |
1912 | 1975 |
1913 // Tests that a GET followed by a DELETE results in DELETE immediately starting | 1976 // Tests that a GET followed by a DELETE results in DELETE immediately starting |
1914 // the headers phase and the entry is doomed. | 1977 // the headers phase and the entry is doomed. |
1915 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { | 1978 TEST(HttpCache, SimpleGET_ParallelValidationDelete) { |
1916 MockHttpCache cache; | 1979 MockHttpCache cache; |
1917 | 1980 |
1918 MockHttpRequest request(kSimpleGET_Transaction); | 1981 MockHttpRequest request(kSimpleGET_Transaction); |
1919 request.load_flags |= LOAD_VALIDATE_CACHE; | 1982 request.load_flags |= LOAD_VALIDATE_CACHE; |
1920 | 1983 |
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2149 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2212 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2150 | 2213 |
2151 // Resume network start for headers_transaction. It will doom the entry as it | 2214 // Resume network start for headers_transaction. It will doom the entry as it |
2152 // will be a 200 and will go to network for the response body. | 2215 // will be a 200 and will go to network for the response body. |
2153 auto& context = context_list[3]; | 2216 auto& context = context_list[3]; |
2154 context->trans->ResumeNetworkStart(); | 2217 context->trans->ResumeNetworkStart(); |
2155 | 2218 |
2156 // The pending transactions will be added to a new entry. | 2219 // The pending transactions will be added to a new entry. |
2157 base::RunLoop().RunUntilIdle(); | 2220 base::RunLoop().RunUntilIdle(); |
2158 | 2221 |
2159 EXPECT_EQ(1, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); | 2222 EXPECT_EQ(2, cache.GetCountDoneHeadersQueue(kSimpleGET_Transaction.url)); |
2160 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2223 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2161 | 2224 |
2162 // Complete the rest of the transactions. | 2225 // Complete the rest of the transactions. |
2163 for (int i = 2; i < kNumTransactions; ++i) { | 2226 for (int i = 2; i < kNumTransactions; ++i) { |
2164 auto& c = context_list[i]; | 2227 auto& c = context_list[i]; |
2165 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); | 2228 ReadAndVerifyTransaction(c->trans.get(), kSimpleGET_Transaction); |
2166 } | 2229 } |
2167 | 2230 |
2168 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2231 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
2169 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2232 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2170 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2233 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
2171 } | 2234 } |
2172 | 2235 |
2173 // Tests that a transaction is in validated queue and writer is destroyed | 2236 // Tests that a transaction is in validated queue and writer is destroyed |
2174 // leading to restarting the validated transaction. | 2237 // leading to restarting the validated transaction. |
2175 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { | 2238 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriter) { |
2176 MockHttpCache cache; | 2239 MockHttpCache cache; |
2177 | 2240 |
2178 MockHttpRequest request(kSimpleGET_Transaction); | 2241 MockHttpRequest request(kSimpleGET_Transaction); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2223 | 2286 |
2224 base::RunLoop().RunUntilIdle(); | 2287 base::RunLoop().RunUntilIdle(); |
2225 | 2288 |
2226 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2289 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2227 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2290 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2228 | 2291 |
2229 // Resume network start for the transaction the second time. | 2292 // Resume network start for the transaction the second time. |
2230 c->trans->ResumeNetworkStart(); | 2293 c->trans->ResumeNetworkStart(); |
2231 base::RunLoop().RunUntilIdle(); | 2294 base::RunLoop().RunUntilIdle(); |
2232 | 2295 |
2233 // Headers transaction would have doomed the new entry created. | |
2234 EXPECT_TRUE( | |
2235 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
2236 | |
2237 // Complete the rest of the transactions. | 2296 // Complete the rest of the transactions. |
2238 for (auto& context : context_list) { | 2297 for (auto& context : context_list) { |
2239 if (!context) | 2298 if (!context) |
2240 continue; | 2299 continue; |
2241 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 2300 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
2242 } | 2301 } |
2243 | 2302 |
2244 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 2303 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
2245 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2304 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2246 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2305 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2247 } | 2306 } |
2248 | 2307 |
2249 // Tests when a writer is destroyed mid-read and entry is marked as truncated, | 2308 // Tests when a writer is destroyed mid-read and entry is marked as truncated, |
2250 // it should lead to restarting the dependent transactions. | 2309 // it should lead to restarting the dependent transactions. |
2251 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriterTruncateEntry) { | 2310 TEST(HttpCache, SimpleGET_ParallelValidationCancelWriterTruncateEntry) { |
2252 MockHttpCache cache; | 2311 MockHttpCache cache; |
2253 | 2312 |
2254 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 2313 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
2255 transaction.response_headers = | 2314 transaction.response_headers = |
2256 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" | 2315 "Last-Modified: Wed, 28 Nov 2007 00:40:09 GMT\n" |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2365 | 2424 |
2366 base::RunLoop().RunUntilIdle(); | 2425 base::RunLoop().RunUntilIdle(); |
2367 | 2426 |
2368 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); | 2427 EXPECT_TRUE(cache.IsWriterPresent(kSimpleGET_Transaction.url)); |
2369 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2428 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
2370 | 2429 |
2371 // Resume network start for the transaction the second time. | 2430 // Resume network start for the transaction the second time. |
2372 c->trans->ResumeNetworkStart(); | 2431 c->trans->ResumeNetworkStart(); |
2373 base::RunLoop().RunUntilIdle(); | 2432 base::RunLoop().RunUntilIdle(); |
2374 | 2433 |
2375 // Headers transaction would have doomed the new entry created. | |
2376 EXPECT_TRUE( | |
2377 cache.disk_cache()->IsDiskEntryDoomed(kSimpleGET_Transaction.url)); | |
2378 | |
2379 // Complete the rest of the transactions. | 2434 // Complete the rest of the transactions. |
2380 for (auto& context : context_list) { | 2435 for (auto& context : context_list) { |
2381 if (!context) | 2436 if (!context) |
2382 continue; | 2437 continue; |
2383 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); | 2438 ReadAndVerifyTransaction(context->trans.get(), kSimpleGET_Transaction); |
2384 } | 2439 } |
2385 | 2440 |
2386 EXPECT_EQ(4, cache.network_layer()->transaction_count()); | 2441 EXPECT_EQ(4, cache.network_layer()->transaction_count()); |
2387 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2442 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2388 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2443 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2389 } | 2444 } |
2390 | 2445 |
2391 // Tests that a transaction is currently in headers phase and is destroyed | 2446 // Tests that a transaction is currently in headers phase and is destroyed |
2392 // leading to destroying the entry. | 2447 // leading to destroying the entry. |
2393 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { | 2448 TEST(HttpCache, SimpleGET_ParallelValidationCancelHeaders) { |
2394 MockHttpCache cache; | 2449 MockHttpCache cache; |
2395 | 2450 |
2396 MockHttpRequest request(kSimpleGET_Transaction); | 2451 MockHttpRequest request(kSimpleGET_Transaction); |
2397 | 2452 |
2398 const int kNumTransactions = 2; | 2453 const int kNumTransactions = 2; |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2672 | 2727 |
2673 // Allow all requests to move from the Create queue to the active entry. | 2728 // Allow all requests to move from the Create queue to the active entry. |
2674 base::RunLoop().RunUntilIdle(); | 2729 base::RunLoop().RunUntilIdle(); |
2675 | 2730 |
2676 // The first request should be a writer at this point, and the subsequent | 2731 // The first request should be a writer at this point, and the subsequent |
2677 // requests should have completed validation. Since the validation does not | 2732 // requests should have completed validation. Since the validation does not |
2678 // result in a match, a new entry would be created. | 2733 // result in a match, a new entry would be created. |
2679 | 2734 |
2680 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2735 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
2681 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2736 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2682 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2737 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2683 | 2738 |
2684 // Now, make sure that the second request asks for the entry not to be stored. | 2739 // Now, make sure that the second request asks for the entry not to be stored. |
2685 request_handler.set_no_store(true); | 2740 request_handler.set_no_store(true); |
2686 | 2741 |
2687 for (int i = 0; i < kNumTransactions; ++i) { | 2742 for (int i = 0; i < kNumTransactions; ++i) { |
2688 Context* c = context_list[i].get(); | 2743 Context* c = context_list[i].get(); |
2689 if (c->result == ERR_IO_PENDING) | 2744 if (c->result == ERR_IO_PENDING) |
2690 c->result = c->callback.WaitForResult(); | 2745 c->result = c->callback.WaitForResult(); |
2691 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); | 2746 ReadAndVerifyTransaction(c->trans.get(), kFastNoStoreGET_Transaction); |
2692 context_list[i].reset(); | 2747 context_list[i].reset(); |
2693 } | 2748 } |
2694 | 2749 |
2695 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 2750 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
2696 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 2751 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
2697 EXPECT_EQ(2, cache.disk_cache()->create_count()); | 2752 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
2698 | 2753 |
2699 RemoveMockTransaction(&kFastNoStoreGET_Transaction); | 2754 RemoveMockTransaction(&kFastNoStoreGET_Transaction); |
2700 } | 2755 } |
2701 | 2756 |
2702 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 2757 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
2703 MockHttpCache cache; | 2758 MockHttpCache cache; |
2704 | 2759 |
2705 MockHttpRequest request(kSimpleGET_Transaction); | 2760 MockHttpRequest request(kSimpleGET_Transaction); |
2706 | 2761 |
2707 std::vector<std::unique_ptr<Context>> context_list; | 2762 std::vector<std::unique_ptr<Context>> context_list; |
(...skipping 1743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4451 MockTransaction transaction(kSimpleGET_Transaction); | 4506 MockTransaction transaction(kSimpleGET_Transaction); |
4452 AddMockTransaction(&transaction); | 4507 AddMockTransaction(&transaction); |
4453 | 4508 |
4454 // Populate the cache. | 4509 // Populate the cache. |
4455 RunTransactionTest(cache.http_cache(), transaction); | 4510 RunTransactionTest(cache.http_cache(), transaction); |
4456 | 4511 |
4457 // Load from cache. | 4512 // Load from cache. |
4458 transaction.method = "HEAD"; | 4513 transaction.method = "HEAD"; |
4459 transaction.request_headers = "Range: bytes = 0-4\r\n"; | 4514 transaction.request_headers = "Range: bytes = 0-4\r\n"; |
4460 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 4515 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
4461 transaction.return_code = ERR_CACHE_MISS; | 4516 transaction.start_return_code = ERR_CACHE_MISS; |
4462 RunTransactionTest(cache.http_cache(), transaction); | 4517 RunTransactionTest(cache.http_cache(), transaction); |
4463 | 4518 |
4464 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 4519 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
4465 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4520 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4466 RemoveMockTransaction(&transaction); | 4521 RemoveMockTransaction(&transaction); |
4467 } | 4522 } |
4468 | 4523 |
4469 // Tests that a HEAD request can be served from a partialy cached resource. | 4524 // Tests that a HEAD request can be served from a partialy cached resource. |
4470 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { | 4525 TEST(HttpCache, SimpleHEAD_WithCachedRanges) { |
4471 MockHttpCache cache; | 4526 MockHttpCache cache; |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4617 | 4672 |
4618 // Update the cache. | 4673 // Update the cache. |
4619 transaction.method = "HEAD"; | 4674 transaction.method = "HEAD"; |
4620 transaction.data = ""; | 4675 transaction.data = ""; |
4621 RunTransactionTest(cache.http_cache(), transaction); | 4676 RunTransactionTest(cache.http_cache(), transaction); |
4622 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 4677 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
4623 | 4678 |
4624 // Load from the cache. | 4679 // Load from the cache. |
4625 transaction.method = "GET"; | 4680 transaction.method = "GET"; |
4626 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 4681 transaction.load_flags |= LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
4627 transaction.return_code = ERR_CACHE_MISS; | 4682 transaction.start_return_code = ERR_CACHE_MISS; |
4628 RunTransactionTest(cache.http_cache(), transaction); | 4683 RunTransactionTest(cache.http_cache(), transaction); |
4629 | 4684 |
4630 RemoveMockTransaction(&transaction); | 4685 RemoveMockTransaction(&transaction); |
4631 } | 4686 } |
4632 | 4687 |
4633 // Tests that we do not cache the response of a PUT. | 4688 // Tests that we do not cache the response of a PUT. |
4634 TEST(HttpCache, SimplePUT_Miss) { | 4689 TEST(HttpCache, SimplePUT_Miss) { |
4635 MockHttpCache cache; | 4690 MockHttpCache cache; |
4636 | 4691 |
4637 MockTransaction transaction(kSimplePOST_Transaction); | 4692 MockTransaction transaction(kSimplePOST_Transaction); |
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4894 // Tests that we don't invalidate entries after a failed network transaction. | 4949 // Tests that we don't invalidate entries after a failed network transaction. |
4895 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { | 4950 TEST(HttpCache, SimpleGET_DontInvalidateOnFailure) { |
4896 MockHttpCache cache; | 4951 MockHttpCache cache; |
4897 | 4952 |
4898 // Populate the cache. | 4953 // Populate the cache. |
4899 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 4954 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
4900 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 4955 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
4901 | 4956 |
4902 // Fail the network request. | 4957 // Fail the network request. |
4903 MockTransaction transaction(kSimpleGET_Transaction); | 4958 MockTransaction transaction(kSimpleGET_Transaction); |
4904 transaction.return_code = ERR_FAILED; | 4959 transaction.start_return_code = ERR_FAILED; |
4905 transaction.load_flags |= LOAD_VALIDATE_CACHE; | 4960 transaction.load_flags |= LOAD_VALIDATE_CACHE; |
4906 | 4961 |
4907 AddMockTransaction(&transaction); | 4962 AddMockTransaction(&transaction); |
4908 RunTransactionTest(cache.http_cache(), transaction); | 4963 RunTransactionTest(cache.http_cache(), transaction); |
4909 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 4964 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
4910 RemoveMockTransaction(&transaction); | 4965 RemoveMockTransaction(&transaction); |
4911 | 4966 |
4912 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 4967 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
4913 transaction.return_code = OK; | 4968 transaction.start_return_code = OK; |
4914 AddMockTransaction(&transaction); | 4969 AddMockTransaction(&transaction); |
4915 RunTransactionTest(cache.http_cache(), transaction); | 4970 RunTransactionTest(cache.http_cache(), transaction); |
4916 | 4971 |
4917 // Make sure the transaction didn't reach the network. | 4972 // Make sure the transaction didn't reach the network. |
4918 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 4973 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
4919 RemoveMockTransaction(&transaction); | 4974 RemoveMockTransaction(&transaction); |
4920 } | 4975 } |
4921 | 4976 |
4922 TEST(HttpCache, RangeGET_SkipsCache) { | 4977 TEST(HttpCache, RangeGET_SkipsCache) { |
4923 MockHttpCache cache; | 4978 MockHttpCache cache; |
(...skipping 3054 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7978 transaction.request_headers = "accept-encoding: gzip\r\n"; | 8033 transaction.request_headers = "accept-encoding: gzip\r\n"; |
7979 transaction.response_headers = | 8034 transaction.response_headers = |
7980 "Vary: accept-encoding\n" | 8035 "Vary: accept-encoding\n" |
7981 "Cache-Control: max-age=10000\n"; | 8036 "Cache-Control: max-age=10000\n"; |
7982 RunTransactionTest(cache.http_cache(), transaction); | 8037 RunTransactionTest(cache.http_cache(), transaction); |
7983 | 8038 |
7984 // Change the request headers so that the request doesn't match due to vary. | 8039 // Change the request headers so that the request doesn't match due to vary. |
7985 // The request should fail. | 8040 // The request should fail. |
7986 transaction.load_flags = LOAD_ONLY_FROM_CACHE; | 8041 transaction.load_flags = LOAD_ONLY_FROM_CACHE; |
7987 transaction.request_headers = "accept-encoding: foo\r\n"; | 8042 transaction.request_headers = "accept-encoding: foo\r\n"; |
7988 transaction.return_code = ERR_CACHE_MISS; | 8043 transaction.start_return_code = ERR_CACHE_MISS; |
7989 RunTransactionTest(cache.http_cache(), transaction); | 8044 RunTransactionTest(cache.http_cache(), transaction); |
7990 | 8045 |
7991 // Change the load flags to ignore vary checks, the request should now hit. | 8046 // Change the load flags to ignore vary checks, the request should now hit. |
7992 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK; | 8047 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_VARY_CHECK; |
7993 transaction.return_code = OK; | 8048 transaction.start_return_code = OK; |
7994 RunTransactionTest(cache.http_cache(), transaction); | 8049 RunTransactionTest(cache.http_cache(), transaction); |
7995 } | 8050 } |
7996 | 8051 |
7997 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE | 8052 // Tests that we only return valid entries with LOAD_ONLY_FROM_CACHE |
7998 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. | 8053 // transactions unless LOAD_SKIP_CACHE_VALIDATION is set. |
7999 TEST(HttpCache, ValidLoadOnlyFromCache) { | 8054 TEST(HttpCache, ValidLoadOnlyFromCache) { |
8000 MockHttpCache cache; | 8055 MockHttpCache cache; |
8001 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 8056 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
8002 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); | 8057 cache.http_cache()->SetClockForTesting(base::WrapUnique(clock)); |
8003 cache.network_layer()->SetClock(clock); | 8058 cache.network_layer()->SetClock(clock); |
8004 | 8059 |
8005 // Write a resource that will expire in 100 seconds. | 8060 // Write a resource that will expire in 100 seconds. |
8006 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 8061 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
8007 transaction.response_headers = "Cache-Control: max-age=100\n"; | 8062 transaction.response_headers = "Cache-Control: max-age=100\n"; |
8008 RunTransactionTest(cache.http_cache(), transaction); | 8063 RunTransactionTest(cache.http_cache(), transaction); |
8009 | 8064 |
8010 // Move forward in time such that the cached response is no longer valid. | 8065 // Move forward in time such that the cached response is no longer valid. |
8011 clock->Advance(base::TimeDelta::FromSeconds(101)); | 8066 clock->Advance(base::TimeDelta::FromSeconds(101)); |
8012 | 8067 |
8013 // Skipping cache validation should still return a response. | 8068 // Skipping cache validation should still return a response. |
8014 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; | 8069 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_SKIP_CACHE_VALIDATION; |
8015 RunTransactionTest(cache.http_cache(), transaction); | 8070 RunTransactionTest(cache.http_cache(), transaction); |
8016 | 8071 |
8017 // If the cache entry is checked for validitiy, it should fail. | 8072 // If the cache entry is checked for validitiy, it should fail. |
8018 transaction.load_flags = LOAD_ONLY_FROM_CACHE; | 8073 transaction.load_flags = LOAD_ONLY_FROM_CACHE; |
8019 transaction.return_code = ERR_CACHE_MISS; | 8074 transaction.start_return_code = ERR_CACHE_MISS; |
8020 RunTransactionTest(cache.http_cache(), transaction); | 8075 RunTransactionTest(cache.http_cache(), transaction); |
8021 } | 8076 } |
8022 | 8077 |
8023 TEST(HttpCache, InvalidLoadFlagCombination) { | 8078 TEST(HttpCache, InvalidLoadFlagCombination) { |
8024 MockHttpCache cache; | 8079 MockHttpCache cache; |
8025 | 8080 |
8026 // Put the resource in the cache. | 8081 // Put the resource in the cache. |
8027 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 8082 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
8028 | 8083 |
8029 // Now try to fetch it again, but with a flag combination disallowing both | 8084 // Now try to fetch it again, but with a flag combination disallowing both |
8030 // cache and network access. | 8085 // cache and network access. |
8031 ScopedMockTransaction transaction(kSimpleGET_Transaction); | 8086 ScopedMockTransaction transaction(kSimpleGET_Transaction); |
8032 // DevTools relies on this combination of flags for "disable cache" mode | 8087 // DevTools relies on this combination of flags for "disable cache" mode |
8033 // when a resource is only supposed to be loaded from cache. | 8088 // when a resource is only supposed to be loaded from cache. |
8034 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE; | 8089 transaction.load_flags = LOAD_ONLY_FROM_CACHE | LOAD_BYPASS_CACHE; |
8035 transaction.return_code = ERR_CACHE_MISS; | 8090 transaction.start_return_code = ERR_CACHE_MISS; |
8036 RunTransactionTest(cache.http_cache(), transaction); | 8091 RunTransactionTest(cache.http_cache(), transaction); |
8037 } | 8092 } |
8038 | 8093 |
8039 // Tests that we can read metadata after validating the entry and with READ mode | 8094 // Tests that we can read metadata after validating the entry and with READ mode |
8040 // transactions. | 8095 // transactions. |
8041 TEST(HttpCache, ReadMetadata) { | 8096 TEST(HttpCache, ReadMetadata) { |
8042 MockHttpCache cache; | 8097 MockHttpCache cache; |
8043 | 8098 |
8044 // Write to the cache | 8099 // Write to the cache |
8045 HttpResponseInfo response; | 8100 HttpResponseInfo response; |
(...skipping 1258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9304 ASSERT_TRUE(attrs->GetDictionary( | 9359 ASSERT_TRUE(attrs->GetDictionary( |
9305 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 9360 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
9306 std::string size; | 9361 std::string size; |
9307 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 9362 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
9308 int actual_size = 0; | 9363 int actual_size = 0; |
9309 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 9364 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
9310 ASSERT_LT(0, actual_size); | 9365 ASSERT_LT(0, actual_size); |
9311 } | 9366 } |
9312 | 9367 |
9313 } // namespace net | 9368 } // namespace net |
OLD | NEW |