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