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

Side by Side Diff: base/files/file_util_proxy_unittest.cc

Issue 293433002: Remove unused code from FileUtilProxy. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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/files/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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util_proxy.h" 5 #include "base/files/file_util_proxy.h"
6 6
7 #include <map>
8
9 #include "base/bind.h" 7 #include "base/bind.h"
10 #include "base/file_util.h" 8 #include "base/file_util.h"
11 #include "base/files/scoped_temp_dir.h" 9 #include "base/files/scoped_temp_dir.h"
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h" 10 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
15 #include "base/platform_file.h"
16 #include "base/threading/thread.h" 12 #include "base/threading/thread.h"
17 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
18 14
19 namespace base { 15 namespace base {
20 16
21 class FileUtilProxyTest : public testing::Test { 17 class FileUtilProxyTest : public testing::Test {
22 public: 18 public:
23 FileUtilProxyTest() 19 FileUtilProxyTest()
24 : file_thread_("FileUtilProxyTestFileThread"), 20 : file_thread_("FileUtilProxyTestFileThread"),
25 error_(File::FILE_OK), 21 error_(File::FILE_OK),
26 created_(false), 22 created_(false),
27 file_(kInvalidPlatformFileValue),
28 bytes_written_(-1), 23 bytes_written_(-1),
29 weak_factory_(this) {} 24 weak_factory_(this) {}
30 25
31 virtual void SetUp() OVERRIDE { 26 virtual void SetUp() OVERRIDE {
32 ASSERT_TRUE(dir_.CreateUniqueTempDir()); 27 ASSERT_TRUE(dir_.CreateUniqueTempDir());
33 ASSERT_TRUE(file_thread_.Start()); 28 ASSERT_TRUE(file_thread_.Start());
34 } 29 }
35 30
36 virtual void TearDown() OVERRIDE {
37 if (file_ != kInvalidPlatformFileValue)
38 ClosePlatformFile(file_);
39 }
40
41 void DidFinish(File::Error error) { 31 void DidFinish(File::Error error) {
42 error_ = error; 32 error_ = error;
43 MessageLoop::current()->QuitWhenIdle(); 33 MessageLoop::current()->QuitWhenIdle();
44 } 34 }
45 35
46 void DidCreateOrOpen(File::Error error,
47 PassPlatformFile file,
48 bool created) {
49 error_ = error;
50 file_ = file.ReleaseValue();
51 created_ = created;
52 MessageLoop::current()->QuitWhenIdle();
53 }
54
55 void DidCreateTemporary(File::Error error,
56 PassPlatformFile file,
57 const FilePath& path) {
58 error_ = error;
59 file_ = file.ReleaseValue();
60 path_ = path;
61 MessageLoop::current()->QuitWhenIdle();
62 }
63
64 void DidGetFileInfo(File::Error error, 36 void DidGetFileInfo(File::Error error,
65 const File::Info& file_info) { 37 const File::Info& file_info) {
66 error_ = error; 38 error_ = error;
67 file_info_ = file_info; 39 file_info_ = file_info;
68 MessageLoop::current()->QuitWhenIdle(); 40 MessageLoop::current()->QuitWhenIdle();
69 } 41 }
70 42
71 void DidRead(File::Error 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()->QuitWhenIdle();
78 }
79
80 void DidWrite(File::Error error,
81 int bytes_written) {
82 error_ = error;
83 bytes_written_ = bytes_written;
84 MessageLoop::current()->QuitWhenIdle();
85 }
86
87 protected: 43 protected:
88 PlatformFile GetTestPlatformFile(int flags) {
89 if (file_ != kInvalidPlatformFileValue)
90 return file_;
91
92 File file(test_path(), flags);
93 EXPECT_TRUE(file.IsValid());
94 file_ = file.TakePlatformFile();
95 return file_;
96 }
97
98 TaskRunner* file_task_runner() const { 44 TaskRunner* file_task_runner() const {
99 return file_thread_.message_loop_proxy().get(); 45 return file_thread_.message_loop_proxy().get();
100 } 46 }
101 const FilePath& test_dir_path() const { return dir_.path(); } 47 const FilePath& test_dir_path() const { return dir_.path(); }
102 const FilePath test_path() const { return dir_.path().AppendASCII("test"); } 48 const FilePath test_path() const { return dir_.path().AppendASCII("test"); }
103 49
104 MessageLoopForIO message_loop_; 50 MessageLoopForIO message_loop_;
105 Thread file_thread_; 51 Thread file_thread_;
106 52
107 ScopedTempDir dir_; 53 ScopedTempDir dir_;
108 File::Error error_; 54 File::Error error_;
109 bool created_; 55 bool created_;
110 PlatformFile file_;
111 FilePath path_; 56 FilePath path_;
112 File::Info file_info_; 57 File::Info file_info_;
113 std::vector<char> buffer_; 58 std::vector<char> buffer_;
114 int bytes_written_; 59 int bytes_written_;
115 WeakPtrFactory<FileUtilProxyTest> weak_factory_; 60 WeakPtrFactory<FileUtilProxyTest> weak_factory_;
116 }; 61 };
117 62
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(File::FILE_OK, error_);
127 EXPECT_TRUE(created_);
128 EXPECT_NE(kInvalidPlatformFileValue, file_);
129 EXPECT_TRUE(PathExists(test_path()));
130 }
131
132 TEST_F(FileUtilProxyTest, CreateOrOpen_Open) {
133 // Creates a file.
134 WriteFile(test_path(), NULL, 0);
135 ASSERT_TRUE(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(File::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(File::FILE_ERROR_NOT_FOUND, error_);
158 EXPECT_FALSE(created_);
159 EXPECT_EQ(kInvalidPlatformFileValue, file_);
160 EXPECT_FALSE(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(base::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(File::FILE_OK, error_);
180
181 // Now it should pass on all platforms.
182 EXPECT_TRUE(base::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(File::FILE_OK, error_);
191 EXPECT_TRUE(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(ReadFileToString(path_, &data));
217 EXPECT_EQ("test", data);
218
219 // Make sure we can & do delete the created file to prevent leaks on the bots.
220 EXPECT_TRUE(base::DeleteFile(path_, false));
221 }
222 63
223 TEST_F(FileUtilProxyTest, GetFileInfo_File) { 64 TEST_F(FileUtilProxyTest, GetFileInfo_File) {
224 // Setup. 65 // Setup.
225 ASSERT_EQ(4, WriteFile(test_path(), "test", 4)); 66 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
226 File::Info expected_info; 67 File::Info expected_info;
227 GetFileInfo(test_path(), &expected_info); 68 GetFileInfo(test_path(), &expected_info);
228 69
229 // Run. 70 // Run.
230 FileUtilProxy::GetFileInfo( 71 FileUtilProxy::GetFileInfo(
231 file_task_runner(), 72 file_task_runner(),
(...skipping 27 matching lines...) Expand all
259 // Verify. 100 // Verify.
260 EXPECT_EQ(File::FILE_OK, error_); 101 EXPECT_EQ(File::FILE_OK, error_);
261 EXPECT_EQ(expected_info.size, file_info_.size); 102 EXPECT_EQ(expected_info.size, file_info_.size);
262 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory); 103 EXPECT_EQ(expected_info.is_directory, file_info_.is_directory);
263 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link); 104 EXPECT_EQ(expected_info.is_symbolic_link, file_info_.is_symbolic_link);
264 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified); 105 EXPECT_EQ(expected_info.last_modified, file_info_.last_modified);
265 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed); 106 EXPECT_EQ(expected_info.last_accessed, file_info_.last_accessed);
266 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time); 107 EXPECT_EQ(expected_info.creation_time, file_info_.creation_time);
267 } 108 }
268 109
269 TEST_F(FileUtilProxyTest, Read) {
270 // Setup.
271 const char expected_data[] = "bleh";
272 int expected_bytes = arraysize(expected_data);
273 ASSERT_EQ(expected_bytes,
274 WriteFile(test_path(), expected_data, expected_bytes));
275
276 // Run.
277 FileUtilProxy::Read(
278 file_task_runner(),
279 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_READ),
280 0, // offset
281 128,
282 Bind(&FileUtilProxyTest::DidRead, weak_factory_.GetWeakPtr()));
283 MessageLoop::current()->Run();
284
285 // Verify.
286 EXPECT_EQ(File::FILE_OK, error_);
287 EXPECT_EQ(expected_bytes, static_cast<int>(buffer_.size()));
288 for (size_t i = 0; i < buffer_.size(); ++i) {
289 EXPECT_EQ(expected_data[i], buffer_[i]);
290 }
291 }
292
293 TEST_F(FileUtilProxyTest, WriteAndFlush) {
294 const char data[] = "foo!";
295 int data_bytes = ARRAYSIZE_UNSAFE(data);
296 PlatformFile file = GetTestPlatformFile(
297 PLATFORM_FILE_CREATE | PLATFORM_FILE_WRITE);
298
299 FileUtilProxy::Write(
300 file_task_runner(),
301 file,
302 0, // offset
303 data,
304 data_bytes,
305 Bind(&FileUtilProxyTest::DidWrite, weak_factory_.GetWeakPtr()));
306 MessageLoop::current()->Run();
307 EXPECT_EQ(File::FILE_OK, error_);
308 EXPECT_EQ(data_bytes, bytes_written_);
309
310 // Flush the written data. (So that the following read should always
311 // succeed. On some platforms it may work with or without this flush.)
312 FileUtilProxy::Flush(
313 file_task_runner(),
314 file,
315 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
316 MessageLoop::current()->Run();
317 EXPECT_EQ(File::FILE_OK, error_);
318
319 // Verify the written data.
320 char buffer[10];
321 EXPECT_EQ(data_bytes, base::ReadFile(test_path(), buffer, data_bytes));
322 for (int i = 0; i < data_bytes; ++i) {
323 EXPECT_EQ(data[i], buffer[i]);
324 }
325 }
326
327 TEST_F(FileUtilProxyTest, Touch) { 110 TEST_F(FileUtilProxyTest, Touch) {
111 ASSERT_EQ(4, WriteFile(test_path(), "test", 4));
328 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345); 112 Time last_accessed_time = Time::Now() - TimeDelta::FromDays(12345);
329 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765); 113 Time last_modified_time = Time::Now() - TimeDelta::FromHours(98765);
330 114
331 FileUtilProxy::Touch( 115 FileUtilProxy::Touch(
332 file_task_runner(), 116 file_task_runner(),
333 GetTestPlatformFile(PLATFORM_FILE_CREATE | 117 test_path(),
334 PLATFORM_FILE_WRITE |
335 PLATFORM_FILE_WRITE_ATTRIBUTES),
336 last_accessed_time, 118 last_accessed_time,
337 last_modified_time, 119 last_modified_time,
338 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr())); 120 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
339 MessageLoop::current()->Run(); 121 MessageLoop::current()->Run();
340 EXPECT_EQ(File::FILE_OK, error_); 122 EXPECT_EQ(File::FILE_OK, error_);
341 123
342 File::Info info; 124 File::Info info;
343 GetFileInfo(test_path(), &info); 125 GetFileInfo(test_path(), &info);
344 126
345 // The returned values may only have the seconds precision, so we cast 127 // The returned values may only have the seconds precision, so we cast
346 // the double values to int here. 128 // the double values to int here.
347 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()), 129 EXPECT_EQ(static_cast<int>(last_modified_time.ToDoubleT()),
348 static_cast<int>(info.last_modified.ToDoubleT())); 130 static_cast<int>(info.last_modified.ToDoubleT()));
349 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()), 131 EXPECT_EQ(static_cast<int>(last_accessed_time.ToDoubleT()),
350 static_cast<int>(info.last_accessed.ToDoubleT())); 132 static_cast<int>(info.last_accessed.ToDoubleT()));
351 } 133 }
352 134
353 TEST_F(FileUtilProxyTest, Truncate_Shrink) {
354 // Setup.
355 const char kTestData[] = "0123456789";
356 ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10));
357 File::Info info;
358 GetFileInfo(test_path(), &info);
359 ASSERT_EQ(10, info.size);
360
361 // Run.
362 FileUtilProxy::Truncate(
363 file_task_runner(),
364 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE),
365 7,
366 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
367 MessageLoop::current()->Run();
368
369 // Verify.
370 GetFileInfo(test_path(), &info);
371 ASSERT_EQ(7, info.size);
372
373 char buffer[7];
374 EXPECT_EQ(7, base::ReadFile(test_path(), buffer, 7));
375 int i = 0;
376 for (; i < 7; ++i)
377 EXPECT_EQ(kTestData[i], buffer[i]);
378 }
379
380 TEST_F(FileUtilProxyTest, Truncate_Expand) {
381 // Setup.
382 const char kTestData[] = "9876543210";
383 ASSERT_EQ(10, WriteFile(test_path(), kTestData, 10));
384 File::Info info;
385 GetFileInfo(test_path(), &info);
386 ASSERT_EQ(10, info.size);
387
388 // Run.
389 FileUtilProxy::Truncate(
390 file_task_runner(),
391 GetTestPlatformFile(PLATFORM_FILE_OPEN | PLATFORM_FILE_WRITE),
392 53,
393 Bind(&FileUtilProxyTest::DidFinish, weak_factory_.GetWeakPtr()));
394 MessageLoop::current()->Run();
395
396 // Verify.
397 GetFileInfo(test_path(), &info);
398 ASSERT_EQ(53, info.size);
399
400 char buffer[53];
401 EXPECT_EQ(53, base::ReadFile(test_path(), buffer, 53));
402 int i = 0;
403 for (; i < 10; ++i)
404 EXPECT_EQ(kTestData[i], buffer[i]);
405 for (; i < 53; ++i)
406 EXPECT_EQ(0, buffer[i]);
407 }
408
409 } // namespace base 135 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util_proxy.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698