Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / disk_cache / blockfile / mapped_file_unittest.cc
blob9b31d6d54574a5ea85a6622c725ab79f56cd0302
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.
5 #include "base/basictypes.h"
6 #include "base/files/file_path.h"
7 #include "base/strings/string_util.h"
8 #include "net/disk_cache/blockfile/file_block.h"
9 #include "net/disk_cache/blockfile/mapped_file.h"
10 #include "net/disk_cache/disk_cache_test_base.h"
11 #include "net/disk_cache/disk_cache_test_util.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 namespace {
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) {
24 ~FileCallbackTest() override {}
26 void OnFileIOComplete(int bytes_copied) override;
28 private:
29 int id_;
30 MessageLoopHelper* helper_;
31 int* max_id_;
34 void FileCallbackTest::OnFileIOComplete(int bytes_copied) {
35 if (id_ > *max_id_) {
36 NOTREACHED();
37 helper_->set_callback_reused_error(true);
40 helper_->CallbackWasCalled();
43 class TestFileBlock : public disk_cache::FileBlock {
44 public:
45 TestFileBlock() {
46 CacheTestFillBuffer(buffer_, sizeof(buffer_), false);
48 ~TestFileBlock() override {}
50 // FileBlock interface.
51 void* buffer() const override { return const_cast<char*>(buffer_); }
52 size_t size() const override { return sizeof(buffer_); }
53 int offset() const override { return 1024; }
55 private:
56 char buffer_[20];
59 } // namespace
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));
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);
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));
82 int max_id = 0;
83 MessageLoopHelper helper;
84 FileCallbackTest callback(1, &helper, &max_id);
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;
95 max_id = 1;
96 helper.WaitUntilCacheIoFinished(expected);
98 EXPECT_TRUE(file->Read(buffer2, sizeof(buffer2), 1024 * 1024, &callback,
99 &completed));
100 if (!completed)
101 expected++;
103 helper.WaitUntilCacheIoFinished(expected);
105 EXPECT_EQ(expected, helper.callbacks_called());
106 EXPECT_FALSE(helper.callback_reused_error());
107 EXPECT_STREQ(buffer1, buffer2);
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));
116 int max_id = 0;
117 MessageLoopHelper helper;
118 FileCallbackTest callback(1, &helper, &max_id);
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;
128 max_id = 1;
129 helper.WaitUntilCacheIoFinished(expected);
131 EXPECT_TRUE(file->Load(&file_block2, &callback, &completed));
132 if (!completed)
133 expected++;
135 helper.WaitUntilCacheIoFinished(expected);
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()));