blob: efe5c92429986b8934df58ec9a712060d2fbb6a9 [file] [log] [blame]
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +09001// 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.
4
5#include "base/files/file_proxy.h"
6
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +09007#include "base/bind.h"
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +09008#include "base/files/file.h"
brettw@chromium.org01f3da42014-08-14 05:22:14 +09009#include "base/files/file_util.h"
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090010#include "base/files/scoped_temp_dir.h"
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090011#include "base/memory/weak_ptr.h"
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090012#include "base/threading/thread.h"
rvargas@chromium.orge60d9672014-05-06 09:55:35 +090013#include "base/threading/thread_restrictions.h"
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090014#include "testing/gtest/include/gtest/gtest.h"
15
16namespace base {
17
18class 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) {}
25
dchengca87abb2014-12-23 11:56:47 +090026 void SetUp() override {
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090027 ASSERT_TRUE(dir_.CreateUniqueTempDir());
28 ASSERT_TRUE(file_thread_.Start());
29 }
30
31 void DidFinish(File::Error error) {
32 error_ = error;
33 MessageLoop::current()->QuitWhenIdle();
34 }
35
36 void DidCreateOrOpen(File::Error error) {
37 error_ = error;
38 MessageLoop::current()->QuitWhenIdle();
39 }
40
41 void DidCreateTemporary(File::Error error,
42 const FilePath& path) {
43 error_ = error;
44 path_ = path;
45 MessageLoop::current()->QuitWhenIdle();
46 }
47
48 void DidGetFileInfo(File::Error error,
49 const File::Info& file_info) {
50 error_ = error;
51 file_info_ = file_info;
52 MessageLoop::current()->QuitWhenIdle();
53 }
54
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();
62 }
63
64 void DidWrite(File::Error error,
65 int bytes_written) {
66 error_ = error;
67 bytes_written_ = bytes_written;
68 MessageLoop::current()->QuitWhenIdle();
69 }
70
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());
78 }
79
80 TaskRunner* file_task_runner() const {
skyostil97aefe12015-05-01 04:06:15 +090081 return file_thread_.task_runner().get();
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +090082 }
83 const FilePath& test_dir_path() const { return dir_.path(); }
84 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
85
86 MessageLoopForIO message_loop_;
87 Thread file_thread_;
88
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_;
96};
97
98TEST_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();
105
106 EXPECT_EQ(File::FILE_OK, error_);
107 EXPECT_TRUE(proxy.IsValid());
108 EXPECT_TRUE(proxy.created());
109 EXPECT_TRUE(PathExists(test_path()));
110}
111
112TEST_F(FileProxyTest, CreateOrOpen_Open) {
113 // Creates a file.
114 base::WriteFile(test_path(), NULL, 0);
115 ASSERT_TRUE(PathExists(test_path()));
116
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();
124
125 EXPECT_EQ(File::FILE_OK, error_);
126 EXPECT_TRUE(proxy.IsValid());
127 EXPECT_FALSE(proxy.created());
128}
129
130TEST_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()));
141}
142
rvargas@chromium.orge60d9672014-05-06 09:55:35 +0900143TEST_F(FileProxyTest, CreateOrOpen_AbandonedCreate) {
144 bool prev = ThreadRestrictions::SetIOAllowed(false);
145 {
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()));
151 }
152 MessageLoop::current()->Run();
153 ThreadRestrictions::SetIOAllowed(prev);
154
155 EXPECT_TRUE(PathExists(test_path()));
156}
157
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900158TEST_F(FileProxyTest, Close) {
159 // Creates a file.
160 FileProxy proxy(file_task_runner());
161 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
162
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
167
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());
172
173 // Now it should pass on all platforms.
174 EXPECT_TRUE(base::Move(test_path(), test_dir_path().AppendASCII("new")));
175}
176
177TEST_F(FileProxyTest, CreateTemporary) {
178 {
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();
184
185 EXPECT_TRUE(proxy.IsValid());
186 EXPECT_EQ(File::FILE_OK, error_);
187 EXPECT_TRUE(PathExists(path_));
188
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_);
195 }
196
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);
201
202 // Make sure we can & do delete the created file to prevent leaks on the bots.
203 EXPECT_TRUE(base::DeleteFile(path_, false));
204}
205
rvargas@chromium.orgbea68922014-05-16 04:40:40 +0900206TEST_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());
214
215 file = proxy.TakeFile();
216 EXPECT_FALSE(proxy.IsValid());
217 EXPECT_TRUE(file.IsValid());
218}
219
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900220TEST_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);
225
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();
232
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);
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900238 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
239 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
240}
241
242TEST_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));
248
249 // Run.
250 FileProxy proxy(file_task_runner());
251 CreateProxy(File::FLAG_OPEN | File::FLAG_READ, &proxy);
252
253 proxy.Read(0, 128, Bind(&FileProxyTest::DidRead, weak_factory_.GetWeakPtr()));
254 MessageLoop::current()->Run();
255
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]);
261 }
262}
263
264TEST_F(FileProxyTest, WriteAndFlush) {
265 FileProxy proxy(file_task_runner());
266 CreateProxy(File::FLAG_CREATE | File::FLAG_WRITE, &proxy);
267
268 const char data[] = "foo!";
viettrungluua3f9eff2014-10-16 13:02:49 +0900269 int data_bytes = arraysize(data);
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900270 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_);
275
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_);
281
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]);
287 }
288}
289
pkotwicz63f42512015-08-01 05:01:40 +0900290#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
296TEST_F(FileProxyTest, MAYBE_SetTimes) {
rvargas@chromium.orgc40e3b12014-03-15 14:19:31 +0900297 FileProxy proxy(file_task_runner());
298 CreateProxy(
299 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_WRITE_ATTRIBUTES,
300 &proxy);
301
302 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
303 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
304
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_);
309
310 File::Info info;
311 GetFileInfo(test_path(), &info);
312
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()));
319}
320
321TEST_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);
328
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();
335
336 // Verify.
337 GetFileInfo(test_path(), &info);
338 ASSERT_EQ(7, info.size);
339
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]);
345}
346
347TEST_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);
354
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();
361
362 // Verify.
363 GetFileInfo(test_path(), &info);
364 ASSERT_EQ(53, info.size);
365
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]);
373}
374
375} // namespace base