OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/hash_tables.h" | 7 #include "base/hash_tables.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/scoped_vector.h" | 9 #include "base/scoped_vector.h" |
10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
566 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL; | 566 return (rv == net::OK) ? static_cast<MockDiskCache*>(backend) : NULL; |
567 } | 567 } |
568 | 568 |
569 // Helper function for reading response info from the disk cache. | 569 // Helper function for reading response info from the disk cache. |
570 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, | 570 static bool ReadResponseInfo(disk_cache::Entry* disk_entry, |
571 net::HttpResponseInfo* response_info, | 571 net::HttpResponseInfo* response_info, |
572 bool* response_truncated) { | 572 bool* response_truncated) { |
573 int size = disk_entry->GetDataSize(0); | 573 int size = disk_entry->GetDataSize(0); |
574 | 574 |
575 TestCompletionCallback cb; | 575 TestCompletionCallback cb; |
576 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); | 576 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size)); |
577 int rv = disk_entry->ReadData(0, 0, buffer, size, &cb); | 577 int rv = disk_entry->ReadData(0, 0, buffer, size, &cb); |
578 rv = cb.GetResult(rv); | 578 rv = cb.GetResult(rv); |
579 EXPECT_EQ(size, rv); | 579 EXPECT_EQ(size, rv); |
580 | 580 |
581 return net::HttpCache::ParseResponseInfo(buffer->data(), size, | 581 return net::HttpCache::ParseResponseInfo(buffer->data(), size, |
582 response_info, | 582 response_info, |
583 response_truncated); | 583 response_truncated); |
584 } | 584 } |
585 | 585 |
586 // Helper function for writing response info into the disk cache. | 586 // Helper function for writing response info into the disk cache. |
587 static bool WriteResponseInfo(disk_cache::Entry* disk_entry, | 587 static bool WriteResponseInfo(disk_cache::Entry* disk_entry, |
588 const net::HttpResponseInfo* response_info, | 588 const net::HttpResponseInfo* response_info, |
589 bool skip_transient_headers, | 589 bool skip_transient_headers, |
590 bool response_truncated) { | 590 bool response_truncated) { |
591 Pickle pickle; | 591 Pickle pickle; |
592 response_info->Persist( | 592 response_info->Persist( |
593 &pickle, skip_transient_headers, response_truncated); | 593 &pickle, skip_transient_headers, response_truncated); |
594 | 594 |
595 TestCompletionCallback cb; | 595 TestCompletionCallback cb; |
596 scoped_refptr<net::WrappedIOBuffer> data = new net::WrappedIOBuffer( | 596 scoped_refptr<net::WrappedIOBuffer> data(new net::WrappedIOBuffer( |
597 reinterpret_cast<const char*>(pickle.data())); | 597 reinterpret_cast<const char*>(pickle.data()))); |
598 int len = static_cast<int>(pickle.size()); | 598 int len = static_cast<int>(pickle.size()); |
599 | 599 |
600 int rv = disk_entry->WriteData(0, 0, data, len, &cb, true); | 600 int rv = disk_entry->WriteData(0, 0, data, len, &cb, true); |
601 rv = cb.GetResult(rv); | 601 rv = cb.GetResult(rv); |
602 return (rv == len); | 602 return (rv == len); |
603 } | 603 } |
604 | 604 |
605 // Helper function to synchronously open a backend entry. | 605 // Helper function to synchronously open a backend entry. |
606 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry) { | 606 bool OpenBackendEntry(const std::string& key, disk_cache::Entry** entry) { |
607 TestCompletionCallback cb; | 607 TestCompletionCallback cb; |
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 TEST_MODE_NORMAL, | 944 TEST_MODE_NORMAL, |
945 &RangeTransactionServer::RangeHandler, | 945 &RangeTransactionServer::RangeHandler, |
946 0 | 946 0 |
947 }; | 947 }; |
948 | 948 |
949 // Verifies the response headers (|response|) match a partial content | 949 // Verifies the response headers (|response|) match a partial content |
950 // response for the range starting at |start| and ending at |end|. | 950 // response for the range starting at |start| and ending at |end|. |
951 void Verify206Response(std::string response, int start, int end) { | 951 void Verify206Response(std::string response, int start, int end) { |
952 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), | 952 std::string raw_headers(net::HttpUtil::AssembleRawHeaders(response.data(), |
953 response.size())); | 953 response.size())); |
954 scoped_refptr<net::HttpResponseHeaders> headers = | 954 scoped_refptr<net::HttpResponseHeaders> headers( |
955 new net::HttpResponseHeaders(raw_headers); | 955 new net::HttpResponseHeaders(raw_headers)); |
956 | 956 |
957 ASSERT_EQ(206, headers->response_code()); | 957 ASSERT_EQ(206, headers->response_code()); |
958 | 958 |
959 int64 range_start, range_end, object_size; | 959 int64 range_start, range_end, object_size; |
960 ASSERT_TRUE( | 960 ASSERT_TRUE( |
961 headers->GetContentRange(&range_start, &range_end, &object_size)); | 961 headers->GetContentRange(&range_start, &range_end, &object_size)); |
962 int64 content_length = headers->GetContentLength(); | 962 int64 content_length = headers->GetContentLength(); |
963 | 963 |
964 int length = end - start + 1; | 964 int length = end - start + 1; |
965 ASSERT_EQ(length, content_length); | 965 ASSERT_EQ(length, content_length); |
(...skipping 881 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1847 TestCompletionCallback callback; | 1847 TestCompletionCallback callback; |
1848 | 1848 |
1849 scoped_ptr<net::HttpTransaction> trans; | 1849 scoped_ptr<net::HttpTransaction> trans; |
1850 int rv = cache.http_cache()->CreateTransaction(&trans); | 1850 int rv = cache.http_cache()->CreateTransaction(&trans); |
1851 EXPECT_EQ(net::OK, rv); | 1851 EXPECT_EQ(net::OK, rv); |
1852 rv = trans->Start(&request, &callback, net::BoundNetLog()); | 1852 rv = trans->Start(&request, &callback, net::BoundNetLog()); |
1853 if (rv == net::ERR_IO_PENDING) | 1853 if (rv == net::ERR_IO_PENDING) |
1854 rv = callback.WaitForResult(); | 1854 rv = callback.WaitForResult(); |
1855 ASSERT_EQ(net::OK, rv); | 1855 ASSERT_EQ(net::OK, rv); |
1856 | 1856 |
1857 scoped_refptr<net::IOBuffer> buf = new net::IOBuffer(256); | 1857 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(256)); |
1858 rv = trans->Read(buf, 256, &callback); | 1858 rv = trans->Read(buf, 256, &callback); |
1859 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 1859 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
1860 | 1860 |
1861 // Test that destroying the transaction while it is reading from the cache | 1861 // Test that destroying the transaction while it is reading from the cache |
1862 // works properly. | 1862 // works properly. |
1863 trans.reset(); | 1863 trans.reset(); |
1864 | 1864 |
1865 // Make sure we pump any pending events, which should include a call to | 1865 // Make sure we pump any pending events, which should include a call to |
1866 // HttpCache::Transaction::OnCacheReadCompleted. | 1866 // HttpCache::Transaction::OnCacheReadCompleted. |
1867 MessageLoop::current()->RunAllPending(); | 1867 MessageLoop::current()->RunAllPending(); |
(...skipping 1669 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3537 | 3537 |
3538 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 3538 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
3539 if (rv == net::ERR_IO_PENDING) | 3539 if (rv == net::ERR_IO_PENDING) |
3540 rv = c->callback.WaitForResult(); | 3540 rv = c->callback.WaitForResult(); |
3541 | 3541 |
3542 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 3542 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
3543 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 3543 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
3544 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3544 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
3545 | 3545 |
3546 // Make sure that the entry has some data stored. | 3546 // Make sure that the entry has some data stored. |
3547 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 3547 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
3548 rv = c->trans->Read(buf, buf->size(), &c->callback); | 3548 rv = c->trans->Read(buf, buf->size(), &c->callback); |
3549 if (rv == net::ERR_IO_PENDING) | 3549 if (rv == net::ERR_IO_PENDING) |
3550 rv = c->callback.WaitForResult(); | 3550 rv = c->callback.WaitForResult(); |
3551 EXPECT_EQ(buf->size(), rv); | 3551 EXPECT_EQ(buf->size(), rv); |
3552 | 3552 |
3553 // Destroy the transaction. | 3553 // Destroy the transaction. |
3554 delete c; | 3554 delete c; |
3555 | 3555 |
3556 // Verify that the entry has not been deleted. | 3556 // Verify that the entry has not been deleted. |
3557 disk_cache::Entry* entry; | 3557 disk_cache::Entry* entry; |
(...skipping 20 matching lines...) Expand all Loading... |
3578 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 3578 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
3579 if (rv == net::ERR_IO_PENDING) | 3579 if (rv == net::ERR_IO_PENDING) |
3580 rv = c->callback.WaitForResult(); | 3580 rv = c->callback.WaitForResult(); |
3581 | 3581 |
3582 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3582 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
3583 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3583 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
3584 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3584 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
3585 | 3585 |
3586 // Make sure that we revalidate the entry and read from the cache (a single | 3586 // Make sure that we revalidate the entry and read from the cache (a single |
3587 // read will return while waiting for the network). | 3587 // read will return while waiting for the network). |
3588 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); | 3588 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); |
3589 rv = c->trans->Read(buf, buf->size(), &c->callback); | 3589 rv = c->trans->Read(buf, buf->size(), &c->callback); |
3590 EXPECT_EQ(5, c->callback.GetResult(rv)); | 3590 EXPECT_EQ(5, c->callback.GetResult(rv)); |
3591 rv = c->trans->Read(buf, buf->size(), &c->callback); | 3591 rv = c->trans->Read(buf, buf->size(), &c->callback); |
3592 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 3592 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
3593 | 3593 |
3594 // Destroy the transaction before completing the read. | 3594 // Destroy the transaction before completing the read. |
3595 delete c; | 3595 delete c; |
3596 | 3596 |
3597 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 3597 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
3598 // message loop. This means that a new transaction will just reuse the same | 3598 // message loop. This means that a new transaction will just reuse the same |
(...skipping 25 matching lines...) Expand all Loading... |
3624 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 3624 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
3625 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 3625 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
3626 rv = c->callback.WaitForResult(); | 3626 rv = c->callback.WaitForResult(); |
3627 | 3627 |
3628 EXPECT_EQ(2, cache.network_layer()->transaction_count()); | 3628 EXPECT_EQ(2, cache.network_layer()->transaction_count()); |
3629 EXPECT_EQ(1, cache.disk_cache()->open_count()); | 3629 EXPECT_EQ(1, cache.disk_cache()->open_count()); |
3630 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 3630 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
3631 | 3631 |
3632 // Make sure that we revalidate the entry and read from the cache (a single | 3632 // Make sure that we revalidate the entry and read from the cache (a single |
3633 // read will return while waiting for the network). | 3633 // read will return while waiting for the network). |
3634 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(5); | 3634 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(5)); |
3635 rv = c->trans->Read(buf, buf->size(), &c->callback); | 3635 rv = c->trans->Read(buf, buf->size(), &c->callback); |
3636 EXPECT_EQ(5, c->callback.GetResult(rv)); | 3636 EXPECT_EQ(5, c->callback.GetResult(rv)); |
3637 rv = c->trans->Read(buf, buf->size(), &c->callback); | 3637 rv = c->trans->Read(buf, buf->size(), &c->callback); |
3638 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 3638 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
3639 | 3639 |
3640 // Destroy the transaction before completing the read. | 3640 // Destroy the transaction before completing the read. |
3641 delete c; | 3641 delete c; |
3642 | 3642 |
3643 // We have the read and the delete (OnProcessPendingQueue) waiting on the | 3643 // We have the read and the delete (OnProcessPendingQueue) waiting on the |
3644 // message loop. This means that a new transaction will just reuse the same | 3644 // message loop. This means that a new transaction will just reuse the same |
(...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4022 | 4022 |
4023 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 4023 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
4024 if (rv == net::ERR_IO_PENDING) | 4024 if (rv == net::ERR_IO_PENDING) |
4025 rv = c->callback.WaitForResult(); | 4025 rv = c->callback.WaitForResult(); |
4026 | 4026 |
4027 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 4027 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
4028 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 4028 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
4029 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4029 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4030 | 4030 |
4031 // Make sure that the entry has some data stored. | 4031 // Make sure that the entry has some data stored. |
4032 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 4032 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
4033 rv = c->trans->Read(buf, buf->size(), &c->callback); | 4033 rv = c->trans->Read(buf, buf->size(), &c->callback); |
4034 if (rv == net::ERR_IO_PENDING) | 4034 if (rv == net::ERR_IO_PENDING) |
4035 rv = c->callback.WaitForResult(); | 4035 rv = c->callback.WaitForResult(); |
4036 EXPECT_EQ(buf->size(), rv); | 4036 EXPECT_EQ(buf->size(), rv); |
4037 | 4037 |
4038 // Destroy the transaction. | 4038 // Destroy the transaction. |
4039 delete c; | 4039 delete c; |
4040 | 4040 |
4041 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 4041 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
4042 | 4042 |
(...skipping 23 matching lines...) Expand all Loading... |
4066 | 4066 |
4067 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 4067 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
4068 if (rv == net::ERR_IO_PENDING) | 4068 if (rv == net::ERR_IO_PENDING) |
4069 rv = c->callback.WaitForResult(); | 4069 rv = c->callback.WaitForResult(); |
4070 | 4070 |
4071 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 4071 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
4072 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 4072 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
4073 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4073 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4074 | 4074 |
4075 // Make sure that the entry has some data stored. | 4075 // Make sure that the entry has some data stored. |
4076 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 4076 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
4077 rv = c->trans->Read(buf, buf->size(), &c->callback); | 4077 rv = c->trans->Read(buf, buf->size(), &c->callback); |
4078 if (rv == net::ERR_IO_PENDING) | 4078 if (rv == net::ERR_IO_PENDING) |
4079 rv = c->callback.WaitForResult(); | 4079 rv = c->callback.WaitForResult(); |
4080 EXPECT_EQ(buf->size(), rv); | 4080 EXPECT_EQ(buf->size(), rv); |
4081 | 4081 |
4082 // Destroy the transaction. | 4082 // Destroy the transaction. |
4083 delete c; | 4083 delete c; |
4084 | 4084 |
4085 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); | 4085 RunTransactionTest(cache.http_cache(), kSimpleGET_Transaction); |
4086 | 4086 |
(...skipping 23 matching lines...) Expand all Loading... |
4110 | 4110 |
4111 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); | 4111 rv = c->trans->Start(&request, &c->callback, net::BoundNetLog()); |
4112 if (rv == net::ERR_IO_PENDING) | 4112 if (rv == net::ERR_IO_PENDING) |
4113 rv = c->callback.WaitForResult(); | 4113 rv = c->callback.WaitForResult(); |
4114 | 4114 |
4115 EXPECT_EQ(1, cache.network_layer()->transaction_count()); | 4115 EXPECT_EQ(1, cache.network_layer()->transaction_count()); |
4116 EXPECT_EQ(0, cache.disk_cache()->open_count()); | 4116 EXPECT_EQ(0, cache.disk_cache()->open_count()); |
4117 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4117 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4118 | 4118 |
4119 // Make sure that the entry has some data stored. | 4119 // Make sure that the entry has some data stored. |
4120 scoped_refptr<net::IOBufferWithSize> buf = new net::IOBufferWithSize(10); | 4120 scoped_refptr<net::IOBufferWithSize> buf(new net::IOBufferWithSize(10)); |
4121 rv = c->trans->Read(buf, buf->size(), &c->callback); | 4121 rv = c->trans->Read(buf, buf->size(), &c->callback); |
4122 if (rv == net::ERR_IO_PENDING) | 4122 if (rv == net::ERR_IO_PENDING) |
4123 rv = c->callback.WaitForResult(); | 4123 rv = c->callback.WaitForResult(); |
4124 EXPECT_EQ(buf->size(), rv); | 4124 EXPECT_EQ(buf->size(), rv); |
4125 | 4125 |
4126 // We want to cancel the request when the transaction is busy. | 4126 // We want to cancel the request when the transaction is busy. |
4127 rv = c->trans->Read(buf, buf->size(), &c->callback); | 4127 rv = c->trans->Read(buf, buf->size(), &c->callback); |
4128 EXPECT_EQ(net::ERR_IO_PENDING, rv); | 4128 EXPECT_EQ(net::ERR_IO_PENDING, rv); |
4129 EXPECT_FALSE(c->callback.have_result()); | 4129 EXPECT_FALSE(c->callback.have_result()); |
4130 | 4130 |
(...skipping 712 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4843 // Now return 200 when validating the entry so the metadata will be lost. | 4843 // Now return 200 when validating the entry so the metadata will be lost. |
4844 MockTransaction trans2(kTypicalGET_Transaction); | 4844 MockTransaction trans2(kTypicalGET_Transaction); |
4845 trans2.load_flags = net::LOAD_VALIDATE_CACHE; | 4845 trans2.load_flags = net::LOAD_VALIDATE_CACHE; |
4846 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); | 4846 RunTransactionTestWithResponseInfo(cache.http_cache(), trans2, &response); |
4847 EXPECT_TRUE(response.metadata.get() == NULL); | 4847 EXPECT_TRUE(response.metadata.get() == NULL); |
4848 | 4848 |
4849 EXPECT_EQ(3, cache.network_layer()->transaction_count()); | 4849 EXPECT_EQ(3, cache.network_layer()->transaction_count()); |
4850 EXPECT_EQ(4, cache.disk_cache()->open_count()); | 4850 EXPECT_EQ(4, cache.disk_cache()->open_count()); |
4851 EXPECT_EQ(1, cache.disk_cache()->create_count()); | 4851 EXPECT_EQ(1, cache.disk_cache()->create_count()); |
4852 } | 4852 } |
OLD | NEW |