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