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