Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / quota / quota_manager_unittest.cc
blob39c2315689fa99b2f26c2276e7cbf13fba7730e8
1 // Copyright 2014 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.
5 #include <algorithm>
6 #include <set>
7 #include <sstream>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/files/file_util.h"
12 #include "base/files/scoped_temp_dir.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/run_loop.h"
16 #include "base/stl_util.h"
17 #include "base/sys_info.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "base/time/time.h"
20 #include "content/public/test/mock_special_storage_policy.h"
21 #include "content/public/test/mock_storage_client.h"
22 #include "storage/browser/quota/quota_database.h"
23 #include "storage/browser/quota/quota_manager.h"
24 #include "storage/browser/quota/quota_manager_proxy.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "url/gurl.h"
28 using storage::kQuotaErrorAbort;
29 using storage::kQuotaErrorInvalidModification;
30 using storage::kQuotaErrorNotSupported;
31 using storage::kQuotaStatusOk;
32 using storage::kQuotaStatusUnknown;
33 using storage::kStorageTypePersistent;
34 using storage::kStorageTypeSyncable;
35 using storage::kStorageTypeTemporary;
36 using storage::kStorageTypeUnknown;
37 using storage::QuotaClient;
38 using storage::QuotaManager;
39 using storage::QuotaStatusCode;
40 using storage::StorageType;
41 using storage::UsageAndQuota;
42 using storage::UsageInfo;
43 using storage::UsageInfoEntries;
45 namespace content {
47 namespace {
49 // For shorter names.
50 const StorageType kTemp = kStorageTypeTemporary;
51 const StorageType kPerm = kStorageTypePersistent;
52 const StorageType kSync = kStorageTypeSyncable;
54 const int kAllClients = QuotaClient::kAllClientsMask;
56 const int64 kAvailableSpaceForApp = 13377331U;
58 const int64 kMinimumPreserveForSystem = QuotaManager::kMinimumPreserveForSystem;
59 const int kPerHostTemporaryPortion = QuotaManager::kPerHostTemporaryPortion;
61 // Returns a deterministic value for the amount of available disk space.
62 int64 GetAvailableDiskSpaceForTest(const base::FilePath&) {
63 return kAvailableSpaceForApp + kMinimumPreserveForSystem;
66 } // namespace
68 class QuotaManagerTest : public testing::Test {
69 protected:
70 typedef QuotaManager::QuotaTableEntry QuotaTableEntry;
71 typedef QuotaManager::QuotaTableEntries QuotaTableEntries;
72 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries;
74 public:
75 QuotaManagerTest()
76 : mock_time_counter_(0),
77 weak_factory_(this) {
80 void SetUp() override {
81 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
82 mock_special_storage_policy_ = new MockSpecialStoragePolicy;
83 ResetQuotaManager(false /* is_incognito */);
86 void TearDown() override {
87 // Make sure the quota manager cleans up correctly.
88 quota_manager_ = NULL;
89 base::RunLoop().RunUntilIdle();
92 protected:
93 void ResetQuotaManager(bool is_incognito) {
94 quota_manager_ = new QuotaManager(is_incognito, data_dir_.path(),
95 base::ThreadTaskRunnerHandle::Get().get(),
96 base::ThreadTaskRunnerHandle::Get().get(),
97 mock_special_storage_policy_.get());
98 // Don't (automatically) start the eviction for testing.
99 quota_manager_->eviction_disabled_ = true;
100 // Don't query the hard disk for remaining capacity.
101 quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest;
102 additional_callback_count_ = 0;
105 MockStorageClient* CreateClient(
106 const MockOriginData* mock_data,
107 size_t mock_data_size,
108 QuotaClient::ID id) {
109 return new MockStorageClient(quota_manager_->proxy(),
110 mock_data, id, mock_data_size);
113 void RegisterClient(MockStorageClient* client) {
114 quota_manager_->proxy()->RegisterClient(client);
117 void GetUsageInfo() {
118 usage_info_.clear();
119 quota_manager_->GetUsageInfo(
120 base::Bind(&QuotaManagerTest::DidGetUsageInfo,
121 weak_factory_.GetWeakPtr()));
124 void GetUsageAndQuotaForWebApps(const GURL& origin,
125 StorageType type) {
126 quota_status_ = kQuotaStatusUnknown;
127 usage_ = -1;
128 quota_ = -1;
129 quota_manager_->GetUsageAndQuotaForWebApps(
130 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota,
131 weak_factory_.GetWeakPtr()));
134 void GetUsageAndQuotaForStorageClient(const GURL& origin,
135 StorageType type) {
136 quota_status_ = kQuotaStatusUnknown;
137 usage_ = -1;
138 quota_ = -1;
139 quota_manager_->GetUsageAndQuota(
140 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota,
141 weak_factory_.GetWeakPtr()));
144 void GetTemporaryGlobalQuota() {
145 quota_status_ = kQuotaStatusUnknown;
146 quota_ = -1;
147 quota_manager_->GetTemporaryGlobalQuota(
148 base::Bind(&QuotaManagerTest::DidGetQuota,
149 weak_factory_.GetWeakPtr()));
152 void SetTemporaryGlobalQuota(int64 new_quota) {
153 quota_status_ = kQuotaStatusUnknown;
154 quota_ = -1;
155 quota_manager_->SetTemporaryGlobalOverrideQuota(
156 new_quota,
157 base::Bind(&QuotaManagerTest::DidGetQuota,
158 weak_factory_.GetWeakPtr()));
161 void GetPersistentHostQuota(const std::string& host) {
162 quota_status_ = kQuotaStatusUnknown;
163 quota_ = -1;
164 quota_manager_->GetPersistentHostQuota(
165 host,
166 base::Bind(&QuotaManagerTest::DidGetHostQuota,
167 weak_factory_.GetWeakPtr()));
170 void SetPersistentHostQuota(const std::string& host, int64 new_quota) {
171 quota_status_ = kQuotaStatusUnknown;
172 quota_ = -1;
173 quota_manager_->SetPersistentHostQuota(
174 host, new_quota,
175 base::Bind(&QuotaManagerTest::DidGetHostQuota,
176 weak_factory_.GetWeakPtr()));
179 void GetGlobalUsage(StorageType type) {
180 usage_ = -1;
181 unlimited_usage_ = -1;
182 quota_manager_->GetGlobalUsage(
183 type,
184 base::Bind(&QuotaManagerTest::DidGetGlobalUsage,
185 weak_factory_.GetWeakPtr()));
188 void GetHostUsage(const std::string& host, StorageType type) {
189 usage_ = -1;
190 quota_manager_->GetHostUsage(
191 host, type,
192 base::Bind(&QuotaManagerTest::DidGetHostUsage,
193 weak_factory_.GetWeakPtr()));
196 void RunAdditionalUsageAndQuotaTask(const GURL& origin, StorageType type) {
197 quota_manager_->GetUsageAndQuota(
198 origin, type,
199 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaAdditional,
200 weak_factory_.GetWeakPtr()));
203 void DeleteClientOriginData(QuotaClient* client,
204 const GURL& origin,
205 StorageType type) {
206 DCHECK(client);
207 quota_status_ = kQuotaStatusUnknown;
208 client->DeleteOriginData(
209 origin, type,
210 base::Bind(&QuotaManagerTest::StatusCallback,
211 weak_factory_.GetWeakPtr()));
214 void EvictOriginData(const GURL& origin,
215 StorageType type) {
216 quota_status_ = kQuotaStatusUnknown;
217 quota_manager_->EvictOriginData(
218 origin, type,
219 base::Bind(&QuotaManagerTest::StatusCallback,
220 weak_factory_.GetWeakPtr()));
223 void DeleteOriginData(const GURL& origin,
224 StorageType type,
225 int quota_client_mask) {
226 quota_status_ = kQuotaStatusUnknown;
227 quota_manager_->DeleteOriginData(
228 origin, type, quota_client_mask,
229 base::Bind(&QuotaManagerTest::StatusCallback,
230 weak_factory_.GetWeakPtr()));
233 void DeleteHostData(const std::string& host,
234 StorageType type,
235 int quota_client_mask) {
236 quota_status_ = kQuotaStatusUnknown;
237 quota_manager_->DeleteHostData(
238 host, type, quota_client_mask,
239 base::Bind(&QuotaManagerTest::StatusCallback,
240 weak_factory_.GetWeakPtr()));
243 void GetAvailableSpace() {
244 quota_status_ = kQuotaStatusUnknown;
245 available_space_ = -1;
246 quota_manager_->GetAvailableSpace(
247 base::Bind(&QuotaManagerTest::DidGetAvailableSpace,
248 weak_factory_.GetWeakPtr()));
251 void GetUsageAndQuotaForEviction() {
252 quota_status_ = kQuotaStatusUnknown;
253 usage_ = -1;
254 unlimited_usage_ = -1;
255 quota_ = -1;
256 available_space_ = -1;
257 quota_manager_->GetUsageAndQuotaForEviction(
258 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaForEviction,
259 weak_factory_.GetWeakPtr()));
262 void GetCachedOrigins(StorageType type, std::set<GURL>* origins) {
263 ASSERT_TRUE(origins != NULL);
264 origins->clear();
265 quota_manager_->GetCachedOrigins(type, origins);
268 void NotifyStorageAccessed(QuotaClient* client,
269 const GURL& origin,
270 StorageType type) {
271 DCHECK(client);
272 quota_manager_->NotifyStorageAccessedInternal(
273 client->id(), origin, type, IncrementMockTime());
276 void DeleteOriginFromDatabase(const GURL& origin, StorageType type) {
277 quota_manager_->DeleteOriginFromDatabase(origin, type);
280 void GetLRUOrigin(StorageType type) {
281 lru_origin_ = GURL();
282 quota_manager_->GetLRUOrigin(
283 type,
284 base::Bind(&QuotaManagerTest::DidGetLRUOrigin,
285 weak_factory_.GetWeakPtr()));
288 void NotifyOriginInUse(const GURL& origin) {
289 quota_manager_->NotifyOriginInUse(origin);
292 void NotifyOriginNoLongerInUse(const GURL& origin) {
293 quota_manager_->NotifyOriginNoLongerInUse(origin);
296 void GetOriginsModifiedSince(StorageType type, base::Time modified_since) {
297 modified_origins_.clear();
298 modified_origins_type_ = kStorageTypeUnknown;
299 quota_manager_->GetOriginsModifiedSince(
300 type, modified_since,
301 base::Bind(&QuotaManagerTest::DidGetModifiedOrigins,
302 weak_factory_.GetWeakPtr()));
305 void DumpQuotaTable() {
306 quota_entries_.clear();
307 quota_manager_->DumpQuotaTable(
308 base::Bind(&QuotaManagerTest::DidDumpQuotaTable,
309 weak_factory_.GetWeakPtr()));
312 void DumpOriginInfoTable() {
313 origin_info_entries_.clear();
314 quota_manager_->DumpOriginInfoTable(
315 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable,
316 weak_factory_.GetWeakPtr()));
319 void DidGetUsageInfo(const UsageInfoEntries& entries) {
320 usage_info_.insert(usage_info_.begin(), entries.begin(), entries.end());
323 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) {
324 quota_status_ = status;
325 usage_ = usage;
326 quota_ = quota;
329 void DidGetQuota(QuotaStatusCode status,
330 int64 quota) {
331 quota_status_ = status;
332 quota_ = quota;
335 void DidGetAvailableSpace(QuotaStatusCode status, int64 available_space) {
336 quota_status_ = status;
337 available_space_ = available_space;
340 void DidGetHostQuota(QuotaStatusCode status,
341 int64 quota) {
342 quota_status_ = status;
343 quota_ = quota;
346 void DidGetGlobalUsage(int64 usage,
347 int64 unlimited_usage) {
348 usage_ = usage;
349 unlimited_usage_ = unlimited_usage;
352 void DidGetHostUsage(int64 usage) {
353 usage_ = usage;
356 void StatusCallback(QuotaStatusCode status) {
357 ++status_callback_count_;
358 quota_status_ = status;
361 void DidGetUsageAndQuotaForEviction(QuotaStatusCode status,
362 const UsageAndQuota& usage_and_quota) {
363 quota_status_ = status;
364 limited_usage_ = usage_and_quota.global_limited_usage;
365 quota_ = usage_and_quota.quota;
366 available_space_ = usage_and_quota.available_disk_space;
369 void DidGetLRUOrigin(const GURL& origin) {
370 lru_origin_ = origin;
373 void DidGetModifiedOrigins(const std::set<GURL>& origins, StorageType type) {
374 modified_origins_ = origins;
375 modified_origins_type_ = type;
378 void DidDumpQuotaTable(const QuotaTableEntries& entries) {
379 quota_entries_ = entries;
382 void DidDumpOriginInfoTable(const OriginInfoTableEntries& entries) {
383 origin_info_entries_ = entries;
386 void GetUsage_WithModifyTestBody(const StorageType type);
388 void set_additional_callback_count(int c) { additional_callback_count_ = c; }
389 int additional_callback_count() const { return additional_callback_count_; }
390 void DidGetUsageAndQuotaAdditional(
391 QuotaStatusCode status, int64 usage, int64 quota) {
392 ++additional_callback_count_;
395 QuotaManager* quota_manager() const { return quota_manager_.get(); }
396 void set_quota_manager(QuotaManager* quota_manager) {
397 quota_manager_ = quota_manager;
400 MockSpecialStoragePolicy* mock_special_storage_policy() const {
401 return mock_special_storage_policy_.get();
404 QuotaStatusCode status() const { return quota_status_; }
405 const UsageInfoEntries& usage_info() const { return usage_info_; }
406 int64 usage() const { return usage_; }
407 int64 limited_usage() const { return limited_usage_; }
408 int64 unlimited_usage() const { return unlimited_usage_; }
409 int64 quota() const { return quota_; }
410 int64 available_space() const { return available_space_; }
411 const GURL& lru_origin() const { return lru_origin_; }
412 const std::set<GURL>& modified_origins() const { return modified_origins_; }
413 StorageType modified_origins_type() const { return modified_origins_type_; }
414 const QuotaTableEntries& quota_entries() const { return quota_entries_; }
415 const OriginInfoTableEntries& origin_info_entries() const {
416 return origin_info_entries_;
418 base::FilePath profile_path() const { return data_dir_.path(); }
419 int status_callback_count() const { return status_callback_count_; }
420 void reset_status_callback_count() { status_callback_count_ = 0; }
422 private:
423 base::Time IncrementMockTime() {
424 ++mock_time_counter_;
425 return base::Time::FromDoubleT(mock_time_counter_ * 10.0);
428 base::MessageLoop message_loop_;
429 base::ScopedTempDir data_dir_;
431 scoped_refptr<QuotaManager> quota_manager_;
432 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_;
434 QuotaStatusCode quota_status_;
435 UsageInfoEntries usage_info_;
436 int64 usage_;
437 int64 limited_usage_;
438 int64 unlimited_usage_;
439 int64 quota_;
440 int64 available_space_;
441 GURL lru_origin_;
442 std::set<GURL> modified_origins_;
443 StorageType modified_origins_type_;
444 QuotaTableEntries quota_entries_;
445 OriginInfoTableEntries origin_info_entries_;
446 int status_callback_count_;
448 int additional_callback_count_;
450 int mock_time_counter_;
452 base::WeakPtrFactory<QuotaManagerTest> weak_factory_;
454 DISALLOW_COPY_AND_ASSIGN(QuotaManagerTest);
457 TEST_F(QuotaManagerTest, GetUsageInfo) {
458 static const MockOriginData kData1[] = {
459 { "http://foo.com/", kTemp, 10 },
460 { "http://foo.com:8080/", kTemp, 15 },
461 { "http://bar.com/", kTemp, 20 },
462 { "http://bar.com/", kPerm, 50 },
464 static const MockOriginData kData2[] = {
465 { "https://foo.com/", kTemp, 30 },
466 { "https://foo.com:8081/", kTemp, 35 },
467 { "http://bar.com/", kPerm, 40 },
468 { "http://example.com/", kPerm, 40 },
470 RegisterClient(CreateClient(kData1, arraysize(kData1),
471 QuotaClient::kFileSystem));
472 RegisterClient(CreateClient(kData2, arraysize(kData2),
473 QuotaClient::kDatabase));
475 GetUsageInfo();
476 base::RunLoop().RunUntilIdle();
478 EXPECT_EQ(4U, usage_info().size());
479 for (size_t i = 0; i < usage_info().size(); ++i) {
480 const UsageInfo& info = usage_info()[i];
481 if (info.host == "foo.com" && info.type == kTemp) {
482 EXPECT_EQ(10 + 15 + 30 + 35, info.usage);
483 } else if (info.host == "bar.com" && info.type == kTemp) {
484 EXPECT_EQ(20, info.usage);
485 } else if (info.host == "bar.com" && info.type == kPerm) {
486 EXPECT_EQ(50 + 40, info.usage);
487 } else if (info.host == "example.com" && info.type == kPerm) {
488 EXPECT_EQ(40, info.usage);
489 } else {
490 ADD_FAILURE()
491 << "Unexpected host, type: " << info.host << ", " << info.type;
496 TEST_F(QuotaManagerTest, GetUsageAndQuota_Simple) {
497 static const MockOriginData kData[] = {
498 { "http://foo.com/", kTemp, 10 },
499 { "http://foo.com/", kPerm, 80 },
501 RegisterClient(CreateClient(kData, arraysize(kData),
502 QuotaClient::kFileSystem));
504 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
505 base::RunLoop().RunUntilIdle();
506 EXPECT_EQ(kQuotaStatusOk, status());
507 EXPECT_EQ(80, usage());
508 EXPECT_EQ(0, quota());
510 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
511 base::RunLoop().RunUntilIdle();
512 EXPECT_EQ(kQuotaStatusOk, status());
513 EXPECT_EQ(10, usage());
514 EXPECT_LE(0, quota());
515 int64 quota_returned_for_foo = quota();
517 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp);
518 base::RunLoop().RunUntilIdle();
519 EXPECT_EQ(kQuotaStatusOk, status());
520 EXPECT_EQ(0, usage());
521 EXPECT_EQ(quota_returned_for_foo, quota());
524 TEST_F(QuotaManagerTest, GetUsage_NoClient) {
525 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
526 base::RunLoop().RunUntilIdle();
527 EXPECT_EQ(kQuotaStatusOk, status());
528 EXPECT_EQ(0, usage());
530 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
531 base::RunLoop().RunUntilIdle();
532 EXPECT_EQ(kQuotaStatusOk, status());
533 EXPECT_EQ(0, usage());
535 GetHostUsage("foo.com", kTemp);
536 base::RunLoop().RunUntilIdle();
537 EXPECT_EQ(0, usage());
539 GetHostUsage("foo.com", kPerm);
540 base::RunLoop().RunUntilIdle();
541 EXPECT_EQ(0, usage());
543 GetGlobalUsage(kTemp);
544 base::RunLoop().RunUntilIdle();
545 EXPECT_EQ(0, usage());
546 EXPECT_EQ(0, unlimited_usage());
548 GetGlobalUsage(kPerm);
549 base::RunLoop().RunUntilIdle();
550 EXPECT_EQ(0, usage());
551 EXPECT_EQ(0, unlimited_usage());
554 TEST_F(QuotaManagerTest, GetUsage_EmptyClient) {
555 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
556 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
557 base::RunLoop().RunUntilIdle();
558 EXPECT_EQ(kQuotaStatusOk, status());
559 EXPECT_EQ(0, usage());
561 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
562 base::RunLoop().RunUntilIdle();
563 EXPECT_EQ(kQuotaStatusOk, status());
564 EXPECT_EQ(0, usage());
566 GetHostUsage("foo.com", kTemp);
567 base::RunLoop().RunUntilIdle();
568 EXPECT_EQ(0, usage());
570 GetHostUsage("foo.com", kPerm);
571 base::RunLoop().RunUntilIdle();
572 EXPECT_EQ(0, usage());
574 GetGlobalUsage(kTemp);
575 base::RunLoop().RunUntilIdle();
576 EXPECT_EQ(0, usage());
577 EXPECT_EQ(0, unlimited_usage());
579 GetGlobalUsage(kPerm);
580 base::RunLoop().RunUntilIdle();
581 EXPECT_EQ(0, usage());
582 EXPECT_EQ(0, unlimited_usage());
585 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_MultiOrigins) {
586 static const MockOriginData kData[] = {
587 { "http://foo.com/", kTemp, 10 },
588 { "http://foo.com:8080/", kTemp, 20 },
589 { "http://bar.com/", kTemp, 5 },
590 { "https://bar.com/", kTemp, 7 },
591 { "http://baz.com/", kTemp, 30 },
592 { "http://foo.com/", kPerm, 40 },
594 RegisterClient(CreateClient(kData, arraysize(kData),
595 QuotaClient::kFileSystem));
597 // This time explicitly sets a temporary global quota.
598 SetTemporaryGlobalQuota(100);
599 base::RunLoop().RunUntilIdle();
600 EXPECT_EQ(kQuotaStatusOk, status());
601 EXPECT_EQ(100, quota());
603 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
604 base::RunLoop().RunUntilIdle();
605 EXPECT_EQ(kQuotaStatusOk, status());
606 EXPECT_EQ(10 + 20, usage());
608 const int kPerHostQuota = 100 / kPerHostTemporaryPortion;
610 // The host's quota should be its full portion of the global quota
611 // since global usage is under the global quota.
612 EXPECT_EQ(kPerHostQuota, quota());
614 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp);
615 base::RunLoop().RunUntilIdle();
616 EXPECT_EQ(kQuotaStatusOk, status());
617 EXPECT_EQ(5 + 7, usage());
618 EXPECT_EQ(kPerHostQuota, quota());
621 TEST_F(QuotaManagerTest, GetUsage_MultipleClients) {
622 static const MockOriginData kData1[] = {
623 { "http://foo.com/", kTemp, 1 },
624 { "http://bar.com/", kTemp, 2 },
625 { "http://bar.com/", kPerm, 4 },
626 { "http://unlimited/", kPerm, 8 },
627 { "http://installed/", kPerm, 16 },
629 static const MockOriginData kData2[] = {
630 { "https://foo.com/", kTemp, 128 },
631 { "http://example.com/", kPerm, 256 },
632 { "http://unlimited/", kTemp, 512 },
633 { "http://installed/", kTemp, 1024 },
635 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
636 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
637 RegisterClient(CreateClient(kData1, arraysize(kData1),
638 QuotaClient::kFileSystem));
639 RegisterClient(CreateClient(kData2, arraysize(kData2),
640 QuotaClient::kDatabase));
642 const int64 kTempQuotaBase =
643 GetAvailableDiskSpaceForTest(base::FilePath()) / kPerHostTemporaryPortion;
645 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
646 base::RunLoop().RunUntilIdle();
647 EXPECT_EQ(kQuotaStatusOk, status());
648 EXPECT_EQ(1 + 128, usage());
650 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kPerm);
651 base::RunLoop().RunUntilIdle();
652 EXPECT_EQ(kQuotaStatusOk, status());
653 EXPECT_EQ(4, usage());
655 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
656 base::RunLoop().RunUntilIdle();
657 EXPECT_EQ(kQuotaStatusOk, status());
658 EXPECT_EQ(512, usage());
659 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota());
661 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm);
662 base::RunLoop().RunUntilIdle();
663 EXPECT_EQ(kQuotaStatusOk, status());
664 EXPECT_EQ(8, usage());
665 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
667 GetAvailableSpace();
668 base::RunLoop().RunUntilIdle();
669 EXPECT_EQ(kQuotaStatusOk, status());
670 EXPECT_LE(0, available_space());
672 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kTemp);
673 base::RunLoop().RunUntilIdle();
674 EXPECT_EQ(kQuotaStatusOk, status());
675 EXPECT_EQ(1024, usage());
676 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota());
678 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm);
679 base::RunLoop().RunUntilIdle();
680 EXPECT_EQ(kQuotaStatusOk, status());
681 EXPECT_EQ(16, usage());
682 EXPECT_EQ(usage(), quota()); // Over-budget case.
684 GetGlobalUsage(kTemp);
685 base::RunLoop().RunUntilIdle();
686 EXPECT_EQ(kQuotaStatusOk, status());
687 EXPECT_EQ(1 + 2 + 128 + 512 + 1024, usage());
688 EXPECT_EQ(512, unlimited_usage());
690 GetGlobalUsage(kPerm);
691 base::RunLoop().RunUntilIdle();
692 EXPECT_EQ(kQuotaStatusOk, status());
693 EXPECT_EQ(4 + 8 + 16 + 256, usage());
694 EXPECT_EQ(8, unlimited_usage());
697 void QuotaManagerTest::GetUsage_WithModifyTestBody(const StorageType type) {
698 const MockOriginData data[] = {
699 { "http://foo.com/", type, 10 },
700 { "http://foo.com:1/", type, 20 },
702 MockStorageClient* client = CreateClient(data, arraysize(data),
703 QuotaClient::kFileSystem);
704 RegisterClient(client);
706 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type);
707 base::RunLoop().RunUntilIdle();
708 EXPECT_EQ(kQuotaStatusOk, status());
709 EXPECT_EQ(10 + 20, usage());
711 client->ModifyOriginAndNotify(GURL("http://foo.com/"), type, 30);
712 client->ModifyOriginAndNotify(GURL("http://foo.com:1/"), type, -5);
713 client->AddOriginAndNotify(GURL("https://foo.com/"), type, 1);
715 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type);
716 base::RunLoop().RunUntilIdle();
717 EXPECT_EQ(kQuotaStatusOk, status());
718 EXPECT_EQ(10 + 20 + 30 - 5 + 1, usage());
719 int foo_usage = usage();
721 client->AddOriginAndNotify(GURL("http://bar.com/"), type, 40);
722 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), type);
723 base::RunLoop().RunUntilIdle();
724 EXPECT_EQ(kQuotaStatusOk, status());
725 EXPECT_EQ(40, usage());
727 GetGlobalUsage(type);
728 base::RunLoop().RunUntilIdle();
729 EXPECT_EQ(foo_usage + 40, usage());
730 EXPECT_EQ(0, unlimited_usage());
733 TEST_F(QuotaManagerTest, GetTemporaryUsage_WithModify) {
734 GetUsage_WithModifyTestBody(kTemp);
737 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_WithAdditionalTasks) {
738 static const MockOriginData kData[] = {
739 { "http://foo.com/", kTemp, 10 },
740 { "http://foo.com:8080/", kTemp, 20 },
741 { "http://bar.com/", kTemp, 13 },
742 { "http://foo.com/", kPerm, 40 },
744 RegisterClient(CreateClient(kData, arraysize(kData),
745 QuotaClient::kFileSystem));
746 SetTemporaryGlobalQuota(100);
747 base::RunLoop().RunUntilIdle();
749 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion;
751 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
752 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
753 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
754 base::RunLoop().RunUntilIdle();
755 EXPECT_EQ(kQuotaStatusOk, status());
756 EXPECT_EQ(10 + 20, usage());
757 EXPECT_EQ(kPerHostQuota, quota());
759 set_additional_callback_count(0);
760 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
761 kTemp);
762 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
763 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kTemp);
764 base::RunLoop().RunUntilIdle();
765 EXPECT_EQ(kQuotaStatusOk, status());
766 EXPECT_EQ(10 + 20, usage());
767 EXPECT_EQ(kPerHostQuota, quota());
768 EXPECT_EQ(2, additional_callback_count());
771 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_NukeManager) {
772 static const MockOriginData kData[] = {
773 { "http://foo.com/", kTemp, 10 },
774 { "http://foo.com:8080/", kTemp, 20 },
775 { "http://bar.com/", kTemp, 13 },
776 { "http://foo.com/", kPerm, 40 },
778 RegisterClient(CreateClient(kData, arraysize(kData),
779 QuotaClient::kFileSystem));
780 SetTemporaryGlobalQuota(100);
781 base::RunLoop().RunUntilIdle();
783 set_additional_callback_count(0);
784 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
785 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
786 kTemp);
787 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"),
788 kTemp);
790 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
791 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients);
793 // Nuke before waiting for callbacks.
794 set_quota_manager(NULL);
795 base::RunLoop().RunUntilIdle();
796 EXPECT_EQ(kQuotaErrorAbort, status());
799 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Overbudget) {
800 static const MockOriginData kData[] = {
801 { "http://usage1/", kTemp, 1 },
802 { "http://usage10/", kTemp, 10 },
803 { "http://usage200/", kTemp, 200 },
805 RegisterClient(CreateClient(kData, arraysize(kData),
806 QuotaClient::kFileSystem));
807 SetTemporaryGlobalQuota(100);
808 base::RunLoop().RunUntilIdle();
810 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion;
812 GetUsageAndQuotaForWebApps(GURL("http://usage1/"), kTemp);
813 base::RunLoop().RunUntilIdle();
814 EXPECT_EQ(kQuotaStatusOk, status());
815 EXPECT_EQ(1, usage());
816 EXPECT_EQ(1, quota()); // should be clamped to our current usage
818 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
819 base::RunLoop().RunUntilIdle();
820 EXPECT_EQ(kQuotaStatusOk, status());
821 EXPECT_EQ(10, usage());
822 EXPECT_EQ(10, quota());
824 GetUsageAndQuotaForWebApps(GURL("http://usage200/"), kTemp);
825 base::RunLoop().RunUntilIdle();
826 EXPECT_EQ(kQuotaStatusOk, status());
827 EXPECT_EQ(200, usage());
828 EXPECT_EQ(kPerHostQuota, quota()); // should be clamped to the nominal quota
831 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Unlimited) {
832 static const MockOriginData kData[] = {
833 { "http://usage10/", kTemp, 10 },
834 { "http://usage50/", kTemp, 50 },
835 { "http://unlimited/", kTemp, 4000 },
837 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
838 MockStorageClient* client = CreateClient(kData, arraysize(kData),
839 QuotaClient::kFileSystem);
840 RegisterClient(client);
842 // Test when not overbugdet.
843 SetTemporaryGlobalQuota(1000);
844 base::RunLoop().RunUntilIdle();
846 GetGlobalUsage(kTemp);
847 base::RunLoop().RunUntilIdle();
848 EXPECT_EQ(10 + 50 + 4000, usage());
849 EXPECT_EQ(4000, unlimited_usage());
851 const int kPerHostQuotaFor1000 =
852 1000 / QuotaManager::kPerHostTemporaryPortion;
854 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
855 base::RunLoop().RunUntilIdle();
856 EXPECT_EQ(kQuotaStatusOk, status());
857 EXPECT_EQ(10, usage());
858 EXPECT_EQ(kPerHostQuotaFor1000, quota());
860 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
861 base::RunLoop().RunUntilIdle();
862 EXPECT_EQ(kQuotaStatusOk, status());
863 EXPECT_EQ(50, usage());
864 EXPECT_EQ(kPerHostQuotaFor1000, quota());
866 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
867 base::RunLoop().RunUntilIdle();
868 EXPECT_EQ(kQuotaStatusOk, status());
869 EXPECT_EQ(4000, usage());
870 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
872 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
873 base::RunLoop().RunUntilIdle();
874 EXPECT_EQ(kQuotaStatusOk, status());
875 EXPECT_EQ(0, usage());
876 EXPECT_EQ(QuotaManager::kNoLimit, quota());
878 // Test when overbugdet.
879 SetTemporaryGlobalQuota(100);
880 base::RunLoop().RunUntilIdle();
882 const int kPerHostQuotaFor100 =
883 100 / QuotaManager::kPerHostTemporaryPortion;
885 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
886 base::RunLoop().RunUntilIdle();
887 EXPECT_EQ(kQuotaStatusOk, status());
888 EXPECT_EQ(10, usage());
889 EXPECT_EQ(kPerHostQuotaFor100, quota());
891 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
892 base::RunLoop().RunUntilIdle();
893 EXPECT_EQ(kQuotaStatusOk, status());
894 EXPECT_EQ(50, usage());
895 EXPECT_EQ(kPerHostQuotaFor100, quota());
897 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
898 base::RunLoop().RunUntilIdle();
899 EXPECT_EQ(kQuotaStatusOk, status());
900 EXPECT_EQ(4000, usage());
901 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
903 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
904 base::RunLoop().RunUntilIdle();
905 EXPECT_EQ(kQuotaStatusOk, status());
906 EXPECT_EQ(0, usage());
907 EXPECT_EQ(QuotaManager::kNoLimit, quota());
909 // Revoke the unlimited rights and make sure the change is noticed.
910 mock_special_storage_policy()->Reset();
911 mock_special_storage_policy()->NotifyCleared();
913 GetGlobalUsage(kTemp);
914 base::RunLoop().RunUntilIdle();
915 EXPECT_EQ(10 + 50 + 4000, usage());
916 EXPECT_EQ(0, unlimited_usage());
918 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
919 base::RunLoop().RunUntilIdle();
920 EXPECT_EQ(kQuotaStatusOk, status());
921 EXPECT_EQ(10, usage());
922 EXPECT_EQ(10, quota()); // should be clamped to our current usage
924 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
925 base::RunLoop().RunUntilIdle();
926 EXPECT_EQ(kQuotaStatusOk, status());
927 EXPECT_EQ(50, usage());
928 EXPECT_EQ(kPerHostQuotaFor100, quota());
930 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
931 base::RunLoop().RunUntilIdle();
932 EXPECT_EQ(kQuotaStatusOk, status());
933 EXPECT_EQ(4000, usage());
934 EXPECT_EQ(kPerHostQuotaFor100, quota());
936 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
937 base::RunLoop().RunUntilIdle();
938 EXPECT_EQ(kQuotaStatusOk, status());
939 EXPECT_EQ(4000, usage());
940 EXPECT_EQ(kPerHostQuotaFor100, quota());
943 TEST_F(QuotaManagerTest, OriginInUse) {
944 const GURL kFooOrigin("http://foo.com/");
945 const GURL kBarOrigin("http://bar.com/");
947 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin));
948 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 1
949 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
950 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 2
951 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
952 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin); // count of 1
953 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
955 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin));
956 quota_manager()->NotifyOriginInUse(kBarOrigin);
957 EXPECT_TRUE(quota_manager()->IsOriginInUse(kBarOrigin));
958 quota_manager()->NotifyOriginNoLongerInUse(kBarOrigin);
959 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin));
961 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin);
962 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin));
965 TEST_F(QuotaManagerTest, GetAndSetPerststentHostQuota) {
966 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
968 GetPersistentHostQuota("foo.com");
969 base::RunLoop().RunUntilIdle();
970 EXPECT_EQ(0, quota());
972 SetPersistentHostQuota("foo.com", 100);
973 base::RunLoop().RunUntilIdle();
974 EXPECT_EQ(100, quota());
976 GetPersistentHostQuota("foo.com");
977 SetPersistentHostQuota("foo.com", 200);
978 GetPersistentHostQuota("foo.com");
979 SetPersistentHostQuota("foo.com", QuotaManager::kPerHostPersistentQuotaLimit);
980 GetPersistentHostQuota("foo.com");
981 base::RunLoop().RunUntilIdle();
982 EXPECT_EQ(QuotaManager::kPerHostPersistentQuotaLimit, quota());
984 // Persistent quota should be capped at the per-host quota limit.
985 SetPersistentHostQuota("foo.com",
986 QuotaManager::kPerHostPersistentQuotaLimit + 100);
987 GetPersistentHostQuota("foo.com");
988 base::RunLoop().RunUntilIdle();
989 EXPECT_EQ(QuotaManager::kPerHostPersistentQuotaLimit, quota());
992 TEST_F(QuotaManagerTest, GetAndSetPersistentUsageAndQuota) {
993 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
995 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
996 base::RunLoop().RunUntilIdle();
997 EXPECT_EQ(kQuotaStatusOk, status());
998 EXPECT_EQ(0, usage());
999 EXPECT_EQ(0, quota());
1001 SetPersistentHostQuota("foo.com", 100);
1002 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1003 base::RunLoop().RunUntilIdle();
1004 EXPECT_EQ(kQuotaStatusOk, status());
1005 EXPECT_EQ(0, usage());
1006 EXPECT_EQ(100, quota());
1008 // For installed app GetUsageAndQuotaForWebApps returns the capped quota.
1009 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
1010 SetPersistentHostQuota("installed", kAvailableSpaceForApp + 100);
1011 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm);
1012 base::RunLoop().RunUntilIdle();
1013 EXPECT_EQ(kAvailableSpaceForApp, quota());
1015 // Ditto for unlimited apps.
1016 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
1017 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm);
1018 base::RunLoop().RunUntilIdle();
1019 EXPECT_EQ(kAvailableSpaceForApp, quota());
1021 // GetUsageAndQuotaForStorageClient should just return 0 usage and
1022 // kNoLimit quota.
1023 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kPerm);
1024 base::RunLoop().RunUntilIdle();
1025 EXPECT_EQ(0, usage());
1026 EXPECT_EQ(QuotaManager::kNoLimit, quota());
1029 TEST_F(QuotaManagerTest, GetSyncableQuota) {
1030 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
1032 // Pre-condition check: available disk space (for testing) is less than
1033 // the default quota for syncable storage.
1034 EXPECT_LE(kAvailableSpaceForApp,
1035 QuotaManager::kSyncableStorageDefaultHostQuota);
1037 // For installed apps the quota manager should return
1038 // kAvailableSpaceForApp as syncable quota (because of the pre-condition).
1039 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
1040 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kSync);
1041 base::RunLoop().RunUntilIdle();
1042 EXPECT_EQ(kQuotaStatusOk, status());
1043 EXPECT_EQ(0, usage());
1044 EXPECT_EQ(kAvailableSpaceForApp, quota());
1046 // If it's not installed (which shouldn't happen in real case) it
1047 // should just return the default host quota for syncable.
1048 GetUsageAndQuotaForWebApps(GURL("http://foo/"), kSync);
1049 base::RunLoop().RunUntilIdle();
1050 EXPECT_EQ(kQuotaStatusOk, status());
1051 EXPECT_EQ(0, usage());
1052 EXPECT_EQ(QuotaManager::kSyncableStorageDefaultHostQuota, quota());
1055 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_MultiOrigins) {
1056 static const MockOriginData kData[] = {
1057 { "http://foo.com/", kPerm, 10 },
1058 { "http://foo.com:8080/", kPerm, 20 },
1059 { "https://foo.com/", kPerm, 13 },
1060 { "https://foo.com:8081/", kPerm, 19 },
1061 { "http://bar.com/", kPerm, 5 },
1062 { "https://bar.com/", kPerm, 7 },
1063 { "http://baz.com/", kPerm, 30 },
1064 { "http://foo.com/", kTemp, 40 },
1066 RegisterClient(CreateClient(kData, arraysize(kData),
1067 QuotaClient::kFileSystem));
1069 SetPersistentHostQuota("foo.com", 100);
1070 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1071 base::RunLoop().RunUntilIdle();
1072 EXPECT_EQ(kQuotaStatusOk, status());
1073 EXPECT_EQ(10 + 20 + 13 + 19, usage());
1074 EXPECT_EQ(100, quota());
1077 TEST_F(QuotaManagerTest, GetPersistentUsage_WithModify) {
1078 GetUsage_WithModifyTestBody(kPerm);
1081 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_WithAdditionalTasks) {
1082 static const MockOriginData kData[] = {
1083 { "http://foo.com/", kPerm, 10 },
1084 { "http://foo.com:8080/", kPerm, 20 },
1085 { "http://bar.com/", kPerm, 13 },
1086 { "http://foo.com/", kTemp, 40 },
1088 RegisterClient(CreateClient(kData, arraysize(kData),
1089 QuotaClient::kFileSystem));
1090 SetPersistentHostQuota("foo.com", 100);
1092 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1093 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1094 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1095 base::RunLoop().RunUntilIdle();
1096 EXPECT_EQ(kQuotaStatusOk, status());
1097 EXPECT_EQ(10 + 20, usage());
1098 EXPECT_EQ(100, quota());
1100 set_additional_callback_count(0);
1101 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
1102 kPerm);
1103 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1104 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm);
1105 base::RunLoop().RunUntilIdle();
1106 EXPECT_EQ(kQuotaStatusOk, status());
1107 EXPECT_EQ(10 + 20, usage());
1108 EXPECT_EQ(2, additional_callback_count());
1111 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_NukeManager) {
1112 static const MockOriginData kData[] = {
1113 { "http://foo.com/", kPerm, 10 },
1114 { "http://foo.com:8080/", kPerm, 20 },
1115 { "http://bar.com/", kPerm, 13 },
1116 { "http://foo.com/", kTemp, 40 },
1118 RegisterClient(CreateClient(kData, arraysize(kData),
1119 QuotaClient::kFileSystem));
1120 SetPersistentHostQuota("foo.com", 100);
1122 set_additional_callback_count(0);
1123 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1124 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), kPerm);
1125 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm);
1127 // Nuke before waiting for callbacks.
1128 set_quota_manager(NULL);
1129 base::RunLoop().RunUntilIdle();
1130 EXPECT_EQ(kQuotaErrorAbort, status());
1133 TEST_F(QuotaManagerTest, GetUsage_Simple) {
1134 static const MockOriginData kData[] = {
1135 { "http://foo.com/", kPerm, 1 },
1136 { "http://foo.com:1/", kPerm, 20 },
1137 { "http://bar.com/", kTemp, 300 },
1138 { "https://buz.com/", kTemp, 4000 },
1139 { "http://buz.com/", kTemp, 50000 },
1140 { "http://bar.com:1/", kPerm, 600000 },
1141 { "http://foo.com/", kTemp, 7000000 },
1143 RegisterClient(CreateClient(kData, arraysize(kData),
1144 QuotaClient::kFileSystem));
1146 GetGlobalUsage(kPerm);
1147 base::RunLoop().RunUntilIdle();
1148 EXPECT_EQ(usage(), 1 + 20 + 600000);
1149 EXPECT_EQ(0, unlimited_usage());
1151 GetGlobalUsage(kTemp);
1152 base::RunLoop().RunUntilIdle();
1153 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000);
1154 EXPECT_EQ(0, unlimited_usage());
1156 GetHostUsage("foo.com", kPerm);
1157 base::RunLoop().RunUntilIdle();
1158 EXPECT_EQ(usage(), 1 + 20);
1160 GetHostUsage("buz.com", kTemp);
1161 base::RunLoop().RunUntilIdle();
1162 EXPECT_EQ(usage(), 4000 + 50000);
1165 TEST_F(QuotaManagerTest, GetUsage_WithModification) {
1166 static const MockOriginData kData[] = {
1167 { "http://foo.com/", kPerm, 1 },
1168 { "http://foo.com:1/", kPerm, 20 },
1169 { "http://bar.com/", kTemp, 300 },
1170 { "https://buz.com/", kTemp, 4000 },
1171 { "http://buz.com/", kTemp, 50000 },
1172 { "http://bar.com:1/", kPerm, 600000 },
1173 { "http://foo.com/", kTemp, 7000000 },
1176 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1177 QuotaClient::kFileSystem);
1178 RegisterClient(client);
1180 GetGlobalUsage(kPerm);
1181 base::RunLoop().RunUntilIdle();
1182 EXPECT_EQ(usage(), 1 + 20 + 600000);
1183 EXPECT_EQ(0, unlimited_usage());
1185 client->ModifyOriginAndNotify(
1186 GURL("http://foo.com/"), kPerm, 80000000);
1188 GetGlobalUsage(kPerm);
1189 base::RunLoop().RunUntilIdle();
1190 EXPECT_EQ(usage(), 1 + 20 + 600000 + 80000000);
1191 EXPECT_EQ(0, unlimited_usage());
1193 GetGlobalUsage(kTemp);
1194 base::RunLoop().RunUntilIdle();
1195 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000);
1196 EXPECT_EQ(0, unlimited_usage());
1198 client->ModifyOriginAndNotify(
1199 GURL("http://foo.com/"), kTemp, 1);
1201 GetGlobalUsage(kTemp);
1202 base::RunLoop().RunUntilIdle();
1203 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000 + 1);
1204 EXPECT_EQ(0, unlimited_usage());
1206 GetHostUsage("buz.com", kTemp);
1207 base::RunLoop().RunUntilIdle();
1208 EXPECT_EQ(usage(), 4000 + 50000);
1210 client->ModifyOriginAndNotify(
1211 GURL("http://buz.com/"), kTemp, 900000000);
1213 GetHostUsage("buz.com", kTemp);
1214 base::RunLoop().RunUntilIdle();
1215 EXPECT_EQ(usage(), 4000 + 50000 + 900000000);
1218 TEST_F(QuotaManagerTest, GetUsage_WithDeleteOrigin) {
1219 static const MockOriginData kData[] = {
1220 { "http://foo.com/", kTemp, 1 },
1221 { "http://foo.com:1/", kTemp, 20 },
1222 { "http://foo.com/", kPerm, 300 },
1223 { "http://bar.com/", kTemp, 4000 },
1225 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1226 QuotaClient::kFileSystem);
1227 RegisterClient(client);
1229 GetGlobalUsage(kTemp);
1230 base::RunLoop().RunUntilIdle();
1231 int64 predelete_global_tmp = usage();
1233 GetHostUsage("foo.com", kTemp);
1234 base::RunLoop().RunUntilIdle();
1235 int64 predelete_host_tmp = usage();
1237 GetHostUsage("foo.com", kPerm);
1238 base::RunLoop().RunUntilIdle();
1239 int64 predelete_host_pers = usage();
1241 DeleteClientOriginData(client, GURL("http://foo.com/"),
1242 kTemp);
1243 base::RunLoop().RunUntilIdle();
1244 EXPECT_EQ(kQuotaStatusOk, status());
1246 GetGlobalUsage(kTemp);
1247 base::RunLoop().RunUntilIdle();
1248 EXPECT_EQ(predelete_global_tmp - 1, usage());
1250 GetHostUsage("foo.com", kTemp);
1251 base::RunLoop().RunUntilIdle();
1252 EXPECT_EQ(predelete_host_tmp - 1, usage());
1254 GetHostUsage("foo.com", kPerm);
1255 base::RunLoop().RunUntilIdle();
1256 EXPECT_EQ(predelete_host_pers, usage());
1259 TEST_F(QuotaManagerTest, GetAvailableSpaceTest) {
1260 GetAvailableSpace();
1261 base::RunLoop().RunUntilIdle();
1262 EXPECT_EQ(kQuotaStatusOk, status());
1263 EXPECT_LE(0, available_space());
1266 TEST_F(QuotaManagerTest, EvictOriginData) {
1267 static const MockOriginData kData1[] = {
1268 { "http://foo.com/", kTemp, 1 },
1269 { "http://foo.com:1/", kTemp, 20 },
1270 { "http://foo.com/", kPerm, 300 },
1271 { "http://bar.com/", kTemp, 4000 },
1273 static const MockOriginData kData2[] = {
1274 { "http://foo.com/", kTemp, 50000 },
1275 { "http://foo.com:1/", kTemp, 6000 },
1276 { "http://foo.com/", kPerm, 700 },
1277 { "https://foo.com/", kTemp, 80 },
1278 { "http://bar.com/", kTemp, 9 },
1280 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1281 QuotaClient::kFileSystem);
1282 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1283 QuotaClient::kDatabase);
1284 RegisterClient(client1);
1285 RegisterClient(client2);
1287 GetGlobalUsage(kTemp);
1288 base::RunLoop().RunUntilIdle();
1289 int64 predelete_global_tmp = usage();
1291 GetHostUsage("foo.com", kTemp);
1292 base::RunLoop().RunUntilIdle();
1293 int64 predelete_host_tmp = usage();
1295 GetHostUsage("foo.com", kPerm);
1296 base::RunLoop().RunUntilIdle();
1297 int64 predelete_host_pers = usage();
1299 for (size_t i = 0; i < arraysize(kData1); ++i)
1300 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1301 GURL(kData1[i].origin), kData1[i].type);
1302 for (size_t i = 0; i < arraysize(kData2); ++i)
1303 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1304 GURL(kData2[i].origin), kData2[i].type);
1305 base::RunLoop().RunUntilIdle();
1307 EvictOriginData(GURL("http://foo.com/"), kTemp);
1308 base::RunLoop().RunUntilIdle();
1310 DumpOriginInfoTable();
1311 base::RunLoop().RunUntilIdle();
1313 typedef OriginInfoTableEntries::const_iterator iterator;
1314 for (iterator itr(origin_info_entries().begin()),
1315 end(origin_info_entries().end());
1316 itr != end; ++itr) {
1317 if (itr->type == kTemp)
1318 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1321 GetGlobalUsage(kTemp);
1322 base::RunLoop().RunUntilIdle();
1323 EXPECT_EQ(predelete_global_tmp - (1 + 50000), usage());
1325 GetHostUsage("foo.com", kTemp);
1326 base::RunLoop().RunUntilIdle();
1327 EXPECT_EQ(predelete_host_tmp - (1 + 50000), usage());
1329 GetHostUsage("foo.com", kPerm);
1330 base::RunLoop().RunUntilIdle();
1331 EXPECT_EQ(predelete_host_pers, usage());
1334 TEST_F(QuotaManagerTest, EvictOriginDataWithDeletionError) {
1335 static const MockOriginData kData[] = {
1336 { "http://foo.com/", kTemp, 1 },
1337 { "http://foo.com:1/", kTemp, 20 },
1338 { "http://foo.com/", kPerm, 300 },
1339 { "http://bar.com/", kTemp, 4000 },
1341 static const int kNumberOfTemporaryOrigins = 3;
1342 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1343 QuotaClient::kFileSystem);
1344 RegisterClient(client);
1346 GetGlobalUsage(kTemp);
1347 base::RunLoop().RunUntilIdle();
1348 int64 predelete_global_tmp = usage();
1350 GetHostUsage("foo.com", kTemp);
1351 base::RunLoop().RunUntilIdle();
1352 int64 predelete_host_tmp = usage();
1354 GetHostUsage("foo.com", kPerm);
1355 base::RunLoop().RunUntilIdle();
1356 int64 predelete_host_pers = usage();
1358 for (size_t i = 0; i < arraysize(kData); ++i)
1359 NotifyStorageAccessed(client, GURL(kData[i].origin), kData[i].type);
1360 base::RunLoop().RunUntilIdle();
1362 client->AddOriginToErrorSet(GURL("http://foo.com/"), kTemp);
1364 for (int i = 0;
1365 i < QuotaManager::kThresholdOfErrorsToBeBlacklisted + 1;
1366 ++i) {
1367 EvictOriginData(GURL("http://foo.com/"), kTemp);
1368 base::RunLoop().RunUntilIdle();
1369 EXPECT_EQ(kQuotaErrorInvalidModification, status());
1372 DumpOriginInfoTable();
1373 base::RunLoop().RunUntilIdle();
1375 bool found_origin_in_database = false;
1376 typedef OriginInfoTableEntries::const_iterator iterator;
1377 for (iterator itr(origin_info_entries().begin()),
1378 end(origin_info_entries().end());
1379 itr != end; ++itr) {
1380 if (itr->type == kTemp &&
1381 GURL("http://foo.com/") == itr->origin) {
1382 found_origin_in_database = true;
1383 break;
1386 // The origin "http://foo.com/" should be in the database.
1387 EXPECT_TRUE(found_origin_in_database);
1389 for (size_t i = 0; i < kNumberOfTemporaryOrigins - 1; ++i) {
1390 GetLRUOrigin(kTemp);
1391 base::RunLoop().RunUntilIdle();
1392 EXPECT_FALSE(lru_origin().is_empty());
1393 // The origin "http://foo.com/" should not be in the LRU list.
1394 EXPECT_NE(std::string("http://foo.com/"), lru_origin().spec());
1395 DeleteOriginFromDatabase(lru_origin(), kTemp);
1396 base::RunLoop().RunUntilIdle();
1399 // Now the LRU list must be empty.
1400 GetLRUOrigin(kTemp);
1401 base::RunLoop().RunUntilIdle();
1402 EXPECT_TRUE(lru_origin().is_empty());
1404 // Deleting origins from the database should not affect the results of the
1405 // following checks.
1406 GetGlobalUsage(kTemp);
1407 base::RunLoop().RunUntilIdle();
1408 EXPECT_EQ(predelete_global_tmp, usage());
1410 GetHostUsage("foo.com", kTemp);
1411 base::RunLoop().RunUntilIdle();
1412 EXPECT_EQ(predelete_host_tmp, usage());
1414 GetHostUsage("foo.com", kPerm);
1415 base::RunLoop().RunUntilIdle();
1416 EXPECT_EQ(predelete_host_pers, usage());
1419 TEST_F(QuotaManagerTest, GetUsageAndQuotaForEviction) {
1420 static const MockOriginData kData[] = {
1421 { "http://foo.com/", kTemp, 1 },
1422 { "http://foo.com:1/", kTemp, 20 },
1423 { "http://foo.com/", kPerm, 300 },
1424 { "http://unlimited/", kTemp, 4000 },
1427 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
1428 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1429 QuotaClient::kFileSystem);
1430 RegisterClient(client);
1432 SetTemporaryGlobalQuota(10000000);
1433 base::RunLoop().RunUntilIdle();
1435 GetUsageAndQuotaForEviction();
1436 base::RunLoop().RunUntilIdle();
1437 EXPECT_EQ(kQuotaStatusOk, status());
1438 EXPECT_EQ(21, limited_usage());
1439 EXPECT_EQ(10000000, quota());
1440 EXPECT_LE(0, available_space());
1443 TEST_F(QuotaManagerTest, DeleteHostDataSimple) {
1444 static const MockOriginData kData[] = {
1445 { "http://foo.com/", kTemp, 1 },
1447 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1448 QuotaClient::kFileSystem);
1449 RegisterClient(client);
1451 GetGlobalUsage(kTemp);
1452 base::RunLoop().RunUntilIdle();
1453 const int64 predelete_global_tmp = usage();
1455 GetHostUsage("foo.com", kTemp);
1456 base::RunLoop().RunUntilIdle();
1457 int64 predelete_host_tmp = usage();
1459 GetHostUsage("foo.com", kPerm);
1460 base::RunLoop().RunUntilIdle();
1461 int64 predelete_host_pers = usage();
1463 DeleteHostData(std::string(), kTemp, kAllClients);
1464 base::RunLoop().RunUntilIdle();
1465 EXPECT_EQ(kQuotaStatusOk, status());
1467 GetGlobalUsage(kTemp);
1468 base::RunLoop().RunUntilIdle();
1469 EXPECT_EQ(predelete_global_tmp, usage());
1471 GetHostUsage("foo.com", kTemp);
1472 base::RunLoop().RunUntilIdle();
1473 EXPECT_EQ(predelete_host_tmp, usage());
1475 GetHostUsage("foo.com", kPerm);
1476 base::RunLoop().RunUntilIdle();
1477 EXPECT_EQ(predelete_host_pers, usage());
1479 DeleteHostData("foo.com", kTemp, kAllClients);
1480 base::RunLoop().RunUntilIdle();
1481 EXPECT_EQ(kQuotaStatusOk, status());
1483 GetGlobalUsage(kTemp);
1484 base::RunLoop().RunUntilIdle();
1485 EXPECT_EQ(predelete_global_tmp - 1, usage());
1487 GetHostUsage("foo.com", kTemp);
1488 base::RunLoop().RunUntilIdle();
1489 EXPECT_EQ(predelete_host_tmp - 1, usage());
1491 GetHostUsage("foo.com", kPerm);
1492 base::RunLoop().RunUntilIdle();
1493 EXPECT_EQ(predelete_host_pers, usage());
1496 TEST_F(QuotaManagerTest, DeleteHostDataMultiple) {
1497 static const MockOriginData kData1[] = {
1498 { "http://foo.com/", kTemp, 1 },
1499 { "http://foo.com:1/", kTemp, 20 },
1500 { "http://foo.com/", kPerm, 300 },
1501 { "http://bar.com/", kTemp, 4000 },
1503 static const MockOriginData kData2[] = {
1504 { "http://foo.com/", kTemp, 50000 },
1505 { "http://foo.com:1/", kTemp, 6000 },
1506 { "http://foo.com/", kPerm, 700 },
1507 { "https://foo.com/", kTemp, 80 },
1508 { "http://bar.com/", kTemp, 9 },
1510 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1511 QuotaClient::kFileSystem);
1512 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1513 QuotaClient::kDatabase);
1514 RegisterClient(client1);
1515 RegisterClient(client2);
1517 GetGlobalUsage(kTemp);
1518 base::RunLoop().RunUntilIdle();
1519 const int64 predelete_global_tmp = usage();
1521 GetHostUsage("foo.com", kTemp);
1522 base::RunLoop().RunUntilIdle();
1523 const int64 predelete_foo_tmp = usage();
1525 GetHostUsage("bar.com", kTemp);
1526 base::RunLoop().RunUntilIdle();
1527 const int64 predelete_bar_tmp = usage();
1529 GetHostUsage("foo.com", kPerm);
1530 base::RunLoop().RunUntilIdle();
1531 const int64 predelete_foo_pers = usage();
1533 GetHostUsage("bar.com", kPerm);
1534 base::RunLoop().RunUntilIdle();
1535 const int64 predelete_bar_pers = usage();
1537 reset_status_callback_count();
1538 DeleteHostData("foo.com", kTemp, kAllClients);
1539 DeleteHostData("bar.com", kTemp, kAllClients);
1540 DeleteHostData("foo.com", kTemp, kAllClients);
1541 base::RunLoop().RunUntilIdle();
1543 EXPECT_EQ(3, status_callback_count());
1545 DumpOriginInfoTable();
1546 base::RunLoop().RunUntilIdle();
1548 typedef OriginInfoTableEntries::const_iterator iterator;
1549 for (iterator itr(origin_info_entries().begin()),
1550 end(origin_info_entries().end());
1551 itr != end; ++itr) {
1552 if (itr->type == kTemp) {
1553 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1554 EXPECT_NE(std::string("http://foo.com:1/"), itr->origin.spec());
1555 EXPECT_NE(std::string("https://foo.com/"), itr->origin.spec());
1556 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec());
1560 GetGlobalUsage(kTemp);
1561 base::RunLoop().RunUntilIdle();
1562 EXPECT_EQ(predelete_global_tmp - (1 + 20 + 4000 + 50000 + 6000 + 80 + 9),
1563 usage());
1565 GetHostUsage("foo.com", kTemp);
1566 base::RunLoop().RunUntilIdle();
1567 EXPECT_EQ(predelete_foo_tmp - (1 + 20 + 50000 + 6000 + 80), usage());
1569 GetHostUsage("bar.com", kTemp);
1570 base::RunLoop().RunUntilIdle();
1571 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage());
1573 GetHostUsage("foo.com", kPerm);
1574 base::RunLoop().RunUntilIdle();
1575 EXPECT_EQ(predelete_foo_pers, usage());
1577 GetHostUsage("bar.com", kPerm);
1578 base::RunLoop().RunUntilIdle();
1579 EXPECT_EQ(predelete_bar_pers, usage());
1582 // Single-run DeleteOriginData cases must be well covered by
1583 // EvictOriginData tests.
1584 TEST_F(QuotaManagerTest, DeleteOriginDataMultiple) {
1585 static const MockOriginData kData1[] = {
1586 { "http://foo.com/", kTemp, 1 },
1587 { "http://foo.com:1/", kTemp, 20 },
1588 { "http://foo.com/", kPerm, 300 },
1589 { "http://bar.com/", kTemp, 4000 },
1591 static const MockOriginData kData2[] = {
1592 { "http://foo.com/", kTemp, 50000 },
1593 { "http://foo.com:1/", kTemp, 6000 },
1594 { "http://foo.com/", kPerm, 700 },
1595 { "https://foo.com/", kTemp, 80 },
1596 { "http://bar.com/", kTemp, 9 },
1598 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1599 QuotaClient::kFileSystem);
1600 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1601 QuotaClient::kDatabase);
1602 RegisterClient(client1);
1603 RegisterClient(client2);
1605 GetGlobalUsage(kTemp);
1606 base::RunLoop().RunUntilIdle();
1607 const int64 predelete_global_tmp = usage();
1609 GetHostUsage("foo.com", kTemp);
1610 base::RunLoop().RunUntilIdle();
1611 const int64 predelete_foo_tmp = usage();
1613 GetHostUsage("bar.com", kTemp);
1614 base::RunLoop().RunUntilIdle();
1615 const int64 predelete_bar_tmp = usage();
1617 GetHostUsage("foo.com", kPerm);
1618 base::RunLoop().RunUntilIdle();
1619 const int64 predelete_foo_pers = usage();
1621 GetHostUsage("bar.com", kPerm);
1622 base::RunLoop().RunUntilIdle();
1623 const int64 predelete_bar_pers = usage();
1625 for (size_t i = 0; i < arraysize(kData1); ++i)
1626 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1627 GURL(kData1[i].origin), kData1[i].type);
1628 for (size_t i = 0; i < arraysize(kData2); ++i)
1629 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1630 GURL(kData2[i].origin), kData2[i].type);
1631 base::RunLoop().RunUntilIdle();
1633 reset_status_callback_count();
1634 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
1635 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients);
1636 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
1637 base::RunLoop().RunUntilIdle();
1639 EXPECT_EQ(3, status_callback_count());
1641 DumpOriginInfoTable();
1642 base::RunLoop().RunUntilIdle();
1644 typedef OriginInfoTableEntries::const_iterator iterator;
1645 for (iterator itr(origin_info_entries().begin()),
1646 end(origin_info_entries().end());
1647 itr != end; ++itr) {
1648 if (itr->type == kTemp) {
1649 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1650 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec());
1654 GetGlobalUsage(kTemp);
1655 base::RunLoop().RunUntilIdle();
1656 EXPECT_EQ(predelete_global_tmp - (1 + 4000 + 50000 + 9), usage());
1658 GetHostUsage("foo.com", kTemp);
1659 base::RunLoop().RunUntilIdle();
1660 EXPECT_EQ(predelete_foo_tmp - (1 + 50000), usage());
1662 GetHostUsage("bar.com", kTemp);
1663 base::RunLoop().RunUntilIdle();
1664 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage());
1666 GetHostUsage("foo.com", kPerm);
1667 base::RunLoop().RunUntilIdle();
1668 EXPECT_EQ(predelete_foo_pers, usage());
1670 GetHostUsage("bar.com", kPerm);
1671 base::RunLoop().RunUntilIdle();
1672 EXPECT_EQ(predelete_bar_pers, usage());
1675 TEST_F(QuotaManagerTest, GetCachedOrigins) {
1676 static const MockOriginData kData[] = {
1677 { "http://a.com/", kTemp, 1 },
1678 { "http://a.com:1/", kTemp, 20 },
1679 { "http://b.com/", kPerm, 300 },
1680 { "http://c.com/", kTemp, 4000 },
1682 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1683 QuotaClient::kFileSystem);
1684 RegisterClient(client);
1686 // TODO(kinuko): Be careful when we add cache pruner.
1688 std::set<GURL> origins;
1689 GetCachedOrigins(kTemp, &origins);
1690 EXPECT_TRUE(origins.empty());
1692 // No matter how we make queries the quota manager tries to cache all
1693 // the origins at startup.
1694 GetHostUsage("a.com", kTemp);
1695 base::RunLoop().RunUntilIdle();
1696 GetCachedOrigins(kTemp, &origins);
1697 EXPECT_EQ(3U, origins.size());
1699 GetHostUsage("b.com", kTemp);
1700 base::RunLoop().RunUntilIdle();
1701 GetCachedOrigins(kTemp, &origins);
1702 EXPECT_EQ(3U, origins.size());
1704 GetCachedOrigins(kPerm, &origins);
1705 EXPECT_TRUE(origins.empty());
1707 GetGlobalUsage(kTemp);
1708 base::RunLoop().RunUntilIdle();
1709 GetCachedOrigins(kTemp, &origins);
1710 EXPECT_EQ(3U, origins.size());
1712 for (size_t i = 0; i < arraysize(kData); ++i) {
1713 if (kData[i].type == kTemp)
1714 EXPECT_TRUE(origins.find(GURL(kData[i].origin)) != origins.end());
1718 TEST_F(QuotaManagerTest, NotifyAndLRUOrigin) {
1719 static const MockOriginData kData[] = {
1720 { "http://a.com/", kTemp, 0 },
1721 { "http://a.com:1/", kTemp, 0 },
1722 { "https://a.com/", kTemp, 0 },
1723 { "http://b.com/", kPerm, 0 }, // persistent
1724 { "http://c.com/", kTemp, 0 },
1726 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1727 QuotaClient::kFileSystem);
1728 RegisterClient(client);
1730 GURL origin;
1731 GetLRUOrigin(kTemp);
1732 base::RunLoop().RunUntilIdle();
1733 EXPECT_TRUE(lru_origin().is_empty());
1735 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp);
1736 GetLRUOrigin(kTemp);
1737 base::RunLoop().RunUntilIdle();
1738 EXPECT_EQ("http://a.com/", lru_origin().spec());
1740 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm);
1741 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp);
1742 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1743 GetLRUOrigin(kTemp);
1744 base::RunLoop().RunUntilIdle();
1745 EXPECT_EQ("http://a.com/", lru_origin().spec());
1747 DeleteOriginFromDatabase(lru_origin(), kTemp);
1748 GetLRUOrigin(kTemp);
1749 base::RunLoop().RunUntilIdle();
1750 EXPECT_EQ("https://a.com/", lru_origin().spec());
1752 DeleteOriginFromDatabase(lru_origin(), kTemp);
1753 GetLRUOrigin(kTemp);
1754 base::RunLoop().RunUntilIdle();
1755 EXPECT_EQ("http://c.com/", lru_origin().spec());
1758 TEST_F(QuotaManagerTest, GetLRUOriginWithOriginInUse) {
1759 static const MockOriginData kData[] = {
1760 { "http://a.com/", kTemp, 0 },
1761 { "http://a.com:1/", kTemp, 0 },
1762 { "https://a.com/", kTemp, 0 },
1763 { "http://b.com/", kPerm, 0 }, // persistent
1764 { "http://c.com/", kTemp, 0 },
1766 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1767 QuotaClient::kFileSystem);
1768 RegisterClient(client);
1770 GURL origin;
1771 GetLRUOrigin(kTemp);
1772 base::RunLoop().RunUntilIdle();
1773 EXPECT_TRUE(lru_origin().is_empty());
1775 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp);
1776 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm);
1777 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp);
1778 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1780 GetLRUOrigin(kTemp);
1781 base::RunLoop().RunUntilIdle();
1782 EXPECT_EQ("http://a.com/", lru_origin().spec());
1784 // Notify origin http://a.com is in use.
1785 NotifyOriginInUse(GURL("http://a.com/"));
1786 GetLRUOrigin(kTemp);
1787 base::RunLoop().RunUntilIdle();
1788 EXPECT_EQ("https://a.com/", lru_origin().spec());
1790 // Notify origin https://a.com is in use while GetLRUOrigin is running.
1791 GetLRUOrigin(kTemp);
1792 NotifyOriginInUse(GURL("https://a.com/"));
1793 base::RunLoop().RunUntilIdle();
1794 // Post-filtering must have excluded the returned origin, so we will
1795 // see empty result here.
1796 EXPECT_TRUE(lru_origin().is_empty());
1798 // Notify access for http://c.com while GetLRUOrigin is running.
1799 GetLRUOrigin(kTemp);
1800 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1801 base::RunLoop().RunUntilIdle();
1802 // Post-filtering must have excluded the returned origin, so we will
1803 // see empty result here.
1804 EXPECT_TRUE(lru_origin().is_empty());
1806 NotifyOriginNoLongerInUse(GURL("http://a.com/"));
1807 NotifyOriginNoLongerInUse(GURL("https://a.com/"));
1808 GetLRUOrigin(kTemp);
1809 base::RunLoop().RunUntilIdle();
1810 EXPECT_EQ("http://a.com/", lru_origin().spec());
1813 TEST_F(QuotaManagerTest, GetOriginsModifiedSince) {
1814 static const MockOriginData kData[] = {
1815 { "http://a.com/", kTemp, 0 },
1816 { "http://a.com:1/", kTemp, 0 },
1817 { "https://a.com/", kTemp, 0 },
1818 { "http://b.com/", kPerm, 0 }, // persistent
1819 { "http://c.com/", kTemp, 0 },
1821 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1822 QuotaClient::kFileSystem);
1823 RegisterClient(client);
1825 GetOriginsModifiedSince(kTemp, base::Time());
1826 base::RunLoop().RunUntilIdle();
1827 EXPECT_TRUE(modified_origins().empty());
1828 EXPECT_EQ(modified_origins_type(), kTemp);
1830 base::Time time1 = client->IncrementMockTime();
1831 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10);
1832 client->ModifyOriginAndNotify(GURL("http://a.com:1/"), kTemp, 10);
1833 client->ModifyOriginAndNotify(GURL("http://b.com/"), kPerm, 10);
1834 base::Time time2 = client->IncrementMockTime();
1835 client->ModifyOriginAndNotify(GURL("https://a.com/"), kTemp, 10);
1836 client->ModifyOriginAndNotify(GURL("http://c.com/"), kTemp, 10);
1837 base::Time time3 = client->IncrementMockTime();
1839 GetOriginsModifiedSince(kTemp, time1);
1840 base::RunLoop().RunUntilIdle();
1841 EXPECT_EQ(4U, modified_origins().size());
1842 EXPECT_EQ(modified_origins_type(), kTemp);
1843 for (size_t i = 0; i < arraysize(kData); ++i) {
1844 if (kData[i].type == kTemp)
1845 EXPECT_EQ(1U, modified_origins().count(GURL(kData[i].origin)));
1848 GetOriginsModifiedSince(kTemp, time2);
1849 base::RunLoop().RunUntilIdle();
1850 EXPECT_EQ(2U, modified_origins().size());
1852 GetOriginsModifiedSince(kTemp, time3);
1853 base::RunLoop().RunUntilIdle();
1854 EXPECT_TRUE(modified_origins().empty());
1855 EXPECT_EQ(modified_origins_type(), kTemp);
1857 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10);
1859 GetOriginsModifiedSince(kTemp, time3);
1860 base::RunLoop().RunUntilIdle();
1861 EXPECT_EQ(1U, modified_origins().size());
1862 EXPECT_EQ(1U, modified_origins().count(GURL("http://a.com/")));
1863 EXPECT_EQ(modified_origins_type(), kTemp);
1866 TEST_F(QuotaManagerTest, DumpQuotaTable) {
1867 SetPersistentHostQuota("example1.com", 1);
1868 SetPersistentHostQuota("example2.com", 20);
1869 SetPersistentHostQuota("example3.com", 300);
1870 base::RunLoop().RunUntilIdle();
1872 DumpQuotaTable();
1873 base::RunLoop().RunUntilIdle();
1875 const QuotaTableEntry kEntries[] = {
1876 QuotaTableEntry("example1.com", kPerm, 1),
1877 QuotaTableEntry("example2.com", kPerm, 20),
1878 QuotaTableEntry("example3.com", kPerm, 300),
1880 std::set<QuotaTableEntry> entries(kEntries, kEntries + arraysize(kEntries));
1882 typedef QuotaTableEntries::const_iterator iterator;
1883 for (iterator itr(quota_entries().begin()), end(quota_entries().end());
1884 itr != end; ++itr) {
1885 SCOPED_TRACE(testing::Message()
1886 << "host = " << itr->host << ", "
1887 << "quota = " << itr->quota);
1888 EXPECT_EQ(1u, entries.erase(*itr));
1890 EXPECT_TRUE(entries.empty());
1893 TEST_F(QuotaManagerTest, DumpOriginInfoTable) {
1894 using std::make_pair;
1896 quota_manager()->NotifyStorageAccessed(
1897 QuotaClient::kUnknown,
1898 GURL("http://example.com/"),
1899 kTemp);
1900 quota_manager()->NotifyStorageAccessed(
1901 QuotaClient::kUnknown,
1902 GURL("http://example.com/"),
1903 kPerm);
1904 quota_manager()->NotifyStorageAccessed(
1905 QuotaClient::kUnknown,
1906 GURL("http://example.com/"),
1907 kPerm);
1908 base::RunLoop().RunUntilIdle();
1910 DumpOriginInfoTable();
1911 base::RunLoop().RunUntilIdle();
1913 typedef std::pair<GURL, StorageType> TypedOrigin;
1914 typedef std::pair<TypedOrigin, int> Entry;
1915 const Entry kEntries[] = {
1916 make_pair(make_pair(GURL("http://example.com/"), kTemp), 1),
1917 make_pair(make_pair(GURL("http://example.com/"), kPerm), 2),
1919 std::set<Entry> entries(kEntries, kEntries + arraysize(kEntries));
1921 typedef OriginInfoTableEntries::const_iterator iterator;
1922 for (iterator itr(origin_info_entries().begin()),
1923 end(origin_info_entries().end());
1924 itr != end; ++itr) {
1925 SCOPED_TRACE(testing::Message()
1926 << "host = " << itr->origin << ", "
1927 << "type = " << itr->type << ", "
1928 << "used_count = " << itr->used_count);
1929 EXPECT_EQ(1u, entries.erase(
1930 make_pair(make_pair(itr->origin, itr->type),
1931 itr->used_count)));
1933 EXPECT_TRUE(entries.empty());
1936 TEST_F(QuotaManagerTest, QuotaForEmptyHost) {
1937 GetPersistentHostQuota(std::string());
1938 base::RunLoop().RunUntilIdle();
1939 EXPECT_EQ(kQuotaStatusOk, status());
1940 EXPECT_EQ(0, quota());
1942 SetPersistentHostQuota(std::string(), 10);
1943 base::RunLoop().RunUntilIdle();
1944 EXPECT_EQ(kQuotaErrorNotSupported, status());
1947 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleOrigin) {
1948 static const MockOriginData kData1[] = {
1949 { "http://foo.com/", kTemp, 1 },
1951 static const MockOriginData kData2[] = {
1952 { "http://foo.com/", kTemp, 2 },
1954 static const MockOriginData kData3[] = {
1955 { "http://foo.com/", kTemp, 4 },
1957 static const MockOriginData kData4[] = {
1958 { "http://foo.com/", kTemp, 8 },
1960 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1961 QuotaClient::kFileSystem);
1962 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1963 QuotaClient::kAppcache);
1964 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
1965 QuotaClient::kDatabase);
1966 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
1967 QuotaClient::kIndexedDatabase);
1968 RegisterClient(client1);
1969 RegisterClient(client2);
1970 RegisterClient(client3);
1971 RegisterClient(client4);
1973 GetHostUsage("foo.com", kTemp);
1974 base::RunLoop().RunUntilIdle();
1975 const int64 predelete_foo_tmp = usage();
1977 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kFileSystem);
1978 base::RunLoop().RunUntilIdle();
1979 GetHostUsage("foo.com", kTemp);
1980 base::RunLoop().RunUntilIdle();
1981 EXPECT_EQ(predelete_foo_tmp - 1, usage());
1983 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kAppcache);
1984 base::RunLoop().RunUntilIdle();
1985 GetHostUsage("foo.com", kTemp);
1986 base::RunLoop().RunUntilIdle();
1987 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
1989 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kDatabase);
1990 base::RunLoop().RunUntilIdle();
1991 GetHostUsage("foo.com", kTemp);
1992 base::RunLoop().RunUntilIdle();
1993 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage());
1995 DeleteOriginData(GURL("http://foo.com/"), kTemp,
1996 QuotaClient::kIndexedDatabase);
1997 base::RunLoop().RunUntilIdle();
1998 GetHostUsage("foo.com", kTemp);
1999 base::RunLoop().RunUntilIdle();
2000 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2003 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleHost) {
2004 static const MockOriginData kData1[] = {
2005 { "http://foo.com:1111/", kTemp, 1 },
2007 static const MockOriginData kData2[] = {
2008 { "http://foo.com:2222/", kTemp, 2 },
2010 static const MockOriginData kData3[] = {
2011 { "http://foo.com:3333/", kTemp, 4 },
2013 static const MockOriginData kData4[] = {
2014 { "http://foo.com:4444/", kTemp, 8 },
2016 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2017 QuotaClient::kFileSystem);
2018 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2019 QuotaClient::kAppcache);
2020 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2021 QuotaClient::kDatabase);
2022 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2023 QuotaClient::kIndexedDatabase);
2024 RegisterClient(client1);
2025 RegisterClient(client2);
2026 RegisterClient(client3);
2027 RegisterClient(client4);
2029 GetHostUsage("foo.com", kTemp);
2030 base::RunLoop().RunUntilIdle();
2031 const int64 predelete_foo_tmp = usage();
2033 DeleteHostData("foo.com", kTemp, QuotaClient::kFileSystem);
2034 base::RunLoop().RunUntilIdle();
2035 GetHostUsage("foo.com", kTemp);
2036 base::RunLoop().RunUntilIdle();
2037 EXPECT_EQ(predelete_foo_tmp - 1, usage());
2039 DeleteHostData("foo.com", kTemp, QuotaClient::kAppcache);
2040 base::RunLoop().RunUntilIdle();
2041 GetHostUsage("foo.com", kTemp);
2042 base::RunLoop().RunUntilIdle();
2043 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
2045 DeleteHostData("foo.com", kTemp, QuotaClient::kDatabase);
2046 base::RunLoop().RunUntilIdle();
2047 GetHostUsage("foo.com", kTemp);
2048 base::RunLoop().RunUntilIdle();
2049 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage());
2051 DeleteHostData("foo.com", kTemp, QuotaClient::kIndexedDatabase);
2052 base::RunLoop().RunUntilIdle();
2053 GetHostUsage("foo.com", kTemp);
2054 base::RunLoop().RunUntilIdle();
2055 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2058 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleOrigin) {
2059 static const MockOriginData kData1[] = {
2060 { "http://foo.com/", kTemp, 1 },
2062 static const MockOriginData kData2[] = {
2063 { "http://foo.com/", kTemp, 2 },
2065 static const MockOriginData kData3[] = {
2066 { "http://foo.com/", kTemp, 4 },
2068 static const MockOriginData kData4[] = {
2069 { "http://foo.com/", kTemp, 8 },
2071 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2072 QuotaClient::kFileSystem);
2073 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2074 QuotaClient::kAppcache);
2075 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2076 QuotaClient::kDatabase);
2077 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2078 QuotaClient::kIndexedDatabase);
2079 RegisterClient(client1);
2080 RegisterClient(client2);
2081 RegisterClient(client3);
2082 RegisterClient(client4);
2084 GetHostUsage("foo.com", kTemp);
2085 base::RunLoop().RunUntilIdle();
2086 const int64 predelete_foo_tmp = usage();
2088 DeleteOriginData(GURL("http://foo.com/"), kTemp,
2089 QuotaClient::kFileSystem | QuotaClient::kDatabase);
2090 base::RunLoop().RunUntilIdle();
2091 GetHostUsage("foo.com", kTemp);
2092 base::RunLoop().RunUntilIdle();
2093 EXPECT_EQ(predelete_foo_tmp - 4 - 1, usage());
2095 DeleteOriginData(GURL("http://foo.com/"), kTemp,
2096 QuotaClient::kAppcache | QuotaClient::kIndexedDatabase);
2097 base::RunLoop().RunUntilIdle();
2098 GetHostUsage("foo.com", kTemp);
2099 base::RunLoop().RunUntilIdle();
2100 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2103 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleHost) {
2104 static const MockOriginData kData1[] = {
2105 { "http://foo.com:1111/", kTemp, 1 },
2107 static const MockOriginData kData2[] = {
2108 { "http://foo.com:2222/", kTemp, 2 },
2110 static const MockOriginData kData3[] = {
2111 { "http://foo.com:3333/", kTemp, 4 },
2113 static const MockOriginData kData4[] = {
2114 { "http://foo.com:4444/", kTemp, 8 },
2116 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2117 QuotaClient::kFileSystem);
2118 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2119 QuotaClient::kAppcache);
2120 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2121 QuotaClient::kDatabase);
2122 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2123 QuotaClient::kIndexedDatabase);
2124 RegisterClient(client1);
2125 RegisterClient(client2);
2126 RegisterClient(client3);
2127 RegisterClient(client4);
2129 GetHostUsage("foo.com", kTemp);
2130 base::RunLoop().RunUntilIdle();
2131 const int64 predelete_foo_tmp = usage();
2133 DeleteHostData("foo.com", kTemp,
2134 QuotaClient::kFileSystem | QuotaClient::kAppcache);
2135 base::RunLoop().RunUntilIdle();
2136 GetHostUsage("foo.com", kTemp);
2137 base::RunLoop().RunUntilIdle();
2138 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
2140 DeleteHostData("foo.com", kTemp,
2141 QuotaClient::kDatabase | QuotaClient::kIndexedDatabase);
2142 base::RunLoop().RunUntilIdle();
2143 GetHostUsage("foo.com", kTemp);
2144 base::RunLoop().RunUntilIdle();
2145 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2148 TEST_F(QuotaManagerTest, GetUsageAndQuota_Incognito) {
2149 ResetQuotaManager(true);
2151 static const MockOriginData kData[] = {
2152 { "http://foo.com/", kTemp, 10 },
2153 { "http://foo.com/", kPerm, 80 },
2155 RegisterClient(CreateClient(kData, arraysize(kData),
2156 QuotaClient::kFileSystem));
2158 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
2159 base::RunLoop().RunUntilIdle();
2160 EXPECT_EQ(kQuotaStatusOk, status());
2161 EXPECT_EQ(80, usage());
2162 EXPECT_EQ(0, quota());
2164 SetTemporaryGlobalQuota(100);
2165 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
2166 base::RunLoop().RunUntilIdle();
2167 EXPECT_EQ(kQuotaStatusOk, status());
2168 EXPECT_EQ(10, usage());
2169 EXPECT_LE(std::min(static_cast<int64>(100 / kPerHostTemporaryPortion),
2170 QuotaManager::kIncognitoDefaultQuotaLimit), quota());
2172 mock_special_storage_policy()->AddUnlimited(GURL("http://foo.com/"));
2173 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
2174 base::RunLoop().RunUntilIdle();
2175 EXPECT_EQ(kQuotaStatusOk, status());
2176 EXPECT_EQ(80, usage());
2177 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota());
2179 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
2180 base::RunLoop().RunUntilIdle();
2181 EXPECT_EQ(kQuotaStatusOk, status());
2182 EXPECT_EQ(10, usage());
2183 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota());
2186 } // namespace content