From fed609fe1e0f3952503412d26b25c556eb578872 Mon Sep 17 00:00:00 2001 From: "felipeg@chromium.org" Date: Fri, 7 Jun 2013 15:43:41 +0000 Subject: Add a SimpleCache Read unittest. Test if we can read a subset of data in sequence and still the combine CRC is calculated correctly. BUG=239223 Review URL: https://chromiumcodereview.appspot.com/16224004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204852 0039d316-1c4b-4281-b951-d872f2087c98 --- net/disk_cache/entry_unittest.cc | 98 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/net/disk_cache/entry_unittest.cc b/net/disk_cache/entry_unittest.cc index c683745..371ce3e 100644 --- a/net/disk_cache/entry_unittest.cc +++ b/net/disk_cache/entry_unittest.cc @@ -3198,4 +3198,102 @@ TEST_F(DiskCacheEntryTest, SimpleCacheMultipleReadersCheckCRC2) { DisableIntegrityCheck(); } +// Test if we can sequentially read each subset of the data until all the data +// is read, then the CRC is calculated correctly and the reads are successful. +TEST_F(DiskCacheEntryTest, SimpleCacheReadCombineCRC) { + // Test sequence: + // Create, Write, Read (first half of data), Read (second half of data), + // Close. + SetSimpleCacheMode(); + InitCache(); + disk_cache::Entry* null = NULL; + const char key[] = "the first key"; + + const int kHalfSize = 200; + const int kSize = 2 * kHalfSize; + scoped_refptr 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, 0, 0, buffer1, kSize, false)); + entry->Close(); + + disk_cache::Entry* entry2 = NULL; + ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); + EXPECT_EQ(entry, entry2); + + // Read the first half of the data. + int offset = 0; + int buf_len = kHalfSize; + scoped_refptr buffer1_read1(new net::IOBuffer(buf_len)); + EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read1, buf_len)); + EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), buf_len)); + + // Read the second half of the data. + offset = buf_len; + buf_len = kHalfSize; + scoped_refptr buffer1_read2(new net::IOBuffer(buf_len)); + EXPECT_EQ(buf_len, ReadData(entry2, 0, offset, buffer1_read2, buf_len)); + char* buffer1_data = buffer1->data() + offset; + EXPECT_EQ(0, memcmp(buffer1_data, buffer1_read2->data(), buf_len)); + + // Check that we are not leaking. + EXPECT_NE(entry, null); + EXPECT_TRUE( + static_cast(entry)->HasOneRef()); + entry->Close(); + entry = NULL; +} + +// Test if we can write the data not in sequence and read correctly. In +// this case the CRC will not be present. +TEST_F(DiskCacheEntryTest, SimpleCacheNonSequentialWrite) { + // Test sequence: + // Create, Write (second half of data), Write (first half of data), Read, + // Close. + SetSimpleCacheMode(); + InitCache(); + disk_cache::Entry* null = NULL; + const char key[] = "the first key"; + + const int kHalfSize = 200; + const int kSize = 2 * kHalfSize; + scoped_refptr buffer1(new net::IOBuffer(kSize)); + scoped_refptr 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)); + EXPECT_NE(null, entry); + + int offset = kHalfSize; + int buf_len = kHalfSize; + + EXPECT_EQ(buf_len, WriteData(entry, 0, offset, buffer2, buf_len, false)); + offset = 0; + buf_len = kHalfSize; + EXPECT_EQ(buf_len, WriteData(entry, 0, offset, buffer1, buf_len, false)); + entry->Close(); + + disk_cache::Entry* entry2 = NULL; + ASSERT_EQ(net::OK, OpenEntry(key, &entry2)); + EXPECT_EQ(entry, entry2); + + scoped_refptr buffer1_read1(new net::IOBuffer(kSize)); + EXPECT_EQ(kSize, ReadData(entry2, 0, 0, buffer1_read1, kSize)); + EXPECT_EQ(0, memcmp(buffer1->data(), buffer1_read1->data(), kSize)); + + // Check that we are not leaking. + ASSERT_NE(entry, null); + EXPECT_TRUE( + static_cast(entry)->HasOneRef()); + entry->Close(); + entry = NULL; +} + #endif // defined(OS_POSIX) -- cgit v1.1