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.
6 #include "base/location.h"
7 #include "base/run_loop.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "content/public/test/mock_special_storage_policy.h"
11 #include "net/base/net_util.h"
12 #include "storage/browser/quota/usage_tracker.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using storage::kQuotaStatusOk
;
16 using storage::kStorageTypeTemporary
;
17 using storage::QuotaClient
;
18 using storage::QuotaClientList
;
19 using storage::SpecialStoragePolicy
;
20 using storage::StorageType
;
21 using storage::UsageTracker
;
27 void DidGetGlobalUsage(bool* done
,
29 int64
* unlimited_usage_out
,
31 int64 unlimited_usage
) {
35 *unlimited_usage_out
= unlimited_usage
;
38 void DidGetUsage(bool* done
,
48 class MockQuotaClient
: public QuotaClient
{
51 ~MockQuotaClient() override
{}
53 ID
id() const override
{ return kFileSystem
; }
55 void OnQuotaManagerDestroyed() override
{}
57 void GetOriginUsage(const GURL
& origin
,
59 const GetUsageCallback
& callback
) override
{
60 EXPECT_EQ(kStorageTypeTemporary
, type
);
61 int64 usage
= GetUsage(origin
);
62 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE
,
63 base::Bind(callback
, usage
));
66 void GetOriginsForType(StorageType type
,
67 const GetOriginsCallback
& callback
) override
{
68 EXPECT_EQ(kStorageTypeTemporary
, type
);
69 std::set
<GURL
> origins
;
70 for (UsageMap::const_iterator itr
= usage_map_
.begin();
71 itr
!= usage_map_
.end(); ++itr
) {
72 origins
.insert(itr
->first
);
74 base::ThreadTaskRunnerHandle::Get()->PostTask(
75 FROM_HERE
, base::Bind(callback
, origins
));
78 void GetOriginsForHost(StorageType type
,
79 const std::string
& host
,
80 const GetOriginsCallback
& callback
) override
{
81 EXPECT_EQ(kStorageTypeTemporary
, type
);
82 std::set
<GURL
> origins
;
83 for (UsageMap::const_iterator itr
= usage_map_
.begin();
84 itr
!= usage_map_
.end(); ++itr
) {
85 if (net::GetHostOrSpecFromURL(itr
->first
) == host
)
86 origins
.insert(itr
->first
);
88 base::ThreadTaskRunnerHandle::Get()->PostTask(
89 FROM_HERE
, base::Bind(callback
, origins
));
92 void DeleteOriginData(const GURL
& origin
,
94 const DeletionCallback
& callback
) override
{
95 EXPECT_EQ(kStorageTypeTemporary
, type
);
96 usage_map_
.erase(origin
);
97 base::ThreadTaskRunnerHandle::Get()->PostTask(
98 FROM_HERE
, base::Bind(callback
, kQuotaStatusOk
));
101 bool DoesSupport(storage::StorageType type
) const override
{
102 return type
== storage::kStorageTypeTemporary
;
105 int64
GetUsage(const GURL
& origin
) {
106 UsageMap::const_iterator found
= usage_map_
.find(origin
);
107 if (found
== usage_map_
.end())
109 return found
->second
;
112 void SetUsage(const GURL
& origin
, int64 usage
) {
113 usage_map_
[origin
] = usage
;
116 int64
UpdateUsage(const GURL
& origin
, int64 delta
) {
117 return usage_map_
[origin
] += delta
;
121 typedef std::map
<GURL
, int64
> UsageMap
;
125 DISALLOW_COPY_AND_ASSIGN(MockQuotaClient
);
128 class UsageTrackerTest
: public testing::Test
{
131 : storage_policy_(new MockSpecialStoragePolicy()),
132 usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary
,
133 storage_policy_
.get(), NULL
) {
136 ~UsageTrackerTest() override
{}
138 UsageTracker
* usage_tracker() {
139 return &usage_tracker_
;
142 void UpdateUsage(const GURL
& origin
, int64 delta
) {
143 quota_client_
.UpdateUsage(origin
, delta
);
144 usage_tracker_
.UpdateUsageCache(quota_client_
.id(), origin
, delta
);
145 base::RunLoop().RunUntilIdle();
148 void UpdateUsageWithoutNotification(const GURL
& origin
, int64 delta
) {
149 quota_client_
.UpdateUsage(origin
, delta
);
152 void GetGlobalLimitedUsage(int64
* limited_usage
) {
154 usage_tracker_
.GetGlobalLimitedUsage(base::Bind(
155 &DidGetUsage
, &done
, limited_usage
));
156 base::RunLoop().RunUntilIdle();
161 void GetGlobalUsage(int64
* usage
, int64
* unlimited_usage
) {
163 usage_tracker_
.GetGlobalUsage(base::Bind(
165 &done
, usage
, unlimited_usage
));
166 base::RunLoop().RunUntilIdle();
171 void GetHostUsage(const std::string
& host
, int64
* usage
) {
173 usage_tracker_
.GetHostUsage(host
, base::Bind(&DidGetUsage
, &done
, usage
));
174 base::RunLoop().RunUntilIdle();
179 void GrantUnlimitedStoragePolicy(const GURL
& origin
) {
180 if (!storage_policy_
->IsStorageUnlimited(origin
)) {
181 storage_policy_
->AddUnlimited(origin
);
182 storage_policy_
->NotifyGranted(
183 origin
, SpecialStoragePolicy::STORAGE_UNLIMITED
);
187 void RevokeUnlimitedStoragePolicy(const GURL
& origin
) {
188 if (storage_policy_
->IsStorageUnlimited(origin
)) {
189 storage_policy_
->RemoveUnlimited(origin
);
190 storage_policy_
->NotifyRevoked(
191 origin
, SpecialStoragePolicy::STORAGE_UNLIMITED
);
195 void SetUsageCacheEnabled(const GURL
& origin
, bool enabled
) {
196 usage_tracker_
.SetUsageCacheEnabled(
197 quota_client_
.id(), origin
, enabled
);
201 QuotaClientList
GetUsageTrackerList() {
202 QuotaClientList client_list
;
203 client_list
.push_back("a_client_
);
207 base::MessageLoop message_loop_
;
209 scoped_refptr
<MockSpecialStoragePolicy
> storage_policy_
;
210 MockQuotaClient quota_client_
;
211 UsageTracker usage_tracker_
;
213 DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest
);
216 TEST_F(UsageTrackerTest
, GrantAndRevokeUnlimitedStorage
) {
218 int64 unlimited_usage
= 0;
219 int64 host_usage
= 0;
220 GetGlobalUsage(&usage
, &unlimited_usage
);
222 EXPECT_EQ(0, unlimited_usage
);
224 const GURL
origin("http://example.com");
225 const std::string
host(net::GetHostOrSpecFromURL(origin
));
227 UpdateUsage(origin
, 100);
228 GetGlobalUsage(&usage
, &unlimited_usage
);
229 GetHostUsage(host
, &host_usage
);
230 EXPECT_EQ(100, usage
);
231 EXPECT_EQ(0, unlimited_usage
);
232 EXPECT_EQ(100, host_usage
);
234 GrantUnlimitedStoragePolicy(origin
);
235 GetGlobalUsage(&usage
, &unlimited_usage
);
236 GetHostUsage(host
, &host_usage
);
237 EXPECT_EQ(100, usage
);
238 EXPECT_EQ(100, unlimited_usage
);
239 EXPECT_EQ(100, host_usage
);
241 RevokeUnlimitedStoragePolicy(origin
);
242 GetGlobalUsage(&usage
, &unlimited_usage
);
243 GetHostUsage(host
, &host_usage
);
244 EXPECT_EQ(100, usage
);
245 EXPECT_EQ(0, unlimited_usage
);
246 EXPECT_EQ(100, host_usage
);
249 TEST_F(UsageTrackerTest
, CacheDisabledClientTest
) {
251 int64 unlimited_usage
= 0;
252 int64 host_usage
= 0;
254 const GURL
origin("http://example.com");
255 const std::string
host(net::GetHostOrSpecFromURL(origin
));
257 UpdateUsage(origin
, 100);
258 GetGlobalUsage(&usage
, &unlimited_usage
);
259 GetHostUsage(host
, &host_usage
);
260 EXPECT_EQ(100, usage
);
261 EXPECT_EQ(0, unlimited_usage
);
262 EXPECT_EQ(100, host_usage
);
264 UpdateUsageWithoutNotification(origin
, 100);
265 GetGlobalUsage(&usage
, &unlimited_usage
);
266 GetHostUsage(host
, &host_usage
);
267 EXPECT_EQ(100, usage
);
268 EXPECT_EQ(0, unlimited_usage
);
269 EXPECT_EQ(100, host_usage
);
271 GrantUnlimitedStoragePolicy(origin
);
272 UpdateUsageWithoutNotification(origin
, 100);
273 SetUsageCacheEnabled(origin
, false);
274 UpdateUsageWithoutNotification(origin
, 100);
276 GetGlobalUsage(&usage
, &unlimited_usage
);
277 GetHostUsage(host
, &host_usage
);
278 EXPECT_EQ(400, usage
);
279 EXPECT_EQ(400, unlimited_usage
);
280 EXPECT_EQ(400, host_usage
);
282 RevokeUnlimitedStoragePolicy(origin
);
283 GetGlobalUsage(&usage
, &unlimited_usage
);
284 GetHostUsage(host
, &host_usage
);
285 EXPECT_EQ(400, usage
);
286 EXPECT_EQ(0, unlimited_usage
);
287 EXPECT_EQ(400, host_usage
);
289 SetUsageCacheEnabled(origin
, true);
290 UpdateUsage(origin
, 100);
292 GetGlobalUsage(&usage
, &unlimited_usage
);
293 GetHostUsage(host
, &host_usage
);
294 EXPECT_EQ(500, usage
);
295 EXPECT_EQ(0, unlimited_usage
);
296 EXPECT_EQ(500, host_usage
);
299 TEST_F(UsageTrackerTest
, LimitedGlobalUsageTest
) {
300 const GURL
kNormal("http://normal");
301 const GURL
kUnlimited("http://unlimited");
302 const GURL
kNonCached("http://non_cached");
303 const GURL
kNonCachedUnlimited("http://non_cached-unlimited");
305 GrantUnlimitedStoragePolicy(kUnlimited
);
306 GrantUnlimitedStoragePolicy(kNonCachedUnlimited
);
308 SetUsageCacheEnabled(kNonCached
, false);
309 SetUsageCacheEnabled(kNonCachedUnlimited
, false);
311 UpdateUsageWithoutNotification(kNormal
, 1);
312 UpdateUsageWithoutNotification(kUnlimited
, 2);
313 UpdateUsageWithoutNotification(kNonCached
, 4);
314 UpdateUsageWithoutNotification(kNonCachedUnlimited
, 8);
316 int64 limited_usage
= 0;
317 int64 total_usage
= 0;
318 int64 unlimited_usage
= 0;
320 GetGlobalLimitedUsage(&limited_usage
);
321 GetGlobalUsage(&total_usage
, &unlimited_usage
);
322 EXPECT_EQ(1 + 4, limited_usage
);
323 EXPECT_EQ(1 + 2 + 4 + 8, total_usage
);
324 EXPECT_EQ(2 + 8, unlimited_usage
);
326 UpdateUsageWithoutNotification(kNonCached
, 16 - 4);
327 UpdateUsageWithoutNotification(kNonCachedUnlimited
, 32 - 8);
329 GetGlobalLimitedUsage(&limited_usage
);
330 GetGlobalUsage(&total_usage
, &unlimited_usage
);
331 EXPECT_EQ(1 + 16, limited_usage
);
332 EXPECT_EQ(1 + 2 + 16 + 32, total_usage
);
333 EXPECT_EQ(2 + 32, unlimited_usage
);
337 } // namespace content