Roll src/third_party/WebKit 57aef96:a1089e6 (svn 201978:201979)
[chromium-blink-merge.git] / base / files / file_proxy_unittest.cc
blobefe5c92429986b8934df58ec9a712060d2fbb6a9
1 // Copyright 2014 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_proxy.h"
7 #include "base/bind.h"
8 #include "base/files/file.h"
9 #include "base/files/file_util.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/threading/thread.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 namespace base {
18 class FileProxyTest : public testing::Test {
19 public:
20 FileProxyTest()
21 : file_thread_("FileProxyTestFileThread"),
22 error_(File::FILE_OK),
23 bytes_written_(-1),
24 weak_factory_(this) {}
26 void SetUp() override {
27 ASSERT_TRUE(dir_.CreateUniqueTempDir());
28 ASSERT_TRUE(file_thread_.Start());
31 void DidFinish(File::Error error) {
32 error_ = error;
33 MessageLoop::current()->QuitWhenIdle();
36 void DidCreateOrOpen(File::Error error) {
37 error_ = error;
38 MessageLoop::current()->QuitWhenIdle();
41 void DidCreateTemporary(File::Error error,
42 const FilePath& path) {
43 error_ = error;
44 path_ = path;
45 MessageLoop::current()->QuitWhenIdle();
48 void DidGetFileInfo(File::Error error,
49 const File::Info& file_info) {
50 error_ = error;
51 file_info_ = file_info;
52 MessageLoop::current()->QuitWhenIdle();
55 void DidRead(File::Error error,
56 const char* data,
57 int bytes_read) {
58 error_ = error;
59 buffer_.resize(bytes_read);
60 memcpy(&buffer_[0], data, bytes_read);
61 MessageLoop::current()->QuitWhenIdle();
64 void DidWrite(File::Error error,
65 int bytes_written) {
66 error_ = error;
67 bytes_written_ = bytes_written;
68 MessageLoop::current()->QuitWhenIdle();
71 protected:
72 void CreateProxy(uint32 flags, FileProxy* proxy) {
73 proxy->CreateOrOpen(
74 test_path(), flags,
75 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
76 MessageLoop::current()->Run();
77 EXPECT_TRUE(proxy->IsValid());
80 TaskRunner* file_task_runner() const {
81 return file_thread_.task_runner().get();
83 const FilePath& test_dir_path() const { return dir_.path(); }
84 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
86 MessageLoopForIO message_loop_;
87 Thread file_thread_;
89 ScopedTempDir dir_;
90 File::Error error_;
91 FilePath path_;
92 File::Info file_info_;
93 std::vector<char> buffer_;
94 int bytes_written_;
95 WeakPtrFactory<FileProxyTest> weak_factory_;
98 TEST_F(FileProxyTest, CreateOrOpen_Create) {
99 FileProxy proxy(file_task_runner());
100 proxy.CreateOrOpen(
101 test_path(),
102 File::FLAG_CREATE | File::FLAG_READ,
103 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
104 MessageLoop::current()->Run();
106 EXPECT_EQ(File::FILE_OK, error_);
107 EXPECT_TRUE(proxy.IsValid());
108 EXPECT_TRUE(proxy.created());
109 EXPECT_TRUE(PathExists(test_path()));
112 TEST_F(FileProxyTest, CreateOrOpen_Open) {
113 // Creates a file.
114 base::WriteFile(test_path(), NULL, 0);
115 ASSERT_TRUE(PathExists(test_path()));
117 // Opens the created file.
118 FileProxy proxy(file_task_runner());
119 proxy.CreateOrOpen(
120 test_path(),
121 File::FLAG_OPEN | File::FLAG_READ,
122 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
123 MessageLoop::current()->Run();
125 EXPECT_EQ(File::FILE_OK, error_);
126 EXPECT_TRUE(proxy.IsValid());
127 EXPECT_FALSE(proxy.created());
130 TEST_F(FileProxyTest, CreateOrOpen_OpenNonExistent) {
131 FileProxy proxy(file_task_runner());
132 proxy.CreateOrOpen(
133 test_path(),
134 File::FLAG_OPEN | File::FLAG_READ,
135 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
136 MessageLoop::current()->Run();
137 EXPECT_EQ(File::FILE_ERROR_NOT_FOUND, error_);
138 EXPECT_FALSE(proxy.IsValid());
139 EXPECT_FALSE(proxy.created());
140 EXPECT_FALSE(PathExists(test_path()));
143 TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) {
144 bool prev = ThreadRestrictions::SetIOAllowed(false);
146 FileProxy proxy(file_task_runner());
147 proxy.CreateOrOpen(
148 test_path(),
149 File::FLAG_CREATE | File::FLAG_READ,
150 Bind(&FileProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
152 MessageLoop::current()->Run();
153 ThreadRestrictions::SetIOAllowed(prev);
155 EXPECT_TRUE(PathExists(test_path()));
158 TEST_F(FileProxyTest, Close) {
159 // Creates a file.
160 FileProxy proxy(file_task_runner());
161 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
163 #if defined(OS_WIN)
164 // This fails on Windows if the file is not closed.
165 EXPECT_FALSE(base::Move(test_path(), test_dir_path().AppendASCII("new")));
166 #endif
168 proxy.Close(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
169 MessageLoop::current()->Run();
170 EXPECT_EQ(File::FILE_OK, error_);
171 EXPECT_FALSE(proxy.IsValid());
173 // Now it should pass on all platforms.
174 EXPECT_TRUE(base::Move(test_path(), test_dir_path().AppendASCII("new")));
177 TEST_F(FileProxyTest, CreateTemporary) {
179 FileProxy proxy(file_task_runner());
180 proxy.CreateTemporary(
181 0 /* additional_file_flags */,
182 Bind(&FileProxyTest::DidCreateTemporary, weak_factory_.GetWeakPtr()));
183 MessageLoop::current()->Run();
185 EXPECT_TRUE(proxy.IsValid());
186 EXPECT_EQ(File::FILE_OK, error_);
187 EXPECT_TRUE(PathExists(path_));
189 // The file should be writable.
190 proxy.Write(0, "test", 4,
191 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
192 MessageLoop::current()->Run();
193 EXPECT_EQ(File::FILE_OK, error_);
194 EXPECT_EQ(4, bytes_written_);
197 // Make sure the written data can be read from the returned path.
198 std::string data;
199 EXPECT_TRUE(ReadFileToString(path_, &data));
200 EXPECT_EQ("test", data);
202 // Make sure we can & do delete the created file to prevent leaks on the bots.
203 EXPECT_TRUE(base::DeleteFile(path_, false));
206 TEST_F(FileProxyTest, SetAndTake) {
207 File file(test_path(), File::FLAG_CREATE | File::FLAG_READ);
208 ASSERT_TRUE(file.IsValid());
209 FileProxy proxy(file_task_runner());
210 EXPECT_FALSE(proxy.IsValid());
211 proxy.SetFile(file.Pass());
212 EXPECT_TRUE(proxy.IsValid());
213 EXPECT_FALSE(file.IsValid());
215 file = proxy.TakeFile();
216 EXPECT_FALSE(proxy.IsValid());
217 EXPECT_TRUE(file.IsValid());
220 TEST_F(FileProxyTest, GetInfo) {
221 // Setup.
222 ASSERT_EQ(4, base::WriteFile(test_path(), "test", 4));
223 File::Info expected_info;
224 GetFileInfo(test_path(), &expected_info);
226 // Run.
227 FileProxy proxy(file_task_runner());
228 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
229 proxy.GetInfo(
230 Bind(&FileProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
231 MessageLoop::current()->Run();
233 // Verify.
234 EXPECT_EQ(File::FILE_OK, error_);
235 EXPECT_EQ(expected_info.size, file_info_.size);
236 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
237 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
238 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
239 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
242 TEST_F(FileProxyTest, Read) {
243 // Setup.
244 const char expected_data[] = "bleh";
245 int expected_bytes = arraysize(expected_data);
246 ASSERT_EQ(expected_bytes,
247 base::WriteFile(test_path(), expected_data, expected_bytes));
249 // Run.
250 FileProxy proxy(file_task_runner());
251 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
253 proxy.Read(0, 128, Bind(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr()));
254 MessageLoop::current()->Run();
256 // Verify.
257 EXPECT_EQ(File::FILE_OK, error_);
258 EXPECT_EQ(expected_bytes, static_cast<int>(buffer_.size()));
259 for (size_t i = 0; i < buffer_.size(); ++i) {
260 EXPECT_EQ(expected_data[i], buffer_[i]);
264 TEST_F(FileProxyTest, WriteAndFlush) {
265 FileProxy proxy(file_task_runner());
266 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
268 const char data[] = "foo!";
269 int data_bytes = arraysize(data);
270 proxy.Write(0, data, data_bytes,
271 Bind(&FileProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
272 MessageLoop::current()->Run();
273 EXPECT_EQ(File::FILE_OK, error_);
274 EXPECT_EQ(data_bytes, bytes_written_);
276 // Flush the written data. (So that the following read should always
277 // succeed. On some platforms it may work with or without this flush.)
278 proxy.Flush(Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
279 MessageLoop::current()->Run();
280 EXPECT_EQ(File::FILE_OK, error_);
282 // Verify the written data.
283 char buffer[10];
284 EXPECT_EQ(data_bytes, base::ReadFile(test_path(), buffer, data_bytes));
285 for (int i = 0; i < data_bytes; ++i) {
286 EXPECT_EQ(data[i], buffer[i]);
290 #if defined(OS_ANDROID)
291 // Flaky on Android, see http://crbug.com/489602
292 #define MAYBE_SetTimes DISABLED_SetTimes
293 #else
294 #define MAYBE_SetTimes SetTimes
295 #endif
296 TEST_F(FileProxyTest, MAYBE_SetTimes) {
297 FileProxy proxy(file_task_runner());
298 CreateProxy(
299 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_WRITE_ATTRIBUTES,
300 &proxy);
302 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
303 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
305 proxy.SetTimes(last_accessed_time, last_modified_time,
306 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
307 MessageLoop::current()->Run();
308 EXPECT_EQ(File::FILE_OK, error_);
310 File::Info info;
311 GetFileInfo(test_path(), &info);
313 // The returned values may only have the seconds precision, so we cast
314 // the double values to int here.
315 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
316 static_cast<int>(info.last_modified.ToDoubleT()));
317 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
318 static_cast<int>(info.last_accessed.ToDoubleT()));
321 TEST_F(FileProxyTest, SetLength_Shrink) {
322 // Setup.
323 const char kTestData[] = "0123456789";
324 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
325 File::Info info;
326 GetFileInfo(test_path(), &info);
327 ASSERT_EQ(10, info.size);
329 // Run.
330 FileProxy proxy(file_task_runner());
331 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
332 proxy.SetLength(7,
333 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
334 MessageLoop::current()->Run();
336 // Verify.
337 GetFileInfo(test_path(), &info);
338 ASSERT_EQ(7, info.size);
340 char buffer[7];
341 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7));
342 int i = 0;
343 for (; i < 7; ++i)
344 EXPECT_EQ(kTestData[i], buffer[i]);
347 TEST_F(FileProxyTest, SetLength_Expand) {
348 // Setup.
349 const char kTestData[] = "9876543210";
350 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
351 File::Info info;
352 GetFileInfo(test_path(), &info);
353 ASSERT_EQ(10, info.size);
355 // Run.
356 FileProxy proxy(file_task_runner());
357 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
358 proxy.SetLength(53,
359 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
360 MessageLoop::current()->Run();
362 // Verify.
363 GetFileInfo(test_path(), &info);
364 ASSERT_EQ(53, info.size);
366 char buffer[53];
367 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53));
368 int i = 0;
369 for (; i < 10; ++i)
370 EXPECT_EQ(kTestData[i], buffer[i]);
371 for (; i < 53; ++i)
372 EXPECT_EQ(0, buffer[i]);
375 } // namespace base