| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "storage/browser/fileapi/file_system_operation_impl.h" | 5 #include "storage/browser/fileapi/file_system_operation_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
| 7 #include "base/bind.h" | 10 #include "base/bind.h" |
| 8 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 9 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 13 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 14 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 15 #include "base/thread_task_runner_handle.h" | 19 #include "base/thread_task_runner_handle.h" |
| 16 #include "content/browser/fileapi/mock_file_change_observer.h" | 20 #include "content/browser/fileapi/mock_file_change_observer.h" |
| 17 #include "content/browser/fileapi/mock_file_update_observer.h" | 21 #include "content/browser/fileapi/mock_file_update_observer.h" |
| 18 #include "content/browser/quota/mock_quota_manager.h" | 22 #include "content/browser/quota/mock_quota_manager.h" |
| 19 #include "content/browser/quota/mock_quota_manager_proxy.h" | 23 #include "content/browser/quota/mock_quota_manager_proxy.h" |
| 20 #include "content/public/test/async_file_test_helper.h" | 24 #include "content/public/test/async_file_test_helper.h" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 } | 152 } |
| 149 | 153 |
| 150 FileSystemURL CreateDirectory(const std::string& path) { | 154 FileSystemURL CreateDirectory(const std::string& path) { |
| 151 FileSystemURL url = URLForPath(path); | 155 FileSystemURL url = URLForPath(path); |
| 152 EXPECT_EQ(base::File::FILE_OK, | 156 EXPECT_EQ(base::File::FILE_OK, |
| 153 file_util()->CreateDirectory(NewContext().get(), url, | 157 file_util()->CreateDirectory(NewContext().get(), url, |
| 154 false /* exclusive */, true)); | 158 false /* exclusive */, true)); |
| 155 return url; | 159 return url; |
| 156 } | 160 } |
| 157 | 161 |
| 158 int64 GetFileSize(const std::string& path) { | 162 int64_t GetFileSize(const std::string& path) { |
| 159 base::File::Info info; | 163 base::File::Info info; |
| 160 EXPECT_TRUE(base::GetFileInfo(PlatformPath(path), &info)); | 164 EXPECT_TRUE(base::GetFileInfo(PlatformPath(path), &info)); |
| 161 return info.size; | 165 return info.size; |
| 162 } | 166 } |
| 163 | 167 |
| 164 // Callbacks for recording test results. | 168 // Callbacks for recording test results. |
| 165 FileSystemOperation::StatusCallback RecordStatusCallback( | 169 FileSystemOperation::StatusCallback RecordStatusCallback( |
| 166 const base::Closure& closure, | 170 const base::Closure& closure, |
| 167 base::File::Error* status) { | 171 base::File::Error* status) { |
| 168 return base::Bind(&FileSystemOperationImplTest::DidFinish, | 172 return base::Bind(&FileSystemOperationImplTest::DidFinish, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 const base::File::Info& info, | 235 const base::File::Info& info, |
| 232 const base::FilePath& platform_path, | 236 const base::FilePath& platform_path, |
| 233 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { | 237 const scoped_refptr<ShareableFileReference>& shareable_file_ref) { |
| 234 info_ = info; | 238 info_ = info; |
| 235 path_ = platform_path; | 239 path_ = platform_path; |
| 236 *status = actual; | 240 *status = actual; |
| 237 shareable_file_ref_ = shareable_file_ref; | 241 shareable_file_ref_ = shareable_file_ref; |
| 238 closure.Run(); | 242 closure.Run(); |
| 239 } | 243 } |
| 240 | 244 |
| 241 int64 GetDataSizeOnDisk() { | 245 int64_t GetDataSizeOnDisk() { |
| 242 return sandbox_file_system_.ComputeCurrentOriginUsage() - | 246 return sandbox_file_system_.ComputeCurrentOriginUsage() - |
| 243 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage(); | 247 sandbox_file_system_.ComputeCurrentDirectoryDatabaseUsage(); |
| 244 } | 248 } |
| 245 | 249 |
| 246 void GetUsageAndQuota(int64* usage, int64* quota) { | 250 void GetUsageAndQuota(int64_t* usage, int64_t* quota) { |
| 247 storage::QuotaStatusCode status = | 251 storage::QuotaStatusCode status = |
| 248 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(), | 252 AsyncFileTestHelper::GetUsageAndQuota(quota_manager_.get(), |
| 249 sandbox_file_system_.origin(), | 253 sandbox_file_system_.origin(), |
| 250 sandbox_file_system_.type(), | 254 sandbox_file_system_.type(), |
| 251 usage, | 255 usage, |
| 252 quota); | 256 quota); |
| 253 base::RunLoop().RunUntilIdle(); | 257 base::RunLoop().RunUntilIdle(); |
| 254 ASSERT_EQ(storage::kQuotaStatusOk, status); | 258 ASSERT_EQ(storage::kQuotaStatusOk, status); |
| 255 } | 259 } |
| 256 | 260 |
| 257 int64 ComputePathCost(const FileSystemURL& url) { | 261 int64_t ComputePathCost(const FileSystemURL& url) { |
| 258 int64 base_usage; | 262 int64_t base_usage; |
| 259 GetUsageAndQuota(&base_usage, NULL); | 263 GetUsageAndQuota(&base_usage, NULL); |
| 260 | 264 |
| 261 AsyncFileTestHelper::CreateFile( | 265 AsyncFileTestHelper::CreateFile( |
| 262 sandbox_file_system_.file_system_context(), url); | 266 sandbox_file_system_.file_system_context(), url); |
| 263 EXPECT_EQ(base::File::FILE_OK, Remove(url, false /* recursive */)); | 267 EXPECT_EQ(base::File::FILE_OK, Remove(url, false /* recursive */)); |
| 264 | 268 |
| 265 change_observer()->ResetCount(); | 269 change_observer()->ResetCount(); |
| 266 | 270 |
| 267 int64 total_usage; | 271 int64_t total_usage; |
| 268 GetUsageAndQuota(&total_usage, NULL); | 272 GetUsageAndQuota(&total_usage, NULL); |
| 269 return total_usage - base_usage; | 273 return total_usage - base_usage; |
| 270 } | 274 } |
| 271 | 275 |
| 272 void GrantQuotaForCurrentUsage() { | 276 void GrantQuotaForCurrentUsage() { |
| 273 int64 usage; | 277 int64_t usage; |
| 274 GetUsageAndQuota(&usage, NULL); | 278 GetUsageAndQuota(&usage, NULL); |
| 275 quota_manager()->SetQuota(sandbox_file_system_.origin(), | 279 quota_manager()->SetQuota(sandbox_file_system_.origin(), |
| 276 sandbox_file_system_.storage_type(), | 280 sandbox_file_system_.storage_type(), |
| 277 usage); | 281 usage); |
| 278 } | 282 } |
| 279 | 283 |
| 280 int64 GetUsage() { | 284 int64_t GetUsage() { |
| 281 int64 usage = 0; | 285 int64_t usage = 0; |
| 282 GetUsageAndQuota(&usage, NULL); | 286 GetUsageAndQuota(&usage, NULL); |
| 283 return usage; | 287 return usage; |
| 284 } | 288 } |
| 285 | 289 |
| 286 void AddQuota(int64 quota_delta) { | 290 void AddQuota(int64_t quota_delta) { |
| 287 int64 quota; | 291 int64_t quota; |
| 288 GetUsageAndQuota(NULL, "a); | 292 GetUsageAndQuota(NULL, "a); |
| 289 quota_manager()->SetQuota(sandbox_file_system_.origin(), | 293 quota_manager()->SetQuota(sandbox_file_system_.origin(), |
| 290 sandbox_file_system_.storage_type(), | 294 sandbox_file_system_.storage_type(), |
| 291 quota + quota_delta); | 295 quota + quota_delta); |
| 292 } | 296 } |
| 293 | 297 |
| 294 base::File::Error Move( | 298 base::File::Error Move( |
| 295 const FileSystemURL& src, | 299 const FileSystemURL& src, |
| 296 const FileSystemURL& dest, | 300 const FileSystemURL& dest, |
| 297 storage::FileSystemOperation::CopyOrMoveOption option) { | 301 storage::FileSystemOperation::CopyOrMoveOption option) { |
| (...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 } | 704 } |
| 701 | 705 |
| 702 TEST_F(FileSystemOperationImplTest, TestCopyFailureByQuota) { | 706 TEST_F(FileSystemOperationImplTest, TestCopyFailureByQuota) { |
| 703 FileSystemURL src_dir(CreateDirectory("src")); | 707 FileSystemURL src_dir(CreateDirectory("src")); |
| 704 FileSystemURL src_file(CreateFile("src/file")); | 708 FileSystemURL src_file(CreateFile("src/file")); |
| 705 FileSystemURL dest_dir(CreateDirectory("dest")); | 709 FileSystemURL dest_dir(CreateDirectory("dest")); |
| 706 EXPECT_EQ(base::File::FILE_OK, Truncate(src_file, 6)); | 710 EXPECT_EQ(base::File::FILE_OK, Truncate(src_file, 6)); |
| 707 EXPECT_EQ(6, GetFileSize("src/file")); | 711 EXPECT_EQ(6, GetFileSize("src/file")); |
| 708 | 712 |
| 709 FileSystemURL dest_file(URLForPath("dest/file")); | 713 FileSystemURL dest_file(URLForPath("dest/file")); |
| 710 int64 dest_path_cost = ComputePathCost(dest_file); | 714 int64_t dest_path_cost = ComputePathCost(dest_file); |
| 711 GrantQuotaForCurrentUsage(); | 715 GrantQuotaForCurrentUsage(); |
| 712 AddQuota(6 + dest_path_cost - 1); | 716 AddQuota(6 + dest_path_cost - 1); |
| 713 | 717 |
| 714 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, | 718 EXPECT_EQ(base::File::FILE_ERROR_NO_SPACE, |
| 715 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE)); | 719 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE)); |
| 716 EXPECT_FALSE(FileExists("dest/file")); | 720 EXPECT_FALSE(FileExists("dest/file")); |
| 717 } | 721 } |
| 718 | 722 |
| 719 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndOverwrite) { | 723 TEST_F(FileSystemOperationImplTest, TestCopySuccessSrcFileAndOverwrite) { |
| 720 FileSystemURL src_file(CreateFile("src")); | 724 FileSystemURL src_file(CreateFile("src")); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 | 820 |
| 817 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileSuccess) { | 821 TEST_F(FileSystemOperationImplTest, TestCopyInForeignFileSuccess) { |
| 818 base::FilePath src_local_disk_file_path; | 822 base::FilePath src_local_disk_file_path; |
| 819 base::CreateTemporaryFile(&src_local_disk_file_path); | 823 base::CreateTemporaryFile(&src_local_disk_file_path); |
| 820 const char test_data[] = "foo"; | 824 const char test_data[] = "foo"; |
| 821 int data_size = arraysize(test_data); | 825 int data_size = arraysize(test_data); |
| 822 base::WriteFile(src_local_disk_file_path, test_data, data_size); | 826 base::WriteFile(src_local_disk_file_path, test_data, data_size); |
| 823 | 827 |
| 824 FileSystemURL dest_dir(CreateDirectory("dest")); | 828 FileSystemURL dest_dir(CreateDirectory("dest")); |
| 825 | 829 |
| 826 int64 before_usage; | 830 int64_t before_usage; |
| 827 GetUsageAndQuota(&before_usage, NULL); | 831 GetUsageAndQuota(&before_usage, NULL); |
| 828 | 832 |
| 829 // Check that the file copied and corresponding usage increased. | 833 // Check that the file copied and corresponding usage increased. |
| 830 EXPECT_EQ( | 834 EXPECT_EQ( |
| 831 base::File::FILE_OK, | 835 base::File::FILE_OK, |
| 832 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file"))); | 836 CopyInForeignFile(src_local_disk_file_path, URLForPath("dest/file"))); |
| 833 | 837 |
| 834 EXPECT_EQ(1, change_observer()->create_file_count()); | 838 EXPECT_EQ(1, change_observer()->create_file_count()); |
| 835 EXPECT_TRUE(FileExists("dest/file")); | 839 EXPECT_TRUE(FileExists("dest/file")); |
| 836 int64 after_usage; | 840 int64_t after_usage; |
| 837 GetUsageAndQuota(&after_usage, NULL); | 841 GetUsageAndQuota(&after_usage, NULL); |
| 838 EXPECT_GT(after_usage, before_usage); | 842 EXPECT_GT(after_usage, before_usage); |
| 839 | 843 |
| 840 // Compare contents of src and copied file. | 844 // Compare contents of src and copied file. |
| 841 char buffer[100]; | 845 char buffer[100]; |
| 842 EXPECT_EQ(data_size, base::ReadFile(PlatformPath("dest/file"), | 846 EXPECT_EQ(data_size, base::ReadFile(PlatformPath("dest/file"), |
| 843 buffer, data_size)); | 847 buffer, data_size)); |
| 844 for (int i = 0; i < data_size; ++i) | 848 for (int i = 0; i < data_size; ++i) |
| 845 EXPECT_EQ(test_data[i], buffer[i]); | 849 EXPECT_EQ(test_data[i], buffer[i]); |
| 846 } | 850 } |
| (...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1212 FileSystemURL grandchild_file2(CreateFile("src/dir/file2")); | 1216 FileSystemURL grandchild_file2(CreateFile("src/dir/file2")); |
| 1213 | 1217 |
| 1214 int total_path_cost = GetUsage(); | 1218 int total_path_cost = GetUsage(); |
| 1215 EXPECT_EQ(0, GetDataSizeOnDisk()); | 1219 EXPECT_EQ(0, GetDataSizeOnDisk()); |
| 1216 | 1220 |
| 1217 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 5000)); | 1221 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 5000)); |
| 1218 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 400)); | 1222 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 400)); |
| 1219 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 30)); | 1223 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 30)); |
| 1220 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 2)); | 1224 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 2)); |
| 1221 | 1225 |
| 1222 const int64 all_file_size = 5000 + 400 + 30 + 2; | 1226 const int64_t all_file_size = 5000 + 400 + 30 + 2; |
| 1223 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); | 1227 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); |
| 1224 EXPECT_EQ(all_file_size + total_path_cost, GetUsage()); | 1228 EXPECT_EQ(all_file_size + total_path_cost, GetUsage()); |
| 1225 | 1229 |
| 1226 EXPECT_EQ(base::File::FILE_OK, | 1230 EXPECT_EQ(base::File::FILE_OK, |
| 1227 Move(src, dest, FileSystemOperation::OPTION_NONE)); | 1231 Move(src, dest, FileSystemOperation::OPTION_NONE)); |
| 1228 | 1232 |
| 1229 EXPECT_FALSE(DirectoryExists("src/dir")); | 1233 EXPECT_FALSE(DirectoryExists("src/dir")); |
| 1230 EXPECT_FALSE(FileExists("src/dir/file2")); | 1234 EXPECT_FALSE(FileExists("src/dir/file2")); |
| 1231 EXPECT_TRUE(DirectoryExists("dest/dir")); | 1235 EXPECT_TRUE(DirectoryExists("dest/dir")); |
| 1232 EXPECT_TRUE(FileExists("dest/dir/file2")); | 1236 EXPECT_TRUE(FileExists("dest/dir/file2")); |
| 1233 | 1237 |
| 1234 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); | 1238 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); |
| 1235 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost, | 1239 EXPECT_EQ(all_file_size + total_path_cost - src_path_cost, |
| 1236 GetUsage()); | 1240 GetUsage()); |
| 1237 } | 1241 } |
| 1238 | 1242 |
| 1239 TEST_F(FileSystemOperationImplTest, | 1243 TEST_F(FileSystemOperationImplTest, |
| 1240 TestCopySuccessSrcDirRecursiveWithQuota) { | 1244 TestCopySuccessSrcDirRecursiveWithQuota) { |
| 1241 FileSystemURL src(CreateDirectory("src")); | 1245 FileSystemURL src(CreateDirectory("src")); |
| 1242 FileSystemURL dest1(CreateDirectory("dest1")); | 1246 FileSystemURL dest1(CreateDirectory("dest1")); |
| 1243 FileSystemURL dest2(CreateDirectory("dest2")); | 1247 FileSystemURL dest2(CreateDirectory("dest2")); |
| 1244 | 1248 |
| 1245 int64 usage = GetUsage(); | 1249 int64_t usage = GetUsage(); |
| 1246 FileSystemURL child_file1(CreateFile("src/file1")); | 1250 FileSystemURL child_file1(CreateFile("src/file1")); |
| 1247 FileSystemURL child_file2(CreateFile("src/file2")); | 1251 FileSystemURL child_file2(CreateFile("src/file2")); |
| 1248 FileSystemURL child_dir(CreateDirectory("src/dir")); | 1252 FileSystemURL child_dir(CreateDirectory("src/dir")); |
| 1249 int64 child_path_cost = GetUsage() - usage; | 1253 int64_t child_path_cost = GetUsage() - usage; |
| 1250 usage += child_path_cost; | 1254 usage += child_path_cost; |
| 1251 | 1255 |
| 1252 FileSystemURL grandchild_file1(CreateFile("src/dir/file1")); | 1256 FileSystemURL grandchild_file1(CreateFile("src/dir/file1")); |
| 1253 FileSystemURL grandchild_file2(CreateFile("src/dir/file2")); | 1257 FileSystemURL grandchild_file2(CreateFile("src/dir/file2")); |
| 1254 int64 total_path_cost = GetUsage(); | 1258 int64_t total_path_cost = GetUsage(); |
| 1255 int64 grandchild_path_cost = total_path_cost - usage; | 1259 int64_t grandchild_path_cost = total_path_cost - usage; |
| 1256 | 1260 |
| 1257 EXPECT_EQ(0, GetDataSizeOnDisk()); | 1261 EXPECT_EQ(0, GetDataSizeOnDisk()); |
| 1258 | 1262 |
| 1259 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 8000)); | 1263 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file1, 8000)); |
| 1260 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 700)); | 1264 EXPECT_EQ(base::File::FILE_OK, Truncate(child_file2, 700)); |
| 1261 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 60)); | 1265 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file1, 60)); |
| 1262 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 5)); | 1266 EXPECT_EQ(base::File::FILE_OK, Truncate(grandchild_file2, 5)); |
| 1263 | 1267 |
| 1264 const int64 child_file_size = 8000 + 700; | 1268 const int64_t child_file_size = 8000 + 700; |
| 1265 const int64 grandchild_file_size = 60 + 5; | 1269 const int64_t grandchild_file_size = 60 + 5; |
| 1266 const int64 all_file_size = child_file_size + grandchild_file_size; | 1270 const int64_t all_file_size = child_file_size + grandchild_file_size; |
| 1267 int64 expected_usage = all_file_size + total_path_cost; | 1271 int64_t expected_usage = all_file_size + total_path_cost; |
| 1268 | 1272 |
| 1269 usage = GetUsage(); | 1273 usage = GetUsage(); |
| 1270 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); | 1274 EXPECT_EQ(all_file_size, GetDataSizeOnDisk()); |
| 1271 EXPECT_EQ(expected_usage, usage); | 1275 EXPECT_EQ(expected_usage, usage); |
| 1272 | 1276 |
| 1273 EXPECT_EQ(base::File::FILE_OK, | 1277 EXPECT_EQ(base::File::FILE_OK, |
| 1274 Copy(src, dest1, FileSystemOperation::OPTION_NONE)); | 1278 Copy(src, dest1, FileSystemOperation::OPTION_NONE)); |
| 1275 | 1279 |
| 1276 expected_usage += all_file_size + child_path_cost + grandchild_path_cost; | 1280 expected_usage += all_file_size + child_path_cost + grandchild_path_cost; |
| 1277 EXPECT_TRUE(DirectoryExists("src/dir")); | 1281 EXPECT_TRUE(DirectoryExists("src/dir")); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 1297 FileSystemURL src_file(CreateFile("src")); | 1301 FileSystemURL src_file(CreateFile("src")); |
| 1298 FileSystemURL dest_file(CreateFile("dest")); | 1302 FileSystemURL dest_file(CreateFile("dest")); |
| 1299 | 1303 |
| 1300 EXPECT_EQ(base::File::FILE_OK, Truncate(dest_file, 6)); | 1304 EXPECT_EQ(base::File::FILE_OK, Truncate(dest_file, 6)); |
| 1301 EXPECT_EQ(base::File::FILE_OK, | 1305 EXPECT_EQ(base::File::FILE_OK, |
| 1302 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE)); | 1306 Copy(src_file, dest_file, FileSystemOperation::OPTION_NONE)); |
| 1303 EXPECT_EQ(0, GetFileSize("dest")); | 1307 EXPECT_EQ(0, GetFileSize("dest")); |
| 1304 } | 1308 } |
| 1305 | 1309 |
| 1306 } // namespace content | 1310 } // namespace content |
| OLD | NEW |