| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "net/base/cache_type.h" | 12 #include "net/base/cache_type.h" |
| 13 #include "net/base/cert_status_flags.h" | 13 #include "net/base/cert_status_flags.h" |
| 14 #include "net/base/completion_callback.h" |
| 14 #include "net/base/host_port_pair.h" | 15 #include "net/base/host_port_pair.h" |
| 15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
| 16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 17 #include "net/base/net_log_unittest.h" | 18 #include "net/base/net_log_unittest.h" |
| 18 #include "net/base/ssl_cert_request_info.h" | 19 #include "net/base/ssl_cert_request_info.h" |
| 19 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
| 20 #include "net/http/http_byte_range.h" | 21 #include "net/http/http_byte_range.h" |
| 21 #include "net/http/http_request_headers.h" | 22 #include "net/http/http_request_headers.h" |
| 22 #include "net/http/http_request_info.h" | 23 #include "net/http/http_request_info.h" |
| 23 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 24 #include "net/http/http_response_info.h" | 25 #include "net/http/http_response_info.h" |
| 25 #include "net/http/http_transaction.h" | 26 #include "net/http/http_transaction.h" |
| 26 #include "net/http/http_transaction_unittest.h" | 27 #include "net/http/http_transaction_unittest.h" |
| 27 #include "net/http/http_util.h" | 28 #include "net/http/http_util.h" |
| 28 #include "net/http/mock_http_cache.h" | 29 #include "net/http/mock_http_cache.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 30 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 31 |
| 31 using base::Time; | 32 using base::Time; |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 class DeleteCacheOldCompletionCallback : public TestOldCompletionCallback { | 36 class DeleteCacheCompletionCallback : public TestCompletionCallbackBase { |
| 36 public: | 37 public: |
| 37 explicit DeleteCacheOldCompletionCallback(MockHttpCache* cache) | 38 explicit DeleteCacheCompletionCallback(MockHttpCache* cache) |
| 38 : cache_(cache) {} | 39 : cache_(cache), |
| 39 | 40 ALLOW_THIS_IN_INITIALIZER_LIST(callback_( |
| 40 virtual void RunWithParams(const Tuple1<int>& params) { | 41 base::Bind(&DeleteCacheCompletionCallback::OnComplete, |
| 41 delete cache_; | 42 base::Unretained(this)))) { |
| 42 TestOldCompletionCallback::RunWithParams(params); | |
| 43 } | 43 } |
| 44 | 44 |
| 45 const net::CompletionCallback& callback() const { return callback_; } |
| 46 |
| 45 private: | 47 private: |
| 48 void OnComplete(int result) { |
| 49 delete cache_; |
| 50 SetResult(result); |
| 51 } |
| 52 |
| 46 MockHttpCache* cache_; | 53 MockHttpCache* cache_; |
| 54 const net::CompletionCallback callback_; |
| 55 |
| 56 DISALLOW_COPY_AND_ASSIGN(DeleteCacheCompletionCallback); |
| 47 }; | 57 }; |
| 48 | 58 |
| 49 //----------------------------------------------------------------------------- | 59 //----------------------------------------------------------------------------- |
| 50 // helpers | 60 // helpers |
| 51 | 61 |
| 52 void ReadAndVerifyTransaction(net::HttpTransaction* trans, | 62 void ReadAndVerifyTransaction(net::HttpTransaction* trans, |
| 53 const MockTransaction& trans_info) { | 63 const MockTransaction& trans_info) { |
| 54 std::string content; | 64 std::string content; |
| 55 int rv = ReadTransaction(trans, &content); | 65 int rv = ReadTransaction(trans, &content); |
| 56 | 66 |
| (...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 scoped_ptr<net::HttpTransaction> trans; | 398 scoped_ptr<net::HttpTransaction> trans; |
| 389 int rv = cache.http_cache()->CreateTransaction(&trans); | 399 int rv = cache.http_cache()->CreateTransaction(&trans); |
| 390 EXPECT_EQ(net::OK, rv); | 400 EXPECT_EQ(net::OK, rv); |
| 391 ASSERT_TRUE(trans.get()); | 401 ASSERT_TRUE(trans.get()); |
| 392 } | 402 } |
| 393 | 403 |
| 394 TEST(HttpCache, GetBackend) { | 404 TEST(HttpCache, GetBackend) { |
| 395 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); | 405 MockHttpCache cache(net::HttpCache::DefaultBackend::InMemory(0)); |
| 396 | 406 |
| 397 disk_cache::Backend* backend; | 407 disk_cache::Backend* backend; |
| 398 TestOldCompletionCallback cb; | 408 net::TestCompletionCallback cb; |
| 399 // This will lazily initialize the backend. | 409 // This will lazily initialize the backend. |
| 400 int rv = cache.http_cache()->GetBackend(&backend, &cb); | 410 int rv = cache.http_cache()->GetBackend(&backend, cb.callback()); |
| 401 EXPECT_EQ(net::OK, cb.GetResult(rv)); | 411 EXPECT_EQ(net::OK, cb.GetResult(rv)); |
| 402 } | 412 } |
| 403 | 413 |
| 404 TEST(HttpCache, SimpleGET) { | 414 TEST(HttpCache, SimpleGET) { |
| 405 MockHttpCache cache; | 415 MockHttpCache cache; |
| 406 | 416 |
| 407 // write to the cache | 417 // write to the cache |
| 408 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 418 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
| 409 | 419 |
| 410 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 420 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
| (...skipping 1047 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1458 *backend = NULL; | 1468 *backend = NULL; |
| 1459 callback->Run(net::ERR_ABORTED); | 1469 callback->Run(net::ERR_ABORTED); |
| 1460 } | 1470 } |
| 1461 | 1471 |
| 1462 // Tests that we can delete the cache while creating the backend, from within | 1472 // Tests that we can delete the cache while creating the backend, from within |
| 1463 // one of the callbacks. | 1473 // one of the callbacks. |
| 1464 TEST(HttpCache, DeleteCacheWaitingForBackend2) { | 1474 TEST(HttpCache, DeleteCacheWaitingForBackend2) { |
| 1465 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); | 1475 MockBlockingBackendFactory* factory = new MockBlockingBackendFactory(); |
| 1466 MockHttpCache* cache = new MockHttpCache(factory); | 1476 MockHttpCache* cache = new MockHttpCache(factory); |
| 1467 | 1477 |
| 1468 DeleteCacheOldCompletionCallback cb(cache); | 1478 DeleteCacheCompletionCallback cb(cache); |
| 1469 disk_cache::Backend* backend; | 1479 disk_cache::Backend* backend; |
| 1470 int rv = cache->http_cache()->GetBackend(&backend, &cb); | 1480 int rv = cache->http_cache()->GetBackend(&backend, cb.callback()); |
| 1471 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1481 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1472 | 1482 |
| 1473 // Now let's queue a regular transaction | 1483 // Now let's queue a regular transaction |
| 1474 MockHttpRequest request(kSimpleGET_Transaction); | 1484 MockHttpRequest request(kSimpleGET_Transaction); |
| 1475 | 1485 |
| 1476 scoped_ptr<Context> c(new Context()); | 1486 scoped_ptr<Context> c(new Context()); |
| 1477 c->result = cache->http_cache()->CreateTransaction(&c->trans); | 1487 c->result = cache->http_cache()->CreateTransaction(&c->trans); |
| 1478 EXPECT_EQ(net::OK, c->result); | 1488 EXPECT_EQ(net::OK, c->result); |
| 1479 | 1489 |
| 1480 c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 1490 c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
| 1481 | 1491 |
| 1482 // And another direct backend request. | 1492 // And another direct backend request. |
| 1483 TestOldCompletionCallback cb2; | 1493 net::TestCompletionCallback cb2; |
| 1484 rv = cache->http_cache()->GetBackend(&backend, &cb2); | 1494 rv = cache->http_cache()->GetBackend(&backend, cb2.callback()); |
| 1485 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1495 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
| 1486 | 1496 |
| 1487 // Just to make sure that everything is still pending. | 1497 // Just to make sure that everything is still pending. |
| 1488 MessageLoop::current()->RunAllPending(); | 1498 MessageLoop::current()->RunAllPending(); |
| 1489 | 1499 |
| 1490 // The request should be queued. | 1500 // The request should be queued. |
| 1491 EXPECT_FALSE(c->callback.have_result()); | 1501 EXPECT_FALSE(c->callback.have_result()); |
| 1492 | 1502 |
| 1493 // Generate the callback. | 1503 // Generate the callback. |
| 1494 factory->FinishCreation(); | 1504 factory->FinishCreation(); |
| (...skipping 3158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4653 | 4663 |
| 4654 // Verify that the entry is marked as incomplete. | 4664 // Verify that the entry is marked as incomplete. |
| 4655 disk_cache::Entry* entry; | 4665 disk_cache::Entry* entry; |
| 4656 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); | 4666 ASSERT_TRUE(cache.OpenBackendEntry(kSimpleGET_Transaction.url, &entry)); |
| 4657 net::HttpResponseInfo response; | 4667 net::HttpResponseInfo response; |
| 4658 bool truncated = false; | 4668 bool truncated = false; |
| 4659 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); | 4669 EXPECT_TRUE(MockHttpCache::ReadResponseInfo(entry, &response, &truncated)); |
| 4660 EXPECT_TRUE(truncated); | 4670 EXPECT_TRUE(truncated); |
| 4661 entry->Close(); | 4671 entry->Close(); |
| 4662 } | 4672 } |
| OLD | NEW |