Commit 0fe1d248 authored by Konstantin Ganenko's avatar Konstantin Ganenko Committed by Commit Bot

Make simple cache stream 2 eof validation on entry open.

Previously simple cache 2 stream's EOF signature was checked at end of the read, not when the entry was opened.

R=morlovich@chromium.org

Change-Id: Ic7672fbaa7bcec19337a9004da8c966b8a0029c7
Reviewed-on: https://chromium-review.googlesource.com/c/1477830Reviewed-by: default avatarMaks Orlovich <morlovich@chromium.org>
Commit-Queue: Konstantin Ganenko <ganenkokb@yandex-team.ru>
Cr-Commit-Position: refs/heads/master@{#634642}
parent 4c800eb7
......@@ -83,6 +83,12 @@ class DiskCacheEntryTest : public DiskCacheTestWithCache {
bool SimpleCacheMakeBadChecksumEntry(const std::string& key, int data_size);
bool SimpleCacheThirdStreamFileExists(const char* key);
void SyncDoomEntry(const char* key);
void CreateEntryWithHeaderBodyAndSideData(const std::string& key,
int data_size);
void TruncateFileFromEnd(int file_index,
const std::string& key,
int data_size,
int truncate_size);
void UseAfterBackendDestruction();
void LastUsedTimePersists();
};
......@@ -4258,6 +4264,37 @@ void DiskCacheEntryTest::SyncDoomEntry(const char* key) {
callback.WaitForResult();
}
void DiskCacheEntryTest::CreateEntryWithHeaderBodyAndSideData(
const std::string& key,
int data_size) {
// Use one buffer for simplicity.
scoped_refptr<net::IOBuffer> buffer =
base::MakeRefCounted<net::IOBuffer>(data_size);
CacheTestFillBuffer(buffer->data(), data_size, false);
disk_cache::Entry* entry = nullptr;
ASSERT_THAT(CreateEntry(key, &entry), IsOk());
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(data_size, WriteData(entry, i, /* offset */ 0, buffer.get(),
data_size, false));
}
entry->Close();
}
void DiskCacheEntryTest::TruncateFileFromEnd(int file_index,
const std::string& key,
int data_size,
int truncate_size) {
// Remove last eof bytes from cache file.
ASSERT_GT(data_size, truncate_size);
const int64_t new_size =
disk_cache::simple_util::GetFileSizeFromDataSize(key.size(), data_size) -
truncate_size;
const base::FilePath entry_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, file_index));
EXPECT_TRUE(TruncatePath(entry_path, new_size));
}
void DiskCacheEntryTest::UseAfterBackendDestruction() {
disk_cache::Entry* entry = NULL;
ASSERT_THAT(CreateEntry("the first key", &entry), IsOk());
......@@ -4634,6 +4671,40 @@ TEST_F(DiskCacheEntryTest, SimpleCacheTruncateLargeSparseFile) {
entry->Close();
}
TEST_F(DiskCacheEntryTest, SimpleCacheNoBodyEOF) {
SetSimpleCacheMode();
InitCache();
const std::string key("the first key");
const int kSize = 1024;
CreateEntryWithHeaderBodyAndSideData(key, kSize);
disk_cache::Entry* entry = nullptr;
ASSERT_THAT(OpenEntry(key, &entry), IsOk());
entry->Close();
TruncateFileFromEnd(0 /*header and body file index*/, key, kSize,
static_cast<int>(sizeof(disk_cache::SimpleFileEOF)));
EXPECT_THAT(OpenEntry(key, &entry), IsError(net::ERR_FAILED));
}
TEST_F(DiskCacheEntryTest, SimpleCacheNoSideDataEOF) {
SetSimpleCacheMode();
InitCache();
const std::string key("the first key");
const int kSize = 1024;
CreateEntryWithHeaderBodyAndSideData(key, kSize);
disk_cache::Entry* entry = nullptr;
ASSERT_THAT(OpenEntry(key, &entry), IsOk());
entry->Close();
TruncateFileFromEnd(1 /*side data file_index*/, key, kSize,
static_cast<int>(sizeof(disk_cache::SimpleFileEOF)));
EXPECT_THAT(OpenEntry(key, &entry), IsError(net::ERR_FAILED));
}
TEST_F(DiskCacheEntryTest, SimpleCacheReadWithoutKeySHA256) {
// This test runs as APP_CACHE to make operations more synchronous.
SetCacheType(net::APP_CACHE);
......
......@@ -1457,9 +1457,21 @@ int SimpleSynchronousEntry::InitializeForOpen(
out_entry_stat->set_data_size(
2,
GetDataSizeFromFileSize(key_.size(), out_entry_stat->data_size(2)));
if (out_entry_stat->data_size(2) < 0) {
const int32_t data_size_2 = out_entry_stat->data_size(2);
if (data_size_2 < 0) {
DLOG(WARNING) << "Stream 2 file is too small.";
return net::ERR_FAILED;
} else if (data_size_2 > 0) {
// Validate non empty stream 2.
SimpleFileEOF eof_record;
SimpleFileTracker::FileHandle file =
file_tracker_->Acquire(this, SubFileForFileIndex(i));
int file_offset =
out_entry_stat->GetEOFOffsetInFile(key_.size(), 2 /*stream index*/);
int ret_value_stream_2 =
GetEOFRecordData(file.get(), nullptr, i, file_offset, &eof_record);
if (ret_value_stream_2 != net::OK)
return ret_value_stream_2;
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment