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