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 <utility> | 5 #include <utility> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/run_loop.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
14 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
15 #include "net/base/completion_callback.h" | 16 #include "net/base/completion_callback.h" |
16 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
19 #include "net/disk_cache/blockfile/backend_impl.h" | 20 #include "net/disk_cache/blockfile/backend_impl.h" |
20 #include "net/disk_cache/blockfile/entry_impl.h" | 21 #include "net/disk_cache/blockfile/entry_impl.h" |
21 #include "net/disk_cache/disk_cache_test_base.h" | 22 #include "net/disk_cache/disk_cache_test_base.h" |
22 #include "net/disk_cache/disk_cache_test_util.h" | 23 #include "net/disk_cache/disk_cache_test_util.h" |
23 #include "net/disk_cache/memory/mem_entry_impl.h" | 24 #include "net/disk_cache/memory/mem_entry_impl.h" |
| 25 #include "net/disk_cache/simple/simple_backend_impl.h" |
24 #include "net/disk_cache/simple/simple_entry_format.h" | 26 #include "net/disk_cache/simple/simple_entry_format.h" |
25 #include "net/disk_cache/simple/simple_entry_impl.h" | 27 #include "net/disk_cache/simple/simple_entry_impl.h" |
26 #include "net/disk_cache/simple/simple_synchronous_entry.h" | 28 #include "net/disk_cache/simple/simple_synchronous_entry.h" |
27 #include "net/disk_cache/simple/simple_test_util.h" | 29 #include "net/disk_cache/simple/simple_test_util.h" |
28 #include "net/disk_cache/simple/simple_util.h" | 30 #include "net/disk_cache/simple/simple_util.h" |
29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
30 | 32 |
31 using base::Time; | 33 using base::Time; |
32 using disk_cache::ScopedEntryPtr; | 34 using disk_cache::ScopedEntryPtr; |
33 | 35 |
(...skipping 2667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2701 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN); | 2703 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN); |
2702 if (!file.IsValid()) | 2704 if (!file.IsValid()) |
2703 return false; | 2705 return false; |
2704 return file.SetLength(length); | 2706 return file.SetLength(length); |
2705 } | 2707 } |
2706 | 2708 |
2707 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) { | 2709 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) { |
2708 SetSimpleCacheMode(); | 2710 SetSimpleCacheMode(); |
2709 InitCache(); | 2711 InitCache(); |
2710 | 2712 |
2711 const char key[] = "the first key"; | 2713 const std::string key("the first key"); |
2712 | 2714 |
2713 disk_cache::Entry* entry = NULL; | 2715 disk_cache::Entry* entry = NULL; |
2714 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 2716 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
2715 disk_cache::Entry* null = NULL; | 2717 disk_cache::Entry* null = NULL; |
2716 EXPECT_NE(null, entry); | 2718 EXPECT_NE(null, entry); |
2717 entry->Close(); | 2719 entry->Close(); |
2718 entry = NULL; | 2720 entry = NULL; |
2719 | 2721 |
2720 // Force the entry to flush to disk, so subsequent platform file operations | 2722 // Force the entry to flush to disk, so subsequent platform file operations |
2721 // succed. | 2723 // succed. |
2722 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 2724 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
2723 entry->Close(); | 2725 entry->Close(); |
2724 entry = NULL; | 2726 entry = NULL; |
2725 | 2727 |
2726 // Truncate the file such that the length isn't sufficient to have an EOF | 2728 // Truncate the file such that the length isn't sufficient to have an EOF |
2727 // record. | 2729 // record. |
2728 int kTruncationBytes = -static_cast<int>(sizeof(disk_cache::SimpleFileEOF)); | 2730 int kTruncationBytes = -static_cast<int>(sizeof(disk_cache::SimpleFileEOF)); |
2729 const base::FilePath entry_path = cache_path_.AppendASCII( | 2731 const base::FilePath entry_path = cache_path_.AppendASCII( |
2730 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 2732 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
2731 const int64_t invalid_size = | 2733 const int64_t invalid_size = disk_cache::simple_util::GetFileSizeFromDataSize( |
2732 disk_cache::simple_util::GetFileSizeFromKeyAndDataSize(key, | 2734 key.size(), kTruncationBytes); |
2733 kTruncationBytes); | |
2734 EXPECT_TRUE(TruncatePath(entry_path, invalid_size)); | 2735 EXPECT_TRUE(TruncatePath(entry_path, invalid_size)); |
2735 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); | 2736 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); |
2736 DisableIntegrityCheck(); | 2737 DisableIntegrityCheck(); |
2737 } | 2738 } |
2738 | 2739 |
2739 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) { | 2740 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) { |
2740 // Test sequence: | 2741 // Test sequence: |
2741 // Create, Write, Read, Close. | 2742 // Create, Write, Read, Close. |
2742 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. | 2743 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. |
2743 SetSimpleCacheMode(); | 2744 SetSimpleCacheMode(); |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3681 entry->Close(); | 3682 entry->Close(); |
3682 } | 3683 } |
3683 } | 3684 } |
3684 | 3685 |
3685 // Test that changing stream1 size does not affect stream0 (stream0 and stream1 | 3686 // Test that changing stream1 size does not affect stream0 (stream0 and stream1 |
3686 // are stored in the same file in Simple Cache). | 3687 // are stored in the same file in Simple Cache). |
3687 TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) { | 3688 TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) { |
3688 SetSimpleCacheMode(); | 3689 SetSimpleCacheMode(); |
3689 InitCache(); | 3690 InitCache(); |
3690 disk_cache::Entry* entry = NULL; | 3691 disk_cache::Entry* entry = NULL; |
3691 const char key[] = "the key"; | 3692 const std::string key("the key"); |
3692 const int kSize = 100; | 3693 const int kSize = 100; |
3693 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3694 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3694 scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize)); | 3695 scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize)); |
3695 CacheTestFillBuffer(buffer->data(), kSize, false); | 3696 CacheTestFillBuffer(buffer->data(), kSize, false); |
3696 | 3697 |
3697 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3698 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
3698 EXPECT_TRUE(entry); | 3699 EXPECT_TRUE(entry); |
3699 | 3700 |
3700 // Write something into stream0. | 3701 // Write something into stream0. |
3701 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 3702 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); |
(...skipping 17 matching lines...) Expand all Loading... |
3719 base::FilePath entry_file0_path = cache_path_.AppendASCII( | 3720 base::FilePath entry_file0_path = cache_path_.AppendASCII( |
3720 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 3721 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
3721 base::File entry_file0(entry_file0_path, | 3722 base::File entry_file0(entry_file0_path, |
3722 base::File::FLAG_READ | base::File::FLAG_OPEN); | 3723 base::File::FLAG_READ | base::File::FLAG_OPEN); |
3723 ASSERT_TRUE(entry_file0.IsValid()); | 3724 ASSERT_TRUE(entry_file0.IsValid()); |
3724 | 3725 |
3725 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0}; | 3726 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0}; |
3726 int sparse_data_size = 0; | 3727 int sparse_data_size = 0; |
3727 disk_cache::SimpleEntryStat entry_stat( | 3728 disk_cache::SimpleEntryStat entry_stat( |
3728 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size); | 3729 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size); |
3729 int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0); | 3730 int eof_offset = entry_stat.GetEOFOffsetInFile(key.size(), 0); |
3730 disk_cache::SimpleFileEOF eof_record; | 3731 disk_cache::SimpleFileEOF eof_record; |
3731 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), | 3732 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), |
3732 entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record), | 3733 entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record), |
3733 sizeof(eof_record))); | 3734 sizeof(eof_record))); |
3734 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number); | 3735 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number); |
3735 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) == | 3736 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) == |
3736 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32); | 3737 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32); |
3737 | 3738 |
3738 buffer_read = new net::IOBuffer(kSize); | 3739 buffer_read = new net::IOBuffer(kSize); |
3739 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize)); | 3740 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize)); |
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4158 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 4159 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
4159 | 4160 |
4160 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); | 4161 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); |
4161 EXPECT_EQ(0, callback.GetResult(ret)); | 4162 EXPECT_EQ(0, callback.GetResult(ret)); |
4162 | 4163 |
4163 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); | 4164 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); |
4164 EXPECT_EQ(kSize, callback.GetResult(ret)); | 4165 EXPECT_EQ(kSize, callback.GetResult(ret)); |
4165 | 4166 |
4166 entry->Close(); | 4167 entry->Close(); |
4167 } | 4168 } |
| 4169 |
| 4170 TEST_F(DiskCacheEntryTest, SimpleCacheReadWithoutKeySHA256) { |
| 4171 // This test runs as APP_CACHE to make operations more synchronous. |
| 4172 SetCacheType(net::APP_CACHE); |
| 4173 SetSimpleCacheMode(); |
| 4174 InitCache(); |
| 4175 disk_cache::Entry* entry; |
| 4176 std::string key("a key"); |
| 4177 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 4178 |
| 4179 const std::string stream_0_data = "data for stream zero"; |
| 4180 scoped_refptr<net::IOBuffer> stream_0_iobuffer( |
| 4181 new net::StringIOBuffer(stream_0_data)); |
| 4182 EXPECT_EQ(static_cast<int>(stream_0_data.size()), |
| 4183 WriteData(entry, 0, 0, stream_0_iobuffer.get(), |
| 4184 stream_0_data.size(), false)); |
| 4185 const std::string stream_1_data = "FOR STREAM ONE, QUITE DIFFERENT THINGS"; |
| 4186 scoped_refptr<net::IOBuffer> stream_1_iobuffer( |
| 4187 new net::StringIOBuffer(stream_1_data)); |
| 4188 EXPECT_EQ(static_cast<int>(stream_1_data.size()), |
| 4189 WriteData(entry, 1, 0, stream_1_iobuffer.get(), |
| 4190 stream_1_data.size(), false)); |
| 4191 entry->Close(); |
| 4192 |
| 4193 base::RunLoop().RunUntilIdle(); |
| 4194 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 4195 base::RunLoop().RunUntilIdle(); |
| 4196 |
| 4197 EXPECT_TRUE( |
| 4198 disk_cache::simple_util::RemoveKeySHA256FromEntry(key, cache_path_)); |
| 4199 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
| 4200 ScopedEntryPtr entry_closer(entry); |
| 4201 |
| 4202 EXPECT_EQ(static_cast<int>(stream_0_data.size()), entry->GetDataSize(0)); |
| 4203 scoped_refptr<net::IOBuffer> check_stream_0_data( |
| 4204 new net::IOBuffer(stream_0_data.size())); |
| 4205 EXPECT_EQ( |
| 4206 static_cast<int>(stream_0_data.size()), |
| 4207 ReadData(entry, 0, 0, check_stream_0_data.get(), stream_0_data.size())); |
| 4208 EXPECT_EQ(0, stream_0_data.compare(0, std::string::npos, |
| 4209 check_stream_0_data->data(), |
| 4210 stream_0_data.size())); |
| 4211 |
| 4212 EXPECT_EQ(static_cast<int>(stream_1_data.size()), entry->GetDataSize(1)); |
| 4213 scoped_refptr<net::IOBuffer> check_stream_1_data( |
| 4214 new net::IOBuffer(stream_1_data.size())); |
| 4215 EXPECT_EQ( |
| 4216 static_cast<int>(stream_1_data.size()), |
| 4217 ReadData(entry, 1, 0, check_stream_1_data.get(), stream_1_data.size())); |
| 4218 EXPECT_EQ(0, stream_1_data.compare(0, std::string::npos, |
| 4219 check_stream_1_data->data(), |
| 4220 stream_1_data.size())); |
| 4221 } |
| 4222 |
| 4223 TEST_F(DiskCacheEntryTest, SimpleCacheReadCorruptKeySHA256) { |
| 4224 // This test runs as APP_CACHE to make operations more synchronous. |
| 4225 SetCacheType(net::APP_CACHE); |
| 4226 SetSimpleCacheMode(); |
| 4227 InitCache(); |
| 4228 disk_cache::Entry* entry; |
| 4229 std::string key("a key"); |
| 4230 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 4231 entry->Close(); |
| 4232 |
| 4233 base::RunLoop().RunUntilIdle(); |
| 4234 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting(); |
| 4235 base::RunLoop().RunUntilIdle(); |
| 4236 |
| 4237 EXPECT_TRUE( |
| 4238 disk_cache::simple_util::CorruptKeySHA256FromEntry(key, cache_path_)); |
| 4239 EXPECT_NE(net::OK, OpenEntry(key, &entry)); |
| 4240 } |
OLD | NEW |