Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: net/disk_cache/entry_unittest.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "base/basictypes.h" 5 #include "base/basictypes.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/bind_helpers.h" 7 #include "base/bind_helpers.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback())); 119 entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback()));
120 EXPECT_EQ(8192, 120 EXPECT_EQ(8192,
121 entry->WriteData( 121 entry->WriteData(
122 1, 0, buffer3.get(), 8192, net::CompletionCallback(), false)); 122 1, 0, buffer3.get(), 8192, net::CompletionCallback(), false));
123 EXPECT_EQ( 123 EXPECT_EQ(
124 8192, 124 8192,
125 entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback())); 125 entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback()));
126 EXPECT_EQ(8192, entry->GetDataSize(1)); 126 EXPECT_EQ(8192, entry->GetDataSize(1));
127 127
128 // We need to delete the memory buffer on this thread. 128 // We need to delete the memory buffer on this thread.
129 EXPECT_EQ(0, entry->WriteData( 129 EXPECT_EQ(0,
130 0, 0, NULL, 0, net::CompletionCallback(), true)); 130 entry->WriteData(0, 0, NULL, 0, net::CompletionCallback(), true));
131 EXPECT_EQ(0, entry->WriteData( 131 EXPECT_EQ(0,
132 1, 0, NULL, 0, net::CompletionCallback(), true)); 132 entry->WriteData(1, 0, NULL, 0, net::CompletionCallback(), true));
133 } 133 }
134 134
135 // We need to support synchronous IO even though it is not a supported operation 135 // We need to support synchronous IO even though it is not a supported operation
136 // from the point of view of the disk cache's public interface, because we use 136 // from the point of view of the disk cache's public interface, because we use
137 // it internally, not just by a few tests, but as part of the implementation 137 // it internally, not just by a few tests, but as part of the implementation
138 // (see sparse_control.cc, for example). 138 // (see sparse_control.cc, for example).
139 void DiskCacheEntryTest::InternalSyncIO() { 139 void DiskCacheEntryTest::InternalSyncIO() {
140 disk_cache::Entry* entry = NULL; 140 disk_cache::Entry* entry = NULL;
141 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry)); 141 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
142 ASSERT_TRUE(NULL != entry); 142 ASSERT_TRUE(NULL != entry);
143 143
144 // The bulk of the test runs from within the callback, on the cache thread. 144 // The bulk of the test runs from within the callback, on the cache thread.
145 RunTaskForTest(base::Bind(&DiskCacheEntryTest::InternalSyncIOBackground, 145 RunTaskForTest(base::Bind(&DiskCacheEntryTest::InternalSyncIOBackground,
146 base::Unretained(this), 146 base::Unretained(this),
147 entry)); 147 entry));
148 148
149
150 entry->Doom(); 149 entry->Doom();
151 entry->Close(); 150 entry->Close();
152 FlushQueueForTest(); 151 FlushQueueForTest();
153 EXPECT_EQ(0, cache_->GetEntryCount()); 152 EXPECT_EQ(0, cache_->GetEntryCount());
154 } 153 }
155 154
156 TEST_F(DiskCacheEntryTest, InternalSyncIO) { 155 TEST_F(DiskCacheEntryTest, InternalSyncIO) {
157 InitCache(); 156 InitCache();
158 InternalSyncIO(); 157 InternalSyncIO();
159 } 158 }
(...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 EXPECT_EQ( 405 EXPECT_EQ(
407 17000, 406 17000,
408 entry->ReadData(1, 0, buffer1.get(), kSize1, net::CompletionCallback())); 407 entry->ReadData(1, 0, buffer1.get(), kSize1, net::CompletionCallback()));
409 EXPECT_EQ( 408 EXPECT_EQ(
410 17000, 409 17000,
411 entry->WriteData( 410 entry->WriteData(
412 1, 20000, buffer1.get(), kSize1, net::CompletionCallback(), false)); 411 1, 20000, buffer1.get(), kSize1, net::CompletionCallback(), false));
413 EXPECT_EQ(37000, entry->GetDataSize(1)); 412 EXPECT_EQ(37000, entry->GetDataSize(1));
414 413
415 // We need to delete the memory buffer on this thread. 414 // We need to delete the memory buffer on this thread.
416 EXPECT_EQ(0, entry->WriteData( 415 EXPECT_EQ(0,
417 0, 0, NULL, 0, net::CompletionCallback(), true)); 416 entry->WriteData(0, 0, NULL, 0, net::CompletionCallback(), true));
418 EXPECT_EQ(0, entry->WriteData( 417 EXPECT_EQ(0,
419 1, 0, NULL, 0, net::CompletionCallback(), true)); 418 entry->WriteData(1, 0, NULL, 0, net::CompletionCallback(), true));
420 } 419 }
421 420
422 void DiskCacheEntryTest::ExternalSyncIO() { 421 void DiskCacheEntryTest::ExternalSyncIO() {
423 disk_cache::Entry* entry; 422 disk_cache::Entry* entry;
424 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry)); 423 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
425 424
426 // The bulk of the test runs from within the callback, on the cache thread. 425 // The bulk of the test runs from within the callback, on the cache thread.
427 RunTaskForTest(base::Bind(&DiskCacheEntryTest::ExternalSyncIOBackground, 426 RunTaskForTest(base::Bind(&DiskCacheEntryTest::ExternalSyncIOBackground,
428 base::Unretained(this), 427 base::Unretained(this),
429 entry)); 428 entry));
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after
1129 } 1128 }
1130 1129
1131 TEST_F(DiskCacheEntryTest, BufferingNoBuffer) { 1130 TEST_F(DiskCacheEntryTest, BufferingNoBuffer) {
1132 InitCache(); 1131 InitCache();
1133 cache_impl_->SetFlags(disk_cache::kNoBuffering); 1132 cache_impl_->SetFlags(disk_cache::kNoBuffering);
1134 Buffering(); 1133 Buffering();
1135 } 1134 }
1136 1135
1137 // Checks that entries are zero length when created. 1136 // Checks that entries are zero length when created.
1138 void DiskCacheEntryTest::SizeAtCreate() { 1137 void DiskCacheEntryTest::SizeAtCreate() {
1139 const char key[] = "the first key"; 1138 const char key[] = "the first key";
1140 disk_cache::Entry* entry; 1139 disk_cache::Entry* entry;
1141 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1140 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1142 1141
1143 const int kNumStreams = 3; 1142 const int kNumStreams = 3;
1144 for (int i = 0; i < kNumStreams; ++i) 1143 for (int i = 0; i < kNumStreams; ++i)
1145 EXPECT_EQ(0, entry->GetDataSize(i)); 1144 EXPECT_EQ(0, entry->GetDataSize(i));
1146 entry->Close(); 1145 entry->Close();
1147 } 1146 }
1148 1147
1149 TEST_F(DiskCacheEntryTest, SizeAtCreate) { 1148 TEST_F(DiskCacheEntryTest, SizeAtCreate) {
(...skipping 475 matching lines...) Expand 10 before | Expand all | Expand 10 after
1625 ++count; 1624 ++count;
1626 disk_cache::MemEntryImpl* mem_entry = 1625 disk_cache::MemEntryImpl* mem_entry =
1627 reinterpret_cast<disk_cache::MemEntryImpl*>(entry); 1626 reinterpret_cast<disk_cache::MemEntryImpl*>(entry);
1628 EXPECT_EQ(disk_cache::MemEntryImpl::kParentEntry, mem_entry->type()); 1627 EXPECT_EQ(disk_cache::MemEntryImpl::kParentEntry, mem_entry->type());
1629 mem_entry->Close(); 1628 mem_entry->Close();
1630 } 1629 }
1631 EXPECT_EQ(1, count); 1630 EXPECT_EQ(1, count);
1632 } 1631 }
1633 1632
1634 // Writes |buf_1| to offset and reads it back as |buf_2|. 1633 // Writes |buf_1| to offset and reads it back as |buf_2|.
1635 void VerifySparseIO(disk_cache::Entry* entry, int64 offset, 1634 void VerifySparseIO(disk_cache::Entry* entry,
1636 net::IOBuffer* buf_1, int size, net::IOBuffer* buf_2) { 1635 int64 offset,
1636 net::IOBuffer* buf_1,
1637 int size,
1638 net::IOBuffer* buf_2) {
1637 net::TestCompletionCallback cb; 1639 net::TestCompletionCallback cb;
1638 1640
1639 memset(buf_2->data(), 0, size); 1641 memset(buf_2->data(), 0, size);
1640 int ret = entry->ReadSparseData(offset, buf_2, size, cb.callback()); 1642 int ret = entry->ReadSparseData(offset, buf_2, size, cb.callback());
1641 EXPECT_EQ(0, cb.GetResult(ret)); 1643 EXPECT_EQ(0, cb.GetResult(ret));
1642 1644
1643 ret = entry->WriteSparseData(offset, buf_1, size, cb.callback()); 1645 ret = entry->WriteSparseData(offset, buf_1, size, cb.callback());
1644 EXPECT_EQ(size, cb.GetResult(ret)); 1646 EXPECT_EQ(size, cb.GetResult(ret));
1645 1647
1646 ret = entry->ReadSparseData(offset, buf_2, size, cb.callback()); 1648 ret = entry->ReadSparseData(offset, buf_2, size, cb.callback());
1647 EXPECT_EQ(size, cb.GetResult(ret)); 1649 EXPECT_EQ(size, cb.GetResult(ret));
1648 1650
1649 EXPECT_EQ(0, memcmp(buf_1->data(), buf_2->data(), size)); 1651 EXPECT_EQ(0, memcmp(buf_1->data(), buf_2->data(), size));
1650 } 1652 }
1651 1653
1652 // Reads |size| bytes from |entry| at |offset| and verifies that they are the 1654 // Reads |size| bytes from |entry| at |offset| and verifies that they are the
1653 // same as the content of the provided |buffer|. 1655 // same as the content of the provided |buffer|.
1654 void VerifyContentSparseIO(disk_cache::Entry* entry, int64 offset, char* buffer, 1656 void VerifyContentSparseIO(disk_cache::Entry* entry,
1657 int64 offset,
1658 char* buffer,
1655 int size) { 1659 int size) {
1656 net::TestCompletionCallback cb; 1660 net::TestCompletionCallback cb;
1657 1661
1658 scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(size)); 1662 scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(size));
1659 memset(buf_1->data(), 0, size); 1663 memset(buf_1->data(), 0, size);
1660 int ret = entry->ReadSparseData(offset, buf_1.get(), size, cb.callback()); 1664 int ret = entry->ReadSparseData(offset, buf_1.get(), size, cb.callback());
1661 EXPECT_EQ(size, cb.GetResult(ret)); 1665 EXPECT_EQ(size, cb.GetResult(ret));
1662 EXPECT_EQ(0, memcmp(buf_1->data(), buffer, size)); 1666 EXPECT_EQ(0, memcmp(buf_1->data(), buffer, size));
1663 } 1667 }
1664 1668
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
1743 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize)); 1747 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1744 CacheTestFillBuffer(buf->data(), kSize, false); 1748 CacheTestFillBuffer(buf->data(), kSize, false);
1745 1749
1746 // Write at offset 0x20F0000 (33 MB - 64 KB), and 0x20F4400 (33 MB - 47 KB). 1750 // Write at offset 0x20F0000 (33 MB - 64 KB), and 0x20F4400 (33 MB - 47 KB).
1747 EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf.get(), kSize)); 1751 EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf.get(), kSize));
1748 EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F4400, buf.get(), kSize)); 1752 EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F4400, buf.get(), kSize));
1749 1753
1750 // We stop at the first empty block. 1754 // We stop at the first empty block.
1751 int64 start; 1755 int64 start;
1752 net::TestCompletionCallback cb; 1756 net::TestCompletionCallback cb;
1753 int rv = entry->GetAvailableRange( 1757 int rv =
1754 0x20F0000, kSize * 2, &start, cb.callback()); 1758 entry->GetAvailableRange(0x20F0000, kSize * 2, &start, cb.callback());
1755 EXPECT_EQ(kSize, cb.GetResult(rv)); 1759 EXPECT_EQ(kSize, cb.GetResult(rv));
1756 EXPECT_EQ(0x20F0000, start); 1760 EXPECT_EQ(0x20F0000, start);
1757 1761
1758 start = 0; 1762 start = 0;
1759 rv = entry->GetAvailableRange(0, kSize, &start, cb.callback()); 1763 rv = entry->GetAvailableRange(0, kSize, &start, cb.callback());
1760 EXPECT_EQ(0, cb.GetResult(rv)); 1764 EXPECT_EQ(0, cb.GetResult(rv));
1761 rv = entry->GetAvailableRange( 1765 rv =
1762 0x20F0000 - kSize, kSize, &start, cb.callback()); 1766 entry->GetAvailableRange(0x20F0000 - kSize, kSize, &start, cb.callback());
1763 EXPECT_EQ(0, cb.GetResult(rv)); 1767 EXPECT_EQ(0, cb.GetResult(rv));
1764 rv = entry->GetAvailableRange(0, 0x2100000, &start, cb.callback()); 1768 rv = entry->GetAvailableRange(0, 0x2100000, &start, cb.callback());
1765 EXPECT_EQ(kSize, cb.GetResult(rv)); 1769 EXPECT_EQ(kSize, cb.GetResult(rv));
1766 EXPECT_EQ(0x20F0000, start); 1770 EXPECT_EQ(0x20F0000, start);
1767 1771
1768 // We should be able to Read based on the results of GetAvailableRange. 1772 // We should be able to Read based on the results of GetAvailableRange.
1769 start = -1; 1773 start = -1;
1770 rv = entry->GetAvailableRange(0x2100000, kSize, &start, cb.callback()); 1774 rv = entry->GetAvailableRange(0x2100000, kSize, &start, cb.callback());
1771 EXPECT_EQ(0, cb.GetResult(rv)); 1775 EXPECT_EQ(0, cb.GetResult(rv));
1772 rv = entry->ReadSparseData(start, buf.get(), kSize, cb.callback()); 1776 rv = entry->ReadSparseData(start, buf.get(), kSize, cb.callback());
1773 EXPECT_EQ(0, cb.GetResult(rv)); 1777 EXPECT_EQ(0, cb.GetResult(rv));
1774 1778
1775 start = 0; 1779 start = 0;
1776 rv = entry->GetAvailableRange(0x20F2000, kSize, &start, cb.callback()); 1780 rv = entry->GetAvailableRange(0x20F2000, kSize, &start, cb.callback());
1777 EXPECT_EQ(0x2000, cb.GetResult(rv)); 1781 EXPECT_EQ(0x2000, cb.GetResult(rv));
1778 EXPECT_EQ(0x20F2000, start); 1782 EXPECT_EQ(0x20F2000, start);
1779 EXPECT_EQ(0x2000, ReadSparseData(entry, start, buf.get(), kSize)); 1783 EXPECT_EQ(0x2000, ReadSparseData(entry, start, buf.get(), kSize));
1780 1784
1781 // Make sure that we respect the |len| argument. 1785 // Make sure that we respect the |len| argument.
1782 start = 0; 1786 start = 0;
1783 rv = entry->GetAvailableRange( 1787 rv =
1784 0x20F0001 - kSize, kSize, &start, cb.callback()); 1788 entry->GetAvailableRange(0x20F0001 - kSize, kSize, &start, cb.callback());
1785 EXPECT_EQ(1, cb.GetResult(rv)); 1789 EXPECT_EQ(1, cb.GetResult(rv));
1786 EXPECT_EQ(0x20F0000, start); 1790 EXPECT_EQ(0x20F0000, start);
1787 1791
1788 entry->Close(); 1792 entry->Close();
1789 } 1793 }
1790 1794
1791 TEST_F(DiskCacheEntryTest, GetAvailableRange) { 1795 TEST_F(DiskCacheEntryTest, GetAvailableRange) {
1792 InitCache(); 1796 InitCache();
1793 GetAvailableRange(); 1797 GetAvailableRange();
1794 } 1798 }
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1855 scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize)); 1859 scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
1856 CacheTestFillBuffer(buf_1->data(), kSize, false); 1860 CacheTestFillBuffer(buf_1->data(), kSize, false);
1857 1861
1858 std::string key("the first key"); 1862 std::string key("the first key");
1859 disk_cache::Entry* entry; 1863 disk_cache::Entry* entry;
1860 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); 1864 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
1861 1865
1862 // This loop writes back to back starting from offset 0 and 9000. 1866 // This loop writes back to back starting from offset 0 and 9000.
1863 for (int i = 0; i < kSize; i += 1024) { 1867 for (int i = 0; i < kSize; i += 1024) {
1864 scoped_refptr<net::WrappedIOBuffer> buf_3( 1868 scoped_refptr<net::WrappedIOBuffer> buf_3(
1865 new net::WrappedIOBuffer(buf_1->data() + i)); 1869 new net::WrappedIOBuffer(buf_1->data() + i));
1866 VerifySparseIO(entry, i, buf_3.get(), 1024, buf_2.get()); 1870 VerifySparseIO(entry, i, buf_3.get(), 1024, buf_2.get());
1867 VerifySparseIO(entry, 9000 + i, buf_3.get(), 1024, buf_2.get()); 1871 VerifySparseIO(entry, 9000 + i, buf_3.get(), 1024, buf_2.get());
1868 } 1872 }
1869 1873
1870 // Make sure we have data written. 1874 // Make sure we have data written.
1871 VerifyContentSparseIO(entry, 0, buf_1->data(), kSize); 1875 VerifyContentSparseIO(entry, 0, buf_1->data(), kSize);
1872 VerifyContentSparseIO(entry, 9000, buf_1->data(), kSize); 1876 VerifyContentSparseIO(entry, 9000, buf_1->data(), kSize);
1873 1877
1874 // This tests a large write that spans 3 entries from a misaligned offset. 1878 // This tests a large write that spans 3 entries from a misaligned offset.
1875 VerifySparseIO(entry, 20481, buf_1.get(), 8192, buf_2.get()); 1879 VerifySparseIO(entry, 20481, buf_1.get(), 8192, buf_2.get());
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
1982 TEST_F(DiskCacheEntryTest, MemoryOnlyUpdateSparseEntry) { 1986 TEST_F(DiskCacheEntryTest, MemoryOnlyUpdateSparseEntry) {
1983 SetMemoryOnlyMode(); 1987 SetMemoryOnlyMode();
1984 SetCacheType(net::MEDIA_CACHE); 1988 SetCacheType(net::MEDIA_CACHE);
1985 InitCache(); 1989 InitCache();
1986 UpdateSparseEntry(); 1990 UpdateSparseEntry();
1987 } 1991 }
1988 1992
1989 void DiskCacheEntryTest::DoomSparseEntry() { 1993 void DiskCacheEntryTest::DoomSparseEntry() {
1990 std::string key1("the first key"); 1994 std::string key1("the first key");
1991 std::string key2("the second key"); 1995 std::string key2("the second key");
1992 disk_cache::Entry *entry1, *entry2; 1996 disk_cache::Entry* entry1, *entry2;
1993 ASSERT_EQ(net::OK, CreateEntry(key1, &entry1)); 1997 ASSERT_EQ(net::OK, CreateEntry(key1, &entry1));
1994 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2)); 1998 ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
1995 1999
1996 const int kSize = 4 * 1024; 2000 const int kSize = 4 * 1024;
1997 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize)); 2001 scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
1998 CacheTestFillBuffer(buf->data(), kSize, false); 2002 CacheTestFillBuffer(buf->data(), kSize, false);
1999 2003
2000 int64 offset = 1024; 2004 int64 offset = 1024;
2001 // Write to a bunch of ranges. 2005 // Write to a bunch of ranges.
2002 for (int i = 0; i < 12; i++) { 2006 for (int i = 0; i < 12; i++) {
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
2049 TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) { 2053 TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) {
2050 SetMemoryOnlyMode(); 2054 SetMemoryOnlyMode();
2051 InitCache(); 2055 InitCache();
2052 DoomSparseEntry(); 2056 DoomSparseEntry();
2053 } 2057 }
2054 2058
2055 // A CompletionCallback wrapper that deletes the cache from within the callback. 2059 // A CompletionCallback wrapper that deletes the cache from within the callback.
2056 // The way a CompletionCallback works means that all tasks (even new ones) 2060 // The way a CompletionCallback works means that all tasks (even new ones)
2057 // are executed by the message loop before returning to the caller so the only 2061 // are executed by the message loop before returning to the caller so the only
2058 // way to simulate a race is to execute what we want on the callback. 2062 // way to simulate a race is to execute what we want on the callback.
2059 class SparseTestCompletionCallback: public net::TestCompletionCallback { 2063 class SparseTestCompletionCallback : public net::TestCompletionCallback {
2060 public: 2064 public:
2061 explicit SparseTestCompletionCallback(scoped_ptr<disk_cache::Backend> cache) 2065 explicit SparseTestCompletionCallback(scoped_ptr<disk_cache::Backend> cache)
2062 : cache_(cache.Pass()) { 2066 : cache_(cache.Pass()) {}
2063 }
2064 2067
2065 private: 2068 private:
2066 virtual void SetResult(int result) OVERRIDE { 2069 virtual void SetResult(int result) OVERRIDE {
2067 cache_.reset(); 2070 cache_.reset();
2068 TestCompletionCallback::SetResult(result); 2071 TestCompletionCallback::SetResult(result);
2069 } 2072 }
2070 2073
2071 scoped_ptr<disk_cache::Backend> cache_; 2074 scoped_ptr<disk_cache::Backend> cache_;
2072 DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback); 2075 DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback);
2073 }; 2076 };
(...skipping 486 matching lines...) Expand 10 before | Expand all | Expand 10 after
2560 2563
2561 const int kReadBufferSize = 200; 2564 const int kReadBufferSize = 200;
2562 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1)); 2565 EXPECT_GE(kReadBufferSize, entry->GetDataSize(1));
2563 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize)); 2566 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
2564 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH, 2567 EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
2565 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize)); 2568 ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize));
2566 2569
2567 entry->Doom(); // Should not crash. 2570 entry->Doom(); // Should not crash.
2568 } 2571 }
2569 2572
2570 bool TruncatePath(const base::FilePath& file_path, int64 length) { 2573 bool TruncatePath(const base::FilePath& file_path, int64 length) {
2571 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN); 2574 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN);
2572 if (!file.IsValid()) 2575 if (!file.IsValid())
2573 return false; 2576 return false;
2574 return file.SetLength(length); 2577 return file.SetLength(length);
2575 } 2578 }
2576 2579
2577 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) { 2580 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) {
2578 SetSimpleCacheMode(); 2581 SetSimpleCacheMode();
2579 InitCache(); 2582 InitCache();
2580 2583
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
2739 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 2742 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
2740 scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1)); 2743 scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1));
2741 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2)); 2744 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
2742 scoped_refptr<net::IOBuffer> buffer2_read(new net::IOBuffer(kSize2)); 2745 scoped_refptr<net::IOBuffer> buffer2_read(new net::IOBuffer(kSize2));
2743 CacheTestFillBuffer(buffer1->data(), kSize1, false); 2746 CacheTestFillBuffer(buffer1->data(), kSize1, false);
2744 CacheTestFillBuffer(buffer2->data(), kSize2, false); 2747 CacheTestFillBuffer(buffer2->data(), kSize2, false);
2745 2748
2746 disk_cache::Entry* entry = NULL; 2749 disk_cache::Entry* entry = NULL;
2747 // Create is optimistic, must return OK. 2750 // Create is optimistic, must return OK.
2748 ASSERT_EQ(net::OK, 2751 ASSERT_EQ(net::OK,
2749 cache_->CreateEntry(key, &entry, 2752 cache_->CreateEntry(
2750 base::Bind(&CallbackTest::Run, 2753 key,
2751 base::Unretained(&callback1)))); 2754 &entry,
2755 base::Bind(&CallbackTest::Run, base::Unretained(&callback1))));
2752 EXPECT_NE(null, entry); 2756 EXPECT_NE(null, entry);
2753 ScopedEntryPtr entry_closer(entry); 2757 ScopedEntryPtr entry_closer(entry);
2754 2758
2755 // This write may or may not be optimistic (it depends if the previous 2759 // This write may or may not be optimistic (it depends if the previous
2756 // optimistic create already finished by the time we call the write here). 2760 // optimistic create already finished by the time we call the write here).
2757 int ret = entry->WriteData( 2761 int ret = entry->WriteData(
2758 1, 2762 1,
2759 0, 2763 0,
2760 buffer1.get(), 2764 buffer1.get(),
2761 kSize1, 2765 kSize1,
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
2797 buffer2_read.get(), 2801 buffer2_read.get(),
2798 kSize2, 2802 kSize2,
2799 base::Bind(&CallbackTest::Run, base::Unretained(&callback5)))); 2803 base::Bind(&CallbackTest::Run, base::Unretained(&callback5))));
2800 expected++; 2804 expected++;
2801 2805
2802 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected)); 2806 EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
2803 EXPECT_EQ(0, memcmp(buffer2->data(), buffer2_read->data(), kSize2)); 2807 EXPECT_EQ(0, memcmp(buffer2->data(), buffer2_read->data(), kSize2));
2804 2808
2805 // Check that we are not leaking. 2809 // Check that we are not leaking.
2806 EXPECT_NE(entry, null); 2810 EXPECT_NE(entry, null);
2807 EXPECT_TRUE( 2811 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2808 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2809 } 2812 }
2810 2813
2811 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic2) { 2814 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic2) {
2812 // Test sequence: 2815 // Test sequence:
2813 // Create, Open, Close, Close. 2816 // Create, Open, Close, Close.
2814 SetSimpleCacheMode(); 2817 SetSimpleCacheMode();
2815 InitCache(); 2818 InitCache();
2816 disk_cache::Entry* null = NULL; 2819 disk_cache::Entry* null = NULL;
2817 const char key[] = "the first key"; 2820 const char key[] = "the first key";
2818 2821
2819 MessageLoopHelper helper; 2822 MessageLoopHelper helper;
2820 CallbackTest callback1(&helper, false); 2823 CallbackTest callback1(&helper, false);
2821 CallbackTest callback2(&helper, false); 2824 CallbackTest callback2(&helper, false);
2822 2825
2823 disk_cache::Entry* entry = NULL; 2826 disk_cache::Entry* entry = NULL;
2824 ASSERT_EQ(net::OK, 2827 ASSERT_EQ(net::OK,
2825 cache_->CreateEntry(key, &entry, 2828 cache_->CreateEntry(
2826 base::Bind(&CallbackTest::Run, 2829 key,
2827 base::Unretained(&callback1)))); 2830 &entry,
2831 base::Bind(&CallbackTest::Run, base::Unretained(&callback1))));
2828 EXPECT_NE(null, entry); 2832 EXPECT_NE(null, entry);
2829 ScopedEntryPtr entry_closer(entry); 2833 ScopedEntryPtr entry_closer(entry);
2830 2834
2831 disk_cache::Entry* entry2 = NULL; 2835 disk_cache::Entry* entry2 = NULL;
2832 ASSERT_EQ(net::ERR_IO_PENDING, 2836 ASSERT_EQ(net::ERR_IO_PENDING,
2833 cache_->OpenEntry(key, &entry2, 2837 cache_->OpenEntry(
2834 base::Bind(&CallbackTest::Run, 2838 key,
2835 base::Unretained(&callback2)))); 2839 &entry2,
2840 base::Bind(&CallbackTest::Run, base::Unretained(&callback2))));
2836 ASSERT_TRUE(helper.WaitUntilCacheIoFinished(1)); 2841 ASSERT_TRUE(helper.WaitUntilCacheIoFinished(1));
2837 2842
2838 EXPECT_NE(null, entry2); 2843 EXPECT_NE(null, entry2);
2839 EXPECT_EQ(entry, entry2); 2844 EXPECT_EQ(entry, entry2);
2840 2845
2841 // We have to call close twice, since we called create and open above. 2846 // We have to call close twice, since we called create and open above.
2842 entry->Close(); 2847 entry->Close();
2843 2848
2844 // Check that we are not leaking. 2849 // Check that we are not leaking.
2845 EXPECT_TRUE( 2850 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2846 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2847 } 2851 }
2848 2852
2849 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic3) { 2853 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic3) {
2850 // Test sequence: 2854 // Test sequence:
2851 // Create, Close, Open, Close. 2855 // Create, Close, Open, Close.
2852 SetSimpleCacheMode(); 2856 SetSimpleCacheMode();
2853 InitCache(); 2857 InitCache();
2854 disk_cache::Entry* null = NULL; 2858 disk_cache::Entry* null = NULL;
2855 const char key[] = "the first key"; 2859 const char key[] = "the first key";
2856 2860
2857 disk_cache::Entry* entry = NULL; 2861 disk_cache::Entry* entry = NULL;
2858 ASSERT_EQ(net::OK, 2862 ASSERT_EQ(net::OK,
2859 cache_->CreateEntry(key, &entry, net::CompletionCallback())); 2863 cache_->CreateEntry(key, &entry, net::CompletionCallback()));
2860 EXPECT_NE(null, entry); 2864 EXPECT_NE(null, entry);
2861 entry->Close(); 2865 entry->Close();
2862 2866
2863 net::TestCompletionCallback cb; 2867 net::TestCompletionCallback cb;
2864 disk_cache::Entry* entry2 = NULL; 2868 disk_cache::Entry* entry2 = NULL;
2865 ASSERT_EQ(net::ERR_IO_PENDING, 2869 ASSERT_EQ(net::ERR_IO_PENDING,
2866 cache_->OpenEntry(key, &entry2, cb.callback())); 2870 cache_->OpenEntry(key, &entry2, cb.callback()));
2867 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING)); 2871 ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
2868 ScopedEntryPtr entry_closer(entry2); 2872 ScopedEntryPtr entry_closer(entry2);
2869 2873
2870 EXPECT_NE(null, entry2); 2874 EXPECT_NE(null, entry2);
2871 EXPECT_EQ(entry, entry2); 2875 EXPECT_EQ(entry, entry2);
2872 2876
2873 // Check that we are not leaking. 2877 // Check that we are not leaking.
2874 EXPECT_TRUE( 2878 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
2875 static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
2876 } 2879 }
2877 2880
2878 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic4) { 2881 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic4) {
2879 // Test sequence: 2882 // Test sequence:
2880 // Create, Close, Write, Open, Open, Close, Write, Read, Close. 2883 // Create, Close, Write, Open, Open, Close, Write, Read, Close.
2881 SetSimpleCacheMode(); 2884 SetSimpleCacheMode();
2882 InitCache(); 2885 InitCache();
2883 disk_cache::Entry* null = NULL; 2886 disk_cache::Entry* null = NULL;
2884 const char key[] = "the first key"; 2887 const char key[] = "the first key";
2885 2888
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
2929 entry2->WriteData( 2932 entry2->WriteData(
2930 1, 0, buffer1.get(), kSize1, net::CompletionCallback(), false)); 2933 1, 0, buffer1.get(), kSize1, net::CompletionCallback(), false));
2931 2934
2932 // Lets do another read so we block until both the write and the read 2935 // Lets do another read so we block until both the write and the read
2933 // operation finishes and we can then test for HasOneRef() below. 2936 // operation finishes and we can then test for HasOneRef() below.
2934 EXPECT_EQ(net::ERR_IO_PENDING, 2937 EXPECT_EQ(net::ERR_IO_PENDING,
2935 entry2->ReadData(1, 0, buffer1.get(), kSize1, cb.callback())); 2938 entry2->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
2936 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2939 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2937 2940
2938 // Check that we are not leaking. 2941 // Check that we are not leaking.
2939 EXPECT_TRUE( 2942 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
2940 static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
2941 entry2->Close(); 2943 entry2->Close();
2942 } 2944 }
2943 2945
2944 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic5) { 2946 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic5) {
2945 // Test sequence: 2947 // Test sequence:
2946 // Create, Doom, Write, Read, Close. 2948 // Create, Doom, Write, Read, Close.
2947 SetSimpleCacheMode(); 2949 SetSimpleCacheMode();
2948 InitCache(); 2950 InitCache();
2949 disk_cache::Entry* null = NULL; 2951 disk_cache::Entry* null = NULL;
2950 const char key[] = "the first key"; 2952 const char key[] = "the first key";
(...skipping 13 matching lines...) Expand all
2964 EXPECT_EQ( 2966 EXPECT_EQ(
2965 net::ERR_IO_PENDING, 2967 net::ERR_IO_PENDING,
2966 entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false)); 2968 entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
2967 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2969 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2968 2970
2969 EXPECT_EQ(net::ERR_IO_PENDING, 2971 EXPECT_EQ(net::ERR_IO_PENDING,
2970 entry->ReadData(1, 0, buffer1.get(), kSize1, cb.callback())); 2972 entry->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
2971 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING)); 2973 EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
2972 2974
2973 // Check that we are not leaking. 2975 // Check that we are not leaking.
2974 EXPECT_TRUE( 2976 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2975 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
2976 } 2977 }
2977 2978
2978 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic6) { 2979 TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic6) {
2979 // Test sequence: 2980 // Test sequence:
2980 // Create, Write, Doom, Doom, Read, Doom, Close. 2981 // Create, Write, Doom, Doom, Read, Doom, Close.
2981 SetSimpleCacheMode(); 2982 SetSimpleCacheMode();
2982 InitCache(); 2983 InitCache();
2983 disk_cache::Entry* null = NULL; 2984 disk_cache::Entry* null = NULL;
2984 const char key[] = "the first key"; 2985 const char key[] = "the first key";
2985 2986
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
3136 3137
3137 disk_cache::Entry* entry2 = NULL; 3138 disk_cache::Entry* entry2 = NULL;
3138 ASSERT_EQ(net::OK, CreateEntry(key, &entry2)); 3139 ASSERT_EQ(net::OK, CreateEntry(key, &entry2));
3139 ScopedEntryPtr entry2_closer(entry2); 3140 ScopedEntryPtr entry2_closer(entry2);
3140 EXPECT_NE(null, entry2); 3141 EXPECT_NE(null, entry2);
3141 3142
3142 // Redundantly dooming entry1 should not delete entry2. 3143 // Redundantly dooming entry1 should not delete entry2.
3143 disk_cache::SimpleEntryImpl* simple_entry1 = 3144 disk_cache::SimpleEntryImpl* simple_entry1 =
3144 static_cast<disk_cache::SimpleEntryImpl*>(entry1); 3145 static_cast<disk_cache::SimpleEntryImpl*>(entry1);
3145 net::TestCompletionCallback cb; 3146 net::TestCompletionCallback cb;
3146 EXPECT_EQ(net::OK, 3147 EXPECT_EQ(net::OK, cb.GetResult(simple_entry1->DoomEntry(cb.callback())));
3147 cb.GetResult(simple_entry1->DoomEntry(cb.callback())));
3148 3148
3149 disk_cache::Entry* entry3 = NULL; 3149 disk_cache::Entry* entry3 = NULL;
3150 ASSERT_EQ(net::OK, OpenEntry(key, &entry3)); 3150 ASSERT_EQ(net::OK, OpenEntry(key, &entry3));
3151 ScopedEntryPtr entry3_closer(entry3); 3151 ScopedEntryPtr entry3_closer(entry3);
3152 EXPECT_NE(null, entry3); 3152 EXPECT_NE(null, entry3);
3153 } 3153 }
3154 3154
3155 TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateDoom) { 3155 TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateDoom) {
3156 // Test sequence: 3156 // Test sequence:
3157 // Create, Doom, Create, Doom. 3157 // Create, Doom, Create, Doom.
(...skipping 26 matching lines...) Expand all
3184 SetSimpleCacheMode(); 3184 SetSimpleCacheMode();
3185 InitCache(); 3185 InitCache();
3186 3186
3187 // Create a corrupt file in place of a future entry. Optimistic create should 3187 // Create a corrupt file in place of a future entry. Optimistic create should
3188 // initially succeed, but realize later that creation failed. 3188 // initially succeed, but realize later that creation failed.
3189 const std::string key = "the key"; 3189 const std::string key = "the key";
3190 net::TestCompletionCallback cb; 3190 net::TestCompletionCallback cb;
3191 disk_cache::Entry* entry = NULL; 3191 disk_cache::Entry* entry = NULL;
3192 disk_cache::Entry* entry2 = NULL; 3192 disk_cache::Entry* entry2 = NULL;
3193 3193
3194 EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests( 3194 EXPECT_TRUE(
3195 key, cache_path_)); 3195 disk_cache::simple_util::CreateCorruptFileForTests(key, cache_path_));
3196 EXPECT_EQ(net::OK, cache_->CreateEntry(key, &entry, cb.callback())); 3196 EXPECT_EQ(net::OK, cache_->CreateEntry(key, &entry, cb.callback()));
3197 ASSERT_TRUE(entry); 3197 ASSERT_TRUE(entry);
3198 ScopedEntryPtr entry_closer(entry); 3198 ScopedEntryPtr entry_closer(entry);
3199 ASSERT_NE(net::OK, OpenEntry(key, &entry2)); 3199 ASSERT_NE(net::OK, OpenEntry(key, &entry2));
3200 3200
3201 // Check that we are not leaking. 3201 // Check that we are not leaking.
3202 EXPECT_TRUE( 3202 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3203 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3204 3203
3205 DisableIntegrityCheck(); 3204 DisableIntegrityCheck();
3206 } 3205 }
3207 3206
3208 // Tests that old entries are evicted while new entries remain in the index. 3207 // Tests that old entries are evicted while new entries remain in the index.
3209 // This test relies on non-mandatory properties of the simple Cache Backend: 3208 // This test relies on non-mandatory properties of the simple Cache Backend:
3210 // LRU eviction, specific values of high-watermark and low-watermark etc. 3209 // LRU eviction, specific values of high-watermark and low-watermark etc.
3211 // When changing the eviction algorithm, the test will have to be re-engineered. 3210 // When changing the eviction algorithm, the test will have to be re-engineered.
3212 TEST_F(DiskCacheEntryTest, SimpleCacheEvictOldEntries) { 3211 TEST_F(DiskCacheEntryTest, SimpleCacheEvictOldEntries) {
3213 const int kMaxSize = 200 * 1024; 3212 const int kMaxSize = 200 * 1024;
(...skipping 28 matching lines...) Expand all
3242 3241
3243 // TODO(pasko): Find a way to wait for the eviction task(s) to finish by using 3242 // TODO(pasko): Find a way to wait for the eviction task(s) to finish by using
3244 // the internal knowledge about |SimpleBackendImpl|. 3243 // the internal knowledge about |SimpleBackendImpl|.
3245 ASSERT_NE(net::OK, OpenEntry(key1, &entry)) 3244 ASSERT_NE(net::OK, OpenEntry(key1, &entry))
3246 << "Should have evicted the old entry"; 3245 << "Should have evicted the old entry";
3247 for (int i = 0; i < 2; i++) { 3246 for (int i = 0; i < 2; i++) {
3248 int entry_no = kNumExtraEntries - i - 1; 3247 int entry_no = kNumExtraEntries - i - 1;
3249 // Generally there is no guarantee that at this point the backround eviction 3248 // Generally there is no guarantee that at this point the backround eviction
3250 // is finished. We are testing the positive case, i.e. when the eviction 3249 // is finished. We are testing the positive case, i.e. when the eviction
3251 // never reaches this entry, should be non-flaky. 3250 // never reaches this entry, should be non-flaky.
3252 ASSERT_EQ(net::OK, OpenEntry(key2 + base::StringPrintf("%d", entry_no), 3251 ASSERT_EQ(net::OK,
3253 &entry)) 3252 OpenEntry(key2 + base::StringPrintf("%d", entry_no), &entry))
3254 << "Should not have evicted fresh entry " << entry_no; 3253 << "Should not have evicted fresh entry " << entry_no;
3255 entry->Close(); 3254 entry->Close();
3256 } 3255 }
3257 } 3256 }
3258 3257
3259 // Tests that if a read and a following in-flight truncate are both in progress 3258 // Tests that if a read and a following in-flight truncate are both in progress
3260 // simultaniously that they both can occur successfully. See 3259 // simultaniously that they both can occur successfully. See
3261 // http://crbug.com/239223 3260 // http://crbug.com/239223
3262 TEST_F(DiskCacheEntryTest, SimpleCacheInFlightTruncate) { 3261 TEST_F(DiskCacheEntryTest, SimpleCacheInFlightTruncate) {
3263 SetSimpleCacheMode(); 3262 SetSimpleCacheMode();
3264 InitCache(); 3263 InitCache();
3265 3264
3266 const char key[] = "the first key"; 3265 const char key[] = "the first key";
3267 3266
3268 const int kBufferSize = 1024; 3267 const int kBufferSize = 1024;
3269 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize)); 3268 scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
3270 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false); 3269 CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
3271 3270
3272 disk_cache::Entry* entry = NULL; 3271 disk_cache::Entry* entry = NULL;
(...skipping 17 matching lines...) Expand all
3290 EXPECT_EQ(net::ERR_IO_PENDING, 3289 EXPECT_EQ(net::ERR_IO_PENDING,
3291 entry->ReadData(1, 3290 entry->ReadData(1,
3292 0, 3291 0,
3293 read_buffer.get(), 3292 read_buffer.get(),
3294 kReadBufferSize, 3293 kReadBufferSize,
3295 base::Bind(&CallbackTest::Run, 3294 base::Bind(&CallbackTest::Run,
3296 base::Unretained(&read_callback)))); 3295 base::Unretained(&read_callback))));
3297 ++expected; 3296 ++expected;
3298 3297
3299 // Truncate the entry to the length of that read. 3298 // Truncate the entry to the length of that read.
3300 scoped_refptr<net::IOBuffer> 3299 scoped_refptr<net::IOBuffer> truncate_buffer(
3301 truncate_buffer(new net::IOBuffer(kReadBufferSize)); 3300 new net::IOBuffer(kReadBufferSize));
3302 CacheTestFillBuffer(truncate_buffer->data(), kReadBufferSize, false); 3301 CacheTestFillBuffer(truncate_buffer->data(), kReadBufferSize, false);
3303 CallbackTest truncate_callback(&helper, false); 3302 CallbackTest truncate_callback(&helper, false);
3304 EXPECT_EQ(net::ERR_IO_PENDING, 3303 EXPECT_EQ(net::ERR_IO_PENDING,
3305 entry->WriteData(1, 3304 entry->WriteData(1,
3306 0, 3305 0,
3307 truncate_buffer.get(), 3306 truncate_buffer.get(),
3308 kReadBufferSize, 3307 kReadBufferSize,
3309 base::Bind(&CallbackTest::Run, 3308 base::Bind(&CallbackTest::Run,
3310 base::Unretained(&truncate_callback)), 3309 base::Unretained(&truncate_callback)),
3311 true)); 3310 true));
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
3491 // Read the second half of the data. 3490 // Read the second half of the data.
3492 offset = buf_len; 3491 offset = buf_len;
3493 buf_len = kHalfSize; 3492 buf_len = kHalfSize;
3494 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len)); 3493 scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len));
3495 EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read2.get(), buf_len)); 3494 EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read2.get(), buf_len));
3496 char* buffer1_data = buffer1->data() + offset; 3495 char* buffer1_data = buffer1->data() + offset;
3497 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len)); 3496 EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len));
3498 3497
3499 // Check that we are not leaking. 3498 // Check that we are not leaking.
3500 EXPECT_NE(entry, null); 3499 EXPECT_NE(entry, null);
3501 EXPECT_TRUE( 3500 EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3502 static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
3503 entry->Close(); 3501 entry->Close();
3504 entry = NULL; 3502 entry = NULL;
3505 } 3503 }
3506 3504
3507 // Test if we can write the data not in sequence and read correctly. In 3505 // Test if we can write the data not in sequence and read correctly. In
3508 // this case the CRC will not be present. 3506 // this case the CRC will not be present.
3509 TEST_F(DiskCacheEntryTest, SimpleCacheNonSequentialWrite) { 3507 TEST_F(DiskCacheEntryTest, SimpleCacheNonSequentialWrite) {
3510 // Test sequence: 3508 // Test sequence:
3511 // Create, Write (second half of data), Write (first half of data), Read, 3509 // Create, Write (second half of data), Write (first half of data), Read,
3512 // Close. 3510 // Close.
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
3593 base::File::FLAG_READ | base::File::FLAG_OPEN); 3591 base::File::FLAG_READ | base::File::FLAG_OPEN);
3594 ASSERT_TRUE(entry_file0.IsValid()); 3592 ASSERT_TRUE(entry_file0.IsValid());
3595 3593
3596 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0}; 3594 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0};
3597 int sparse_data_size = 0; 3595 int sparse_data_size = 0;
3598 disk_cache::SimpleEntryStat entry_stat( 3596 disk_cache::SimpleEntryStat entry_stat(
3599 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size); 3597 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size);
3600 int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0); 3598 int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0);
3601 disk_cache::SimpleFileEOF eof_record; 3599 disk_cache::SimpleFileEOF eof_record;
3602 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), 3600 ASSERT_EQ(static_cast<int>(sizeof(eof_record)),
3603 entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record), 3601 entry_file0.Read(eof_offset,
3602 reinterpret_cast<char*>(&eof_record),
3604 sizeof(eof_record))); 3603 sizeof(eof_record)));
3605 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number); 3604 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number);
3606 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) == 3605 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) ==
3607 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32); 3606 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32);
3608 3607
3609 buffer_read = new net::IOBuffer(kSize); 3608 buffer_read = new net::IOBuffer(kSize);
3610 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize)); 3609 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
3611 EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize)); 3610 EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
3612 3611
3613 // Shrink stream1. 3612 // Shrink stream1.
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
3962 EXPECT_EQ(kSize, callback.GetResult(ret)); 3961 EXPECT_EQ(kSize, callback.GetResult(ret));
3963 3962
3964 // Make sure the first range was removed when the second was written. 3963 // Make sure the first range was removed when the second was written.
3965 ret = entry->ReadSparseData(0, buffer, kSize, callback.callback()); 3964 ret = entry->ReadSparseData(0, buffer, kSize, callback.callback());
3966 EXPECT_EQ(0, callback.GetResult(ret)); 3965 EXPECT_EQ(0, callback.GetResult(ret));
3967 3966
3968 entry->Close(); 3967 entry->Close();
3969 } 3968 }
3970 3969
3971 #endif // defined(OS_POSIX) 3970 #endif // defined(OS_POSIX)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698