This source file includes following definitions.
- InternalSyncIOBackground
- InternalSyncIO
- TEST_F
- TEST_F
- InternalAsyncIO
- TEST_F
- TEST_F
- ExternalSyncIOBackground
- ExternalSyncIO
- TEST_F
- TEST_F
- TEST_F
- ExternalAsyncIO
- TEST_F
- TEST_F
- TEST_F
- ReleaseBuffer
- TEST_F
- TEST_F
- StreamAccess
- TEST_F
- TEST_F
- GetKey
- TEST_F
- TEST_F
- GetTimes
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- GrowData
- TEST_F
- TEST_F
- TEST_F
- TruncateData
- TEST_F
- TEST_F
- TEST_F
- ZeroLengthIO
- TEST_F
- TEST_F
- TEST_F
- Buffering
- TEST_F
- TEST_F
- SizeAtCreate
- TEST_F
- TEST_F
- SizeChanges
- TEST_F
- TEST_F
- ReuseEntry
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- InvalidData
- TEST_F
- TEST_F
- TEST_F
- ReadWriteDestroyBuffer
- TEST_F
- DoomNormalEntry
- TEST_F
- TEST_F
- DoomEntryNextToOpenEntry
- TEST_F
- TEST_F
- TEST_F
- DoomedEntry
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- VerifySparseIO
- VerifyContentSparseIO
- BasicSparseIO
- TEST_F
- TEST_F
- HugeSparseIO
- TEST_F
- TEST_F
- GetAvailableRange
- TEST_F
- TEST_F
- CouldBeSparse
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- UpdateSparseEntry
- TEST_F
- TEST_F
- DoomSparseEntry
- TEST_F
- TEST_F
- SetResult
- TEST_F
- PartialSparseEntry
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SimpleCacheMakeBadChecksumEntry
- TEST_F
- TEST_F
- TruncatePath
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- SimpleCacheThirdStreamFileExists
- SyncDoomEntry
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_util.h"
#include "base/files/file.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "base/timer/timer.h"
#include "net/base/completion_callback.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
#include "net/disk_cache/blockfile/backend_impl.h"
#include "net/disk_cache/blockfile/entry_impl.h"
#include "net/disk_cache/disk_cache_test_base.h"
#include "net/disk_cache/disk_cache_test_util.h"
#include "net/disk_cache/memory/mem_entry_impl.h"
#include "net/disk_cache/simple/simple_entry_format.h"
#include "net/disk_cache/simple/simple_entry_impl.h"
#include "net/disk_cache/simple/simple_synchronous_entry.h"
#include "net/disk_cache/simple/simple_test_util.h"
#include "net/disk_cache/simple/simple_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::Time;
using disk_cache::ScopedEntryPtr;
class DiskCacheEntryTest : public DiskCacheTestWithCache {
public:
void InternalSyncIOBackground(disk_cache::Entry* entry);
void ExternalSyncIOBackground(disk_cache::Entry* entry);
protected:
void InternalSyncIO();
void InternalAsyncIO();
void ExternalSyncIO();
void ExternalAsyncIO();
void ReleaseBuffer(int stream_index);
void StreamAccess();
void GetKey();
void GetTimes(int stream_index);
void GrowData(int stream_index);
void TruncateData(int stream_index);
void ZeroLengthIO(int stream_index);
void Buffering();
void SizeAtCreate();
void SizeChanges(int stream_index);
void ReuseEntry(int size, int stream_index);
void InvalidData(int stream_index);
void ReadWriteDestroyBuffer(int stream_index);
void DoomNormalEntry();
void DoomEntryNextToOpenEntry();
void DoomedEntry(int stream_index);
void BasicSparseIO();
void HugeSparseIO();
void GetAvailableRange();
void CouldBeSparse();
void UpdateSparseEntry();
void DoomSparseEntry();
void PartialSparseEntry();
bool SimpleCacheMakeBadChecksumEntry(const std::string& key, int* data_size);
bool SimpleCacheThirdStreamFileExists(const char* key);
void SyncDoomEntry(const char* key);
};
void DiskCacheEntryTest::InternalSyncIOBackground(disk_cache::Entry* entry) {
const int kSize1 = 10;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
EXPECT_EQ(
0,
entry->ReadData(0, 0, buffer1.get(), kSize1, net::CompletionCallback()));
base::strlcpy(buffer1->data(), "the data", kSize1);
EXPECT_EQ(10,
entry->WriteData(
0, 0, buffer1.get(), kSize1, net::CompletionCallback(), false));
memset(buffer1->data(), 0, kSize1);
EXPECT_EQ(
10,
entry->ReadData(0, 0, buffer1.get(), kSize1, net::CompletionCallback()));
EXPECT_STREQ("the data", buffer1->data());
const int kSize2 = 5000;
const int kSize3 = 10000;
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
memset(buffer3->data(), 0, kSize3);
CacheTestFillBuffer(buffer2->data(), kSize2, false);
base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
EXPECT_EQ(
5000,
entry->WriteData(
1, 1500, buffer2.get(), kSize2, net::CompletionCallback(), false));
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(4989,
entry->ReadData(
1, 1511, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_STREQ("big data goes here", buffer2->data());
EXPECT_EQ(
5000,
entry->ReadData(1, 0, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
EXPECT_EQ(1500,
entry->ReadData(
1, 5000, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(0,
entry->ReadData(
1, 6500, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(
6500,
entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback()));
EXPECT_EQ(8192,
entry->WriteData(
1, 0, buffer3.get(), 8192, net::CompletionCallback(), false));
EXPECT_EQ(
8192,
entry->ReadData(1, 0, buffer3.get(), kSize3, net::CompletionCallback()));
EXPECT_EQ(8192, entry->GetDataSize(1));
EXPECT_EQ(0, entry->WriteData(
0, 0, NULL, 0, net::CompletionCallback(), true));
EXPECT_EQ(0, entry->WriteData(
1, 0, NULL, 0, net::CompletionCallback(), true));
}
void DiskCacheEntryTest::InternalSyncIO() {
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
ASSERT_TRUE(NULL != entry);
RunTaskForTest(base::Bind(&DiskCacheEntryTest::InternalSyncIOBackground,
base::Unretained(this),
entry));
entry->Doom();
entry->Close();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, InternalSyncIO) {
InitCache();
InternalSyncIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyInternalSyncIO) {
SetMemoryOnlyMode();
InitCache();
InternalSyncIO();
}
void DiskCacheEntryTest::InternalAsyncIO() {
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
ASSERT_TRUE(NULL != entry);
EXPECT_EQ(0, WriteData(entry, 0, 15 * 1024, NULL, 0, false));
EXPECT_EQ(0, WriteData(entry, 1, 15 * 1024, NULL, 0, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry("the first key", &entry));
MessageLoopHelper helper;
CallbackTest callback1(&helper, false);
CallbackTest callback2(&helper, false);
CallbackTest callback3(&helper, false);
CallbackTest callback4(&helper, false);
CallbackTest callback5(&helper, false);
CallbackTest callback6(&helper, false);
CallbackTest callback7(&helper, false);
CallbackTest callback8(&helper, false);
CallbackTest callback9(&helper, false);
CallbackTest callback10(&helper, false);
CallbackTest callback11(&helper, false);
CallbackTest callback12(&helper, false);
CallbackTest callback13(&helper, false);
const int kSize1 = 10;
const int kSize2 = 5000;
const int kSize3 = 10000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kSize2, false);
CacheTestFillBuffer(buffer3->data(), kSize3, false);
EXPECT_EQ(0,
entry->ReadData(
0,
15 * 1024,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback1))));
base::strlcpy(buffer1->data(), "the data", kSize1);
int expected = 0;
int ret = entry->WriteData(
0,
0,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback2)),
false);
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
memset(buffer2->data(), 0, kSize2);
ret = entry->ReadData(
0,
0,
buffer2.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback3)));
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_STREQ("the data", buffer2->data());
base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
ret = entry->WriteData(
1,
1500,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback4)),
true);
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
memset(buffer3->data(), 0, kSize3);
ret = entry->ReadData(
1,
1511,
buffer3.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback5)));
EXPECT_TRUE(4989 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_STREQ("big data goes here", buffer3->data());
ret = entry->ReadData(
1,
0,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback6)));
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
memset(buffer3->data(), 0, kSize3);
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 1500));
ret = entry->ReadData(
1,
5000,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback7)));
EXPECT_TRUE(1500 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
ret = entry->ReadData(
1,
0,
buffer3.get(),
kSize3,
base::Bind(&CallbackTest::Run, base::Unretained(&callback9)));
EXPECT_TRUE(6500 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
ret = entry->WriteData(
1,
0,
buffer3.get(),
8192,
base::Bind(&CallbackTest::Run, base::Unretained(&callback10)),
true);
EXPECT_TRUE(8192 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
ret = entry->ReadData(
1,
0,
buffer3.get(),
kSize3,
base::Bind(&CallbackTest::Run, base::Unretained(&callback11)));
EXPECT_TRUE(8192 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_EQ(8192, entry->GetDataSize(1));
ret = entry->ReadData(
0,
0,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback12)));
EXPECT_TRUE(10 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
ret = entry->ReadData(
1,
0,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback13)));
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_FALSE(helper.callback_reused_error());
entry->Doom();
entry->Close();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, InternalAsyncIO) {
InitCache();
InternalAsyncIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyInternalAsyncIO) {
SetMemoryOnlyMode();
InitCache();
InternalAsyncIO();
}
void DiskCacheEntryTest::ExternalSyncIOBackground(disk_cache::Entry* entry) {
const int kSize1 = 17000;
const int kSize2 = 25000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kSize2, false);
base::strlcpy(buffer1->data(), "the data", kSize1);
EXPECT_EQ(17000,
entry->WriteData(
0, 0, buffer1.get(), kSize1, net::CompletionCallback(), false));
memset(buffer1->data(), 0, kSize1);
EXPECT_EQ(
17000,
entry->ReadData(0, 0, buffer1.get(), kSize1, net::CompletionCallback()));
EXPECT_STREQ("the data", buffer1->data());
base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
EXPECT_EQ(
25000,
entry->WriteData(
1, 10000, buffer2.get(), kSize2, net::CompletionCallback(), false));
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(24989,
entry->ReadData(
1, 10011, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_STREQ("big data goes here", buffer2->data());
EXPECT_EQ(
25000,
entry->ReadData(1, 0, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(5000,
entry->ReadData(
1, 30000, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(0,
entry->ReadData(
1, 35000, buffer2.get(), kSize2, net::CompletionCallback()));
EXPECT_EQ(
17000,
entry->ReadData(1, 0, buffer1.get(), kSize1, net::CompletionCallback()));
EXPECT_EQ(
17000,
entry->WriteData(
1, 20000, buffer1.get(), kSize1, net::CompletionCallback(), false));
EXPECT_EQ(37000, entry->GetDataSize(1));
EXPECT_EQ(0, entry->WriteData(
0, 0, NULL, 0, net::CompletionCallback(), true));
EXPECT_EQ(0, entry->WriteData(
1, 0, NULL, 0, net::CompletionCallback(), true));
}
void DiskCacheEntryTest::ExternalSyncIO() {
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
RunTaskForTest(base::Bind(&DiskCacheEntryTest::ExternalSyncIOBackground,
base::Unretained(this),
entry));
entry->Doom();
entry->Close();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, ExternalSyncIO) {
InitCache();
ExternalSyncIO();
}
TEST_F(DiskCacheEntryTest, ExternalSyncIONoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
ExternalSyncIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyExternalSyncIO) {
SetMemoryOnlyMode();
InitCache();
ExternalSyncIO();
}
void DiskCacheEntryTest::ExternalAsyncIO() {
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
int expected = 0;
MessageLoopHelper helper;
CallbackTest callback1(&helper, false);
CallbackTest callback2(&helper, false);
CallbackTest callback3(&helper, false);
CallbackTest callback4(&helper, false);
CallbackTest callback5(&helper, false);
CallbackTest callback6(&helper, false);
CallbackTest callback7(&helper, false);
CallbackTest callback8(&helper, false);
CallbackTest callback9(&helper, false);
const int kSize1 = 17000;
const int kSize2 = 25000;
const int kSize3 = 25000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kSize2, false);
CacheTestFillBuffer(buffer3->data(), kSize3, false);
base::strlcpy(buffer1->data(), "the data", kSize1);
int ret = entry->WriteData(
0,
0,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback1)),
false);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
memset(buffer2->data(), 0, kSize1);
ret = entry->ReadData(
0,
0,
buffer2.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback2)));
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_STREQ("the data", buffer2->data());
base::strlcpy(buffer2->data(), "The really big data goes here", kSize2);
ret = entry->WriteData(
1,
10000,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback3)),
false);
EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
memset(buffer3->data(), 0, kSize3);
ret = entry->ReadData(
1,
10011,
buffer3.get(),
kSize3,
base::Bind(&CallbackTest::Run, base::Unretained(&callback4)));
EXPECT_TRUE(24989 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_STREQ("big data goes here", buffer3->data());
ret = entry->ReadData(
1,
0,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback5)));
EXPECT_TRUE(25000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
memset(buffer3->data(), 0, kSize3);
EXPECT_EQ(0, memcmp(buffer2->data(), buffer3->data(), 10000));
ret = entry->ReadData(
1,
30000,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback6)));
EXPECT_TRUE(5000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_EQ(0,
entry->ReadData(
1,
35000,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback7))));
ret = entry->ReadData(
1,
0,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback8)));
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
ret = entry->WriteData(
1,
20000,
buffer3.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback9)),
false);
EXPECT_TRUE(17000 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(37000, entry->GetDataSize(1));
EXPECT_FALSE(helper.callback_reused_error());
entry->Doom();
entry->Close();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, ExternalAsyncIO) {
InitCache();
ExternalAsyncIO();
}
TEST_F(DiskCacheEntryTest, ExternalAsyncIONoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
ExternalAsyncIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyExternalAsyncIO) {
SetMemoryOnlyMode();
InitCache();
ExternalAsyncIO();
}
void DiskCacheEntryTest::ReleaseBuffer(int stream_index) {
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
ASSERT_TRUE(NULL != entry);
const int kBufferSize = 1024;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kBufferSize));
CacheTestFillBuffer(buffer->data(), kBufferSize, false);
net::ReleaseBufferCompletionCallback cb(buffer.get());
int rv = entry->WriteData(
stream_index, 0, buffer.get(), kBufferSize, cb.callback(), false);
EXPECT_EQ(kBufferSize, cb.GetResult(rv));
entry->Close();
}
TEST_F(DiskCacheEntryTest, ReleaseBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
ReleaseBuffer(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyReleaseBuffer) {
SetMemoryOnlyMode();
InitCache();
ReleaseBuffer(0);
}
void DiskCacheEntryTest::StreamAccess() {
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry("the first key", &entry));
ASSERT_TRUE(NULL != entry);
const int kBufferSize = 1024;
const int kNumStreams = 3;
scoped_refptr<net::IOBuffer> reference_buffers[kNumStreams];
for (int i = 0; i < kNumStreams; i++) {
reference_buffers[i] = new net::IOBuffer(kBufferSize);
CacheTestFillBuffer(reference_buffers[i]->data(), kBufferSize, false);
}
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kBufferSize));
for (int i = 0; i < kNumStreams; i++) {
EXPECT_EQ(
kBufferSize,
WriteData(entry, i, 0, reference_buffers[i].get(), kBufferSize, false));
memset(buffer1->data(), 0, kBufferSize);
EXPECT_EQ(kBufferSize, ReadData(entry, i, 0, buffer1.get(), kBufferSize));
EXPECT_EQ(
0, memcmp(reference_buffers[i]->data(), buffer1->data(), kBufferSize));
}
EXPECT_EQ(net::ERR_INVALID_ARGUMENT,
ReadData(entry, kNumStreams, 0, buffer1.get(), kBufferSize));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry("the first key", &entry));
ASSERT_TRUE(NULL != entry);
const int kReadBufferSize = 600;
const int kFinalReadSize = kBufferSize - kReadBufferSize;
COMPILE_ASSERT(kFinalReadSize < kReadBufferSize, should_be_exactly_two_reads);
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kReadBufferSize));
for (int i = 0; i < kNumStreams; i++) {
memset(buffer2->data(), 0, kReadBufferSize);
EXPECT_EQ(kReadBufferSize,
ReadData(entry, i, 0, buffer2.get(), kReadBufferSize));
EXPECT_EQ(
0,
memcmp(reference_buffers[i]->data(), buffer2->data(), kReadBufferSize));
memset(buffer2->data(), 0, kReadBufferSize);
EXPECT_EQ(
kFinalReadSize,
ReadData(entry, i, kReadBufferSize, buffer2.get(), kReadBufferSize));
EXPECT_EQ(0,
memcmp(reference_buffers[i]->data() + kReadBufferSize,
buffer2->data(),
kFinalReadSize));
}
entry->Close();
}
TEST_F(DiskCacheEntryTest, StreamAccess) {
InitCache();
StreamAccess();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyStreamAccess) {
SetMemoryOnlyMode();
InitCache();
StreamAccess();
}
void DiskCacheEntryTest::GetKey() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(key, entry->GetKey()) << "short key";
entry->Close();
int seed = static_cast<int>(Time::Now().ToInternalValue());
srand(seed);
char key_buffer[20000];
CacheTestFillBuffer(key_buffer, 3000, true);
key_buffer[1000] = '\0';
key = key_buffer;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(key == entry->GetKey()) << "1000 bytes key";
entry->Close();
key_buffer[1000] = 'p';
key_buffer[3000] = '\0';
key = key_buffer;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(key == entry->GetKey()) << "medium size key";
entry->Close();
CacheTestFillBuffer(key_buffer, sizeof(key_buffer), true);
key_buffer[19999] = '\0';
key = key_buffer;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(key == entry->GetKey()) << "long key";
entry->Close();
CacheTestFillBuffer(key_buffer, 0x4000, true);
key_buffer[0x4000] = '\0';
key = key_buffer;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(key == entry->GetKey()) << "16KB key";
entry->Close();
}
TEST_F(DiskCacheEntryTest, GetKey) {
InitCache();
GetKey();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyGetKey) {
SetMemoryOnlyMode();
InitCache();
GetKey();
}
void DiskCacheEntryTest::GetTimes(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
Time t1 = Time::Now();
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(entry->GetLastModified() >= t1);
EXPECT_TRUE(entry->GetLastModified() == entry->GetLastUsed());
AddDelay();
Time t2 = Time::Now();
EXPECT_TRUE(t2 > t1);
EXPECT_EQ(0, WriteData(entry, stream_index, 200, NULL, 0, false));
if (type_ == net::APP_CACHE) {
EXPECT_TRUE(entry->GetLastModified() < t2);
} else {
EXPECT_TRUE(entry->GetLastModified() >= t2);
}
EXPECT_TRUE(entry->GetLastModified() == entry->GetLastUsed());
AddDelay();
Time t3 = Time::Now();
EXPECT_TRUE(t3 > t2);
const int kSize = 200;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
EXPECT_EQ(kSize, ReadData(entry, stream_index, 0, buffer.get(), kSize));
if (type_ == net::APP_CACHE) {
EXPECT_TRUE(entry->GetLastUsed() < t2);
EXPECT_TRUE(entry->GetLastModified() < t2);
} else if (type_ == net::SHADER_CACHE) {
EXPECT_TRUE(entry->GetLastUsed() < t3);
EXPECT_TRUE(entry->GetLastModified() < t3);
} else {
EXPECT_TRUE(entry->GetLastUsed() >= t3);
EXPECT_TRUE(entry->GetLastModified() < t3);
}
entry->Close();
}
TEST_F(DiskCacheEntryTest, GetTimes) {
InitCache();
GetTimes(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyGetTimes) {
SetMemoryOnlyMode();
InitCache();
GetTimes(0);
}
TEST_F(DiskCacheEntryTest, AppCacheGetTimes) {
SetCacheType(net::APP_CACHE);
InitCache();
GetTimes(0);
}
TEST_F(DiskCacheEntryTest, ShaderCacheGetTimes) {
SetCacheType(net::SHADER_CACHE);
InitCache();
GetTimes(0);
}
void DiskCacheEntryTest::GrowData(int stream_index) {
std::string key1("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
const int kSize = 20000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, false);
memset(buffer2->data(), 0, kSize);
base::strlcpy(buffer1->data(), "the data", kSize);
EXPECT_EQ(10, WriteData(entry, stream_index, 0, buffer1.get(), 10, false));
EXPECT_EQ(10, ReadData(entry, stream_index, 0, buffer2.get(), 10));
EXPECT_STREQ("the data", buffer2->data());
EXPECT_EQ(10, entry->GetDataSize(stream_index));
EXPECT_EQ(2000,
WriteData(entry, stream_index, 0, buffer1.get(), 2000, false));
EXPECT_EQ(2000, entry->GetDataSize(stream_index));
EXPECT_EQ(2000, ReadData(entry, stream_index, 0, buffer2.get(), 2000));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
EXPECT_EQ(20000,
WriteData(entry, stream_index, 0, buffer1.get(), kSize, false));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(20000, ReadData(entry, stream_index, 0, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
entry->Close();
memset(buffer2->data(), 0, kSize);
std::string key2("Second key");
ASSERT_EQ(net::OK, CreateEntry(key2, &entry));
EXPECT_EQ(10, WriteData(entry, stream_index, 0, buffer1.get(), 10, false));
EXPECT_EQ(10, entry->GetDataSize(stream_index));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
EXPECT_EQ(2000,
WriteData(entry, stream_index, 0, buffer1.get(), 2000, false));
EXPECT_EQ(2000, entry->GetDataSize(stream_index));
EXPECT_EQ(2000, ReadData(entry, stream_index, 0, buffer2.get(), 2000));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 2000));
entry->Close();
memset(buffer2->data(), 0, kSize);
ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
EXPECT_EQ(20000,
WriteData(entry, stream_index, 0, buffer1.get(), kSize, false));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(20000, ReadData(entry, stream_index, 0, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), kSize));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 45500, buffer1.get(), 0, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
EXPECT_EQ(45500, entry->GetDataSize(stream_index));
entry->Close();
}
TEST_F(DiskCacheEntryTest, GrowData) {
InitCache();
GrowData(0);
}
TEST_F(DiskCacheEntryTest, GrowDataNoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
GrowData(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyGrowData) {
SetMemoryOnlyMode();
InitCache();
GrowData(0);
}
void DiskCacheEntryTest::TruncateData(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize1 = 20000;
const int kSize2 = 20000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(200, WriteData(entry, stream_index, 0, buffer1.get(), 200, false));
EXPECT_EQ(200, entry->GetDataSize(stream_index));
EXPECT_EQ(100, WriteData(entry, stream_index, 0, buffer1.get(), 100, false));
EXPECT_EQ(200, entry->GetDataSize(stream_index));
EXPECT_EQ(100, WriteData(entry, stream_index, 0, buffer1.get(), 100, true));
EXPECT_EQ(100, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 50, buffer1.get(), 0, true));
EXPECT_EQ(50, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 0, buffer1.get(), 0, true));
EXPECT_EQ(0, entry->GetDataSize(stream_index));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(20000,
WriteData(entry, stream_index, 0, buffer1.get(), 20000, true));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(20000, ReadData(entry, stream_index, 0, buffer2.get(), 20000));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 20000));
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(18000,
WriteData(entry, stream_index, 0, buffer1.get(), 18000, false));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(18000,
WriteData(entry, stream_index, 0, buffer1.get(), 18000, true));
EXPECT_EQ(18000, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 17500, buffer1.get(), 0, true));
EXPECT_EQ(17500, entry->GetDataSize(stream_index));
EXPECT_EQ(600,
WriteData(entry, stream_index, 1000, buffer1.get(), 600, true));
EXPECT_EQ(1600, entry->GetDataSize(stream_index));
EXPECT_EQ(600, ReadData(entry, stream_index, 1000, buffer2.get(), 600));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 600));
EXPECT_EQ(1000, ReadData(entry, stream_index, 0, buffer2.get(), 1000));
EXPECT_TRUE(!memcmp(buffer1->data(), buffer2->data(), 1000))
<< "Preserves previous data";
EXPECT_EQ(20000,
WriteData(entry, stream_index, 0, buffer1.get(), 20000, true));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 0, buffer1.get(), 0, true));
EXPECT_EQ(0, entry->GetDataSize(stream_index));
entry->Close();
}
TEST_F(DiskCacheEntryTest, TruncateData) {
InitCache();
TruncateData(0);
}
TEST_F(DiskCacheEntryTest, TruncateDataNoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
TruncateData(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyTruncateData) {
SetMemoryOnlyMode();
InitCache();
TruncateData(0);
}
void DiskCacheEntryTest::ZeroLengthIO(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(0, ReadData(entry, stream_index, 0, NULL, 0));
EXPECT_EQ(0, WriteData(entry, stream_index, 0, NULL, 0, false));
EXPECT_EQ(0, WriteData(entry, stream_index, 1000, NULL, 0, false));
EXPECT_EQ(0, ReadData(entry, stream_index, 500, NULL, 0));
EXPECT_EQ(0, ReadData(entry, stream_index, 2000, NULL, 0));
EXPECT_EQ(1000, entry->GetDataSize(stream_index));
EXPECT_EQ(0, WriteData(entry, stream_index, 100000, NULL, 0, true));
EXPECT_EQ(0, ReadData(entry, stream_index, 50000, NULL, 0));
EXPECT_EQ(100000, entry->GetDataSize(stream_index));
const int kSize = 20;
const char zeros[kSize] = {};
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
EXPECT_EQ(kSize, ReadData(entry, stream_index, 500, buffer.get(), kSize));
EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
EXPECT_EQ(kSize, ReadData(entry, stream_index, 5000, buffer.get(), kSize));
EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
EXPECT_EQ(kSize, ReadData(entry, stream_index, 50000, buffer.get(), kSize));
EXPECT_TRUE(!memcmp(buffer->data(), zeros, kSize));
entry->Close();
}
TEST_F(DiskCacheEntryTest, ZeroLengthIO) {
InitCache();
ZeroLengthIO(0);
}
TEST_F(DiskCacheEntryTest, ZeroLengthIONoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
ZeroLengthIO(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyZeroLengthIO) {
SetMemoryOnlyMode();
InitCache();
ZeroLengthIO(0);
}
void DiskCacheEntryTest::Buffering() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 200;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, true);
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer1.get(), kSize, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(kSize, WriteData(entry, 1, 5000, buffer1.get(), kSize, false));
EXPECT_EQ(kSize, ReadData(entry, 1, 0, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
EXPECT_EQ(kSize, WriteData(entry, 1, 18000, buffer1.get(), kSize, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(kSize, WriteData(entry, 1, 10000, buffer1.get(), kSize, false));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 5000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 0, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 18000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
EXPECT_EQ(kSize, WriteData(entry, 1, 23000, buffer1.get(), kSize, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(kSize, WriteData(entry, 1, 17000, buffer1.get(), kSize, false));
EXPECT_EQ(kSize, WriteData(entry, 1, 19000, buffer1.get(), kSize, false));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 18000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 17000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
EXPECT_EQ(kSize, WriteData(entry, 1, 22900, buffer1.get(), kSize, false));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(100, ReadData(entry, 1, 23000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + 100, 100));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(100, ReadData(entry, 1, 23100, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + 100, 100));
EXPECT_EQ(kSize, WriteData(entry, 1, 25000, buffer1.get(), kSize, false));
EXPECT_EQ(kSize, WriteData(entry, 1, 45000, buffer1.get(), kSize, false));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 25000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, 1, 45000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data(), kSize));
entry->Close();
}
TEST_F(DiskCacheEntryTest, Buffering) {
InitCache();
Buffering();
}
TEST_F(DiskCacheEntryTest, BufferingNoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
Buffering();
}
void DiskCacheEntryTest::SizeAtCreate() {
const char key[] = "the first key";
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kNumStreams = 3;
for (int i = 0; i < kNumStreams; ++i)
EXPECT_EQ(0, entry->GetDataSize(i));
entry->Close();
}
TEST_F(DiskCacheEntryTest, SizeAtCreate) {
InitCache();
SizeAtCreate();
}
TEST_F(DiskCacheEntryTest, MemoryOnlySizeAtCreate) {
SetMemoryOnlyMode();
InitCache();
SizeAtCreate();
}
void DiskCacheEntryTest::SizeChanges(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 200;
const char zeros[kSize] = {};
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, true);
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 0, buffer1.get(), kSize, true));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 17000, buffer1.get(), kSize, true));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 23000, buffer1.get(), kSize, true));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(23000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 25000, buffer1.get(), kSize, true));
EXPECT_EQ(25000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(kSize, ReadData(entry, stream_index, 24000, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), zeros, kSize));
EXPECT_EQ(
kSize,
ReadData(entry, stream_index, 23000 + kSize - 35, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + kSize - 35, 35));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, stream_index, 24900, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 26000, buffer1.get(), kSize, true));
EXPECT_EQ(26000 + kSize, entry->GetDataSize(stream_index));
CacheTestFillBuffer(buffer2->data(), kSize, true);
EXPECT_EQ(kSize, ReadData(entry, stream_index, 25900, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 25000, buffer1.get(), kSize, true));
EXPECT_EQ(25000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(
28,
ReadData(entry, stream_index, 25000 + kSize - 28, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), buffer1->data() + kSize - 28, 28));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 24000, buffer1.get(), kSize, false));
EXPECT_EQ(25000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 24500, buffer1.get(), kSize, true));
EXPECT_EQ(24500 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(kSize, ReadData(entry, stream_index, 23900, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
EXPECT_EQ(kSize,
WriteData(entry, stream_index, 19000, buffer1.get(), kSize, true));
EXPECT_EQ(19000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(kSize, ReadData(entry, stream_index, 18900, buffer2.get(), kSize));
EXPECT_TRUE(!memcmp(buffer2->data(), zeros, 100));
EXPECT_TRUE(!memcmp(buffer2->data() + 100, buffer1->data(), kSize - 100));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(19000 + kSize, entry->GetDataSize(stream_index));
EXPECT_EQ(
0,
WriteData(entry, stream_index, 20000 + kSize, buffer1.get(), 0, false));
EXPECT_EQ(kSize,
ReadData(entry, stream_index, 19000 + kSize, buffer1.get(), kSize));
EXPECT_EQ(0, memcmp(buffer1->data(), zeros, kSize));
entry->Close();
}
TEST_F(DiskCacheEntryTest, SizeChanges) {
InitCache();
SizeChanges(1);
}
TEST_F(DiskCacheEntryTest, SizeChangesNoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
SizeChanges(1);
}
void DiskCacheEntryTest::ReuseEntry(int size, int stream_index) {
std::string key1("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
entry->Close();
std::string key2("the second key");
ASSERT_EQ(net::OK, CreateEntry(key2, &entry));
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(size));
CacheTestFillBuffer(buffer->data(), size, false);
for (int i = 0; i < 15; i++) {
EXPECT_EQ(0, WriteData(entry, stream_index, 0, buffer.get(), 0, true));
EXPECT_EQ(size,
WriteData(entry, stream_index, 0, buffer.get(), size, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key2, &entry));
}
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key1, &entry)) << "have not evicted this entry";
entry->Close();
}
TEST_F(DiskCacheEntryTest, ReuseExternalEntry) {
SetMaxSize(200 * 1024);
InitCache();
ReuseEntry(20 * 1024, 0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyReuseExternalEntry) {
SetMemoryOnlyMode();
SetMaxSize(200 * 1024);
InitCache();
ReuseEntry(20 * 1024, 0);
}
TEST_F(DiskCacheEntryTest, ReuseInternalEntry) {
SetMaxSize(100 * 1024);
InitCache();
ReuseEntry(10 * 1024, 0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyReuseInternalEntry) {
SetMemoryOnlyMode();
SetMaxSize(100 * 1024);
InitCache();
ReuseEntry(10 * 1024, 0);
}
void DiskCacheEntryTest::InvalidData(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize1 = 20000;
const int kSize2 = 20000;
const int kSize3 = 20000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
scoped_refptr<net::IOBuffer> buffer3(new net::IOBuffer(kSize3));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(200,
WriteData(entry, stream_index, 400, buffer1.get(), 200, false));
EXPECT_EQ(600, entry->GetDataSize(stream_index));
EXPECT_EQ(100, ReadData(entry, stream_index, 300, buffer3.get(), 100));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(200,
WriteData(entry, stream_index, 800, buffer1.get(), 200, false));
EXPECT_EQ(1000, entry->GetDataSize(stream_index));
EXPECT_EQ(100, ReadData(entry, stream_index, 700, buffer3.get(), 100));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(200,
WriteData(entry, stream_index, 1800, buffer1.get(), 200, true));
EXPECT_EQ(2000, entry->GetDataSize(stream_index));
EXPECT_EQ(100, ReadData(entry, stream_index, 1500, buffer3.get(), 100));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 100));
EXPECT_EQ(200,
WriteData(entry, stream_index, 19800, buffer1.get(), 200, false));
EXPECT_EQ(20000, entry->GetDataSize(stream_index));
EXPECT_EQ(4000, ReadData(entry, stream_index, 14000, buffer3.get(), 4000));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 4000));
EXPECT_EQ(600,
WriteData(entry, stream_index, 1000, buffer1.get(), 600, true));
EXPECT_EQ(1600, entry->GetDataSize(stream_index));
EXPECT_EQ(600, ReadData(entry, stream_index, 1000, buffer3.get(), 600));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer1->data(), 600));
EXPECT_EQ(600,
WriteData(entry, stream_index, 2000, buffer1.get(), 600, false));
EXPECT_EQ(2600, entry->GetDataSize(stream_index));
EXPECT_EQ(200, ReadData(entry, stream_index, 1800, buffer3.get(), 200));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
EXPECT_EQ(600,
WriteData(entry, stream_index, 3000, buffer1.get(), 600, true));
EXPECT_EQ(3600, entry->GetDataSize(stream_index));
EXPECT_EQ(200, ReadData(entry, stream_index, 2800, buffer3.get(), 200));
EXPECT_TRUE(!memcmp(buffer3->data(), buffer2->data(), 200));
entry->Close();
}
TEST_F(DiskCacheEntryTest, InvalidData) {
InitCache();
InvalidData(0);
}
TEST_F(DiskCacheEntryTest, InvalidDataNoBuffer) {
InitCache();
cache_impl_->SetFlags(disk_cache::kNoBuffering);
InvalidData(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyInvalidData) {
SetMemoryOnlyMode();
InitCache();
InvalidData(0);
}
void DiskCacheEntryTest::ReadWriteDestroyBuffer(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 200;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
net::TestCompletionCallback cb;
EXPECT_EQ(net::ERR_IO_PENDING,
entry->WriteData(
stream_index, 0, buffer.get(), kSize, cb.callback(), false));
buffer = NULL;
EXPECT_EQ(kSize, cb.WaitForResult());
buffer = new net::IOBuffer(kSize);
CacheTestFillBuffer(buffer->data(), kSize, false);
EXPECT_EQ(
net::ERR_IO_PENDING,
entry->ReadData(stream_index, 0, buffer.get(), kSize, cb.callback()));
buffer = NULL;
EXPECT_EQ(kSize, cb.WaitForResult());
entry->Close();
}
TEST_F(DiskCacheEntryTest, ReadWriteDestroyBuffer) {
InitCache();
ReadWriteDestroyBuffer(0);
}
void DiskCacheEntryTest::DoomNormalEntry() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
entry->Doom();
entry->Close();
const int kSize = 20000;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, true);
buffer->data()[19999] = '\0';
key = buffer->data();
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(20000, WriteData(entry, 0, 0, buffer.get(), kSize, false));
EXPECT_EQ(20000, WriteData(entry, 1, 0, buffer.get(), kSize, false));
entry->Doom();
entry->Close();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, DoomEntry) {
InitCache();
DoomNormalEntry();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyDoomEntry) {
SetMemoryOnlyMode();
InitCache();
DoomNormalEntry();
}
void DiskCacheEntryTest::DoomEntryNextToOpenEntry() {
disk_cache::Entry* entry1;
disk_cache::Entry* entry2;
ASSERT_EQ(net::OK, CreateEntry("fixed", &entry1));
entry1->Close();
ASSERT_EQ(net::OK, CreateEntry("foo", &entry1));
entry1->Close();
ASSERT_EQ(net::OK, CreateEntry("bar", &entry1));
entry1->Close();
ASSERT_EQ(net::OK, OpenEntry("foo", &entry1));
ASSERT_EQ(net::OK, OpenEntry("bar", &entry2));
entry2->Doom();
entry2->Close();
ASSERT_EQ(net::OK, OpenEntry("foo", &entry2));
entry2->Doom();
entry2->Close();
entry1->Close();
ASSERT_EQ(net::OK, OpenEntry("fixed", &entry1));
entry1->Close();
}
TEST_F(DiskCacheEntryTest, DoomEntryNextToOpenEntry) {
InitCache();
DoomEntryNextToOpenEntry();
}
TEST_F(DiskCacheEntryTest, NewEvictionDoomEntryNextToOpenEntry) {
SetNewEviction();
InitCache();
DoomEntryNextToOpenEntry();
}
TEST_F(DiskCacheEntryTest, AppCacheDoomEntryNextToOpenEntry) {
SetCacheType(net::APP_CACHE);
InitCache();
DoomEntryNextToOpenEntry();
}
void DiskCacheEntryTest::DoomedEntry(int stream_index) {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
entry->Doom();
FlushQueueForTest();
EXPECT_EQ(0, cache_->GetEntryCount());
Time initial = Time::Now();
AddDelay();
const int kSize1 = 2000;
const int kSize2 = 2000;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
memset(buffer2->data(), 0, kSize2);
EXPECT_EQ(2000,
WriteData(entry, stream_index, 0, buffer1.get(), 2000, false));
EXPECT_EQ(2000, ReadData(entry, stream_index, 0, buffer2.get(), 2000));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kSize1));
EXPECT_EQ(key, entry->GetKey());
EXPECT_TRUE(initial < entry->GetLastModified());
EXPECT_TRUE(initial < entry->GetLastUsed());
entry->Close();
}
TEST_F(DiskCacheEntryTest, DoomedEntry) {
InitCache();
DoomedEntry(0);
}
TEST_F(DiskCacheEntryTest, MemoryOnlyDoomedEntry) {
SetMemoryOnlyMode();
InitCache();
DoomedEntry(0);
}
TEST_F(DiskCacheEntryTest, MissingData) {
InitCache();
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 20000;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
entry->Close();
FlushQueueForTest();
disk_cache::Addr address(0x80000001);
base::FilePath name = cache_impl_->GetFileName(address);
EXPECT_TRUE(base::DeleteFile(name, false));
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(net::ERR_FILE_NOT_FOUND,
ReadData(entry, 0, 0, buffer.get(), kSize));
entry->Close();
ASSERT_NE(net::OK, OpenEntry(key, &entry));
}
TEST_F(DiskCacheEntryTest, MemoryOnlyEnumerationWithSparseEntries) {
SetMemoryOnlyMode();
InitCache();
const int kSize = 4096;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
std::string key("the first key");
disk_cache::Entry* parent_entry;
ASSERT_EQ(net::OK, CreateEntry(key, &parent_entry));
EXPECT_EQ(kSize,
parent_entry->WriteSparseData(
0, buf.get(), kSize, net::CompletionCallback()));
EXPECT_EQ(kSize,
parent_entry->WriteSparseData(
8192, buf.get(), kSize, net::CompletionCallback()));
parent_entry->Close();
void* iter = NULL;
disk_cache::Entry* entry = NULL;
int count = 0;
while (OpenNextEntry(&iter, &entry) == net::OK) {
ASSERT_TRUE(entry != NULL);
++count;
disk_cache::MemEntryImpl* mem_entry =
reinterpret_cast<disk_cache::MemEntryImpl*>(entry);
EXPECT_EQ(disk_cache::MemEntryImpl::kParentEntry, mem_entry->type());
mem_entry->Close();
}
EXPECT_EQ(1, count);
}
void VerifySparseIO(disk_cache::Entry* entry, int64 offset,
net::IOBuffer* buf_1, int size, net::IOBuffer* buf_2) {
net::TestCompletionCallback cb;
memset(buf_2->data(), 0, size);
int ret = entry->ReadSparseData(offset, buf_2, size, cb.callback());
EXPECT_EQ(0, cb.GetResult(ret));
ret = entry->WriteSparseData(offset, buf_1, size, cb.callback());
EXPECT_EQ(size, cb.GetResult(ret));
ret = entry->ReadSparseData(offset, buf_2, size, cb.callback());
EXPECT_EQ(size, cb.GetResult(ret));
EXPECT_EQ(0, memcmp(buf_1->data(), buf_2->data(), size));
}
void VerifyContentSparseIO(disk_cache::Entry* entry, int64 offset, char* buffer,
int size) {
net::TestCompletionCallback cb;
scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(size));
memset(buf_1->data(), 0, size);
int ret = entry->ReadSparseData(offset, buf_1.get(), size, cb.callback());
EXPECT_EQ(size, cb.GetResult(ret));
EXPECT_EQ(0, memcmp(buf_1->data(), buffer, size));
}
void DiskCacheEntryTest::BasicSparseIO() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 2048;
scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf_1->data(), kSize, false);
VerifySparseIO(entry, 0, buf_1.get(), kSize, buf_2.get());
VerifySparseIO(entry, 0x400000, buf_1.get(), kSize, buf_2.get());
VerifySparseIO(entry, 0x800000000LL, buf_1.get(), kSize, buf_2.get());
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
VerifyContentSparseIO(entry, 0, buf_1->data(), kSize);
VerifyContentSparseIO(entry, 0x400000, buf_1->data(), kSize);
VerifyContentSparseIO(entry, 0x800000000LL, buf_1->data(), kSize);
entry->Close();
}
TEST_F(DiskCacheEntryTest, BasicSparseIO) {
InitCache();
BasicSparseIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyBasicSparseIO) {
SetMemoryOnlyMode();
InitCache();
BasicSparseIO();
}
void DiskCacheEntryTest::HugeSparseIO() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 1200 * 1024;
scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf_1->data(), kSize, false);
VerifySparseIO(entry, 0x20F0000, buf_1.get(), kSize, buf_2.get());
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
VerifyContentSparseIO(entry, 0x20F0000, buf_1->data(), kSize);
entry->Close();
}
TEST_F(DiskCacheEntryTest, HugeSparseIO) {
InitCache();
HugeSparseIO();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyHugeSparseIO) {
SetMemoryOnlyMode();
InitCache();
HugeSparseIO();
}
void DiskCacheEntryTest::GetAvailableRange() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 16 * 1024;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf.get(), kSize));
EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F4400, buf.get(), kSize));
int64 start;
net::TestCompletionCallback cb;
int rv = entry->GetAvailableRange(
0x20F0000, kSize * 2, &start, cb.callback());
EXPECT_EQ(kSize, cb.GetResult(rv));
EXPECT_EQ(0x20F0000, start);
start = 0;
rv = entry->GetAvailableRange(0, kSize, &start, cb.callback());
EXPECT_EQ(0, cb.GetResult(rv));
rv = entry->GetAvailableRange(
0x20F0000 - kSize, kSize, &start, cb.callback());
EXPECT_EQ(0, cb.GetResult(rv));
rv = entry->GetAvailableRange(0, 0x2100000, &start, cb.callback());
EXPECT_EQ(kSize, cb.GetResult(rv));
EXPECT_EQ(0x20F0000, start);
start = -1;
rv = entry->GetAvailableRange(0x2100000, kSize, &start, cb.callback());
EXPECT_EQ(0, cb.GetResult(rv));
rv = entry->ReadSparseData(start, buf.get(), kSize, cb.callback());
EXPECT_EQ(0, cb.GetResult(rv));
start = 0;
rv = entry->GetAvailableRange(0x20F2000, kSize, &start, cb.callback());
EXPECT_EQ(0x2000, cb.GetResult(rv));
EXPECT_EQ(0x20F2000, start);
EXPECT_EQ(0x2000, ReadSparseData(entry, start, buf.get(), kSize));
start = 0;
rv = entry->GetAvailableRange(
0x20F0001 - kSize, kSize, &start, cb.callback());
EXPECT_EQ(1, cb.GetResult(rv));
EXPECT_EQ(0x20F0000, start);
entry->Close();
}
TEST_F(DiskCacheEntryTest, GetAvailableRange) {
InitCache();
GetAvailableRange();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyGetAvailableRange) {
SetMemoryOnlyMode();
InitCache();
GetAvailableRange();
}
void DiskCacheEntryTest::CouldBeSparse() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 16 * 1024;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
EXPECT_EQ(kSize, WriteSparseData(entry, 0x20F0000, buf.get(), kSize));
EXPECT_TRUE(entry->CouldBeSparse());
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_TRUE(entry->CouldBeSparse());
entry->Close();
key.assign("another key");
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_FALSE(entry->CouldBeSparse());
EXPECT_EQ(kSize, WriteData(entry, 0, 0, buf.get(), kSize, false));
EXPECT_EQ(kSize, WriteData(entry, 1, 0, buf.get(), kSize, false));
EXPECT_EQ(kSize, WriteData(entry, 2, 0, buf.get(), kSize, false));
EXPECT_FALSE(entry->CouldBeSparse());
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_FALSE(entry->CouldBeSparse());
entry->Close();
}
TEST_F(DiskCacheEntryTest, CouldBeSparse) {
InitCache();
CouldBeSparse();
}
TEST_F(DiskCacheEntryTest, MemoryCouldBeSparse) {
SetMemoryOnlyMode();
InitCache();
CouldBeSparse();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyMisalignedSparseIO) {
SetMemoryOnlyMode();
InitCache();
const int kSize = 8192;
scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf_1->data(), kSize, false);
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
for (int i = 0; i < kSize; i += 1024) {
scoped_refptr<net::WrappedIOBuffer> buf_3(
new net::WrappedIOBuffer(buf_1->data() + i));
VerifySparseIO(entry, i, buf_3.get(), 1024, buf_2.get());
VerifySparseIO(entry, 9000 + i, buf_3.get(), 1024, buf_2.get());
}
VerifyContentSparseIO(entry, 0, buf_1->data(), kSize);
VerifyContentSparseIO(entry, 9000, buf_1->data(), kSize);
VerifySparseIO(entry, 20481, buf_1.get(), 8192, buf_2.get());
entry->Close();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyMisalignedGetAvailableRange) {
SetMemoryOnlyMode();
InitCache();
const int kSize = 8192;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
disk_cache::Entry* entry;
std::string key("the first key");
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(
1024,
entry->WriteSparseData(0, buf.get(), 1024, net::CompletionCallback()));
EXPECT_EQ(
1024,
entry->WriteSparseData(5120, buf.get(), 1024, net::CompletionCallback()));
EXPECT_EQ(1024,
entry->WriteSparseData(
10000, buf.get(), 1024, net::CompletionCallback()));
EXPECT_EQ(8192,
entry->WriteSparseData(
50000, buf.get(), 8192, net::CompletionCallback()));
int64 start;
net::TestCompletionCallback cb;
int rv = entry->GetAvailableRange(0, 10000, &start, cb.callback());
EXPECT_EQ(1024, cb.GetResult(rv));
EXPECT_EQ(0, start);
rv = entry->GetAvailableRange(512, 10000, &start, cb.callback());
EXPECT_EQ(512, cb.GetResult(rv));
EXPECT_EQ(512, start);
rv = entry->GetAvailableRange(1024, 10000, &start, cb.callback());
EXPECT_EQ(1024, cb.GetResult(rv));
EXPECT_EQ(5120, start);
rv = entry->GetAvailableRange(5500, 512, &start, cb.callback());
EXPECT_EQ(512, cb.GetResult(rv));
EXPECT_EQ(5500, start);
rv = entry->GetAvailableRange(5000, 620, &start, cb.callback());
EXPECT_EQ(500, cb.GetResult(rv));
EXPECT_EQ(5120, start);
rv = entry->GetAvailableRange(40000, 20000, &start, cb.callback());
EXPECT_EQ(8192, cb.GetResult(rv));
EXPECT_EQ(50000, start);
entry->Close();
}
void DiskCacheEntryTest::UpdateSparseEntry() {
std::string key("the first key");
disk_cache::Entry* entry1;
ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
const int kSize = 2048;
scoped_refptr<net::IOBuffer> buf_1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buf_2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf_1->data(), kSize, false);
VerifySparseIO(entry1, 0, buf_1.get(), kSize, buf_2.get());
entry1->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry1));
VerifySparseIO(entry1, 2048, buf_1.get(), kSize, buf_2.get());
disk_cache::Entry* entry2;
ASSERT_EQ(net::OK, CreateEntry("the second key", &entry2));
entry1->Close();
entry2->Close();
FlushQueueForTest();
if (memory_only_ || simple_cache_mode_)
EXPECT_EQ(2, cache_->GetEntryCount());
else
EXPECT_EQ(3, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, UpdateSparseEntry) {
SetCacheType(net::MEDIA_CACHE);
InitCache();
UpdateSparseEntry();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyUpdateSparseEntry) {
SetMemoryOnlyMode();
SetCacheType(net::MEDIA_CACHE);
InitCache();
UpdateSparseEntry();
}
void DiskCacheEntryTest::DoomSparseEntry() {
std::string key1("the first key");
std::string key2("the second key");
disk_cache::Entry *entry1, *entry2;
ASSERT_EQ(net::OK, CreateEntry(key1, &entry1));
ASSERT_EQ(net::OK, CreateEntry(key2, &entry2));
const int kSize = 4 * 1024;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
int64 offset = 1024;
for (int i = 0; i < 12; i++) {
EXPECT_EQ(kSize, WriteSparseData(entry1, offset, buf.get(), kSize));
if (i < 9)
EXPECT_EQ(kSize, WriteSparseData(entry2, offset, buf.get(), kSize));
offset *= 4;
}
if (memory_only_ || simple_cache_mode_)
EXPECT_EQ(2, cache_->GetEntryCount());
else
EXPECT_EQ(15, cache_->GetEntryCount());
entry1->Doom();
entry1->Close();
entry2->Close();
EXPECT_EQ(net::OK, DoomEntry(key2));
base::MessageLoop::current()->RunUntilIdle();
if (memory_only_) {
EXPECT_EQ(0, cache_->GetEntryCount());
} else {
if (5 == cache_->GetEntryCount()) {
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(500));
base::MessageLoop::current()->RunUntilIdle();
}
EXPECT_EQ(0, cache_->GetEntryCount());
}
}
TEST_F(DiskCacheEntryTest, DoomSparseEntry) {
UseCurrentThread();
InitCache();
DoomSparseEntry();
}
TEST_F(DiskCacheEntryTest, MemoryOnlyDoomSparseEntry) {
SetMemoryOnlyMode();
InitCache();
DoomSparseEntry();
}
class SparseTestCompletionCallback: public net::TestCompletionCallback {
public:
explicit SparseTestCompletionCallback(scoped_ptr<disk_cache::Backend> cache)
: cache_(cache.Pass()) {
}
private:
virtual void SetResult(int result) OVERRIDE {
cache_.reset();
TestCompletionCallback::SetResult(result);
}
scoped_ptr<disk_cache::Backend> cache_;
DISALLOW_COPY_AND_ASSIGN(SparseTestCompletionCallback);
};
TEST_F(DiskCacheEntryTest, DoomSparseEntry2) {
UseCurrentThread();
InitCache();
std::string key("the key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 4 * 1024;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
int64 offset = 1024;
for (int i = 0; i < 12; i++) {
EXPECT_EQ(kSize,
entry->WriteSparseData(
offset, buf.get(), kSize, net::CompletionCallback()));
offset *= 4;
}
EXPECT_EQ(9, cache_->GetEntryCount());
entry->Close();
disk_cache::Backend* cache = cache_.get();
SparseTestCompletionCallback cb(cache_.Pass());
int rv = cache->DoomEntry(key, cb.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
EXPECT_EQ(net::OK, cb.WaitForResult());
}
void DiskCacheEntryTest::PartialSparseEntry() {
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 4 * 1024;
const int kSmallSize = 128;
scoped_refptr<net::IOBuffer> buf1(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf1->data(), kSize, false);
EXPECT_EQ(kSize, WriteSparseData(entry, 20000, buf1.get(), kSize));
EXPECT_EQ(kSize, WriteSparseData(entry, 500, buf1.get(), kSize));
EXPECT_EQ(kSmallSize,
WriteSparseData(entry, 1080321, buf1.get(), kSmallSize));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
scoped_refptr<net::IOBuffer> buf2(new net::IOBuffer(kSize));
memset(buf2->data(), 0, kSize);
EXPECT_EQ(0, ReadSparseData(entry, 8000, buf2.get(), kSize));
EXPECT_EQ(500, ReadSparseData(entry, kSize, buf2.get(), kSize));
EXPECT_EQ(0, memcmp(buf2->data(), buf1->data() + kSize - 500, 500));
EXPECT_EQ(0, ReadSparseData(entry, 0, buf2.get(), kSize));
EXPECT_EQ(96, ReadSparseData(entry, 24000, buf2.get(), kSize));
EXPECT_EQ(500, ReadSparseData(entry, kSize, buf2.get(), kSize));
EXPECT_EQ(0, ReadSparseData(entry, 99, buf2.get(), kSize));
int rv;
int64 start;
net::TestCompletionCallback cb;
if (memory_only_ || simple_cache_mode_) {
rv = entry->GetAvailableRange(0, 600, &start, cb.callback());
EXPECT_EQ(100, cb.GetResult(rv));
EXPECT_EQ(500, start);
} else {
rv = entry->GetAvailableRange(0, 2048, &start, cb.callback());
EXPECT_EQ(1024, cb.GetResult(rv));
EXPECT_EQ(1024, start);
}
rv = entry->GetAvailableRange(kSize, kSize, &start, cb.callback());
EXPECT_EQ(500, cb.GetResult(rv));
EXPECT_EQ(kSize, start);
rv = entry->GetAvailableRange(20 * 1024, 10000, &start, cb.callback());
EXPECT_EQ(3616, cb.GetResult(rv));
EXPECT_EQ(20 * 1024, start);
if (memory_only_ || simple_cache_mode_) {
rv = entry->GetAvailableRange(19400, kSize, &start, cb.callback());
EXPECT_EQ(3496, cb.GetResult(rv));
EXPECT_EQ(20000, start);
} else {
rv = entry->GetAvailableRange(19400, kSize, &start, cb.callback());
EXPECT_EQ(3016, cb.GetResult(rv));
EXPECT_EQ(20480, start);
}
rv = entry->GetAvailableRange(3073, kSize, &start, cb.callback());
EXPECT_EQ(1523, cb.GetResult(rv));
EXPECT_EQ(3073, start);
rv = entry->GetAvailableRange(4600, kSize, &start, cb.callback());
EXPECT_EQ(0, cb.GetResult(rv));
EXPECT_EQ(4600, start);
EXPECT_EQ(kSize, WriteSparseData(entry, 500 + kSize, buf1.get(), kSize));
rv = entry->GetAvailableRange(1024, 10000, &start, cb.callback());
EXPECT_EQ(7 * 1024 + 500, cb.GetResult(rv));
EXPECT_EQ(1024, start);
EXPECT_EQ(kSize, ReadSparseData(entry, kSize, buf2.get(), kSize));
EXPECT_EQ(0, memcmp(buf2->data(), buf1->data() + kSize - 500, 500));
EXPECT_EQ(0, memcmp(buf2->data() + 500, buf1->data(), kSize - 500));
entry->Close();
}
TEST_F(DiskCacheEntryTest, PartialSparseEntry) {
InitCache();
PartialSparseEntry();
}
TEST_F(DiskCacheEntryTest, MemoryPartialSparseEntry) {
SetMemoryOnlyMode();
InitCache();
PartialSparseEntry();
}
TEST_F(DiskCacheEntryTest, CleanupSparseEntry) {
InitCache();
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 4 * 1024;
scoped_refptr<net::IOBuffer> buf1(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf1->data(), kSize, false);
const int k1Meg = 1024 * 1024;
EXPECT_EQ(kSize, WriteSparseData(entry, 8192, buf1.get(), kSize));
EXPECT_EQ(kSize, WriteSparseData(entry, k1Meg + 8192, buf1.get(), kSize));
EXPECT_EQ(kSize, WriteSparseData(entry, 2 * k1Meg + 8192, buf1.get(), kSize));
entry->Close();
EXPECT_EQ(4, cache_->GetEntryCount());
void* iter = NULL;
int count = 0;
std::string child_key[2];
while (OpenNextEntry(&iter, &entry) == net::OK) {
ASSERT_TRUE(entry != NULL);
if (entry->GetKey() != key && count < 2)
child_key[count++] = entry->GetKey();
entry->Close();
}
for (int i = 0; i < 2; i++) {
ASSERT_EQ(net::OK, OpenEntry(child_key[i], &entry));
EXPECT_EQ(12, WriteData(entry, 2, 0, buf1.get(), 12, false));
entry->Close();
}
EXPECT_EQ(4, cache_->GetEntryCount());
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(0, ReadSparseData(entry, 2 * k1Meg + 8192, buf1.get(), kSize));
EXPECT_EQ(kSize, WriteSparseData(entry, k1Meg + 16384, buf1.get(), kSize));
EXPECT_EQ(0, ReadSparseData(entry, k1Meg + 8192, buf1.get(), kSize));
EXPECT_EQ(kSize, ReadSparseData(entry, 8192, buf1.get(), kSize));
entry->Close();
EXPECT_EQ(3, cache_->GetEntryCount());
}
TEST_F(DiskCacheEntryTest, CancelSparseIO) {
UseCurrentThread();
InitCache();
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
const int kSize = 40 * 1024;
scoped_refptr<net::IOBuffer> buf(new net::IOBuffer(kSize));
CacheTestFillBuffer(buf->data(), kSize, false);
net::TestCompletionCallback cb1, cb2, cb3, cb4, cb5;
int rv = entry->WriteSparseData(
1024 * 1024 - 4096, buf.get(), kSize, cb1.callback());
EXPECT_EQ(net::ERR_IO_PENDING, rv);
int64 offset = 0;
rv = entry->GetAvailableRange(offset, kSize, &offset, cb5.callback());
rv = cb5.GetResult(rv);
if (!cb1.have_result()) {
EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED, rv);
}
entry->CancelSparseIO();
EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(cb2.callback()));
EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(cb3.callback()));
entry->CancelSparseIO();
EXPECT_EQ(net::ERR_IO_PENDING, entry->ReadyForSparseIO(cb4.callback()));
if (!cb1.have_result()) {
EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED,
entry->ReadSparseData(
offset, buf.get(), kSize, net::CompletionCallback()));
EXPECT_EQ(net::ERR_CACHE_OPERATION_NOT_SUPPORTED,
entry->WriteSparseData(
offset, buf.get(), kSize, net::CompletionCallback()));
}
rv = cb1.WaitForResult();
EXPECT_TRUE(rv == 4096 || rv == kSize);
EXPECT_EQ(net::OK, cb2.WaitForResult());
EXPECT_EQ(net::OK, cb3.WaitForResult());
EXPECT_EQ(net::OK, cb4.WaitForResult());
rv = entry->GetAvailableRange(offset, kSize, &offset, cb5.callback());
EXPECT_EQ(0, cb5.GetResult(rv));
entry->Close();
}
TEST_F(DiskCacheEntryTest, KeySanityCheck) {
UseCurrentThread();
InitCache();
std::string key("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
disk_cache::EntryImpl* entry_impl =
static_cast<disk_cache::EntryImpl*>(entry);
disk_cache::EntryStore* store = entry_impl->entry()->Data();
store->key_len = 800;
memset(store->key + key.size(), 'k', sizeof(store->key) - key.size());
entry_impl->entry()->set_modified();
entry->Close();
ASSERT_NE(net::OK, OpenEntry(key, &entry));
DisableIntegrityCheck();
}
#if defined(OS_POSIX)
TEST_F(DiskCacheEntryTest, SimpleCacheInternalAsyncIO) {
SetSimpleCacheMode();
InitCache();
InternalAsyncIO();
}
TEST_F(DiskCacheEntryTest, SimpleCacheExternalAsyncIO) {
SetSimpleCacheMode();
InitCache();
ExternalAsyncIO();
}
TEST_F(DiskCacheEntryTest, SimpleCacheReleaseBuffer) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
ReleaseBuffer(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheStreamAccess) {
SetSimpleCacheMode();
InitCache();
StreamAccess();
}
TEST_F(DiskCacheEntryTest, SimpleCacheGetKey) {
SetSimpleCacheMode();
InitCache();
GetKey();
}
TEST_F(DiskCacheEntryTest, SimpleCacheGetTimes) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
GetTimes(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheGrowData) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
GrowData(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheTruncateData) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
TruncateData(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheZeroLengthIO) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
ZeroLengthIO(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheSizeAtCreate) {
SetSimpleCacheMode();
InitCache();
SizeAtCreate();
}
TEST_F(DiskCacheEntryTest, SimpleCacheReuseExternalEntry) {
SetSimpleCacheMode();
SetMaxSize(200 * 1024);
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
ReuseEntry(20 * 1024, i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheReuseInternalEntry) {
SetSimpleCacheMode();
SetMaxSize(100 * 1024);
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
ReuseEntry(10 * 1024, i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheSizeChanges) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
SizeChanges(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheInvalidData) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
InvalidData(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheReadWriteDestroyBuffer) {
SetCacheType(net::APP_CACHE);
SetSimpleCacheMode();
InitCache();
for (int i = 1; i < disk_cache::kSimpleEntryStreamCount; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
ReadWriteDestroyBuffer(i);
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomEntry) {
SetSimpleCacheMode();
InitCache();
DoomNormalEntry();
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomEntryNextToOpenEntry) {
SetSimpleCacheMode();
InitCache();
DoomEntryNextToOpenEntry();
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomedEntry) {
SetSimpleCacheMode();
InitCache();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount - 1; ++i) {
EXPECT_EQ(net::OK, DoomAllEntries());
DoomedEntry(i);
}
}
bool DiskCacheEntryTest::SimpleCacheMakeBadChecksumEntry(const std::string& key,
int* data_size) {
disk_cache::Entry* entry = NULL;
if (CreateEntry(key, &entry) != net::OK || !entry) {
LOG(ERROR) << "Could not create entry";
return false;
}
const char data[] = "this is very good data";
const int kDataSize = arraysize(data);
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kDataSize));
base::strlcpy(buffer->data(), data, kDataSize);
EXPECT_EQ(kDataSize, WriteData(entry, 1, 0, buffer.get(), kDataSize, false));
entry->Close();
entry = NULL;
base::FilePath entry_file0_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
base::File entry_file0(entry_file0_path,
base::File::FLAG_WRITE | base::File::FLAG_OPEN);
if (!entry_file0.IsValid())
return false;
int64 file_offset =
sizeof(disk_cache::SimpleFileHeader) + key.size() + kDataSize - 2;
EXPECT_EQ(1, entry_file0.Write(file_offset, "X", 1));
*data_size = kDataSize;
return true;
}
TEST_F(DiskCacheEntryTest, SimpleCacheBadChecksum) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
int size_unused;
ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused));
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
ScopedEntryPtr entry_closer(entry);
const int kReadBufferSize = 200;
EXPECT_GE(kReadBufferSize, entry->GetDataSize(1));
scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize));
}
TEST_F(DiskCacheEntryTest, SimpleCacheErrorThenDoom) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
int size_unused;
ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size_unused));
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
ScopedEntryPtr entry_closer(entry);
const int kReadBufferSize = 200;
EXPECT_GE(kReadBufferSize, entry->GetDataSize(1));
scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
EXPECT_EQ(net::ERR_CACHE_CHECKSUM_MISMATCH,
ReadData(entry, 1, 0, read_buffer.get(), kReadBufferSize));
entry->Doom();
}
bool TruncatePath(const base::FilePath& file_path, int64 length) {
base::File file(file_path, base::File::FLAG_WRITE | base::File::FLAG_OPEN);
if (!file.IsValid())
return false;
return file.SetLength(length);
}
TEST_F(DiskCacheEntryTest, SimpleCacheNoEOF) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
disk_cache::Entry* null = NULL;
EXPECT_NE(null, entry);
entry->Close();
entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
entry->Close();
entry = NULL;
int kTruncationBytes = -implicit_cast<int>(sizeof(disk_cache::SimpleFileEOF));
const base::FilePath entry_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
const int64 invalid_size =
disk_cache::simple_util::GetFileSizeFromKeyAndDataSize(key,
kTruncationBytes);
EXPECT_TRUE(TruncatePath(entry_path, invalid_size));
EXPECT_EQ(net::ERR_FAILED, OpenEntry(key, &entry));
DisableIntegrityCheck();
}
TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsBasic) {
SetCacheType(net::APP_CACHE);
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* const null_entry = NULL;
disk_cache::Entry* entry = NULL;
EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
ASSERT_NE(null_entry, entry);
ScopedEntryPtr entry_closer(entry);
const int kBufferSize = 10;
scoped_refptr<net::IOBufferWithSize> write_buffer(
new net::IOBufferWithSize(kBufferSize));
CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
EXPECT_EQ(
write_buffer->size(),
WriteData(entry, 1, 0, write_buffer.get(), write_buffer->size(), false));
scoped_refptr<net::IOBufferWithSize> read_buffer(
new net::IOBufferWithSize(kBufferSize));
EXPECT_EQ(read_buffer->size(),
ReadData(entry, 1, 0, read_buffer.get(), read_buffer->size()));
}
TEST_F(DiskCacheEntryTest, SimpleCacheNonOptimisticOperationsDontBlock) {
SetCacheType(net::APP_CACHE);
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* const null_entry = NULL;
MessageLoopHelper helper;
CallbackTest create_callback(&helper, false);
int expected_callback_runs = 0;
const int kBufferSize = 10;
scoped_refptr<net::IOBufferWithSize> write_buffer(
new net::IOBufferWithSize(kBufferSize));
disk_cache::Entry* entry = NULL;
EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
ASSERT_NE(null_entry, entry);
ScopedEntryPtr entry_closer(entry);
CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
CallbackTest write_callback(&helper, false);
int ret = entry->WriteData(
1,
0,
write_buffer.get(),
write_buffer->size(),
base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)),
false);
ASSERT_EQ(net::ERR_IO_PENDING, ret);
helper.WaitUntilCacheIoFinished(++expected_callback_runs);
}
TEST_F(DiskCacheEntryTest,
SimpleCacheNonOptimisticOperationsBasicsWithoutWaiting) {
SetCacheType(net::APP_CACHE);
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* const null_entry = NULL;
MessageLoopHelper helper;
disk_cache::Entry* entry = NULL;
EXPECT_EQ(net::OK, CreateEntry("my key", &entry));
ASSERT_NE(null_entry, entry);
ScopedEntryPtr entry_closer(entry);
const int kBufferSize = 10;
scoped_refptr<net::IOBufferWithSize> write_buffer(
new net::IOBufferWithSize(kBufferSize));
CacheTestFillBuffer(write_buffer->data(), write_buffer->size(), false);
CallbackTest write_callback(&helper, false);
int ret = entry->WriteData(
1,
0,
write_buffer.get(),
write_buffer->size(),
base::Bind(&CallbackTest::Run, base::Unretained(&write_callback)),
false);
EXPECT_EQ(net::ERR_IO_PENDING, ret);
int expected_callback_runs = 1;
scoped_refptr<net::IOBufferWithSize> read_buffer(
new net::IOBufferWithSize(kBufferSize));
CallbackTest read_callback(&helper, false);
ret = entry->ReadData(
1,
0,
read_buffer.get(),
read_buffer->size(),
base::Bind(&CallbackTest::Run, base::Unretained(&read_callback)));
EXPECT_EQ(net::ERR_IO_PENDING, ret);
++expected_callback_runs;
helper.WaitUntilCacheIoFinished(expected_callback_runs);
ASSERT_EQ(read_buffer->size(), write_buffer->size());
EXPECT_EQ(
0,
memcmp(read_buffer->data(), write_buffer->data(), read_buffer->size()));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
MessageLoopHelper helper;
CallbackTest callback1(&helper, false);
CallbackTest callback2(&helper, false);
CallbackTest callback3(&helper, false);
CallbackTest callback4(&helper, false);
CallbackTest callback5(&helper, false);
int expected = 0;
const int kSize1 = 10;
const int kSize2 = 20;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize2));
scoped_refptr<net::IOBuffer> buffer2_read(new net::IOBuffer(kSize2));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
CacheTestFillBuffer(buffer2->data(), kSize2, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry,
base::Bind(&CallbackTest::Run,
base::Unretained(&callback1))));
EXPECT_NE(null, entry);
ScopedEntryPtr entry_closer(entry);
int ret = entry->WriteData(
1,
0,
buffer1.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback2)),
false);
EXPECT_TRUE(kSize1 == ret || net::ERR_IO_PENDING == ret);
if (net::ERR_IO_PENDING == ret)
expected++;
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(
1,
0,
buffer1_read.get(),
kSize1,
base::Bind(&CallbackTest::Run, base::Unretained(&callback3))));
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1));
EXPECT_EQ(kSize2,
entry->WriteData(
1,
0,
buffer2.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback4)),
false));
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(
1,
0,
buffer2_read.get(),
kSize2,
base::Bind(&CallbackTest::Run, base::Unretained(&callback5))));
expected++;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(0, memcmp(buffer2->data(), buffer2_read->data(), kSize2));
EXPECT_NE(entry, null);
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic2) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
MessageLoopHelper helper;
CallbackTest callback1(&helper, false);
CallbackTest callback2(&helper, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry,
base::Bind(&CallbackTest::Run,
base::Unretained(&callback1))));
EXPECT_NE(null, entry);
ScopedEntryPtr entry_closer(entry);
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::ERR_IO_PENDING,
cache_->OpenEntry(key, &entry2,
base::Bind(&CallbackTest::Run,
base::Unretained(&callback2))));
ASSERT_TRUE(helper.WaitUntilCacheIoFinished(1));
EXPECT_NE(null, entry2);
EXPECT_EQ(entry, entry2);
entry->Close();
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic3) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
EXPECT_NE(null, entry);
entry->Close();
net::TestCompletionCallback cb;
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::ERR_IO_PENDING,
cache_->OpenEntry(key, &entry2, cb.callback()));
ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
ScopedEntryPtr entry_closer(entry2);
EXPECT_NE(null, entry2);
EXPECT_EQ(entry, entry2);
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic4) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
net::TestCompletionCallback cb;
const int kSize1 = 10;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
EXPECT_NE(null, entry);
entry->Close();
EXPECT_EQ(
net::ERR_IO_PENDING,
entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
EXPECT_EQ(net::ERR_FAILED, cb.GetResult(net::ERR_IO_PENDING));
base::MessageLoop::current()->RunUntilIdle();
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::ERR_IO_PENDING,
cache_->OpenEntry(key, &entry2, cb.callback()));
ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_NE(null, entry2);
disk_cache::Entry* entry3 = NULL;
ASSERT_EQ(net::ERR_IO_PENDING,
cache_->OpenEntry(key, &entry3, cb.callback()));
ASSERT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_NE(null, entry3);
EXPECT_EQ(entry2, entry3);
entry3->Close();
EXPECT_EQ(kSize1,
entry2->WriteData(
1, 0, buffer1.get(), kSize1, net::CompletionCallback(), false));
EXPECT_EQ(net::ERR_IO_PENDING,
entry2->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry2)->HasOneRef());
entry2->Close();
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic5) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
net::TestCompletionCallback cb;
const int kSize1 = 10;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
EXPECT_NE(null, entry);
ScopedEntryPtr entry_closer(entry);
entry->Doom();
EXPECT_EQ(
net::ERR_IO_PENDING,
entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(1, 0, buffer1.get(), kSize1, cb.callback()));
EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimistic6) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
net::TestCompletionCallback cb;
const int kSize1 = 10;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
scoped_refptr<net::IOBuffer> buffer1_read(new net::IOBuffer(kSize1));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
EXPECT_NE(null, entry);
ScopedEntryPtr entry_closer(entry);
EXPECT_EQ(
net::ERR_IO_PENDING,
entry->WriteData(1, 0, buffer1.get(), kSize1, cb.callback(), false));
EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
entry->Doom();
entry->Doom();
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(1, 0, buffer1_read.get(), kSize1, cb.callback()));
EXPECT_EQ(kSize1, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read->data(), kSize1));
entry->Doom();
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticWriteReleases) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
ASSERT_TRUE(entry);
ScopedEntryPtr entry_closer(entry);
const int kWriteSize = 512;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kWriteSize));
EXPECT_TRUE(buffer1->HasOneRef());
CacheTestFillBuffer(buffer1->data(), kWriteSize, false);
EXPECT_EQ(kWriteSize,
WriteData(entry, 1, 0, buffer1.get(), kWriteSize, false));
EXPECT_TRUE(buffer1->HasOneRef());
EXPECT_EQ(
kWriteSize,
entry->WriteData(
1, 0, buffer1.get(), kWriteSize, net::CompletionCallback(), false));
EXPECT_TRUE(buffer1->HasOneRef());
}
TEST_F(DiskCacheEntryTest, SimpleCacheCreateDoomRace) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
net::TestCompletionCallback cb;
const int kSize1 = 10;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
CacheTestFillBuffer(buffer1->data(), kSize1, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
EXPECT_NE(null, entry);
EXPECT_EQ(net::ERR_IO_PENDING, cache_->DoomEntry(key, cb.callback()));
EXPECT_EQ(net::OK, cb.GetResult(net::ERR_IO_PENDING));
EXPECT_EQ(
kSize1,
entry->WriteData(0, 0, buffer1.get(), kSize1, cb.callback(), false));
entry->Close();
base::MessageLoop::current()->RunUntilIdle();
for (int i = 0; i < disk_cache::kSimpleEntryFileCount; ++i) {
base::FilePath entry_file_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, i));
base::File::Info info;
EXPECT_FALSE(base::GetFileInfo(entry_file_path, &info));
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateRace) {
SetCacheType(net::APP_CACHE);
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
net::TestCompletionCallback create_callback;
disk_cache::Entry* entry1 = NULL;
ASSERT_EQ(net::OK,
create_callback.GetResult(
cache_->CreateEntry(key, &entry1, create_callback.callback())));
ScopedEntryPtr entry1_closer(entry1);
EXPECT_NE(null, entry1);
net::TestCompletionCallback doom_callback;
EXPECT_EQ(net::ERR_IO_PENDING,
cache_->DoomEntry(key, doom_callback.callback()));
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::OK,
create_callback.GetResult(
cache_->CreateEntry(key, &entry2, create_callback.callback())));
ScopedEntryPtr entry2_closer(entry2);
EXPECT_EQ(net::OK, doom_callback.GetResult(net::ERR_IO_PENDING));
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomDoom) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
disk_cache::Entry* entry1 = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
ScopedEntryPtr entry1_closer(entry1);
EXPECT_NE(null, entry1);
EXPECT_EQ(net::OK, DoomEntry(key));
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry2));
ScopedEntryPtr entry2_closer(entry2);
EXPECT_NE(null, entry2);
disk_cache::SimpleEntryImpl* simple_entry1 =
static_cast<disk_cache::SimpleEntryImpl*>(entry1);
net::TestCompletionCallback cb;
EXPECT_EQ(net::OK,
cb.GetResult(simple_entry1->DoomEntry(cb.callback())));
disk_cache::Entry* entry3 = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry3));
ScopedEntryPtr entry3_closer(entry3);
EXPECT_NE(null, entry3);
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomCreateDoom) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
disk_cache::Entry* entry1 = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry1));
ScopedEntryPtr entry1_closer(entry1);
EXPECT_NE(null, entry1);
entry1->Doom();
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry2));
ScopedEntryPtr entry2_closer(entry2);
EXPECT_NE(null, entry2);
entry2->Doom();
}
TEST_F(DiskCacheEntryTest, SimpleCacheOptimisticCreateFailsOnOpen) {
SetSimpleCacheMode();
InitCache();
const std::string key = "the key";
net::TestCompletionCallback cb;
disk_cache::Entry* entry = NULL;
disk_cache::Entry* entry2 = NULL;
EXPECT_TRUE(disk_cache::simple_util::CreateCorruptFileForTests(
key, cache_path_));
EXPECT_EQ(net::OK, cache_->CreateEntry(key, &entry, cb.callback()));
ASSERT_TRUE(entry);
ScopedEntryPtr entry_closer(entry);
ASSERT_NE(net::OK, OpenEntry(key, &entry2));
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
DisableIntegrityCheck();
}
TEST_F(DiskCacheEntryTest, SimpleCacheEvictOldEntries) {
const int kMaxSize = 200 * 1024;
const int kWriteSize = kMaxSize / 10;
const int kNumExtraEntries = 12;
SetSimpleCacheMode();
SetMaxSize(kMaxSize);
InitCache();
std::string key1("the first key");
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key1, &entry));
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kWriteSize));
CacheTestFillBuffer(buffer->data(), kWriteSize, false);
EXPECT_EQ(kWriteSize,
WriteData(entry, 1, 0, buffer.get(), kWriteSize, false));
entry->Close();
AddDelay();
std::string key2("the key prefix");
for (int i = 0; i < kNumExtraEntries; i++) {
ASSERT_EQ(net::OK, CreateEntry(key2 + base::StringPrintf("%d", i), &entry));
ScopedEntryPtr entry_closer(entry);
EXPECT_EQ(kWriteSize,
WriteData(entry, 1, 0, buffer.get(), kWriteSize, false));
}
ASSERT_NE(net::OK, OpenEntry(key1, &entry))
<< "Should have evicted the old entry";
for (int i = 0; i < 2; i++) {
int entry_no = kNumExtraEntries - i - 1;
ASSERT_EQ(net::OK, OpenEntry(key2 + base::StringPrintf("%d", entry_no),
&entry))
<< "Should not have evicted fresh entry " << entry_no;
entry->Close();
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheInFlightTruncate) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
const int kBufferSize = 1024;
scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(kBufferSize,
WriteData(entry, 1, 0, write_buffer.get(), kBufferSize, false));
entry->Close();
entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
ScopedEntryPtr entry_closer(entry);
MessageLoopHelper helper;
int expected = 0;
const int kReadBufferSize = 512;
scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize));
CallbackTest read_callback(&helper, false);
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(1,
0,
read_buffer.get(),
kReadBufferSize,
base::Bind(&CallbackTest::Run,
base::Unretained(&read_callback))));
++expected;
scoped_refptr<net::IOBuffer>
truncate_buffer(new net::IOBuffer(kReadBufferSize));
CacheTestFillBuffer(truncate_buffer->data(), kReadBufferSize, false);
CallbackTest truncate_callback(&helper, false);
EXPECT_EQ(net::ERR_IO_PENDING,
entry->WriteData(1,
0,
truncate_buffer.get(),
kReadBufferSize,
base::Bind(&CallbackTest::Run,
base::Unretained(&truncate_callback)),
true));
++expected;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(kReadBufferSize, read_callback.last_result());
EXPECT_EQ(kReadBufferSize, truncate_callback.last_result());
EXPECT_EQ(0,
memcmp(write_buffer->data(), read_buffer->data(), kReadBufferSize));
}
TEST_F(DiskCacheEntryTest, SimpleCacheInFlightRead) {
SetSimpleCacheMode();
InitCache();
const char key[] = "the first key";
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK,
cache_->CreateEntry(key, &entry, net::CompletionCallback()));
ScopedEntryPtr entry_closer(entry);
const int kBufferSize = 1024;
scoped_refptr<net::IOBuffer> write_buffer(new net::IOBuffer(kBufferSize));
CacheTestFillBuffer(write_buffer->data(), kBufferSize, false);
MessageLoopHelper helper;
int expected = 0;
CallbackTest write_callback(&helper, false);
EXPECT_EQ(net::ERR_IO_PENDING,
entry->WriteData(1,
0,
write_buffer.get(),
kBufferSize,
base::Bind(&CallbackTest::Run,
base::Unretained(&write_callback)),
true));
++expected;
scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kBufferSize));
CallbackTest read_callback(&helper, false);
EXPECT_EQ(net::ERR_IO_PENDING,
entry->ReadData(1,
0,
read_buffer.get(),
kBufferSize,
base::Bind(&CallbackTest::Run,
base::Unretained(&read_callback))));
++expected;
EXPECT_TRUE(helper.WaitUntilCacheIoFinished(expected));
EXPECT_EQ(kBufferSize, write_callback.last_result());
EXPECT_EQ(kBufferSize, read_callback.last_result());
EXPECT_EQ(0, memcmp(write_buffer->data(), read_buffer->data(), kBufferSize));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOpenCreateRaceWithNoIndex) {
SetSimpleCacheMode();
DisableSimpleCacheWaitForIndex();
DisableIntegrityCheck();
InitCache();
disk_cache::Entry* entry1;
net::TestCompletionCallback cb1;
disk_cache::Entry* entry2;
net::TestCompletionCallback cb2;
int rv1 = cache_->OpenEntry("key", &entry1, cb1.callback());
int rv2 = cache_->CreateEntry("key", &entry2, cb2.callback());
EXPECT_EQ(net::ERR_FAILED, cb1.GetResult(rv1));
ASSERT_EQ(net::OK, cb2.GetResult(rv2));
entry2->Close();
}
TEST_F(DiskCacheEntryTest, DISABLED_SimpleCacheMultipleReadersCheckCRC) {
SetSimpleCacheMode();
InitCache();
const char key[] = "key";
int size;
ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size));
scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size));
scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size));
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(1, ReadData(entry, 0, 0, read_buffer1.get(), 1));
disk_cache::Entry* entry2 = NULL;
EXPECT_EQ(net::OK, OpenEntry(key, &entry2));
EXPECT_EQ(1, ReadData(entry2, 0, 0, read_buffer2.get(), 1));
EXPECT_EQ(1, ReadData(entry2, 0, 1, read_buffer2.get(), 1));
entry2->Close();
EXPECT_GT(0, ReadData(entry, 0, 1, read_buffer1.get(), size));
entry->Close();
DisableIntegrityCheck();
}
TEST_F(DiskCacheEntryTest, SimpleCacheMultipleReadersCheckCRC2) {
SetSimpleCacheMode();
InitCache();
const char key[] = "key";
int size;
ASSERT_TRUE(SimpleCacheMakeBadChecksumEntry(key, &size));
scoped_refptr<net::IOBuffer> read_buffer1(new net::IOBuffer(size));
scoped_refptr<net::IOBuffer> read_buffer2(new net::IOBuffer(size));
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
ScopedEntryPtr entry_closer(entry);
EXPECT_EQ(1, ReadData(entry, 1, 0, read_buffer1.get(), 1));
disk_cache::Entry* entry2 = NULL;
EXPECT_EQ(net::OK, OpenEntry(key, &entry2));
ScopedEntryPtr entry2_closer(entry2);
EXPECT_EQ(1, ReadData(entry2, 1, 0, read_buffer2.get(), 1));
EXPECT_GT(0, ReadData(entry, 1, 1, read_buffer1.get(), size));
EXPECT_GT(0, ReadData(entry2, 1, 1, read_buffer2.get(), 1));
DisableIntegrityCheck();
}
TEST_F(DiskCacheEntryTest, SimpleCacheReadCombineCRC) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
const int kHalfSize = 200;
const int kSize = 2 * kHalfSize;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_NE(null, entry);
EXPECT_EQ(kSize, WriteData(entry, 1, 0, buffer1.get(), kSize, false));
entry->Close();
disk_cache::Entry* entry2 = NULL;
ASSERT_EQ(net::OK, OpenEntry(key, &entry2));
EXPECT_EQ(entry, entry2);
int offset = 0;
int buf_len = kHalfSize;
scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(buf_len));
EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read1.get(), buf_len));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len));
offset = buf_len;
buf_len = kHalfSize;
scoped_refptr<net::IOBuffer> buffer1_read2(new net::IOBuffer(buf_len));
EXPECT_EQ(buf_len, ReadData(entry2, 1, offset, buffer1_read2.get(), buf_len));
char* buffer1_data = buffer1->data() + offset;
EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len));
EXPECT_NE(entry, null);
EXPECT_TRUE(
static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
entry->Close();
entry = NULL;
}
TEST_F(DiskCacheEntryTest, SimpleCacheNonSequentialWrite) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
const int kHalfSize = 200;
const int kSize = 2 * kHalfSize;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, false);
char* buffer1_data = buffer1->data() + kHalfSize;
memcpy(buffer2->data(), buffer1_data, kHalfSize);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
entry->Close();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_NE(null, entry);
int offset = kHalfSize;
int buf_len = kHalfSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer2.get(), buf_len, false));
offset = 0;
buf_len = kHalfSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer1.get(), buf_len, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(kSize));
EXPECT_EQ(kSize, ReadData(entry, i, 0, buffer1_read1.get(), kSize));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize));
ASSERT_NE(entry, null);
EXPECT_TRUE(static_cast<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
entry->Close();
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheStream1SizeChanges) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* entry = NULL;
const char key[] = "the key";
const int kSize = 100;
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer_read(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_TRUE(entry);
EXPECT_EQ(kSize, WriteData(entry, 0, 0, buffer.get(), kSize, false));
EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
int stream1_size = 100;
EXPECT_EQ(0, WriteData(entry, 1, stream1_size, buffer.get(), 0, false));
EXPECT_EQ(stream1_size, entry->GetDataSize(1));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
base::FilePath entry_file0_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(key, 0));
base::File entry_file0(entry_file0_path,
base::File::FLAG_READ | base::File::FLAG_OPEN);
ASSERT_TRUE(entry_file0.IsValid());
int data_size[disk_cache::kSimpleEntryStreamCount] = {kSize, stream1_size, 0};
int sparse_data_size = 0;
disk_cache::SimpleEntryStat entry_stat(
base::Time::Now(), base::Time::Now(), data_size, sparse_data_size);
int eof_offset = entry_stat.GetEOFOffsetInFile(key, 0);
disk_cache::SimpleFileEOF eof_record;
ASSERT_EQ(static_cast<int>(sizeof(eof_record)),
entry_file0.Read(eof_offset, reinterpret_cast<char*>(&eof_record),
sizeof(eof_record)));
EXPECT_EQ(disk_cache::kSimpleFinalMagicNumber, eof_record.final_magic_number);
EXPECT_TRUE((eof_record.flags & disk_cache::SimpleFileEOF::FLAG_HAS_CRC32) ==
disk_cache::SimpleFileEOF::FLAG_HAS_CRC32);
buffer_read = new net::IOBuffer(kSize);
EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
stream1_size = 50;
EXPECT_EQ(0, WriteData(entry, 1, stream1_size, buffer.get(), 0, true));
EXPECT_EQ(stream1_size, entry->GetDataSize(1));
entry->Close();
buffer_read = new net::IOBuffer(kSize);
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(kSize, ReadData(entry, 0, 0, buffer_read.get(), kSize));
EXPECT_EQ(0, memcmp(buffer->data(), buffer_read->data(), kSize));
entry->Close();
entry = NULL;
}
TEST_F(DiskCacheEntryTest, SimpleCacheCRCRewrite) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
const int kHalfSize = 200;
const int kSize = 2 * kHalfSize;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kHalfSize));
CacheTestFillBuffer(buffer1->data(), kSize, false);
CacheTestFillBuffer(buffer2->data(), kHalfSize, false);
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_NE(null, entry);
entry->Close();
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
int offset = 0;
int buf_len = kSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer1.get(), buf_len, false));
offset = kHalfSize;
buf_len = kHalfSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer2.get(), buf_len, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
scoped_refptr<net::IOBuffer> buffer1_read1(new net::IOBuffer(kSize));
EXPECT_EQ(kSize, ReadData(entry, i, 0, buffer1_read1.get(), kSize));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kHalfSize));
EXPECT_EQ(
0,
memcmp(buffer2->data(), buffer1_read1->data() + kHalfSize, kHalfSize));
entry->Close();
}
}
bool DiskCacheEntryTest::SimpleCacheThirdStreamFileExists(const char* key) {
int third_stream_file_index =
disk_cache::simple_util::GetFileIndexFromStreamIndex(2);
base::FilePath third_stream_file_path = cache_path_.AppendASCII(
disk_cache::simple_util::GetFilenameFromKeyAndFileIndex(
key, third_stream_file_index));
return PathExists(third_stream_file_path);
}
void DiskCacheEntryTest::SyncDoomEntry(const char* key) {
net::TestCompletionCallback callback;
cache_->DoomEntry(key, callback.callback());
callback.WaitForResult();
}
TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream1) {
SetSimpleCacheMode();
InitCache();
const char key[] = "key";
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
entry->Close();
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
SyncDoomEntry(key);
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream2) {
SetSimpleCacheMode();
InitCache();
const int kHalfSize = 8;
const int kSize = kHalfSize * 2;
const char key[] = "key";
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kHalfSize, false);
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(0, WriteData(entry, 2, 0, buffer, 0, true));
entry->Close();
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
SyncDoomEntry(key);
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream3) {
SetSimpleCacheMode();
InitCache();
const int kHalfSize = 8;
const int kSize = kHalfSize * 2;
const char key[] = "key";
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kHalfSize, false);
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1, kHalfSize, true));
entry->Close();
EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key));
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_EQ(kHalfSize, ReadData(entry, 2, 0, buffer2, kSize));
EXPECT_EQ(0, memcmp(buffer1->data(), buffer2->data(), kHalfSize));
entry->Close();
EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key));
SyncDoomEntry(key);
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream4) {
SetSimpleCacheMode();
InitCache();
const int kHalfSize = 8;
const int kSize = kHalfSize * 2;
const char key[] = "key";
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kHalfSize, false);
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_EQ(kHalfSize, WriteData(entry, 2, 0, buffer1, kHalfSize, true));
EXPECT_EQ(0, WriteData(entry, 2, 0, buffer1, 0, true));
entry->Close();
EXPECT_TRUE(SimpleCacheThirdStreamFileExists(key));
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
EXPECT_EQ(0, ReadData(entry, 2, 0, buffer2, kSize));
entry->Close();
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
SyncDoomEntry(key);
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
}
TEST_F(DiskCacheEntryTest, SimpleCacheOmittedThirdStream5) {
SetSimpleCacheMode();
InitCache();
const int kHalfSize = 8;
const int kSize = kHalfSize * 2;
const char key[] = "key";
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kHalfSize, false);
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
entry->Doom();
WriteData(entry, 2, 0, buffer, kHalfSize, true);
entry->Close();
EXPECT_FALSE(SimpleCacheThirdStreamFileExists(key));
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomOptimisticWritesRace) {
SetSimpleCacheMode();
InitCache();
disk_cache::Entry* null = NULL;
const char key[] = "the first key";
const int kSize = 200;
scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize));
scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer1->data(), kSize, false);
CacheTestFillBuffer(buffer2->data(), kSize, false);
for (int i = 0; i < disk_cache::kSimpleEntryStreamCount; ++i) {
ASSERT_EQ(net::OK, DoomAllEntries());
disk_cache::Entry* entry = NULL;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_NE(null, entry);
entry->Close();
entry = NULL;
ASSERT_EQ(net::OK, DoomAllEntries());
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_NE(null, entry);
int offset = 0;
int buf_len = kSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer1.get(), buf_len, false));
offset = kSize;
EXPECT_EQ(buf_len,
WriteData(entry, i, offset, buffer2.get(), buf_len, false));
entry->Close();
ASSERT_EQ(net::OK, OpenEntry(key, &entry));
EXPECT_NE(null, entry);
entry->Close();
entry = NULL;
}
}
TEST_F(DiskCacheEntryTest, SimpleCacheBasicSparseIO) {
SetSimpleCacheMode();
InitCache();
BasicSparseIO();
}
TEST_F(DiskCacheEntryTest, SimpleCacheHugeSparseIO) {
SetSimpleCacheMode();
InitCache();
HugeSparseIO();
}
TEST_F(DiskCacheEntryTest, SimpleCacheGetAvailableRange) {
SetSimpleCacheMode();
InitCache();
GetAvailableRange();
}
TEST_F(DiskCacheEntryTest, DISABLED_SimpleCacheCouldBeSparse) {
SetSimpleCacheMode();
InitCache();
CouldBeSparse();
}
TEST_F(DiskCacheEntryTest, SimpleCacheUpdateSparseEntry) {
SetSimpleCacheMode();
InitCache();
UpdateSparseEntry();
}
TEST_F(DiskCacheEntryTest, SimpleCacheDoomSparseEntry) {
SetSimpleCacheMode();
InitCache();
DoomSparseEntry();
}
TEST_F(DiskCacheEntryTest, SimpleCachePartialSparseEntry) {
SetSimpleCacheMode();
InitCache();
PartialSparseEntry();
}
TEST_F(DiskCacheEntryTest, SimpleCacheTruncateLargeSparseFile) {
const int kSize = 1024;
SetSimpleCacheMode();
SetMaxSize(kSize * 15);
InitCache();
const char key[] = "key";
disk_cache::Entry* null = NULL;
disk_cache::Entry* entry;
ASSERT_EQ(net::OK, CreateEntry(key, &entry));
EXPECT_NE(null, entry);
scoped_refptr<net::IOBuffer> buffer(new net::IOBuffer(kSize));
CacheTestFillBuffer(buffer->data(), kSize, false);
net::TestCompletionCallback callback;
int ret;
ret = entry->ReadSparseData(0, buffer, kSize, callback.callback());
EXPECT_EQ(0, callback.GetResult(ret));
ret = entry->ReadSparseData(kSize, buffer, kSize, callback.callback());
EXPECT_EQ(0, callback.GetResult(ret));
ret = entry->WriteSparseData(0, buffer, kSize, callback.callback());
EXPECT_EQ(kSize, callback.GetResult(ret));
ret = entry->ReadSparseData(0, buffer, kSize, callback.callback());
EXPECT_EQ(kSize, callback.GetResult(ret));
ret = entry->WriteSparseData(kSize, buffer, kSize, callback.callback());
EXPECT_EQ(kSize, callback.GetResult(ret));
ret = entry->ReadSparseData(kSize, buffer, kSize, callback.callback());
EXPECT_EQ(kSize, callback.GetResult(ret));
ret = entry->ReadSparseData(0, buffer, kSize, callback.callback());
EXPECT_EQ(0, callback.GetResult(ret));
entry->Close();
}
#endif