| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 5 #include <algorithm> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #elif defined(OS_WIN) | 29 #elif defined(OS_WIN) |
| 30 result = base::SysUTF8ToWide(str); | 30 result = base::SysUTF8ToWide(str); |
| 31 #endif | 31 #endif |
| 32 return FilePath(result); | 32 return FilePath(result); |
| 33 } | 33 } |
| 34 | 34 |
| 35 bool FileExists(const FilePath& path) { | 35 bool FileExists(const FilePath& path) { |
| 36 return file_util::PathExists(path) && !file_util::DirectoryExists(path); | 36 return file_util::PathExists(path) && !file_util::DirectoryExists(path); |
| 37 } | 37 } |
| 38 | 38 |
| 39 int64 GetSize(const FilePath& path) { |
| 40 int64 size; |
| 41 EXPECT_TRUE(file_util::GetFileSize(path, &size)); |
| 42 return size; |
| 43 } |
| 44 |
| 39 // After a move, the dest exists and the source doesn't. | 45 // After a move, the dest exists and the source doesn't. |
| 40 // After a copy, both source and dest exist. | 46 // After a copy, both source and dest exist. |
| 41 struct CopyMoveTestCaseRecord { | 47 struct CopyMoveTestCaseRecord { |
| 42 bool is_copy_not_move; | 48 bool is_copy_not_move; |
| 43 const char source_path[64]; | 49 const char source_path[64]; |
| 44 const char dest_path[64]; | 50 const char dest_path[64]; |
| 45 bool cause_overwrite; | 51 bool cause_overwrite; |
| 46 }; | 52 }; |
| 47 | 53 |
| 48 const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { | 54 const CopyMoveTestCaseRecord kCopyMoveTestCases[] = { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 } | 156 } |
| 151 | 157 |
| 152 const GURL& origin_url() const { | 158 const GURL& origin_url() const { |
| 153 return origin_; | 159 return origin_; |
| 154 } | 160 } |
| 155 | 161 |
| 156 fileapi::FileSystemType type() const { | 162 fileapi::FileSystemType type() const { |
| 157 return type_; | 163 return type_; |
| 158 } | 164 } |
| 159 | 165 |
| 160 int64 GetSize(const FilePath& path) { | |
| 161 int64 size; | |
| 162 EXPECT_TRUE(file_util::GetFileSize(path, &size)); | |
| 163 return size; | |
| 164 } | |
| 165 | |
| 166 void CheckFileAndCloseHandle( | 166 void CheckFileAndCloseHandle( |
| 167 const FilePath& virtual_path, PlatformFile file_handle) { | 167 const FilePath& virtual_path, PlatformFile file_handle) { |
| 168 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 168 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 169 FilePath local_path; | 169 FilePath local_path; |
| 170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | 170 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( |
| 171 context.get(), virtual_path, &local_path)); | 171 context.get(), virtual_path, &local_path)); |
| 172 | 172 |
| 173 base::PlatformFileInfo file_info0; | 173 base::PlatformFileInfo file_info0; |
| 174 FilePath data_path; | 174 FilePath data_path; |
| 175 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 175 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( |
| 176 context.get(), virtual_path, &file_info0, &data_path)); | 176 context.get(), virtual_path, &file_info0, &data_path)); |
| 177 EXPECT_EQ(data_path, local_path); | 177 EXPECT_EQ(data_path, local_path); |
| 178 EXPECT_TRUE(FileExists(data_path)); | 178 EXPECT_TRUE(FileExists(data_path)); |
| 179 EXPECT_EQ(0, GetSize(data_path)); | 179 EXPECT_EQ(0, GetSize(data_path)); |
| 180 | 180 |
| 181 const char data[] = "test data"; | 181 const char data[] = "test data"; |
| 182 const int length = arraysize(data) - 1; | 182 const int length = arraysize(data) - 1; |
| 183 | 183 |
| 184 if (base::kInvalidPlatformFileValue == file_handle) { | 184 if (base::kInvalidPlatformFileValue == file_handle) { |
| 185 bool created = true; | 185 bool created = true; |
| 186 PlatformFileError error; | 186 PlatformFileError error; |
| 187 file_handle = base::CreatePlatformFile( | 187 file_handle = base::CreatePlatformFile( |
| 188 data_path, | 188 data_path, |
| 189 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 189 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 190 &created, | 190 &created, |
| 191 &error); | 191 &error); |
| 192 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 192 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | 193 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 193 EXPECT_FALSE(created); | 194 EXPECT_FALSE(created); |
| 194 } | 195 } |
| 195 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); | 196 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); |
| 196 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | 197 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 197 | 198 |
| 198 base::PlatformFileInfo file_info1; | 199 base::PlatformFileInfo file_info1; |
| 199 EXPECT_EQ(length, GetSize(data_path)); | 200 EXPECT_EQ(length, GetSize(data_path)); |
| 200 context.reset(NewContext()); | 201 context.reset(NewContext()); |
| 201 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 202 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 last_modified_time += base::TimeDelta::FromHours(1); | 334 last_modified_time += base::TimeDelta::FromHours(1); |
| 334 EXPECT_EQ(base::PLATFORM_FILE_OK, | 335 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 335 ofsfu()->Touch( | 336 ofsfu()->Touch( |
| 336 context.get(), path, last_access_time, last_modified_time)); | 337 context.get(), path, last_access_time, last_modified_time)); |
| 337 context.reset(NewContext()); | 338 context.reset(NewContext()); |
| 338 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 339 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( |
| 339 context.get(), path, &file_info, &local_path)); | 340 context.get(), path, &file_info, &local_path)); |
| 340 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); | 341 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 341 } | 342 } |
| 342 | 343 |
| 344 void TestCopyInForeignFileHelper(bool overwrite) { |
| 345 ScopedTempDir source_dir; |
| 346 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 347 FilePath root_path = source_dir.path(); |
| 348 FilePath src_path = root_path.AppendASCII("file_name"); |
| 349 FilePath dest_path(FILE_PATH_LITERAL("new file")); |
| 350 int64 src_file_length = 87; |
| 351 |
| 352 base::PlatformFileError error_code; |
| 353 bool created = false; |
| 354 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 355 base::PlatformFile file_handle = |
| 356 base::CreatePlatformFile( |
| 357 src_path, file_flags, &created, &error_code); |
| 358 EXPECT_TRUE(created); |
| 359 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 360 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 361 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); |
| 362 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 363 |
| 364 scoped_ptr<FileSystemOperationContext> context; |
| 365 |
| 366 if (overwrite) { |
| 367 context.reset(NewContext()); |
| 368 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 369 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); |
| 370 EXPECT_TRUE(created); |
| 371 } |
| 372 |
| 373 context.reset(NewContext()); |
| 374 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 375 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); |
| 376 context.reset(NewContext()); |
| 377 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path)); |
| 378 context.reset(NewContext()); |
| 379 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); |
| 380 context.reset(NewContext()); |
| 381 base::PlatformFileInfo file_info; |
| 382 FilePath data_path; |
| 383 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( |
| 384 context.get(), dest_path, &file_info, &data_path)); |
| 385 EXPECT_NE(data_path, src_path); |
| 386 EXPECT_TRUE(FileExists(data_path)); |
| 387 EXPECT_EQ(src_file_length, GetSize(data_path)); |
| 388 |
| 389 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 390 ofsfu()->DeleteFile(context.get(), dest_path)); |
| 391 } |
| 392 |
| 343 private: | 393 private: |
| 344 ScopedTempDir data_dir_; | 394 ScopedTempDir data_dir_; |
| 345 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; | 395 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; |
| 346 GURL origin_; | 396 GURL origin_; |
| 347 fileapi::FileSystemType type_; | 397 fileapi::FileSystemType type_; |
| 348 FileSystemTestOriginHelper test_helper_; | 398 FileSystemTestOriginHelper test_helper_; |
| 349 | 399 |
| 350 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); | 400 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); |
| 351 }; | 401 }; |
| 352 | 402 |
| (...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 774 FilePath local_path; | 824 FilePath local_path; |
| 775 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 825 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( |
| 776 context.get(), dest_path, &file_info, &local_path)); | 826 context.get(), dest_path, &file_info, &local_path)); |
| 777 EXPECT_EQ(kSourceLength, file_info.size); | 827 EXPECT_EQ(kSourceLength, file_info.size); |
| 778 | 828 |
| 779 EXPECT_EQ(base::PLATFORM_FILE_OK, | 829 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 780 ofsfu()->DeleteFile(context.get(), dest_path)); | 830 ofsfu()->DeleteFile(context.get(), dest_path)); |
| 781 } | 831 } |
| 782 } | 832 } |
| 783 | 833 |
| 834 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) { |
| 835 TestCopyInForeignFileHelper(false /* overwrite */); |
| 836 TestCopyInForeignFileHelper(true /* overwrite */); |
| 837 } |
| 838 |
| 784 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) { | 839 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) { |
| 785 scoped_ptr<FileSystemOperationContext> context(NewContext()); | 840 scoped_ptr<FileSystemOperationContext> context(NewContext()); |
| 786 FilePath src_path = UTF8ToFilePath("source dir"); | 841 FilePath src_path = UTF8ToFilePath("source dir"); |
| 787 bool exclusive = true; | 842 bool exclusive = true; |
| 788 bool recursive = false; | 843 bool recursive = false; |
| 789 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 844 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( |
| 790 context.get(), src_path, exclusive, recursive)); | 845 context.get(), src_path, exclusive, recursive)); |
| 791 | 846 |
| 792 std::set<FilePath::StringType> files; | 847 std::set<FilePath::StringType> files; |
| 793 std::set<FilePath::StringType> directories; | 848 std::set<FilePath::StringType> directories; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 828 ASSERT_TRUE( | 883 ASSERT_TRUE( |
| 829 file_util::CreateDirectory(local_src_path)); | 884 file_util::CreateDirectory(local_src_path)); |
| 830 } else { | 885 } else { |
| 831 base::PlatformFileError error_code; | 886 base::PlatformFileError error_code; |
| 832 bool created = false; | 887 bool created = false; |
| 833 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 888 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 834 base::PlatformFile file_handle = | 889 base::PlatformFile file_handle = |
| 835 base::CreatePlatformFile( | 890 base::CreatePlatformFile( |
| 836 local_src_path, file_flags, &created, &error_code); | 891 local_src_path, file_flags, &created, &error_code); |
| 837 EXPECT_TRUE(created); | 892 EXPECT_TRUE(created); |
| 893 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 838 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | 894 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 839 ASSERT_TRUE( | 895 ASSERT_TRUE( |
| 840 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); | 896 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); |
| 841 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | 897 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 842 } | 898 } |
| 843 } | 899 } |
| 844 | 900 |
| 845 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url(), type(), root_path)); | 901 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin_url(), type(), root_path)); |
| 846 | 902 |
| 847 FilePath new_root = | 903 FilePath new_root = |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 939 } | 995 } |
| 940 EXPECT_TRUE(found); | 996 EXPECT_TRUE(found); |
| 941 } | 997 } |
| 942 | 998 |
| 943 std::set<GURL> diff; | 999 std::set<GURL> diff; |
| 944 std::set_symmetric_difference(origins_expected.begin(), | 1000 std::set_symmetric_difference(origins_expected.begin(), |
| 945 origins_expected.end(), origins_found.begin(), origins_found.end(), | 1001 origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 946 inserter(diff, diff.begin())); | 1002 inserter(diff, diff.begin())); |
| 947 EXPECT_TRUE(diff.empty()); | 1003 EXPECT_TRUE(diff.empty()); |
| 948 } | 1004 } |
| OLD | NEW |