Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / quota / quota_manager_unittest.cc
blob1337bc389f47b0c0bc20759ab004a26d0493c4f4
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/message_loop/message_loop_proxy.h"
16 #include "base/run_loop.h"
17 #include "base/stl_util.h"
18 #include "base/sys_info.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 base::MessageLoopProxy;
29 using storage::kQuotaErrorAbort;
30 using storage::kQuotaErrorInvalidModification;
31 using storage::kQuotaErrorNotSupported;
32 using storage::kQuotaStatusOk;
33 using storage::kQuotaStatusUnknown;
34 using storage::kStorageTypePersistent;
35 using storage::kStorageTypeSyncable;
36 using storage::kStorageTypeTemporary;
37 using storage::kStorageTypeUnknown;
38 using storage::QuotaClient;
39 using storage::QuotaManager;
40 using storage::QuotaStatusCode;
41 using storage::StorageType;
42 using storage::UsageAndQuota;
43 using storage::UsageInfo;
44 using storage::UsageInfoEntries;
46 namespace content {
48 namespace {
50 // For shorter names.
51 const StorageType kTemp = kStorageTypeTemporary;
52 const StorageType kPerm = kStorageTypePersistent;
53 const StorageType kSync = kStorageTypeSyncable;
55 const int kAllClients = QuotaClient::kAllClientsMask;
57 const int64 kAvailableSpaceForApp = 13377331U;
59 const int64 kMinimumPreserveForSystem = QuotaManager::kMinimumPreserveForSystem;
60 const int kPerHostTemporaryPortion = QuotaManager::kPerHostTemporaryPortion;
62 // Returns a deterministic value for the amount of available disk space.
63 int64 GetAvailableDiskSpaceForTest(const base::FilePath&) {
64 return kAvailableSpaceForApp + kMinimumPreserveForSystem;
67 } // namespace
69 class QuotaManagerTest : public testing::Test {
70 protected:
71 typedef QuotaManager::QuotaTableEntry QuotaTableEntry;
72 typedef QuotaManager::QuotaTableEntries QuotaTableEntries;
73 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries;
75 public:
76 QuotaManagerTest()
77 : mock_time_counter_(0),
78 weak_factory_(this) {
81 void SetUp() override {
82 ASSERT_TRUE(data_dir_.CreateUniqueTempDir());
83 mock_special_storage_policy_ = new MockSpecialStoragePolicy;
84 ResetQuotaManager(false /* is_incognito */);
87 void TearDown() override {
88 // Make sure the quota manager cleans up correctly.
89 quota_manager_ = NULL;
90 base::RunLoop().RunUntilIdle();
93 protected:
94 void ResetQuotaManager(bool is_incognito) {
95 quota_manager_ = new QuotaManager(is_incognito,
96 data_dir_.path(),
97 MessageLoopProxy::current().get(),
98 MessageLoopProxy::current().get(),
99 mock_special_storage_policy_.get());
100 // Don't (automatically) start the eviction for testing.
101 quota_manager_->eviction_disabled_ = true;
102 // Don't query the hard disk for remaining capacity.
103 quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest;
104 additional_callback_count_ = 0;
107 MockStorageClient* CreateClient(
108 const MockOriginData* mock_data,
109 size_t mock_data_size,
110 QuotaClient::ID id) {
111 return new MockStorageClient(quota_manager_->proxy(),
112 mock_data, id, mock_data_size);
115 void RegisterClient(MockStorageClient* client) {
116 quota_manager_->proxy()->RegisterClient(client);
119 void GetUsageInfo() {
120 usage_info_.clear();
121 quota_manager_->GetUsageInfo(
122 base::Bind(&QuotaManagerTest::DidGetUsageInfo,
123 weak_factory_.GetWeakPtr()));
126 void GetUsageAndQuotaForWebApps(const GURL& origin,
127 StorageType type) {
128 quota_status_ = kQuotaStatusUnknown;
129 usage_ = -1;
130 quota_ = -1;
131 quota_manager_->GetUsageAndQuotaForWebApps(
132 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota,
133 weak_factory_.GetWeakPtr()));
136 void GetUsageAndQuotaForStorageClient(const GURL& origin,
137 StorageType type) {
138 quota_status_ = kQuotaStatusUnknown;
139 usage_ = -1;
140 quota_ = -1;
141 quota_manager_->GetUsageAndQuota(
142 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota,
143 weak_factory_.GetWeakPtr()));
146 void GetTemporaryGlobalQuota() {
147 quota_status_ = kQuotaStatusUnknown;
148 quota_ = -1;
149 quota_manager_->GetTemporaryGlobalQuota(
150 base::Bind(&QuotaManagerTest::DidGetQuota,
151 weak_factory_.GetWeakPtr()));
154 void SetTemporaryGlobalQuota(int64 new_quota) {
155 quota_status_ = kQuotaStatusUnknown;
156 quota_ = -1;
157 quota_manager_->SetTemporaryGlobalOverrideQuota(
158 new_quota,
159 base::Bind(&QuotaManagerTest::DidGetQuota,
160 weak_factory_.GetWeakPtr()));
163 void GetPersistentHostQuota(const std::string& host) {
164 quota_status_ = kQuotaStatusUnknown;
165 quota_ = -1;
166 quota_manager_->GetPersistentHostQuota(
167 host,
168 base::Bind(&QuotaManagerTest::DidGetHostQuota,
169 weak_factory_.GetWeakPtr()));
172 void SetPersistentHostQuota(const std::string& host, int64 new_quota) {
173 quota_status_ = kQuotaStatusUnknown;
174 quota_ = -1;
175 quota_manager_->SetPersistentHostQuota(
176 host, new_quota,
177 base::Bind(&QuotaManagerTest::DidGetHostQuota,
178 weak_factory_.GetWeakPtr()));
181 void GetGlobalUsage(StorageType type) {
182 usage_ = -1;
183 unlimited_usage_ = -1;
184 quota_manager_->GetGlobalUsage(
185 type,
186 base::Bind(&QuotaManagerTest::DidGetGlobalUsage,
187 weak_factory_.GetWeakPtr()));
190 void GetHostUsage(const std::string& host, StorageType type) {
191 usage_ = -1;
192 quota_manager_->GetHostUsage(
193 host, type,
194 base::Bind(&QuotaManagerTest::DidGetHostUsage,
195 weak_factory_.GetWeakPtr()));
198 void RunAdditionalUsageAndQuotaTask(const GURL& origin, StorageType type) {
199 quota_manager_->GetUsageAndQuota(
200 origin, type,
201 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaAdditional,
202 weak_factory_.GetWeakPtr()));
205 void DeleteClientOriginData(QuotaClient* client,
206 const GURL& origin,
207 StorageType type) {
208 DCHECK(client);
209 quota_status_ = kQuotaStatusUnknown;
210 client->DeleteOriginData(
211 origin, type,
212 base::Bind(&QuotaManagerTest::StatusCallback,
213 weak_factory_.GetWeakPtr()));
216 void EvictOriginData(const GURL& origin,
217 StorageType type) {
218 quota_status_ = kQuotaStatusUnknown;
219 quota_manager_->EvictOriginData(
220 origin, type,
221 base::Bind(&QuotaManagerTest::StatusCallback,
222 weak_factory_.GetWeakPtr()));
225 void DeleteOriginData(const GURL& origin,
226 StorageType type,
227 int quota_client_mask) {
228 quota_status_ = kQuotaStatusUnknown;
229 quota_manager_->DeleteOriginData(
230 origin, type, quota_client_mask,
231 base::Bind(&QuotaManagerTest::StatusCallback,
232 weak_factory_.GetWeakPtr()));
235 void DeleteHostData(const std::string& host,
236 StorageType type,
237 int quota_client_mask) {
238 quota_status_ = kQuotaStatusUnknown;
239 quota_manager_->DeleteHostData(
240 host, type, quota_client_mask,
241 base::Bind(&QuotaManagerTest::StatusCallback,
242 weak_factory_.GetWeakPtr()));
245 void GetAvailableSpace() {
246 quota_status_ = kQuotaStatusUnknown;
247 available_space_ = -1;
248 quota_manager_->GetAvailableSpace(
249 base::Bind(&QuotaManagerTest::DidGetAvailableSpace,
250 weak_factory_.GetWeakPtr()));
253 void GetUsageAndQuotaForEviction() {
254 quota_status_ = kQuotaStatusUnknown;
255 usage_ = -1;
256 unlimited_usage_ = -1;
257 quota_ = -1;
258 available_space_ = -1;
259 quota_manager_->GetUsageAndQuotaForEviction(
260 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaForEviction,
261 weak_factory_.GetWeakPtr()));
264 void GetCachedOrigins(StorageType type, std::set<GURL>* origins) {
265 ASSERT_TRUE(origins != NULL);
266 origins->clear();
267 quota_manager_->GetCachedOrigins(type, origins);
270 void NotifyStorageAccessed(QuotaClient* client,
271 const GURL& origin,
272 StorageType type) {
273 DCHECK(client);
274 quota_manager_->NotifyStorageAccessedInternal(
275 client->id(), origin, type, IncrementMockTime());
278 void DeleteOriginFromDatabase(const GURL& origin, StorageType type) {
279 quota_manager_->DeleteOriginFromDatabase(origin, type);
282 void GetLRUOrigin(StorageType type) {
283 lru_origin_ = GURL();
284 quota_manager_->GetLRUOrigin(
285 type,
286 base::Bind(&QuotaManagerTest::DidGetLRUOrigin,
287 weak_factory_.GetWeakPtr()));
290 void NotifyOriginInUse(const GURL& origin) {
291 quota_manager_->NotifyOriginInUse(origin);
294 void NotifyOriginNoLongerInUse(const GURL& origin) {
295 quota_manager_->NotifyOriginNoLongerInUse(origin);
298 void GetOriginsModifiedSince(StorageType type, base::Time modified_since) {
299 modified_origins_.clear();
300 modified_origins_type_ = kStorageTypeUnknown;
301 quota_manager_->GetOriginsModifiedSince(
302 type, modified_since,
303 base::Bind(&QuotaManagerTest::DidGetModifiedOrigins,
304 weak_factory_.GetWeakPtr()));
307 void DumpQuotaTable() {
308 quota_entries_.clear();
309 quota_manager_->DumpQuotaTable(
310 base::Bind(&QuotaManagerTest::DidDumpQuotaTable,
311 weak_factory_.GetWeakPtr()));
314 void DumpOriginInfoTable() {
315 origin_info_entries_.clear();
316 quota_manager_->DumpOriginInfoTable(
317 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable,
318 weak_factory_.GetWeakPtr()));
321 void DidGetUsageInfo(const UsageInfoEntries& entries) {
322 usage_info_.insert(usage_info_.begin(), entries.begin(), entries.end());
325 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) {
326 quota_status_ = status;
327 usage_ = usage;
328 quota_ = quota;
331 void DidGetQuota(QuotaStatusCode status,
332 int64 quota) {
333 quota_status_ = status;
334 quota_ = quota;
337 void DidGetAvailableSpace(QuotaStatusCode status, int64 available_space) {
338 quota_status_ = status;
339 available_space_ = available_space;
342 void DidGetHostQuota(QuotaStatusCode status,
343 int64 quota) {
344 quota_status_ = status;
345 quota_ = quota;
348 void DidGetGlobalUsage(int64 usage,
349 int64 unlimited_usage) {
350 usage_ = usage;
351 unlimited_usage_ = unlimited_usage;
354 void DidGetHostUsage(int64 usage) {
355 usage_ = usage;
358 void StatusCallback(QuotaStatusCode status) {
359 ++status_callback_count_;
360 quota_status_ = status;
363 void DidGetUsageAndQuotaForEviction(QuotaStatusCode status,
364 const UsageAndQuota& usage_and_quota) {
365 quota_status_ = status;
366 limited_usage_ = usage_and_quota.global_limited_usage;
367 quota_ = usage_and_quota.quota;
368 available_space_ = usage_and_quota.available_disk_space;
371 void DidGetLRUOrigin(const GURL& origin) {
372 lru_origin_ = origin;
375 void DidGetModifiedOrigins(const std::set<GURL>& origins, StorageType type) {
376 modified_origins_ = origins;
377 modified_origins_type_ = type;
380 void DidDumpQuotaTable(const QuotaTableEntries& entries) {
381 quota_entries_ = entries;
384 void DidDumpOriginInfoTable(const OriginInfoTableEntries& entries) {
385 origin_info_entries_ = entries;
388 void GetUsage_WithModifyTestBody(const StorageType type);
390 void set_additional_callback_count(int c) { additional_callback_count_ = c; }
391 int additional_callback_count() const { return additional_callback_count_; }
392 void DidGetUsageAndQuotaAdditional(
393 QuotaStatusCode status, int64 usage, int64 quota) {
394 ++additional_callback_count_;
397 QuotaManager* quota_manager() const { return quota_manager_.get(); }
398 void set_quota_manager(QuotaManager* quota_manager) {
399 quota_manager_ = quota_manager;
402 MockSpecialStoragePolicy* mock_special_storage_policy() const {
403 return mock_special_storage_policy_.get();
406 QuotaStatusCode status() const { return quota_status_; }
407 const UsageInfoEntries& usage_info() const { return usage_info_; }
408 int64 usage() const { return usage_; }
409 int64 limited_usage() const { return limited_usage_; }
410 int64 unlimited_usage() const { return unlimited_usage_; }
411 int64 quota() const { return quota_; }
412 int64 available_space() const { return available_space_; }
413 const GURL& lru_origin() const { return lru_origin_; }
414 const std::set<GURL>& modified_origins() const { return modified_origins_; }
415 StorageType modified_origins_type() const { return modified_origins_type_; }
416 const QuotaTableEntries& quota_entries() const { return quota_entries_; }
417 const OriginInfoTableEntries& origin_info_entries() const {
418 return origin_info_entries_;
420 base::FilePath profile_path() const { return data_dir_.path(); }
421 int status_callback_count() const { return status_callback_count_; }
422 void reset_status_callback_count() { status_callback_count_ = 0; }
424 private:
425 base::Time IncrementMockTime() {
426 ++mock_time_counter_;
427 return base::Time::FromDoubleT(mock_time_counter_ * 10.0);
430 base::MessageLoop message_loop_;
431 base::ScopedTempDir data_dir_;
433 scoped_refptr<QuotaManager> quota_manager_;
434 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_;
436 QuotaStatusCode quota_status_;
437 UsageInfoEntries usage_info_;
438 int64 usage_;
439 int64 limited_usage_;
440 int64 unlimited_usage_;
441 int64 quota_;
442 int64 available_space_;
443 GURL lru_origin_;
444 std::set<GURL> modified_origins_;
445 StorageType modified_origins_type_;
446 QuotaTableEntries quota_entries_;
447 OriginInfoTableEntries origin_info_entries_;
448 int status_callback_count_;
450 int additional_callback_count_;
452 int mock_time_counter_;
454 base::WeakPtrFactory<QuotaManagerTest> weak_factory_;
456 DISALLOW_COPY_AND_ASSIGN(QuotaManagerTest);
459 TEST_F(QuotaManagerTest, GetUsageInfo) {
460 static const MockOriginData kData1[] = {
461 { "http://foo.com/", kTemp, 10 },
462 { "http://foo.com:8080/", kTemp, 15 },
463 { "http://bar.com/", kTemp, 20 },
464 { "http://bar.com/", kPerm, 50 },
466 static const MockOriginData kData2[] = {
467 { "https://foo.com/", kTemp, 30 },
468 { "https://foo.com:8081/", kTemp, 35 },
469 { "http://bar.com/", kPerm, 40 },
470 { "http://example.com/", kPerm, 40 },
472 RegisterClient(CreateClient(kData1, arraysize(kData1),
473 QuotaClient::kFileSystem));
474 RegisterClient(CreateClient(kData2, arraysize(kData2),
475 QuotaClient::kDatabase));
477 GetUsageInfo();
478 base::RunLoop().RunUntilIdle();
480 EXPECT_EQ(4U, usage_info().size());
481 for (size_t i = 0; i < usage_info().size(); ++i) {
482 const UsageInfo& info = usage_info()[i];
483 if (info.host == "foo.com" && info.type == kTemp) {
484 EXPECT_EQ(10 + 15 + 30 + 35, info.usage);
485 } else if (info.host == "bar.com" && info.type == kTemp) {
486 EXPECT_EQ(20, info.usage);
487 } else if (info.host == "bar.com" && info.type == kPerm) {
488 EXPECT_EQ(50 + 40, info.usage);
489 } else if (info.host == "example.com" && info.type == kPerm) {
490 EXPECT_EQ(40, info.usage);
491 } else {
492 ADD_FAILURE()
493 << "Unexpected host, type: " << info.host << ", " << info.type;
498 TEST_F(QuotaManagerTest, GetUsageAndQuota_Simple) {
499 static const MockOriginData kData[] = {
500 { "http://foo.com/", kTemp, 10 },
501 { "http://foo.com/", kPerm, 80 },
503 RegisterClient(CreateClient(kData, arraysize(kData),
504 QuotaClient::kFileSystem));
506 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
507 base::RunLoop().RunUntilIdle();
508 EXPECT_EQ(kQuotaStatusOk, status());
509 EXPECT_EQ(80, usage());
510 EXPECT_EQ(0, quota());
512 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
513 base::RunLoop().RunUntilIdle();
514 EXPECT_EQ(kQuotaStatusOk, status());
515 EXPECT_EQ(10, usage());
516 EXPECT_LE(0, quota());
517 int64 quota_returned_for_foo = quota();
519 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp);
520 base::RunLoop().RunUntilIdle();
521 EXPECT_EQ(kQuotaStatusOk, status());
522 EXPECT_EQ(0, usage());
523 EXPECT_EQ(quota_returned_for_foo, quota());
526 TEST_F(QuotaManagerTest, GetUsage_NoClient) {
527 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
528 base::RunLoop().RunUntilIdle();
529 EXPECT_EQ(kQuotaStatusOk, status());
530 EXPECT_EQ(0, usage());
532 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
533 base::RunLoop().RunUntilIdle();
534 EXPECT_EQ(kQuotaStatusOk, status());
535 EXPECT_EQ(0, usage());
537 GetHostUsage("foo.com", kTemp);
538 base::RunLoop().RunUntilIdle();
539 EXPECT_EQ(0, usage());
541 GetHostUsage("foo.com", kPerm);
542 base::RunLoop().RunUntilIdle();
543 EXPECT_EQ(0, usage());
545 GetGlobalUsage(kTemp);
546 base::RunLoop().RunUntilIdle();
547 EXPECT_EQ(0, usage());
548 EXPECT_EQ(0, unlimited_usage());
550 GetGlobalUsage(kPerm);
551 base::RunLoop().RunUntilIdle();
552 EXPECT_EQ(0, usage());
553 EXPECT_EQ(0, unlimited_usage());
556 TEST_F(QuotaManagerTest, GetUsage_EmptyClient) {
557 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
558 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
559 base::RunLoop().RunUntilIdle();
560 EXPECT_EQ(kQuotaStatusOk, status());
561 EXPECT_EQ(0, usage());
563 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
564 base::RunLoop().RunUntilIdle();
565 EXPECT_EQ(kQuotaStatusOk, status());
566 EXPECT_EQ(0, usage());
568 GetHostUsage("foo.com", kTemp);
569 base::RunLoop().RunUntilIdle();
570 EXPECT_EQ(0, usage());
572 GetHostUsage("foo.com", kPerm);
573 base::RunLoop().RunUntilIdle();
574 EXPECT_EQ(0, usage());
576 GetGlobalUsage(kTemp);
577 base::RunLoop().RunUntilIdle();
578 EXPECT_EQ(0, usage());
579 EXPECT_EQ(0, unlimited_usage());
581 GetGlobalUsage(kPerm);
582 base::RunLoop().RunUntilIdle();
583 EXPECT_EQ(0, usage());
584 EXPECT_EQ(0, unlimited_usage());
587 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_MultiOrigins) {
588 static const MockOriginData kData[] = {
589 { "http://foo.com/", kTemp, 10 },
590 { "http://foo.com:8080/", kTemp, 20 },
591 { "http://bar.com/", kTemp, 5 },
592 { "https://bar.com/", kTemp, 7 },
593 { "http://baz.com/", kTemp, 30 },
594 { "http://foo.com/", kPerm, 40 },
596 RegisterClient(CreateClient(kData, arraysize(kData),
597 QuotaClient::kFileSystem));
599 // This time explicitly sets a temporary global quota.
600 SetTemporaryGlobalQuota(100);
601 base::RunLoop().RunUntilIdle();
602 EXPECT_EQ(kQuotaStatusOk, status());
603 EXPECT_EQ(100, quota());
605 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
606 base::RunLoop().RunUntilIdle();
607 EXPECT_EQ(kQuotaStatusOk, status());
608 EXPECT_EQ(10 + 20, usage());
610 const int kPerHostQuota = 100 / kPerHostTemporaryPortion;
612 // The host's quota should be its full portion of the global quota
613 // since global usage is under the global quota.
614 EXPECT_EQ(kPerHostQuota, quota());
616 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp);
617 base::RunLoop().RunUntilIdle();
618 EXPECT_EQ(kQuotaStatusOk, status());
619 EXPECT_EQ(5 + 7, usage());
620 EXPECT_EQ(kPerHostQuota, quota());
623 TEST_F(QuotaManagerTest, GetUsage_MultipleClients) {
624 static const MockOriginData kData1[] = {
625 { "http://foo.com/", kTemp, 1 },
626 { "http://bar.com/", kTemp, 2 },
627 { "http://bar.com/", kPerm, 4 },
628 { "http://unlimited/", kPerm, 8 },
629 { "http://installed/", kPerm, 16 },
631 static const MockOriginData kData2[] = {
632 { "https://foo.com/", kTemp, 128 },
633 { "http://example.com/", kPerm, 256 },
634 { "http://unlimited/", kTemp, 512 },
635 { "http://installed/", kTemp, 1024 },
637 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
638 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
639 RegisterClient(CreateClient(kData1, arraysize(kData1),
640 QuotaClient::kFileSystem));
641 RegisterClient(CreateClient(kData2, arraysize(kData2),
642 QuotaClient::kDatabase));
644 const int64 kTempQuotaBase =
645 GetAvailableDiskSpaceForTest(base::FilePath()) / kPerHostTemporaryPortion;
647 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
648 base::RunLoop().RunUntilIdle();
649 EXPECT_EQ(kQuotaStatusOk, status());
650 EXPECT_EQ(1 + 128, usage());
652 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kPerm);
653 base::RunLoop().RunUntilIdle();
654 EXPECT_EQ(kQuotaStatusOk, status());
655 EXPECT_EQ(4, usage());
657 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
658 base::RunLoop().RunUntilIdle();
659 EXPECT_EQ(kQuotaStatusOk, status());
660 EXPECT_EQ(512, usage());
661 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota());
663 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm);
664 base::RunLoop().RunUntilIdle();
665 EXPECT_EQ(kQuotaStatusOk, status());
666 EXPECT_EQ(8, usage());
667 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
669 GetAvailableSpace();
670 base::RunLoop().RunUntilIdle();
671 EXPECT_EQ(kQuotaStatusOk, status());
672 EXPECT_LE(0, available_space());
674 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kTemp);
675 base::RunLoop().RunUntilIdle();
676 EXPECT_EQ(kQuotaStatusOk, status());
677 EXPECT_EQ(1024, usage());
678 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota());
680 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm);
681 base::RunLoop().RunUntilIdle();
682 EXPECT_EQ(kQuotaStatusOk, status());
683 EXPECT_EQ(16, usage());
684 EXPECT_EQ(usage(), quota()); // Over-budget case.
686 GetGlobalUsage(kTemp);
687 base::RunLoop().RunUntilIdle();
688 EXPECT_EQ(kQuotaStatusOk, status());
689 EXPECT_EQ(1 + 2 + 128 + 512 + 1024, usage());
690 EXPECT_EQ(512, unlimited_usage());
692 GetGlobalUsage(kPerm);
693 base::RunLoop().RunUntilIdle();
694 EXPECT_EQ(kQuotaStatusOk, status());
695 EXPECT_EQ(4 + 8 + 16 + 256, usage());
696 EXPECT_EQ(8, unlimited_usage());
699 void QuotaManagerTest::GetUsage_WithModifyTestBody(const StorageType type) {
700 const MockOriginData data[] = {
701 { "http://foo.com/", type, 10 },
702 { "http://foo.com:1/", type, 20 },
704 MockStorageClient* client = CreateClient(data, arraysize(data),
705 QuotaClient::kFileSystem);
706 RegisterClient(client);
708 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type);
709 base::RunLoop().RunUntilIdle();
710 EXPECT_EQ(kQuotaStatusOk, status());
711 EXPECT_EQ(10 + 20, usage());
713 client->ModifyOriginAndNotify(GURL("http://foo.com/"), type, 30);
714 client->ModifyOriginAndNotify(GURL("http://foo.com:1/"), type, -5);
715 client->AddOriginAndNotify(GURL("https://foo.com/"), type, 1);
717 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type);
718 base::RunLoop().RunUntilIdle();
719 EXPECT_EQ(kQuotaStatusOk, status());
720 EXPECT_EQ(10 + 20 + 30 - 5 + 1, usage());
721 int foo_usage = usage();
723 client->AddOriginAndNotify(GURL("http://bar.com/"), type, 40);
724 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), type);
725 base::RunLoop().RunUntilIdle();
726 EXPECT_EQ(kQuotaStatusOk, status());
727 EXPECT_EQ(40, usage());
729 GetGlobalUsage(type);
730 base::RunLoop().RunUntilIdle();
731 EXPECT_EQ(foo_usage + 40, usage());
732 EXPECT_EQ(0, unlimited_usage());
735 TEST_F(QuotaManagerTest, GetTemporaryUsage_WithModify) {
736 GetUsage_WithModifyTestBody(kTemp);
739 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_WithAdditionalTasks) {
740 static const MockOriginData kData[] = {
741 { "http://foo.com/", kTemp, 10 },
742 { "http://foo.com:8080/", kTemp, 20 },
743 { "http://bar.com/", kTemp, 13 },
744 { "http://foo.com/", kPerm, 40 },
746 RegisterClient(CreateClient(kData, arraysize(kData),
747 QuotaClient::kFileSystem));
748 SetTemporaryGlobalQuota(100);
749 base::RunLoop().RunUntilIdle();
751 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion;
753 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
754 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
755 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
756 base::RunLoop().RunUntilIdle();
757 EXPECT_EQ(kQuotaStatusOk, status());
758 EXPECT_EQ(10 + 20, usage());
759 EXPECT_EQ(kPerHostQuota, quota());
761 set_additional_callback_count(0);
762 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
763 kTemp);
764 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
765 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kTemp);
766 base::RunLoop().RunUntilIdle();
767 EXPECT_EQ(kQuotaStatusOk, status());
768 EXPECT_EQ(10 + 20, usage());
769 EXPECT_EQ(kPerHostQuota, quota());
770 EXPECT_EQ(2, additional_callback_count());
773 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_NukeManager) {
774 static const MockOriginData kData[] = {
775 { "http://foo.com/", kTemp, 10 },
776 { "http://foo.com:8080/", kTemp, 20 },
777 { "http://bar.com/", kTemp, 13 },
778 { "http://foo.com/", kPerm, 40 },
780 RegisterClient(CreateClient(kData, arraysize(kData),
781 QuotaClient::kFileSystem));
782 SetTemporaryGlobalQuota(100);
783 base::RunLoop().RunUntilIdle();
785 set_additional_callback_count(0);
786 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
787 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
788 kTemp);
789 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"),
790 kTemp);
792 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
793 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients);
795 // Nuke before waiting for callbacks.
796 set_quota_manager(NULL);
797 base::RunLoop().RunUntilIdle();
798 EXPECT_EQ(kQuotaErrorAbort, status());
801 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Overbudget) {
802 static const MockOriginData kData[] = {
803 { "http://usage1/", kTemp, 1 },
804 { "http://usage10/", kTemp, 10 },
805 { "http://usage200/", kTemp, 200 },
807 RegisterClient(CreateClient(kData, arraysize(kData),
808 QuotaClient::kFileSystem));
809 SetTemporaryGlobalQuota(100);
810 base::RunLoop().RunUntilIdle();
812 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion;
814 GetUsageAndQuotaForWebApps(GURL("http://usage1/"), kTemp);
815 base::RunLoop().RunUntilIdle();
816 EXPECT_EQ(kQuotaStatusOk, status());
817 EXPECT_EQ(1, usage());
818 EXPECT_EQ(1, quota()); // should be clamped to our current usage
820 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
821 base::RunLoop().RunUntilIdle();
822 EXPECT_EQ(kQuotaStatusOk, status());
823 EXPECT_EQ(10, usage());
824 EXPECT_EQ(10, quota());
826 GetUsageAndQuotaForWebApps(GURL("http://usage200/"), kTemp);
827 base::RunLoop().RunUntilIdle();
828 EXPECT_EQ(kQuotaStatusOk, status());
829 EXPECT_EQ(200, usage());
830 EXPECT_EQ(kPerHostQuota, quota()); // should be clamped to the nominal quota
833 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Unlimited) {
834 static const MockOriginData kData[] = {
835 { "http://usage10/", kTemp, 10 },
836 { "http://usage50/", kTemp, 50 },
837 { "http://unlimited/", kTemp, 4000 },
839 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
840 MockStorageClient* client = CreateClient(kData, arraysize(kData),
841 QuotaClient::kFileSystem);
842 RegisterClient(client);
844 // Test when not overbugdet.
845 SetTemporaryGlobalQuota(1000);
846 base::RunLoop().RunUntilIdle();
848 GetGlobalUsage(kTemp);
849 base::RunLoop().RunUntilIdle();
850 EXPECT_EQ(10 + 50 + 4000, usage());
851 EXPECT_EQ(4000, unlimited_usage());
853 const int kPerHostQuotaFor1000 =
854 1000 / QuotaManager::kPerHostTemporaryPortion;
856 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
857 base::RunLoop().RunUntilIdle();
858 EXPECT_EQ(kQuotaStatusOk, status());
859 EXPECT_EQ(10, usage());
860 EXPECT_EQ(kPerHostQuotaFor1000, quota());
862 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
863 base::RunLoop().RunUntilIdle();
864 EXPECT_EQ(kQuotaStatusOk, status());
865 EXPECT_EQ(50, usage());
866 EXPECT_EQ(kPerHostQuotaFor1000, quota());
868 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
869 base::RunLoop().RunUntilIdle();
870 EXPECT_EQ(kQuotaStatusOk, status());
871 EXPECT_EQ(4000, usage());
872 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
874 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
875 base::RunLoop().RunUntilIdle();
876 EXPECT_EQ(kQuotaStatusOk, status());
877 EXPECT_EQ(0, usage());
878 EXPECT_EQ(QuotaManager::kNoLimit, quota());
880 // Test when overbugdet.
881 SetTemporaryGlobalQuota(100);
882 base::RunLoop().RunUntilIdle();
884 const int kPerHostQuotaFor100 =
885 100 / QuotaManager::kPerHostTemporaryPortion;
887 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
888 base::RunLoop().RunUntilIdle();
889 EXPECT_EQ(kQuotaStatusOk, status());
890 EXPECT_EQ(10, usage());
891 EXPECT_EQ(kPerHostQuotaFor100, quota());
893 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
894 base::RunLoop().RunUntilIdle();
895 EXPECT_EQ(kQuotaStatusOk, status());
896 EXPECT_EQ(50, usage());
897 EXPECT_EQ(kPerHostQuotaFor100, quota());
899 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
900 base::RunLoop().RunUntilIdle();
901 EXPECT_EQ(kQuotaStatusOk, status());
902 EXPECT_EQ(4000, usage());
903 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota());
905 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
906 base::RunLoop().RunUntilIdle();
907 EXPECT_EQ(kQuotaStatusOk, status());
908 EXPECT_EQ(0, usage());
909 EXPECT_EQ(QuotaManager::kNoLimit, quota());
911 // Revoke the unlimited rights and make sure the change is noticed.
912 mock_special_storage_policy()->Reset();
913 mock_special_storage_policy()->NotifyCleared();
915 GetGlobalUsage(kTemp);
916 base::RunLoop().RunUntilIdle();
917 EXPECT_EQ(10 + 50 + 4000, usage());
918 EXPECT_EQ(0, unlimited_usage());
920 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp);
921 base::RunLoop().RunUntilIdle();
922 EXPECT_EQ(kQuotaStatusOk, status());
923 EXPECT_EQ(10, usage());
924 EXPECT_EQ(10, quota()); // should be clamped to our current usage
926 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp);
927 base::RunLoop().RunUntilIdle();
928 EXPECT_EQ(kQuotaStatusOk, status());
929 EXPECT_EQ(50, usage());
930 EXPECT_EQ(kPerHostQuotaFor100, quota());
932 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp);
933 base::RunLoop().RunUntilIdle();
934 EXPECT_EQ(kQuotaStatusOk, status());
935 EXPECT_EQ(4000, usage());
936 EXPECT_EQ(kPerHostQuotaFor100, quota());
938 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp);
939 base::RunLoop().RunUntilIdle();
940 EXPECT_EQ(kQuotaStatusOk, status());
941 EXPECT_EQ(4000, usage());
942 EXPECT_EQ(kPerHostQuotaFor100, quota());
945 TEST_F(QuotaManagerTest, OriginInUse) {
946 const GURL kFooOrigin("http://foo.com/");
947 const GURL kBarOrigin("http://bar.com/");
949 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin));
950 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 1
951 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
952 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 2
953 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
954 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin); // count of 1
955 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin));
957 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin));
958 quota_manager()->NotifyOriginInUse(kBarOrigin);
959 EXPECT_TRUE(quota_manager()->IsOriginInUse(kBarOrigin));
960 quota_manager()->NotifyOriginNoLongerInUse(kBarOrigin);
961 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin));
963 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin);
964 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin));
967 TEST_F(QuotaManagerTest, GetAndSetPerststentHostQuota) {
968 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
970 GetPersistentHostQuota("foo.com");
971 base::RunLoop().RunUntilIdle();
972 EXPECT_EQ(0, quota());
974 SetPersistentHostQuota("foo.com", 100);
975 base::RunLoop().RunUntilIdle();
976 EXPECT_EQ(100, quota());
978 GetPersistentHostQuota("foo.com");
979 SetPersistentHostQuota("foo.com", 200);
980 GetPersistentHostQuota("foo.com");
981 SetPersistentHostQuota("foo.com", QuotaManager::kPerHostPersistentQuotaLimit);
982 GetPersistentHostQuota("foo.com");
983 base::RunLoop().RunUntilIdle();
984 EXPECT_EQ(QuotaManager::kPerHostPersistentQuotaLimit, quota());
986 // Persistent quota should be capped at the per-host quota limit.
987 SetPersistentHostQuota("foo.com",
988 QuotaManager::kPerHostPersistentQuotaLimit + 100);
989 GetPersistentHostQuota("foo.com");
990 base::RunLoop().RunUntilIdle();
991 EXPECT_EQ(QuotaManager::kPerHostPersistentQuotaLimit, quota());
994 TEST_F(QuotaManagerTest, GetAndSetPersistentUsageAndQuota) {
995 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
997 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
998 base::RunLoop().RunUntilIdle();
999 EXPECT_EQ(kQuotaStatusOk, status());
1000 EXPECT_EQ(0, usage());
1001 EXPECT_EQ(0, quota());
1003 SetPersistentHostQuota("foo.com", 100);
1004 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1005 base::RunLoop().RunUntilIdle();
1006 EXPECT_EQ(kQuotaStatusOk, status());
1007 EXPECT_EQ(0, usage());
1008 EXPECT_EQ(100, quota());
1010 // For installed app GetUsageAndQuotaForWebApps returns the capped quota.
1011 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
1012 SetPersistentHostQuota("installed", kAvailableSpaceForApp + 100);
1013 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm);
1014 base::RunLoop().RunUntilIdle();
1015 EXPECT_EQ(kAvailableSpaceForApp, quota());
1017 // Ditto for unlimited apps.
1018 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
1019 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm);
1020 base::RunLoop().RunUntilIdle();
1021 EXPECT_EQ(kAvailableSpaceForApp, quota());
1023 // GetUsageAndQuotaForStorageClient should just return 0 usage and
1024 // kNoLimit quota.
1025 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kPerm);
1026 base::RunLoop().RunUntilIdle();
1027 EXPECT_EQ(0, usage());
1028 EXPECT_EQ(QuotaManager::kNoLimit, quota());
1031 TEST_F(QuotaManagerTest, GetSyncableQuota) {
1032 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem));
1034 // Pre-condition check: available disk space (for testing) is less than
1035 // the default quota for syncable storage.
1036 EXPECT_LE(kAvailableSpaceForApp,
1037 QuotaManager::kSyncableStorageDefaultHostQuota);
1039 // For installed apps the quota manager should return
1040 // kAvailableSpaceForApp as syncable quota (because of the pre-condition).
1041 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/"));
1042 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kSync);
1043 base::RunLoop().RunUntilIdle();
1044 EXPECT_EQ(kQuotaStatusOk, status());
1045 EXPECT_EQ(0, usage());
1046 EXPECT_EQ(kAvailableSpaceForApp, quota());
1048 // If it's not installed (which shouldn't happen in real case) it
1049 // should just return the default host quota for syncable.
1050 GetUsageAndQuotaForWebApps(GURL("http://foo/"), kSync);
1051 base::RunLoop().RunUntilIdle();
1052 EXPECT_EQ(kQuotaStatusOk, status());
1053 EXPECT_EQ(0, usage());
1054 EXPECT_EQ(QuotaManager::kSyncableStorageDefaultHostQuota, quota());
1057 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_MultiOrigins) {
1058 static const MockOriginData kData[] = {
1059 { "http://foo.com/", kPerm, 10 },
1060 { "http://foo.com:8080/", kPerm, 20 },
1061 { "https://foo.com/", kPerm, 13 },
1062 { "https://foo.com:8081/", kPerm, 19 },
1063 { "http://bar.com/", kPerm, 5 },
1064 { "https://bar.com/", kPerm, 7 },
1065 { "http://baz.com/", kPerm, 30 },
1066 { "http://foo.com/", kTemp, 40 },
1068 RegisterClient(CreateClient(kData, arraysize(kData),
1069 QuotaClient::kFileSystem));
1071 SetPersistentHostQuota("foo.com", 100);
1072 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1073 base::RunLoop().RunUntilIdle();
1074 EXPECT_EQ(kQuotaStatusOk, status());
1075 EXPECT_EQ(10 + 20 + 13 + 19, usage());
1076 EXPECT_EQ(100, quota());
1079 TEST_F(QuotaManagerTest, GetPersistentUsage_WithModify) {
1080 GetUsage_WithModifyTestBody(kPerm);
1083 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_WithAdditionalTasks) {
1084 static const MockOriginData kData[] = {
1085 { "http://foo.com/", kPerm, 10 },
1086 { "http://foo.com:8080/", kPerm, 20 },
1087 { "http://bar.com/", kPerm, 13 },
1088 { "http://foo.com/", kTemp, 40 },
1090 RegisterClient(CreateClient(kData, arraysize(kData),
1091 QuotaClient::kFileSystem));
1092 SetPersistentHostQuota("foo.com", 100);
1094 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1095 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1096 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1097 base::RunLoop().RunUntilIdle();
1098 EXPECT_EQ(kQuotaStatusOk, status());
1099 EXPECT_EQ(10 + 20, usage());
1100 EXPECT_EQ(100, quota());
1102 set_additional_callback_count(0);
1103 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"),
1104 kPerm);
1105 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1106 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm);
1107 base::RunLoop().RunUntilIdle();
1108 EXPECT_EQ(kQuotaStatusOk, status());
1109 EXPECT_EQ(10 + 20, usage());
1110 EXPECT_EQ(2, additional_callback_count());
1113 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_NukeManager) {
1114 static const MockOriginData kData[] = {
1115 { "http://foo.com/", kPerm, 10 },
1116 { "http://foo.com:8080/", kPerm, 20 },
1117 { "http://bar.com/", kPerm, 13 },
1118 { "http://foo.com/", kTemp, 40 },
1120 RegisterClient(CreateClient(kData, arraysize(kData),
1121 QuotaClient::kFileSystem));
1122 SetPersistentHostQuota("foo.com", 100);
1124 set_additional_callback_count(0);
1125 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
1126 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), kPerm);
1127 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm);
1129 // Nuke before waiting for callbacks.
1130 set_quota_manager(NULL);
1131 base::RunLoop().RunUntilIdle();
1132 EXPECT_EQ(kQuotaErrorAbort, status());
1135 TEST_F(QuotaManagerTest, GetUsage_Simple) {
1136 static const MockOriginData kData[] = {
1137 { "http://foo.com/", kPerm, 1 },
1138 { "http://foo.com:1/", kPerm, 20 },
1139 { "http://bar.com/", kTemp, 300 },
1140 { "https://buz.com/", kTemp, 4000 },
1141 { "http://buz.com/", kTemp, 50000 },
1142 { "http://bar.com:1/", kPerm, 600000 },
1143 { "http://foo.com/", kTemp, 7000000 },
1145 RegisterClient(CreateClient(kData, arraysize(kData),
1146 QuotaClient::kFileSystem));
1148 GetGlobalUsage(kPerm);
1149 base::RunLoop().RunUntilIdle();
1150 EXPECT_EQ(usage(), 1 + 20 + 600000);
1151 EXPECT_EQ(0, unlimited_usage());
1153 GetGlobalUsage(kTemp);
1154 base::RunLoop().RunUntilIdle();
1155 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000);
1156 EXPECT_EQ(0, unlimited_usage());
1158 GetHostUsage("foo.com", kPerm);
1159 base::RunLoop().RunUntilIdle();
1160 EXPECT_EQ(usage(), 1 + 20);
1162 GetHostUsage("buz.com", kTemp);
1163 base::RunLoop().RunUntilIdle();
1164 EXPECT_EQ(usage(), 4000 + 50000);
1167 TEST_F(QuotaManagerTest, GetUsage_WithModification) {
1168 static const MockOriginData kData[] = {
1169 { "http://foo.com/", kPerm, 1 },
1170 { "http://foo.com:1/", kPerm, 20 },
1171 { "http://bar.com/", kTemp, 300 },
1172 { "https://buz.com/", kTemp, 4000 },
1173 { "http://buz.com/", kTemp, 50000 },
1174 { "http://bar.com:1/", kPerm, 600000 },
1175 { "http://foo.com/", kTemp, 7000000 },
1178 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1179 QuotaClient::kFileSystem);
1180 RegisterClient(client);
1182 GetGlobalUsage(kPerm);
1183 base::RunLoop().RunUntilIdle();
1184 EXPECT_EQ(usage(), 1 + 20 + 600000);
1185 EXPECT_EQ(0, unlimited_usage());
1187 client->ModifyOriginAndNotify(
1188 GURL("http://foo.com/"), kPerm, 80000000);
1190 GetGlobalUsage(kPerm);
1191 base::RunLoop().RunUntilIdle();
1192 EXPECT_EQ(usage(), 1 + 20 + 600000 + 80000000);
1193 EXPECT_EQ(0, unlimited_usage());
1195 GetGlobalUsage(kTemp);
1196 base::RunLoop().RunUntilIdle();
1197 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000);
1198 EXPECT_EQ(0, unlimited_usage());
1200 client->ModifyOriginAndNotify(
1201 GURL("http://foo.com/"), kTemp, 1);
1203 GetGlobalUsage(kTemp);
1204 base::RunLoop().RunUntilIdle();
1205 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000 + 1);
1206 EXPECT_EQ(0, unlimited_usage());
1208 GetHostUsage("buz.com", kTemp);
1209 base::RunLoop().RunUntilIdle();
1210 EXPECT_EQ(usage(), 4000 + 50000);
1212 client->ModifyOriginAndNotify(
1213 GURL("http://buz.com/"), kTemp, 900000000);
1215 GetHostUsage("buz.com", kTemp);
1216 base::RunLoop().RunUntilIdle();
1217 EXPECT_EQ(usage(), 4000 + 50000 + 900000000);
1220 TEST_F(QuotaManagerTest, GetUsage_WithDeleteOrigin) {
1221 static const MockOriginData kData[] = {
1222 { "http://foo.com/", kTemp, 1 },
1223 { "http://foo.com:1/", kTemp, 20 },
1224 { "http://foo.com/", kPerm, 300 },
1225 { "http://bar.com/", kTemp, 4000 },
1227 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1228 QuotaClient::kFileSystem);
1229 RegisterClient(client);
1231 GetGlobalUsage(kTemp);
1232 base::RunLoop().RunUntilIdle();
1233 int64 predelete_global_tmp = usage();
1235 GetHostUsage("foo.com", kTemp);
1236 base::RunLoop().RunUntilIdle();
1237 int64 predelete_host_tmp = usage();
1239 GetHostUsage("foo.com", kPerm);
1240 base::RunLoop().RunUntilIdle();
1241 int64 predelete_host_pers = usage();
1243 DeleteClientOriginData(client, GURL("http://foo.com/"),
1244 kTemp);
1245 base::RunLoop().RunUntilIdle();
1246 EXPECT_EQ(kQuotaStatusOk, status());
1248 GetGlobalUsage(kTemp);
1249 base::RunLoop().RunUntilIdle();
1250 EXPECT_EQ(predelete_global_tmp - 1, usage());
1252 GetHostUsage("foo.com", kTemp);
1253 base::RunLoop().RunUntilIdle();
1254 EXPECT_EQ(predelete_host_tmp - 1, usage());
1256 GetHostUsage("foo.com", kPerm);
1257 base::RunLoop().RunUntilIdle();
1258 EXPECT_EQ(predelete_host_pers, usage());
1261 TEST_F(QuotaManagerTest, GetAvailableSpaceTest) {
1262 GetAvailableSpace();
1263 base::RunLoop().RunUntilIdle();
1264 EXPECT_EQ(kQuotaStatusOk, status());
1265 EXPECT_LE(0, available_space());
1268 TEST_F(QuotaManagerTest, EvictOriginData) {
1269 static const MockOriginData kData1[] = {
1270 { "http://foo.com/", kTemp, 1 },
1271 { "http://foo.com:1/", kTemp, 20 },
1272 { "http://foo.com/", kPerm, 300 },
1273 { "http://bar.com/", kTemp, 4000 },
1275 static const MockOriginData kData2[] = {
1276 { "http://foo.com/", kTemp, 50000 },
1277 { "http://foo.com:1/", kTemp, 6000 },
1278 { "http://foo.com/", kPerm, 700 },
1279 { "https://foo.com/", kTemp, 80 },
1280 { "http://bar.com/", kTemp, 9 },
1282 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1283 QuotaClient::kFileSystem);
1284 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1285 QuotaClient::kDatabase);
1286 RegisterClient(client1);
1287 RegisterClient(client2);
1289 GetGlobalUsage(kTemp);
1290 base::RunLoop().RunUntilIdle();
1291 int64 predelete_global_tmp = usage();
1293 GetHostUsage("foo.com", kTemp);
1294 base::RunLoop().RunUntilIdle();
1295 int64 predelete_host_tmp = usage();
1297 GetHostUsage("foo.com", kPerm);
1298 base::RunLoop().RunUntilIdle();
1299 int64 predelete_host_pers = usage();
1301 for (size_t i = 0; i < arraysize(kData1); ++i)
1302 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1303 GURL(kData1[i].origin), kData1[i].type);
1304 for (size_t i = 0; i < arraysize(kData2); ++i)
1305 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1306 GURL(kData2[i].origin), kData2[i].type);
1307 base::RunLoop().RunUntilIdle();
1309 EvictOriginData(GURL("http://foo.com/"), kTemp);
1310 base::RunLoop().RunUntilIdle();
1312 DumpOriginInfoTable();
1313 base::RunLoop().RunUntilIdle();
1315 typedef OriginInfoTableEntries::const_iterator iterator;
1316 for (iterator itr(origin_info_entries().begin()),
1317 end(origin_info_entries().end());
1318 itr != end; ++itr) {
1319 if (itr->type == kTemp)
1320 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1323 GetGlobalUsage(kTemp);
1324 base::RunLoop().RunUntilIdle();
1325 EXPECT_EQ(predelete_global_tmp - (1 + 50000), usage());
1327 GetHostUsage("foo.com", kTemp);
1328 base::RunLoop().RunUntilIdle();
1329 EXPECT_EQ(predelete_host_tmp - (1 + 50000), usage());
1331 GetHostUsage("foo.com", kPerm);
1332 base::RunLoop().RunUntilIdle();
1333 EXPECT_EQ(predelete_host_pers, usage());
1336 TEST_F(QuotaManagerTest, EvictOriginDataWithDeletionError) {
1337 static const MockOriginData kData[] = {
1338 { "http://foo.com/", kTemp, 1 },
1339 { "http://foo.com:1/", kTemp, 20 },
1340 { "http://foo.com/", kPerm, 300 },
1341 { "http://bar.com/", kTemp, 4000 },
1343 static const int kNumberOfTemporaryOrigins = 3;
1344 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1345 QuotaClient::kFileSystem);
1346 RegisterClient(client);
1348 GetGlobalUsage(kTemp);
1349 base::RunLoop().RunUntilIdle();
1350 int64 predelete_global_tmp = usage();
1352 GetHostUsage("foo.com", kTemp);
1353 base::RunLoop().RunUntilIdle();
1354 int64 predelete_host_tmp = usage();
1356 GetHostUsage("foo.com", kPerm);
1357 base::RunLoop().RunUntilIdle();
1358 int64 predelete_host_pers = usage();
1360 for (size_t i = 0; i < arraysize(kData); ++i)
1361 NotifyStorageAccessed(client, GURL(kData[i].origin), kData[i].type);
1362 base::RunLoop().RunUntilIdle();
1364 client->AddOriginToErrorSet(GURL("http://foo.com/"), kTemp);
1366 for (int i = 0;
1367 i < QuotaManager::kThresholdOfErrorsToBeBlacklisted + 1;
1368 ++i) {
1369 EvictOriginData(GURL("http://foo.com/"), kTemp);
1370 base::RunLoop().RunUntilIdle();
1371 EXPECT_EQ(kQuotaErrorInvalidModification, status());
1374 DumpOriginInfoTable();
1375 base::RunLoop().RunUntilIdle();
1377 bool found_origin_in_database = false;
1378 typedef OriginInfoTableEntries::const_iterator iterator;
1379 for (iterator itr(origin_info_entries().begin()),
1380 end(origin_info_entries().end());
1381 itr != end; ++itr) {
1382 if (itr->type == kTemp &&
1383 GURL("http://foo.com/") == itr->origin) {
1384 found_origin_in_database = true;
1385 break;
1388 // The origin "http://foo.com/" should be in the database.
1389 EXPECT_TRUE(found_origin_in_database);
1391 for (size_t i = 0; i < kNumberOfTemporaryOrigins - 1; ++i) {
1392 GetLRUOrigin(kTemp);
1393 base::RunLoop().RunUntilIdle();
1394 EXPECT_FALSE(lru_origin().is_empty());
1395 // The origin "http://foo.com/" should not be in the LRU list.
1396 EXPECT_NE(std::string("http://foo.com/"), lru_origin().spec());
1397 DeleteOriginFromDatabase(lru_origin(), kTemp);
1398 base::RunLoop().RunUntilIdle();
1401 // Now the LRU list must be empty.
1402 GetLRUOrigin(kTemp);
1403 base::RunLoop().RunUntilIdle();
1404 EXPECT_TRUE(lru_origin().is_empty());
1406 // Deleting origins from the database should not affect the results of the
1407 // following checks.
1408 GetGlobalUsage(kTemp);
1409 base::RunLoop().RunUntilIdle();
1410 EXPECT_EQ(predelete_global_tmp, usage());
1412 GetHostUsage("foo.com", kTemp);
1413 base::RunLoop().RunUntilIdle();
1414 EXPECT_EQ(predelete_host_tmp, usage());
1416 GetHostUsage("foo.com", kPerm);
1417 base::RunLoop().RunUntilIdle();
1418 EXPECT_EQ(predelete_host_pers, usage());
1421 TEST_F(QuotaManagerTest, GetUsageAndQuotaForEviction) {
1422 static const MockOriginData kData[] = {
1423 { "http://foo.com/", kTemp, 1 },
1424 { "http://foo.com:1/", kTemp, 20 },
1425 { "http://foo.com/", kPerm, 300 },
1426 { "http://unlimited/", kTemp, 4000 },
1429 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/"));
1430 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1431 QuotaClient::kFileSystem);
1432 RegisterClient(client);
1434 SetTemporaryGlobalQuota(10000000);
1435 base::RunLoop().RunUntilIdle();
1437 GetUsageAndQuotaForEviction();
1438 base::RunLoop().RunUntilIdle();
1439 EXPECT_EQ(kQuotaStatusOk, status());
1440 EXPECT_EQ(21, limited_usage());
1441 EXPECT_EQ(10000000, quota());
1442 EXPECT_LE(0, available_space());
1445 TEST_F(QuotaManagerTest, DeleteHostDataSimple) {
1446 static const MockOriginData kData[] = {
1447 { "http://foo.com/", kTemp, 1 },
1449 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1450 QuotaClient::kFileSystem);
1451 RegisterClient(client);
1453 GetGlobalUsage(kTemp);
1454 base::RunLoop().RunUntilIdle();
1455 const int64 predelete_global_tmp = usage();
1457 GetHostUsage("foo.com", kTemp);
1458 base::RunLoop().RunUntilIdle();
1459 int64 predelete_host_tmp = usage();
1461 GetHostUsage("foo.com", kPerm);
1462 base::RunLoop().RunUntilIdle();
1463 int64 predelete_host_pers = usage();
1465 DeleteHostData(std::string(), kTemp, kAllClients);
1466 base::RunLoop().RunUntilIdle();
1467 EXPECT_EQ(kQuotaStatusOk, status());
1469 GetGlobalUsage(kTemp);
1470 base::RunLoop().RunUntilIdle();
1471 EXPECT_EQ(predelete_global_tmp, usage());
1473 GetHostUsage("foo.com", kTemp);
1474 base::RunLoop().RunUntilIdle();
1475 EXPECT_EQ(predelete_host_tmp, usage());
1477 GetHostUsage("foo.com", kPerm);
1478 base::RunLoop().RunUntilIdle();
1479 EXPECT_EQ(predelete_host_pers, usage());
1481 DeleteHostData("foo.com", kTemp, kAllClients);
1482 base::RunLoop().RunUntilIdle();
1483 EXPECT_EQ(kQuotaStatusOk, status());
1485 GetGlobalUsage(kTemp);
1486 base::RunLoop().RunUntilIdle();
1487 EXPECT_EQ(predelete_global_tmp - 1, usage());
1489 GetHostUsage("foo.com", kTemp);
1490 base::RunLoop().RunUntilIdle();
1491 EXPECT_EQ(predelete_host_tmp - 1, usage());
1493 GetHostUsage("foo.com", kPerm);
1494 base::RunLoop().RunUntilIdle();
1495 EXPECT_EQ(predelete_host_pers, usage());
1498 TEST_F(QuotaManagerTest, DeleteHostDataMultiple) {
1499 static const MockOriginData kData1[] = {
1500 { "http://foo.com/", kTemp, 1 },
1501 { "http://foo.com:1/", kTemp, 20 },
1502 { "http://foo.com/", kPerm, 300 },
1503 { "http://bar.com/", kTemp, 4000 },
1505 static const MockOriginData kData2[] = {
1506 { "http://foo.com/", kTemp, 50000 },
1507 { "http://foo.com:1/", kTemp, 6000 },
1508 { "http://foo.com/", kPerm, 700 },
1509 { "https://foo.com/", kTemp, 80 },
1510 { "http://bar.com/", kTemp, 9 },
1512 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1513 QuotaClient::kFileSystem);
1514 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1515 QuotaClient::kDatabase);
1516 RegisterClient(client1);
1517 RegisterClient(client2);
1519 GetGlobalUsage(kTemp);
1520 base::RunLoop().RunUntilIdle();
1521 const int64 predelete_global_tmp = usage();
1523 GetHostUsage("foo.com", kTemp);
1524 base::RunLoop().RunUntilIdle();
1525 const int64 predelete_foo_tmp = usage();
1527 GetHostUsage("bar.com", kTemp);
1528 base::RunLoop().RunUntilIdle();
1529 const int64 predelete_bar_tmp = usage();
1531 GetHostUsage("foo.com", kPerm);
1532 base::RunLoop().RunUntilIdle();
1533 const int64 predelete_foo_pers = usage();
1535 GetHostUsage("bar.com", kPerm);
1536 base::RunLoop().RunUntilIdle();
1537 const int64 predelete_bar_pers = usage();
1539 reset_status_callback_count();
1540 DeleteHostData("foo.com", kTemp, kAllClients);
1541 DeleteHostData("bar.com", kTemp, kAllClients);
1542 DeleteHostData("foo.com", kTemp, kAllClients);
1543 base::RunLoop().RunUntilIdle();
1545 EXPECT_EQ(3, status_callback_count());
1547 DumpOriginInfoTable();
1548 base::RunLoop().RunUntilIdle();
1550 typedef OriginInfoTableEntries::const_iterator iterator;
1551 for (iterator itr(origin_info_entries().begin()),
1552 end(origin_info_entries().end());
1553 itr != end; ++itr) {
1554 if (itr->type == kTemp) {
1555 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1556 EXPECT_NE(std::string("http://foo.com:1/"), itr->origin.spec());
1557 EXPECT_NE(std::string("https://foo.com/"), itr->origin.spec());
1558 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec());
1562 GetGlobalUsage(kTemp);
1563 base::RunLoop().RunUntilIdle();
1564 EXPECT_EQ(predelete_global_tmp - (1 + 20 + 4000 + 50000 + 6000 + 80 + 9),
1565 usage());
1567 GetHostUsage("foo.com", kTemp);
1568 base::RunLoop().RunUntilIdle();
1569 EXPECT_EQ(predelete_foo_tmp - (1 + 20 + 50000 + 6000 + 80), usage());
1571 GetHostUsage("bar.com", kTemp);
1572 base::RunLoop().RunUntilIdle();
1573 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage());
1575 GetHostUsage("foo.com", kPerm);
1576 base::RunLoop().RunUntilIdle();
1577 EXPECT_EQ(predelete_foo_pers, usage());
1579 GetHostUsage("bar.com", kPerm);
1580 base::RunLoop().RunUntilIdle();
1581 EXPECT_EQ(predelete_bar_pers, usage());
1584 // Single-run DeleteOriginData cases must be well covered by
1585 // EvictOriginData tests.
1586 TEST_F(QuotaManagerTest, DeleteOriginDataMultiple) {
1587 static const MockOriginData kData1[] = {
1588 { "http://foo.com/", kTemp, 1 },
1589 { "http://foo.com:1/", kTemp, 20 },
1590 { "http://foo.com/", kPerm, 300 },
1591 { "http://bar.com/", kTemp, 4000 },
1593 static const MockOriginData kData2[] = {
1594 { "http://foo.com/", kTemp, 50000 },
1595 { "http://foo.com:1/", kTemp, 6000 },
1596 { "http://foo.com/", kPerm, 700 },
1597 { "https://foo.com/", kTemp, 80 },
1598 { "http://bar.com/", kTemp, 9 },
1600 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1601 QuotaClient::kFileSystem);
1602 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1603 QuotaClient::kDatabase);
1604 RegisterClient(client1);
1605 RegisterClient(client2);
1607 GetGlobalUsage(kTemp);
1608 base::RunLoop().RunUntilIdle();
1609 const int64 predelete_global_tmp = usage();
1611 GetHostUsage("foo.com", kTemp);
1612 base::RunLoop().RunUntilIdle();
1613 const int64 predelete_foo_tmp = usage();
1615 GetHostUsage("bar.com", kTemp);
1616 base::RunLoop().RunUntilIdle();
1617 const int64 predelete_bar_tmp = usage();
1619 GetHostUsage("foo.com", kPerm);
1620 base::RunLoop().RunUntilIdle();
1621 const int64 predelete_foo_pers = usage();
1623 GetHostUsage("bar.com", kPerm);
1624 base::RunLoop().RunUntilIdle();
1625 const int64 predelete_bar_pers = usage();
1627 for (size_t i = 0; i < arraysize(kData1); ++i)
1628 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1629 GURL(kData1[i].origin), kData1[i].type);
1630 for (size_t i = 0; i < arraysize(kData2); ++i)
1631 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown,
1632 GURL(kData2[i].origin), kData2[i].type);
1633 base::RunLoop().RunUntilIdle();
1635 reset_status_callback_count();
1636 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
1637 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients);
1638 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients);
1639 base::RunLoop().RunUntilIdle();
1641 EXPECT_EQ(3, status_callback_count());
1643 DumpOriginInfoTable();
1644 base::RunLoop().RunUntilIdle();
1646 typedef OriginInfoTableEntries::const_iterator iterator;
1647 for (iterator itr(origin_info_entries().begin()),
1648 end(origin_info_entries().end());
1649 itr != end; ++itr) {
1650 if (itr->type == kTemp) {
1651 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec());
1652 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec());
1656 GetGlobalUsage(kTemp);
1657 base::RunLoop().RunUntilIdle();
1658 EXPECT_EQ(predelete_global_tmp - (1 + 4000 + 50000 + 9), usage());
1660 GetHostUsage("foo.com", kTemp);
1661 base::RunLoop().RunUntilIdle();
1662 EXPECT_EQ(predelete_foo_tmp - (1 + 50000), usage());
1664 GetHostUsage("bar.com", kTemp);
1665 base::RunLoop().RunUntilIdle();
1666 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage());
1668 GetHostUsage("foo.com", kPerm);
1669 base::RunLoop().RunUntilIdle();
1670 EXPECT_EQ(predelete_foo_pers, usage());
1672 GetHostUsage("bar.com", kPerm);
1673 base::RunLoop().RunUntilIdle();
1674 EXPECT_EQ(predelete_bar_pers, usage());
1677 TEST_F(QuotaManagerTest, GetCachedOrigins) {
1678 static const MockOriginData kData[] = {
1679 { "http://a.com/", kTemp, 1 },
1680 { "http://a.com:1/", kTemp, 20 },
1681 { "http://b.com/", kPerm, 300 },
1682 { "http://c.com/", kTemp, 4000 },
1684 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1685 QuotaClient::kFileSystem);
1686 RegisterClient(client);
1688 // TODO(kinuko): Be careful when we add cache pruner.
1690 std::set<GURL> origins;
1691 GetCachedOrigins(kTemp, &origins);
1692 EXPECT_TRUE(origins.empty());
1694 // No matter how we make queries the quota manager tries to cache all
1695 // the origins at startup.
1696 GetHostUsage("a.com", kTemp);
1697 base::RunLoop().RunUntilIdle();
1698 GetCachedOrigins(kTemp, &origins);
1699 EXPECT_EQ(3U, origins.size());
1701 GetHostUsage("b.com", kTemp);
1702 base::RunLoop().RunUntilIdle();
1703 GetCachedOrigins(kTemp, &origins);
1704 EXPECT_EQ(3U, origins.size());
1706 GetCachedOrigins(kPerm, &origins);
1707 EXPECT_TRUE(origins.empty());
1709 GetGlobalUsage(kTemp);
1710 base::RunLoop().RunUntilIdle();
1711 GetCachedOrigins(kTemp, &origins);
1712 EXPECT_EQ(3U, origins.size());
1714 for (size_t i = 0; i < arraysize(kData); ++i) {
1715 if (kData[i].type == kTemp)
1716 EXPECT_TRUE(origins.find(GURL(kData[i].origin)) != origins.end());
1720 TEST_F(QuotaManagerTest, NotifyAndLRUOrigin) {
1721 static const MockOriginData kData[] = {
1722 { "http://a.com/", kTemp, 0 },
1723 { "http://a.com:1/", kTemp, 0 },
1724 { "https://a.com/", kTemp, 0 },
1725 { "http://b.com/", kPerm, 0 }, // persistent
1726 { "http://c.com/", kTemp, 0 },
1728 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1729 QuotaClient::kFileSystem);
1730 RegisterClient(client);
1732 GURL origin;
1733 GetLRUOrigin(kTemp);
1734 base::RunLoop().RunUntilIdle();
1735 EXPECT_TRUE(lru_origin().is_empty());
1737 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp);
1738 GetLRUOrigin(kTemp);
1739 base::RunLoop().RunUntilIdle();
1740 EXPECT_EQ("http://a.com/", lru_origin().spec());
1742 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm);
1743 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp);
1744 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1745 GetLRUOrigin(kTemp);
1746 base::RunLoop().RunUntilIdle();
1747 EXPECT_EQ("http://a.com/", lru_origin().spec());
1749 DeleteOriginFromDatabase(lru_origin(), kTemp);
1750 GetLRUOrigin(kTemp);
1751 base::RunLoop().RunUntilIdle();
1752 EXPECT_EQ("https://a.com/", lru_origin().spec());
1754 DeleteOriginFromDatabase(lru_origin(), kTemp);
1755 GetLRUOrigin(kTemp);
1756 base::RunLoop().RunUntilIdle();
1757 EXPECT_EQ("http://c.com/", lru_origin().spec());
1760 TEST_F(QuotaManagerTest, GetLRUOriginWithOriginInUse) {
1761 static const MockOriginData kData[] = {
1762 { "http://a.com/", kTemp, 0 },
1763 { "http://a.com:1/", kTemp, 0 },
1764 { "https://a.com/", kTemp, 0 },
1765 { "http://b.com/", kPerm, 0 }, // persistent
1766 { "http://c.com/", kTemp, 0 },
1768 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1769 QuotaClient::kFileSystem);
1770 RegisterClient(client);
1772 GURL origin;
1773 GetLRUOrigin(kTemp);
1774 base::RunLoop().RunUntilIdle();
1775 EXPECT_TRUE(lru_origin().is_empty());
1777 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp);
1778 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm);
1779 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp);
1780 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1782 GetLRUOrigin(kTemp);
1783 base::RunLoop().RunUntilIdle();
1784 EXPECT_EQ("http://a.com/", lru_origin().spec());
1786 // Notify origin http://a.com is in use.
1787 NotifyOriginInUse(GURL("http://a.com/"));
1788 GetLRUOrigin(kTemp);
1789 base::RunLoop().RunUntilIdle();
1790 EXPECT_EQ("https://a.com/", lru_origin().spec());
1792 // Notify origin https://a.com is in use while GetLRUOrigin is running.
1793 GetLRUOrigin(kTemp);
1794 NotifyOriginInUse(GURL("https://a.com/"));
1795 base::RunLoop().RunUntilIdle();
1796 // Post-filtering must have excluded the returned origin, so we will
1797 // see empty result here.
1798 EXPECT_TRUE(lru_origin().is_empty());
1800 // Notify access for http://c.com while GetLRUOrigin is running.
1801 GetLRUOrigin(kTemp);
1802 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp);
1803 base::RunLoop().RunUntilIdle();
1804 // Post-filtering must have excluded the returned origin, so we will
1805 // see empty result here.
1806 EXPECT_TRUE(lru_origin().is_empty());
1808 NotifyOriginNoLongerInUse(GURL("http://a.com/"));
1809 NotifyOriginNoLongerInUse(GURL("https://a.com/"));
1810 GetLRUOrigin(kTemp);
1811 base::RunLoop().RunUntilIdle();
1812 EXPECT_EQ("http://a.com/", lru_origin().spec());
1815 TEST_F(QuotaManagerTest, GetOriginsModifiedSince) {
1816 static const MockOriginData kData[] = {
1817 { "http://a.com/", kTemp, 0 },
1818 { "http://a.com:1/", kTemp, 0 },
1819 { "https://a.com/", kTemp, 0 },
1820 { "http://b.com/", kPerm, 0 }, // persistent
1821 { "http://c.com/", kTemp, 0 },
1823 MockStorageClient* client = CreateClient(kData, arraysize(kData),
1824 QuotaClient::kFileSystem);
1825 RegisterClient(client);
1827 GetOriginsModifiedSince(kTemp, base::Time());
1828 base::RunLoop().RunUntilIdle();
1829 EXPECT_TRUE(modified_origins().empty());
1830 EXPECT_EQ(modified_origins_type(), kTemp);
1832 base::Time time1 = client->IncrementMockTime();
1833 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10);
1834 client->ModifyOriginAndNotify(GURL("http://a.com:1/"), kTemp, 10);
1835 client->ModifyOriginAndNotify(GURL("http://b.com/"), kPerm, 10);
1836 base::Time time2 = client->IncrementMockTime();
1837 client->ModifyOriginAndNotify(GURL("https://a.com/"), kTemp, 10);
1838 client->ModifyOriginAndNotify(GURL("http://c.com/"), kTemp, 10);
1839 base::Time time3 = client->IncrementMockTime();
1841 GetOriginsModifiedSince(kTemp, time1);
1842 base::RunLoop().RunUntilIdle();
1843 EXPECT_EQ(4U, modified_origins().size());
1844 EXPECT_EQ(modified_origins_type(), kTemp);
1845 for (size_t i = 0; i < arraysize(kData); ++i) {
1846 if (kData[i].type == kTemp)
1847 EXPECT_EQ(1U, modified_origins().count(GURL(kData[i].origin)));
1850 GetOriginsModifiedSince(kTemp, time2);
1851 base::RunLoop().RunUntilIdle();
1852 EXPECT_EQ(2U, modified_origins().size());
1854 GetOriginsModifiedSince(kTemp, time3);
1855 base::RunLoop().RunUntilIdle();
1856 EXPECT_TRUE(modified_origins().empty());
1857 EXPECT_EQ(modified_origins_type(), kTemp);
1859 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10);
1861 GetOriginsModifiedSince(kTemp, time3);
1862 base::RunLoop().RunUntilIdle();
1863 EXPECT_EQ(1U, modified_origins().size());
1864 EXPECT_EQ(1U, modified_origins().count(GURL("http://a.com/")));
1865 EXPECT_EQ(modified_origins_type(), kTemp);
1868 TEST_F(QuotaManagerTest, DumpQuotaTable) {
1869 SetPersistentHostQuota("example1.com", 1);
1870 SetPersistentHostQuota("example2.com", 20);
1871 SetPersistentHostQuota("example3.com", 300);
1872 base::RunLoop().RunUntilIdle();
1874 DumpQuotaTable();
1875 base::RunLoop().RunUntilIdle();
1877 const QuotaTableEntry kEntries[] = {
1878 QuotaTableEntry("example1.com", kPerm, 1),
1879 QuotaTableEntry("example2.com", kPerm, 20),
1880 QuotaTableEntry("example3.com", kPerm, 300),
1882 std::set<QuotaTableEntry> entries(kEntries, kEntries + arraysize(kEntries));
1884 typedef QuotaTableEntries::const_iterator iterator;
1885 for (iterator itr(quota_entries().begin()), end(quota_entries().end());
1886 itr != end; ++itr) {
1887 SCOPED_TRACE(testing::Message()
1888 << "host = " << itr->host << ", "
1889 << "quota = " << itr->quota);
1890 EXPECT_EQ(1u, entries.erase(*itr));
1892 EXPECT_TRUE(entries.empty());
1895 TEST_F(QuotaManagerTest, DumpOriginInfoTable) {
1896 using std::make_pair;
1898 quota_manager()->NotifyStorageAccessed(
1899 QuotaClient::kUnknown,
1900 GURL("http://example.com/"),
1901 kTemp);
1902 quota_manager()->NotifyStorageAccessed(
1903 QuotaClient::kUnknown,
1904 GURL("http://example.com/"),
1905 kPerm);
1906 quota_manager()->NotifyStorageAccessed(
1907 QuotaClient::kUnknown,
1908 GURL("http://example.com/"),
1909 kPerm);
1910 base::RunLoop().RunUntilIdle();
1912 DumpOriginInfoTable();
1913 base::RunLoop().RunUntilIdle();
1915 typedef std::pair<GURL, StorageType> TypedOrigin;
1916 typedef std::pair<TypedOrigin, int> Entry;
1917 const Entry kEntries[] = {
1918 make_pair(make_pair(GURL("http://example.com/"), kTemp), 1),
1919 make_pair(make_pair(GURL("http://example.com/"), kPerm), 2),
1921 std::set<Entry> entries(kEntries, kEntries + arraysize(kEntries));
1923 typedef OriginInfoTableEntries::const_iterator iterator;
1924 for (iterator itr(origin_info_entries().begin()),
1925 end(origin_info_entries().end());
1926 itr != end; ++itr) {
1927 SCOPED_TRACE(testing::Message()
1928 << "host = " << itr->origin << ", "
1929 << "type = " << itr->type << ", "
1930 << "used_count = " << itr->used_count);
1931 EXPECT_EQ(1u, entries.erase(
1932 make_pair(make_pair(itr->origin, itr->type),
1933 itr->used_count)));
1935 EXPECT_TRUE(entries.empty());
1938 TEST_F(QuotaManagerTest, QuotaForEmptyHost) {
1939 GetPersistentHostQuota(std::string());
1940 base::RunLoop().RunUntilIdle();
1941 EXPECT_EQ(kQuotaStatusOk, status());
1942 EXPECT_EQ(0, quota());
1944 SetPersistentHostQuota(std::string(), 10);
1945 base::RunLoop().RunUntilIdle();
1946 EXPECT_EQ(kQuotaErrorNotSupported, status());
1949 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleOrigin) {
1950 static const MockOriginData kData1[] = {
1951 { "http://foo.com/", kTemp, 1 },
1953 static const MockOriginData kData2[] = {
1954 { "http://foo.com/", kTemp, 2 },
1956 static const MockOriginData kData3[] = {
1957 { "http://foo.com/", kTemp, 4 },
1959 static const MockOriginData kData4[] = {
1960 { "http://foo.com/", kTemp, 8 },
1962 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
1963 QuotaClient::kFileSystem);
1964 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
1965 QuotaClient::kAppcache);
1966 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
1967 QuotaClient::kDatabase);
1968 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
1969 QuotaClient::kIndexedDatabase);
1970 RegisterClient(client1);
1971 RegisterClient(client2);
1972 RegisterClient(client3);
1973 RegisterClient(client4);
1975 GetHostUsage("foo.com", kTemp);
1976 base::RunLoop().RunUntilIdle();
1977 const int64 predelete_foo_tmp = usage();
1979 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kFileSystem);
1980 base::RunLoop().RunUntilIdle();
1981 GetHostUsage("foo.com", kTemp);
1982 base::RunLoop().RunUntilIdle();
1983 EXPECT_EQ(predelete_foo_tmp - 1, usage());
1985 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kAppcache);
1986 base::RunLoop().RunUntilIdle();
1987 GetHostUsage("foo.com", kTemp);
1988 base::RunLoop().RunUntilIdle();
1989 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
1991 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kDatabase);
1992 base::RunLoop().RunUntilIdle();
1993 GetHostUsage("foo.com", kTemp);
1994 base::RunLoop().RunUntilIdle();
1995 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage());
1997 DeleteOriginData(GURL("http://foo.com/"), kTemp,
1998 QuotaClient::kIndexedDatabase);
1999 base::RunLoop().RunUntilIdle();
2000 GetHostUsage("foo.com", kTemp);
2001 base::RunLoop().RunUntilIdle();
2002 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2005 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleHost) {
2006 static const MockOriginData kData1[] = {
2007 { "http://foo.com:1111/", kTemp, 1 },
2009 static const MockOriginData kData2[] = {
2010 { "http://foo.com:2222/", kTemp, 2 },
2012 static const MockOriginData kData3[] = {
2013 { "http://foo.com:3333/", kTemp, 4 },
2015 static const MockOriginData kData4[] = {
2016 { "http://foo.com:4444/", kTemp, 8 },
2018 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2019 QuotaClient::kFileSystem);
2020 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2021 QuotaClient::kAppcache);
2022 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2023 QuotaClient::kDatabase);
2024 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2025 QuotaClient::kIndexedDatabase);
2026 RegisterClient(client1);
2027 RegisterClient(client2);
2028 RegisterClient(client3);
2029 RegisterClient(client4);
2031 GetHostUsage("foo.com", kTemp);
2032 base::RunLoop().RunUntilIdle();
2033 const int64 predelete_foo_tmp = usage();
2035 DeleteHostData("foo.com", kTemp, QuotaClient::kFileSystem);
2036 base::RunLoop().RunUntilIdle();
2037 GetHostUsage("foo.com", kTemp);
2038 base::RunLoop().RunUntilIdle();
2039 EXPECT_EQ(predelete_foo_tmp - 1, usage());
2041 DeleteHostData("foo.com", kTemp, QuotaClient::kAppcache);
2042 base::RunLoop().RunUntilIdle();
2043 GetHostUsage("foo.com", kTemp);
2044 base::RunLoop().RunUntilIdle();
2045 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
2047 DeleteHostData("foo.com", kTemp, QuotaClient::kDatabase);
2048 base::RunLoop().RunUntilIdle();
2049 GetHostUsage("foo.com", kTemp);
2050 base::RunLoop().RunUntilIdle();
2051 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage());
2053 DeleteHostData("foo.com", kTemp, QuotaClient::kIndexedDatabase);
2054 base::RunLoop().RunUntilIdle();
2055 GetHostUsage("foo.com", kTemp);
2056 base::RunLoop().RunUntilIdle();
2057 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2060 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleOrigin) {
2061 static const MockOriginData kData1[] = {
2062 { "http://foo.com/", kTemp, 1 },
2064 static const MockOriginData kData2[] = {
2065 { "http://foo.com/", kTemp, 2 },
2067 static const MockOriginData kData3[] = {
2068 { "http://foo.com/", kTemp, 4 },
2070 static const MockOriginData kData4[] = {
2071 { "http://foo.com/", kTemp, 8 },
2073 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2074 QuotaClient::kFileSystem);
2075 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2076 QuotaClient::kAppcache);
2077 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2078 QuotaClient::kDatabase);
2079 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2080 QuotaClient::kIndexedDatabase);
2081 RegisterClient(client1);
2082 RegisterClient(client2);
2083 RegisterClient(client3);
2084 RegisterClient(client4);
2086 GetHostUsage("foo.com", kTemp);
2087 base::RunLoop().RunUntilIdle();
2088 const int64 predelete_foo_tmp = usage();
2090 DeleteOriginData(GURL("http://foo.com/"), kTemp,
2091 QuotaClient::kFileSystem | QuotaClient::kDatabase);
2092 base::RunLoop().RunUntilIdle();
2093 GetHostUsage("foo.com", kTemp);
2094 base::RunLoop().RunUntilIdle();
2095 EXPECT_EQ(predelete_foo_tmp - 4 - 1, usage());
2097 DeleteOriginData(GURL("http://foo.com/"), kTemp,
2098 QuotaClient::kAppcache | QuotaClient::kIndexedDatabase);
2099 base::RunLoop().RunUntilIdle();
2100 GetHostUsage("foo.com", kTemp);
2101 base::RunLoop().RunUntilIdle();
2102 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2105 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleHost) {
2106 static const MockOriginData kData1[] = {
2107 { "http://foo.com:1111/", kTemp, 1 },
2109 static const MockOriginData kData2[] = {
2110 { "http://foo.com:2222/", kTemp, 2 },
2112 static const MockOriginData kData3[] = {
2113 { "http://foo.com:3333/", kTemp, 4 },
2115 static const MockOriginData kData4[] = {
2116 { "http://foo.com:4444/", kTemp, 8 },
2118 MockStorageClient* client1 = CreateClient(kData1, arraysize(kData1),
2119 QuotaClient::kFileSystem);
2120 MockStorageClient* client2 = CreateClient(kData2, arraysize(kData2),
2121 QuotaClient::kAppcache);
2122 MockStorageClient* client3 = CreateClient(kData3, arraysize(kData3),
2123 QuotaClient::kDatabase);
2124 MockStorageClient* client4 = CreateClient(kData4, arraysize(kData4),
2125 QuotaClient::kIndexedDatabase);
2126 RegisterClient(client1);
2127 RegisterClient(client2);
2128 RegisterClient(client3);
2129 RegisterClient(client4);
2131 GetHostUsage("foo.com", kTemp);
2132 base::RunLoop().RunUntilIdle();
2133 const int64 predelete_foo_tmp = usage();
2135 DeleteHostData("foo.com", kTemp,
2136 QuotaClient::kFileSystem | QuotaClient::kAppcache);
2137 base::RunLoop().RunUntilIdle();
2138 GetHostUsage("foo.com", kTemp);
2139 base::RunLoop().RunUntilIdle();
2140 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage());
2142 DeleteHostData("foo.com", kTemp,
2143 QuotaClient::kDatabase | QuotaClient::kIndexedDatabase);
2144 base::RunLoop().RunUntilIdle();
2145 GetHostUsage("foo.com", kTemp);
2146 base::RunLoop().RunUntilIdle();
2147 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage());
2150 TEST_F(QuotaManagerTest, GetUsageAndQuota_Incognito) {
2151 ResetQuotaManager(true);
2153 static const MockOriginData kData[] = {
2154 { "http://foo.com/", kTemp, 10 },
2155 { "http://foo.com/", kPerm, 80 },
2157 RegisterClient(CreateClient(kData, arraysize(kData),
2158 QuotaClient::kFileSystem));
2160 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
2161 base::RunLoop().RunUntilIdle();
2162 EXPECT_EQ(kQuotaStatusOk, status());
2163 EXPECT_EQ(80, usage());
2164 EXPECT_EQ(0, quota());
2166 SetTemporaryGlobalQuota(100);
2167 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
2168 base::RunLoop().RunUntilIdle();
2169 EXPECT_EQ(kQuotaStatusOk, status());
2170 EXPECT_EQ(10, usage());
2171 EXPECT_LE(std::min(static_cast<int64>(100 / kPerHostTemporaryPortion),
2172 QuotaManager::kIncognitoDefaultQuotaLimit), quota());
2174 mock_special_storage_policy()->AddUnlimited(GURL("http://foo.com/"));
2175 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm);
2176 base::RunLoop().RunUntilIdle();
2177 EXPECT_EQ(kQuotaStatusOk, status());
2178 EXPECT_EQ(80, usage());
2179 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota());
2181 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp);
2182 base::RunLoop().RunUntilIdle();
2183 EXPECT_EQ(kQuotaStatusOk, status());
2184 EXPECT_EQ(10, usage());
2185 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota());
2188 } // namespace content