1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "base/basictypes.h" 6 #include "base/files/file_path.h" 7 #include "base/strings/string_util.h" 8 #include "net/disk_cache/disk_cache_test_base.h" 9 #include "net/disk_cache/disk_cache_test_util.h" 10 #include "net/disk_cache/file_block.h" 11 #include "net/disk_cache/mapped_file.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace { 15 16 // Implementation of FileIOCallback for the tests. 17 class FileCallbackTest: public disk_cache::FileIOCallback { 18 public: 19 FileCallbackTest(int id, MessageLoopHelper* helper, int* max_id) 20 : id_(id), 21 helper_(helper), 22 max_id_(max_id) { 23 } 24 virtual ~FileCallbackTest() {} 25 26 virtual void OnFileIOComplete(int bytes_copied) OVERRIDE; 27 28 private: 29 int id_; 30 MessageLoopHelper* helper_; 31 int* max_id_; 32 }; 33 34 void FileCallbackTest::OnFileIOComplete(int bytes_copied) { 35 if (id_ > *max_id_) { 36 NOTREACHED(); 37 helper_->set_callback_reused_error(true); 38 } 39 40 helper_->CallbackWasCalled(); 41 } 42 43 class TestFileBlock : public disk_cache::FileBlock { 44 public: 45 TestFileBlock() { 46 CacheTestFillBuffer(buffer_, sizeof(buffer_), false); 47 } 48 virtual ~TestFileBlock() {} 49 50 // FileBlock interface. 51 virtual void* buffer() const OVERRIDE { return const_cast<char*>(buffer_); } 52 virtual size_t size() const OVERRIDE { return sizeof(buffer_); } 53 virtual int offset() const OVERRIDE { return 1024; } 54 55 private: 56 char buffer_[20]; 57 }; 58 59 } // namespace 60 61 TEST_F(DiskCacheTest, MappedFile_SyncIO) { 62 base::FilePath filename = cache_path_.AppendASCII("a_test"); 63 scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile); 64 ASSERT_TRUE(CreateCacheTestFile(filename)); 65 ASSERT_TRUE(file->Init(filename, 8192)); 66 67 char buffer1[20]; 68 char buffer2[20]; 69 CacheTestFillBuffer(buffer1, sizeof(buffer1), false); 70 base::strlcpy(buffer1, "the data", arraysize(buffer1)); 71 EXPECT_TRUE(file->Write(buffer1, sizeof(buffer1), 8192)); 72 EXPECT_TRUE(file->Read(buffer2, sizeof(buffer2), 8192)); 73 EXPECT_STREQ(buffer1, buffer2); 74 } 75 76 TEST_F(DiskCacheTest, MappedFile_AsyncIO) { 77 base::FilePath filename = cache_path_.AppendASCII("a_test"); 78 scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile); 79 ASSERT_TRUE(CreateCacheTestFile(filename)); 80 ASSERT_TRUE(file->Init(filename, 8192)); 81 82 int max_id = 0; 83 MessageLoopHelper helper; 84 FileCallbackTest callback(1, &helper, &max_id); 85 86 char buffer1[20]; 87 char buffer2[20]; 88 CacheTestFillBuffer(buffer1, sizeof(buffer1), false); 89 base::strlcpy(buffer1, "the data", arraysize(buffer1)); 90 bool completed; 91 EXPECT_TRUE(file->Write(buffer1, sizeof(buffer1), 1024 * 1024, &callback, 92 &completed)); 93 int expected = completed ? 0 : 1; 94 95 max_id = 1; 96 helper.WaitUntilCacheIoFinished(expected); 97 98 EXPECT_TRUE(file->Read(buffer2, sizeof(buffer2), 1024 * 1024, &callback, 99 &completed)); 100 if (!completed) 101 expected++; 102 103 helper.WaitUntilCacheIoFinished(expected); 104 105 EXPECT_EQ(expected, helper.callbacks_called()); 106 EXPECT_FALSE(helper.callback_reused_error()); 107 EXPECT_STREQ(buffer1, buffer2); 108 } 109 110 TEST_F(DiskCacheTest, MappedFile_AsyncLoadStore) { 111 base::FilePath filename = cache_path_.AppendASCII("a_test"); 112 scoped_refptr<disk_cache::MappedFile> file(new disk_cache::MappedFile); 113 ASSERT_TRUE(CreateCacheTestFile(filename)); 114 ASSERT_TRUE(file->Init(filename, 8192)); 115 116 int max_id = 0; 117 MessageLoopHelper helper; 118 FileCallbackTest callback(1, &helper, &max_id); 119 120 TestFileBlock file_block1; 121 TestFileBlock file_block2; 122 base::strlcpy(static_cast<char*>(file_block1.buffer()), "the data", 123 file_block1.size()); 124 bool completed; 125 EXPECT_TRUE(file->Store(&file_block1, &callback, &completed)); 126 int expected = completed ? 0 : 1; 127 128 max_id = 1; 129 helper.WaitUntilCacheIoFinished(expected); 130 131 EXPECT_TRUE(file->Load(&file_block2, &callback, &completed)); 132 if (!completed) 133 expected++; 134 135 helper.WaitUntilCacheIoFinished(expected); 136 137 EXPECT_EQ(expected, helper.callbacks_called()); 138 EXPECT_FALSE(helper.callback_reused_error()); 139 EXPECT_STREQ(static_cast<char*>(file_block1.buffer()), 140 static_cast<char*>(file_block2.buffer())); 141 } 142