| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <windows.h> | |
| 8 | |
| 9 #include "base/hash_tables.h" | 7 #include "base/hash_tables.h" |
| 10 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 11 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 12 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 13 #include "net/base/load_flags.h" | 11 #include "net/base/load_flags.h" |
| 14 #include "net/disk_cache/disk_cache.h" | 12 #include "net/disk_cache/disk_cache.h" |
| 15 #include "net/http/http_request_info.h" | 13 #include "net/http/http_request_info.h" |
| 16 #include "net/http/http_response_info.h" | 14 #include "net/http/http_response_info.h" |
| 17 #include "net/http/http_transaction.h" | 15 #include "net/http/http_transaction.h" |
| 18 #include "net/http/http_transaction_unittest.h" | 16 #include "net/http/http_transaction_unittest.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 18 |
| 21 #pragma warning(disable: 4355) | |
| 22 | |
| 23 namespace { | 19 namespace { |
| 24 | 20 |
| 25 //----------------------------------------------------------------------------- | 21 //----------------------------------------------------------------------------- |
| 26 // mock disk cache (a very basic memory cache implementation) | 22 // mock disk cache (a very basic memory cache implementation) |
| 27 | 23 |
| 28 class MockDiskEntry : public disk_cache::Entry, | 24 class MockDiskEntry : public disk_cache::Entry, |
| 29 public base::RefCounted<MockDiskEntry> { | 25 public base::RefCounted<MockDiskEntry> { |
| 30 public: | 26 public: |
| 31 MockDiskEntry() : test_mode_(0), doomed_(false) { | 27 MockDiskEntry() : test_mode_(0), doomed_(false) { |
| 32 } | 28 } |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 DCHECK(index >= 0 && index < 2); | 62 DCHECK(index >= 0 && index < 2); |
| 67 return static_cast<int32>(data_[index].size()); | 63 return static_cast<int32>(data_[index].size()); |
| 68 } | 64 } |
| 69 | 65 |
| 70 virtual int ReadData(int index, int offset, char* buf, int buf_len, | 66 virtual int ReadData(int index, int offset, char* buf, int buf_len, |
| 71 net::CompletionCallback* callback) { | 67 net::CompletionCallback* callback) { |
| 72 DCHECK(index >= 0 && index < 2); | 68 DCHECK(index >= 0 && index < 2); |
| 73 | 69 |
| 74 if (offset < 0 || offset > static_cast<int>(data_[index].size())) | 70 if (offset < 0 || offset > static_cast<int>(data_[index].size())) |
| 75 return net::ERR_FAILED; | 71 return net::ERR_FAILED; |
| 76 if (offset == data_[index].size()) | 72 if (static_cast<size_t>(offset) == data_[index].size()) |
| 77 return 0; | 73 return 0; |
| 78 | 74 |
| 79 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); | 75 int num = std::min(buf_len, static_cast<int>(data_[index].size()) - offset); |
| 80 memcpy(buf, &data_[index][offset], num); | 76 memcpy(buf, &data_[index][offset], num); |
| 81 | 77 |
| 82 if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_READ)) | 78 if (!callback || (test_mode_ & TEST_MODE_SYNC_CACHE_READ)) |
| 83 return num; | 79 return num; |
| 84 | 80 |
| 85 CallbackLater(callback, num); | 81 CallbackLater(callback, num); |
| 86 return net::ERR_IO_PENDING; | 82 return net::ERR_IO_PENDING; |
| (...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 MockTransaction transaction(kSimpleGET_Transaction); | 472 MockTransaction transaction(kSimpleGET_Transaction); |
| 477 transaction.request_headers = "cache-control: max-age=0"; | 473 transaction.request_headers = "cache-control: max-age=0"; |
| 478 | 474 |
| 479 RunTransactionTest(cache.http_cache(), transaction); | 475 RunTransactionTest(cache.http_cache(), transaction); |
| 480 | 476 |
| 481 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 477 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
| 482 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 478 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
| 483 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 479 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
| 484 } | 480 } |
| 485 | 481 |
| 482 struct Context { |
| 483 int result; |
| 484 TestCompletionCallback callback; |
| 485 net::HttpTransaction* trans; |
| 486 |
| 487 Context(net::HttpTransaction* t) : result(net::ERR_IO_PENDING), trans(t) { |
| 488 } |
| 489 }; |
| 490 |
| 486 TEST(HttpCache, SimpleGET_ManyReaders) { | 491 TEST(HttpCache, SimpleGET_ManyReaders) { |
| 487 MockHttpCache cache; | 492 MockHttpCache cache; |
| 488 | 493 |
| 489 MockHttpRequest request(kSimpleGET_Transaction); | 494 MockHttpRequest request(kSimpleGET_Transaction); |
| 490 | 495 |
| 491 struct Context { | |
| 492 int result; | |
| 493 TestCompletionCallback callback; | |
| 494 net::HttpTransaction* trans; | |
| 495 | |
| 496 Context(net::HttpTransaction* t) : result(net::ERR_IO_PENDING), trans(t) { | |
| 497 } | |
| 498 }; | |
| 499 | |
| 500 std::vector<Context*> context_list; | 496 std::vector<Context*> context_list; |
| 501 const int kNumTransactions = 5; | 497 const int kNumTransactions = 5; |
| 502 | 498 |
| 503 for (int i = 0; i < kNumTransactions; ++i) { | 499 for (int i = 0; i < kNumTransactions; ++i) { |
| 504 context_list.push_back( | 500 context_list.push_back( |
| 505 new Context(cache.http_cache()->CreateTransaction())); | 501 new Context(cache.http_cache()->CreateTransaction())); |
| 506 | 502 |
| 507 Context* c = context_list[i]; | 503 Context* c = context_list[i]; |
| 508 int rv = c->trans->Start(&request, &c->callback); | 504 int rv = c->trans->Start(&request, &c->callback); |
| 509 if (rv != net::ERR_IO_PENDING) | 505 if (rv != net::ERR_IO_PENDING) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 535 c->trans->Destroy(); | 531 c->trans->Destroy(); |
| 536 delete c; | 532 delete c; |
| 537 } | 533 } |
| 538 } | 534 } |
| 539 | 535 |
| 540 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { | 536 TEST(HttpCache, SimpleGET_ManyWriters_CancelFirst) { |
| 541 MockHttpCache cache; | 537 MockHttpCache cache; |
| 542 | 538 |
| 543 MockHttpRequest request(kSimpleGET_Transaction); | 539 MockHttpRequest request(kSimpleGET_Transaction); |
| 544 | 540 |
| 545 struct Context { | |
| 546 int result; | |
| 547 TestCompletionCallback callback; | |
| 548 net::HttpTransaction* trans; | |
| 549 | |
| 550 Context(net::HttpTransaction* t) : result(net::ERR_IO_PENDING), trans(t) { | |
| 551 } | |
| 552 }; | |
| 553 | |
| 554 std::vector<Context*> context_list; | 541 std::vector<Context*> context_list; |
| 555 const int kNumTransactions = 2; | 542 const int kNumTransactions = 2; |
| 556 | 543 |
| 557 for (int i = 0; i < kNumTransactions; ++i) { | 544 for (int i = 0; i < kNumTransactions; ++i) { |
| 558 context_list.push_back( | 545 context_list.push_back( |
| 559 new Context(cache.http_cache()->CreateTransaction())); | 546 new Context(cache.http_cache()->CreateTransaction())); |
| 560 | 547 |
| 561 Context* c = context_list[i]; | 548 Context* c = context_list[i]; |
| 562 int rv = c->trans->Start(&request, &c->callback); | 549 int rv = c->trans->Start(&request, &c->callback); |
| 563 if (rv != net::ERR_IO_PENDING) | 550 if (rv != net::ERR_IO_PENDING) |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 978 net::HttpTransaction* trans = cache.http_cache()->CreateTransaction(); | 965 net::HttpTransaction* trans = cache.http_cache()->CreateTransaction(); |
| 979 ASSERT_TRUE(trans); | 966 ASSERT_TRUE(trans); |
| 980 | 967 |
| 981 int rv = trans->Start(&request, &callback); | 968 int rv = trans->Start(&request, &callback); |
| 982 if (rv == net::ERR_IO_PENDING) | 969 if (rv == net::ERR_IO_PENDING) |
| 983 rv = callback.WaitForResult(); | 970 rv = callback.WaitForResult(); |
| 984 ASSERT_EQ(net::ERR_CACHE_MISS, rv); | 971 ASSERT_EQ(net::ERR_CACHE_MISS, rv); |
| 985 | 972 |
| 986 trans->Destroy(); | 973 trans->Destroy(); |
| 987 } | 974 } |
| OLD | NEW |