OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <set> | |
6 #include <sstream> | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/file_util.h" | |
11 #include "base/files/scoped_temp_dir.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/message_loop_proxy.h" | |
16 #include "base/stl_util.h" | |
17 #include "base/sys_info.h" | |
18 #include "base/time.h" | |
19 #include "googleurl/src/gurl.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 #include "webkit/quota/mock_special_storage_policy.h" | |
22 #include "webkit/quota/mock_storage_client.h" | |
23 #include "webkit/quota/quota_database.h" | |
24 #include "webkit/quota/quota_manager.h" | |
25 | |
26 using base::MessageLoopProxy; | |
27 | |
28 namespace quota { | |
29 | |
30 namespace { | |
31 | |
32 // For shorter names. | |
33 const StorageType kTemp = kStorageTypeTemporary; | |
34 const StorageType kPerm = kStorageTypePersistent; | |
35 const StorageType kSync = kStorageTypeSyncable; | |
36 | |
37 const int kAllClients = QuotaClient::kAllClientsMask; | |
38 | |
39 const int64 kAvailableSpaceForApp = 13377331U; | |
40 | |
41 const int64 kMinimumPreserveForSystem = QuotaManager::kMinimumPreserveForSystem; | |
42 const int kPerHostTemporaryPortion = QuotaManager::kPerHostTemporaryPortion; | |
43 | |
44 // Returns a deterministic value for the amount of available disk space. | |
45 int64 GetAvailableDiskSpaceForTest(const base::FilePath&) { | |
46 return kAvailableSpaceForApp + kMinimumPreserveForSystem; | |
47 } | |
48 | |
49 } // namespace | |
50 | |
51 class QuotaManagerTest : public testing::Test { | |
52 protected: | |
53 typedef QuotaManager::QuotaTableEntry QuotaTableEntry; | |
54 typedef QuotaManager::QuotaTableEntries QuotaTableEntries; | |
55 typedef QuotaManager::OriginInfoTableEntries OriginInfoTableEntries; | |
56 | |
57 public: | |
58 QuotaManagerTest() | |
59 : weak_factory_(this), | |
60 mock_time_counter_(0) { | |
61 } | |
62 | |
63 virtual void SetUp() { | |
64 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
65 mock_special_storage_policy_ = new MockSpecialStoragePolicy; | |
66 ResetQuotaManager(false /* is_incognito */); | |
67 } | |
68 | |
69 virtual void TearDown() { | |
70 // Make sure the quota manager cleans up correctly. | |
71 quota_manager_ = NULL; | |
72 base::MessageLoop::current()->RunUntilIdle(); | |
73 } | |
74 | |
75 protected: | |
76 void ResetQuotaManager(bool is_incognito) { | |
77 quota_manager_ = new QuotaManager( | |
78 is_incognito, | |
79 data_dir_.path(), | |
80 MessageLoopProxy::current(), | |
81 MessageLoopProxy::current(), | |
82 mock_special_storage_policy_); | |
83 // Don't (automatically) start the eviction for testing. | |
84 quota_manager_->eviction_disabled_ = true; | |
85 // Don't query the hard disk for remaining capacity. | |
86 quota_manager_->get_disk_space_fn_ = &GetAvailableDiskSpaceForTest; | |
87 additional_callback_count_ = 0; | |
88 } | |
89 | |
90 MockStorageClient* CreateClient( | |
91 const MockOriginData* mock_data, | |
92 size_t mock_data_size, | |
93 QuotaClient::ID id) { | |
94 return new MockStorageClient(quota_manager_->proxy(), | |
95 mock_data, id, mock_data_size); | |
96 } | |
97 | |
98 void RegisterClient(MockStorageClient* client) { | |
99 quota_manager_->proxy()->RegisterClient(client); | |
100 } | |
101 | |
102 void GetUsageInfo() { | |
103 usage_info_.clear(); | |
104 quota_manager_->GetUsageInfo( | |
105 base::Bind(&QuotaManagerTest::DidGetUsageInfo, | |
106 weak_factory_.GetWeakPtr())); | |
107 } | |
108 | |
109 void GetUsageAndQuotaForWebApps(const GURL& origin, | |
110 StorageType type) { | |
111 quota_status_ = kQuotaStatusUnknown; | |
112 usage_ = -1; | |
113 quota_ = -1; | |
114 quota_manager_->GetUsageAndQuotaForWebApps( | |
115 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota, | |
116 weak_factory_.GetWeakPtr())); | |
117 } | |
118 | |
119 void GetUsageAndQuotaForStorageClient(const GURL& origin, | |
120 StorageType type) { | |
121 quota_status_ = kQuotaStatusUnknown; | |
122 usage_ = -1; | |
123 quota_ = -1; | |
124 quota_manager_->GetUsageAndQuota( | |
125 origin, type, base::Bind(&QuotaManagerTest::DidGetUsageAndQuota, | |
126 weak_factory_.GetWeakPtr())); | |
127 } | |
128 | |
129 void GetTemporaryGlobalQuota() { | |
130 quota_status_ = kQuotaStatusUnknown; | |
131 quota_ = -1; | |
132 quota_manager_->GetTemporaryGlobalQuota( | |
133 base::Bind(&QuotaManagerTest::DidGetQuota, | |
134 weak_factory_.GetWeakPtr())); | |
135 } | |
136 | |
137 void SetTemporaryGlobalQuota(int64 new_quota) { | |
138 quota_status_ = kQuotaStatusUnknown; | |
139 quota_ = -1; | |
140 quota_manager_->SetTemporaryGlobalOverrideQuota( | |
141 new_quota, | |
142 base::Bind(&QuotaManagerTest::DidGetQuota, | |
143 weak_factory_.GetWeakPtr())); | |
144 } | |
145 | |
146 void GetPersistentHostQuota(const std::string& host) { | |
147 quota_status_ = kQuotaStatusUnknown; | |
148 quota_ = -1; | |
149 quota_manager_->GetPersistentHostQuota( | |
150 host, | |
151 base::Bind(&QuotaManagerTest::DidGetHostQuota, | |
152 weak_factory_.GetWeakPtr())); | |
153 } | |
154 | |
155 void SetPersistentHostQuota(const std::string& host, int64 new_quota) { | |
156 quota_status_ = kQuotaStatusUnknown; | |
157 quota_ = -1; | |
158 quota_manager_->SetPersistentHostQuota( | |
159 host, new_quota, | |
160 base::Bind(&QuotaManagerTest::DidGetHostQuota, | |
161 weak_factory_.GetWeakPtr())); | |
162 } | |
163 | |
164 void GetGlobalUsage(StorageType type) { | |
165 usage_ = -1; | |
166 unlimited_usage_ = -1; | |
167 quota_manager_->GetGlobalUsage( | |
168 type, | |
169 base::Bind(&QuotaManagerTest::DidGetGlobalUsage, | |
170 weak_factory_.GetWeakPtr())); | |
171 } | |
172 | |
173 void GetHostUsage(const std::string& host, StorageType type) { | |
174 usage_ = -1; | |
175 quota_manager_->GetHostUsage( | |
176 host, type, | |
177 base::Bind(&QuotaManagerTest::DidGetHostUsage, | |
178 weak_factory_.GetWeakPtr())); | |
179 } | |
180 | |
181 void RunAdditionalUsageAndQuotaTask(const GURL& origin, StorageType type) { | |
182 quota_manager_->GetUsageAndQuota( | |
183 origin, type, | |
184 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaAdditional, | |
185 weak_factory_.GetWeakPtr())); | |
186 } | |
187 | |
188 void DeleteClientOriginData(QuotaClient* client, | |
189 const GURL& origin, | |
190 StorageType type) { | |
191 DCHECK(client); | |
192 quota_status_ = kQuotaStatusUnknown; | |
193 client->DeleteOriginData( | |
194 origin, type, | |
195 base::Bind(&QuotaManagerTest::StatusCallback, | |
196 weak_factory_.GetWeakPtr())); | |
197 } | |
198 | |
199 void EvictOriginData(const GURL& origin, | |
200 StorageType type) { | |
201 quota_status_ = kQuotaStatusUnknown; | |
202 quota_manager_->EvictOriginData( | |
203 origin, type, | |
204 base::Bind(&QuotaManagerTest::StatusCallback, | |
205 weak_factory_.GetWeakPtr())); | |
206 } | |
207 | |
208 void DeleteOriginData(const GURL& origin, | |
209 StorageType type, | |
210 int quota_client_mask) { | |
211 quota_status_ = kQuotaStatusUnknown; | |
212 quota_manager_->DeleteOriginData( | |
213 origin, type, quota_client_mask, | |
214 base::Bind(&QuotaManagerTest::StatusCallback, | |
215 weak_factory_.GetWeakPtr())); | |
216 } | |
217 | |
218 void DeleteHostData(const std::string& host, | |
219 StorageType type, | |
220 int quota_client_mask) { | |
221 quota_status_ = kQuotaStatusUnknown; | |
222 quota_manager_->DeleteHostData( | |
223 host, type, quota_client_mask, | |
224 base::Bind(&QuotaManagerTest::StatusCallback, | |
225 weak_factory_.GetWeakPtr())); | |
226 } | |
227 | |
228 void GetAvailableSpace() { | |
229 quota_status_ = kQuotaStatusUnknown; | |
230 available_space_ = -1; | |
231 quota_manager_->GetAvailableSpace( | |
232 base::Bind(&QuotaManagerTest::DidGetAvailableSpace, | |
233 weak_factory_.GetWeakPtr())); | |
234 } | |
235 | |
236 void GetUsageAndQuotaForEviction() { | |
237 quota_status_ = kQuotaStatusUnknown; | |
238 usage_ = -1; | |
239 unlimited_usage_ = -1; | |
240 quota_ = -1; | |
241 available_space_ = -1; | |
242 quota_manager_->GetUsageAndQuotaForEviction( | |
243 base::Bind(&QuotaManagerTest::DidGetUsageAndQuotaForEviction, | |
244 weak_factory_.GetWeakPtr())); | |
245 } | |
246 | |
247 void GetCachedOrigins(StorageType type, std::set<GURL>* origins) { | |
248 ASSERT_TRUE(origins != NULL); | |
249 origins->clear(); | |
250 quota_manager_->GetCachedOrigins(type, origins); | |
251 } | |
252 | |
253 void NotifyStorageAccessed(QuotaClient* client, | |
254 const GURL& origin, | |
255 StorageType type) { | |
256 DCHECK(client); | |
257 quota_manager_->NotifyStorageAccessedInternal( | |
258 client->id(), origin, type, IncrementMockTime()); | |
259 } | |
260 | |
261 void DeleteOriginFromDatabase(const GURL& origin, StorageType type) { | |
262 quota_manager_->DeleteOriginFromDatabase(origin, type); | |
263 } | |
264 | |
265 void GetLRUOrigin(StorageType type) { | |
266 lru_origin_ = GURL(); | |
267 quota_manager_->GetLRUOrigin( | |
268 type, | |
269 base::Bind(&QuotaManagerTest::DidGetLRUOrigin, | |
270 weak_factory_.GetWeakPtr())); | |
271 } | |
272 | |
273 void NotifyOriginInUse(const GURL& origin) { | |
274 quota_manager_->NotifyOriginInUse(origin); | |
275 } | |
276 | |
277 void NotifyOriginNoLongerInUse(const GURL& origin) { | |
278 quota_manager_->NotifyOriginNoLongerInUse(origin); | |
279 } | |
280 | |
281 void GetOriginsModifiedSince(StorageType type, base::Time modified_since) { | |
282 modified_origins_.clear(); | |
283 modified_origins_type_ = kStorageTypeUnknown; | |
284 quota_manager_->GetOriginsModifiedSince( | |
285 type, modified_since, | |
286 base::Bind(&QuotaManagerTest::DidGetModifiedOrigins, | |
287 weak_factory_.GetWeakPtr())); | |
288 } | |
289 | |
290 void DumpQuotaTable() { | |
291 quota_entries_.clear(); | |
292 quota_manager_->DumpQuotaTable( | |
293 base::Bind(&QuotaManagerTest::DidDumpQuotaTable, | |
294 weak_factory_.GetWeakPtr())); | |
295 } | |
296 | |
297 void DumpOriginInfoTable() { | |
298 origin_info_entries_.clear(); | |
299 quota_manager_->DumpOriginInfoTable( | |
300 base::Bind(&QuotaManagerTest::DidDumpOriginInfoTable, | |
301 weak_factory_.GetWeakPtr())); | |
302 } | |
303 | |
304 void DidGetUsageInfo(const UsageInfoEntries& entries) { | |
305 usage_info_.insert(usage_info_.begin(), entries.begin(), entries.end()); | |
306 } | |
307 | |
308 void DidGetUsageAndQuota(QuotaStatusCode status, int64 usage, int64 quota) { | |
309 quota_status_ = status; | |
310 usage_ = usage; | |
311 quota_ = quota; | |
312 } | |
313 | |
314 void DidGetQuota(QuotaStatusCode status, | |
315 int64 quota) { | |
316 quota_status_ = status; | |
317 quota_ = quota; | |
318 } | |
319 | |
320 void DidGetAvailableSpace(QuotaStatusCode status, int64 available_space) { | |
321 quota_status_ = status; | |
322 available_space_ = available_space; | |
323 } | |
324 | |
325 void DidGetHostQuota(QuotaStatusCode status, | |
326 int64 quota) { | |
327 quota_status_ = status; | |
328 quota_ = quota; | |
329 } | |
330 | |
331 void DidGetGlobalUsage(int64 usage, | |
332 int64 unlimited_usage) { | |
333 usage_ = usage; | |
334 unlimited_usage_ = unlimited_usage; | |
335 } | |
336 | |
337 void DidGetHostUsage(int64 usage) { | |
338 usage_ = usage; | |
339 } | |
340 | |
341 void StatusCallback(QuotaStatusCode status) { | |
342 ++status_callback_count_; | |
343 quota_status_ = status; | |
344 } | |
345 | |
346 void DidGetUsageAndQuotaForEviction(QuotaStatusCode status, | |
347 const UsageAndQuota& usage_and_quota) { | |
348 quota_status_ = status; | |
349 limited_usage_ = usage_and_quota.global_limited_usage; | |
350 quota_ = usage_and_quota.quota; | |
351 available_space_ = usage_and_quota.available_disk_space; | |
352 } | |
353 | |
354 void DidGetLRUOrigin(const GURL& origin) { | |
355 lru_origin_ = origin; | |
356 } | |
357 | |
358 void DidGetModifiedOrigins(const std::set<GURL>& origins, StorageType type) { | |
359 modified_origins_ = origins; | |
360 modified_origins_type_ = type; | |
361 } | |
362 | |
363 void DidDumpQuotaTable(const QuotaTableEntries& entries) { | |
364 quota_entries_ = entries; | |
365 } | |
366 | |
367 void DidDumpOriginInfoTable(const OriginInfoTableEntries& entries) { | |
368 origin_info_entries_ = entries; | |
369 } | |
370 | |
371 void GetUsage_WithModifyTestBody(const StorageType type); | |
372 | |
373 void set_additional_callback_count(int c) { additional_callback_count_ = c; } | |
374 int additional_callback_count() const { return additional_callback_count_; } | |
375 void DidGetUsageAndQuotaAdditional( | |
376 QuotaStatusCode status, int64 usage, int64 quota) { | |
377 ++additional_callback_count_; | |
378 } | |
379 | |
380 QuotaManager* quota_manager() const { return quota_manager_.get(); } | |
381 void set_quota_manager(QuotaManager* quota_manager) { | |
382 quota_manager_ = quota_manager; | |
383 } | |
384 | |
385 MockSpecialStoragePolicy* mock_special_storage_policy() const { | |
386 return mock_special_storage_policy_.get(); | |
387 } | |
388 | |
389 QuotaStatusCode status() const { return quota_status_; } | |
390 const UsageInfoEntries& usage_info() const { return usage_info_; } | |
391 int64 usage() const { return usage_; } | |
392 int64 limited_usage() const { return limited_usage_; } | |
393 int64 unlimited_usage() const { return unlimited_usage_; } | |
394 int64 quota() const { return quota_; } | |
395 int64 available_space() const { return available_space_; } | |
396 const GURL& lru_origin() const { return lru_origin_; } | |
397 const std::set<GURL>& modified_origins() const { return modified_origins_; } | |
398 StorageType modified_origins_type() const { return modified_origins_type_; } | |
399 const QuotaTableEntries& quota_entries() const { return quota_entries_; } | |
400 const OriginInfoTableEntries& origin_info_entries() const { | |
401 return origin_info_entries_; | |
402 } | |
403 base::FilePath profile_path() const { return data_dir_.path(); } | |
404 int status_callback_count() const { return status_callback_count_; } | |
405 void reset_status_callback_count() { status_callback_count_ = 0; } | |
406 | |
407 private: | |
408 base::Time IncrementMockTime() { | |
409 ++mock_time_counter_; | |
410 return base::Time::FromDoubleT(mock_time_counter_ * 10.0); | |
411 } | |
412 | |
413 base::MessageLoop message_loop_; | |
414 base::ScopedTempDir data_dir_; | |
415 base::WeakPtrFactory<QuotaManagerTest> weak_factory_; | |
416 | |
417 scoped_refptr<QuotaManager> quota_manager_; | |
418 scoped_refptr<MockSpecialStoragePolicy> mock_special_storage_policy_; | |
419 | |
420 QuotaStatusCode quota_status_; | |
421 UsageInfoEntries usage_info_; | |
422 int64 usage_; | |
423 int64 limited_usage_; | |
424 int64 unlimited_usage_; | |
425 int64 quota_; | |
426 int64 available_space_; | |
427 GURL lru_origin_; | |
428 std::set<GURL> modified_origins_; | |
429 StorageType modified_origins_type_; | |
430 QuotaTableEntries quota_entries_; | |
431 OriginInfoTableEntries origin_info_entries_; | |
432 int status_callback_count_; | |
433 | |
434 int additional_callback_count_; | |
435 | |
436 int mock_time_counter_; | |
437 | |
438 DISALLOW_COPY_AND_ASSIGN(QuotaManagerTest); | |
439 }; | |
440 | |
441 TEST_F(QuotaManagerTest, GetUsageInfo) { | |
442 static const MockOriginData kData1[] = { | |
443 { "http://foo.com/", kTemp, 10 }, | |
444 { "http://foo.com:8080/", kTemp, 15 }, | |
445 { "http://bar.com/", kTemp, 20 }, | |
446 { "http://bar.com/", kPerm, 50 }, | |
447 }; | |
448 static const MockOriginData kData2[] = { | |
449 { "https://foo.com/", kTemp, 30 }, | |
450 { "https://foo.com:8081/", kTemp, 35 }, | |
451 { "http://bar.com/", kPerm, 40 }, | |
452 { "http://example.com/", kPerm, 40 }, | |
453 }; | |
454 RegisterClient(CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
455 QuotaClient::kFileSystem)); | |
456 RegisterClient(CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
457 QuotaClient::kDatabase)); | |
458 | |
459 GetUsageInfo(); | |
460 base::MessageLoop::current()->RunUntilIdle(); | |
461 | |
462 EXPECT_EQ(4U, usage_info().size()); | |
463 for (size_t i = 0; i < usage_info().size(); ++i) { | |
464 const UsageInfo& info = usage_info()[i]; | |
465 if (info.host == "foo.com" && info.type == kTemp) { | |
466 EXPECT_EQ(10 + 15 + 30 + 35, info.usage); | |
467 } else if (info.host == "bar.com" && info.type == kTemp) { | |
468 EXPECT_EQ(20, info.usage); | |
469 } else if (info.host == "bar.com" && info.type == kPerm) { | |
470 EXPECT_EQ(50 + 40, info.usage); | |
471 } else if (info.host == "example.com" && info.type == kPerm) { | |
472 EXPECT_EQ(40, info.usage); | |
473 } else { | |
474 ADD_FAILURE() | |
475 << "Unexpected host, type: " << info.host << ", " << info.type; | |
476 } | |
477 } | |
478 } | |
479 | |
480 TEST_F(QuotaManagerTest, GetUsageAndQuota_Simple) { | |
481 static const MockOriginData kData[] = { | |
482 { "http://foo.com/", kTemp, 10 }, | |
483 { "http://foo.com/", kPerm, 80 }, | |
484 }; | |
485 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
486 QuotaClient::kFileSystem)); | |
487 | |
488 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
489 base::MessageLoop::current()->RunUntilIdle(); | |
490 EXPECT_EQ(kQuotaStatusOk, status()); | |
491 EXPECT_EQ(80, usage()); | |
492 EXPECT_EQ(0, quota()); | |
493 | |
494 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
495 base::MessageLoop::current()->RunUntilIdle(); | |
496 EXPECT_EQ(kQuotaStatusOk, status()); | |
497 EXPECT_EQ(10, usage()); | |
498 EXPECT_LE(0, quota()); | |
499 int64 quota_returned_for_foo = quota(); | |
500 | |
501 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp); | |
502 base::MessageLoop::current()->RunUntilIdle(); | |
503 EXPECT_EQ(kQuotaStatusOk, status()); | |
504 EXPECT_EQ(0, usage()); | |
505 EXPECT_EQ(quota_returned_for_foo, quota()); | |
506 } | |
507 | |
508 TEST_F(QuotaManagerTest, GetUsage_NoClient) { | |
509 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
510 base::MessageLoop::current()->RunUntilIdle(); | |
511 EXPECT_EQ(kQuotaStatusOk, status()); | |
512 EXPECT_EQ(0, usage()); | |
513 | |
514 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
515 base::MessageLoop::current()->RunUntilIdle(); | |
516 EXPECT_EQ(kQuotaStatusOk, status()); | |
517 EXPECT_EQ(0, usage()); | |
518 | |
519 GetHostUsage("foo.com", kTemp); | |
520 base::MessageLoop::current()->RunUntilIdle(); | |
521 EXPECT_EQ(0, usage()); | |
522 | |
523 GetHostUsage("foo.com", kPerm); | |
524 base::MessageLoop::current()->RunUntilIdle(); | |
525 EXPECT_EQ(0, usage()); | |
526 | |
527 GetGlobalUsage(kTemp); | |
528 base::MessageLoop::current()->RunUntilIdle(); | |
529 EXPECT_EQ(0, usage()); | |
530 EXPECT_EQ(0, unlimited_usage()); | |
531 | |
532 GetGlobalUsage(kPerm); | |
533 base::MessageLoop::current()->RunUntilIdle(); | |
534 EXPECT_EQ(0, usage()); | |
535 EXPECT_EQ(0, unlimited_usage()); | |
536 } | |
537 | |
538 TEST_F(QuotaManagerTest, GetUsage_EmptyClient) { | |
539 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem)); | |
540 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
541 base::MessageLoop::current()->RunUntilIdle(); | |
542 EXPECT_EQ(kQuotaStatusOk, status()); | |
543 EXPECT_EQ(0, usage()); | |
544 | |
545 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
546 base::MessageLoop::current()->RunUntilIdle(); | |
547 EXPECT_EQ(kQuotaStatusOk, status()); | |
548 EXPECT_EQ(0, usage()); | |
549 | |
550 GetHostUsage("foo.com", kTemp); | |
551 base::MessageLoop::current()->RunUntilIdle(); | |
552 EXPECT_EQ(0, usage()); | |
553 | |
554 GetHostUsage("foo.com", kPerm); | |
555 base::MessageLoop::current()->RunUntilIdle(); | |
556 EXPECT_EQ(0, usage()); | |
557 | |
558 GetGlobalUsage(kTemp); | |
559 base::MessageLoop::current()->RunUntilIdle(); | |
560 EXPECT_EQ(0, usage()); | |
561 EXPECT_EQ(0, unlimited_usage()); | |
562 | |
563 GetGlobalUsage(kPerm); | |
564 base::MessageLoop::current()->RunUntilIdle(); | |
565 EXPECT_EQ(0, usage()); | |
566 EXPECT_EQ(0, unlimited_usage()); | |
567 } | |
568 | |
569 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_MultiOrigins) { | |
570 static const MockOriginData kData[] = { | |
571 { "http://foo.com/", kTemp, 10 }, | |
572 { "http://foo.com:8080/", kTemp, 20 }, | |
573 { "http://bar.com/", kTemp, 5 }, | |
574 { "https://bar.com/", kTemp, 7 }, | |
575 { "http://baz.com/", kTemp, 30 }, | |
576 { "http://foo.com/", kPerm, 40 }, | |
577 }; | |
578 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
579 QuotaClient::kFileSystem)); | |
580 | |
581 // This time explicitly sets a temporary global quota. | |
582 SetTemporaryGlobalQuota(100); | |
583 base::MessageLoop::current()->RunUntilIdle(); | |
584 EXPECT_EQ(kQuotaStatusOk, status()); | |
585 EXPECT_EQ(100, quota()); | |
586 | |
587 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
588 base::MessageLoop::current()->RunUntilIdle(); | |
589 EXPECT_EQ(kQuotaStatusOk, status()); | |
590 EXPECT_EQ(10 + 20, usage()); | |
591 | |
592 const int kPerHostQuota = 100 / kPerHostTemporaryPortion; | |
593 | |
594 // The host's quota should be its full portion of the global quota | |
595 // since global usage is under the global quota. | |
596 EXPECT_EQ(kPerHostQuota, quota()); | |
597 | |
598 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kTemp); | |
599 base::MessageLoop::current()->RunUntilIdle(); | |
600 EXPECT_EQ(kQuotaStatusOk, status()); | |
601 EXPECT_EQ(5 + 7, usage()); | |
602 EXPECT_EQ(kPerHostQuota, quota()); | |
603 } | |
604 | |
605 TEST_F(QuotaManagerTest, GetUsage_MultipleClients) { | |
606 static const MockOriginData kData1[] = { | |
607 { "http://foo.com/", kTemp, 1 }, | |
608 { "http://bar.com/", kTemp, 2 }, | |
609 { "http://bar.com/", kPerm, 4 }, | |
610 { "http://unlimited/", kPerm, 8 }, | |
611 { "http://installed/", kPerm, 16 }, | |
612 }; | |
613 static const MockOriginData kData2[] = { | |
614 { "https://foo.com/", kTemp, 128 }, | |
615 { "http://example.com/", kPerm, 256 }, | |
616 { "http://unlimited/", kTemp, 512 }, | |
617 { "http://installed/", kTemp, 1024 }, | |
618 }; | |
619 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); | |
620 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/")); | |
621 RegisterClient(CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
622 QuotaClient::kFileSystem)); | |
623 RegisterClient(CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
624 QuotaClient::kDatabase)); | |
625 | |
626 const int64 kTempQuotaBase = | |
627 GetAvailableDiskSpaceForTest(base::FilePath()) / kPerHostTemporaryPortion; | |
628 | |
629 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
630 base::MessageLoop::current()->RunUntilIdle(); | |
631 EXPECT_EQ(kQuotaStatusOk, status()); | |
632 EXPECT_EQ(1 + 128, usage()); | |
633 | |
634 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), kPerm); | |
635 base::MessageLoop::current()->RunUntilIdle(); | |
636 EXPECT_EQ(kQuotaStatusOk, status()); | |
637 EXPECT_EQ(4, usage()); | |
638 | |
639 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp); | |
640 base::MessageLoop::current()->RunUntilIdle(); | |
641 EXPECT_EQ(kQuotaStatusOk, status()); | |
642 EXPECT_EQ(512, usage()); | |
643 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota()); | |
644 | |
645 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm); | |
646 base::MessageLoop::current()->RunUntilIdle(); | |
647 EXPECT_EQ(kQuotaStatusOk, status()); | |
648 EXPECT_EQ(8, usage()); | |
649 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota()); | |
650 | |
651 GetAvailableSpace(); | |
652 base::MessageLoop::current()->RunUntilIdle(); | |
653 EXPECT_EQ(kQuotaStatusOk, status()); | |
654 EXPECT_LE(0, available_space()); | |
655 | |
656 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kTemp); | |
657 base::MessageLoop::current()->RunUntilIdle(); | |
658 EXPECT_EQ(kQuotaStatusOk, status()); | |
659 EXPECT_EQ(1024, usage()); | |
660 EXPECT_EQ(std::min(kAvailableSpaceForApp, kTempQuotaBase) + usage(), quota()); | |
661 | |
662 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm); | |
663 base::MessageLoop::current()->RunUntilIdle(); | |
664 EXPECT_EQ(kQuotaStatusOk, status()); | |
665 EXPECT_EQ(16, usage()); | |
666 EXPECT_EQ(usage(), quota()); // Over-budget case. | |
667 | |
668 GetGlobalUsage(kTemp); | |
669 base::MessageLoop::current()->RunUntilIdle(); | |
670 EXPECT_EQ(kQuotaStatusOk, status()); | |
671 EXPECT_EQ(1 + 2 + 128 + 512 + 1024, usage()); | |
672 EXPECT_EQ(512, unlimited_usage()); | |
673 | |
674 GetGlobalUsage(kPerm); | |
675 base::MessageLoop::current()->RunUntilIdle(); | |
676 EXPECT_EQ(kQuotaStatusOk, status()); | |
677 EXPECT_EQ(4 + 8 + 16 + 256, usage()); | |
678 EXPECT_EQ(8, unlimited_usage()); | |
679 } | |
680 | |
681 void QuotaManagerTest::GetUsage_WithModifyTestBody(const StorageType type) { | |
682 const MockOriginData data[] = { | |
683 { "http://foo.com/", type, 10 }, | |
684 { "http://foo.com:1/", type, 20 }, | |
685 }; | |
686 MockStorageClient* client = CreateClient(data, ARRAYSIZE_UNSAFE(data), | |
687 QuotaClient::kFileSystem); | |
688 RegisterClient(client); | |
689 | |
690 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type); | |
691 base::MessageLoop::current()->RunUntilIdle(); | |
692 EXPECT_EQ(kQuotaStatusOk, status()); | |
693 EXPECT_EQ(10 + 20, usage()); | |
694 | |
695 client->ModifyOriginAndNotify(GURL("http://foo.com/"), type, 30); | |
696 client->ModifyOriginAndNotify(GURL("http://foo.com:1/"), type, -5); | |
697 client->AddOriginAndNotify(GURL("https://foo.com/"), type, 1); | |
698 | |
699 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), type); | |
700 base::MessageLoop::current()->RunUntilIdle(); | |
701 EXPECT_EQ(kQuotaStatusOk, status()); | |
702 EXPECT_EQ(10 + 20 + 30 - 5 + 1, usage()); | |
703 int foo_usage = usage(); | |
704 | |
705 client->AddOriginAndNotify(GURL("http://bar.com/"), type, 40); | |
706 GetUsageAndQuotaForWebApps(GURL("http://bar.com/"), type); | |
707 base::MessageLoop::current()->RunUntilIdle(); | |
708 EXPECT_EQ(kQuotaStatusOk, status()); | |
709 EXPECT_EQ(40, usage()); | |
710 | |
711 GetGlobalUsage(type); | |
712 base::MessageLoop::current()->RunUntilIdle(); | |
713 EXPECT_EQ(foo_usage + 40, usage()); | |
714 EXPECT_EQ(0, unlimited_usage()); | |
715 } | |
716 | |
717 TEST_F(QuotaManagerTest, GetTemporaryUsage_WithModify) { | |
718 GetUsage_WithModifyTestBody(kTemp); | |
719 } | |
720 | |
721 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_WithAdditionalTasks) { | |
722 static const MockOriginData kData[] = { | |
723 { "http://foo.com/", kTemp, 10 }, | |
724 { "http://foo.com:8080/", kTemp, 20 }, | |
725 { "http://bar.com/", kTemp, 13 }, | |
726 { "http://foo.com/", kPerm, 40 }, | |
727 }; | |
728 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
729 QuotaClient::kFileSystem)); | |
730 SetTemporaryGlobalQuota(100); | |
731 base::MessageLoop::current()->RunUntilIdle(); | |
732 | |
733 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion; | |
734 | |
735 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
736 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
737 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
738 base::MessageLoop::current()->RunUntilIdle(); | |
739 EXPECT_EQ(kQuotaStatusOk, status()); | |
740 EXPECT_EQ(10 + 20, usage()); | |
741 EXPECT_EQ(kPerHostQuota, quota()); | |
742 | |
743 set_additional_callback_count(0); | |
744 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), | |
745 kTemp); | |
746 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
747 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kTemp); | |
748 base::MessageLoop::current()->RunUntilIdle(); | |
749 EXPECT_EQ(kQuotaStatusOk, status()); | |
750 EXPECT_EQ(10 + 20, usage()); | |
751 EXPECT_EQ(kPerHostQuota, quota()); | |
752 EXPECT_EQ(2, additional_callback_count()); | |
753 } | |
754 | |
755 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_NukeManager) { | |
756 static const MockOriginData kData[] = { | |
757 { "http://foo.com/", kTemp, 10 }, | |
758 { "http://foo.com:8080/", kTemp, 20 }, | |
759 { "http://bar.com/", kTemp, 13 }, | |
760 { "http://foo.com/", kPerm, 40 }, | |
761 }; | |
762 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
763 QuotaClient::kFileSystem)); | |
764 SetTemporaryGlobalQuota(100); | |
765 base::MessageLoop::current()->RunUntilIdle(); | |
766 | |
767 set_additional_callback_count(0); | |
768 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
769 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), | |
770 kTemp); | |
771 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), | |
772 kTemp); | |
773 | |
774 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients); | |
775 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients); | |
776 | |
777 // Nuke before waiting for callbacks. | |
778 set_quota_manager(NULL); | |
779 base::MessageLoop::current()->RunUntilIdle(); | |
780 EXPECT_EQ(kQuotaErrorAbort, status()); | |
781 } | |
782 | |
783 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Overbudget) { | |
784 static const MockOriginData kData[] = { | |
785 { "http://usage1/", kTemp, 1 }, | |
786 { "http://usage10/", kTemp, 10 }, | |
787 { "http://usage200/", kTemp, 200 }, | |
788 }; | |
789 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
790 QuotaClient::kFileSystem)); | |
791 SetTemporaryGlobalQuota(100); | |
792 base::MessageLoop::current()->RunUntilIdle(); | |
793 | |
794 const int kPerHostQuota = 100 / QuotaManager::kPerHostTemporaryPortion; | |
795 | |
796 GetUsageAndQuotaForWebApps(GURL("http://usage1/"), kTemp); | |
797 base::MessageLoop::current()->RunUntilIdle(); | |
798 EXPECT_EQ(kQuotaStatusOk, status()); | |
799 EXPECT_EQ(1, usage()); | |
800 EXPECT_EQ(1, quota()); // should be clamped to our current usage | |
801 | |
802 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp); | |
803 base::MessageLoop::current()->RunUntilIdle(); | |
804 EXPECT_EQ(kQuotaStatusOk, status()); | |
805 EXPECT_EQ(10, usage()); | |
806 EXPECT_EQ(10, quota()); | |
807 | |
808 GetUsageAndQuotaForWebApps(GURL("http://usage200/"), kTemp); | |
809 base::MessageLoop::current()->RunUntilIdle(); | |
810 EXPECT_EQ(kQuotaStatusOk, status()); | |
811 EXPECT_EQ(200, usage()); | |
812 EXPECT_EQ(kPerHostQuota, quota()); // should be clamped to the nominal quota | |
813 } | |
814 | |
815 TEST_F(QuotaManagerTest, GetTemporaryUsageAndQuota_Unlimited) { | |
816 static const MockOriginData kData[] = { | |
817 { "http://usage10/", kTemp, 10 }, | |
818 { "http://usage50/", kTemp, 50 }, | |
819 { "http://unlimited/", kTemp, 4000 }, | |
820 }; | |
821 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); | |
822 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
823 QuotaClient::kFileSystem); | |
824 RegisterClient(client); | |
825 | |
826 // Test when not overbugdet. | |
827 SetTemporaryGlobalQuota(1000); | |
828 base::MessageLoop::current()->RunUntilIdle(); | |
829 | |
830 GetGlobalUsage(kTemp); | |
831 base::MessageLoop::current()->RunUntilIdle(); | |
832 EXPECT_EQ(10 + 50 + 4000, usage()); | |
833 EXPECT_EQ(4000, unlimited_usage()); | |
834 | |
835 const int kPerHostQuotaFor1000 = | |
836 1000 / QuotaManager::kPerHostTemporaryPortion; | |
837 | |
838 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp); | |
839 base::MessageLoop::current()->RunUntilIdle(); | |
840 EXPECT_EQ(kQuotaStatusOk, status()); | |
841 EXPECT_EQ(10, usage()); | |
842 EXPECT_EQ(kPerHostQuotaFor1000, quota()); | |
843 | |
844 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp); | |
845 base::MessageLoop::current()->RunUntilIdle(); | |
846 EXPECT_EQ(kQuotaStatusOk, status()); | |
847 EXPECT_EQ(50, usage()); | |
848 EXPECT_EQ(kPerHostQuotaFor1000, quota()); | |
849 | |
850 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp); | |
851 base::MessageLoop::current()->RunUntilIdle(); | |
852 EXPECT_EQ(kQuotaStatusOk, status()); | |
853 EXPECT_EQ(4000, usage()); | |
854 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota()); | |
855 | |
856 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp); | |
857 base::MessageLoop::current()->RunUntilIdle(); | |
858 EXPECT_EQ(kQuotaStatusOk, status()); | |
859 EXPECT_EQ(0, usage()); | |
860 EXPECT_EQ(QuotaManager::kNoLimit, quota()); | |
861 | |
862 // Test when overbugdet. | |
863 SetTemporaryGlobalQuota(100); | |
864 base::MessageLoop::current()->RunUntilIdle(); | |
865 | |
866 const int kPerHostQuotaFor100 = | |
867 100 / QuotaManager::kPerHostTemporaryPortion; | |
868 | |
869 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp); | |
870 base::MessageLoop::current()->RunUntilIdle(); | |
871 EXPECT_EQ(kQuotaStatusOk, status()); | |
872 EXPECT_EQ(10, usage()); | |
873 EXPECT_EQ(kPerHostQuotaFor100, quota()); | |
874 | |
875 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp); | |
876 base::MessageLoop::current()->RunUntilIdle(); | |
877 EXPECT_EQ(kQuotaStatusOk, status()); | |
878 EXPECT_EQ(50, usage()); | |
879 EXPECT_EQ(kPerHostQuotaFor100, quota()); | |
880 | |
881 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp); | |
882 base::MessageLoop::current()->RunUntilIdle(); | |
883 EXPECT_EQ(kQuotaStatusOk, status()); | |
884 EXPECT_EQ(4000, usage()); | |
885 EXPECT_EQ(kAvailableSpaceForApp + usage(), quota()); | |
886 | |
887 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp); | |
888 base::MessageLoop::current()->RunUntilIdle(); | |
889 EXPECT_EQ(kQuotaStatusOk, status()); | |
890 EXPECT_EQ(0, usage()); | |
891 EXPECT_EQ(QuotaManager::kNoLimit, quota()); | |
892 | |
893 // Revoke the unlimited rights and make sure the change is noticed. | |
894 mock_special_storage_policy()->Reset(); | |
895 mock_special_storage_policy()->NotifyCleared(); | |
896 | |
897 GetGlobalUsage(kTemp); | |
898 base::MessageLoop::current()->RunUntilIdle(); | |
899 EXPECT_EQ(10 + 50 + 4000, usage()); | |
900 EXPECT_EQ(0, unlimited_usage()); | |
901 | |
902 GetUsageAndQuotaForWebApps(GURL("http://usage10/"), kTemp); | |
903 base::MessageLoop::current()->RunUntilIdle(); | |
904 EXPECT_EQ(kQuotaStatusOk, status()); | |
905 EXPECT_EQ(10, usage()); | |
906 EXPECT_EQ(10, quota()); // should be clamped to our current usage | |
907 | |
908 GetUsageAndQuotaForWebApps(GURL("http://usage50/"), kTemp); | |
909 base::MessageLoop::current()->RunUntilIdle(); | |
910 EXPECT_EQ(kQuotaStatusOk, status()); | |
911 EXPECT_EQ(50, usage()); | |
912 EXPECT_EQ(kPerHostQuotaFor100, quota()); | |
913 | |
914 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kTemp); | |
915 base::MessageLoop::current()->RunUntilIdle(); | |
916 EXPECT_EQ(kQuotaStatusOk, status()); | |
917 EXPECT_EQ(4000, usage()); | |
918 EXPECT_EQ(kPerHostQuotaFor100, quota()); | |
919 | |
920 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kTemp); | |
921 base::MessageLoop::current()->RunUntilIdle(); | |
922 EXPECT_EQ(kQuotaStatusOk, status()); | |
923 EXPECT_EQ(4000, usage()); | |
924 EXPECT_EQ(kPerHostQuotaFor100, quota()); | |
925 } | |
926 | |
927 TEST_F(QuotaManagerTest, OriginInUse) { | |
928 const GURL kFooOrigin("http://foo.com/"); | |
929 const GURL kBarOrigin("http://bar.com/"); | |
930 | |
931 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin)); | |
932 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 1 | |
933 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin)); | |
934 quota_manager()->NotifyOriginInUse(kFooOrigin); // count of 2 | |
935 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin)); | |
936 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin); // count of 1 | |
937 EXPECT_TRUE(quota_manager()->IsOriginInUse(kFooOrigin)); | |
938 | |
939 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin)); | |
940 quota_manager()->NotifyOriginInUse(kBarOrigin); | |
941 EXPECT_TRUE(quota_manager()->IsOriginInUse(kBarOrigin)); | |
942 quota_manager()->NotifyOriginNoLongerInUse(kBarOrigin); | |
943 EXPECT_FALSE(quota_manager()->IsOriginInUse(kBarOrigin)); | |
944 | |
945 quota_manager()->NotifyOriginNoLongerInUse(kFooOrigin); | |
946 EXPECT_FALSE(quota_manager()->IsOriginInUse(kFooOrigin)); | |
947 } | |
948 | |
949 TEST_F(QuotaManagerTest, GetAndSetPerststentHostQuota) { | |
950 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem)); | |
951 | |
952 GetPersistentHostQuota("foo.com"); | |
953 base::MessageLoop::current()->RunUntilIdle(); | |
954 EXPECT_EQ(0, quota()); | |
955 | |
956 SetPersistentHostQuota("foo.com", 100); | |
957 base::MessageLoop::current()->RunUntilIdle(); | |
958 EXPECT_EQ(100, quota()); | |
959 | |
960 GetPersistentHostQuota("foo.com"); | |
961 SetPersistentHostQuota("foo.com", 200); | |
962 GetPersistentHostQuota("foo.com"); | |
963 SetPersistentHostQuota("foo.com", 300000000000ll); | |
964 GetPersistentHostQuota("foo.com"); | |
965 base::MessageLoop::current()->RunUntilIdle(); | |
966 EXPECT_EQ(300000000000ll, quota()); | |
967 } | |
968 | |
969 TEST_F(QuotaManagerTest, GetAndSetPersistentUsageAndQuota) { | |
970 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem)); | |
971 | |
972 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
973 base::MessageLoop::current()->RunUntilIdle(); | |
974 EXPECT_EQ(kQuotaStatusOk, status()); | |
975 EXPECT_EQ(0, usage()); | |
976 EXPECT_EQ(0, quota()); | |
977 | |
978 SetPersistentHostQuota("foo.com", 100); | |
979 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
980 base::MessageLoop::current()->RunUntilIdle(); | |
981 EXPECT_EQ(kQuotaStatusOk, status()); | |
982 EXPECT_EQ(0, usage()); | |
983 EXPECT_EQ(100, quota()); | |
984 | |
985 // For installed app GetUsageAndQuotaForWebApps returns the capped quota. | |
986 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/")); | |
987 SetPersistentHostQuota("installed", kAvailableSpaceForApp + 100); | |
988 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kPerm); | |
989 base::MessageLoop::current()->RunUntilIdle(); | |
990 EXPECT_EQ(kAvailableSpaceForApp, quota()); | |
991 | |
992 // Ditto for unlimited apps. | |
993 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); | |
994 GetUsageAndQuotaForWebApps(GURL("http://unlimited/"), kPerm); | |
995 base::MessageLoop::current()->RunUntilIdle(); | |
996 EXPECT_EQ(kAvailableSpaceForApp, quota()); | |
997 | |
998 // GetUsageAndQuotaForStorageClient should just return 0 usage and | |
999 // kNoLimit quota. | |
1000 GetUsageAndQuotaForStorageClient(GURL("http://unlimited/"), kPerm); | |
1001 base::MessageLoop::current()->RunUntilIdle(); | |
1002 EXPECT_EQ(0, usage()); | |
1003 EXPECT_EQ(QuotaManager::kNoLimit, quota()); | |
1004 } | |
1005 | |
1006 TEST_F(QuotaManagerTest, GetSyncableQuota) { | |
1007 RegisterClient(CreateClient(NULL, 0, QuotaClient::kFileSystem)); | |
1008 | |
1009 // Pre-condition check: available disk space (for testing) is less than | |
1010 // the default quota for syncable storage. | |
1011 EXPECT_LE(kAvailableSpaceForApp, | |
1012 QuotaManager::kSyncableStorageDefaultHostQuota); | |
1013 | |
1014 // For installed apps the quota manager should return | |
1015 // kAvailableSpaceForApp as syncable quota (because of the pre-condition). | |
1016 mock_special_storage_policy()->GrantQueryDiskSize(GURL("http://installed/")); | |
1017 GetUsageAndQuotaForWebApps(GURL("http://installed/"), kSync); | |
1018 base::MessageLoop::current()->RunUntilIdle(); | |
1019 EXPECT_EQ(kQuotaStatusOk, status()); | |
1020 EXPECT_EQ(0, usage()); | |
1021 EXPECT_EQ(kAvailableSpaceForApp, quota()); | |
1022 | |
1023 // If it's not installed (which shouldn't happen in real case) it | |
1024 // should just return the default host quota for syncable. | |
1025 GetUsageAndQuotaForWebApps(GURL("http://foo/"), kSync); | |
1026 base::MessageLoop::current()->RunUntilIdle(); | |
1027 EXPECT_EQ(kQuotaStatusOk, status()); | |
1028 EXPECT_EQ(0, usage()); | |
1029 EXPECT_EQ(QuotaManager::kSyncableStorageDefaultHostQuota, quota()); | |
1030 } | |
1031 | |
1032 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_MultiOrigins) { | |
1033 static const MockOriginData kData[] = { | |
1034 { "http://foo.com/", kPerm, 10 }, | |
1035 { "http://foo.com:8080/", kPerm, 20 }, | |
1036 { "https://foo.com/", kPerm, 13 }, | |
1037 { "https://foo.com:8081/", kPerm, 19 }, | |
1038 { "http://bar.com/", kPerm, 5 }, | |
1039 { "https://bar.com/", kPerm, 7 }, | |
1040 { "http://baz.com/", kPerm, 30 }, | |
1041 { "http://foo.com/", kTemp, 40 }, | |
1042 }; | |
1043 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1044 QuotaClient::kFileSystem)); | |
1045 | |
1046 SetPersistentHostQuota("foo.com", 100); | |
1047 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1048 base::MessageLoop::current()->RunUntilIdle(); | |
1049 EXPECT_EQ(kQuotaStatusOk, status()); | |
1050 EXPECT_EQ(10 + 20 + 13 + 19, usage()); | |
1051 EXPECT_EQ(100, quota()); | |
1052 } | |
1053 | |
1054 TEST_F(QuotaManagerTest, GetPersistentUsage_WithModify) { | |
1055 GetUsage_WithModifyTestBody(kPerm); | |
1056 } | |
1057 | |
1058 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_WithAdditionalTasks) { | |
1059 static const MockOriginData kData[] = { | |
1060 { "http://foo.com/", kPerm, 10 }, | |
1061 { "http://foo.com:8080/", kPerm, 20 }, | |
1062 { "http://bar.com/", kPerm, 13 }, | |
1063 { "http://foo.com/", kTemp, 40 }, | |
1064 }; | |
1065 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1066 QuotaClient::kFileSystem)); | |
1067 SetPersistentHostQuota("foo.com", 100); | |
1068 | |
1069 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1070 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1071 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1072 base::MessageLoop::current()->RunUntilIdle(); | |
1073 EXPECT_EQ(kQuotaStatusOk, status()); | |
1074 EXPECT_EQ(10 + 20, usage()); | |
1075 EXPECT_EQ(100, quota()); | |
1076 | |
1077 set_additional_callback_count(0); | |
1078 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), | |
1079 kPerm); | |
1080 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1081 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm); | |
1082 base::MessageLoop::current()->RunUntilIdle(); | |
1083 EXPECT_EQ(kQuotaStatusOk, status()); | |
1084 EXPECT_EQ(10 + 20, usage()); | |
1085 EXPECT_EQ(2, additional_callback_count()); | |
1086 } | |
1087 | |
1088 TEST_F(QuotaManagerTest, GetPersistentUsageAndQuota_NukeManager) { | |
1089 static const MockOriginData kData[] = { | |
1090 { "http://foo.com/", kPerm, 10 }, | |
1091 { "http://foo.com:8080/", kPerm, 20 }, | |
1092 { "http://bar.com/", kPerm, 13 }, | |
1093 { "http://foo.com/", kTemp, 40 }, | |
1094 }; | |
1095 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1096 QuotaClient::kFileSystem)); | |
1097 SetPersistentHostQuota("foo.com", 100); | |
1098 | |
1099 set_additional_callback_count(0); | |
1100 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
1101 RunAdditionalUsageAndQuotaTask(GURL("http://foo.com/"), kPerm); | |
1102 RunAdditionalUsageAndQuotaTask(GURL("http://bar.com/"), kPerm); | |
1103 | |
1104 // Nuke before waiting for callbacks. | |
1105 set_quota_manager(NULL); | |
1106 base::MessageLoop::current()->RunUntilIdle(); | |
1107 EXPECT_EQ(kQuotaErrorAbort, status()); | |
1108 } | |
1109 | |
1110 TEST_F(QuotaManagerTest, GetUsage_Simple) { | |
1111 static const MockOriginData kData[] = { | |
1112 { "http://foo.com/", kPerm, 1 }, | |
1113 { "http://foo.com:1/", kPerm, 20 }, | |
1114 { "http://bar.com/", kTemp, 300 }, | |
1115 { "https://buz.com/", kTemp, 4000 }, | |
1116 { "http://buz.com/", kTemp, 50000 }, | |
1117 { "http://bar.com:1/", kPerm, 600000 }, | |
1118 { "http://foo.com/", kTemp, 7000000 }, | |
1119 }; | |
1120 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1121 QuotaClient::kFileSystem)); | |
1122 | |
1123 GetGlobalUsage(kPerm); | |
1124 base::MessageLoop::current()->RunUntilIdle(); | |
1125 EXPECT_EQ(usage(), 1 + 20 + 600000); | |
1126 EXPECT_EQ(0, unlimited_usage()); | |
1127 | |
1128 GetGlobalUsage(kTemp); | |
1129 base::MessageLoop::current()->RunUntilIdle(); | |
1130 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000); | |
1131 EXPECT_EQ(0, unlimited_usage()); | |
1132 | |
1133 GetHostUsage("foo.com", kPerm); | |
1134 base::MessageLoop::current()->RunUntilIdle(); | |
1135 EXPECT_EQ(usage(), 1 + 20); | |
1136 | |
1137 GetHostUsage("buz.com", kTemp); | |
1138 base::MessageLoop::current()->RunUntilIdle(); | |
1139 EXPECT_EQ(usage(), 4000 + 50000); | |
1140 } | |
1141 | |
1142 TEST_F(QuotaManagerTest, GetUsage_WithModification) { | |
1143 static const MockOriginData kData[] = { | |
1144 { "http://foo.com/", kPerm, 1 }, | |
1145 { "http://foo.com:1/", kPerm, 20 }, | |
1146 { "http://bar.com/", kTemp, 300 }, | |
1147 { "https://buz.com/", kTemp, 4000 }, | |
1148 { "http://buz.com/", kTemp, 50000 }, | |
1149 { "http://bar.com:1/", kPerm, 600000 }, | |
1150 { "http://foo.com/", kTemp, 7000000 }, | |
1151 }; | |
1152 | |
1153 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1154 QuotaClient::kFileSystem); | |
1155 RegisterClient(client); | |
1156 | |
1157 GetGlobalUsage(kPerm); | |
1158 base::MessageLoop::current()->RunUntilIdle(); | |
1159 EXPECT_EQ(usage(), 1 + 20 + 600000); | |
1160 EXPECT_EQ(0, unlimited_usage()); | |
1161 | |
1162 client->ModifyOriginAndNotify( | |
1163 GURL("http://foo.com/"), kPerm, 80000000); | |
1164 | |
1165 GetGlobalUsage(kPerm); | |
1166 base::MessageLoop::current()->RunUntilIdle(); | |
1167 EXPECT_EQ(usage(), 1 + 20 + 600000 + 80000000); | |
1168 EXPECT_EQ(0, unlimited_usage()); | |
1169 | |
1170 GetGlobalUsage(kTemp); | |
1171 base::MessageLoop::current()->RunUntilIdle(); | |
1172 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000); | |
1173 EXPECT_EQ(0, unlimited_usage()); | |
1174 | |
1175 client->ModifyOriginAndNotify( | |
1176 GURL("http://foo.com/"), kTemp, 1); | |
1177 | |
1178 GetGlobalUsage(kTemp); | |
1179 base::MessageLoop::current()->RunUntilIdle(); | |
1180 EXPECT_EQ(usage(), 300 + 4000 + 50000 + 7000000 + 1); | |
1181 EXPECT_EQ(0, unlimited_usage()); | |
1182 | |
1183 GetHostUsage("buz.com", kTemp); | |
1184 base::MessageLoop::current()->RunUntilIdle(); | |
1185 EXPECT_EQ(usage(), 4000 + 50000); | |
1186 | |
1187 client->ModifyOriginAndNotify( | |
1188 GURL("http://buz.com/"), kTemp, 900000000); | |
1189 | |
1190 GetHostUsage("buz.com", kTemp); | |
1191 base::MessageLoop::current()->RunUntilIdle(); | |
1192 EXPECT_EQ(usage(), 4000 + 50000 + 900000000); | |
1193 } | |
1194 | |
1195 TEST_F(QuotaManagerTest, GetUsage_WithDeleteOrigin) { | |
1196 static const MockOriginData kData[] = { | |
1197 { "http://foo.com/", kTemp, 1 }, | |
1198 { "http://foo.com:1/", kTemp, 20 }, | |
1199 { "http://foo.com/", kPerm, 300 }, | |
1200 { "http://bar.com/", kTemp, 4000 }, | |
1201 }; | |
1202 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1203 QuotaClient::kFileSystem); | |
1204 RegisterClient(client); | |
1205 | |
1206 GetGlobalUsage(kTemp); | |
1207 base::MessageLoop::current()->RunUntilIdle(); | |
1208 int64 predelete_global_tmp = usage(); | |
1209 | |
1210 GetHostUsage("foo.com", kTemp); | |
1211 base::MessageLoop::current()->RunUntilIdle(); | |
1212 int64 predelete_host_tmp = usage(); | |
1213 | |
1214 GetHostUsage("foo.com", kPerm); | |
1215 base::MessageLoop::current()->RunUntilIdle(); | |
1216 int64 predelete_host_pers = usage(); | |
1217 | |
1218 DeleteClientOriginData(client, GURL("http://foo.com/"), | |
1219 kTemp); | |
1220 base::MessageLoop::current()->RunUntilIdle(); | |
1221 EXPECT_EQ(kQuotaStatusOk, status()); | |
1222 | |
1223 GetGlobalUsage(kTemp); | |
1224 base::MessageLoop::current()->RunUntilIdle(); | |
1225 EXPECT_EQ(predelete_global_tmp - 1, usage()); | |
1226 | |
1227 GetHostUsage("foo.com", kTemp); | |
1228 base::MessageLoop::current()->RunUntilIdle(); | |
1229 EXPECT_EQ(predelete_host_tmp - 1, usage()); | |
1230 | |
1231 GetHostUsage("foo.com", kPerm); | |
1232 base::MessageLoop::current()->RunUntilIdle(); | |
1233 EXPECT_EQ(predelete_host_pers, usage()); | |
1234 } | |
1235 | |
1236 TEST_F(QuotaManagerTest, GetAvailableSpaceTest) { | |
1237 GetAvailableSpace(); | |
1238 base::MessageLoop::current()->RunUntilIdle(); | |
1239 EXPECT_EQ(kQuotaStatusOk, status()); | |
1240 EXPECT_LE(0, available_space()); | |
1241 } | |
1242 | |
1243 TEST_F(QuotaManagerTest, EvictOriginData) { | |
1244 static const MockOriginData kData1[] = { | |
1245 { "http://foo.com/", kTemp, 1 }, | |
1246 { "http://foo.com:1/", kTemp, 20 }, | |
1247 { "http://foo.com/", kPerm, 300 }, | |
1248 { "http://bar.com/", kTemp, 4000 }, | |
1249 }; | |
1250 static const MockOriginData kData2[] = { | |
1251 { "http://foo.com/", kTemp, 50000 }, | |
1252 { "http://foo.com:1/", kTemp, 6000 }, | |
1253 { "http://foo.com/", kPerm, 700 }, | |
1254 { "https://foo.com/", kTemp, 80 }, | |
1255 { "http://bar.com/", kTemp, 9 }, | |
1256 }; | |
1257 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
1258 QuotaClient::kFileSystem); | |
1259 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
1260 QuotaClient::kDatabase); | |
1261 RegisterClient(client1); | |
1262 RegisterClient(client2); | |
1263 | |
1264 GetGlobalUsage(kTemp); | |
1265 base::MessageLoop::current()->RunUntilIdle(); | |
1266 int64 predelete_global_tmp = usage(); | |
1267 | |
1268 GetHostUsage("foo.com", kTemp); | |
1269 base::MessageLoop::current()->RunUntilIdle(); | |
1270 int64 predelete_host_tmp = usage(); | |
1271 | |
1272 GetHostUsage("foo.com", kPerm); | |
1273 base::MessageLoop::current()->RunUntilIdle(); | |
1274 int64 predelete_host_pers = usage(); | |
1275 | |
1276 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData1); ++i) | |
1277 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown, | |
1278 GURL(kData1[i].origin), kData1[i].type); | |
1279 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData2); ++i) | |
1280 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown, | |
1281 GURL(kData2[i].origin), kData2[i].type); | |
1282 base::MessageLoop::current()->RunUntilIdle(); | |
1283 | |
1284 EvictOriginData(GURL("http://foo.com/"), kTemp); | |
1285 base::MessageLoop::current()->RunUntilIdle(); | |
1286 | |
1287 DumpOriginInfoTable(); | |
1288 base::MessageLoop::current()->RunUntilIdle(); | |
1289 | |
1290 typedef OriginInfoTableEntries::const_iterator iterator; | |
1291 for (iterator itr(origin_info_entries().begin()), | |
1292 end(origin_info_entries().end()); | |
1293 itr != end; ++itr) { | |
1294 if (itr->type == kTemp) | |
1295 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec()); | |
1296 } | |
1297 | |
1298 GetGlobalUsage(kTemp); | |
1299 base::MessageLoop::current()->RunUntilIdle(); | |
1300 EXPECT_EQ(predelete_global_tmp - (1 + 50000), usage()); | |
1301 | |
1302 GetHostUsage("foo.com", kTemp); | |
1303 base::MessageLoop::current()->RunUntilIdle(); | |
1304 EXPECT_EQ(predelete_host_tmp - (1 + 50000), usage()); | |
1305 | |
1306 GetHostUsage("foo.com", kPerm); | |
1307 base::MessageLoop::current()->RunUntilIdle(); | |
1308 EXPECT_EQ(predelete_host_pers, usage()); | |
1309 } | |
1310 | |
1311 TEST_F(QuotaManagerTest, EvictOriginDataWithDeletionError) { | |
1312 static const MockOriginData kData[] = { | |
1313 { "http://foo.com/", kTemp, 1 }, | |
1314 { "http://foo.com:1/", kTemp, 20 }, | |
1315 { "http://foo.com/", kPerm, 300 }, | |
1316 { "http://bar.com/", kTemp, 4000 }, | |
1317 }; | |
1318 static const int kNumberOfTemporaryOrigins = 3; | |
1319 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1320 QuotaClient::kFileSystem); | |
1321 RegisterClient(client); | |
1322 | |
1323 GetGlobalUsage(kTemp); | |
1324 base::MessageLoop::current()->RunUntilIdle(); | |
1325 int64 predelete_global_tmp = usage(); | |
1326 | |
1327 GetHostUsage("foo.com", kTemp); | |
1328 base::MessageLoop::current()->RunUntilIdle(); | |
1329 int64 predelete_host_tmp = usage(); | |
1330 | |
1331 GetHostUsage("foo.com", kPerm); | |
1332 base::MessageLoop::current()->RunUntilIdle(); | |
1333 int64 predelete_host_pers = usage(); | |
1334 | |
1335 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData); ++i) | |
1336 NotifyStorageAccessed(client, GURL(kData[i].origin), kData[i].type); | |
1337 base::MessageLoop::current()->RunUntilIdle(); | |
1338 | |
1339 client->AddOriginToErrorSet(GURL("http://foo.com/"), kTemp); | |
1340 | |
1341 for (int i = 0; | |
1342 i < QuotaManager::kThresholdOfErrorsToBeBlacklisted + 1; | |
1343 ++i) { | |
1344 EvictOriginData(GURL("http://foo.com/"), kTemp); | |
1345 base::MessageLoop::current()->RunUntilIdle(); | |
1346 EXPECT_EQ(kQuotaErrorInvalidModification, status()); | |
1347 } | |
1348 | |
1349 DumpOriginInfoTable(); | |
1350 base::MessageLoop::current()->RunUntilIdle(); | |
1351 | |
1352 bool found_origin_in_database = false; | |
1353 typedef OriginInfoTableEntries::const_iterator iterator; | |
1354 for (iterator itr(origin_info_entries().begin()), | |
1355 end(origin_info_entries().end()); | |
1356 itr != end; ++itr) { | |
1357 if (itr->type == kTemp && | |
1358 GURL("http://foo.com/") == itr->origin) { | |
1359 found_origin_in_database = true; | |
1360 break; | |
1361 } | |
1362 } | |
1363 // The origin "http://foo.com/" should be in the database. | |
1364 EXPECT_TRUE(found_origin_in_database); | |
1365 | |
1366 for (size_t i = 0; i < kNumberOfTemporaryOrigins - 1; ++i) { | |
1367 GetLRUOrigin(kTemp); | |
1368 base::MessageLoop::current()->RunUntilIdle(); | |
1369 EXPECT_FALSE(lru_origin().is_empty()); | |
1370 // The origin "http://foo.com/" should not be in the LRU list. | |
1371 EXPECT_NE(std::string("http://foo.com/"), lru_origin().spec()); | |
1372 DeleteOriginFromDatabase(lru_origin(), kTemp); | |
1373 base::MessageLoop::current()->RunUntilIdle(); | |
1374 } | |
1375 | |
1376 // Now the LRU list must be empty. | |
1377 GetLRUOrigin(kTemp); | |
1378 base::MessageLoop::current()->RunUntilIdle(); | |
1379 EXPECT_TRUE(lru_origin().is_empty()); | |
1380 | |
1381 // Deleting origins from the database should not affect the results of the | |
1382 // following checks. | |
1383 GetGlobalUsage(kTemp); | |
1384 base::MessageLoop::current()->RunUntilIdle(); | |
1385 EXPECT_EQ(predelete_global_tmp, usage()); | |
1386 | |
1387 GetHostUsage("foo.com", kTemp); | |
1388 base::MessageLoop::current()->RunUntilIdle(); | |
1389 EXPECT_EQ(predelete_host_tmp, usage()); | |
1390 | |
1391 GetHostUsage("foo.com", kPerm); | |
1392 base::MessageLoop::current()->RunUntilIdle(); | |
1393 EXPECT_EQ(predelete_host_pers, usage()); | |
1394 } | |
1395 | |
1396 TEST_F(QuotaManagerTest, GetUsageAndQuotaForEviction) { | |
1397 static const MockOriginData kData[] = { | |
1398 { "http://foo.com/", kTemp, 1 }, | |
1399 { "http://foo.com:1/", kTemp, 20 }, | |
1400 { "http://foo.com/", kPerm, 300 }, | |
1401 { "http://unlimited/", kTemp, 4000 }, | |
1402 }; | |
1403 | |
1404 mock_special_storage_policy()->AddUnlimited(GURL("http://unlimited/")); | |
1405 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1406 QuotaClient::kFileSystem); | |
1407 RegisterClient(client); | |
1408 | |
1409 SetTemporaryGlobalQuota(10000000); | |
1410 base::MessageLoop::current()->RunUntilIdle(); | |
1411 | |
1412 GetUsageAndQuotaForEviction(); | |
1413 base::MessageLoop::current()->RunUntilIdle(); | |
1414 EXPECT_EQ(kQuotaStatusOk, status()); | |
1415 EXPECT_EQ(21, limited_usage()); | |
1416 EXPECT_EQ(10000000, quota()); | |
1417 EXPECT_LE(0, available_space()); | |
1418 } | |
1419 | |
1420 TEST_F(QuotaManagerTest, DeleteHostDataSimple) { | |
1421 static const MockOriginData kData[] = { | |
1422 { "http://foo.com/", kTemp, 1 }, | |
1423 }; | |
1424 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1425 QuotaClient::kFileSystem); | |
1426 RegisterClient(client); | |
1427 | |
1428 GetGlobalUsage(kTemp); | |
1429 base::MessageLoop::current()->RunUntilIdle(); | |
1430 const int64 predelete_global_tmp = usage(); | |
1431 | |
1432 GetHostUsage("foo.com", kTemp); | |
1433 base::MessageLoop::current()->RunUntilIdle(); | |
1434 int64 predelete_host_tmp = usage(); | |
1435 | |
1436 GetHostUsage("foo.com", kPerm); | |
1437 base::MessageLoop::current()->RunUntilIdle(); | |
1438 int64 predelete_host_pers = usage(); | |
1439 | |
1440 DeleteHostData(std::string(), kTemp, kAllClients); | |
1441 base::MessageLoop::current()->RunUntilIdle(); | |
1442 EXPECT_EQ(kQuotaStatusOk, status()); | |
1443 | |
1444 GetGlobalUsage(kTemp); | |
1445 base::MessageLoop::current()->RunUntilIdle(); | |
1446 EXPECT_EQ(predelete_global_tmp, usage()); | |
1447 | |
1448 GetHostUsage("foo.com", kTemp); | |
1449 base::MessageLoop::current()->RunUntilIdle(); | |
1450 EXPECT_EQ(predelete_host_tmp, usage()); | |
1451 | |
1452 GetHostUsage("foo.com", kPerm); | |
1453 base::MessageLoop::current()->RunUntilIdle(); | |
1454 EXPECT_EQ(predelete_host_pers, usage()); | |
1455 | |
1456 DeleteHostData("foo.com", kTemp, kAllClients); | |
1457 base::MessageLoop::current()->RunUntilIdle(); | |
1458 EXPECT_EQ(kQuotaStatusOk, status()); | |
1459 | |
1460 GetGlobalUsage(kTemp); | |
1461 base::MessageLoop::current()->RunUntilIdle(); | |
1462 EXPECT_EQ(predelete_global_tmp - 1, usage()); | |
1463 | |
1464 GetHostUsage("foo.com", kTemp); | |
1465 base::MessageLoop::current()->RunUntilIdle(); | |
1466 EXPECT_EQ(predelete_host_tmp - 1, usage()); | |
1467 | |
1468 GetHostUsage("foo.com", kPerm); | |
1469 base::MessageLoop::current()->RunUntilIdle(); | |
1470 EXPECT_EQ(predelete_host_pers, usage()); | |
1471 } | |
1472 | |
1473 TEST_F(QuotaManagerTest, DeleteHostDataMultiple) { | |
1474 static const MockOriginData kData1[] = { | |
1475 { "http://foo.com/", kTemp, 1 }, | |
1476 { "http://foo.com:1/", kTemp, 20 }, | |
1477 { "http://foo.com/", kPerm, 300 }, | |
1478 { "http://bar.com/", kTemp, 4000 }, | |
1479 }; | |
1480 static const MockOriginData kData2[] = { | |
1481 { "http://foo.com/", kTemp, 50000 }, | |
1482 { "http://foo.com:1/", kTemp, 6000 }, | |
1483 { "http://foo.com/", kPerm, 700 }, | |
1484 { "https://foo.com/", kTemp, 80 }, | |
1485 { "http://bar.com/", kTemp, 9 }, | |
1486 }; | |
1487 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
1488 QuotaClient::kFileSystem); | |
1489 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
1490 QuotaClient::kDatabase); | |
1491 RegisterClient(client1); | |
1492 RegisterClient(client2); | |
1493 | |
1494 GetGlobalUsage(kTemp); | |
1495 base::MessageLoop::current()->RunUntilIdle(); | |
1496 const int64 predelete_global_tmp = usage(); | |
1497 | |
1498 GetHostUsage("foo.com", kTemp); | |
1499 base::MessageLoop::current()->RunUntilIdle(); | |
1500 const int64 predelete_foo_tmp = usage(); | |
1501 | |
1502 GetHostUsage("bar.com", kTemp); | |
1503 base::MessageLoop::current()->RunUntilIdle(); | |
1504 const int64 predelete_bar_tmp = usage(); | |
1505 | |
1506 GetHostUsage("foo.com", kPerm); | |
1507 base::MessageLoop::current()->RunUntilIdle(); | |
1508 const int64 predelete_foo_pers = usage(); | |
1509 | |
1510 GetHostUsage("bar.com", kPerm); | |
1511 base::MessageLoop::current()->RunUntilIdle(); | |
1512 const int64 predelete_bar_pers = usage(); | |
1513 | |
1514 reset_status_callback_count(); | |
1515 DeleteHostData("foo.com", kTemp, kAllClients); | |
1516 DeleteHostData("bar.com", kTemp, kAllClients); | |
1517 DeleteHostData("foo.com", kTemp, kAllClients); | |
1518 base::MessageLoop::current()->RunUntilIdle(); | |
1519 | |
1520 EXPECT_EQ(3, status_callback_count()); | |
1521 | |
1522 DumpOriginInfoTable(); | |
1523 base::MessageLoop::current()->RunUntilIdle(); | |
1524 | |
1525 typedef OriginInfoTableEntries::const_iterator iterator; | |
1526 for (iterator itr(origin_info_entries().begin()), | |
1527 end(origin_info_entries().end()); | |
1528 itr != end; ++itr) { | |
1529 if (itr->type == kTemp) { | |
1530 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec()); | |
1531 EXPECT_NE(std::string("http://foo.com:1/"), itr->origin.spec()); | |
1532 EXPECT_NE(std::string("https://foo.com/"), itr->origin.spec()); | |
1533 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec()); | |
1534 } | |
1535 } | |
1536 | |
1537 GetGlobalUsage(kTemp); | |
1538 base::MessageLoop::current()->RunUntilIdle(); | |
1539 EXPECT_EQ(predelete_global_tmp - (1 + 20 + 4000 + 50000 + 6000 + 80 + 9), | |
1540 usage()); | |
1541 | |
1542 GetHostUsage("foo.com", kTemp); | |
1543 base::MessageLoop::current()->RunUntilIdle(); | |
1544 EXPECT_EQ(predelete_foo_tmp - (1 + 20 + 50000 + 6000 + 80), usage()); | |
1545 | |
1546 GetHostUsage("bar.com", kTemp); | |
1547 base::MessageLoop::current()->RunUntilIdle(); | |
1548 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage()); | |
1549 | |
1550 GetHostUsage("foo.com", kPerm); | |
1551 base::MessageLoop::current()->RunUntilIdle(); | |
1552 EXPECT_EQ(predelete_foo_pers, usage()); | |
1553 | |
1554 GetHostUsage("bar.com", kPerm); | |
1555 base::MessageLoop::current()->RunUntilIdle(); | |
1556 EXPECT_EQ(predelete_bar_pers, usage()); | |
1557 } | |
1558 | |
1559 // Single-run DeleteOriginData cases must be well covered by | |
1560 // EvictOriginData tests. | |
1561 TEST_F(QuotaManagerTest, DeleteOriginDataMultiple) { | |
1562 static const MockOriginData kData1[] = { | |
1563 { "http://foo.com/", kTemp, 1 }, | |
1564 { "http://foo.com:1/", kTemp, 20 }, | |
1565 { "http://foo.com/", kPerm, 300 }, | |
1566 { "http://bar.com/", kTemp, 4000 }, | |
1567 }; | |
1568 static const MockOriginData kData2[] = { | |
1569 { "http://foo.com/", kTemp, 50000 }, | |
1570 { "http://foo.com:1/", kTemp, 6000 }, | |
1571 { "http://foo.com/", kPerm, 700 }, | |
1572 { "https://foo.com/", kTemp, 80 }, | |
1573 { "http://bar.com/", kTemp, 9 }, | |
1574 }; | |
1575 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
1576 QuotaClient::kFileSystem); | |
1577 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
1578 QuotaClient::kDatabase); | |
1579 RegisterClient(client1); | |
1580 RegisterClient(client2); | |
1581 | |
1582 GetGlobalUsage(kTemp); | |
1583 base::MessageLoop::current()->RunUntilIdle(); | |
1584 const int64 predelete_global_tmp = usage(); | |
1585 | |
1586 GetHostUsage("foo.com", kTemp); | |
1587 base::MessageLoop::current()->RunUntilIdle(); | |
1588 const int64 predelete_foo_tmp = usage(); | |
1589 | |
1590 GetHostUsage("bar.com", kTemp); | |
1591 base::MessageLoop::current()->RunUntilIdle(); | |
1592 const int64 predelete_bar_tmp = usage(); | |
1593 | |
1594 GetHostUsage("foo.com", kPerm); | |
1595 base::MessageLoop::current()->RunUntilIdle(); | |
1596 const int64 predelete_foo_pers = usage(); | |
1597 | |
1598 GetHostUsage("bar.com", kPerm); | |
1599 base::MessageLoop::current()->RunUntilIdle(); | |
1600 const int64 predelete_bar_pers = usage(); | |
1601 | |
1602 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData1); ++i) | |
1603 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown, | |
1604 GURL(kData1[i].origin), kData1[i].type); | |
1605 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData2); ++i) | |
1606 quota_manager()->NotifyStorageAccessed(QuotaClient::kUnknown, | |
1607 GURL(kData2[i].origin), kData2[i].type); | |
1608 base::MessageLoop::current()->RunUntilIdle(); | |
1609 | |
1610 reset_status_callback_count(); | |
1611 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients); | |
1612 DeleteOriginData(GURL("http://bar.com/"), kTemp, kAllClients); | |
1613 DeleteOriginData(GURL("http://foo.com/"), kTemp, kAllClients); | |
1614 base::MessageLoop::current()->RunUntilIdle(); | |
1615 | |
1616 EXPECT_EQ(3, status_callback_count()); | |
1617 | |
1618 DumpOriginInfoTable(); | |
1619 base::MessageLoop::current()->RunUntilIdle(); | |
1620 | |
1621 typedef OriginInfoTableEntries::const_iterator iterator; | |
1622 for (iterator itr(origin_info_entries().begin()), | |
1623 end(origin_info_entries().end()); | |
1624 itr != end; ++itr) { | |
1625 if (itr->type == kTemp) { | |
1626 EXPECT_NE(std::string("http://foo.com/"), itr->origin.spec()); | |
1627 EXPECT_NE(std::string("http://bar.com/"), itr->origin.spec()); | |
1628 } | |
1629 } | |
1630 | |
1631 GetGlobalUsage(kTemp); | |
1632 base::MessageLoop::current()->RunUntilIdle(); | |
1633 EXPECT_EQ(predelete_global_tmp - (1 + 4000 + 50000 + 9), usage()); | |
1634 | |
1635 GetHostUsage("foo.com", kTemp); | |
1636 base::MessageLoop::current()->RunUntilIdle(); | |
1637 EXPECT_EQ(predelete_foo_tmp - (1 + 50000), usage()); | |
1638 | |
1639 GetHostUsage("bar.com", kTemp); | |
1640 base::MessageLoop::current()->RunUntilIdle(); | |
1641 EXPECT_EQ(predelete_bar_tmp - (4000 + 9), usage()); | |
1642 | |
1643 GetHostUsage("foo.com", kPerm); | |
1644 base::MessageLoop::current()->RunUntilIdle(); | |
1645 EXPECT_EQ(predelete_foo_pers, usage()); | |
1646 | |
1647 GetHostUsage("bar.com", kPerm); | |
1648 base::MessageLoop::current()->RunUntilIdle(); | |
1649 EXPECT_EQ(predelete_bar_pers, usage()); | |
1650 } | |
1651 | |
1652 TEST_F(QuotaManagerTest, GetCachedOrigins) { | |
1653 static const MockOriginData kData[] = { | |
1654 { "http://a.com/", kTemp, 1 }, | |
1655 { "http://a.com:1/", kTemp, 20 }, | |
1656 { "http://b.com/", kPerm, 300 }, | |
1657 { "http://c.com/", kTemp, 4000 }, | |
1658 }; | |
1659 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1660 QuotaClient::kFileSystem); | |
1661 RegisterClient(client); | |
1662 | |
1663 // TODO(kinuko): Be careful when we add cache pruner. | |
1664 | |
1665 std::set<GURL> origins; | |
1666 GetCachedOrigins(kTemp, &origins); | |
1667 EXPECT_TRUE(origins.empty()); | |
1668 | |
1669 // No matter how we make queries the quota manager tries to cache all | |
1670 // the origins at startup. | |
1671 GetHostUsage("a.com", kTemp); | |
1672 base::MessageLoop::current()->RunUntilIdle(); | |
1673 GetCachedOrigins(kTemp, &origins); | |
1674 EXPECT_EQ(3U, origins.size()); | |
1675 | |
1676 GetHostUsage("b.com", kTemp); | |
1677 base::MessageLoop::current()->RunUntilIdle(); | |
1678 GetCachedOrigins(kTemp, &origins); | |
1679 EXPECT_EQ(3U, origins.size()); | |
1680 | |
1681 GetCachedOrigins(kPerm, &origins); | |
1682 EXPECT_TRUE(origins.empty()); | |
1683 | |
1684 GetGlobalUsage(kTemp); | |
1685 base::MessageLoop::current()->RunUntilIdle(); | |
1686 GetCachedOrigins(kTemp, &origins); | |
1687 EXPECT_EQ(3U, origins.size()); | |
1688 | |
1689 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData); ++i) { | |
1690 if (kData[i].type == kTemp) | |
1691 EXPECT_TRUE(origins.find(GURL(kData[i].origin)) != origins.end()); | |
1692 } | |
1693 } | |
1694 | |
1695 TEST_F(QuotaManagerTest, NotifyAndLRUOrigin) { | |
1696 static const MockOriginData kData[] = { | |
1697 { "http://a.com/", kTemp, 0 }, | |
1698 { "http://a.com:1/", kTemp, 0 }, | |
1699 { "https://a.com/", kTemp, 0 }, | |
1700 { "http://b.com/", kPerm, 0 }, // persistent | |
1701 { "http://c.com/", kTemp, 0 }, | |
1702 }; | |
1703 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1704 QuotaClient::kFileSystem); | |
1705 RegisterClient(client); | |
1706 | |
1707 GURL origin; | |
1708 GetLRUOrigin(kTemp); | |
1709 base::MessageLoop::current()->RunUntilIdle(); | |
1710 EXPECT_TRUE(lru_origin().is_empty()); | |
1711 | |
1712 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp); | |
1713 GetLRUOrigin(kTemp); | |
1714 base::MessageLoop::current()->RunUntilIdle(); | |
1715 EXPECT_EQ("http://a.com/", lru_origin().spec()); | |
1716 | |
1717 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm); | |
1718 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp); | |
1719 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp); | |
1720 GetLRUOrigin(kTemp); | |
1721 base::MessageLoop::current()->RunUntilIdle(); | |
1722 EXPECT_EQ("http://a.com/", lru_origin().spec()); | |
1723 | |
1724 DeleteOriginFromDatabase(lru_origin(), kTemp); | |
1725 GetLRUOrigin(kTemp); | |
1726 base::MessageLoop::current()->RunUntilIdle(); | |
1727 EXPECT_EQ("https://a.com/", lru_origin().spec()); | |
1728 | |
1729 DeleteOriginFromDatabase(lru_origin(), kTemp); | |
1730 GetLRUOrigin(kTemp); | |
1731 base::MessageLoop::current()->RunUntilIdle(); | |
1732 EXPECT_EQ("http://c.com/", lru_origin().spec()); | |
1733 } | |
1734 | |
1735 TEST_F(QuotaManagerTest, GetLRUOriginWithOriginInUse) { | |
1736 static const MockOriginData kData[] = { | |
1737 { "http://a.com/", kTemp, 0 }, | |
1738 { "http://a.com:1/", kTemp, 0 }, | |
1739 { "https://a.com/", kTemp, 0 }, | |
1740 { "http://b.com/", kPerm, 0 }, // persistent | |
1741 { "http://c.com/", kTemp, 0 }, | |
1742 }; | |
1743 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1744 QuotaClient::kFileSystem); | |
1745 RegisterClient(client); | |
1746 | |
1747 GURL origin; | |
1748 GetLRUOrigin(kTemp); | |
1749 base::MessageLoop::current()->RunUntilIdle(); | |
1750 EXPECT_TRUE(lru_origin().is_empty()); | |
1751 | |
1752 NotifyStorageAccessed(client, GURL("http://a.com/"), kTemp); | |
1753 NotifyStorageAccessed(client, GURL("http://b.com/"), kPerm); | |
1754 NotifyStorageAccessed(client, GURL("https://a.com/"), kTemp); | |
1755 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp); | |
1756 | |
1757 GetLRUOrigin(kTemp); | |
1758 base::MessageLoop::current()->RunUntilIdle(); | |
1759 EXPECT_EQ("http://a.com/", lru_origin().spec()); | |
1760 | |
1761 // Notify origin http://a.com is in use. | |
1762 NotifyOriginInUse(GURL("http://a.com/")); | |
1763 GetLRUOrigin(kTemp); | |
1764 base::MessageLoop::current()->RunUntilIdle(); | |
1765 EXPECT_EQ("https://a.com/", lru_origin().spec()); | |
1766 | |
1767 // Notify origin https://a.com is in use while GetLRUOrigin is running. | |
1768 GetLRUOrigin(kTemp); | |
1769 NotifyOriginInUse(GURL("https://a.com/")); | |
1770 base::MessageLoop::current()->RunUntilIdle(); | |
1771 // Post-filtering must have excluded the returned origin, so we will | |
1772 // see empty result here. | |
1773 EXPECT_TRUE(lru_origin().is_empty()); | |
1774 | |
1775 // Notify access for http://c.com while GetLRUOrigin is running. | |
1776 GetLRUOrigin(kTemp); | |
1777 NotifyStorageAccessed(client, GURL("http://c.com/"), kTemp); | |
1778 base::MessageLoop::current()->RunUntilIdle(); | |
1779 // Post-filtering must have excluded the returned origin, so we will | |
1780 // see empty result here. | |
1781 EXPECT_TRUE(lru_origin().is_empty()); | |
1782 | |
1783 NotifyOriginNoLongerInUse(GURL("http://a.com/")); | |
1784 NotifyOriginNoLongerInUse(GURL("https://a.com/")); | |
1785 GetLRUOrigin(kTemp); | |
1786 base::MessageLoop::current()->RunUntilIdle(); | |
1787 EXPECT_EQ("http://a.com/", lru_origin().spec()); | |
1788 } | |
1789 | |
1790 TEST_F(QuotaManagerTest, GetOriginsModifiedSince) { | |
1791 static const MockOriginData kData[] = { | |
1792 { "http://a.com/", kTemp, 0 }, | |
1793 { "http://a.com:1/", kTemp, 0 }, | |
1794 { "https://a.com/", kTemp, 0 }, | |
1795 { "http://b.com/", kPerm, 0 }, // persistent | |
1796 { "http://c.com/", kTemp, 0 }, | |
1797 }; | |
1798 MockStorageClient* client = CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
1799 QuotaClient::kFileSystem); | |
1800 RegisterClient(client); | |
1801 | |
1802 GetOriginsModifiedSince(kTemp, base::Time()); | |
1803 base::MessageLoop::current()->RunUntilIdle(); | |
1804 EXPECT_TRUE(modified_origins().empty()); | |
1805 EXPECT_EQ(modified_origins_type(), kTemp); | |
1806 | |
1807 base::Time time1 = client->IncrementMockTime(); | |
1808 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10); | |
1809 client->ModifyOriginAndNotify(GURL("http://a.com:1/"), kTemp, 10); | |
1810 client->ModifyOriginAndNotify(GURL("http://b.com/"), kPerm, 10); | |
1811 base::Time time2 = client->IncrementMockTime(); | |
1812 client->ModifyOriginAndNotify(GURL("https://a.com/"), kTemp, 10); | |
1813 client->ModifyOriginAndNotify(GURL("http://c.com/"), kTemp, 10); | |
1814 base::Time time3 = client->IncrementMockTime(); | |
1815 | |
1816 GetOriginsModifiedSince(kTemp, time1); | |
1817 base::MessageLoop::current()->RunUntilIdle(); | |
1818 EXPECT_EQ(4U, modified_origins().size()); | |
1819 EXPECT_EQ(modified_origins_type(), kTemp); | |
1820 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kData); ++i) { | |
1821 if (kData[i].type == kTemp) | |
1822 EXPECT_EQ(1U, modified_origins().count(GURL(kData[i].origin))); | |
1823 } | |
1824 | |
1825 GetOriginsModifiedSince(kTemp, time2); | |
1826 base::MessageLoop::current()->RunUntilIdle(); | |
1827 EXPECT_EQ(2U, modified_origins().size()); | |
1828 | |
1829 GetOriginsModifiedSince(kTemp, time3); | |
1830 base::MessageLoop::current()->RunUntilIdle(); | |
1831 EXPECT_TRUE(modified_origins().empty()); | |
1832 EXPECT_EQ(modified_origins_type(), kTemp); | |
1833 | |
1834 client->ModifyOriginAndNotify(GURL("http://a.com/"), kTemp, 10); | |
1835 | |
1836 GetOriginsModifiedSince(kTemp, time3); | |
1837 base::MessageLoop::current()->RunUntilIdle(); | |
1838 EXPECT_EQ(1U, modified_origins().size()); | |
1839 EXPECT_EQ(1U, modified_origins().count(GURL("http://a.com/"))); | |
1840 EXPECT_EQ(modified_origins_type(), kTemp); | |
1841 } | |
1842 | |
1843 TEST_F(QuotaManagerTest, DumpQuotaTable) { | |
1844 SetPersistentHostQuota("example1.com", 1); | |
1845 SetPersistentHostQuota("example2.com", 20); | |
1846 SetPersistentHostQuota("example3.com", 300); | |
1847 base::MessageLoop::current()->RunUntilIdle(); | |
1848 | |
1849 DumpQuotaTable(); | |
1850 base::MessageLoop::current()->RunUntilIdle(); | |
1851 | |
1852 const QuotaTableEntry kEntries[] = { | |
1853 QuotaTableEntry("example1.com", kPerm, 1), | |
1854 QuotaTableEntry("example2.com", kPerm, 20), | |
1855 QuotaTableEntry("example3.com", kPerm, 300), | |
1856 }; | |
1857 std::set<QuotaTableEntry> entries | |
1858 (kEntries, kEntries + ARRAYSIZE_UNSAFE(kEntries)); | |
1859 | |
1860 typedef QuotaTableEntries::const_iterator iterator; | |
1861 for (iterator itr(quota_entries().begin()), end(quota_entries().end()); | |
1862 itr != end; ++itr) { | |
1863 SCOPED_TRACE(testing::Message() | |
1864 << "host = " << itr->host << ", " | |
1865 << "quota = " << itr->quota); | |
1866 EXPECT_EQ(1u, entries.erase(*itr)); | |
1867 } | |
1868 EXPECT_TRUE(entries.empty()); | |
1869 } | |
1870 | |
1871 TEST_F(QuotaManagerTest, DumpOriginInfoTable) { | |
1872 using std::make_pair; | |
1873 | |
1874 quota_manager()->NotifyStorageAccessed( | |
1875 QuotaClient::kUnknown, | |
1876 GURL("http://example.com/"), | |
1877 kTemp); | |
1878 quota_manager()->NotifyStorageAccessed( | |
1879 QuotaClient::kUnknown, | |
1880 GURL("http://example.com/"), | |
1881 kPerm); | |
1882 quota_manager()->NotifyStorageAccessed( | |
1883 QuotaClient::kUnknown, | |
1884 GURL("http://example.com/"), | |
1885 kPerm); | |
1886 base::MessageLoop::current()->RunUntilIdle(); | |
1887 | |
1888 DumpOriginInfoTable(); | |
1889 base::MessageLoop::current()->RunUntilIdle(); | |
1890 | |
1891 typedef std::pair<GURL, StorageType> TypedOrigin; | |
1892 typedef std::pair<TypedOrigin, int> Entry; | |
1893 const Entry kEntries[] = { | |
1894 make_pair(make_pair(GURL("http://example.com/"), kTemp), 1), | |
1895 make_pair(make_pair(GURL("http://example.com/"), kPerm), 2), | |
1896 }; | |
1897 std::set<Entry> entries | |
1898 (kEntries, kEntries + ARRAYSIZE_UNSAFE(kEntries)); | |
1899 | |
1900 typedef OriginInfoTableEntries::const_iterator iterator; | |
1901 for (iterator itr(origin_info_entries().begin()), | |
1902 end(origin_info_entries().end()); | |
1903 itr != end; ++itr) { | |
1904 SCOPED_TRACE(testing::Message() | |
1905 << "host = " << itr->origin << ", " | |
1906 << "type = " << itr->type << ", " | |
1907 << "used_count = " << itr->used_count); | |
1908 EXPECT_EQ(1u, entries.erase( | |
1909 make_pair(make_pair(itr->origin, itr->type), | |
1910 itr->used_count))); | |
1911 } | |
1912 EXPECT_TRUE(entries.empty()); | |
1913 } | |
1914 | |
1915 TEST_F(QuotaManagerTest, QuotaForEmptyHost) { | |
1916 GetPersistentHostQuota(std::string()); | |
1917 base::MessageLoop::current()->RunUntilIdle(); | |
1918 EXPECT_EQ(kQuotaStatusOk, status()); | |
1919 EXPECT_EQ(0, quota()); | |
1920 | |
1921 SetPersistentHostQuota(std::string(), 10); | |
1922 base::MessageLoop::current()->RunUntilIdle(); | |
1923 EXPECT_EQ(kQuotaErrorNotSupported, status()); | |
1924 } | |
1925 | |
1926 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleOrigin) { | |
1927 static const MockOriginData kData1[] = { | |
1928 { "http://foo.com/", kTemp, 1 }, | |
1929 }; | |
1930 static const MockOriginData kData2[] = { | |
1931 { "http://foo.com/", kTemp, 2 }, | |
1932 }; | |
1933 static const MockOriginData kData3[] = { | |
1934 { "http://foo.com/", kTemp, 4 }, | |
1935 }; | |
1936 static const MockOriginData kData4[] = { | |
1937 { "http://foo.com/", kTemp, 8 }, | |
1938 }; | |
1939 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
1940 QuotaClient::kFileSystem); | |
1941 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
1942 QuotaClient::kAppcache); | |
1943 MockStorageClient* client3 = CreateClient(kData3, ARRAYSIZE_UNSAFE(kData3), | |
1944 QuotaClient::kDatabase); | |
1945 MockStorageClient* client4 = CreateClient(kData4, ARRAYSIZE_UNSAFE(kData4), | |
1946 QuotaClient::kIndexedDatabase); | |
1947 RegisterClient(client1); | |
1948 RegisterClient(client2); | |
1949 RegisterClient(client3); | |
1950 RegisterClient(client4); | |
1951 | |
1952 GetHostUsage("foo.com", kTemp); | |
1953 base::MessageLoop::current()->RunUntilIdle(); | |
1954 const int64 predelete_foo_tmp = usage(); | |
1955 | |
1956 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kFileSystem); | |
1957 base::MessageLoop::current()->RunUntilIdle(); | |
1958 GetHostUsage("foo.com", kTemp); | |
1959 base::MessageLoop::current()->RunUntilIdle(); | |
1960 EXPECT_EQ(predelete_foo_tmp - 1, usage()); | |
1961 | |
1962 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kAppcache); | |
1963 base::MessageLoop::current()->RunUntilIdle(); | |
1964 GetHostUsage("foo.com", kTemp); | |
1965 base::MessageLoop::current()->RunUntilIdle(); | |
1966 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage()); | |
1967 | |
1968 DeleteOriginData(GURL("http://foo.com/"), kTemp, QuotaClient::kDatabase); | |
1969 base::MessageLoop::current()->RunUntilIdle(); | |
1970 GetHostUsage("foo.com", kTemp); | |
1971 base::MessageLoop::current()->RunUntilIdle(); | |
1972 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage()); | |
1973 | |
1974 DeleteOriginData(GURL("http://foo.com/"), kTemp, | |
1975 QuotaClient::kIndexedDatabase); | |
1976 base::MessageLoop::current()->RunUntilIdle(); | |
1977 GetHostUsage("foo.com", kTemp); | |
1978 base::MessageLoop::current()->RunUntilIdle(); | |
1979 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); | |
1980 } | |
1981 | |
1982 TEST_F(QuotaManagerTest, DeleteSpecificClientTypeSingleHost) { | |
1983 static const MockOriginData kData1[] = { | |
1984 { "http://foo.com:1111/", kTemp, 1 }, | |
1985 }; | |
1986 static const MockOriginData kData2[] = { | |
1987 { "http://foo.com:2222/", kTemp, 2 }, | |
1988 }; | |
1989 static const MockOriginData kData3[] = { | |
1990 { "http://foo.com:3333/", kTemp, 4 }, | |
1991 }; | |
1992 static const MockOriginData kData4[] = { | |
1993 { "http://foo.com:4444/", kTemp, 8 }, | |
1994 }; | |
1995 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
1996 QuotaClient::kFileSystem); | |
1997 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
1998 QuotaClient::kAppcache); | |
1999 MockStorageClient* client3 = CreateClient(kData3, ARRAYSIZE_UNSAFE(kData3), | |
2000 QuotaClient::kDatabase); | |
2001 MockStorageClient* client4 = CreateClient(kData4, ARRAYSIZE_UNSAFE(kData4), | |
2002 QuotaClient::kIndexedDatabase); | |
2003 RegisterClient(client1); | |
2004 RegisterClient(client2); | |
2005 RegisterClient(client3); | |
2006 RegisterClient(client4); | |
2007 | |
2008 GetHostUsage("foo.com", kTemp); | |
2009 base::MessageLoop::current()->RunUntilIdle(); | |
2010 const int64 predelete_foo_tmp = usage(); | |
2011 | |
2012 DeleteHostData("foo.com", kTemp, QuotaClient::kFileSystem); | |
2013 base::MessageLoop::current()->RunUntilIdle(); | |
2014 GetHostUsage("foo.com", kTemp); | |
2015 base::MessageLoop::current()->RunUntilIdle(); | |
2016 EXPECT_EQ(predelete_foo_tmp - 1, usage()); | |
2017 | |
2018 DeleteHostData("foo.com", kTemp, QuotaClient::kAppcache); | |
2019 base::MessageLoop::current()->RunUntilIdle(); | |
2020 GetHostUsage("foo.com", kTemp); | |
2021 base::MessageLoop::current()->RunUntilIdle(); | |
2022 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage()); | |
2023 | |
2024 DeleteHostData("foo.com", kTemp, QuotaClient::kDatabase); | |
2025 base::MessageLoop::current()->RunUntilIdle(); | |
2026 GetHostUsage("foo.com", kTemp); | |
2027 base::MessageLoop::current()->RunUntilIdle(); | |
2028 EXPECT_EQ(predelete_foo_tmp - 4 - 2 - 1, usage()); | |
2029 | |
2030 DeleteHostData("foo.com", kTemp, QuotaClient::kIndexedDatabase); | |
2031 base::MessageLoop::current()->RunUntilIdle(); | |
2032 GetHostUsage("foo.com", kTemp); | |
2033 base::MessageLoop::current()->RunUntilIdle(); | |
2034 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); | |
2035 } | |
2036 | |
2037 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleOrigin) { | |
2038 static const MockOriginData kData1[] = { | |
2039 { "http://foo.com/", kTemp, 1 }, | |
2040 }; | |
2041 static const MockOriginData kData2[] = { | |
2042 { "http://foo.com/", kTemp, 2 }, | |
2043 }; | |
2044 static const MockOriginData kData3[] = { | |
2045 { "http://foo.com/", kTemp, 4 }, | |
2046 }; | |
2047 static const MockOriginData kData4[] = { | |
2048 { "http://foo.com/", kTemp, 8 }, | |
2049 }; | |
2050 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
2051 QuotaClient::kFileSystem); | |
2052 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
2053 QuotaClient::kAppcache); | |
2054 MockStorageClient* client3 = CreateClient(kData3, ARRAYSIZE_UNSAFE(kData3), | |
2055 QuotaClient::kDatabase); | |
2056 MockStorageClient* client4 = CreateClient(kData4, ARRAYSIZE_UNSAFE(kData4), | |
2057 QuotaClient::kIndexedDatabase); | |
2058 RegisterClient(client1); | |
2059 RegisterClient(client2); | |
2060 RegisterClient(client3); | |
2061 RegisterClient(client4); | |
2062 | |
2063 GetHostUsage("foo.com", kTemp); | |
2064 base::MessageLoop::current()->RunUntilIdle(); | |
2065 const int64 predelete_foo_tmp = usage(); | |
2066 | |
2067 DeleteOriginData(GURL("http://foo.com/"), kTemp, | |
2068 QuotaClient::kFileSystem | QuotaClient::kDatabase); | |
2069 base::MessageLoop::current()->RunUntilIdle(); | |
2070 GetHostUsage("foo.com", kTemp); | |
2071 base::MessageLoop::current()->RunUntilIdle(); | |
2072 EXPECT_EQ(predelete_foo_tmp - 4 - 1, usage()); | |
2073 | |
2074 DeleteOriginData(GURL("http://foo.com/"), kTemp, | |
2075 QuotaClient::kAppcache | QuotaClient::kIndexedDatabase); | |
2076 base::MessageLoop::current()->RunUntilIdle(); | |
2077 GetHostUsage("foo.com", kTemp); | |
2078 base::MessageLoop::current()->RunUntilIdle(); | |
2079 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); | |
2080 } | |
2081 | |
2082 TEST_F(QuotaManagerTest, DeleteMultipleClientTypesSingleHost) { | |
2083 static const MockOriginData kData1[] = { | |
2084 { "http://foo.com:1111/", kTemp, 1 }, | |
2085 }; | |
2086 static const MockOriginData kData2[] = { | |
2087 { "http://foo.com:2222/", kTemp, 2 }, | |
2088 }; | |
2089 static const MockOriginData kData3[] = { | |
2090 { "http://foo.com:3333/", kTemp, 4 }, | |
2091 }; | |
2092 static const MockOriginData kData4[] = { | |
2093 { "http://foo.com:4444/", kTemp, 8 }, | |
2094 }; | |
2095 MockStorageClient* client1 = CreateClient(kData1, ARRAYSIZE_UNSAFE(kData1), | |
2096 QuotaClient::kFileSystem); | |
2097 MockStorageClient* client2 = CreateClient(kData2, ARRAYSIZE_UNSAFE(kData2), | |
2098 QuotaClient::kAppcache); | |
2099 MockStorageClient* client3 = CreateClient(kData3, ARRAYSIZE_UNSAFE(kData3), | |
2100 QuotaClient::kDatabase); | |
2101 MockStorageClient* client4 = CreateClient(kData4, ARRAYSIZE_UNSAFE(kData4), | |
2102 QuotaClient::kIndexedDatabase); | |
2103 RegisterClient(client1); | |
2104 RegisterClient(client2); | |
2105 RegisterClient(client3); | |
2106 RegisterClient(client4); | |
2107 | |
2108 GetHostUsage("foo.com", kTemp); | |
2109 base::MessageLoop::current()->RunUntilIdle(); | |
2110 const int64 predelete_foo_tmp = usage(); | |
2111 | |
2112 DeleteHostData("foo.com", kTemp, | |
2113 QuotaClient::kFileSystem | QuotaClient::kAppcache); | |
2114 base::MessageLoop::current()->RunUntilIdle(); | |
2115 GetHostUsage("foo.com", kTemp); | |
2116 base::MessageLoop::current()->RunUntilIdle(); | |
2117 EXPECT_EQ(predelete_foo_tmp - 2 - 1, usage()); | |
2118 | |
2119 DeleteHostData("foo.com", kTemp, | |
2120 QuotaClient::kDatabase | QuotaClient::kIndexedDatabase); | |
2121 base::MessageLoop::current()->RunUntilIdle(); | |
2122 GetHostUsage("foo.com", kTemp); | |
2123 base::MessageLoop::current()->RunUntilIdle(); | |
2124 EXPECT_EQ(predelete_foo_tmp - 8 - 4 - 2 - 1, usage()); | |
2125 } | |
2126 | |
2127 TEST_F(QuotaManagerTest, GetUsageAndQuota_Incognito) { | |
2128 ResetQuotaManager(true); | |
2129 | |
2130 static const MockOriginData kData[] = { | |
2131 { "http://foo.com/", kTemp, 10 }, | |
2132 { "http://foo.com/", kPerm, 80 }, | |
2133 }; | |
2134 RegisterClient(CreateClient(kData, ARRAYSIZE_UNSAFE(kData), | |
2135 QuotaClient::kFileSystem)); | |
2136 | |
2137 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
2138 base::MessageLoop::current()->RunUntilIdle(); | |
2139 EXPECT_EQ(kQuotaStatusOk, status()); | |
2140 EXPECT_EQ(80, usage()); | |
2141 EXPECT_EQ(0, quota()); | |
2142 | |
2143 SetTemporaryGlobalQuota(100); | |
2144 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
2145 base::MessageLoop::current()->RunUntilIdle(); | |
2146 EXPECT_EQ(kQuotaStatusOk, status()); | |
2147 EXPECT_EQ(10, usage()); | |
2148 EXPECT_LE(std::min(static_cast<int64>(100 / kPerHostTemporaryPortion), | |
2149 QuotaManager::kIncognitoDefaultQuotaLimit), quota()); | |
2150 | |
2151 mock_special_storage_policy()->AddUnlimited(GURL("http://foo.com/")); | |
2152 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kPerm); | |
2153 base::MessageLoop::current()->RunUntilIdle(); | |
2154 EXPECT_EQ(kQuotaStatusOk, status()); | |
2155 EXPECT_EQ(80, usage()); | |
2156 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota()); | |
2157 | |
2158 GetUsageAndQuotaForWebApps(GURL("http://foo.com/"), kTemp); | |
2159 base::MessageLoop::current()->RunUntilIdle(); | |
2160 EXPECT_EQ(kQuotaStatusOk, status()); | |
2161 EXPECT_EQ(10, usage()); | |
2162 EXPECT_EQ(QuotaManager::kIncognitoDefaultQuotaLimit, quota()); | |
2163 } | |
2164 | |
2165 } // namespace quota | |
OLD | NEW |