1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/bind.h"
#include "base/run_loop.h"
#include "content/public/test/mock_special_storage_policy.h"
#include "net/base/net_util.h"
#include "storage/browser/quota/usage_tracker.h"
#include "testing/gtest/include/gtest/gtest.h"
using storage::kQuotaStatusOk;
using storage::kStorageTypeTemporary;
using storage::QuotaClient;
using storage::QuotaClientList;
using storage::SpecialStoragePolicy;
using storage::StorageType;
using storage::UsageTracker;
namespace content {
namespace {
void DidGetGlobalUsage(bool* done,
int64* usage_out,
int64* unlimited_usage_out,
int64 usage,
int64 unlimited_usage) {
EXPECT_FALSE(*done);
*done = true;
*usage_out = usage;
*unlimited_usage_out = unlimited_usage;
}
void DidGetUsage(bool* done,
int64* usage_out,
int64 usage) {
EXPECT_FALSE(*done);
*done = true;
*usage_out = usage;
}
} // namespace
class MockQuotaClient : public QuotaClient {
public:
MockQuotaClient() {}
~MockQuotaClient() override {}
ID id() const override { return kFileSystem; }
void OnQuotaManagerDestroyed() override {}
void GetOriginUsage(const GURL& origin,
StorageType type,
const GetUsageCallback& callback) override {
EXPECT_EQ(kStorageTypeTemporary, type);
int64 usage = GetUsage(origin);
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, usage));
}
void GetOriginsForType(StorageType type,
const GetOriginsCallback& callback) override {
EXPECT_EQ(kStorageTypeTemporary, type);
std::set<GURL> origins;
for (UsageMap::const_iterator itr = usage_map_.begin();
itr != usage_map_.end(); ++itr) {
origins.insert(itr->first);
}
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, origins));
}
void GetOriginsForHost(StorageType type,
const std::string& host,
const GetOriginsCallback& callback) override {
EXPECT_EQ(kStorageTypeTemporary, type);
std::set<GURL> origins;
for (UsageMap::const_iterator itr = usage_map_.begin();
itr != usage_map_.end(); ++itr) {
if (net::GetHostOrSpecFromURL(itr->first) == host)
origins.insert(itr->first);
}
base::MessageLoop::current()->PostTask(FROM_HERE,
base::Bind(callback, origins));
}
void DeleteOriginData(const GURL& origin,
StorageType type,
const DeletionCallback& callback) override {
EXPECT_EQ(kStorageTypeTemporary, type);
usage_map_.erase(origin);
base::MessageLoop::current()->PostTask(
FROM_HERE, base::Bind(callback, kQuotaStatusOk));
}
bool DoesSupport(storage::StorageType type) const override {
return type == storage::kStorageTypeTemporary;
}
int64 GetUsage(const GURL& origin) {
UsageMap::const_iterator found = usage_map_.find(origin);
if (found == usage_map_.end())
return 0;
return found->second;
}
void SetUsage(const GURL& origin, int64 usage) {
usage_map_[origin] = usage;
}
int64 UpdateUsage(const GURL& origin, int64 delta) {
return usage_map_[origin] += delta;
}
private:
typedef std::map<GURL, int64> UsageMap;
UsageMap usage_map_;
DISALLOW_COPY_AND_ASSIGN(MockQuotaClient);
};
class UsageTrackerTest : public testing::Test {
public:
UsageTrackerTest()
: storage_policy_(new MockSpecialStoragePolicy()),
usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary,
storage_policy_.get(), NULL) {
}
~UsageTrackerTest() override {}
UsageTracker* usage_tracker() {
return &usage_tracker_;
}
void UpdateUsage(const GURL& origin, int64 delta) {
quota_client_.UpdateUsage(origin, delta);
usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta);
base::RunLoop().RunUntilIdle();
}
void UpdateUsageWithoutNotification(const GURL& origin, int64 delta) {
quota_client_.UpdateUsage(origin, delta);
}
void GetGlobalLimitedUsage(int64* limited_usage) {
bool done = false;
usage_tracker_.GetGlobalLimitedUsage(base::Bind(
&DidGetUsage, &done, limited_usage));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(done);
}
void GetGlobalUsage(int64* usage, int64* unlimited_usage) {
bool done = false;
usage_tracker_.GetGlobalUsage(base::Bind(
&DidGetGlobalUsage,
&done, usage, unlimited_usage));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(done);
}
void GetHostUsage(const std::string& host, int64* usage) {
bool done = false;
usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage));
base::RunLoop().RunUntilIdle();
EXPECT_TRUE(done);
}
void GrantUnlimitedStoragePolicy(const GURL& origin) {
if (!storage_policy_->IsStorageUnlimited(origin)) {
storage_policy_->AddUnlimited(origin);
storage_policy_->NotifyGranted(
origin, SpecialStoragePolicy::STORAGE_UNLIMITED);
}
}
void RevokeUnlimitedStoragePolicy(const GURL& origin) {
if (storage_policy_->IsStorageUnlimited(origin)) {
storage_policy_->RemoveUnlimited(origin);
storage_policy_->NotifyRevoked(
origin, SpecialStoragePolicy::STORAGE_UNLIMITED);
}
}
void SetUsageCacheEnabled(const GURL& origin, bool enabled) {
usage_tracker_.SetUsageCacheEnabled(
quota_client_.id(), origin, enabled);
}
private:
QuotaClientList GetUsageTrackerList() {
QuotaClientList client_list;
client_list.push_back("a_client_);
return client_list;
}
base::MessageLoop message_loop_;
scoped_refptr<MockSpecialStoragePolicy> storage_policy_;
MockQuotaClient quota_client_;
UsageTracker usage_tracker_;
DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest);
};
TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) {
int64 usage = 0;
int64 unlimited_usage = 0;
int64 host_usage = 0;
GetGlobalUsage(&usage, &unlimited_usage);
EXPECT_EQ(0, usage);
EXPECT_EQ(0, unlimited_usage);
const GURL origin("http://example.com");
const std::string host(net::GetHostOrSpecFromURL(origin));
UpdateUsage(origin, 100);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(100, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(100, host_usage);
GrantUnlimitedStoragePolicy(origin);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(100, usage);
EXPECT_EQ(100, unlimited_usage);
EXPECT_EQ(100, host_usage);
RevokeUnlimitedStoragePolicy(origin);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(100, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(100, host_usage);
}
TEST_F(UsageTrackerTest, CacheDisabledClientTest) {
int64 usage = 0;
int64 unlimited_usage = 0;
int64 host_usage = 0;
const GURL origin("http://example.com");
const std::string host(net::GetHostOrSpecFromURL(origin));
UpdateUsage(origin, 100);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(100, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(100, host_usage);
UpdateUsageWithoutNotification(origin, 100);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(100, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(100, host_usage);
GrantUnlimitedStoragePolicy(origin);
UpdateUsageWithoutNotification(origin, 100);
SetUsageCacheEnabled(origin, false);
UpdateUsageWithoutNotification(origin, 100);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(400, usage);
EXPECT_EQ(400, unlimited_usage);
EXPECT_EQ(400, host_usage);
RevokeUnlimitedStoragePolicy(origin);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(400, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(400, host_usage);
SetUsageCacheEnabled(origin, true);
UpdateUsage(origin, 100);
GetGlobalUsage(&usage, &unlimited_usage);
GetHostUsage(host, &host_usage);
EXPECT_EQ(500, usage);
EXPECT_EQ(0, unlimited_usage);
EXPECT_EQ(500, host_usage);
}
TEST_F(UsageTrackerTest, LimitedGlobalUsageTest) {
const GURL kNormal("http://normal");
const GURL kUnlimited("http://unlimited");
const GURL kNonCached("http://non_cached");
const GURL kNonCachedUnlimited("http://non_cached-unlimited");
GrantUnlimitedStoragePolicy(kUnlimited);
GrantUnlimitedStoragePolicy(kNonCachedUnlimited);
SetUsageCacheEnabled(kNonCached, false);
SetUsageCacheEnabled(kNonCachedUnlimited, false);
UpdateUsageWithoutNotification(kNormal, 1);
UpdateUsageWithoutNotification(kUnlimited, 2);
UpdateUsageWithoutNotification(kNonCached, 4);
UpdateUsageWithoutNotification(kNonCachedUnlimited, 8);
int64 limited_usage = 0;
int64 total_usage = 0;
int64 unlimited_usage = 0;
GetGlobalLimitedUsage(&limited_usage);
GetGlobalUsage(&total_usage, &unlimited_usage);
EXPECT_EQ(1 + 4, limited_usage);
EXPECT_EQ(1 + 2 + 4 + 8, total_usage);
EXPECT_EQ(2 + 8, unlimited_usage);
UpdateUsageWithoutNotification(kNonCached, 16 - 4);
UpdateUsageWithoutNotification(kNonCachedUnlimited, 32 - 8);
GetGlobalLimitedUsage(&limited_usage);
GetGlobalUsage(&total_usage, &unlimited_usage);
EXPECT_EQ(1 + 16, limited_usage);
EXPECT_EQ(1 + 2 + 16 + 32, total_usage);
EXPECT_EQ(2 + 32, unlimited_usage);
}
} // namespace content
|