Add connectivity check for Android feedback.
[chromium-blink-merge.git] / base / files / file_proxy_unittest.cc
blobdf0bbc869c2d9a31a0113cf18d9b0961b8f35bae
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 TEST_F(FileProxyTest, SetTimes) {
291 FileProxy proxy(file_task_runner());
292 CreateProxy(
293 File::FLAG_CREATE | File::FLAG_WRITE | File::FLAG_WRITE_ATTRIBUTES,
294 &proxy);
296 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
297 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
299 proxy.SetTimes(last_accessed_time, last_modified_time,
300 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
301 MessageLoop::current()->Run();
302 EXPECT_EQ(File::FILE_OK, error_);
304 File::Info info;
305 GetFileInfo(test_path(), &info);
307 // The returned values may only have the seconds precision, so we cast
308 // the double values to int here.
309 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
310 static_cast<int>(info.last_modified.ToDoubleT()));
311 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
312 static_cast<int>(info.last_accessed.ToDoubleT()));
315 TEST_F(FileProxyTest, SetLength_Shrink) {
316 // Setup.
317 const char kTestData[] = "0123456789";
318 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
319 File::Info info;
320 GetFileInfo(test_path(), &info);
321 ASSERT_EQ(10, info.size);
323 // Run.
324 FileProxy proxy(file_task_runner());
325 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
326 proxy.SetLength(7,
327 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
328 MessageLoop::current()->Run();
330 // Verify.
331 GetFileInfo(test_path(), &info);
332 ASSERT_EQ(7, info.size);
334 char buffer[7];
335 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7));
336 int i = 0;
337 for (; i < 7; ++i)
338 EXPECT_EQ(kTestData[i], buffer[i]);
341 TEST_F(FileProxyTest, SetLength_Expand) {
342 // Setup.
343 const char kTestData[] = "9876543210";
344 ASSERT_EQ(10, base::WriteFile(test_path(), kTestData, 10));
345 File::Info info;
346 GetFileInfo(test_path(), &info);
347 ASSERT_EQ(10, info.size);
349 // Run.
350 FileProxy proxy(file_task_runner());
351 CreateProxy(File::FLAG_OPEN | File::FLAG_WRITE, &proxy);
352 proxy.SetLength(53,
353 Bind(&FileProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
354 MessageLoop::current()->Run();
356 // Verify.
357 GetFileInfo(test_path(), &info);
358 ASSERT_EQ(53, info.size);
360 char buffer[53];
361 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53));
362 int i = 0;
363 for (; i < 10; ++i)
364 EXPECT_EQ(kTestData[i], buffer[i]);
365 for (; i < 53; ++i)
366 EXPECT_EQ(0, buffer[i]);
369 } // namespace base