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

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

Issue 1977863003: SimpleCache: open with fewer seeks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@open-speeder-macbetter
Patch Set: add unit tests Created 4 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
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 <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
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
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
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
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 entry->Close();
4179
4180 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
4181 base::RunLoop().RunUntilIdle();
4182
4183 EXPECT_TRUE(
4184 disk_cache::simple_util::RemoveKeySHA256FromEntry(key, cache_path_));
4185 EXPECT_EQ(net::OK, OpenEntry(key, &entry));
Randy Smith (Not in Mondays) 2016/05/19 19:38:50 Confirm contents ok? (Probably need to write some
gavinp 2016/05/20 01:42:32 Done.
4186 }
4187
4188 TEST_F(DiskCacheEntryTest, SimpleCacheReadCorruptKeySHA256) {
4189 // This test runs as APP_CACHE to make operations more synchronous.
4190 SetCacheType(net::APP_CACHE);
4191 SetSimpleCacheMode();
4192 InitCache();
4193 disk_cache::Entry* entry;
4194 std::string key("a key");
4195 ASSERT_EQ(net::OK, CreateEntry(key, &entry));
4196 entry->Close();
4197
4198 disk_cache::SimpleBackendImpl::FlushWorkerPoolForTesting();
4199 base::RunLoop().RunUntilIdle();
4200
4201 EXPECT_TRUE(
4202 disk_cache::simple_util::CorruptKeySHA256FromEntry(key, cache_path_));
4203 EXPECT_NE(net::OK, OpenEntry(key, &entry));
4204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698