summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-07 15:43:41 +0000
committerfelipeg@chromium.org <felipeg@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-06-07 15:43:41 +0000
commitfed609fe1e0f3952503412d26b25c556eb578872 (patch)
tree084674d84842fb301b940e19d4eb18d48f67dd4a
parentf88a5e6669c3601b0a8267d166b3752d55df9fef (diff)
downloadchromium_src-fed609fe1e0f3952503412d26b25c556eb578872.zip
chromium_src-fed609fe1e0f3952503412d26b25c556eb578872.tar.gz
chromium_src-fed609fe1e0f3952503412d26b25c556eb578872.tar.bz2
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
-rw-r--r--net/disk_cache/entry_unittest.cc98
1 files changed, 98 insertions, 0 deletions
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<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, 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<net::IOBuffer> 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<net::IOBuffer> 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<disk_cache::SimpleEntryImpl*>(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<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));
+ 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<net::IOBuffer> 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<disk_cache::SimpleEntryImpl*>(entry)->HasOneRef());
+ entry->Close();
+ entry = NULL;
+}
+
#endif // defined(OS_POSIX)