| 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 "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 3741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3752 const char key[] = "key"; | 3752 const char key[] = "key"; |
| 3753 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3753 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
| 3754 CacheTestFillBuffer(buffer->data(), kHalfSize, false); | 3754 CacheTestFillBuffer(buffer->data(), kHalfSize, false); |
| 3755 | 3755 |
| 3756 disk_cache::Entry* entry; | 3756 disk_cache::Entry* entry; |
| 3757 | 3757 |
| 3758 // Create entry, write empty buffer to third stream, and close: third stream | 3758 // Create entry, write empty buffer to third stream, and close: third stream |
| 3759 // should still be omitted, since the entry ignores writes that don't modify | 3759 // should still be omitted, since the entry ignores writes that don't modify |
| 3760 // data or change the length. | 3760 // data or change the length. |
| 3761 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3761 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3762 EXPECT_EQ(0, WriteData(entry, 2, 0, buffer, 0, true)); | 3762 EXPECT_EQ(0, WriteData(entry, 2, 0, buffer.get(), 0, true)); |
| 3763 entry->Close(); | 3763 entry->Close(); |
| 3764 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3764 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3765 | 3765 |
| 3766 SyncDoomEntry(key); | 3766 SyncDoomEntry(key); |
| 3767 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3767 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3768 } | 3768 } |
| 3769 | 3769 |
| 3770 // Check that we can read back data written to the third stream. | 3770 // Check that we can read back data written to the third stream. |
| 3771 TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream3) { | 3771 TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream3) { |
| 3772 SetSimpleCacheMode(); | 3772 SetSimpleCacheMode(); |
| 3773 InitCache(); | 3773 InitCache(); |
| 3774 | 3774 |
| 3775 const int kHalfSize = 8; | 3775 const int kHalfSize = 8; |
| 3776 const int kSize = kHalfSize * 2; | 3776 const int kSize = kHalfSize * 2; |
| 3777 const char key[] = "key"; | 3777 const char key[] = "key"; |
| 3778 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); | 3778 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize)); |
| 3779 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); | 3779 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize)); |
| 3780 CacheTestFillBuffer(buffer1->data(), kHalfSize, false); | 3780 CacheTestFillBuffer(buffer1->data(), kHalfSize, false); |
| 3781 | 3781 |
| 3782 disk_cache::Entry* entry; | 3782 disk_cache::Entry* entry; |
| 3783 | 3783 |
| 3784 // Create entry, write data to third stream, and close: third stream should | 3784 // Create entry, write data to third stream, and close: third stream should |
| 3785 // not be omitted, since it contains data. Re-open entry and ensure there | 3785 // not be omitted, since it contains data. Re-open entry and ensure there |
| 3786 // are that many bytes in the third stream. | 3786 // are that many bytes in the third stream. |
| 3787 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3787 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3788 EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1, kHalfSize, true)); | 3788 EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1.get(), kHalfSize, true)); |
| 3789 entry->Close(); | 3789 entry->Close(); |
| 3790 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); | 3790 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); |
| 3791 | 3791 |
| 3792 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 3792 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
| 3793 EXPECT_EQ(kHalfSize, ReadData(entry, 2, 0, buffer2, kSize)); | 3793 EXPECT_EQ(kHalfSize, ReadData(entry, 2, 0, buffer2.get(), kSize)); |
| 3794 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kHalfSize)); | 3794 EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kHalfSize)); |
| 3795 entry->Close(); | 3795 entry->Close(); |
| 3796 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); | 3796 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); |
| 3797 | 3797 |
| 3798 SyncDoomEntry(key); | 3798 SyncDoomEntry(key); |
| 3799 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3799 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3800 } | 3800 } |
| 3801 | 3801 |
| 3802 // Check that we remove the third stream file upon opening an entry and finding | 3802 // Check that we remove the third stream file upon opening an entry and finding |
| 3803 // the third stream empty. (This is the upgrade path for entries written | 3803 // the third stream empty. (This is the upgrade path for entries written |
| (...skipping 10 matching lines...) Expand all Loading... |
| 3814 CacheTestFillBuffer(buffer1->data(), kHalfSize, false); | 3814 CacheTestFillBuffer(buffer1->data(), kHalfSize, false); |
| 3815 | 3815 |
| 3816 disk_cache::Entry* entry; | 3816 disk_cache::Entry* entry; |
| 3817 | 3817 |
| 3818 // Create entry, write data to third stream, truncate third stream back to | 3818 // Create entry, write data to third stream, truncate third stream back to |
| 3819 // empty, and close: third stream will not initially be omitted, since entry | 3819 // empty, and close: third stream will not initially be omitted, since entry |
| 3820 // creates the file when the first significant write comes in, and only | 3820 // creates the file when the first significant write comes in, and only |
| 3821 // removes it on open if it is empty. Reopen, ensure that the file is | 3821 // removes it on open if it is empty. Reopen, ensure that the file is |
| 3822 // deleted, and that there's no data in the third stream. | 3822 // deleted, and that there's no data in the third stream. |
| 3823 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3823 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3824 EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1, kHalfSize, true)); | 3824 EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1.get(), kHalfSize, true)); |
| 3825 EXPECT_EQ(0, WriteData(entry, 2, 0, buffer1, 0, true)); | 3825 EXPECT_EQ(0, WriteData(entry, 2, 0, buffer1.get(), 0, true)); |
| 3826 entry->Close(); | 3826 entry->Close(); |
| 3827 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); | 3827 EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key)); |
| 3828 | 3828 |
| 3829 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 3829 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
| 3830 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3830 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3831 EXPECT_EQ(0, ReadData(entry, 2, 0, buffer2, kSize)); | 3831 EXPECT_EQ(0, ReadData(entry, 2, 0, buffer2.get(), kSize)); |
| 3832 entry->Close(); | 3832 entry->Close(); |
| 3833 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3833 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3834 | 3834 |
| 3835 SyncDoomEntry(key); | 3835 SyncDoomEntry(key); |
| 3836 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3836 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3837 } | 3837 } |
| 3838 | 3838 |
| 3839 // Check that we don't accidentally create the third stream file once the entry | 3839 // Check that we don't accidentally create the third stream file once the entry |
| 3840 // has been doomed. | 3840 // has been doomed. |
| 3841 TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream5) { | 3841 TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream5) { |
| 3842 SetSimpleCacheMode(); | 3842 SetSimpleCacheMode(); |
| 3843 InitCache(); | 3843 InitCache(); |
| 3844 | 3844 |
| 3845 const int kHalfSize = 8; | 3845 const int kHalfSize = 8; |
| 3846 const int kSize = kHalfSize * 2; | 3846 const int kSize = kHalfSize * 2; |
| 3847 const char key[] = "key"; | 3847 const char key[] = "key"; |
| 3848 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3848 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
| 3849 CacheTestFillBuffer(buffer->data(), kHalfSize, false); | 3849 CacheTestFillBuffer(buffer->data(), kHalfSize, false); |
| 3850 | 3850 |
| 3851 disk_cache::Entry* entry; | 3851 disk_cache::Entry* entry; |
| 3852 | 3852 |
| 3853 // Create entry, doom entry, write data to third stream, and close: third | 3853 // Create entry, doom entry, write data to third stream, and close: third |
| 3854 // stream should not exist. (Note: We don't care if the write fails, just | 3854 // stream should not exist. (Note: We don't care if the write fails, just |
| 3855 // that it doesn't cause the file to be created on disk.) | 3855 // that it doesn't cause the file to be created on disk.) |
| 3856 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3856 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3857 entry->Doom(); | 3857 entry->Doom(); |
| 3858 WriteData(entry, 2, 0, buffer, kHalfSize, true); | 3858 WriteData(entry, 2, 0, buffer.get(), kHalfSize, true); |
| 3859 entry->Close(); | 3859 entry->Close(); |
| 3860 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); | 3860 EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key)); |
| 3861 } | 3861 } |
| 3862 | 3862 |
| 3863 // There could be a race between Doom and an optimistic write. | 3863 // There could be a race between Doom and an optimistic write. |
| 3864 TEST_F(DiskCacheEntryTest, SimpleCacheDoomOptimisticWritesRace) { | 3864 TEST_F(DiskCacheEntryTest, SimpleCacheDoomOptimisticWritesRace) { |
| 3865 // Test sequence: | 3865 // Test sequence: |
| 3866 // Create, first Write, second Write, Close. | 3866 // Create, first Write, second Write, Close. |
| 3867 // Open, Close. | 3867 // Open, Close. |
| 3868 SetSimpleCacheMode(); | 3868 SetSimpleCacheMode(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3966 disk_cache::Entry* entry; | 3966 disk_cache::Entry* entry; |
| 3967 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3967 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 3968 EXPECT_NE(null, entry); | 3968 EXPECT_NE(null, entry); |
| 3969 | 3969 |
| 3970 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3970 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
| 3971 CacheTestFillBuffer(buffer->data(), kSize, false); | 3971 CacheTestFillBuffer(buffer->data(), kSize, false); |
| 3972 net::TestCompletionCallback callback; | 3972 net::TestCompletionCallback callback; |
| 3973 int ret; | 3973 int ret; |
| 3974 | 3974 |
| 3975 // Verify initial conditions. | 3975 // Verify initial conditions. |
| 3976 ret = entry->ReadSparseData(0, buffer, kSize, callback.callback()); | 3976 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); |
| 3977 EXPECT_EQ(0, callback.GetResult(ret)); | 3977 EXPECT_EQ(0, callback.GetResult(ret)); |
| 3978 | 3978 |
| 3979 ret = entry->ReadSparseData(kSize, buffer, kSize, callback.callback()); | 3979 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); |
| 3980 EXPECT_EQ(0, callback.GetResult(ret)); | 3980 EXPECT_EQ(0, callback.GetResult(ret)); |
| 3981 | 3981 |
| 3982 // Write a range and make sure it reads back. | 3982 // Write a range and make sure it reads back. |
| 3983 ret = entry->WriteSparseData(0, buffer, kSize, callback.callback()); | 3983 ret = entry->WriteSparseData(0, buffer.get(), kSize, callback.callback()); |
| 3984 EXPECT_EQ(kSize, callback.GetResult(ret)); | 3984 EXPECT_EQ(kSize, callback.GetResult(ret)); |
| 3985 | 3985 |
| 3986 ret = entry->ReadSparseData(0, buffer, kSize, callback.callback()); | 3986 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); |
| 3987 EXPECT_EQ(kSize, callback.GetResult(ret)); | 3987 EXPECT_EQ(kSize, callback.GetResult(ret)); |
| 3988 | 3988 |
| 3989 // Write another range and make sure it reads back. | 3989 // Write another range and make sure it reads back. |
| 3990 ret = entry->WriteSparseData(kSize, buffer, kSize, callback.callback()); | 3990 ret = entry->WriteSparseData(kSize, buffer.get(), kSize, callback.callback()); |
| 3991 EXPECT_EQ(kSize, callback.GetResult(ret)); | 3991 EXPECT_EQ(kSize, callback.GetResult(ret)); |
| 3992 | 3992 |
| 3993 ret = entry->ReadSparseData(kSize, buffer, kSize, callback.callback()); | 3993 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); |
| 3994 EXPECT_EQ(kSize, callback.GetResult(ret)); | 3994 EXPECT_EQ(kSize, callback.GetResult(ret)); |
| 3995 | 3995 |
| 3996 // Make sure the first range was removed when the second was written. | 3996 // Make sure the first range was removed when the second was written. |
| 3997 ret = entry->ReadSparseData(0, buffer, kSize, callback.callback()); | 3997 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); |
| 3998 EXPECT_EQ(0, callback.GetResult(ret)); | 3998 EXPECT_EQ(0, callback.GetResult(ret)); |
| 3999 | 3999 |
| 4000 entry->Close(); | 4000 entry->Close(); |
| 4001 } | 4001 } |
| 4002 | 4002 |
| 4003 #endif // defined(OS_POSIX) | 4003 #endif // defined(OS_POSIX) |
| OLD | NEW |