| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 // same value. | 140 // same value. |
| 141 EXPECT_FALSE(load_timing_info.send_start.is_null()); | 141 EXPECT_FALSE(load_timing_info.send_start.is_null()); |
| 142 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); | 142 EXPECT_EQ(load_timing_info.send_start, load_timing_info.send_end); |
| 143 | 143 |
| 144 // Set by URLRequest / URLRequestHttpJob, at a higher level. | 144 // Set by URLRequest / URLRequestHttpJob, at a higher level. |
| 145 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); | 145 EXPECT_TRUE(load_timing_info.request_start_time.is_null()); |
| 146 EXPECT_TRUE(load_timing_info.request_start.is_null()); | 146 EXPECT_TRUE(load_timing_info.request_start.is_null()); |
| 147 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); | 147 EXPECT_TRUE(load_timing_info.receive_headers_end.is_null()); |
| 148 } | 148 } |
| 149 | 149 |
| 150 void DeferNetworkStart(bool* defer) { | 150 void DeferCallback(bool* defer) { |
| 151 *defer = true; | 151 *defer = true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { | 154 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { |
| 155 public: | 155 public: |
| 156 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) | 156 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
| 157 : cache_(cache), | 157 : cache_(cache), |
| 158 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, | 158 callback_(base::Bind(&DeleteCacheCompletionCallback::OnComplete, |
| 159 base::Unretained(this))) { | 159 base::Unretained(this))) { |
| 160 } | 160 } |
| (...skipping 1310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1471 c->result = c->callback.WaitForResult(); | 1471 c->result = c->callback.WaitForResult(); |
| 1472 | 1472 |
| 1473 ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK); | 1473 ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK); |
| 1474 } | 1474 } |
| 1475 | 1475 |
| 1476 EXPECT_EQ(5, cache.network_layer()->transaction_count()); | 1476 EXPECT_EQ(5, cache.network_layer()->transaction_count()); |
| 1477 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 1477 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1478 EXPECT_EQ(5, cache.disk_cache()->create_count()); | 1478 EXPECT_EQ(5, cache.disk_cache()->create_count()); |
| 1479 } | 1479 } |
| 1480 | 1480 |
| 1481 // Tests that if a transaction is dooming the entry and the entry was doomed by |
| 1482 // another transaction that was not part of the entry and created a new entry, |
| 1483 // the new entry should not be incorrectly doomed. (crbug.com/736993) |
| 1484 TEST(HttpCache, RangeGET_ParallelValidationNoMatchDoomEntry) { |
| 1485 MockHttpCache cache; |
| 1486 |
| 1487 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 1488 MockHttpRequest request(transaction); |
| 1489 |
| 1490 MockTransaction dooming_transaction(kRangeGET_TransactionOK); |
| 1491 dooming_transaction.load_flags |= LOAD_BYPASS_CACHE; |
| 1492 MockHttpRequest dooming_request(dooming_transaction); |
| 1493 |
| 1494 std::vector<std::unique_ptr<Context>> context_list; |
| 1495 const int kNumTransactions = 3; |
| 1496 |
| 1497 for (int i = 0; i < kNumTransactions; ++i) { |
| 1498 context_list.push_back(base::MakeUnique<Context>()); |
| 1499 auto& c = context_list[i]; |
| 1500 |
| 1501 c->result = cache.CreateTransaction(&c->trans); |
| 1502 ASSERT_THAT(c->result, IsOk()); |
| 1503 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1504 |
| 1505 MockHttpRequest* this_request = &request; |
| 1506 |
| 1507 if (i == 2) |
| 1508 this_request = &dooming_request; |
| 1509 |
| 1510 if (i == 1) |
| 1511 cache.disk_cache()->SetDefer(kRangeGET_TransactionOK.url, DEFER_READ); |
| 1512 |
| 1513 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1514 NetLogWithSource()); |
| 1515 |
| 1516 // Continue the transactions. 2nd will pause at the cache reading state and |
| 1517 // 3rd transaction will doom the entry. |
| 1518 base::RunLoop().RunUntilIdle(); |
| 1519 } |
| 1520 |
| 1521 EXPECT_TRUE( |
| 1522 cache.disk_cache()->IsDiskEntryDoomed(kRangeGET_TransactionOK.url)); |
| 1523 |
| 1524 // Resume cache read by 1st transaction which will lead to dooming the entry |
| 1525 // as well since the entry cannot be validated. This double dooming should not |
| 1526 // lead to an assertion. |
| 1527 // Pause before it creates an entry so we can check the status of entry |
| 1528 // created by the third transaction. |
| 1529 cache.disk_cache()->SetDefer(DEFER_CREATE); |
| 1530 cache.disk_cache()->ResumeDoomedEntryCacheOperation( |
| 1531 kRangeGET_TransactionOK.url); |
| 1532 base::RunLoop().RunUntilIdle(); |
| 1533 |
| 1534 EXPECT_TRUE( |
| 1535 cache.disk_cache()->IsDiskEntryNotDoomed(kRangeGET_TransactionOK.url)); |
| 1536 |
| 1537 // Resume creation of entry by 2nd transaction. |
| 1538 cache.disk_cache()->ResumeCacheOperation(); |
| 1539 base::RunLoop().RunUntilIdle(); |
| 1540 |
| 1541 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1542 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1543 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1544 |
| 1545 for (auto& context : context_list) { |
| 1546 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1547 } |
| 1548 |
| 1549 for (auto& c : context_list) { |
| 1550 ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK); |
| 1551 } |
| 1552 |
| 1553 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1554 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1555 EXPECT_EQ(3, cache.disk_cache()->create_count()); |
| 1556 } |
| 1557 |
| 1558 // Same as above but tests that the 2nd transaction does not do anything if |
| 1559 // there is nothing to doom. (crbug.com/736993) |
| 1560 TEST(HttpCache, RangeGET_ParallelValidationNoMatchDoomEntry1) { |
| 1561 MockHttpCache cache; |
| 1562 |
| 1563 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 1564 MockHttpRequest request(transaction); |
| 1565 |
| 1566 MockTransaction dooming_transaction(kRangeGET_TransactionOK); |
| 1567 dooming_transaction.load_flags |= LOAD_BYPASS_CACHE; |
| 1568 MockHttpRequest dooming_request(dooming_transaction); |
| 1569 |
| 1570 std::vector<std::unique_ptr<Context>> context_list; |
| 1571 const int kNumTransactions = 3; |
| 1572 |
| 1573 for (int i = 0; i < kNumTransactions; ++i) { |
| 1574 context_list.push_back(base::MakeUnique<Context>()); |
| 1575 auto& c = context_list[i]; |
| 1576 |
| 1577 c->result = cache.CreateTransaction(&c->trans); |
| 1578 ASSERT_THAT(c->result, IsOk()); |
| 1579 EXPECT_EQ(LOAD_STATE_IDLE, c->trans->GetLoadState()); |
| 1580 |
| 1581 MockHttpRequest* this_request = &request; |
| 1582 |
| 1583 if (i == 2) { |
| 1584 this_request = &dooming_request; |
| 1585 cache.disk_cache()->SetDefer(DEFER_CREATE); |
| 1586 } |
| 1587 |
| 1588 if (i == 1) |
| 1589 cache.disk_cache()->SetDefer(kRangeGET_TransactionOK.url, DEFER_READ); |
| 1590 |
| 1591 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 1592 NetLogWithSource()); |
| 1593 |
| 1594 // Continue the transactions. 2nd will pause at the cache reading state and |
| 1595 // 3rd transaction will doom the entry and pause before creating a new |
| 1596 // entry. |
| 1597 base::RunLoop().RunUntilIdle(); |
| 1598 } |
| 1599 |
| 1600 EXPECT_TRUE( |
| 1601 cache.disk_cache()->IsDiskEntryDoomed(kRangeGET_TransactionOK.url)); |
| 1602 |
| 1603 // Resume cache read by 2nd transaction which will lead to dooming the entry |
| 1604 // as well since the entry cannot be validated. This double dooming should not |
| 1605 // lead to an assertion. |
| 1606 cache.disk_cache()->ResumeDoomedEntryCacheOperation( |
| 1607 kRangeGET_TransactionOK.url); |
| 1608 base::RunLoop().RunUntilIdle(); |
| 1609 |
| 1610 // Resume creation of entry by 3rd transaction. |
| 1611 cache.disk_cache()->ResumeCacheOperation(); |
| 1612 base::RunLoop().RunUntilIdle(); |
| 1613 |
| 1614 // Note that 2nd transaction gets added to entry created by transaction 3rd |
| 1615 // transaction, thus only 2 entries get created. |
| 1616 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1617 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1618 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1619 |
| 1620 for (auto& context : context_list) { |
| 1621 EXPECT_EQ(LOAD_STATE_IDLE, context->trans->GetLoadState()); |
| 1622 } |
| 1623 |
| 1624 for (auto& c : context_list) { |
| 1625 ReadAndVerifyTransaction(c->trans.get(), kRangeGET_TransactionOK); |
| 1626 } |
| 1627 |
| 1628 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
| 1629 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
| 1630 EXPECT_EQ(2, cache.disk_cache()->create_count()); |
| 1631 } |
| 1632 |
| 1481 // Tests parallel validation on range requests with non-overlapping ranges. | 1633 // Tests parallel validation on range requests with non-overlapping ranges. |
| 1482 TEST(HttpCache, RangeGET_ParallelValidationDifferentRanges) { | 1634 TEST(HttpCache, RangeGET_ParallelValidationDifferentRanges) { |
| 1483 MockHttpCache cache; | 1635 MockHttpCache cache; |
| 1484 | 1636 |
| 1485 ScopedMockTransaction transaction(kRangeGET_TransactionOK); | 1637 ScopedMockTransaction transaction(kRangeGET_TransactionOK); |
| 1486 | 1638 |
| 1487 std::vector<std::unique_ptr<Context>> context_list; | 1639 std::vector<std::unique_ptr<Context>> context_list; |
| 1488 const int kNumTransactions = 2; | 1640 const int kNumTransactions = 2; |
| 1489 | 1641 |
| 1490 for (int i = 0; i < kNumTransactions; ++i) { | 1642 for (int i = 0; i < kNumTransactions; ++i) { |
| (...skipping 661 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2152 for (int i = 0; i < kNumTransactions; ++i) { | 2304 for (int i = 0; i < kNumTransactions; ++i) { |
| 2153 context_list.push_back(base::MakeUnique<Context>()); | 2305 context_list.push_back(base::MakeUnique<Context>()); |
| 2154 auto& c = context_list[i]; | 2306 auto& c = context_list[i]; |
| 2155 | 2307 |
| 2156 c->result = cache.CreateTransaction(&c->trans); | 2308 c->result = cache.CreateTransaction(&c->trans); |
| 2157 ASSERT_THAT(c->result, IsOk()); | 2309 ASSERT_THAT(c->result, IsOk()); |
| 2158 | 2310 |
| 2159 MockHttpRequest* this_request = &request; | 2311 MockHttpRequest* this_request = &request; |
| 2160 if (i == 3) { | 2312 if (i == 3) { |
| 2161 this_request = &validate_request; | 2313 this_request = &validate_request; |
| 2162 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); | 2314 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferCallback)); |
| 2163 } | 2315 } |
| 2164 | 2316 |
| 2165 c->result = c->trans->Start(this_request, c->callback.callback(), | 2317 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 2166 NetLogWithSource()); | 2318 NetLogWithSource()); |
| 2167 } | 2319 } |
| 2168 | 2320 |
| 2169 // Allow all requests to move from the Create queue to the active entry. | 2321 // Allow all requests to move from the Create queue to the active entry. |
| 2170 base::RunLoop().RunUntilIdle(); | 2322 base::RunLoop().RunUntilIdle(); |
| 2171 | 2323 |
| 2172 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2324 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2250 for (int i = 0; i < kNumTransactions; ++i) { | 2402 for (int i = 0; i < kNumTransactions; ++i) { |
| 2251 context_list.push_back(base::MakeUnique<Context>()); | 2403 context_list.push_back(base::MakeUnique<Context>()); |
| 2252 auto& c = context_list[i]; | 2404 auto& c = context_list[i]; |
| 2253 | 2405 |
| 2254 c->result = cache.CreateTransaction(&c->trans); | 2406 c->result = cache.CreateTransaction(&c->trans); |
| 2255 ASSERT_THAT(c->result, IsOk()); | 2407 ASSERT_THAT(c->result, IsOk()); |
| 2256 | 2408 |
| 2257 MockHttpRequest* this_request = &request; | 2409 MockHttpRequest* this_request = &request; |
| 2258 if (i == 2) { | 2410 if (i == 2) { |
| 2259 this_request = &validate_request; | 2411 this_request = &validate_request; |
| 2260 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); | 2412 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferCallback)); |
| 2261 } | 2413 } |
| 2262 | 2414 |
| 2263 c->result = c->trans->Start(this_request, c->callback.callback(), | 2415 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 2264 NetLogWithSource()); | 2416 NetLogWithSource()); |
| 2265 } | 2417 } |
| 2266 | 2418 |
| 2267 // Allow all requests to move from the Create queue to the active entry. | 2419 // Allow all requests to move from the Create queue to the active entry. |
| 2268 base::RunLoop().RunUntilIdle(); | 2420 base::RunLoop().RunUntilIdle(); |
| 2269 | 2421 |
| 2270 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2422 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2388 for (int i = 0; i < kNumTransactions; ++i) { | 2540 for (int i = 0; i < kNumTransactions; ++i) { |
| 2389 context_list.push_back(base::MakeUnique<Context>()); | 2541 context_list.push_back(base::MakeUnique<Context>()); |
| 2390 auto& c = context_list[i]; | 2542 auto& c = context_list[i]; |
| 2391 | 2543 |
| 2392 c->result = cache.CreateTransaction(&c->trans); | 2544 c->result = cache.CreateTransaction(&c->trans); |
| 2393 ASSERT_THAT(c->result, IsOk()); | 2545 ASSERT_THAT(c->result, IsOk()); |
| 2394 | 2546 |
| 2395 MockHttpRequest* this_request = &request; | 2547 MockHttpRequest* this_request = &request; |
| 2396 if (i == 2) { | 2548 if (i == 2) { |
| 2397 this_request = &validate_request; | 2549 this_request = &validate_request; |
| 2398 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); | 2550 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferCallback)); |
| 2399 } | 2551 } |
| 2400 | 2552 |
| 2401 c->result = c->trans->Start(this_request, c->callback.callback(), | 2553 c->result = c->trans->Start(this_request, c->callback.callback(), |
| 2402 NetLogWithSource()); | 2554 NetLogWithSource()); |
| 2403 } | 2555 } |
| 2404 | 2556 |
| 2405 // Allow all requests to move from the Create queue to the active entry. | 2557 // Allow all requests to move from the Create queue to the active entry. |
| 2406 base::RunLoop().RunUntilIdle(); | 2558 base::RunLoop().RunUntilIdle(); |
| 2407 | 2559 |
| 2408 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 2560 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2454 std::vector<std::unique_ptr<Context>> context_list; | 2606 std::vector<std::unique_ptr<Context>> context_list; |
| 2455 | 2607 |
| 2456 for (int i = 0; i < kNumTransactions; ++i) { | 2608 for (int i = 0; i < kNumTransactions; ++i) { |
| 2457 context_list.push_back(base::MakeUnique<Context>()); | 2609 context_list.push_back(base::MakeUnique<Context>()); |
| 2458 auto& c = context_list[i]; | 2610 auto& c = context_list[i]; |
| 2459 | 2611 |
| 2460 c->result = cache.CreateTransaction(&c->trans); | 2612 c->result = cache.CreateTransaction(&c->trans); |
| 2461 ASSERT_THAT(c->result, IsOk()); | 2613 ASSERT_THAT(c->result, IsOk()); |
| 2462 | 2614 |
| 2463 if (i == 0) | 2615 if (i == 0) |
| 2464 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferNetworkStart)); | 2616 c->trans->SetBeforeNetworkStartCallback(base::Bind(&DeferCallback)); |
| 2465 | 2617 |
| 2466 c->result = | 2618 c->result = |
| 2467 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); | 2619 c->trans->Start(&request, c->callback.callback(), NetLogWithSource()); |
| 2468 } | 2620 } |
| 2469 | 2621 |
| 2470 base::RunLoop().RunUntilIdle(); | 2622 base::RunLoop().RunUntilIdle(); |
| 2471 | 2623 |
| 2472 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); | 2624 EXPECT_TRUE(cache.IsHeadersTransactionPresent(kSimpleGET_Transaction.url)); |
| 2473 EXPECT_EQ(1, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url)); | 2625 EXPECT_EQ(1, cache.GetCountAddToEntryQueue(kSimpleGET_Transaction.url)); |
| 2474 | 2626 |
| (...skipping 6884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 9359 ASSERT_TRUE(attrs->GetDictionary( | 9511 ASSERT_TRUE(attrs->GetDictionary( |
| 9360 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); | 9512 base::trace_event::MemoryAllocatorDump::kNameSize, &size_attrs)); |
| 9361 std::string size; | 9513 std::string size; |
| 9362 ASSERT_TRUE(size_attrs->GetString("value", &size)); | 9514 ASSERT_TRUE(size_attrs->GetString("value", &size)); |
| 9363 int actual_size = 0; | 9515 int actual_size = 0; |
| 9364 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); | 9516 ASSERT_TRUE(base::HexStringToInt(size, &actual_size)); |
| 9365 ASSERT_LT(0, actual_size); | 9517 ASSERT_LT(0, actual_size); |
| 9366 } | 9518 } |
| 9367 | 9519 |
| 9368 } // namespace net | 9520 } // namespace net |
| OLD | NEW |