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