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" |
(...skipping 2690 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); | 2701 base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN); |
2702 if (!file.IsValid()) | 2702 if (!file.IsValid()) |
2703 return false; | 2703 return false; |
2704 return file.SetLength(length); | 2704 return file.SetLength(length); |
2705 } | 2705 } |
2706 | 2706 |
2707 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) { | 2707 TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) { |
2708 SetSimpleCacheMode(); | 2708 SetSimpleCacheMode(); |
2709 InitCache(); | 2709 InitCache(); |
2710 | 2710 |
2711 const char key[] = "the first key"; | 2711 const std::string key("the first key"); |
2712 | 2712 |
2713 disk_cache::Entry* entry = NULL; | 2713 disk_cache::Entry* entry = NULL; |
2714 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 2714 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
2715 disk_cache::Entry* null = NULL; | 2715 disk_cache::Entry* null = NULL; |
2716 EXPECT_NE(null, entry); | 2716 EXPECT_NE(null, entry); |
2717 entry->Close(); | 2717 entry->Close(); |
2718 entry = NULL; | 2718 entry = NULL; |
2719 | 2719 |
2720 // Force the entry to flush to disk, so subsequent platform file operations | 2720 // Force the entry to flush to disk, so subsequent platform file operations |
2721 // succed. | 2721 // succed. |
2722 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 2722 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
2723 entry->Close(); | 2723 entry->Close(); |
2724 entry = NULL; | 2724 entry = NULL; |
2725 | 2725 |
2726 // Truncate the file such that the length isn't sufficient to have an EOF | 2726 // Truncate the file such that the length isn't sufficient to have an EOF |
2727 // record. | 2727 // record. |
2728 int kTruncationBytes = -static_cast<int>(sizeof(disk_cache::SimpleFileEOF)); | 2728 int kTruncationBytes = -static_cast<int>(sizeof(disk_cache::SimpleFileEOF)); |
2729 const base::FilePath entry_path = cache_path_.AppendASCII( | 2729 const base::FilePath entry_path = cache_path_.AppendASCII( |
2730 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 2730 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
2731 const int64_t invalid_size = | 2731 const int64_t invalid_size = disk_cache::simple_util::GetFileSizeFromDataSize( |
2732 disk_cache::simple_util::GetFileSizeFromKeyAndDataSize(key, | 2732 key.size(), kTruncationBytes); |
2733 kTruncationBytes); | |
2734 EXPECT_TRUE(TruncatePath(entry_path, invalid_size)); | 2733 EXPECT_TRUE(TruncatePath(entry_path, invalid_size)); |
2735 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); | 2734 EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry)); |
2736 DisableIntegrityCheck(); | 2735 DisableIntegrityCheck(); |
2737 } | 2736 } |
2738 | 2737 |
2739 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) { | 2738 TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) { |
2740 // Test sequence: | 2739 // Test sequence: |
2741 // Create, Write, Read, Close. | 2740 // Create, Write, Read, Close. |
2742 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. | 2741 SetCacheType(net::APP_CACHE); // APP_CACHE doesn't use optimistic operations. |
2743 SetSimpleCacheMode(); | 2742 SetSimpleCacheMode(); |
(...skipping 937 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3681 entry->Close(); | 3680 entry->Close(); |
3682 } | 3681 } |
3683 } | 3682 } |
3684 | 3683 |
3685 // Test that changing stream1 size does not affect stream0 (stream0 and stream1 | 3684 // Test that changing stream1 size does not affect stream0 (stream0 and stream1 |
3686 // are stored in the same file in Simple Cache). | 3685 // are stored in the same file in Simple Cache). |
3687 TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) { | 3686 TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) { |
3688 SetSimpleCacheMode(); | 3687 SetSimpleCacheMode(); |
3689 InitCache(); | 3688 InitCache(); |
3690 disk_cache::Entry* entry = NULL; | 3689 disk_cache::Entry* entry = NULL; |
3691 const char key[] = "the key"; | 3690 const std::string key("the key"); |
3692 const int kSize = 100; | 3691 const int kSize = 100; |
3693 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); | 3692 scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize)); |
3694 scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize)); | 3693 scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize)); |
3695 CacheTestFillBuffer(buffer->data(), kSize, false); | 3694 CacheTestFillBuffer(buffer->data(), kSize, false); |
3696 | 3695 |
3697 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 3696 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
3698 EXPECT_TRUE(entry); | 3697 EXPECT_TRUE(entry); |
3699 | 3698 |
3700 // Write something into stream0. | 3699 // Write something into stream0. |
3701 EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false)); | 3700 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( | 3718 base::FilePath entry_file0_path = cache_path_.AppendASCII( |
3720 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); | 3719 disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0)); |
3721 base::File entry_file0(entry_file0_path, | 3720 base::File entry_file0(entry_file0_path, |
3722 base::File::FLAG_READ | base::File::FLAG_OPEN); | 3721 base::File::FLAG_READ | base::File::FLAG_OPEN); |
3723 ASSERT_TRUE(entry_file0.IsValid()); | 3722 ASSERT_TRUE(entry_file0.IsValid()); |
3724 | 3723 |
3725 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0}; | 3724 int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0}; |
3726 int sparse_data_size = 0; | 3725 int sparse_data_size = 0; |
3727 disk_cache::SimpleEntryStat entry_stat( | 3726 disk_cache::SimpleEntryStat entry_stat( |
3728 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size); | 3727 base::Time::Now(), base::Time::Now(), data_size, sparse_data_size); |
3729 int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0); | 3728 int eof_offset = entry_stat.GetEOFOffsetInFile(key.size(), 0); |
3730 disk_cache::SimpleFileEOF eof_record; | 3729 disk_cache::SimpleFileEOF eof_record; |
3731 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), | 3730 ASSERT_EQ(static_cast<int>(sizeof(eof_record)), |
3732 entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record), | 3731 entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record), |
3733 sizeof(eof_record))); | 3732 sizeof(eof_record))); |
3734 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number); | 3733 EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number); |
3735 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) == | 3734 EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) == |
3736 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32); | 3735 disk_cache::SimpleFileEOF::FLAG_HAS_CRC32); |
3737 | 3736 |
3738 buffer_read = new net::IOBuffer(kSize); | 3737 buffer_read = new net::IOBuffer(kSize); |
3739 EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize)); | 3738 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)); | 4157 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
4159 | 4158 |
4160 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); | 4159 ret = entry->ReadSparseData(0, buffer.get(), kSize, callback.callback()); |
4161 EXPECT_EQ(0, callback.GetResult(ret)); | 4160 EXPECT_EQ(0, callback.GetResult(ret)); |
4162 | 4161 |
4163 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); | 4162 ret = entry->ReadSparseData(kSize, buffer.get(), kSize, callback.callback()); |
4164 EXPECT_EQ(kSize, callback.GetResult(ret)); | 4163 EXPECT_EQ(kSize, callback.GetResult(ret)); |
4165 | 4164 |
4166 entry->Close(); | 4165 entry->Close(); |
4167 } | 4166 } |
OLD | NEW |