| 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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/scoped_callback_factory.h" | 12 #include "base/memory/scoped_callback_factory.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/platform_file.h" | 14 #include "base/platform_file.h" |
| 15 #include "base/scoped_temp_dir.h" | 15 #include "base/scoped_temp_dir.h" |
| 16 #include "base/sys_string_conversions.h" | 16 #include "base/sys_string_conversions.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 #include "webkit/fileapi/file_system_context.h" | 18 #include "webkit/fileapi/file_system_context.h" |
| 19 #include "webkit/fileapi/file_system_operation_context.h" | 19 #include "webkit/fileapi/file_system_operation_context.h" |
| 20 #include "webkit/fileapi/file_system_path_manager.h" | 20 #include "webkit/fileapi/file_system_path_manager.h" |
| 21 #include "webkit/fileapi/file_system_test_helper.h" | 21 #include "webkit/fileapi/file_system_test_helper.h" |
| 22 #include "webkit/fileapi/file_system_usage_cache.h" | 22 #include "webkit/fileapi/file_system_usage_cache.h" |
| 23 #include "webkit/fileapi/obfuscated_file_system_file_util.h" | 23 #include "webkit/fileapi/obfuscated_file_util.h" |
| 24 #include "webkit/quota/mock_special_storage_policy.h" | 24 #include "webkit/quota/mock_special_storage_policy.h" |
| 25 #include "webkit/quota/quota_manager.h" | 25 #include "webkit/quota/quota_manager.h" |
| 26 #include "webkit/quota/quota_types.h" | 26 #include "webkit/quota/quota_types.h" |
| 27 | 27 |
| 28 using namespace fileapi; | 28 using namespace fileapi; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 FilePath UTF8ToFilePath(const std::string& str) { | 32 FilePath UTF8ToFilePath(const std::string& str) { |
| 33 FilePath::StringType result; | 33 FilePath::StringType result; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 {"file://", false, true}, | 121 {"file://", false, true}, |
| 122 {"http://example.com:8000", false, true}, | 122 {"http://example.com:8000", false, true}, |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 } // namespace (anonymous) | 125 } // namespace (anonymous) |
| 126 | 126 |
| 127 // TODO(ericu): The vast majority of this and the other FSFU subclass tests | 127 // TODO(ericu): The vast majority of this and the other FSFU subclass tests |
| 128 // could theoretically be shared. It would basically be a FSFU interface | 128 // could theoretically be shared. It would basically be a FSFU interface |
| 129 // compliance test, and only the subclass-specific bits that look into the | 129 // compliance test, and only the subclass-specific bits that look into the |
| 130 // implementation would need to be written per-subclass. | 130 // implementation would need to be written per-subclass. |
| 131 class ObfuscatedFileSystemFileUtilTest : public testing::Test { | 131 class ObfuscatedFileUtilTest : public testing::Test { |
| 132 public: | 132 public: |
| 133 ObfuscatedFileSystemFileUtilTest() | 133 ObfuscatedFileUtilTest() |
| 134 : origin_(GURL("http://www.example.com")), | 134 : origin_(GURL("http://www.example.com")), |
| 135 type_(kFileSystemTypeTemporary), | 135 type_(kFileSystemTypeTemporary), |
| 136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 136 callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 137 test_helper_(origin_, type_), | 137 test_helper_(origin_, type_), |
| 138 quota_status_(quota::kQuotaStatusUnknown), | 138 quota_status_(quota::kQuotaStatusUnknown), |
| 139 usage_(-1) { | 139 usage_(-1) { |
| 140 } | 140 } |
| 141 | 141 |
| 142 void SetUp() { | 142 void SetUp() { |
| 143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | 143 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); |
| 144 | 144 |
| 145 quota_manager_ = new quota::QuotaManager( | 145 quota_manager_ = new quota::QuotaManager( |
| 146 false /* is_incognito */, | 146 false /* is_incognito */, |
| 147 data_dir_.path(), | 147 data_dir_.path(), |
| 148 base::MessageLoopProxy::current(), | 148 base::MessageLoopProxy::current(), |
| 149 base::MessageLoopProxy::current(), | 149 base::MessageLoopProxy::current(), |
| 150 NULL /* special storage policy */); | 150 NULL /* special storage policy */); |
| 151 | 151 |
| 152 // Every time we create a new helper, it creates another context, which | 152 // Every time we create a new helper, it creates another context, which |
| 153 // creates another path manager, another sandbox_mount_point_provider, and | 153 // creates another path manager, another sandbox_mount_point_provider, and |
| 154 // another OFSFU. We need to pass in the context to skip all that. | 154 // another OFU. We need to pass in the context to skip all that. |
| 155 file_system_context_ = new FileSystemContext( | 155 file_system_context_ = new FileSystemContext( |
| 156 base::MessageLoopProxy::current(), | 156 base::MessageLoopProxy::current(), |
| 157 base::MessageLoopProxy::current(), | 157 base::MessageLoopProxy::current(), |
| 158 new quota::MockSpecialStoragePolicy(), | 158 new quota::MockSpecialStoragePolicy(), |
| 159 quota_manager_->proxy(), | 159 quota_manager_->proxy(), |
| 160 data_dir_.path(), | 160 data_dir_.path(), |
| 161 false /* incognito */, | 161 false /* incognito */, |
| 162 true /* allow_file_access_from_files */, | 162 true /* allow_file_access_from_files */, |
| 163 false /* unlimited_quota */, | 163 false /* unlimited_quota */, |
| 164 NULL /* path_manager */); | 164 NULL /* path_manager */); |
| 165 | 165 |
| 166 obfuscated_file_system_file_util_ = | 166 obfuscated_file_util_ = static_cast<ObfuscatedFileUtil*>( |
| 167 static_cast<ObfuscatedFileSystemFileUtil*>( | 167 file_system_context_->path_manager()->GetFileUtil(type_)); |
| 168 file_system_context_->path_manager()->GetFileSystemFileUtil(type_)); | |
| 169 | |
| 170 | 168 |
| 171 test_helper_.SetUp(file_system_context_.get(), | 169 test_helper_.SetUp(file_system_context_.get(), |
| 172 obfuscated_file_system_file_util_.get()); | 170 obfuscated_file_util_.get()); |
| 173 } | 171 } |
| 174 | 172 |
| 175 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) { | 173 FileSystemOperationContext* NewContext(FileSystemTestOriginHelper* helper) { |
| 176 FileSystemOperationContext* context; | 174 FileSystemOperationContext* context; |
| 177 if (helper) | 175 if (helper) |
| 178 context = helper->NewOperationContext(); | 176 context = helper->NewOperationContext(); |
| 179 else | 177 else |
| 180 context = test_helper_.NewOperationContext(); | 178 context = test_helper_.NewOperationContext(); |
| 181 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests. | 179 context->set_allowed_bytes_growth(1024 * 1024); // Big enough for all tests. |
| 182 return context; | 180 return context; |
| 183 } | 181 } |
| 184 | 182 |
| 185 // This can only be used after SetUp has run and created file_system_context_ | 183 // This can only be used after SetUp has run and created file_system_context_ |
| 186 // and obfuscated_file_system_file_util_. | 184 // and obfuscated_file_util_. |
| 187 // Use this for tests which need to run in multiple origins; we need a test | 185 // Use this for tests which need to run in multiple origins; we need a test |
| 188 // helper per origin. | 186 // helper per origin. |
| 189 FileSystemTestOriginHelper* NewHelper( | 187 FileSystemTestOriginHelper* NewHelper( |
| 190 const GURL& origin, fileapi::FileSystemType type) { | 188 const GURL& origin, fileapi::FileSystemType type) { |
| 191 FileSystemTestOriginHelper* helper = | 189 FileSystemTestOriginHelper* helper = |
| 192 new FileSystemTestOriginHelper(origin, type); | 190 new FileSystemTestOriginHelper(origin, type); |
| 193 | 191 |
| 194 helper->SetUp(file_system_context_.get(), | 192 helper->SetUp(file_system_context_.get(), |
| 195 obfuscated_file_system_file_util_.get()); | 193 obfuscated_file_util_.get()); |
| 196 return helper; | 194 return helper; |
| 197 } | 195 } |
| 198 | 196 |
| 199 ObfuscatedFileSystemFileUtil* ofsfu() { | 197 ObfuscatedFileUtil* ofu() { |
| 200 return obfuscated_file_system_file_util_.get(); | 198 return obfuscated_file_util_.get(); |
| 201 } | 199 } |
| 202 | 200 |
| 203 const FilePath& test_directory() const { | 201 const FilePath& test_directory() const { |
| 204 return data_dir_.path(); | 202 return data_dir_.path(); |
| 205 } | 203 } |
| 206 | 204 |
| 207 const GURL& origin() const { | 205 const GURL& origin() const { |
| 208 return origin_; | 206 return origin_; |
| 209 } | 207 } |
| 210 | 208 |
| 211 fileapi::FileSystemType type() const { | 209 fileapi::FileSystemType type() const { |
| 212 return type_; | 210 return type_; |
| 213 } | 211 } |
| 214 | 212 |
| 215 void GetUsageFromQuotaManager() { | 213 void GetUsageFromQuotaManager() { |
| 216 quota_manager_->GetUsageAndQuota( | 214 quota_manager_->GetUsageAndQuota( |
| 217 origin(), test_helper_.storage_type(), | 215 origin(), test_helper_.storage_type(), |
| 218 callback_factory_.NewCallback( | 216 callback_factory_.NewCallback( |
| 219 &ObfuscatedFileSystemFileUtilTest::OnGetUsage)); | 217 &ObfuscatedFileUtilTest::OnGetUsage)); |
| 220 MessageLoop::current()->RunAllPending(); | 218 MessageLoop::current()->RunAllPending(); |
| 221 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); | 219 EXPECT_EQ(quota::kQuotaStatusOk, quota_status_); |
| 222 } | 220 } |
| 223 | 221 |
| 224 void RevokeUsageCache() { | 222 void RevokeUsageCache() { |
| 225 quota_manager_->ResetUsageTracker(test_helper_.storage_type()); | 223 quota_manager_->ResetUsageTracker(test_helper_.storage_type()); |
| 226 ASSERT_TRUE(test_helper_.RevokeUsageCache()); | 224 ASSERT_TRUE(test_helper_.RevokeUsageCache()); |
| 227 } | 225 } |
| 228 | 226 |
| 229 int64 SizeInUsageFile() { | 227 int64 SizeInUsageFile() { |
| 230 return test_helper_.GetCachedOriginUsage(); | 228 return test_helper_.GetCachedOriginUsage(); |
| 231 } | 229 } |
| 232 | 230 |
| 233 int64 usage() const { return usage_; } | 231 int64 usage() const { return usage_; } |
| 234 | 232 |
| 235 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { | 233 void OnGetUsage(quota::QuotaStatusCode status, int64 usage, int64 unused) { |
| 236 EXPECT_EQ(quota::kQuotaStatusOk, status); | 234 EXPECT_EQ(quota::kQuotaStatusOk, status); |
| 237 quota_status_ = status; | 235 quota_status_ = status; |
| 238 usage_ = usage; | 236 usage_ = usage; |
| 239 } | 237 } |
| 240 | 238 |
| 241 void CheckFileAndCloseHandle( | 239 void CheckFileAndCloseHandle( |
| 242 const FilePath& virtual_path, PlatformFile file_handle) { | 240 const FilePath& virtual_path, PlatformFile file_handle) { |
| 243 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 241 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 244 FilePath local_path; | 242 FilePath local_path; |
| 245 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | 243 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
| 246 context.get(), virtual_path, &local_path)); | 244 context.get(), virtual_path, &local_path)); |
| 247 | 245 |
| 248 base::PlatformFileInfo file_info0; | 246 base::PlatformFileInfo file_info0; |
| 249 FilePath data_path; | 247 FilePath data_path; |
| 250 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 248 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 251 context.get(), virtual_path, &file_info0, &data_path)); | 249 context.get(), virtual_path, &file_info0, &data_path)); |
| 252 EXPECT_EQ(data_path, local_path); | 250 EXPECT_EQ(data_path, local_path); |
| 253 EXPECT_TRUE(FileExists(data_path)); | 251 EXPECT_TRUE(FileExists(data_path)); |
| 254 EXPECT_EQ(0, GetSize(data_path)); | 252 EXPECT_EQ(0, GetSize(data_path)); |
| 255 | 253 |
| 256 const char data[] = "test data"; | 254 const char data[] = "test data"; |
| 257 const int length = arraysize(data) - 1; | 255 const int length = arraysize(data) - 1; |
| 258 | 256 |
| 259 if (base::kInvalidPlatformFileValue == file_handle) { | 257 if (base::kInvalidPlatformFileValue == file_handle) { |
| 260 bool created = true; | 258 bool created = true; |
| 261 PlatformFileError error; | 259 PlatformFileError error; |
| 262 file_handle = base::CreatePlatformFile( | 260 file_handle = base::CreatePlatformFile( |
| 263 data_path, | 261 data_path, |
| 264 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, | 262 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_WRITE, |
| 265 &created, | 263 &created, |
| 266 &error); | 264 &error); |
| 267 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | 265 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 268 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | 266 ASSERT_EQ(base::PLATFORM_FILE_OK, error); |
| 269 EXPECT_FALSE(created); | 267 EXPECT_FALSE(created); |
| 270 } | 268 } |
| 271 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); | 269 ASSERT_EQ(length, base::WritePlatformFile(file_handle, 0, data, length)); |
| 272 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | 270 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 273 | 271 |
| 274 base::PlatformFileInfo file_info1; | 272 base::PlatformFileInfo file_info1; |
| 275 EXPECT_EQ(length, GetSize(data_path)); | 273 EXPECT_EQ(length, GetSize(data_path)); |
| 276 context.reset(NewContext(NULL)); | 274 context.reset(NewContext(NULL)); |
| 277 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 275 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 278 context.get(), virtual_path, &file_info1, &data_path)); | 276 context.get(), virtual_path, &file_info1, &data_path)); |
| 279 EXPECT_EQ(data_path, local_path); | 277 EXPECT_EQ(data_path, local_path); |
| 280 | 278 |
| 281 EXPECT_FALSE(file_info0.is_directory); | 279 EXPECT_FALSE(file_info0.is_directory); |
| 282 EXPECT_FALSE(file_info1.is_directory); | 280 EXPECT_FALSE(file_info1.is_directory); |
| 283 EXPECT_FALSE(file_info0.is_symbolic_link); | 281 EXPECT_FALSE(file_info0.is_symbolic_link); |
| 284 EXPECT_FALSE(file_info1.is_symbolic_link); | 282 EXPECT_FALSE(file_info1.is_symbolic_link); |
| 285 EXPECT_EQ(0, file_info0.size); | 283 EXPECT_EQ(0, file_info0.size); |
| 286 EXPECT_EQ(length, file_info1.size); | 284 EXPECT_EQ(length, file_info1.size); |
| 287 EXPECT_LE(file_info0.last_modified, file_info1.last_modified); | 285 EXPECT_LE(file_info0.last_modified, file_info1.last_modified); |
| 288 | 286 |
| 289 context.reset(NewContext(NULL)); | 287 context.reset(NewContext(NULL)); |
| 290 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | 288 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
| 291 context.get(), virtual_path, length * 2)); | 289 context.get(), virtual_path, length * 2)); |
| 292 EXPECT_EQ(length * 2, GetSize(data_path)); | 290 EXPECT_EQ(length * 2, GetSize(data_path)); |
| 293 | 291 |
| 294 context.reset(NewContext(NULL)); | 292 context.reset(NewContext(NULL)); |
| 295 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | 293 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
| 296 context.get(), virtual_path, 0)); | 294 context.get(), virtual_path, 0)); |
| 297 EXPECT_EQ(0, GetSize(data_path)); | 295 EXPECT_EQ(0, GetSize(data_path)); |
| 298 } | 296 } |
| 299 | 297 |
| 300 void ValidateTestDirectory( | 298 void ValidateTestDirectory( |
| 301 const FilePath& root_path, | 299 const FilePath& root_path, |
| 302 const std::set<FilePath::StringType>& files, | 300 const std::set<FilePath::StringType>& files, |
| 303 const std::set<FilePath::StringType>& directories) { | 301 const std::set<FilePath::StringType>& directories) { |
| 304 scoped_ptr<FileSystemOperationContext> context; | 302 scoped_ptr<FileSystemOperationContext> context; |
| 305 std::set<FilePath::StringType>::const_iterator iter; | 303 std::set<FilePath::StringType>::const_iterator iter; |
| 306 for (iter = files.begin(); iter != files.end(); ++iter) { | 304 for (iter = files.begin(); iter != files.end(); ++iter) { |
| 307 bool created = true; | 305 bool created = true; |
| 308 context.reset(NewContext(NULL)); | 306 context.reset(NewContext(NULL)); |
| 309 ASSERT_EQ(base::PLATFORM_FILE_OK, | 307 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 310 ofsfu()->EnsureFileExists( | 308 ofu()->EnsureFileExists( |
| 311 context.get(), root_path.Append(*iter), | 309 context.get(), root_path.Append(*iter), |
| 312 &created)); | 310 &created)); |
| 313 ASSERT_FALSE(created); | 311 ASSERT_FALSE(created); |
| 314 } | 312 } |
| 315 for (iter = directories.begin(); iter != directories.end(); ++iter) { | 313 for (iter = directories.begin(); iter != directories.end(); ++iter) { |
| 316 context.reset(NewContext(NULL)); | 314 context.reset(NewContext(NULL)); |
| 317 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), | 315 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), |
| 318 root_path.Append(*iter))); | 316 root_path.Append(*iter))); |
| 319 } | 317 } |
| 320 } | 318 } |
| 321 | 319 |
| 322 void FillTestDirectory( | 320 void FillTestDirectory( |
| 323 const FilePath& root_path, | 321 const FilePath& root_path, |
| 324 std::set<FilePath::StringType>* files, | 322 std::set<FilePath::StringType>* files, |
| 325 std::set<FilePath::StringType>* directories) { | 323 std::set<FilePath::StringType>* directories) { |
| 326 scoped_ptr<FileSystemOperationContext> context; | 324 scoped_ptr<FileSystemOperationContext> context; |
| 327 context.reset(NewContext(NULL)); | 325 context.reset(NewContext(NULL)); |
| 328 std::vector<base::FileUtilProxy::Entry> entries; | 326 std::vector<base::FileUtilProxy::Entry> entries; |
| 329 EXPECT_EQ(base::PLATFORM_FILE_OK, | 327 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 330 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); | 328 ofu()->ReadDirectory(context.get(), root_path, &entries)); |
| 331 EXPECT_EQ(0UL, entries.size()); | 329 EXPECT_EQ(0UL, entries.size()); |
| 332 | 330 |
| 333 files->clear(); | 331 files->clear(); |
| 334 files->insert(FILE_PATH_LITERAL("first")); | 332 files->insert(FILE_PATH_LITERAL("first")); |
| 335 files->insert(FILE_PATH_LITERAL("second")); | 333 files->insert(FILE_PATH_LITERAL("second")); |
| 336 files->insert(FILE_PATH_LITERAL("third")); | 334 files->insert(FILE_PATH_LITERAL("third")); |
| 337 directories->clear(); | 335 directories->clear(); |
| 338 directories->insert(FILE_PATH_LITERAL("fourth")); | 336 directories->insert(FILE_PATH_LITERAL("fourth")); |
| 339 directories->insert(FILE_PATH_LITERAL("fifth")); | 337 directories->insert(FILE_PATH_LITERAL("fifth")); |
| 340 directories->insert(FILE_PATH_LITERAL("sixth")); | 338 directories->insert(FILE_PATH_LITERAL("sixth")); |
| 341 std::set<FilePath::StringType>::iterator iter; | 339 std::set<FilePath::StringType>::iterator iter; |
| 342 for (iter = files->begin(); iter != files->end(); ++iter) { | 340 for (iter = files->begin(); iter != files->end(); ++iter) { |
| 343 bool created = false; | 341 bool created = false; |
| 344 context.reset(NewContext(NULL)); | 342 context.reset(NewContext(NULL)); |
| 345 ASSERT_EQ(base::PLATFORM_FILE_OK, | 343 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 346 ofsfu()->EnsureFileExists( | 344 ofu()->EnsureFileExists( |
| 347 context.get(), root_path.Append(*iter), &created)); | 345 context.get(), root_path.Append(*iter), &created)); |
| 348 ASSERT_TRUE(created); | 346 ASSERT_TRUE(created); |
| 349 } | 347 } |
| 350 for (iter = directories->begin(); iter != directories->end(); ++iter) { | 348 for (iter = directories->begin(); iter != directories->end(); ++iter) { |
| 351 bool exclusive = true; | 349 bool exclusive = true; |
| 352 bool recursive = false; | 350 bool recursive = false; |
| 353 context.reset(NewContext(NULL)); | 351 context.reset(NewContext(NULL)); |
| 354 EXPECT_EQ(base::PLATFORM_FILE_OK, | 352 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 355 ofsfu()->CreateDirectory( | 353 ofu()->CreateDirectory( |
| 356 context.get(), root_path.Append(*iter), exclusive, recursive)); | 354 context.get(), root_path.Append(*iter), exclusive, recursive)); |
| 357 } | 355 } |
| 358 ValidateTestDirectory(root_path, *files, *directories); | 356 ValidateTestDirectory(root_path, *files, *directories); |
| 359 } | 357 } |
| 360 | 358 |
| 361 void TestReadDirectoryHelper(const FilePath& root_path) { | 359 void TestReadDirectoryHelper(const FilePath& root_path) { |
| 362 std::set<FilePath::StringType> files; | 360 std::set<FilePath::StringType> files; |
| 363 std::set<FilePath::StringType> directories; | 361 std::set<FilePath::StringType> directories; |
| 364 FillTestDirectory(root_path, &files, &directories); | 362 FillTestDirectory(root_path, &files, &directories); |
| 365 | 363 |
| 366 scoped_ptr<FileSystemOperationContext> context; | 364 scoped_ptr<FileSystemOperationContext> context; |
| 367 std::vector<base::FileUtilProxy::Entry> entries; | 365 std::vector<base::FileUtilProxy::Entry> entries; |
| 368 context.reset(NewContext(NULL)); | 366 context.reset(NewContext(NULL)); |
| 369 EXPECT_EQ(base::PLATFORM_FILE_OK, | 367 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 370 ofsfu()->ReadDirectory(context.get(), root_path, &entries)); | 368 ofu()->ReadDirectory(context.get(), root_path, &entries)); |
| 371 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; | 369 std::vector<base::FileUtilProxy::Entry>::iterator entry_iter; |
| 372 EXPECT_EQ(files.size() + directories.size(), entries.size()); | 370 EXPECT_EQ(files.size() + directories.size(), entries.size()); |
| 373 for (entry_iter = entries.begin(); entry_iter != entries.end(); | 371 for (entry_iter = entries.begin(); entry_iter != entries.end(); |
| 374 ++entry_iter) { | 372 ++entry_iter) { |
| 375 const base::FileUtilProxy::Entry& entry = *entry_iter; | 373 const base::FileUtilProxy::Entry& entry = *entry_iter; |
| 376 std::set<FilePath::StringType>::iterator iter = files.find(entry.name); | 374 std::set<FilePath::StringType>::iterator iter = files.find(entry.name); |
| 377 if (iter != files.end()) { | 375 if (iter != files.end()) { |
| 378 EXPECT_FALSE(entry.is_directory); | 376 EXPECT_FALSE(entry.is_directory); |
| 379 files.erase(iter); | 377 files.erase(iter); |
| 380 continue; | 378 continue; |
| 381 } | 379 } |
| 382 iter = directories.find(entry.name); | 380 iter = directories.find(entry.name); |
| 383 EXPECT_FALSE(directories.end() == iter); | 381 EXPECT_FALSE(directories.end() == iter); |
| 384 EXPECT_TRUE(entry.is_directory); | 382 EXPECT_TRUE(entry.is_directory); |
| 385 directories.erase(iter); | 383 directories.erase(iter); |
| 386 } | 384 } |
| 387 } | 385 } |
| 388 | 386 |
| 389 void TestTouchHelper(const FilePath& path, bool is_file) { | 387 void TestTouchHelper(const FilePath& path, bool is_file) { |
| 390 base::Time last_access_time = base::Time::Now(); | 388 base::Time last_access_time = base::Time::Now(); |
| 391 base::Time last_modified_time = base::Time::Now(); | 389 base::Time last_modified_time = base::Time::Now(); |
| 392 | 390 |
| 393 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 391 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 394 EXPECT_EQ(base::PLATFORM_FILE_OK, | 392 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 395 ofsfu()->Touch( | 393 ofu()->Touch( |
| 396 context.get(), path, last_access_time, last_modified_time)); | 394 context.get(), path, last_access_time, last_modified_time)); |
| 397 FilePath local_path; | 395 FilePath local_path; |
| 398 base::PlatformFileInfo file_info; | 396 base::PlatformFileInfo file_info; |
| 399 context.reset(NewContext(NULL)); | 397 context.reset(NewContext(NULL)); |
| 400 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 398 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 401 context.get(), path, &file_info, &local_path)); | 399 context.get(), path, &file_info, &local_path)); |
| 402 // We compare as time_t here to lower our resolution, to avoid false | 400 // We compare as time_t here to lower our resolution, to avoid false |
| 403 // negatives caused by conversion to the local filesystem's native | 401 // negatives caused by conversion to the local filesystem's native |
| 404 // representation and back. | 402 // representation and back. |
| 405 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); | 403 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 406 | 404 |
| 407 context.reset(NewContext(NULL)); | 405 context.reset(NewContext(NULL)); |
| 408 last_modified_time += base::TimeDelta::FromHours(1); | 406 last_modified_time += base::TimeDelta::FromHours(1); |
| 409 last_access_time += base::TimeDelta::FromHours(14); | 407 last_access_time += base::TimeDelta::FromHours(14); |
| 410 EXPECT_EQ(base::PLATFORM_FILE_OK, | 408 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 411 ofsfu()->Touch( | 409 ofu()->Touch( |
| 412 context.get(), path, last_access_time, last_modified_time)); | 410 context.get(), path, last_access_time, last_modified_time)); |
| 413 context.reset(NewContext(NULL)); | 411 context.reset(NewContext(NULL)); |
| 414 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 412 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 415 context.get(), path, &file_info, &local_path)); | 413 context.get(), path, &file_info, &local_path)); |
| 416 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); | 414 EXPECT_EQ(file_info.last_modified.ToTimeT(), last_modified_time.ToTimeT()); |
| 417 if (is_file) // Directories in OFSFU don't support atime. | 415 if (is_file) // Directories in OFU don't support atime. |
| 418 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT()); | 416 EXPECT_EQ(file_info.last_accessed.ToTimeT(), last_access_time.ToTimeT()); |
| 419 } | 417 } |
| 420 | 418 |
| 421 void TestCopyInForeignFileHelper(bool overwrite) { | 419 void TestCopyInForeignFileHelper(bool overwrite) { |
| 422 ScopedTempDir source_dir; | 420 ScopedTempDir source_dir; |
| 423 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); | 421 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 424 FilePath root_path = source_dir.path(); | 422 FilePath root_path = source_dir.path(); |
| 425 FilePath src_path = root_path.AppendASCII("file_name"); | 423 FilePath src_path = root_path.AppendASCII("file_name"); |
| 426 FilePath dest_path(FILE_PATH_LITERAL("new file")); | 424 FilePath dest_path(FILE_PATH_LITERAL("new file")); |
| 427 int64 src_file_length = 87; | 425 int64 src_file_length = 87; |
| 428 | 426 |
| 429 base::PlatformFileError error_code; | 427 base::PlatformFileError error_code; |
| 430 bool created = false; | 428 bool created = false; |
| 431 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 429 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 432 base::PlatformFile file_handle = | 430 base::PlatformFile file_handle = |
| 433 base::CreatePlatformFile( | 431 base::CreatePlatformFile( |
| 434 src_path, file_flags, &created, &error_code); | 432 src_path, file_flags, &created, &error_code); |
| 435 EXPECT_TRUE(created); | 433 EXPECT_TRUE(created); |
| 436 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | 434 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 437 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | 435 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 438 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); | 436 ASSERT_TRUE(base::TruncatePlatformFile(file_handle, src_file_length)); |
| 439 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | 437 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 440 | 438 |
| 441 scoped_ptr<FileSystemOperationContext> context; | 439 scoped_ptr<FileSystemOperationContext> context; |
| 442 | 440 |
| 443 if (overwrite) { | 441 if (overwrite) { |
| 444 context.reset(NewContext(NULL)); | 442 context.reset(NewContext(NULL)); |
| 445 EXPECT_EQ(base::PLATFORM_FILE_OK, | 443 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 446 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); | 444 ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
| 447 EXPECT_TRUE(created); | 445 EXPECT_TRUE(created); |
| 448 } | 446 } |
| 449 | 447 |
| 450 const int64 path_cost = | 448 const int64 path_cost = |
| 451 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path); | 449 ObfuscatedFileUtil::ComputeFilePathCost(dest_path); |
| 452 if (!overwrite) { | 450 if (!overwrite) { |
| 453 // Verify that file creation requires sufficient quota for the path. | 451 // Verify that file creation requires sufficient quota for the path. |
| 454 context.reset(NewContext(NULL)); | 452 context.reset(NewContext(NULL)); |
| 455 context->set_allowed_bytes_growth(path_cost + src_file_length - 1); | 453 context->set_allowed_bytes_growth(path_cost + src_file_length - 1); |
| 456 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 454 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 457 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); | 455 ofu()->CopyInForeignFile(context.get(), src_path, dest_path)); |
| 458 } | 456 } |
| 459 | 457 |
| 460 context.reset(NewContext(NULL)); | 458 context.reset(NewContext(NULL)); |
| 461 context->set_allowed_bytes_growth(path_cost + src_file_length); | 459 context->set_allowed_bytes_growth(path_cost + src_file_length); |
| 462 EXPECT_EQ(base::PLATFORM_FILE_OK, | 460 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 463 ofsfu()->CopyInForeignFile(context.get(), src_path, dest_path)); | 461 ofu()->CopyInForeignFile(context.get(), src_path, dest_path)); |
| 464 | 462 |
| 465 context.reset(NewContext(NULL)); | 463 context.reset(NewContext(NULL)); |
| 466 EXPECT_TRUE(ofsfu()->PathExists(context.get(), dest_path)); | 464 EXPECT_TRUE(ofu()->PathExists(context.get(), dest_path)); |
| 467 context.reset(NewContext(NULL)); | 465 context.reset(NewContext(NULL)); |
| 468 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | 466 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
| 469 context.reset(NewContext(NULL)); | 467 context.reset(NewContext(NULL)); |
| 470 base::PlatformFileInfo file_info; | 468 base::PlatformFileInfo file_info; |
| 471 FilePath data_path; | 469 FilePath data_path; |
| 472 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 470 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 473 context.get(), dest_path, &file_info, &data_path)); | 471 context.get(), dest_path, &file_info, &data_path)); |
| 474 EXPECT_NE(data_path, src_path); | 472 EXPECT_NE(data_path, src_path); |
| 475 EXPECT_TRUE(FileExists(data_path)); | 473 EXPECT_TRUE(FileExists(data_path)); |
| 476 EXPECT_EQ(src_file_length, GetSize(data_path)); | 474 EXPECT_EQ(src_file_length, GetSize(data_path)); |
| 477 | 475 |
| 478 EXPECT_EQ(base::PLATFORM_FILE_OK, | 476 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 479 ofsfu()->DeleteFile(context.get(), dest_path)); | 477 ofu()->DeleteFile(context.get(), dest_path)); |
| 480 } | 478 } |
| 481 | 479 |
| 482 private: | 480 private: |
| 483 ScopedTempDir data_dir_; | 481 ScopedTempDir data_dir_; |
| 484 scoped_refptr<ObfuscatedFileSystemFileUtil> obfuscated_file_system_file_util_; | 482 scoped_refptr<ObfuscatedFileUtil> obfuscated_file_util_; |
| 485 scoped_refptr<quota::QuotaManager> quota_manager_; | 483 scoped_refptr<quota::QuotaManager> quota_manager_; |
| 486 scoped_refptr<FileSystemContext> file_system_context_; | 484 scoped_refptr<FileSystemContext> file_system_context_; |
| 487 GURL origin_; | 485 GURL origin_; |
| 488 fileapi::FileSystemType type_; | 486 fileapi::FileSystemType type_; |
| 489 base::ScopedCallbackFactory<ObfuscatedFileSystemFileUtilTest> | 487 base::ScopedCallbackFactory<ObfuscatedFileUtilTest> |
| 490 callback_factory_; | 488 callback_factory_; |
| 491 FileSystemTestOriginHelper test_helper_; | 489 FileSystemTestOriginHelper test_helper_; |
| 492 quota::QuotaStatusCode quota_status_; | 490 quota::QuotaStatusCode quota_status_; |
| 493 int64 usage_; | 491 int64 usage_; |
| 494 | 492 |
| 495 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileSystemFileUtilTest); | 493 DISALLOW_COPY_AND_ASSIGN(ObfuscatedFileUtilTest); |
| 496 }; | 494 }; |
| 497 | 495 |
| 498 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCreateAndDeleteFile) { | 496 TEST_F(ObfuscatedFileUtilTest, TestCreateAndDeleteFile) { |
| 499 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; | 497 base::PlatformFile file_handle = base::kInvalidPlatformFileValue; |
| 500 bool created; | 498 bool created; |
| 501 FilePath path = UTF8ToFilePath("fake/file"); | 499 FilePath path = UTF8ToFilePath("fake/file"); |
| 502 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 500 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 503 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 501 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 504 | 502 |
| 505 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 503 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 506 ofsfu()->CreateOrOpen( | 504 ofu()->CreateOrOpen( |
| 507 context.get(), path, file_flags, &file_handle, | 505 context.get(), path, file_flags, &file_handle, |
| 508 &created)); | 506 &created)); |
| 509 | 507 |
| 510 context.reset(NewContext(NULL)); | 508 context.reset(NewContext(NULL)); |
| 511 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 509 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 512 ofsfu()->DeleteFile(context.get(), path)); | 510 ofu()->DeleteFile(context.get(), path)); |
| 513 | 511 |
| 514 path = UTF8ToFilePath("test file"); | 512 path = UTF8ToFilePath("test file"); |
| 515 | 513 |
| 516 // Verify that file creation requires sufficient quota for the path. | 514 // Verify that file creation requires sufficient quota for the path. |
| 517 context.reset(NewContext(NULL)); | 515 context.reset(NewContext(NULL)); |
| 518 context->set_allowed_bytes_growth( | 516 context->set_allowed_bytes_growth( |
| 519 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | 517 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
| 520 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 518 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 521 ofsfu()->CreateOrOpen( | 519 ofu()->CreateOrOpen( |
| 522 context.get(), path, file_flags, &file_handle, &created)); | 520 context.get(), path, file_flags, &file_handle, &created)); |
| 523 | 521 |
| 524 context.reset(NewContext(NULL)); | 522 context.reset(NewContext(NULL)); |
| 525 context->set_allowed_bytes_growth( | 523 context->set_allowed_bytes_growth( |
| 526 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | 524 ObfuscatedFileUtil::ComputeFilePathCost(path)); |
| 527 ASSERT_EQ(base::PLATFORM_FILE_OK, | 525 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 528 ofsfu()->CreateOrOpen( | 526 ofu()->CreateOrOpen( |
| 529 context.get(), path, file_flags, &file_handle, &created)); | 527 context.get(), path, file_flags, &file_handle, &created)); |
| 530 ASSERT_TRUE(created); | 528 ASSERT_TRUE(created); |
| 531 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); | 529 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 532 | 530 |
| 533 CheckFileAndCloseHandle(path, file_handle); | 531 CheckFileAndCloseHandle(path, file_handle); |
| 534 | 532 |
| 535 context.reset(NewContext(NULL)); | 533 context.reset(NewContext(NULL)); |
| 536 FilePath local_path; | 534 FilePath local_path; |
| 537 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | 535 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
| 538 context.get(), path, &local_path)); | 536 context.get(), path, &local_path)); |
| 539 EXPECT_TRUE(file_util::PathExists(local_path)); | 537 EXPECT_TRUE(file_util::PathExists(local_path)); |
| 540 | 538 |
| 541 // Verify that deleting a file isn't stopped by zero quota, and that it frees | 539 // Verify that deleting a file isn't stopped by zero quota, and that it frees |
| 542 // up quote from its path. | 540 // up quote from its path. |
| 543 context.reset(NewContext(NULL)); | 541 context.reset(NewContext(NULL)); |
| 544 context->set_allowed_bytes_growth(0); | 542 context->set_allowed_bytes_growth(0); |
| 545 EXPECT_EQ(base::PLATFORM_FILE_OK, | 543 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 546 ofsfu()->DeleteFile(context.get(), path)); | 544 ofu()->DeleteFile(context.get(), path)); |
| 547 EXPECT_FALSE(file_util::PathExists(local_path)); | 545 EXPECT_FALSE(file_util::PathExists(local_path)); |
| 548 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path), | 546 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
| 549 context->allowed_bytes_growth()); | 547 context->allowed_bytes_growth()); |
| 550 | 548 |
| 551 context.reset(NewContext(NULL)); | 549 context.reset(NewContext(NULL)); |
| 552 bool exclusive = true; | 550 bool exclusive = true; |
| 553 bool recursive = true; | 551 bool recursive = true; |
| 554 FilePath directory_path = UTF8ToFilePath("series/of/directories"); | 552 FilePath directory_path = UTF8ToFilePath("series/of/directories"); |
| 555 path = directory_path.AppendASCII("file name"); | 553 path = directory_path.AppendASCII("file name"); |
| 556 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 554 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 557 context.get(), directory_path, exclusive, recursive)); | 555 context.get(), directory_path, exclusive, recursive)); |
| 558 | 556 |
| 559 context.reset(NewContext(NULL)); | 557 context.reset(NewContext(NULL)); |
| 560 file_handle = base::kInvalidPlatformFileValue; | 558 file_handle = base::kInvalidPlatformFileValue; |
| 561 ASSERT_EQ(base::PLATFORM_FILE_OK, | 559 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 562 ofsfu()->CreateOrOpen( | 560 ofu()->CreateOrOpen( |
| 563 context.get(), path, file_flags, &file_handle, &created)); | 561 context.get(), path, file_flags, &file_handle, &created)); |
| 564 ASSERT_TRUE(created); | 562 ASSERT_TRUE(created); |
| 565 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); | 563 EXPECT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 566 | 564 |
| 567 CheckFileAndCloseHandle(path, file_handle); | 565 CheckFileAndCloseHandle(path, file_handle); |
| 568 | 566 |
| 569 context.reset(NewContext(NULL)); | 567 context.reset(NewContext(NULL)); |
| 570 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | 568 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
| 571 context.get(), path, &local_path)); | 569 context.get(), path, &local_path)); |
| 572 EXPECT_TRUE(file_util::PathExists(local_path)); | 570 EXPECT_TRUE(file_util::PathExists(local_path)); |
| 573 | 571 |
| 574 context.reset(NewContext(NULL)); | 572 context.reset(NewContext(NULL)); |
| 575 EXPECT_EQ(base::PLATFORM_FILE_OK, | 573 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 576 ofsfu()->DeleteFile(context.get(), path)); | 574 ofu()->DeleteFile(context.get(), path)); |
| 577 EXPECT_FALSE(file_util::PathExists(local_path)); | 575 EXPECT_FALSE(file_util::PathExists(local_path)); |
| 578 } | 576 } |
| 579 | 577 |
| 580 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTruncate) { | 578 TEST_F(ObfuscatedFileUtilTest, TestTruncate) { |
| 581 bool created = false; | 579 bool created = false; |
| 582 FilePath path = UTF8ToFilePath("file"); | 580 FilePath path = UTF8ToFilePath("file"); |
| 583 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 581 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 584 | 582 |
| 585 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 583 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 586 ofsfu()->Truncate(context.get(), path, 4)); | 584 ofu()->Truncate(context.get(), path, 4)); |
| 587 | 585 |
| 588 context.reset(NewContext(NULL)); | 586 context.reset(NewContext(NULL)); |
| 589 ASSERT_EQ(base::PLATFORM_FILE_OK, | 587 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 590 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 588 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 591 ASSERT_TRUE(created); | 589 ASSERT_TRUE(created); |
| 592 | 590 |
| 593 context.reset(NewContext(NULL)); | 591 context.reset(NewContext(NULL)); |
| 594 FilePath local_path; | 592 FilePath local_path; |
| 595 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetLocalFilePath( | 593 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetLocalFilePath( |
| 596 context.get(), path, &local_path)); | 594 context.get(), path, &local_path)); |
| 597 EXPECT_EQ(0, GetSize(local_path)); | 595 EXPECT_EQ(0, GetSize(local_path)); |
| 598 | 596 |
| 599 context.reset(NewContext(NULL)); | 597 context.reset(NewContext(NULL)); |
| 600 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | 598 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
| 601 context.get(), path, 10)); | 599 context.get(), path, 10)); |
| 602 EXPECT_EQ(10, GetSize(local_path)); | 600 EXPECT_EQ(10, GetSize(local_path)); |
| 603 | 601 |
| 604 context.reset(NewContext(NULL)); | 602 context.reset(NewContext(NULL)); |
| 605 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->Truncate( | 603 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->Truncate( |
| 606 context.get(), path, 1)); | 604 context.get(), path, 1)); |
| 607 EXPECT_EQ(1, GetSize(local_path)); | 605 EXPECT_EQ(1, GetSize(local_path)); |
| 608 | 606 |
| 609 context.reset(NewContext(NULL)); | 607 context.reset(NewContext(NULL)); |
| 610 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | 608 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
| 611 context.reset(NewContext(NULL)); | 609 context.reset(NewContext(NULL)); |
| 612 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | 610 EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
| 613 } | 611 } |
| 614 | 612 |
| 615 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnsureFileExists) { | 613 TEST_F(ObfuscatedFileUtilTest, TestEnsureFileExists) { |
| 616 FilePath path = UTF8ToFilePath("fake/file"); | 614 FilePath path = UTF8ToFilePath("fake/file"); |
| 617 bool created = false; | 615 bool created = false; |
| 618 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 616 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 619 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 617 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 620 ofsfu()->EnsureFileExists( | 618 ofu()->EnsureFileExists( |
| 621 context.get(), path, &created)); | 619 context.get(), path, &created)); |
| 622 | 620 |
| 623 // Verify that file creation requires sufficient quota for the path. | 621 // Verify that file creation requires sufficient quota for the path. |
| 624 context.reset(NewContext(NULL)); | 622 context.reset(NewContext(NULL)); |
| 625 path = UTF8ToFilePath("test file"); | 623 path = UTF8ToFilePath("test file"); |
| 626 created = false; | 624 created = false; |
| 627 context->set_allowed_bytes_growth( | 625 context->set_allowed_bytes_growth( |
| 628 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | 626 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
| 629 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 627 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 630 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 628 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 631 ASSERT_FALSE(created); | 629 ASSERT_FALSE(created); |
| 632 | 630 |
| 633 context.reset(NewContext(NULL)); | 631 context.reset(NewContext(NULL)); |
| 634 context->set_allowed_bytes_growth( | 632 context->set_allowed_bytes_growth( |
| 635 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | 633 ObfuscatedFileUtil::ComputeFilePathCost(path)); |
| 636 ASSERT_EQ(base::PLATFORM_FILE_OK, | 634 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 637 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 635 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 638 ASSERT_TRUE(created); | 636 ASSERT_TRUE(created); |
| 639 | 637 |
| 640 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); | 638 CheckFileAndCloseHandle(path, base::kInvalidPlatformFileValue); |
| 641 | 639 |
| 642 context.reset(NewContext(NULL)); | 640 context.reset(NewContext(NULL)); |
| 643 ASSERT_EQ(base::PLATFORM_FILE_OK, | 641 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 644 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 642 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 645 ASSERT_FALSE(created); | 643 ASSERT_FALSE(created); |
| 646 | 644 |
| 647 // Also test in a subdirectory. | 645 // Also test in a subdirectory. |
| 648 path = UTF8ToFilePath("path/to/file.txt"); | 646 path = UTF8ToFilePath("path/to/file.txt"); |
| 649 context.reset(NewContext(NULL)); | 647 context.reset(NewContext(NULL)); |
| 650 bool exclusive = true; | 648 bool exclusive = true; |
| 651 bool recursive = true; | 649 bool recursive = true; |
| 652 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 650 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 653 context.get(), path.DirName(), exclusive, recursive)); | 651 context.get(), path.DirName(), exclusive, recursive)); |
| 654 | 652 |
| 655 context.reset(NewContext(NULL)); | 653 context.reset(NewContext(NULL)); |
| 656 ASSERT_EQ(base::PLATFORM_FILE_OK, | 654 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 657 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 655 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 658 ASSERT_TRUE(created); | 656 ASSERT_TRUE(created); |
| 659 context.reset(NewContext(NULL)); | 657 context.reset(NewContext(NULL)); |
| 660 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | 658 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
| 661 context.reset(NewContext(NULL)); | 659 context.reset(NewContext(NULL)); |
| 662 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | 660 EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
| 663 } | 661 } |
| 664 | 662 |
| 665 TEST_F(ObfuscatedFileSystemFileUtilTest, TestDirectoryOps) { | 663 TEST_F(ObfuscatedFileUtilTest, TestDirectoryOps) { |
| 666 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 664 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 667 | 665 |
| 668 bool exclusive = false; | 666 bool exclusive = false; |
| 669 bool recursive = false; | 667 bool recursive = false; |
| 670 FilePath path = UTF8ToFilePath("foo/bar"); | 668 FilePath path = UTF8ToFilePath("foo/bar"); |
| 671 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->CreateDirectory( | 669 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->CreateDirectory( |
| 672 context.get(), path, exclusive, recursive)); | 670 context.get(), path, exclusive, recursive)); |
| 673 | 671 |
| 674 context.reset(NewContext(NULL)); | 672 context.reset(NewContext(NULL)); |
| 675 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 673 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 676 ofsfu()->DeleteSingleDirectory(context.get(), path)); | 674 ofu()->DeleteSingleDirectory(context.get(), path)); |
| 677 | 675 |
| 678 FilePath root = UTF8ToFilePath(""); | 676 FilePath root = UTF8ToFilePath(""); |
| 679 context.reset(NewContext(NULL)); | 677 context.reset(NewContext(NULL)); |
| 680 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | 678 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
| 681 context.reset(NewContext(NULL)); | 679 context.reset(NewContext(NULL)); |
| 682 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | 680 EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
| 683 context.reset(NewContext(NULL)); | 681 context.reset(NewContext(NULL)); |
| 684 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), root)); | 682 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), root)); |
| 685 | 683 |
| 686 context.reset(NewContext(NULL)); | 684 context.reset(NewContext(NULL)); |
| 687 exclusive = false; | 685 exclusive = false; |
| 688 recursive = true; | 686 recursive = true; |
| 689 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 687 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 690 context.get(), path, exclusive, recursive)); | 688 context.get(), path, exclusive, recursive)); |
| 691 | 689 |
| 692 context.reset(NewContext(NULL)); | 690 context.reset(NewContext(NULL)); |
| 693 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | 691 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
| 694 context.reset(NewContext(NULL)); | 692 context.reset(NewContext(NULL)); |
| 695 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | 693 EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
| 696 context.reset(NewContext(NULL)); | 694 context.reset(NewContext(NULL)); |
| 697 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), root)); | 695 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), root)); |
| 698 context.reset(NewContext(NULL)); | 696 context.reset(NewContext(NULL)); |
| 699 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path.DirName())); | 697 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path.DirName())); |
| 700 context.reset(NewContext(NULL)); | 698 context.reset(NewContext(NULL)); |
| 701 EXPECT_FALSE(ofsfu()->IsDirectoryEmpty(context.get(), path.DirName())); | 699 EXPECT_FALSE(ofu()->IsDirectoryEmpty(context.get(), path.DirName())); |
| 702 | 700 |
| 703 // Can't remove a non-empty directory. | 701 // Can't remove a non-empty directory. |
| 704 context.reset(NewContext(NULL)); | 702 context.reset(NewContext(NULL)); |
| 705 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, | 703 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_EMPTY, |
| 706 ofsfu()->DeleteSingleDirectory(context.get(), path.DirName())); | 704 ofu()->DeleteSingleDirectory(context.get(), path.DirName())); |
| 707 | 705 |
| 708 base::PlatformFileInfo file_info; | 706 base::PlatformFileInfo file_info; |
| 709 FilePath local_path; | 707 FilePath local_path; |
| 710 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 708 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 711 context.get(), path, &file_info, &local_path)); | 709 context.get(), path, &file_info, &local_path)); |
| 712 EXPECT_TRUE(local_path.empty()); | 710 EXPECT_TRUE(local_path.empty()); |
| 713 EXPECT_TRUE(file_info.is_directory); | 711 EXPECT_TRUE(file_info.is_directory); |
| 714 EXPECT_FALSE(file_info.is_symbolic_link); | 712 EXPECT_FALSE(file_info.is_symbolic_link); |
| 715 | 713 |
| 716 // Same create again should succeed, since exclusive is false. | 714 // Same create again should succeed, since exclusive is false. |
| 717 context.reset(NewContext(NULL)); | 715 context.reset(NewContext(NULL)); |
| 718 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 716 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 719 context.get(), path, exclusive, recursive)); | 717 context.get(), path, exclusive, recursive)); |
| 720 | 718 |
| 721 exclusive = true; | 719 exclusive = true; |
| 722 recursive = true; | 720 recursive = true; |
| 723 context.reset(NewContext(NULL)); | 721 context.reset(NewContext(NULL)); |
| 724 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | 722 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
| 725 context.get(), path, exclusive, recursive)); | 723 context.get(), path, exclusive, recursive)); |
| 726 | 724 |
| 727 // Verify that deleting a directory isn't stopped by zero quota, and that it | 725 // Verify that deleting a directory isn't stopped by zero quota, and that it |
| 728 // frees up quota from its path. | 726 // frees up quota from its path. |
| 729 context.reset(NewContext(NULL)); | 727 context.reset(NewContext(NULL)); |
| 730 context->set_allowed_bytes_growth(0); | 728 context->set_allowed_bytes_growth(0); |
| 731 EXPECT_EQ(base::PLATFORM_FILE_OK, | 729 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 732 ofsfu()->DeleteSingleDirectory(context.get(), path)); | 730 ofu()->DeleteSingleDirectory(context.get(), path)); |
| 733 EXPECT_EQ(ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path), | 731 EXPECT_EQ(ObfuscatedFileUtil::ComputeFilePathCost(path), |
| 734 context->allowed_bytes_growth()); | 732 context->allowed_bytes_growth()); |
| 735 | 733 |
| 736 path = UTF8ToFilePath("foo/bop"); | 734 path = UTF8ToFilePath("foo/bop"); |
| 737 | 735 |
| 738 context.reset(NewContext(NULL)); | 736 context.reset(NewContext(NULL)); |
| 739 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | 737 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
| 740 context.reset(NewContext(NULL)); | 738 context.reset(NewContext(NULL)); |
| 741 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | 739 EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
| 742 context.reset(NewContext(NULL)); | 740 context.reset(NewContext(NULL)); |
| 743 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); | 741 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
| 744 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( | 742 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
| 745 context.get(), path, &file_info, &local_path)); | 743 context.get(), path, &file_info, &local_path)); |
| 746 | 744 |
| 747 // Verify that file creation requires sufficient quota for the path. | 745 // Verify that file creation requires sufficient quota for the path. |
| 748 exclusive = true; | 746 exclusive = true; |
| 749 recursive = false; | 747 recursive = false; |
| 750 context.reset(NewContext(NULL)); | 748 context.reset(NewContext(NULL)); |
| 751 context->set_allowed_bytes_growth( | 749 context->set_allowed_bytes_growth( |
| 752 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path) - 1); | 750 ObfuscatedFileUtil::ComputeFilePathCost(path) - 1); |
| 753 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofsfu()->CreateDirectory( | 751 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, ofu()->CreateDirectory( |
| 754 context.get(), path, exclusive, recursive)); | 752 context.get(), path, exclusive, recursive)); |
| 755 | 753 |
| 756 context.reset(NewContext(NULL)); | 754 context.reset(NewContext(NULL)); |
| 757 context->set_allowed_bytes_growth( | 755 context->set_allowed_bytes_growth( |
| 758 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path)); | 756 ObfuscatedFileUtil::ComputeFilePathCost(path)); |
| 759 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 757 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 760 context.get(), path, exclusive, recursive)); | 758 context.get(), path, exclusive, recursive)); |
| 761 | 759 |
| 762 context.reset(NewContext(NULL)); | 760 context.reset(NewContext(NULL)); |
| 763 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | 761 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
| 764 context.reset(NewContext(NULL)); | 762 context.reset(NewContext(NULL)); |
| 765 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | 763 EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
| 766 | 764 |
| 767 exclusive = true; | 765 exclusive = true; |
| 768 recursive = false; | 766 recursive = false; |
| 769 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | 767 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
| 770 context.get(), path, exclusive, recursive)); | 768 context.get(), path, exclusive, recursive)); |
| 771 | 769 |
| 772 exclusive = true; | 770 exclusive = true; |
| 773 recursive = false; | 771 recursive = false; |
| 774 path = UTF8ToFilePath("foo"); | 772 path = UTF8ToFilePath("foo"); |
| 775 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | 773 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
| 776 context.get(), path, exclusive, recursive)); | 774 context.get(), path, exclusive, recursive)); |
| 777 | 775 |
| 778 path = UTF8ToFilePath("blah"); | 776 path = UTF8ToFilePath("blah"); |
| 779 | 777 |
| 780 context.reset(NewContext(NULL)); | 778 context.reset(NewContext(NULL)); |
| 781 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), path)); | 779 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), path)); |
| 782 context.reset(NewContext(NULL)); | 780 context.reset(NewContext(NULL)); |
| 783 EXPECT_FALSE(ofsfu()->PathExists(context.get(), path)); | 781 EXPECT_FALSE(ofu()->PathExists(context.get(), path)); |
| 784 | 782 |
| 785 exclusive = true; | 783 exclusive = true; |
| 786 recursive = false; | 784 recursive = false; |
| 787 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 785 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 788 context.get(), path, exclusive, recursive)); | 786 context.get(), path, exclusive, recursive)); |
| 789 | 787 |
| 790 context.reset(NewContext(NULL)); | 788 context.reset(NewContext(NULL)); |
| 791 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), path)); | 789 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), path)); |
| 792 context.reset(NewContext(NULL)); | 790 context.reset(NewContext(NULL)); |
| 793 EXPECT_TRUE(ofsfu()->PathExists(context.get(), path)); | 791 EXPECT_TRUE(ofu()->PathExists(context.get(), path)); |
| 794 | 792 |
| 795 exclusive = true; | 793 exclusive = true; |
| 796 recursive = false; | 794 recursive = false; |
| 797 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofsfu()->CreateDirectory( | 795 EXPECT_EQ(base::PLATFORM_FILE_ERROR_EXISTS, ofu()->CreateDirectory( |
| 798 context.get(), path, exclusive, recursive)); | 796 context.get(), path, exclusive, recursive)); |
| 799 } | 797 } |
| 800 | 798 |
| 801 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectory) { | 799 TEST_F(ObfuscatedFileUtilTest, TestReadDirectory) { |
| 802 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 800 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 803 bool exclusive = true; | 801 bool exclusive = true; |
| 804 bool recursive = true; | 802 bool recursive = true; |
| 805 FilePath path = UTF8ToFilePath("directory/to/use"); | 803 FilePath path = UTF8ToFilePath("directory/to/use"); |
| 806 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 804 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 807 context.get(), path, exclusive, recursive)); | 805 context.get(), path, exclusive, recursive)); |
| 808 TestReadDirectoryHelper(path); | 806 TestReadDirectoryHelper(path); |
| 809 } | 807 } |
| 810 | 808 |
| 811 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithSlash) { | 809 TEST_F(ObfuscatedFileUtilTest, TestReadRootWithSlash) { |
| 812 TestReadDirectoryHelper(UTF8ToFilePath("")); | 810 TestReadDirectoryHelper(UTF8ToFilePath("")); |
| 813 } | 811 } |
| 814 | 812 |
| 815 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadRootWithEmptyString) { | 813 TEST_F(ObfuscatedFileUtilTest, TestReadRootWithEmptyString) { |
| 816 TestReadDirectoryHelper(UTF8ToFilePath("/")); | 814 TestReadDirectoryHelper(UTF8ToFilePath("/")); |
| 817 } | 815 } |
| 818 | 816 |
| 819 TEST_F(ObfuscatedFileSystemFileUtilTest, TestReadDirectoryOnFile) { | 817 TEST_F(ObfuscatedFileUtilTest, TestReadDirectoryOnFile) { |
| 820 FilePath path = UTF8ToFilePath("file"); | 818 FilePath path = UTF8ToFilePath("file"); |
| 821 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 819 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 822 | 820 |
| 823 bool created = false; | 821 bool created = false; |
| 824 ASSERT_EQ(base::PLATFORM_FILE_OK, | 822 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 825 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 823 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 826 ASSERT_TRUE(created); | 824 ASSERT_TRUE(created); |
| 827 | 825 |
| 828 context.reset(NewContext(NULL)); | 826 context.reset(NewContext(NULL)); |
| 829 std::vector<base::FileUtilProxy::Entry> entries; | 827 std::vector<base::FileUtilProxy::Entry> entries; |
| 830 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 828 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 831 ofsfu()->ReadDirectory(context.get(), path, &entries)); | 829 ofu()->ReadDirectory(context.get(), path, &entries)); |
| 832 | 830 |
| 833 EXPECT_TRUE(ofsfu()->IsDirectoryEmpty(context.get(), path)); | 831 EXPECT_TRUE(ofu()->IsDirectoryEmpty(context.get(), path)); |
| 834 } | 832 } |
| 835 | 833 |
| 836 TEST_F(ObfuscatedFileSystemFileUtilTest, TestTouch) { | 834 TEST_F(ObfuscatedFileUtilTest, TestTouch) { |
| 837 FilePath path = UTF8ToFilePath("file"); | 835 FilePath path = UTF8ToFilePath("file"); |
| 838 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 836 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 839 | 837 |
| 840 base::Time last_access_time = base::Time::Now(); | 838 base::Time last_access_time = base::Time::Now(); |
| 841 base::Time last_modified_time = base::Time::Now(); | 839 base::Time last_modified_time = base::Time::Now(); |
| 842 | 840 |
| 843 // It's not there yet. | 841 // It's not there yet. |
| 844 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 842 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 845 ofsfu()->Touch( | 843 ofu()->Touch( |
| 846 context.get(), path, last_access_time, last_modified_time)); | 844 context.get(), path, last_access_time, last_modified_time)); |
| 847 | 845 |
| 848 // OK, now create it. | 846 // OK, now create it. |
| 849 context.reset(NewContext(NULL)); | 847 context.reset(NewContext(NULL)); |
| 850 bool created = false; | 848 bool created = false; |
| 851 ASSERT_EQ(base::PLATFORM_FILE_OK, | 849 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 852 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 850 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 853 ASSERT_TRUE(created); | 851 ASSERT_TRUE(created); |
| 854 TestTouchHelper(path, true); | 852 TestTouchHelper(path, true); |
| 855 | 853 |
| 856 // Now test a directory: | 854 // Now test a directory: |
| 857 context.reset(NewContext(NULL)); | 855 context.reset(NewContext(NULL)); |
| 858 bool exclusive = true; | 856 bool exclusive = true; |
| 859 bool recursive = false; | 857 bool recursive = false; |
| 860 path = UTF8ToFilePath("dir"); | 858 path = UTF8ToFilePath("dir"); |
| 861 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory(context.get(), | 859 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory(context.get(), |
| 862 path, exclusive, recursive)); | 860 path, exclusive, recursive)); |
| 863 TestTouchHelper(path, false); | 861 TestTouchHelper(path, false); |
| 864 } | 862 } |
| 865 | 863 |
| 866 TEST_F(ObfuscatedFileSystemFileUtilTest, TestPathQuotas) { | 864 TEST_F(ObfuscatedFileUtilTest, TestPathQuotas) { |
| 867 FilePath path = UTF8ToFilePath("fake/file"); | 865 FilePath path = UTF8ToFilePath("fake/file"); |
| 868 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 866 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 869 | 867 |
| 870 path = UTF8ToFilePath("file name"); | 868 path = UTF8ToFilePath("file name"); |
| 871 context->set_allowed_bytes_growth(5); | 869 context->set_allowed_bytes_growth(5); |
| 872 bool created = false; | 870 bool created = false; |
| 873 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 871 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 874 ofsfu()->EnsureFileExists( | 872 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 875 context.get(), path, &created)); | |
| 876 EXPECT_FALSE(created); | 873 EXPECT_FALSE(created); |
| 877 context->set_allowed_bytes_growth(1024); | 874 context->set_allowed_bytes_growth(1024); |
| 878 EXPECT_EQ(base::PLATFORM_FILE_OK, | 875 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 879 ofsfu()->EnsureFileExists( | 876 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 880 context.get(), path, &created)); | |
| 881 EXPECT_TRUE(created); | 877 EXPECT_TRUE(created); |
| 882 int64 path_cost = ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path); | 878 int64 path_cost = ObfuscatedFileUtil::ComputeFilePathCost(path); |
| 883 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); | 879 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 884 | 880 |
| 885 context->set_allowed_bytes_growth(1024); | 881 context->set_allowed_bytes_growth(1024); |
| 886 bool exclusive = true; | 882 bool exclusive = true; |
| 887 bool recursive = true; | 883 bool recursive = true; |
| 888 path = UTF8ToFilePath("directory/to/use"); | 884 path = UTF8ToFilePath("directory/to/use"); |
| 889 std::vector<FilePath::StringType> components; | 885 std::vector<FilePath::StringType> components; |
| 890 path.GetComponents(&components); | 886 path.GetComponents(&components); |
| 891 path_cost = 0; | 887 path_cost = 0; |
| 892 for (std::vector<FilePath::StringType>::iterator iter = components.begin(); | 888 for (std::vector<FilePath::StringType>::iterator iter = components.begin(); |
| 893 iter != components.end(); ++iter) { | 889 iter != components.end(); ++iter) { |
| 894 path_cost += ObfuscatedFileSystemFileUtil::ComputeFilePathCost( | 890 path_cost += ObfuscatedFileUtil::ComputeFilePathCost( |
| 895 FilePath(*iter)); | 891 FilePath(*iter)); |
| 896 } | 892 } |
| 897 context.reset(NewContext(NULL)); | 893 context.reset(NewContext(NULL)); |
| 898 context->set_allowed_bytes_growth(1024); | 894 context->set_allowed_bytes_growth(1024); |
| 899 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 895 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 900 context.get(), path, exclusive, recursive)); | 896 context.get(), path, exclusive, recursive)); |
| 901 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); | 897 EXPECT_EQ(1024 - path_cost, context->allowed_bytes_growth()); |
| 902 } | 898 } |
| 903 | 899 |
| 904 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileNotFound) { | 900 TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileNotFound) { |
| 905 FilePath source_path = UTF8ToFilePath("path0.txt"); | 901 FilePath source_path = UTF8ToFilePath("path0.txt"); |
| 906 FilePath dest_path = UTF8ToFilePath("path1.txt"); | 902 FilePath dest_path = UTF8ToFilePath("path1.txt"); |
| 907 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 903 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 908 | 904 |
| 909 bool is_copy_not_move = false; | 905 bool is_copy_not_move = false; |
| 910 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 906 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 911 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | 907 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
| 912 is_copy_not_move)); | 908 is_copy_not_move)); |
| 913 context.reset(NewContext(NULL)); | 909 context.reset(NewContext(NULL)); |
| 914 is_copy_not_move = true; | 910 is_copy_not_move = true; |
| 915 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 911 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 916 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | 912 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
| 917 is_copy_not_move)); | 913 is_copy_not_move)); |
| 918 source_path = UTF8ToFilePath("dir/dir/file"); | 914 source_path = UTF8ToFilePath("dir/dir/file"); |
| 919 bool exclusive = true; | 915 bool exclusive = true; |
| 920 bool recursive = true; | 916 bool recursive = true; |
| 921 context.reset(NewContext(NULL)); | 917 context.reset(NewContext(NULL)); |
| 922 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 918 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 923 context.get(), source_path.DirName(), exclusive, recursive)); | 919 context.get(), source_path.DirName(), exclusive, recursive)); |
| 924 is_copy_not_move = false; | 920 is_copy_not_move = false; |
| 925 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 921 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 926 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | 922 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
| 927 is_copy_not_move)); | 923 is_copy_not_move)); |
| 928 context.reset(NewContext(NULL)); | 924 context.reset(NewContext(NULL)); |
| 929 is_copy_not_move = true; | 925 is_copy_not_move = true; |
| 930 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 926 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 931 ofsfu()->CopyOrMoveFile(context.get(), source_path, dest_path, | 927 ofu()->CopyOrMoveFile(context.get(), source_path, dest_path, |
| 932 is_copy_not_move)); | 928 is_copy_not_move)); |
| 933 } | 929 } |
| 934 | 930 |
| 935 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyOrMoveFileSuccess) { | 931 TEST_F(ObfuscatedFileUtilTest, TestCopyOrMoveFileSuccess) { |
| 936 const int64 kSourceLength = 5; | 932 const int64 kSourceLength = 5; |
| 937 const int64 kDestLength = 50; | 933 const int64 kDestLength = 50; |
| 938 | 934 |
| 939 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { | 935 for (size_t i = 0; i < arraysize(kCopyMoveTestCases); ++i) { |
| 940 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); | 936 SCOPED_TRACE(testing::Message() << "kCopyMoveTestCase " << i); |
| 941 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; | 937 const CopyMoveTestCaseRecord& test_case = kCopyMoveTestCases[i]; |
| 942 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << | 938 SCOPED_TRACE(testing::Message() << "\t is_copy_not_move " << |
| 943 test_case.is_copy_not_move); | 939 test_case.is_copy_not_move); |
| 944 SCOPED_TRACE(testing::Message() << "\t source_path " << | 940 SCOPED_TRACE(testing::Message() << "\t source_path " << |
| 945 test_case.source_path); | 941 test_case.source_path); |
| 946 SCOPED_TRACE(testing::Message() << "\t dest_path " << | 942 SCOPED_TRACE(testing::Message() << "\t dest_path " << |
| 947 test_case.dest_path); | 943 test_case.dest_path); |
| 948 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << | 944 SCOPED_TRACE(testing::Message() << "\t cause_overwrite " << |
| 949 test_case.cause_overwrite); | 945 test_case.cause_overwrite); |
| 950 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 946 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 951 | 947 |
| 952 bool exclusive = false; | 948 bool exclusive = false; |
| 953 bool recursive = true; | 949 bool recursive = true; |
| 954 FilePath source_path = UTF8ToFilePath(test_case.source_path); | 950 FilePath source_path = UTF8ToFilePath(test_case.source_path); |
| 955 FilePath dest_path = UTF8ToFilePath(test_case.dest_path); | 951 FilePath dest_path = UTF8ToFilePath(test_case.dest_path); |
| 956 | 952 |
| 957 context.reset(NewContext(NULL)); | 953 context.reset(NewContext(NULL)); |
| 958 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 954 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 959 context.get(), source_path.DirName(), exclusive, recursive)); | 955 context.get(), source_path.DirName(), exclusive, recursive)); |
| 960 context.reset(NewContext(NULL)); | 956 context.reset(NewContext(NULL)); |
| 961 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 957 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 962 context.get(), dest_path.DirName(), exclusive, recursive)); | 958 context.get(), dest_path.DirName(), exclusive, recursive)); |
| 963 | 959 |
| 964 bool created = false; | 960 bool created = false; |
| 965 context.reset(NewContext(NULL)); | 961 context.reset(NewContext(NULL)); |
| 966 ASSERT_EQ(base::PLATFORM_FILE_OK, | 962 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 967 ofsfu()->EnsureFileExists(context.get(), source_path, &created)); | 963 ofu()->EnsureFileExists(context.get(), source_path, &created)); |
| 968 ASSERT_TRUE(created); | 964 ASSERT_TRUE(created); |
| 969 context.reset(NewContext(NULL)); | 965 context.reset(NewContext(NULL)); |
| 970 ASSERT_EQ(base::PLATFORM_FILE_OK, | 966 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 971 ofsfu()->Truncate(context.get(), source_path, kSourceLength)); | 967 ofu()->Truncate(context.get(), source_path, kSourceLength)); |
| 972 | 968 |
| 973 if (test_case.cause_overwrite) { | 969 if (test_case.cause_overwrite) { |
| 974 context.reset(NewContext(NULL)); | 970 context.reset(NewContext(NULL)); |
| 975 created = false; | 971 created = false; |
| 976 ASSERT_EQ(base::PLATFORM_FILE_OK, | 972 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 977 ofsfu()->EnsureFileExists(context.get(), dest_path, &created)); | 973 ofu()->EnsureFileExists(context.get(), dest_path, &created)); |
| 978 ASSERT_TRUE(created); | 974 ASSERT_TRUE(created); |
| 979 context.reset(NewContext(NULL)); | 975 context.reset(NewContext(NULL)); |
| 980 ASSERT_EQ(base::PLATFORM_FILE_OK, | 976 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 981 ofsfu()->Truncate(context.get(), dest_path, kDestLength)); | 977 ofu()->Truncate(context.get(), dest_path, kDestLength)); |
| 982 } | 978 } |
| 983 | 979 |
| 984 context.reset(NewContext(NULL)); | 980 context.reset(NewContext(NULL)); |
| 985 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CopyOrMoveFile(context.get(), | 981 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->CopyOrMoveFile(context.get(), |
| 986 source_path, dest_path, test_case.is_copy_not_move)); | 982 source_path, dest_path, test_case.is_copy_not_move)); |
| 987 if (test_case.is_copy_not_move) { | 983 if (test_case.is_copy_not_move) { |
| 988 base::PlatformFileInfo file_info; | 984 base::PlatformFileInfo file_info; |
| 989 FilePath local_path; | 985 FilePath local_path; |
| 990 context.reset(NewContext(NULL)); | 986 context.reset(NewContext(NULL)); |
| 991 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 987 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 992 context.get(), source_path, &file_info, &local_path)); | 988 context.get(), source_path, &file_info, &local_path)); |
| 993 EXPECT_EQ(kSourceLength, file_info.size); | 989 EXPECT_EQ(kSourceLength, file_info.size); |
| 994 EXPECT_EQ(base::PLATFORM_FILE_OK, | 990 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 995 ofsfu()->DeleteFile(context.get(), source_path)); | 991 ofu()->DeleteFile(context.get(), source_path)); |
| 996 } else { | 992 } else { |
| 997 base::PlatformFileInfo file_info; | 993 base::PlatformFileInfo file_info; |
| 998 FilePath local_path; | 994 FilePath local_path; |
| 999 context.reset(NewContext(NULL)); | 995 context.reset(NewContext(NULL)); |
| 1000 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofsfu()->GetFileInfo( | 996 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, ofu()->GetFileInfo( |
| 1001 context.get(), source_path, &file_info, &local_path)); | 997 context.get(), source_path, &file_info, &local_path)); |
| 1002 } | 998 } |
| 1003 base::PlatformFileInfo file_info; | 999 base::PlatformFileInfo file_info; |
| 1004 FilePath local_path; | 1000 FilePath local_path; |
| 1005 EXPECT_EQ(base::PLATFORM_FILE_OK, ofsfu()->GetFileInfo( | 1001 EXPECT_EQ(base::PLATFORM_FILE_OK, ofu()->GetFileInfo( |
| 1006 context.get(), dest_path, &file_info, &local_path)); | 1002 context.get(), dest_path, &file_info, &local_path)); |
| 1007 EXPECT_EQ(kSourceLength, file_info.size); | 1003 EXPECT_EQ(kSourceLength, file_info.size); |
| 1008 | 1004 |
| 1009 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1005 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1010 ofsfu()->DeleteFile(context.get(), dest_path)); | 1006 ofu()->DeleteFile(context.get(), dest_path)); |
| 1011 } | 1007 } |
| 1012 } | 1008 } |
| 1013 | 1009 |
| 1014 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyPathQuotas) { | 1010 TEST_F(ObfuscatedFileUtilTest, TestCopyPathQuotas) { |
| 1015 FilePath src_path = UTF8ToFilePath("src path"); | 1011 FilePath src_path = UTF8ToFilePath("src path"); |
| 1016 FilePath dest_path = UTF8ToFilePath("destination path"); | 1012 FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1017 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1013 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1018 bool created = false; | 1014 bool created = false; |
| 1019 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | 1015 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
| 1020 context.get(), src_path, &created)); | 1016 context.get(), src_path, &created)); |
| 1021 | 1017 |
| 1022 bool is_copy = true; | 1018 bool is_copy = true; |
| 1023 // Copy, no overwrite. | 1019 // Copy, no overwrite. |
| 1024 context->set_allowed_bytes_growth( | 1020 context->set_allowed_bytes_growth( |
| 1025 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - 1); | 1021 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - 1); |
| 1026 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 1022 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 1027 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1023 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1028 context.reset(NewContext(NULL)); | 1024 context.reset(NewContext(NULL)); |
| 1029 context->set_allowed_bytes_growth( | 1025 context->set_allowed_bytes_growth( |
| 1030 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path)); | 1026 ObfuscatedFileUtil::ComputeFilePathCost(dest_path)); |
| 1031 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1027 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1032 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1028 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1033 | 1029 |
| 1034 // Copy, with overwrite. | 1030 // Copy, with overwrite. |
| 1035 context.reset(NewContext(NULL)); | 1031 context.reset(NewContext(NULL)); |
| 1036 context->set_allowed_bytes_growth(0); | 1032 context->set_allowed_bytes_growth(0); |
| 1037 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1033 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1038 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1034 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1039 } | 1035 } |
| 1040 | 1036 |
| 1041 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithRename) { | 1037 TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithRename) { |
| 1042 FilePath src_path = UTF8ToFilePath("src path"); | 1038 FilePath src_path = UTF8ToFilePath("src path"); |
| 1043 FilePath dest_path = UTF8ToFilePath("destination path"); | 1039 FilePath dest_path = UTF8ToFilePath("destination path"); |
| 1044 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1040 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1045 bool created = false; | 1041 bool created = false; |
| 1046 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | 1042 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
| 1047 context.get(), src_path, &created)); | 1043 context.get(), src_path, &created)); |
| 1048 | 1044 |
| 1049 bool is_copy = false; | 1045 bool is_copy = false; |
| 1050 // Move, rename, no overwrite. | 1046 // Move, rename, no overwrite. |
| 1051 context.reset(NewContext(NULL)); | 1047 context.reset(NewContext(NULL)); |
| 1052 context->set_allowed_bytes_growth( | 1048 context->set_allowed_bytes_growth( |
| 1053 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - | 1049 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1054 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path) - 1); | 1050 ObfuscatedFileUtil::ComputeFilePathCost(src_path) - 1); |
| 1055 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, | 1051 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, |
| 1056 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1052 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1057 context.reset(NewContext(NULL)); | 1053 context.reset(NewContext(NULL)); |
| 1058 context->set_allowed_bytes_growth( | 1054 context->set_allowed_bytes_growth( |
| 1059 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(dest_path) - | 1055 ObfuscatedFileUtil::ComputeFilePathCost(dest_path) - |
| 1060 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path)); | 1056 ObfuscatedFileUtil::ComputeFilePathCost(src_path)); |
| 1061 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1057 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1062 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1058 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1063 | 1059 |
| 1064 context.reset(NewContext(NULL)); | 1060 context.reset(NewContext(NULL)); |
| 1065 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | 1061 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
| 1066 context.get(), src_path, &created)); | 1062 context.get(), src_path, &created)); |
| 1067 | 1063 |
| 1068 // Move, rename, with overwrite. | 1064 // Move, rename, with overwrite. |
| 1069 context.reset(NewContext(NULL)); | 1065 context.reset(NewContext(NULL)); |
| 1070 context->set_allowed_bytes_growth(0); | 1066 context->set_allowed_bytes_growth(0); |
| 1071 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1067 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1072 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1068 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1073 } | 1069 } |
| 1074 | 1070 |
| 1075 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMovePathQuotasWithoutRename) { | 1071 TEST_F(ObfuscatedFileUtilTest, TestMovePathQuotasWithoutRename) { |
| 1076 FilePath src_path = UTF8ToFilePath("src path"); | 1072 FilePath src_path = UTF8ToFilePath("src path"); |
| 1077 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1073 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1078 bool created = false; | 1074 bool created = false; |
| 1079 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | 1075 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
| 1080 context.get(), src_path, &created)); | 1076 context.get(), src_path, &created)); |
| 1081 | 1077 |
| 1082 bool exclusive = true; | 1078 bool exclusive = true; |
| 1083 bool recursive = false; | 1079 bool recursive = false; |
| 1084 FilePath dir_path = UTF8ToFilePath("directory path"); | 1080 FilePath dir_path = UTF8ToFilePath("directory path"); |
| 1085 context.reset(NewContext(NULL)); | 1081 context.reset(NewContext(NULL)); |
| 1086 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 1082 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 1087 context.get(), dir_path, exclusive, recursive)); | 1083 context.get(), dir_path, exclusive, recursive)); |
| 1088 | 1084 |
| 1089 FilePath dest_path = dir_path.Append(src_path); | 1085 FilePath dest_path = dir_path.Append(src_path); |
| 1090 | 1086 |
| 1091 bool is_copy = false; | 1087 bool is_copy = false; |
| 1092 int64 allowed_bytes_growth = -1000; // Over quota, this should still work. | 1088 int64 allowed_bytes_growth = -1000; // Over quota, this should still work. |
| 1093 // Move, no rename, no overwrite. | 1089 // Move, no rename, no overwrite. |
| 1094 context.reset(NewContext(NULL)); | 1090 context.reset(NewContext(NULL)); |
| 1095 context->set_allowed_bytes_growth(allowed_bytes_growth); | 1091 context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1096 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1092 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1097 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1093 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1098 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); | 1094 EXPECT_EQ(allowed_bytes_growth, context->allowed_bytes_growth()); |
| 1099 | 1095 |
| 1100 // Move, no rename, with overwrite. | 1096 // Move, no rename, with overwrite. |
| 1101 context.reset(NewContext(NULL)); | 1097 context.reset(NewContext(NULL)); |
| 1102 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->EnsureFileExists( | 1098 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->EnsureFileExists( |
| 1103 context.get(), src_path, &created)); | 1099 context.get(), src_path, &created)); |
| 1104 context.reset(NewContext(NULL)); | 1100 context.reset(NewContext(NULL)); |
| 1105 context->set_allowed_bytes_growth(allowed_bytes_growth); | 1101 context->set_allowed_bytes_growth(allowed_bytes_growth); |
| 1106 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1102 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1107 ofsfu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); | 1103 ofu()->CopyOrMoveFile(context.get(), src_path, dest_path, is_copy)); |
| 1108 EXPECT_EQ( | 1104 EXPECT_EQ( |
| 1109 allowed_bytes_growth + | 1105 allowed_bytes_growth + |
| 1110 ObfuscatedFileSystemFileUtil::ComputeFilePathCost(src_path), | 1106 ObfuscatedFileUtil::ComputeFilePathCost(src_path), |
| 1111 context->allowed_bytes_growth()); | 1107 context->allowed_bytes_growth()); |
| 1112 } | 1108 } |
| 1113 | 1109 |
| 1114 TEST_F(ObfuscatedFileSystemFileUtilTest, TestCopyInForeignFile) { | 1110 TEST_F(ObfuscatedFileUtilTest, TestCopyInForeignFile) { |
| 1115 TestCopyInForeignFileHelper(false /* overwrite */); | 1111 TestCopyInForeignFileHelper(false /* overwrite */); |
| 1116 TestCopyInForeignFileHelper(true /* overwrite */); | 1112 TestCopyInForeignFileHelper(true /* overwrite */); |
| 1117 } | 1113 } |
| 1118 | 1114 |
| 1119 TEST_F(ObfuscatedFileSystemFileUtilTest, TestEnumerator) { | 1115 TEST_F(ObfuscatedFileUtilTest, TestEnumerator) { |
| 1120 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1116 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1121 FilePath src_path = UTF8ToFilePath("source dir"); | 1117 FilePath src_path = UTF8ToFilePath("source dir"); |
| 1122 bool exclusive = true; | 1118 bool exclusive = true; |
| 1123 bool recursive = false; | 1119 bool recursive = false; |
| 1124 ASSERT_EQ(base::PLATFORM_FILE_OK, ofsfu()->CreateDirectory( | 1120 ASSERT_EQ(base::PLATFORM_FILE_OK, ofu()->CreateDirectory( |
| 1125 context.get(), src_path, exclusive, recursive)); | 1121 context.get(), src_path, exclusive, recursive)); |
| 1126 | 1122 |
| 1127 std::set<FilePath::StringType> files; | 1123 std::set<FilePath::StringType> files; |
| 1128 std::set<FilePath::StringType> directories; | 1124 std::set<FilePath::StringType> directories; |
| 1129 FillTestDirectory(src_path, &files, &directories); | 1125 FillTestDirectory(src_path, &files, &directories); |
| 1130 | 1126 |
| 1131 FilePath dest_path = UTF8ToFilePath("destination dir"); | 1127 FilePath dest_path = UTF8ToFilePath("destination dir"); |
| 1132 | 1128 |
| 1133 context.reset(NewContext(NULL)); | 1129 context.reset(NewContext(NULL)); |
| 1134 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | 1130 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
| 1135 context.reset(NewContext(NULL)); | 1131 context.reset(NewContext(NULL)); |
| 1136 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1132 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1137 ofsfu()->Copy(context.get(), src_path, dest_path)); | 1133 ofu()->Copy(context.get(), src_path, dest_path)); |
| 1138 | 1134 |
| 1139 ValidateTestDirectory(dest_path, files, directories); | 1135 ValidateTestDirectory(dest_path, files, directories); |
| 1140 context.reset(NewContext(NULL)); | 1136 context.reset(NewContext(NULL)); |
| 1141 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), src_path)); | 1137 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), src_path)); |
| 1142 context.reset(NewContext(NULL)); | 1138 context.reset(NewContext(NULL)); |
| 1143 EXPECT_TRUE(ofsfu()->DirectoryExists(context.get(), dest_path)); | 1139 EXPECT_TRUE(ofu()->DirectoryExists(context.get(), dest_path)); |
| 1144 context.reset(NewContext(NULL)); | 1140 context.reset(NewContext(NULL)); |
| 1145 recursive = true; | 1141 recursive = true; |
| 1146 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1142 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1147 ofsfu()->Delete(context.get(), dest_path, recursive)); | 1143 ofu()->Delete(context.get(), dest_path, recursive)); |
| 1148 context.reset(NewContext(NULL)); | 1144 context.reset(NewContext(NULL)); |
| 1149 EXPECT_FALSE(ofsfu()->DirectoryExists(context.get(), dest_path)); | 1145 EXPECT_FALSE(ofu()->DirectoryExists(context.get(), dest_path)); |
| 1150 } | 1146 } |
| 1151 | 1147 |
| 1152 TEST_F(ObfuscatedFileSystemFileUtilTest, TestMigration) { | 1148 TEST_F(ObfuscatedFileUtilTest, TestMigration) { |
| 1153 ScopedTempDir source_dir; | 1149 ScopedTempDir source_dir; |
| 1154 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); | 1150 ASSERT_TRUE(source_dir.CreateUniqueTempDir()); |
| 1155 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); | 1151 FilePath root_path = source_dir.path().AppendASCII("chrome-pLmnMWXE7NzTFRsn"); |
| 1156 ASSERT_TRUE(file_util::CreateDirectory(root_path)); | 1152 ASSERT_TRUE(file_util::CreateDirectory(root_path)); |
| 1157 | 1153 |
| 1158 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | 1154 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1159 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); | 1155 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1160 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | 1156 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1161 FilePath local_src_path = root_path.Append(test_case.path); | 1157 FilePath local_src_path = root_path.Append(test_case.path); |
| 1162 if (test_case.is_directory) { | 1158 if (test_case.is_directory) { |
| 1163 ASSERT_TRUE( | 1159 ASSERT_TRUE( |
| 1164 file_util::CreateDirectory(local_src_path)); | 1160 file_util::CreateDirectory(local_src_path)); |
| 1165 } else { | 1161 } else { |
| 1166 base::PlatformFileError error_code; | 1162 base::PlatformFileError error_code; |
| 1167 bool created = false; | 1163 bool created = false; |
| 1168 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; | 1164 int file_flags = base::PLATFORM_FILE_CREATE | base::PLATFORM_FILE_WRITE; |
| 1169 base::PlatformFile file_handle = | 1165 base::PlatformFile file_handle = |
| 1170 base::CreatePlatformFile( | 1166 base::CreatePlatformFile( |
| 1171 local_src_path, file_flags, &created, &error_code); | 1167 local_src_path, file_flags, &created, &error_code); |
| 1172 EXPECT_TRUE(created); | 1168 EXPECT_TRUE(created); |
| 1173 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); | 1169 ASSERT_NE(base::kInvalidPlatformFileValue, file_handle); |
| 1174 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); | 1170 ASSERT_EQ(base::PLATFORM_FILE_OK, error_code); |
| 1175 ASSERT_TRUE( | 1171 ASSERT_TRUE( |
| 1176 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); | 1172 base::TruncatePlatformFile(file_handle, test_case.data_file_size)); |
| 1177 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); | 1173 EXPECT_TRUE(base::ClosePlatformFile(file_handle)); |
| 1178 } | 1174 } |
| 1179 } | 1175 } |
| 1180 | 1176 |
| 1181 EXPECT_TRUE(ofsfu()->MigrateFromOldSandbox(origin(), type(), root_path)); | 1177 EXPECT_TRUE(ofu()->MigrateFromOldSandbox(origin(), type(), root_path)); |
| 1182 | 1178 |
| 1183 FilePath new_root = | 1179 FilePath new_root = |
| 1184 test_directory().AppendASCII("File System").AppendASCII("000").Append( | 1180 test_directory().AppendASCII("File System").AppendASCII("000").Append( |
| 1185 ofsfu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); | 1181 ofu()->GetDirectoryNameForType(type())).AppendASCII("Legacy"); |
| 1186 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | 1182 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1187 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); | 1183 SCOPED_TRACE(testing::Message() << "Validating kMigrationTestPath " << i); |
| 1188 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | 1184 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1189 FilePath local_data_path = new_root.Append(test_case.path); | 1185 FilePath local_data_path = new_root.Append(test_case.path); |
| 1190 #if defined(OS_WIN) | 1186 #if defined(OS_WIN) |
| 1191 local_data_path = local_data_path.NormalizeWindowsPathSeparators(); | 1187 local_data_path = local_data_path.NormalizeWindowsPathSeparators(); |
| 1192 #endif | 1188 #endif |
| 1193 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1189 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1194 base::PlatformFileInfo ofsfu_file_info; | 1190 base::PlatformFileInfo ofu_file_info; |
| 1195 FilePath data_path; | 1191 FilePath data_path; |
| 1196 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); | 1192 SCOPED_TRACE(testing::Message() << "Path is " << test_case.path); |
| 1197 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1193 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1198 ofsfu()->GetFileInfo(context.get(), FilePath(test_case.path), | 1194 ofu()->GetFileInfo(context.get(), FilePath(test_case.path), |
| 1199 &ofsfu_file_info, &data_path)); | 1195 &ofu_file_info, &data_path)); |
| 1200 if (test_case.is_directory) { | 1196 if (test_case.is_directory) { |
| 1201 EXPECT_TRUE(ofsfu_file_info.is_directory); | 1197 EXPECT_TRUE(ofu_file_info.is_directory); |
| 1202 } else { | 1198 } else { |
| 1203 base::PlatformFileInfo platform_file_info; | 1199 base::PlatformFileInfo platform_file_info; |
| 1204 SCOPED_TRACE(testing::Message() << "local_data_path is " << | 1200 SCOPED_TRACE(testing::Message() << "local_data_path is " << |
| 1205 local_data_path.value()); | 1201 local_data_path.value()); |
| 1206 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); | 1202 SCOPED_TRACE(testing::Message() << "data_path is " << data_path.value()); |
| 1207 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); | 1203 ASSERT_TRUE(file_util::GetFileInfo(local_data_path, &platform_file_info)); |
| 1208 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); | 1204 EXPECT_EQ(test_case.data_file_size, platform_file_info.size); |
| 1209 EXPECT_FALSE(platform_file_info.is_directory); | 1205 EXPECT_FALSE(platform_file_info.is_directory); |
| 1210 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1206 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1211 EXPECT_EQ(local_data_path, data_path); | 1207 EXPECT_EQ(local_data_path, data_path); |
| 1212 EXPECT_EQ(platform_file_info.size, ofsfu_file_info.size); | 1208 EXPECT_EQ(platform_file_info.size, ofu_file_info.size); |
| 1213 EXPECT_FALSE(ofsfu_file_info.is_directory); | 1209 EXPECT_FALSE(ofu_file_info.is_directory); |
| 1214 } | 1210 } |
| 1215 } | 1211 } |
| 1216 } | 1212 } |
| 1217 | 1213 |
| 1218 TEST_F(ObfuscatedFileSystemFileUtilTest, TestOriginEnumerator) { | 1214 TEST_F(ObfuscatedFileUtilTest, TestOriginEnumerator) { |
| 1219 scoped_ptr<ObfuscatedFileSystemFileUtil::AbstractOriginEnumerator> | 1215 scoped_ptr<ObfuscatedFileUtil::AbstractOriginEnumerator> |
| 1220 enumerator(ofsfu()->CreateOriginEnumerator()); | 1216 enumerator(ofu()->CreateOriginEnumerator()); |
| 1221 // The test helper starts out with a single filesystem. | 1217 // The test helper starts out with a single filesystem. |
| 1222 EXPECT_TRUE(enumerator.get()); | 1218 EXPECT_TRUE(enumerator.get()); |
| 1223 EXPECT_EQ(origin(), enumerator->Next()); | 1219 EXPECT_EQ(origin(), enumerator->Next()); |
| 1224 ASSERT_TRUE(type() == kFileSystemTypeTemporary); | 1220 ASSERT_TRUE(type() == kFileSystemTypeTemporary); |
| 1225 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | 1221 EXPECT_TRUE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1226 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); | 1222 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1227 EXPECT_EQ(GURL(), enumerator->Next()); | 1223 EXPECT_EQ(GURL(), enumerator->Next()); |
| 1228 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); | 1224 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypeTemporary)); |
| 1229 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); | 1225 EXPECT_FALSE(enumerator->HasFileSystemType(kFileSystemTypePersistent)); |
| 1230 | 1226 |
| 1231 std::set<GURL> origins_expected; | 1227 std::set<GURL> origins_expected; |
| 1232 origins_expected.insert(origin()); | 1228 origins_expected.insert(origin()); |
| 1233 | 1229 |
| 1234 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { | 1230 for (size_t i = 0; i < arraysize(kOriginEnumerationTestRecords); ++i) { |
| 1235 SCOPED_TRACE(testing::Message() << | 1231 SCOPED_TRACE(testing::Message() << |
| 1236 "Validating kOriginEnumerationTestRecords " << i); | 1232 "Validating kOriginEnumerationTestRecords " << i); |
| 1237 const OriginEnumerationTestRecord& record = | 1233 const OriginEnumerationTestRecord& record = |
| 1238 kOriginEnumerationTestRecords[i]; | 1234 kOriginEnumerationTestRecords[i]; |
| 1239 GURL origin_url(record.origin_url); | 1235 GURL origin_url(record.origin_url); |
| 1240 origins_expected.insert(origin_url); | 1236 origins_expected.insert(origin_url); |
| 1241 if (record.has_temporary) { | 1237 if (record.has_temporary) { |
| 1242 scoped_ptr<FileSystemTestOriginHelper> helper( | 1238 scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1243 NewHelper(origin_url, kFileSystemTypeTemporary)); | 1239 NewHelper(origin_url, kFileSystemTypeTemporary)); |
| 1244 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); | 1240 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
| 1245 context->set_src_origin_url(origin_url); | 1241 context->set_src_origin_url(origin_url); |
| 1246 context->set_src_type(kFileSystemTypeTemporary); | 1242 context->set_src_type(kFileSystemTypeTemporary); |
| 1247 bool created = false; | 1243 bool created = false; |
| 1248 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1244 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1249 ofsfu()->EnsureFileExists(context.get(), | 1245 ofu()->EnsureFileExists(context.get(), |
| 1250 FilePath().AppendASCII("file"), &created)); | 1246 FilePath().AppendASCII("file"), &created)); |
| 1251 EXPECT_TRUE(created); | 1247 EXPECT_TRUE(created); |
| 1252 } | 1248 } |
| 1253 if (record.has_persistent) { | 1249 if (record.has_persistent) { |
| 1254 scoped_ptr<FileSystemTestOriginHelper> helper( | 1250 scoped_ptr<FileSystemTestOriginHelper> helper( |
| 1255 NewHelper(origin_url, kFileSystemTypePersistent)); | 1251 NewHelper(origin_url, kFileSystemTypePersistent)); |
| 1256 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); | 1252 scoped_ptr<FileSystemOperationContext> context(NewContext(helper.get())); |
| 1257 context->set_src_origin_url(origin_url); | 1253 context->set_src_origin_url(origin_url); |
| 1258 context->set_src_type(kFileSystemTypePersistent); | 1254 context->set_src_type(kFileSystemTypePersistent); |
| 1259 bool created = false; | 1255 bool created = false; |
| 1260 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1256 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1261 ofsfu()->EnsureFileExists(context.get(), | 1257 ofu()->EnsureFileExists(context.get(), |
| 1262 FilePath().AppendASCII("file"), &created)); | 1258 FilePath().AppendASCII("file"), &created)); |
| 1263 EXPECT_TRUE(created); | 1259 EXPECT_TRUE(created); |
| 1264 } | 1260 } |
| 1265 } | 1261 } |
| 1266 enumerator.reset(ofsfu()->CreateOriginEnumerator()); | 1262 enumerator.reset(ofu()->CreateOriginEnumerator()); |
| 1267 EXPECT_TRUE(enumerator.get()); | 1263 EXPECT_TRUE(enumerator.get()); |
| 1268 std::set<GURL> origins_found; | 1264 std::set<GURL> origins_found; |
| 1269 GURL origin_url; | 1265 GURL origin_url; |
| 1270 while (!(origin_url = enumerator->Next()).is_empty()) { | 1266 while (!(origin_url = enumerator->Next()).is_empty()) { |
| 1271 origins_found.insert(origin_url); | 1267 origins_found.insert(origin_url); |
| 1272 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); | 1268 SCOPED_TRACE(testing::Message() << "Handling " << origin_url.spec()); |
| 1273 bool found = false; | 1269 bool found = false; |
| 1274 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); | 1270 for (size_t i = 0; !found && i < arraysize(kOriginEnumerationTestRecords); |
| 1275 ++i) { | 1271 ++i) { |
| 1276 const OriginEnumerationTestRecord& record = | 1272 const OriginEnumerationTestRecord& record = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1295 EXPECT_TRUE(found); | 1291 EXPECT_TRUE(found); |
| 1296 } | 1292 } |
| 1297 | 1293 |
| 1298 std::set<GURL> diff; | 1294 std::set<GURL> diff; |
| 1299 std::set_symmetric_difference(origins_expected.begin(), | 1295 std::set_symmetric_difference(origins_expected.begin(), |
| 1300 origins_expected.end(), origins_found.begin(), origins_found.end(), | 1296 origins_expected.end(), origins_found.begin(), origins_found.end(), |
| 1301 inserter(diff, diff.begin())); | 1297 inserter(diff, diff.begin())); |
| 1302 EXPECT_TRUE(diff.empty()); | 1298 EXPECT_TRUE(diff.empty()); |
| 1303 } | 1299 } |
| 1304 | 1300 |
| 1305 TEST_F(ObfuscatedFileSystemFileUtilTest, TestRevokeUsageCache) { | 1301 TEST_F(ObfuscatedFileUtilTest, TestRevokeUsageCache) { |
| 1306 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); | 1302 scoped_ptr<FileSystemOperationContext> context(NewContext(NULL)); |
| 1307 | 1303 |
| 1308 int64 expected_quota = 0; | 1304 int64 expected_quota = 0; |
| 1309 | 1305 |
| 1310 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { | 1306 for (size_t i = 0; i < arraysize(kMigrationTestCases); ++i) { |
| 1311 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); | 1307 SCOPED_TRACE(testing::Message() << "Creating kMigrationTestPath " << i); |
| 1312 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; | 1308 const MigrationTestCaseRecord& test_case = kMigrationTestCases[i]; |
| 1313 FilePath path(test_case.path); | 1309 FilePath path(test_case.path); |
| 1314 expected_quota += ObfuscatedFileSystemFileUtil::ComputeFilePathCost(path); | 1310 expected_quota += ObfuscatedFileUtil::ComputeFilePathCost(path); |
| 1315 if (test_case.is_directory) { | 1311 if (test_case.is_directory) { |
| 1316 bool exclusive = true; | 1312 bool exclusive = true; |
| 1317 bool recursive = false; | 1313 bool recursive = false; |
| 1318 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1314 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1319 ofsfu()->CreateDirectory(context.get(), path, exclusive, recursive)); | 1315 ofu()->CreateDirectory(context.get(), path, exclusive, recursive)); |
| 1320 } else { | 1316 } else { |
| 1321 bool created = false; | 1317 bool created = false; |
| 1322 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1318 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1323 ofsfu()->EnsureFileExists(context.get(), path, &created)); | 1319 ofu()->EnsureFileExists(context.get(), path, &created)); |
| 1324 ASSERT_TRUE(created); | 1320 ASSERT_TRUE(created); |
| 1325 ASSERT_EQ(base::PLATFORM_FILE_OK, | 1321 ASSERT_EQ(base::PLATFORM_FILE_OK, |
| 1326 ofsfu()->Truncate(context.get(), path, | 1322 ofu()->Truncate(context.get(), path, |
| 1327 test_case.data_file_size)); | 1323 test_case.data_file_size)); |
| 1328 expected_quota += test_case.data_file_size; | 1324 expected_quota += test_case.data_file_size; |
| 1329 } | 1325 } |
| 1330 } | 1326 } |
| 1331 EXPECT_EQ(expected_quota, SizeInUsageFile()); | 1327 EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1332 RevokeUsageCache(); | 1328 RevokeUsageCache(); |
| 1333 EXPECT_EQ(-1, SizeInUsageFile()); | 1329 EXPECT_EQ(-1, SizeInUsageFile()); |
| 1334 GetUsageFromQuotaManager(); | 1330 GetUsageFromQuotaManager(); |
| 1335 EXPECT_EQ(expected_quota, SizeInUsageFile()); | 1331 EXPECT_EQ(expected_quota, SizeInUsageFile()); |
| 1336 EXPECT_EQ(expected_quota, usage()); | 1332 EXPECT_EQ(expected_quota, usage()); |
| 1337 } | 1333 } |
| 1338 | 1334 |
| 1339 TEST_F(ObfuscatedFileSystemFileUtilTest, TestInconsistency) { | 1335 TEST_F(ObfuscatedFileUtilTest, TestInconsistency) { |
| 1340 const FilePath kPath1 = FilePath().AppendASCII("hoge"); | 1336 const FilePath kPath1 = FilePath().AppendASCII("hoge"); |
| 1341 const FilePath kPath2 = FilePath().AppendASCII("fuga"); | 1337 const FilePath kPath2 = FilePath().AppendASCII("fuga"); |
| 1342 | 1338 |
| 1343 scoped_ptr<FileSystemOperationContext> context; | 1339 scoped_ptr<FileSystemOperationContext> context; |
| 1344 base::PlatformFile file; | 1340 base::PlatformFile file; |
| 1345 base::PlatformFileInfo file_info; | 1341 base::PlatformFileInfo file_info; |
| 1346 FilePath data_path; | 1342 FilePath data_path; |
| 1347 bool created = false; | 1343 bool created = false; |
| 1348 | 1344 |
| 1349 // Create a non-empty file. | 1345 // Create a non-empty file. |
| 1350 context.reset(NewContext(NULL)); | 1346 context.reset(NewContext(NULL)); |
| 1351 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1347 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1352 ofsfu()->EnsureFileExists(context.get(), kPath1, &created)); | 1348 ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
| 1353 EXPECT_TRUE(created); | 1349 EXPECT_TRUE(created); |
| 1354 context.reset(NewContext(NULL)); | 1350 context.reset(NewContext(NULL)); |
| 1355 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1351 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1356 ofsfu()->Truncate(context.get(), kPath1, 10)); | 1352 ofu()->Truncate(context.get(), kPath1, 10)); |
| 1357 context.reset(NewContext(NULL)); | 1353 context.reset(NewContext(NULL)); |
| 1358 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1354 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1359 ofsfu()->GetFileInfo( | 1355 ofu()->GetFileInfo( |
| 1360 context.get(), kPath1, &file_info, &data_path)); | 1356 context.get(), kPath1, &file_info, &data_path)); |
| 1361 EXPECT_EQ(10, file_info.size); | 1357 EXPECT_EQ(10, file_info.size); |
| 1362 | 1358 |
| 1363 // Destroy database to make inconsistency between database and filesystem. | 1359 // Destroy database to make inconsistency between database and filesystem. |
| 1364 ofsfu()->DestroyDirectoryDatabase(origin(), type()); | 1360 ofu()->DestroyDirectoryDatabase(origin(), type()); |
| 1365 | 1361 |
| 1366 // Try to get file info of broken file. | 1362 // Try to get file info of broken file. |
| 1367 context.reset(NewContext(NULL)); | 1363 context.reset(NewContext(NULL)); |
| 1368 EXPECT_FALSE(ofsfu()->PathExists(context.get(), kPath1)); | 1364 EXPECT_FALSE(ofu()->PathExists(context.get(), kPath1)); |
| 1369 context.reset(NewContext(NULL)); | 1365 context.reset(NewContext(NULL)); |
| 1370 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1366 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1371 ofsfu()->EnsureFileExists(context.get(), kPath1, &created)); | 1367 ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
| 1372 EXPECT_TRUE(created); | 1368 EXPECT_TRUE(created); |
| 1373 context.reset(NewContext(NULL)); | 1369 context.reset(NewContext(NULL)); |
| 1374 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1370 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1375 ofsfu()->GetFileInfo( | 1371 ofu()->GetFileInfo( |
| 1376 context.get(), kPath1, &file_info, &data_path)); | 1372 context.get(), kPath1, &file_info, &data_path)); |
| 1377 EXPECT_EQ(0, file_info.size); | 1373 EXPECT_EQ(0, file_info.size); |
| 1378 | 1374 |
| 1379 // Make another broken file to |kPath2|. | 1375 // Make another broken file to |kPath2|. |
| 1380 context.reset(NewContext(NULL)); | 1376 context.reset(NewContext(NULL)); |
| 1381 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1377 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1382 ofsfu()->EnsureFileExists(context.get(), kPath2, &created)); | 1378 ofu()->EnsureFileExists(context.get(), kPath2, &created)); |
| 1383 EXPECT_TRUE(created); | 1379 EXPECT_TRUE(created); |
| 1384 | 1380 |
| 1385 // Destroy again. | 1381 // Destroy again. |
| 1386 ofsfu()->DestroyDirectoryDatabase(origin(), type()); | 1382 ofu()->DestroyDirectoryDatabase(origin(), type()); |
| 1387 | 1383 |
| 1388 // Repair broken |kPath1|. | 1384 // Repair broken |kPath1|. |
| 1389 context.reset(NewContext(NULL)); | 1385 context.reset(NewContext(NULL)); |
| 1390 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, | 1386 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NOT_FOUND, |
| 1391 ofsfu()->Touch(context.get(), kPath1, base::Time::Now(), | 1387 ofu()->Touch(context.get(), kPath1, base::Time::Now(), |
| 1392 base::Time::Now())); | 1388 base::Time::Now())); |
| 1393 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1389 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1394 ofsfu()->EnsureFileExists(context.get(), kPath1, &created)); | 1390 ofu()->EnsureFileExists(context.get(), kPath1, &created)); |
| 1395 EXPECT_TRUE(created); | 1391 EXPECT_TRUE(created); |
| 1396 | 1392 |
| 1397 // Copy from sound |kPath1| to broken |kPath2|. | 1393 // Copy from sound |kPath1| to broken |kPath2|. |
| 1398 context.reset(NewContext(NULL)); | 1394 context.reset(NewContext(NULL)); |
| 1399 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1395 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1400 ofsfu()->CopyOrMoveFile(context.get(), kPath1, kPath2, | 1396 ofu()->CopyOrMoveFile(context.get(), kPath1, kPath2, |
| 1401 true /* copy */)); | 1397 true /* copy */)); |
| 1402 | 1398 |
| 1403 ofsfu()->DestroyDirectoryDatabase(origin(), type()); | 1399 ofu()->DestroyDirectoryDatabase(origin(), type()); |
| 1404 context.reset(NewContext(NULL)); | 1400 context.reset(NewContext(NULL)); |
| 1405 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1401 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1406 ofsfu()->CreateOrOpen( | 1402 ofu()->CreateOrOpen( |
| 1407 context.get(), kPath1, | 1403 context.get(), kPath1, |
| 1408 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, | 1404 base::PLATFORM_FILE_READ | base::PLATFORM_FILE_CREATE, |
| 1409 &file, &created)); | 1405 &file, &created)); |
| 1410 EXPECT_TRUE(created); | 1406 EXPECT_TRUE(created); |
| 1411 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); | 1407 EXPECT_TRUE(base::GetPlatformFileInfo(file, &file_info)); |
| 1412 EXPECT_EQ(0, file_info.size); | 1408 EXPECT_EQ(0, file_info.size); |
| 1413 EXPECT_TRUE(base::ClosePlatformFile(file)); | 1409 EXPECT_TRUE(base::ClosePlatformFile(file)); |
| 1414 } | 1410 } |
| 1415 | 1411 |
| 1416 TEST_F(ObfuscatedFileSystemFileUtilTest, TestIncompleteDirectoryReading) { | 1412 TEST_F(ObfuscatedFileUtilTest, TestIncompleteDirectoryReading) { |
| 1417 const FilePath kPath[] = { | 1413 const FilePath kPath[] = { |
| 1418 FilePath().AppendASCII("foo"), | 1414 FilePath().AppendASCII("foo"), |
| 1419 FilePath().AppendASCII("bar"), | 1415 FilePath().AppendASCII("bar"), |
| 1420 FilePath().AppendASCII("baz") | 1416 FilePath().AppendASCII("baz") |
| 1421 }; | 1417 }; |
| 1422 scoped_ptr<FileSystemOperationContext> context; | 1418 scoped_ptr<FileSystemOperationContext> context; |
| 1423 | 1419 |
| 1424 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { | 1420 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kPath); ++i) { |
| 1425 bool created = false; | 1421 bool created = false; |
| 1426 context.reset(NewContext(NULL)); | 1422 context.reset(NewContext(NULL)); |
| 1427 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1423 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1428 ofsfu()->EnsureFileExists(context.get(), kPath[i], &created)); | 1424 ofu()->EnsureFileExists(context.get(), kPath[i], &created)); |
| 1429 EXPECT_TRUE(created); | 1425 EXPECT_TRUE(created); |
| 1430 } | 1426 } |
| 1431 | 1427 |
| 1432 context.reset(NewContext(NULL)); | 1428 context.reset(NewContext(NULL)); |
| 1433 std::vector<base::FileUtilProxy::Entry> entries; | 1429 std::vector<base::FileUtilProxy::Entry> entries; |
| 1434 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1430 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1435 ofsfu()->ReadDirectory(context.get(), FilePath(), &entries)); | 1431 ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
| 1436 EXPECT_EQ(3u, entries.size()); | 1432 EXPECT_EQ(3u, entries.size()); |
| 1437 | 1433 |
| 1438 context.reset(NewContext(NULL)); | 1434 context.reset(NewContext(NULL)); |
| 1439 FilePath local_path; | 1435 FilePath local_path; |
| 1440 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1436 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1441 ofsfu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); | 1437 ofu()->GetLocalFilePath(context.get(), kPath[0], &local_path)); |
| 1442 EXPECT_TRUE(file_util::Delete(local_path, false)); | 1438 EXPECT_TRUE(file_util::Delete(local_path, false)); |
| 1443 | 1439 |
| 1444 context.reset(NewContext(NULL)); | 1440 context.reset(NewContext(NULL)); |
| 1445 entries.clear(); | 1441 entries.clear(); |
| 1446 EXPECT_EQ(base::PLATFORM_FILE_OK, | 1442 EXPECT_EQ(base::PLATFORM_FILE_OK, |
| 1447 ofsfu()->ReadDirectory(context.get(), FilePath(), &entries)); | 1443 ofu()->ReadDirectory(context.get(), FilePath(), &entries)); |
| 1448 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); | 1444 EXPECT_EQ(ARRAYSIZE_UNSAFE(kPath) - 1, entries.size()); |
| 1449 } | 1445 } |
| OLD | NEW |