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"
16 // Implementation of FileIOCallback for the tests.
17 class FileCallbackTest
: public disk_cache::FileIOCallback
{
19 FileCallbackTest(int id
, MessageLoopHelper
* helper
, int* max_id
)
24 ~FileCallbackTest() override
{}
26 void OnFileIOComplete(int bytes_copied
) override
;
30 MessageLoopHelper
* helper_
;
34 void FileCallbackTest::OnFileIOComplete(int bytes_copied
) {
37 helper_
->set_callback_reused_error(true);
40 helper_
->CallbackWasCalled();
43 class TestFileBlock
: public disk_cache::FileBlock
{
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; }
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));
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));
83 MessageLoopHelper helper
;
84 FileCallbackTest
callback(1, &helper
, &max_id
);
88 CacheTestFillBuffer(buffer1
, sizeof(buffer1
), false);
89 base::strlcpy(buffer1
, "the data", arraysize(buffer1
));
91 EXPECT_TRUE(file
->Write(buffer1
, sizeof(buffer1
), 1024 * 1024, &callback
,
93 int expected
= completed
? 0 : 1;
96 helper
.WaitUntilCacheIoFinished(expected
);
98 EXPECT_TRUE(file
->Read(buffer2
, sizeof(buffer2
), 1024 * 1024, &callback
,
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));
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",
125 EXPECT_TRUE(file
->Store(&file_block1
, &callback
, &completed
));
126 int expected
= completed
? 0 : 1;
129 helper
.WaitUntilCacheIoFinished(expected
);
131 EXPECT_TRUE(file
->Load(&file_block2
, &callback
, &completed
));
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()));