| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/basictypes.h" | |
| 6 #include "base/bind.h" | |
| 7 #include "base/file_util.h" | |
| 8 #include "base/files/scoped_temp_dir.h" | |
| 9 #include "base/message_loop.h" | |
| 10 #include "base/message_loop_proxy.h" | |
| 11 #include "base/platform_file.h" | |
| 12 #include "googleurl/src/gurl.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 #include "webkit/fileapi/file_system_context.h" | |
| 15 #include "webkit/fileapi/file_system_operation_context.h" | |
| 16 #include "webkit/fileapi/file_system_quota_client.h" | |
| 17 #include "webkit/fileapi/file_system_types.h" | |
| 18 #include "webkit/fileapi/file_system_usage_cache.h" | |
| 19 #include "webkit/fileapi/file_system_util.h" | |
| 20 #include "webkit/fileapi/mock_file_system_context.h" | |
| 21 #include "webkit/fileapi/obfuscated_file_util.h" | |
| 22 #include "webkit/fileapi/sandbox_mount_point_provider.h" | |
| 23 #include "webkit/quota/quota_types.h" | |
| 24 | |
| 25 namespace fileapi { | |
| 26 namespace { | |
| 27 | |
| 28 const char kDummyURL1[] = "http://www.dummy.org"; | |
| 29 const char kDummyURL2[] = "http://www.example.com"; | |
| 30 const char kDummyURL3[] = "http://www.bleh"; | |
| 31 | |
| 32 // Declared to shorten the variable names. | |
| 33 const quota::StorageType kTemporary = quota::kStorageTypeTemporary; | |
| 34 const quota::StorageType kPersistent = quota::kStorageTypePersistent; | |
| 35 | |
| 36 } // namespace | |
| 37 | |
| 38 class FileSystemQuotaClientTest : public testing::Test { | |
| 39 public: | |
| 40 FileSystemQuotaClientTest() | |
| 41 : weak_factory_(this), | |
| 42 additional_callback_count_(0), | |
| 43 deletion_status_(quota::kQuotaStatusUnknown) { | |
| 44 } | |
| 45 | |
| 46 virtual void SetUp() { | |
| 47 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 48 file_system_context_ = CreateFileSystemContextForTesting( | |
| 49 NULL, data_dir_.path()); | |
| 50 } | |
| 51 | |
| 52 struct TestFile { | |
| 53 bool isDirectory; | |
| 54 const char* name; | |
| 55 int64 size; | |
| 56 const char* origin_url; | |
| 57 quota::StorageType type; | |
| 58 }; | |
| 59 | |
| 60 protected: | |
| 61 FileSystemQuotaClient* NewQuotaClient(bool is_incognito) { | |
| 62 return new FileSystemQuotaClient( | |
| 63 file_system_context_, is_incognito); | |
| 64 } | |
| 65 | |
| 66 void GetOriginUsageAsync(FileSystemQuotaClient* quota_client, | |
| 67 const std::string& origin_url, | |
| 68 quota::StorageType type) { | |
| 69 quota_client->GetOriginUsage( | |
| 70 GURL(origin_url), type, | |
| 71 base::Bind(&FileSystemQuotaClientTest::OnGetUsage, | |
| 72 weak_factory_.GetWeakPtr())); | |
| 73 } | |
| 74 | |
| 75 int64 GetOriginUsage(FileSystemQuotaClient* quota_client, | |
| 76 const std::string& origin_url, | |
| 77 quota::StorageType type) { | |
| 78 GetOriginUsageAsync(quota_client, origin_url, type); | |
| 79 base::MessageLoop::current()->RunUntilIdle(); | |
| 80 return usage_; | |
| 81 } | |
| 82 | |
| 83 const std::set<GURL>& GetOriginsForType(FileSystemQuotaClient* quota_client, | |
| 84 quota::StorageType type) { | |
| 85 origins_.clear(); | |
| 86 quota_client->GetOriginsForType( | |
| 87 type, | |
| 88 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, | |
| 89 weak_factory_.GetWeakPtr())); | |
| 90 base::MessageLoop::current()->RunUntilIdle(); | |
| 91 return origins_; | |
| 92 } | |
| 93 | |
| 94 const std::set<GURL>& GetOriginsForHost(FileSystemQuotaClient* quota_client, | |
| 95 quota::StorageType type, | |
| 96 const std::string& host) { | |
| 97 origins_.clear(); | |
| 98 quota_client->GetOriginsForHost( | |
| 99 type, host, | |
| 100 base::Bind(&FileSystemQuotaClientTest::OnGetOrigins, | |
| 101 weak_factory_.GetWeakPtr())); | |
| 102 base::MessageLoop::current()->RunUntilIdle(); | |
| 103 return origins_; | |
| 104 } | |
| 105 | |
| 106 void RunAdditionalOriginUsageTask(FileSystemQuotaClient* quota_client, | |
| 107 const std::string& origin_url, | |
| 108 quota::StorageType type) { | |
| 109 quota_client->GetOriginUsage( | |
| 110 GURL(origin_url), type, | |
| 111 base::Bind(&FileSystemQuotaClientTest::OnGetAdditionalUsage, | |
| 112 weak_factory_.GetWeakPtr())); | |
| 113 } | |
| 114 | |
| 115 FileSystemOperationContext* CreateFileSystemOperationContext( | |
| 116 FileSystemType type) { | |
| 117 FileSystemOperationContext* context = | |
| 118 new FileSystemOperationContext(file_system_context_); | |
| 119 context->set_allowed_bytes_growth(100000000); | |
| 120 context->set_update_observers( | |
| 121 *file_system_context_->GetUpdateObservers(type)); | |
| 122 return context; | |
| 123 } | |
| 124 | |
| 125 bool CreateFileSystemDirectory(const base::FilePath& file_path, | |
| 126 const std::string& origin_url, | |
| 127 quota::StorageType storage_type) { | |
| 128 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
| 129 FileSystemFileUtil* file_util = file_system_context_->GetFileUtil(type); | |
| 130 | |
| 131 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | |
| 132 GURL(origin_url), type, file_path); | |
| 133 scoped_ptr<FileSystemOperationContext> context( | |
| 134 CreateFileSystemOperationContext(type)); | |
| 135 | |
| 136 base::PlatformFileError result = | |
| 137 file_util->CreateDirectory(context.get(), url, false, false); | |
| 138 if (result != base::PLATFORM_FILE_OK) | |
| 139 return false; | |
| 140 return true; | |
| 141 } | |
| 142 | |
| 143 bool CreateFileSystemFile(const base::FilePath& file_path, | |
| 144 int64 file_size, | |
| 145 const std::string& origin_url, | |
| 146 quota::StorageType storage_type) { | |
| 147 if (file_path.empty()) | |
| 148 return false; | |
| 149 | |
| 150 FileSystemType type = QuotaStorageTypeToFileSystemType(storage_type); | |
| 151 FileSystemFileUtil* file_util = file_system_context_-> | |
| 152 sandbox_provider()->GetFileUtil(type); | |
| 153 | |
| 154 FileSystemURL url = file_system_context_->CreateCrackedFileSystemURL( | |
| 155 GURL(origin_url), type, file_path); | |
| 156 scoped_ptr<FileSystemOperationContext> context( | |
| 157 CreateFileSystemOperationContext(type)); | |
| 158 | |
| 159 bool created = false; | |
| 160 if (base::PLATFORM_FILE_OK != | |
| 161 file_util->EnsureFileExists(context.get(), url, &created)) | |
| 162 return false; | |
| 163 EXPECT_TRUE(created); | |
| 164 if (base::PLATFORM_FILE_OK != | |
| 165 file_util->Truncate(context.get(), url, file_size)) | |
| 166 return false; | |
| 167 return true; | |
| 168 } | |
| 169 | |
| 170 void InitializeOriginFiles(FileSystemQuotaClient* quota_client, | |
| 171 const TestFile* files, | |
| 172 int num_files) { | |
| 173 for (int i = 0; i < num_files; i++) { | |
| 174 base::FilePath path = base::FilePath().AppendASCII(files[i].name); | |
| 175 if (files[i].isDirectory) { | |
| 176 ASSERT_TRUE(CreateFileSystemDirectory( | |
| 177 path, files[i].origin_url, files[i].type)); | |
| 178 if (path.empty()) { | |
| 179 // Create the usage cache. | |
| 180 // HACK--we always create the root [an empty path] first. If we | |
| 181 // create it later, this will fail due to a quota mismatch. If we | |
| 182 // call this before we create the root, it succeeds, but hasn't | |
| 183 // actually created the cache. | |
| 184 ASSERT_EQ(0, GetOriginUsage( | |
| 185 quota_client, files[i].origin_url, files[i].type)); | |
| 186 } | |
| 187 } else { | |
| 188 ASSERT_TRUE(CreateFileSystemFile( | |
| 189 path, files[i].size, files[i].origin_url, files[i].type)); | |
| 190 } | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 // This is a bit fragile--it depends on the test data always creating a | |
| 195 // directory before adding a file or directory to it, so that we can just | |
| 196 // count the basename of each addition. A recursive creation of a path, which | |
| 197 // created more than one directory in a single shot, would break this. | |
| 198 int64 ComputeFilePathsCostForOriginAndType(const TestFile* files, | |
| 199 int num_files, | |
| 200 const std::string& origin_url, | |
| 201 quota::StorageType type) { | |
| 202 int64 file_paths_cost = 0; | |
| 203 for (int i = 0; i < num_files; i++) { | |
| 204 if (files[i].type == type && | |
| 205 GURL(files[i].origin_url) == GURL(origin_url)) { | |
| 206 base::FilePath path = base::FilePath().AppendASCII(files[i].name); | |
| 207 if (!path.empty()) { | |
| 208 file_paths_cost += ObfuscatedFileUtil::ComputeFilePathCost(path); | |
| 209 } | |
| 210 } | |
| 211 } | |
| 212 return file_paths_cost; | |
| 213 } | |
| 214 | |
| 215 void DeleteOriginData(FileSystemQuotaClient* quota_client, | |
| 216 const std::string& origin, | |
| 217 quota::StorageType type) { | |
| 218 deletion_status_ = quota::kQuotaStatusUnknown; | |
| 219 quota_client->DeleteOriginData( | |
| 220 GURL(origin), type, | |
| 221 base::Bind(&FileSystemQuotaClientTest::OnDeleteOrigin, | |
| 222 weak_factory_.GetWeakPtr())); | |
| 223 } | |
| 224 | |
| 225 int64 usage() const { return usage_; } | |
| 226 quota::QuotaStatusCode status() { return deletion_status_; } | |
| 227 int additional_callback_count() const { return additional_callback_count_; } | |
| 228 void set_additional_callback_count(int count) { | |
| 229 additional_callback_count_ = count; | |
| 230 } | |
| 231 | |
| 232 private: | |
| 233 void OnGetUsage(int64 usage) { | |
| 234 usage_ = usage; | |
| 235 } | |
| 236 | |
| 237 void OnGetOrigins(const std::set<GURL>& origins, | |
| 238 quota::StorageType type) { | |
| 239 origins_ = origins; | |
| 240 type_ = type; | |
| 241 } | |
| 242 | |
| 243 void OnGetAdditionalUsage(int64 usage_unused) { | |
| 244 ++additional_callback_count_; | |
| 245 } | |
| 246 | |
| 247 void OnDeleteOrigin(quota::QuotaStatusCode status) { | |
| 248 deletion_status_ = status; | |
| 249 } | |
| 250 | |
| 251 base::ScopedTempDir data_dir_; | |
| 252 base::MessageLoop message_loop_; | |
| 253 scoped_refptr<FileSystemContext> file_system_context_; | |
| 254 base::WeakPtrFactory<FileSystemQuotaClientTest> weak_factory_; | |
| 255 int64 usage_; | |
| 256 int additional_callback_count_; | |
| 257 std::set<GURL> origins_; | |
| 258 quota::StorageType type_; | |
| 259 quota::QuotaStatusCode deletion_status_; | |
| 260 | |
| 261 DISALLOW_COPY_AND_ASSIGN(FileSystemQuotaClientTest); | |
| 262 }; | |
| 263 | |
| 264 TEST_F(FileSystemQuotaClientTest, NoFileSystemTest) { | |
| 265 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 266 | |
| 267 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 268 } | |
| 269 | |
| 270 TEST_F(FileSystemQuotaClientTest, NoFileTest) { | |
| 271 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 272 const TestFile kFiles[] = { | |
| 273 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 274 }; | |
| 275 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 276 | |
| 277 for (int i = 0; i < 2; i++) { | |
| 278 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 TEST_F(FileSystemQuotaClientTest, OneFileTest) { | |
| 283 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 284 const TestFile kFiles[] = { | |
| 285 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 286 {false, "foo", 4921, kDummyURL1, kTemporary}, | |
| 287 }; | |
| 288 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 289 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType( | |
| 290 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 291 | |
| 292 for (int i = 0; i < 2; i++) { | |
| 293 EXPECT_EQ(4921 + file_paths_cost, | |
| 294 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 TEST_F(FileSystemQuotaClientTest, TwoFilesTest) { | |
| 299 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 300 const TestFile kFiles[] = { | |
| 301 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 302 {false, "foo", 10310, kDummyURL1, kTemporary}, | |
| 303 {false, "bar", 41, kDummyURL1, kTemporary}, | |
| 304 }; | |
| 305 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 306 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType( | |
| 307 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 308 | |
| 309 for (int i = 0; i < 2; i++) { | |
| 310 EXPECT_EQ(10310 + 41 + file_paths_cost, | |
| 311 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 312 } | |
| 313 } | |
| 314 | |
| 315 TEST_F(FileSystemQuotaClientTest, EmptyFilesTest) { | |
| 316 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 317 const TestFile kFiles[] = { | |
| 318 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 319 {false, "foo", 0, kDummyURL1, kTemporary}, | |
| 320 {false, "bar", 0, kDummyURL1, kTemporary}, | |
| 321 {false, "baz", 0, kDummyURL1, kTemporary}, | |
| 322 }; | |
| 323 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 324 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType( | |
| 325 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 326 | |
| 327 for (int i = 0; i < 2; i++) { | |
| 328 EXPECT_EQ(file_paths_cost, | |
| 329 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 330 } | |
| 331 } | |
| 332 | |
| 333 TEST_F(FileSystemQuotaClientTest, SubDirectoryTest) { | |
| 334 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 335 const TestFile kFiles[] = { | |
| 336 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 337 {true, "dirtest", 0, kDummyURL1, kTemporary}, | |
| 338 {false, "dirtest/foo", 11921, kDummyURL1, kTemporary}, | |
| 339 {false, "bar", 4814, kDummyURL1, kTemporary}, | |
| 340 }; | |
| 341 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 342 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType( | |
| 343 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 344 | |
| 345 for (int i = 0; i < 2; i++) { | |
| 346 EXPECT_EQ(11921 + 4814 + file_paths_cost, | |
| 347 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 348 } | |
| 349 } | |
| 350 | |
| 351 TEST_F(FileSystemQuotaClientTest, MultiTypeTest) { | |
| 352 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 353 const TestFile kFiles[] = { | |
| 354 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 355 {true, "dirtest", 0, kDummyURL1, kTemporary}, | |
| 356 {false, "dirtest/foo", 133, kDummyURL1, kTemporary}, | |
| 357 {false, "bar", 14, kDummyURL1, kTemporary}, | |
| 358 {true, NULL, 0, kDummyURL1, kPersistent}, | |
| 359 {true, "dirtest", 0, kDummyURL1, kPersistent}, | |
| 360 {false, "dirtest/foo", 193, kDummyURL1, kPersistent}, | |
| 361 {false, "bar", 9, kDummyURL1, kPersistent}, | |
| 362 }; | |
| 363 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 364 const int64 file_paths_cost_temporary = ComputeFilePathsCostForOriginAndType( | |
| 365 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 366 const int64 file_paths_cost_persistent = ComputeFilePathsCostForOriginAndType( | |
| 367 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 368 | |
| 369 for (int i = 0; i < 2; i++) { | |
| 370 EXPECT_EQ(133 + 14 + file_paths_cost_temporary, | |
| 371 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 372 EXPECT_EQ(193 + 9 + file_paths_cost_persistent, | |
| 373 GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent)); | |
| 374 } | |
| 375 } | |
| 376 | |
| 377 TEST_F(FileSystemQuotaClientTest, MultiDomainTest) { | |
| 378 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 379 const TestFile kFiles[] = { | |
| 380 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 381 {true, "dir1", 0, kDummyURL1, kTemporary}, | |
| 382 {false, "dir1/foo", 1331, kDummyURL1, kTemporary}, | |
| 383 {false, "bar", 134, kDummyURL1, kTemporary}, | |
| 384 {true, NULL, 0, kDummyURL1, kPersistent}, | |
| 385 {true, "dir2", 0, kDummyURL1, kPersistent}, | |
| 386 {false, "dir2/foo", 1903, kDummyURL1, kPersistent}, | |
| 387 {false, "bar", 19, kDummyURL1, kPersistent}, | |
| 388 {true, NULL, 0, kDummyURL2, kTemporary}, | |
| 389 {true, "dom", 0, kDummyURL2, kTemporary}, | |
| 390 {false, "dom/fan", 1319, kDummyURL2, kTemporary}, | |
| 391 {false, "bar", 113, kDummyURL2, kTemporary}, | |
| 392 {true, NULL, 0, kDummyURL2, kPersistent}, | |
| 393 {true, "dom", 0, kDummyURL2, kPersistent}, | |
| 394 {false, "dom/fan", 2013, kDummyURL2, kPersistent}, | |
| 395 {false, "baz", 18, kDummyURL2, kPersistent}, | |
| 396 }; | |
| 397 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 398 const int64 file_paths_cost_temporary1 = ComputeFilePathsCostForOriginAndType( | |
| 399 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 400 const int64 file_paths_cost_persistent1 = | |
| 401 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 402 kDummyURL1, kPersistent); | |
| 403 const int64 file_paths_cost_temporary2 = ComputeFilePathsCostForOriginAndType( | |
| 404 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL2, kTemporary); | |
| 405 const int64 file_paths_cost_persistent2 = | |
| 406 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 407 kDummyURL2, kPersistent); | |
| 408 | |
| 409 for (int i = 0; i < 2; i++) { | |
| 410 EXPECT_EQ(1331 + 134 + file_paths_cost_temporary1, | |
| 411 GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 412 EXPECT_EQ(1903 + 19 + file_paths_cost_persistent1, | |
| 413 GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent)); | |
| 414 EXPECT_EQ(1319 + 113 + file_paths_cost_temporary2, | |
| 415 GetOriginUsage(quota_client.get(), kDummyURL2, kTemporary)); | |
| 416 EXPECT_EQ(2013 + 18 + file_paths_cost_persistent2, | |
| 417 GetOriginUsage(quota_client.get(), kDummyURL2, kPersistent)); | |
| 418 } | |
| 419 } | |
| 420 | |
| 421 TEST_F(FileSystemQuotaClientTest, GetUsage_MultipleTasks) { | |
| 422 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 423 const TestFile kFiles[] = { | |
| 424 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 425 {false, "foo", 11, kDummyURL1, kTemporary}, | |
| 426 {false, "bar", 22, kDummyURL1, kTemporary}, | |
| 427 }; | |
| 428 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 429 const int64 file_paths_cost = ComputeFilePathsCostForOriginAndType( | |
| 430 kFiles, ARRAYSIZE_UNSAFE(kFiles), kDummyURL1, kTemporary); | |
| 431 | |
| 432 // Dispatching three GetUsage tasks. | |
| 433 set_additional_callback_count(0); | |
| 434 GetOriginUsageAsync(quota_client.get(), kDummyURL1, kTemporary); | |
| 435 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary); | |
| 436 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary); | |
| 437 base::MessageLoop::current()->RunUntilIdle(); | |
| 438 EXPECT_EQ(11 + 22 + file_paths_cost, usage()); | |
| 439 EXPECT_EQ(2, additional_callback_count()); | |
| 440 | |
| 441 // Once more, in a different order. | |
| 442 set_additional_callback_count(0); | |
| 443 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary); | |
| 444 GetOriginUsageAsync(quota_client.get(), kDummyURL1, kTemporary); | |
| 445 RunAdditionalOriginUsageTask(quota_client.get(), kDummyURL1, kTemporary); | |
| 446 base::MessageLoop::current()->RunUntilIdle(); | |
| 447 EXPECT_EQ(11 + 22 + file_paths_cost, usage()); | |
| 448 EXPECT_EQ(2, additional_callback_count()); | |
| 449 } | |
| 450 | |
| 451 TEST_F(FileSystemQuotaClientTest, GetOriginsForType) { | |
| 452 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 453 const TestFile kFiles[] = { | |
| 454 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 455 {true, NULL, 0, kDummyURL2, kTemporary}, | |
| 456 {true, NULL, 0, kDummyURL3, kPersistent}, | |
| 457 }; | |
| 458 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 459 | |
| 460 std::set<GURL> origins = GetOriginsForType(quota_client.get(), kTemporary); | |
| 461 EXPECT_EQ(2U, origins.size()); | |
| 462 EXPECT_TRUE(origins.find(GURL(kDummyURL1)) != origins.end()); | |
| 463 EXPECT_TRUE(origins.find(GURL(kDummyURL2)) != origins.end()); | |
| 464 EXPECT_TRUE(origins.find(GURL(kDummyURL3)) == origins.end()); | |
| 465 } | |
| 466 | |
| 467 TEST_F(FileSystemQuotaClientTest, GetOriginsForHost) { | |
| 468 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 469 const char* kURL1 = "http://foo.com/"; | |
| 470 const char* kURL2 = "https://foo.com/"; | |
| 471 const char* kURL3 = "http://foo.com:1/"; | |
| 472 const char* kURL4 = "http://foo2.com/"; | |
| 473 const char* kURL5 = "http://foo.com:2/"; | |
| 474 const TestFile kFiles[] = { | |
| 475 {true, NULL, 0, kURL1, kTemporary}, | |
| 476 {true, NULL, 0, kURL2, kTemporary}, | |
| 477 {true, NULL, 0, kURL3, kTemporary}, | |
| 478 {true, NULL, 0, kURL4, kTemporary}, | |
| 479 {true, NULL, 0, kURL5, kPersistent}, | |
| 480 }; | |
| 481 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 482 | |
| 483 std::set<GURL> origins = GetOriginsForHost( | |
| 484 quota_client.get(), kTemporary, "foo.com"); | |
| 485 EXPECT_EQ(3U, origins.size()); | |
| 486 EXPECT_TRUE(origins.find(GURL(kURL1)) != origins.end()); | |
| 487 EXPECT_TRUE(origins.find(GURL(kURL2)) != origins.end()); | |
| 488 EXPECT_TRUE(origins.find(GURL(kURL3)) != origins.end()); | |
| 489 EXPECT_TRUE(origins.find(GURL(kURL4)) == origins.end()); // Different host. | |
| 490 EXPECT_TRUE(origins.find(GURL(kURL5)) == origins.end()); // Different type. | |
| 491 } | |
| 492 | |
| 493 TEST_F(FileSystemQuotaClientTest, IncognitoTest) { | |
| 494 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(true)); | |
| 495 const TestFile kFiles[] = { | |
| 496 {true, NULL, 0, kDummyURL1, kTemporary}, | |
| 497 {false, "foo", 10, kDummyURL1, kTemporary}, | |
| 498 }; | |
| 499 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 500 | |
| 501 // Having files in the usual directory wouldn't affect the result | |
| 502 // queried in incognito mode. | |
| 503 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kTemporary)); | |
| 504 EXPECT_EQ(0, GetOriginUsage(quota_client.get(), kDummyURL1, kPersistent)); | |
| 505 | |
| 506 std::set<GURL> origins = GetOriginsForType(quota_client.get(), kTemporary); | |
| 507 EXPECT_EQ(0U, origins.size()); | |
| 508 origins = GetOriginsForHost(quota_client.get(), kTemporary, "www.dummy.org"); | |
| 509 EXPECT_EQ(0U, origins.size()); | |
| 510 } | |
| 511 | |
| 512 TEST_F(FileSystemQuotaClientTest, DeleteOriginTest) { | |
| 513 scoped_ptr<FileSystemQuotaClient> quota_client(NewQuotaClient(false)); | |
| 514 const TestFile kFiles[] = { | |
| 515 {true, NULL, 0, "http://foo.com/", kTemporary}, | |
| 516 {false, "a", 1, "http://foo.com/", kTemporary}, | |
| 517 {true, NULL, 0, "https://foo.com/", kTemporary}, | |
| 518 {false, "b", 2, "https://foo.com/", kTemporary}, | |
| 519 {true, NULL, 0, "http://foo.com/", kPersistent}, | |
| 520 {false, "c", 4, "http://foo.com/", kPersistent}, | |
| 521 {true, NULL, 0, "http://bar.com/", kTemporary}, | |
| 522 {false, "d", 8, "http://bar.com/", kTemporary}, | |
| 523 {true, NULL, 0, "http://bar.com/", kPersistent}, | |
| 524 {false, "e", 16, "http://bar.com/", kPersistent}, | |
| 525 {true, NULL, 0, "https://bar.com/", kPersistent}, | |
| 526 {false, "f", 32, "https://bar.com/", kPersistent}, | |
| 527 {true, NULL, 0, "https://bar.com/", kTemporary}, | |
| 528 {false, "g", 64, "https://bar.com/", kTemporary}, | |
| 529 }; | |
| 530 InitializeOriginFiles(quota_client.get(), kFiles, ARRAYSIZE_UNSAFE(kFiles)); | |
| 531 const int64 file_paths_cost_temporary_foo_https = | |
| 532 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 533 "https://foo.com/", kTemporary); | |
| 534 const int64 file_paths_cost_persistent_foo = | |
| 535 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 536 "http://foo.com/", kPersistent); | |
| 537 const int64 file_paths_cost_temporary_bar = | |
| 538 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 539 "http://bar.com/", kTemporary); | |
| 540 const int64 file_paths_cost_temporary_bar_https = | |
| 541 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 542 "https://bar.com/", kTemporary); | |
| 543 const int64 file_paths_cost_persistent_bar_https = | |
| 544 ComputeFilePathsCostForOriginAndType(kFiles, ARRAYSIZE_UNSAFE(kFiles), | |
| 545 "https://bar.com/", kPersistent); | |
| 546 | |
| 547 DeleteOriginData(quota_client.get(), "http://foo.com/", kTemporary); | |
| 548 base::MessageLoop::current()->RunUntilIdle(); | |
| 549 EXPECT_EQ(quota::kQuotaStatusOk, status()); | |
| 550 | |
| 551 DeleteOriginData(quota_client.get(), "http://bar.com/", kPersistent); | |
| 552 base::MessageLoop::current()->RunUntilIdle(); | |
| 553 EXPECT_EQ(quota::kQuotaStatusOk, status()); | |
| 554 | |
| 555 DeleteOriginData(quota_client.get(), "http://buz.com/", kTemporary); | |
| 556 base::MessageLoop::current()->RunUntilIdle(); | |
| 557 EXPECT_EQ(quota::kQuotaStatusOk, status()); | |
| 558 | |
| 559 EXPECT_EQ(0, GetOriginUsage( | |
| 560 quota_client.get(), "http://foo.com/", kTemporary)); | |
| 561 EXPECT_EQ(0, GetOriginUsage( | |
| 562 quota_client.get(), "http://bar.com/", kPersistent)); | |
| 563 EXPECT_EQ(0, GetOriginUsage( | |
| 564 quota_client.get(), "http://buz.com/", kTemporary)); | |
| 565 | |
| 566 EXPECT_EQ(2 + file_paths_cost_temporary_foo_https, | |
| 567 GetOriginUsage(quota_client.get(), | |
| 568 "https://foo.com/", | |
| 569 kTemporary)); | |
| 570 EXPECT_EQ(4 + file_paths_cost_persistent_foo, | |
| 571 GetOriginUsage(quota_client.get(), | |
| 572 "http://foo.com/", | |
| 573 kPersistent)); | |
| 574 EXPECT_EQ(8 + file_paths_cost_temporary_bar, | |
| 575 GetOriginUsage(quota_client.get(), | |
| 576 "http://bar.com/", | |
| 577 kTemporary)); | |
| 578 EXPECT_EQ(32 + file_paths_cost_persistent_bar_https, | |
| 579 GetOriginUsage(quota_client.get(), | |
| 580 "https://bar.com/", | |
| 581 kPersistent)); | |
| 582 EXPECT_EQ(64 + file_paths_cost_temporary_bar_https, | |
| 583 GetOriginUsage(quota_client.get(), | |
| 584 "https://bar.com/", | |
| 585 kTemporary)); | |
| 586 } | |
| 587 | |
| 588 } // namespace fileapi | |
| OLD | NEW |