| 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 <string> | |
| 6 | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/message_loop_proxy.h" | |
| 11 #include "base/platform_file.h" | |
| 12 #include "base/strings/sys_string_conversions.h" | |
| 13 #include "base/utf_string_conversions.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 #include "webkit/fileapi/async_file_test_helper.h" | |
| 16 #include "webkit/fileapi/file_system_context.h" | |
| 17 #include "webkit/fileapi/file_system_file_util.h" | |
| 18 #include "webkit/fileapi/file_system_operation_context.h" | |
| 19 #include "webkit/fileapi/file_system_types.h" | |
| 20 #include "webkit/fileapi/local_file_system_test_helper.h" | |
| 21 #include "webkit/fileapi/local_file_util.h" | |
| 22 #include "webkit/fileapi/native_file_util.h" | |
| 23 | |
| 24 namespace fileapi { | |
| 25 | |
| 26 // TODO(dmikurube): Cover all public methods in LocalFileUtil. | |
| 27 class LocalFileUtilTest : public testing::Test { | |
| 28 public: | |
| 29 LocalFileUtilTest() | |
| 30 : test_helper_(GURL("http://foo/"), kFileSystemTypeTest) {} | |
| 31 | |
| 32 virtual void SetUp() { | |
| 33 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 34 test_helper_.SetUp(data_dir_.path()); | |
| 35 } | |
| 36 | |
| 37 virtual void TearDown() { | |
| 38 test_helper_.TearDown(); | |
| 39 } | |
| 40 | |
| 41 protected: | |
| 42 FileSystemOperationContext* NewContext() { | |
| 43 FileSystemOperationContext* context = test_helper_.NewOperationContext(); | |
| 44 return context; | |
| 45 } | |
| 46 | |
| 47 LocalFileUtil* FileUtil() { | |
| 48 return static_cast<LocalFileUtil*>(test_helper_.file_util()); | |
| 49 } | |
| 50 | |
| 51 FileSystemURL Path(const std::string& file_name) { | |
| 52 return test_helper_.CreateURLFromUTF8(file_name); | |
| 53 } | |
| 54 | |
| 55 base::FilePath LocalPath(const char *file_name) { | |
| 56 return test_helper_.GetLocalPathFromASCII(file_name); | |
| 57 } | |
| 58 | |
| 59 bool FileExists(const char *file_name) { | |
| 60 return file_util::PathExists(LocalPath(file_name)) && | |
| 61 !file_util::DirectoryExists(LocalPath(file_name)); | |
| 62 } | |
| 63 | |
| 64 bool DirectoryExists(const char *file_name) { | |
| 65 return file_util::DirectoryExists(LocalPath(file_name)); | |
| 66 } | |
| 67 | |
| 68 int64 GetSize(const char *file_name) { | |
| 69 base::PlatformFileInfo info; | |
| 70 file_util::GetFileInfo(LocalPath(file_name), &info); | |
| 71 return info.size; | |
| 72 } | |
| 73 | |
| 74 base::PlatformFileError CreateFile(const char* file_name, | |
| 75 base::PlatformFile* file_handle, | |
| 76 bool* created) { | |
| 77 int file_flags = base::PLATFORM_FILE_CREATE | | |
| 78 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC; | |
| 79 | |
| 80 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 81 return FileUtil()->CreateOrOpen( | |
| 82 context.get(), | |
| 83 Path(file_name), | |
| 84 file_flags, file_handle, created); | |
| 85 } | |
| 86 | |
| 87 base::PlatformFileError EnsureFileExists(const char* file_name, | |
| 88 bool* created) { | |
| 89 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 90 return FileUtil()->EnsureFileExists( | |
| 91 context.get(), | |
| 92 Path(file_name), created); | |
| 93 } | |
| 94 | |
| 95 const LocalFileSystemTestOriginHelper& test_helper() const { | |
| 96 return test_helper_; | |
| 97 } | |
| 98 | |
| 99 FileSystemContext* file_system_context() { | |
| 100 return test_helper_.file_system_context(); | |
| 101 } | |
| 102 | |
| 103 private: | |
| 104 scoped_ptr<LocalFileUtil> local_file_util_; | |
| 105 base::ScopedTempDir data_dir_; | |
| 106 base::MessageLoop message_loop_; | |
| 107 LocalFileSystemTestOriginHelper test_helper_; | |
| 108 | |
| 109 DISALLOW_COPY_AND_ASSIGN(LocalFileUtilTest); | |
| 110 }; | |
| 111 | |
| 112 TEST_F(LocalFileUtilTest, CreateAndClose) { | |
| 113 const char *file_name = "test_file"; | |
| 114 base::PlatformFile file_handle; | |
| 115 bool created; | |
| 116 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 117 CreateFile(file_name, &file_handle, &created)); | |
| 118 ASSERT_TRUE(created); | |
| 119 | |
| 120 EXPECT_TRUE(FileExists(file_name)); | |
| 121 EXPECT_EQ(0, GetSize(file_name)); | |
| 122 | |
| 123 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 124 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 125 FileUtil()->Close(context.get(), file_handle)); | |
| 126 } | |
| 127 | |
| 128 // file_util::CreateSymbolicLink is only supported on POSIX. | |
| 129 #if defined(OS_POSIX) | |
| 130 TEST_F(LocalFileUtilTest, CreateFailForSymlink) { | |
| 131 // Create symlink target file. | |
| 132 const char *target_name = "symlink_target"; | |
| 133 base::PlatformFile target_handle; | |
| 134 bool symlink_target_created = false; | |
| 135 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 136 CreateFile(target_name, &target_handle, &symlink_target_created)); | |
| 137 ASSERT_TRUE(symlink_target_created); | |
| 138 base::FilePath target_path = LocalPath(target_name); | |
| 139 | |
| 140 // Create symlink where target must be real file. | |
| 141 const char *symlink_name = "symlink_file"; | |
| 142 base::FilePath symlink_path = LocalPath(symlink_name); | |
| 143 ASSERT_TRUE(file_util::CreateSymbolicLink(target_path, symlink_path)); | |
| 144 ASSERT_TRUE(FileExists(symlink_name)); | |
| 145 | |
| 146 // Try to open the symlink file which should fail. | |
| 147 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 148 FileSystemURL url = test_helper().CreateURLFromUTF8(symlink_name); | |
| 149 int file_flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ; | |
| 150 base::PlatformFile file_handle; | |
| 151 bool created = false; | |
| 152 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, FileUtil()->CreateOrOpen( | |
| 153 context.get(), url, file_flags, &file_handle, &created)); | |
| 154 EXPECT_FALSE(created); | |
| 155 } | |
| 156 #endif | |
| 157 | |
| 158 TEST_F(LocalFileUtilTest, EnsureFileExists) { | |
| 159 const char *file_name = "foobar"; | |
| 160 bool created; | |
| 161 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
| 162 ASSERT_TRUE(created); | |
| 163 | |
| 164 EXPECT_TRUE(FileExists(file_name)); | |
| 165 EXPECT_EQ(0, GetSize(file_name)); | |
| 166 | |
| 167 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
| 168 EXPECT_FALSE(created); | |
| 169 } | |
| 170 | |
| 171 TEST_F(LocalFileUtilTest, TouchFile) { | |
| 172 const char *file_name = "test_file"; | |
| 173 base::PlatformFile file_handle; | |
| 174 bool created; | |
| 175 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 176 CreateFile(file_name, &file_handle, &created)); | |
| 177 ASSERT_TRUE(created); | |
| 178 | |
| 179 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 180 | |
| 181 base::PlatformFileInfo info; | |
| 182 ASSERT_TRUE(file_util::GetFileInfo(LocalPath(file_name), &info)); | |
| 183 const base::Time new_accessed = | |
| 184 info.last_accessed + base::TimeDelta::FromHours(10); | |
| 185 const base::Time new_modified = | |
| 186 info.last_modified + base::TimeDelta::FromHours(5); | |
| 187 | |
| 188 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 189 FileUtil()->Touch(context.get(), Path(file_name), | |
| 190 new_accessed, new_modified)); | |
| 191 | |
| 192 ASSERT_TRUE(file_util::GetFileInfo(LocalPath(file_name), &info)); | |
| 193 EXPECT_EQ(new_accessed, info.last_accessed); | |
| 194 EXPECT_EQ(new_modified, info.last_modified); | |
| 195 | |
| 196 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 197 FileUtil()->Close(context.get(), file_handle)); | |
| 198 } | |
| 199 | |
| 200 TEST_F(LocalFileUtilTest, TouchDirectory) { | |
| 201 const char *dir_name = "test_dir"; | |
| 202 scoped_ptr<FileSystemOperationContext> context(NewContext()); | |
| 203 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 204 FileUtil()->CreateDirectory(context.get(), | |
| 205 Path(dir_name), | |
| 206 false /* exclusive */, | |
| 207 false /* recursive */)); | |
| 208 | |
| 209 base::PlatformFileInfo info; | |
| 210 ASSERT_TRUE(file_util::GetFileInfo(LocalPath(dir_name), &info)); | |
| 211 const base::Time new_accessed = | |
| 212 info.last_accessed + base::TimeDelta::FromHours(10); | |
| 213 const base::Time new_modified = | |
| 214 info.last_modified + base::TimeDelta::FromHours(5); | |
| 215 | |
| 216 EXPECT_EQ(base::PLATFORM_FILE_OK, | |
| 217 FileUtil()->Touch(context.get(), Path(dir_name), | |
| 218 new_accessed, new_modified)); | |
| 219 | |
| 220 ASSERT_TRUE(file_util::GetFileInfo(LocalPath(dir_name), &info)); | |
| 221 EXPECT_EQ(new_accessed, info.last_accessed); | |
| 222 EXPECT_EQ(new_modified, info.last_modified); | |
| 223 } | |
| 224 | |
| 225 TEST_F(LocalFileUtilTest, Truncate) { | |
| 226 const char *file_name = "truncated"; | |
| 227 bool created; | |
| 228 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); | |
| 229 ASSERT_TRUE(created); | |
| 230 | |
| 231 scoped_ptr<FileSystemOperationContext> context; | |
| 232 | |
| 233 context.reset(NewContext()); | |
| 234 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 235 FileUtil()->Truncate(context.get(), Path(file_name), 1020)); | |
| 236 | |
| 237 EXPECT_TRUE(FileExists(file_name)); | |
| 238 EXPECT_EQ(1020, GetSize(file_name)); | |
| 239 } | |
| 240 | |
| 241 TEST_F(LocalFileUtilTest, CopyFile) { | |
| 242 const char *from_file = "fromfile"; | |
| 243 const char *to_file1 = "tofile1"; | |
| 244 const char *to_file2 = "tofile2"; | |
| 245 bool created; | |
| 246 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
| 247 ASSERT_TRUE(created); | |
| 248 | |
| 249 scoped_ptr<FileSystemOperationContext> context; | |
| 250 context.reset(NewContext()); | |
| 251 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 252 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
| 253 | |
| 254 EXPECT_TRUE(FileExists(from_file)); | |
| 255 EXPECT_EQ(1020, GetSize(from_file)); | |
| 256 | |
| 257 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 258 AsyncFileTestHelper::Copy(file_system_context(), | |
| 259 Path(from_file), Path(to_file1))); | |
| 260 | |
| 261 context.reset(NewContext()); | |
| 262 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 263 AsyncFileTestHelper::Copy(file_system_context(), | |
| 264 Path(from_file), Path(to_file2))); | |
| 265 | |
| 266 EXPECT_TRUE(FileExists(from_file)); | |
| 267 EXPECT_EQ(1020, GetSize(from_file)); | |
| 268 EXPECT_TRUE(FileExists(to_file1)); | |
| 269 EXPECT_EQ(1020, GetSize(to_file1)); | |
| 270 EXPECT_TRUE(FileExists(to_file2)); | |
| 271 EXPECT_EQ(1020, GetSize(to_file2)); | |
| 272 } | |
| 273 | |
| 274 TEST_F(LocalFileUtilTest, CopyDirectory) { | |
| 275 const char *from_dir = "fromdir"; | |
| 276 const char *from_file = "fromdir/fromfile"; | |
| 277 const char *to_dir = "todir"; | |
| 278 const char *to_file = "todir/fromfile"; | |
| 279 bool created; | |
| 280 scoped_ptr<FileSystemOperationContext> context; | |
| 281 | |
| 282 context.reset(NewContext()); | |
| 283 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 284 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | |
| 285 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
| 286 ASSERT_TRUE(created); | |
| 287 | |
| 288 context.reset(NewContext()); | |
| 289 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 290 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
| 291 | |
| 292 EXPECT_TRUE(DirectoryExists(from_dir)); | |
| 293 EXPECT_TRUE(FileExists(from_file)); | |
| 294 EXPECT_EQ(1020, GetSize(from_file)); | |
| 295 EXPECT_FALSE(DirectoryExists(to_dir)); | |
| 296 | |
| 297 context.reset(NewContext()); | |
| 298 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 299 AsyncFileTestHelper::Copy(file_system_context(), | |
| 300 Path(from_dir), Path(to_dir))); | |
| 301 | |
| 302 EXPECT_TRUE(DirectoryExists(from_dir)); | |
| 303 EXPECT_TRUE(FileExists(from_file)); | |
| 304 EXPECT_EQ(1020, GetSize(from_file)); | |
| 305 EXPECT_TRUE(DirectoryExists(to_dir)); | |
| 306 EXPECT_TRUE(FileExists(to_file)); | |
| 307 EXPECT_EQ(1020, GetSize(to_file)); | |
| 308 } | |
| 309 | |
| 310 TEST_F(LocalFileUtilTest, MoveFile) { | |
| 311 const char *from_file = "fromfile"; | |
| 312 const char *to_file = "tofile"; | |
| 313 bool created; | |
| 314 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
| 315 ASSERT_TRUE(created); | |
| 316 scoped_ptr<FileSystemOperationContext> context; | |
| 317 | |
| 318 context.reset(NewContext()); | |
| 319 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 320 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
| 321 | |
| 322 EXPECT_TRUE(FileExists(from_file)); | |
| 323 EXPECT_EQ(1020, GetSize(from_file)); | |
| 324 | |
| 325 context.reset(NewContext()); | |
| 326 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 327 AsyncFileTestHelper::Move(file_system_context(), | |
| 328 Path(from_file), Path(to_file))); | |
| 329 | |
| 330 EXPECT_FALSE(FileExists(from_file)); | |
| 331 EXPECT_TRUE(FileExists(to_file)); | |
| 332 EXPECT_EQ(1020, GetSize(to_file)); | |
| 333 } | |
| 334 | |
| 335 TEST_F(LocalFileUtilTest, MoveDirectory) { | |
| 336 const char *from_dir = "fromdir"; | |
| 337 const char *from_file = "fromdir/fromfile"; | |
| 338 const char *to_dir = "todir"; | |
| 339 const char *to_file = "todir/fromfile"; | |
| 340 bool created; | |
| 341 scoped_ptr<FileSystemOperationContext> context; | |
| 342 | |
| 343 context.reset(NewContext()); | |
| 344 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 345 FileUtil()->CreateDirectory(context.get(), Path(from_dir), false, false)); | |
| 346 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); | |
| 347 ASSERT_TRUE(created); | |
| 348 | |
| 349 context.reset(NewContext()); | |
| 350 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 351 FileUtil()->Truncate(context.get(), Path(from_file), 1020)); | |
| 352 | |
| 353 EXPECT_TRUE(DirectoryExists(from_dir)); | |
| 354 EXPECT_TRUE(FileExists(from_file)); | |
| 355 EXPECT_EQ(1020, GetSize(from_file)); | |
| 356 EXPECT_FALSE(DirectoryExists(to_dir)); | |
| 357 | |
| 358 context.reset(NewContext()); | |
| 359 ASSERT_EQ(base::PLATFORM_FILE_OK, | |
| 360 AsyncFileTestHelper::Move(file_system_context(), | |
| 361 Path(from_dir), Path(to_dir))); | |
| 362 | |
| 363 EXPECT_FALSE(DirectoryExists(from_dir)); | |
| 364 EXPECT_TRUE(DirectoryExists(to_dir)); | |
| 365 EXPECT_TRUE(FileExists(to_file)); | |
| 366 EXPECT_EQ(1020, GetSize(to_file)); | |
| 367 } | |
| 368 | |
| 369 } // namespace fileapi | |
| OLD | NEW |