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