1 // Copyright (c) 2012 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/files/file_util_proxy.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/logging.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/platform_file.h"
15 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 class FileUtilProxyTest
: public testing::Test
{
23 : message_loop_(MessageLoop::TYPE_IO
),
24 file_thread_("FileUtilProxyTestFileThread"),
25 error_(PLATFORM_FILE_OK
),
27 file_(kInvalidPlatformFileValue
),
29 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
31 virtual void SetUp() OVERRIDE
{
32 ASSERT_TRUE(dir_
.CreateUniqueTempDir());
33 ASSERT_TRUE(file_thread_
.Start());
36 virtual void TearDown() OVERRIDE
{
37 if (file_
!= kInvalidPlatformFileValue
)
38 ClosePlatformFile(file_
);
41 void DidFinish(PlatformFileError error
) {
43 MessageLoop::current()->QuitWhenIdle();
46 void DidCreateOrOpen(PlatformFileError error
,
47 PassPlatformFile file
,
50 file_
= file
.ReleaseValue();
52 MessageLoop::current()->QuitWhenIdle();
55 void DidCreateTemporary(PlatformFileError error
,
56 PassPlatformFile file
,
57 const FilePath
& path
) {
59 file_
= file
.ReleaseValue();
61 MessageLoop::current()->QuitWhenIdle();
64 void DidGetFileInfo(PlatformFileError error
,
65 const PlatformFileInfo
& file_info
) {
67 file_info_
= file_info
;
68 MessageLoop::current()->QuitWhenIdle();
71 void DidRead(PlatformFileError error
,
75 buffer_
.resize(bytes_read
);
76 memcpy(&buffer_
[0], data
, bytes_read
);
77 MessageLoop::current()->QuitWhenIdle();
80 void DidWrite(PlatformFileError error
,
83 bytes_written_
= bytes_written
;
84 MessageLoop::current()->QuitWhenIdle();
88 PlatformFile
GetTestPlatformFile(int flags
) {
89 if (file_
!= kInvalidPlatformFileValue
)
92 PlatformFileError error
;
93 file_
= CreatePlatformFile(test_path(), flags
, &created
, &error
);
94 EXPECT_EQ(PLATFORM_FILE_OK
, error
);
95 EXPECT_NE(kInvalidPlatformFileValue
, file_
);
99 TaskRunner
* file_task_runner() const {
100 return file_thread_
.message_loop_proxy().get();
102 const FilePath
& test_dir_path() const { return dir_
.path(); }
103 const FilePath
test_path() const { return dir_
.path().AppendASCII("test"); }
105 MessageLoop message_loop_
;
109 PlatformFileError error_
;
113 PlatformFileInfo file_info_
;
114 std::vector
<char> buffer_
;
116 WeakPtrFactory
<FileUtilProxyTest
> weak_factory_
;
119 TEST_F(FileUtilProxyTest
, CreateOrOpen_Create
) {
120 FileUtilProxy::CreateOrOpen(
123 PLATFORM_FILE_CREATE
| PLATFORM_FILE_READ
,
124 Bind(&FileUtilProxyTest::DidCreateOrOpen
, weak_factory_
.GetWeakPtr()));
125 MessageLoop::current()->Run();
127 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
128 EXPECT_TRUE(created_
);
129 EXPECT_NE(kInvalidPlatformFileValue
, file_
);
130 EXPECT_TRUE(file_util::PathExists(test_path()));
133 TEST_F(FileUtilProxyTest
, CreateOrOpen_Open
) {
135 file_util::WriteFile(test_path(), NULL
, 0);
136 ASSERT_TRUE(file_util::PathExists(test_path()));
138 // Opens the created file.
139 FileUtilProxy::CreateOrOpen(
142 PLATFORM_FILE_OPEN
| PLATFORM_FILE_READ
,
143 Bind(&FileUtilProxyTest::DidCreateOrOpen
, weak_factory_
.GetWeakPtr()));
144 MessageLoop::current()->Run();
146 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
147 EXPECT_FALSE(created_
);
148 EXPECT_NE(kInvalidPlatformFileValue
, file_
);
151 TEST_F(FileUtilProxyTest
, CreateOrOpen_OpenNonExistent
) {
152 FileUtilProxy::CreateOrOpen(
155 PLATFORM_FILE_OPEN
| PLATFORM_FILE_READ
,
156 Bind(&FileUtilProxyTest::DidCreateOrOpen
, weak_factory_
.GetWeakPtr()));
157 MessageLoop::current()->Run();
158 EXPECT_EQ(PLATFORM_FILE_ERROR_NOT_FOUND
, error_
);
159 EXPECT_FALSE(created_
);
160 EXPECT_EQ(kInvalidPlatformFileValue
, file_
);
161 EXPECT_FALSE(file_util::PathExists(test_path()));
164 TEST_F(FileUtilProxyTest
, Close
) {
166 PlatformFile file
= GetTestPlatformFile(
167 PLATFORM_FILE_CREATE
| PLATFORM_FILE_WRITE
);
170 // This fails on Windows if the file is not closed.
171 EXPECT_FALSE(file_util::Move(test_path(),
172 test_dir_path().AppendASCII("new")));
175 FileUtilProxy::Close(
178 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
179 MessageLoop::current()->Run();
180 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
182 // Now it should pass on all platforms.
183 EXPECT_TRUE(file_util::Move(test_path(), test_dir_path().AppendASCII("new")));
186 TEST_F(FileUtilProxyTest
, CreateTemporary
) {
187 FileUtilProxy::CreateTemporary(
188 file_task_runner(), 0 /* additional_file_flags */,
189 Bind(&FileUtilProxyTest::DidCreateTemporary
, weak_factory_
.GetWeakPtr()));
190 MessageLoop::current()->Run();
191 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
192 EXPECT_TRUE(file_util::PathExists(path_
));
193 EXPECT_NE(kInvalidPlatformFileValue
, file_
);
195 // The file should be writable.
197 HANDLE hEvent
= CreateEvent(NULL
, FALSE
, FALSE
, NULL
);
198 OVERLAPPED overlapped
= {0};
199 overlapped
.hEvent
= hEvent
;
201 if (!::WriteFile(file_
, "test", 4, &bytes_written
, &overlapped
)) {
202 // Temporary file is created with ASYNC flag, so WriteFile may return 0
203 // with ERROR_IO_PENDING.
204 EXPECT_EQ(ERROR_IO_PENDING
, GetLastError());
205 GetOverlappedResult(file_
, &overlapped
, &bytes_written
, TRUE
);
207 EXPECT_EQ(4, bytes_written
);
209 // On POSIX ASYNC flag does not affect synchronous read/write behavior.
210 EXPECT_EQ(4, WritePlatformFile(file_
, 0, "test", 4));
212 EXPECT_TRUE(ClosePlatformFile(file_
));
213 file_
= kInvalidPlatformFileValue
;
215 // Make sure the written data can be read from the returned path.
217 EXPECT_TRUE(file_util::ReadFileToString(path_
, &data
));
218 EXPECT_EQ("test", data
);
220 // Make sure we can & do delete the created file to prevent leaks on the bots.
221 EXPECT_TRUE(file_util::Delete(path_
, false));
224 TEST_F(FileUtilProxyTest
, GetFileInfo_File
) {
226 ASSERT_EQ(4, file_util::WriteFile(test_path(), "test", 4));
227 PlatformFileInfo expected_info
;
228 file_util::GetFileInfo(test_path(), &expected_info
);
231 FileUtilProxy::GetFileInfo(
234 Bind(&FileUtilProxyTest::DidGetFileInfo
, weak_factory_
.GetWeakPtr()));
235 MessageLoop::current()->Run();
238 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
239 EXPECT_EQ(expected_info
.size
, file_info_
.size
);
240 EXPECT_EQ(expected_info
.is_directory
, file_info_
.is_directory
);
241 EXPECT_EQ(expected_info
.is_symbolic_link
, file_info_
.is_symbolic_link
);
242 EXPECT_EQ(expected_info
.last_modified
, file_info_
.last_modified
);
243 EXPECT_EQ(expected_info
.last_accessed
, file_info_
.last_accessed
);
244 EXPECT_EQ(expected_info
.creation_time
, file_info_
.creation_time
);
247 TEST_F(FileUtilProxyTest
, GetFileInfo_Directory
) {
249 ASSERT_TRUE(file_util::CreateDirectory(test_path()));
250 PlatformFileInfo expected_info
;
251 file_util::GetFileInfo(test_path(), &expected_info
);
254 FileUtilProxy::GetFileInfo(
257 Bind(&FileUtilProxyTest::DidGetFileInfo
, weak_factory_
.GetWeakPtr()));
258 MessageLoop::current()->Run();
261 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
262 EXPECT_EQ(expected_info
.size
, file_info_
.size
);
263 EXPECT_EQ(expected_info
.is_directory
, file_info_
.is_directory
);
264 EXPECT_EQ(expected_info
.is_symbolic_link
, file_info_
.is_symbolic_link
);
265 EXPECT_EQ(expected_info
.last_modified
, file_info_
.last_modified
);
266 EXPECT_EQ(expected_info
.last_accessed
, file_info_
.last_accessed
);
267 EXPECT_EQ(expected_info
.creation_time
, file_info_
.creation_time
);
270 TEST_F(FileUtilProxyTest
, Read
) {
272 const char expected_data
[] = "bleh";
273 int expected_bytes
= arraysize(expected_data
);
274 ASSERT_EQ(expected_bytes
,
275 file_util::WriteFile(test_path(), expected_data
, expected_bytes
));
280 GetTestPlatformFile(PLATFORM_FILE_OPEN
| PLATFORM_FILE_READ
),
283 Bind(&FileUtilProxyTest::DidRead
, weak_factory_
.GetWeakPtr()));
284 MessageLoop::current()->Run();
287 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
288 EXPECT_EQ(expected_bytes
, static_cast<int>(buffer_
.size()));
289 for (size_t i
= 0; i
< buffer_
.size(); ++i
) {
290 EXPECT_EQ(expected_data
[i
], buffer_
[i
]);
294 TEST_F(FileUtilProxyTest
, WriteAndFlush
) {
295 const char data
[] = "foo!";
296 int data_bytes
= ARRAYSIZE_UNSAFE(data
);
297 PlatformFile file
= GetTestPlatformFile(
298 PLATFORM_FILE_CREATE
| PLATFORM_FILE_WRITE
);
300 FileUtilProxy::Write(
306 Bind(&FileUtilProxyTest::DidWrite
, weak_factory_
.GetWeakPtr()));
307 MessageLoop::current()->Run();
308 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
309 EXPECT_EQ(data_bytes
, bytes_written_
);
311 // Flush the written data. (So that the following read should always
312 // succeed. On some platforms it may work with or without this flush.)
313 FileUtilProxy::Flush(
316 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
317 MessageLoop::current()->Run();
318 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
320 // Verify the written data.
322 EXPECT_EQ(data_bytes
, file_util::ReadFile(test_path(), buffer
, data_bytes
));
323 for (int i
= 0; i
< data_bytes
; ++i
) {
324 EXPECT_EQ(data
[i
], buffer
[i
]);
328 TEST_F(FileUtilProxyTest
, Touch
) {
329 Time last_accessed_time
= Time::Now() - TimeDelta::FromDays(12345);
330 Time last_modified_time
= Time::Now() - TimeDelta::FromHours(98765);
332 FileUtilProxy::Touch(
334 GetTestPlatformFile(PLATFORM_FILE_CREATE
|
335 PLATFORM_FILE_WRITE
|
336 PLATFORM_FILE_WRITE_ATTRIBUTES
),
339 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
340 MessageLoop::current()->Run();
341 EXPECT_EQ(PLATFORM_FILE_OK
, error_
);
343 PlatformFileInfo info
;
344 file_util::GetFileInfo(test_path(), &info
);
346 // The returned values may only have the seconds precision, so we cast
347 // the double values to int here.
348 EXPECT_EQ(static_cast<int>(last_modified_time
.ToDoubleT()),
349 static_cast<int>(info
.last_modified
.ToDoubleT()));
350 EXPECT_EQ(static_cast<int>(last_accessed_time
.ToDoubleT()),
351 static_cast<int>(info
.last_accessed
.ToDoubleT()));
354 TEST_F(FileUtilProxyTest
, Truncate_Shrink
) {
356 const char kTestData
[] = "0123456789";
357 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData
, 10));
358 PlatformFileInfo info
;
359 file_util::GetFileInfo(test_path(), &info
);
360 ASSERT_EQ(10, info
.size
);
363 FileUtilProxy::Truncate(
365 GetTestPlatformFile(PLATFORM_FILE_OPEN
| PLATFORM_FILE_WRITE
),
367 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
368 MessageLoop::current()->Run();
371 file_util::GetFileInfo(test_path(), &info
);
372 ASSERT_EQ(7, info
.size
);
375 EXPECT_EQ(7, file_util::ReadFile(test_path(), buffer
, 7));
378 EXPECT_EQ(kTestData
[i
], buffer
[i
]);
381 TEST_F(FileUtilProxyTest
, Truncate_Expand
) {
383 const char kTestData
[] = "9876543210";
384 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData
, 10));
385 PlatformFileInfo info
;
386 file_util::GetFileInfo(test_path(), &info
);
387 ASSERT_EQ(10, info
.size
);
390 FileUtilProxy::Truncate(
392 GetTestPlatformFile(PLATFORM_FILE_OPEN
| PLATFORM_FILE_WRITE
),
394 Bind(&FileUtilProxyTest::DidFinish
, weak_factory_
.GetWeakPtr()));
395 MessageLoop::current()->Run();
398 file_util::GetFileInfo(test_path(), &info
);
399 ASSERT_EQ(53, info
.size
);
402 EXPECT_EQ(53, file_util::ReadFile(test_path(), buffer
, 53));
405 EXPECT_EQ(kTestData
[i
], buffer
[i
]);
407 EXPECT_EQ(0, buffer
[i
]);