Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(308)

Side by Side Diff: base/file_util_proxy_unittest.cc

Issue 10095028: Convert MessageLoopProxy to TaskRunner in FileUtilProxy (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/file_util_proxy.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #include "base/file_util_proxy.h"
6
7 #include <map>
8
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop.h"
13 #include "base/platform_file.h"
14 #include "base/scoped_temp_dir.h"
15 #include "base/threading/thread.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace base {
19
20 class FileUtilProxyTest : public testing::Test {
21 public:
22 FileUtilProxyTest()
23 : message_loop_(MessageLoop::TYPE_IO),
24 file_thread_("FileUtilProxyTestFileThread"),
25 error_(PLATFORM_FILE_OK),
26 created_(false),
27 file_(kInvalidPlatformFileValue),
28 bytes_written_(-1),
29 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {}
30
31 virtual void SetUp() OVERRIDE {
32 ASSERT_TRUE(dir_.CreateUniqueTempDir());
33 ASSERT_TRUE(file_thread_.Start());
34 }
35
36 virtual void TearDown() OVERRIDE {
37 if (file_ != kInvalidPlatformFileValue)
38 ClosePlatformFile(file_);
39 }
40
41 void DidFinish(PlatformFileError error) {
42 error_ = error;
43 MessageLoop::current()->Quit();
44 }
45
46 void DidCreateOrOpen(PlatformFileError error,
47 PassPlatformFile file,
48 bool created) {
49 error_ = error;
50 file_ = file.ReleaseValue();
51 created_ = created;
52 MessageLoop::current()->Quit();
53 }
54
55 void DidCreateTemporary(PlatformFileError error,
56 PassPlatformFile file,
57 const FilePath& path) {
58 error_ = error;
59 file_ = file.ReleaseValue();
60 path_ = path;
61 MessageLoop::current()->Quit();
62 }
63
64 void DidGetFileInfo(PlatformFileError error,
65 const PlatformFileInfo& file_info) {
66 error_ = error;
67 file_info_ = file_info;
68 MessageLoop::current()->Quit();
69 }
70
71 void DidRead(PlatformFileError error,
72 const char* data,
73 int bytes_read) {
74 error_ = error;
75 buffer_.resize(bytes_read);
76 memcpy(&buffer_[0], data, bytes_read);
77 MessageLoop::current()->Quit();
78 }
79
80 void DidWrite(PlatformFileError error,
81 int bytes_written) {
82 error_ = error;
83 bytes_written_ = bytes_written;
84 MessageLoop::current()->Quit();
85 }
86
87 protected:
88 PlatformFile GetTestPlatformFile(int flags) {
89 if (file_ != kInvalidPlatformFileValue)
90 return file_;
91 bool created;
92 PlatformFileError error;
93 file_ = CreatePlatformFile(test_path(), flags, &created, &error);
94 EXPECT_EQ(PLATFORM_FILE_OK, error);
95 EXPECT_NE(kInvalidPlatformFileValue, file_);
96 return file_;
97 }
98
99 TaskRunner* file_task_runner() const {
100 return file_thread_.message_loop_proxy().get();
101 }
102 const FilePath& test_dir_path() const { return dir_.path(); }
103 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
104
105 MessageLoop message_loop_;
106 Thread file_thread_;
107
108 ScopedTempDir dir_;
109 PlatformFileError error_;
110 bool created_;
111 PlatformFile file_;
112 FilePath path_;
113 PlatformFileInfo file_info_;
114 std::vector<char> buffer_;
115 int bytes_written_;
116 WeakPtrFactory<FileUtilProxyTest> weak_factory_;
117 };
118
119 TEST_F(FileUtilProxyTest, CreateOrOpen_Create) {
120 FileUtilProxy::CreateOrOpen(
121 file_task_runner(),
122 test_path(),
123 PLATFORM_FILE_CREATE | PLATFORM_FILE_READ,
124 Bind(&FileUtilProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
125 MessageLoop::current()->Run();
126
127 EXPECT_EQ(PLATFORM_FILE_OK, error_);
128 EXPECT_TRUE(created_);
129 EXPECT_NE(kInvalidPlatformFileValue, file_);
130 EXPECT_TRUE(file_util::PathExists(test_path()));
131 }
132
133 TEST_F(FileUtilProxyTest, CreateOrOpen_Open) {
134 // Creates a file.
135 file_util::WriteFile(test_path(), NULL, 0);
136 ASSERT_TRUE(file_util::PathExists(test_path()));
137
138 // Opens the created file.
139 FileUtilProxy::CreateOrOpen(
140 file_task_runner(),
141 test_path(),
142 PLATFORM_FILE_OPEN | PLATFORM_FILE_READ,
143 Bind(&FileUtilProxyTest::DidCreateOrOpen, weak_factory_.GetWeakPtr()));
144 MessageLoop::current()->Run();
145
146 EXPECT_EQ(PLATFORM_FILE_OK, error_);
147 EXPECT_FALSE(created_);
148 EXPECT_NE(kInvalidPlatformFileValue, file_);
149 }
150
151 TEST_F(FileUtilProxyTest, CreateOrOpen_OpenNonExistent) {
152 FileUtilProxy::CreateOrOpen(
153 file_task_runner(),
154 test_path(),
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()));
162 }
163
164 TEST_F(FileUtilProxyTest, Close) {
165 // Creates a file.
166 PlatformFile file = GetTestPlatformFile(
167 PLATFORM_FILE_CREATE | PLATFORM_FILE_WRITE);
168
169 #if defined(OS_WIN)
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")));
173 #endif
174
175 FileUtilProxy::Close(
176 file_task_runner(),
177 file,
178 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
179 MessageLoop::current()->Run();
180 EXPECT_EQ(PLATFORM_FILE_OK, error_);
181
182 // Now it should pass on all platforms.
183 EXPECT_TRUE(file_util::Move(test_path(), test_dir_path().AppendASCII("new")));
184 }
185
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_);
194
195 // The file should be writable.
196 #if defined(OS_WIN)
197 HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
198 OVERLAPPED overlapped = {0};
199 overlapped.hEvent = hEvent;
200 DWORD bytes_written;
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);
206 }
207 EXPECT_EQ(4, bytes_written);
208 #else
209 // On POSIX ASYNC flag does not affect synchronous read/write behavior.
210 EXPECT_EQ(4, WritePlatformFile(file_, 0, "test", 4));
211 #endif
212 EXPECT_TRUE(ClosePlatformFile(file_));
213 file_ = kInvalidPlatformFileValue;
214
215 // Make sure the written data can be read from the returned path.
216 std::string data;
217 EXPECT_TRUE(file_util::ReadFileToString(path_, &data));
218 EXPECT_EQ("test", data);
219 }
220
221 TEST_F(FileUtilProxyTest, GetFileInfo_File) {
222 // Setup.
223 ASSERT_EQ(4, file_util::WriteFile(test_path(), "test", 4));
224 PlatformFileInfo expected_info;
225 file_util::GetFileInfo(test_path(), &expected_info);
226
227 // Run.
228 FileUtilProxy::GetFileInfo(
229 file_task_runner(),
230 test_path(),
231 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
232 MessageLoop::current()->Run();
233
234 // Verify.
235 EXPECT_EQ(PLATFORM_FILE_OK, error_);
236 EXPECT_EQ(expected_info.size, file_info_.size);
237 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
238 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
239 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
240 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed);
241 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
242 }
243
244 TEST_F(FileUtilProxyTest, GetFileInfo_Directory) {
245 // Setup.
246 ASSERT_TRUE(file_util::CreateDirectory(test_path()));
247 PlatformFileInfo expected_info;
248 file_util::GetFileInfo(test_path(), &expected_info);
249
250 // Run.
251 FileUtilProxy::GetFileInfo(
252 file_task_runner(),
253 test_path(),
254 Bind(&FileUtilProxyTest::DidGetFileInfo, weak_factory_.GetWeakPtr()));
255 MessageLoop::current()->Run();
256
257 // Verify.
258 EXPECT_EQ(PLATFORM_FILE_OK, error_);
259 EXPECT_EQ(expected_info.size, file_info_.size);
260 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
261 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
262 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
263 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed);
264 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
265 }
266
267 TEST_F(FileUtilProxyTest, Read) {
268 // Setup.
269 const char expected_data[] = "bleh";
270 int expected_bytes = arraysize(expected_data);
271 ASSERT_EQ(expected_bytes,
272 file_util::WriteFile(test_path(), expected_data, expected_bytes));
273
274 // Run.
275 FileUtilProxy::Read(
276 file_task_runner(),
277 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_READ),
278 0, // offset
279 128,
280 Bind(&FileUtilProxyTest::DidRead, weak_factory_.GetWeakPtr()));
281 MessageLoop::current()->Run();
282
283 // Verify.
284 EXPECT_EQ(PLATFORM_FILE_OK, error_);
285 EXPECT_EQ(expected_bytes, static_cast<int>(buffer_.size()));
286 for (size_t i = 0; i < buffer_.size(); ++i) {
287 EXPECT_EQ(expected_data[i], buffer_[i]);
288 }
289 }
290
291 TEST_F(FileUtilProxyTest, WriteAndFlush) {
292 const char data[] = "foo!";
293 int data_bytes = ARRAYSIZE_UNSAFE(data);
294 PlatformFile file = GetTestPlatformFile(
295 PLATFORM_FILE_CREATE | PLATFORM_FILE_WRITE);
296
297 FileUtilProxy::Write(
298 file_task_runner(),
299 file,
300 0, // offset
301 data,
302 data_bytes,
303 Bind(&FileUtilProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
304 MessageLoop::current()->Run();
305 EXPECT_EQ(PLATFORM_FILE_OK, error_);
306 EXPECT_EQ(data_bytes, bytes_written_);
307
308 // Flush the written data. (So that the following read should always
309 // succeed. On some platforms it may work with or without this flush.)
310 FileUtilProxy::Flush(
311 file_task_runner(),
312 file,
313 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
314 MessageLoop::current()->Run();
315 EXPECT_EQ(PLATFORM_FILE_OK, error_);
316
317 // Verify the written data.
318 char buffer[10];
319 EXPECT_EQ(data_bytes, file_util::ReadFile(test_path(), buffer, data_bytes));
320 for (int i = 0; i < data_bytes; ++i) {
321 EXPECT_EQ(data[i], buffer[i]);
322 }
323 }
324
325 TEST_F(FileUtilProxyTest, Touch) {
326 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
327 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
328
329 FileUtilProxy::Touch(
330 file_task_runner(),
331 GetTestPlatformFile(PLATFORM_FILE_CREATE |
332 PLATFORM_FILE_WRITE |
333 PLATFORM_FILE_WRITE_ATTRIBUTES),
334 last_accessed_time,
335 last_modified_time,
336 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
337 MessageLoop::current()->Run();
338 EXPECT_EQ(PLATFORM_FILE_OK, error_);
339
340 PlatformFileInfo info;
341 file_util::GetFileInfo(test_path(), &info);
342
343 // The returned values may only have the seconds precision, so we cast
344 // the double values to int here.
345 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
346 static_cast<int>(info.last_modified.ToDoubleT()));
347 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
348 static_cast<int>(info.last_accessed.ToDoubleT()));
349 }
350
351 TEST_F(FileUtilProxyTest, Truncate_Shrink) {
352 // Setup.
353 const char kTestData[] = "0123456789";
354 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
355 PlatformFileInfo info;
356 file_util::GetFileInfo(test_path(), &info);
357 ASSERT_EQ(10, info.size);
358
359 // Run.
360 FileUtilProxy::Truncate(
361 file_task_runner(),
362 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE),
363 7,
364 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
365 MessageLoop::current()->Run();
366
367 // Verify.
368 file_util::GetFileInfo(test_path(), &info);
369 ASSERT_EQ(7, info.size);
370
371 char buffer[7];
372 EXPECT_EQ(7, file_util::ReadFile(test_path(), buffer, 7));
373 int i = 0;
374 for (; i < 7; ++i)
375 EXPECT_EQ(kTestData[i], buffer[i]);
376 }
377
378 TEST_F(FileUtilProxyTest, Truncate_Expand) {
379 // Setup.
380 const char kTestData[] = "9876543210";
381 ASSERT_EQ(10, file_util::WriteFile(test_path(), kTestData, 10));
382 PlatformFileInfo info;
383 file_util::GetFileInfo(test_path(), &info);
384 ASSERT_EQ(10, info.size);
385
386 // Run.
387 FileUtilProxy::Truncate(
388 file_task_runner(),
389 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE),
390 53,
391 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
392 MessageLoop::current()->Run();
393
394 // Verify.
395 file_util::GetFileInfo(test_path(), &info);
396 ASSERT_EQ(53, info.size);
397
398 char buffer[53];
399 EXPECT_EQ(53, file_util::ReadFile(test_path(), buffer, 53));
400 int i = 0;
401 for (; i < 10; ++i)
402 EXPECT_EQ(kTestData[i], buffer[i]);
403 for (; i < 53; ++i)
404 EXPECT_EQ(0, buffer[i]);
405 }
406
407 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_proxy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698