[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 5 | #include "base/file_util.h" |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 6 | #include "base/message_loop/message_loop_proxy.h" |
| 7 | #include "base/run_loop.h" |
| 8 | #include "base/threading/thread.h" |
| 9 | #include "content/browser/browser_thread_impl.h" |
| 10 | #include "content/browser/gpu/shader_disk_cache.h" |
| 11 | #include "content/browser/storage_partition_impl.h" |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 12 | #include "content/public/browser/local_storage_usage_info.h" |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 13 | #include "content/public/browser/storage_partition.h" |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 14 | #include "content/public/test/test_browser_context.h" |
| 15 | #include "content/public/test/test_browser_thread.h" |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 16 | #include "content/public/test/test_browser_thread_bundle.h" |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 17 | #include "net/base/test_completion_callback.h" |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 18 | #include "net/cookies/cookie_monster.h" |
| 19 | #include "net/url_request/url_request_context.h" |
| 20 | #include "net/url_request/url_request_context_getter.h" |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 21 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 22 | #include "webkit/browser/quota/mock_quota_manager.h" |
| 23 | #include "webkit/browser/quota/mock_special_storage_policy.h" |
| 24 | #include "webkit/browser/quota/quota_manager.h" |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 25 | |
| 26 | namespace content { |
| 27 | namespace { |
| 28 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 29 | const int kDefaultClientId = 42; |
| 30 | const char kCacheKey[] = "key"; |
| 31 | const char kCacheValue[] = "cached value"; |
| 32 | |
| 33 | const char kTestOrigin1[] = "http://host1:1/"; |
| 34 | const char kTestOrigin2[] = "http://host2:1/"; |
| 35 | const char kTestOrigin3[] = "http://host3:1/"; |
| 36 | const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/"; |
| 37 | |
| 38 | const GURL kOrigin1(kTestOrigin1); |
| 39 | const GURL kOrigin2(kTestOrigin2); |
| 40 | const GURL kOrigin3(kTestOrigin3); |
| 41 | const GURL kOriginDevTools(kTestOriginDevTools); |
| 42 | |
| 43 | const base::FilePath::CharType kDomStorageOrigin1[] = |
| 44 | FILE_PATH_LITERAL("http_host1_1.localstorage"); |
| 45 | |
| 46 | const base::FilePath::CharType kDomStorageOrigin2[] = |
| 47 | FILE_PATH_LITERAL("http_host2_1.localstorage"); |
| 48 | |
| 49 | const base::FilePath::CharType kDomStorageOrigin3[] = |
| 50 | FILE_PATH_LITERAL("http_host3_1.localstorage"); |
| 51 | |
| 52 | const quota::StorageType kTemporary = quota::kStorageTypeTemporary; |
| 53 | const quota::StorageType kPersistent = quota::kStorageTypePersistent; |
| 54 | |
| 55 | const quota::QuotaClient::ID kClientFile = quota::QuotaClient::kFileSystem; |
| 56 | |
| 57 | const uint32 kAllQuotaRemoveMask = |
| 58 | StoragePartition::REMOVE_DATA_MASK_INDEXEDDB | |
| 59 | StoragePartition::REMOVE_DATA_MASK_WEBSQL | |
| 60 | StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS | |
| 61 | StoragePartition::REMOVE_DATA_MASK_APPCACHE; |
| 62 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 63 | class AwaitCompletionHelper { |
| 64 | public: |
| 65 | AwaitCompletionHelper() : start_(false), already_quit_(false) {} |
| 66 | virtual ~AwaitCompletionHelper() {} |
| 67 | |
| 68 | void BlockUntilNotified() { |
| 69 | if (!already_quit_) { |
| 70 | DCHECK(!start_); |
| 71 | start_ = true; |
| 72 | base::MessageLoop::current()->Run(); |
| 73 | } else { |
| 74 | DCHECK(!start_); |
| 75 | already_quit_ = false; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void Notify() { |
| 80 | if (start_) { |
| 81 | DCHECK(!already_quit_); |
| 82 | base::MessageLoop::current()->Quit(); |
| 83 | start_ = false; |
| 84 | } else { |
| 85 | DCHECK(!already_quit_); |
| 86 | already_quit_ = true; |
| 87 | } |
| 88 | } |
| 89 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 90 | private: |
| 91 | // Helps prevent from running message_loop, if the callback invoked |
| 92 | // immediately. |
| 93 | bool start_; |
| 94 | bool already_quit_; |
| 95 | |
| 96 | DISALLOW_COPY_AND_ASSIGN(AwaitCompletionHelper); |
| 97 | }; |
| 98 | |
| 99 | class RemoveCookieTester { |
| 100 | public: |
| 101 | explicit RemoveCookieTester(TestBrowserContext* context) |
| 102 | : get_cookie_success_(false), monster_(NULL) { |
| 103 | SetMonster(context->GetRequestContext()->GetURLRequestContext()-> |
| 104 | cookie_store()->GetCookieMonster()); |
| 105 | } |
| 106 | |
| 107 | // Returns true, if the given cookie exists in the cookie store. |
| 108 | bool ContainsCookie() { |
| 109 | get_cookie_success_ = false; |
| 110 | monster_->GetCookiesWithOptionsAsync( |
| 111 | kOrigin1, net::CookieOptions(), |
| 112 | base::Bind(&RemoveCookieTester::GetCookieCallback, |
| 113 | base::Unretained(this))); |
| 114 | await_completion_.BlockUntilNotified(); |
| 115 | return get_cookie_success_; |
| 116 | } |
| 117 | |
| 118 | void AddCookie() { |
| 119 | monster_->SetCookieWithOptionsAsync( |
| 120 | kOrigin1, "A=1", net::CookieOptions(), |
| 121 | base::Bind(&RemoveCookieTester::SetCookieCallback, |
| 122 | base::Unretained(this))); |
| 123 | await_completion_.BlockUntilNotified(); |
| 124 | } |
| 125 | |
| 126 | protected: |
| 127 | void SetMonster(net::CookieStore* monster) { |
| 128 | monster_ = monster; |
| 129 | } |
| 130 | |
| 131 | private: |
| 132 | void GetCookieCallback(const std::string& cookies) { |
| 133 | if (cookies == "A=1") { |
| 134 | get_cookie_success_ = true; |
| 135 | } else { |
| 136 | EXPECT_EQ("", cookies); |
| 137 | get_cookie_success_ = false; |
| 138 | } |
| 139 | await_completion_.Notify(); |
| 140 | } |
| 141 | |
| 142 | void SetCookieCallback(bool result) { |
| 143 | ASSERT_TRUE(result); |
| 144 | await_completion_.Notify(); |
| 145 | } |
| 146 | |
| 147 | bool get_cookie_success_; |
| 148 | AwaitCompletionHelper await_completion_; |
| 149 | net::CookieStore* monster_; |
| 150 | |
| 151 | DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); |
| 152 | }; |
| 153 | |
| 154 | class RemoveLocalStorageTester { |
| 155 | public: |
| 156 | explicit RemoveLocalStorageTester(TestBrowserContext* profile) |
| 157 | : profile_(profile), dom_storage_context_(NULL) { |
| 158 | dom_storage_context_ = |
| 159 | content::BrowserContext::GetDefaultStoragePartition(profile)-> |
| 160 | GetDOMStorageContext(); |
| 161 | } |
| 162 | |
| 163 | // Returns true, if the given origin URL exists. |
| 164 | bool DOMStorageExistsForOrigin(const GURL& origin) { |
| 165 | GetLocalStorageUsage(); |
| 166 | await_completion_.BlockUntilNotified(); |
| 167 | for (size_t i = 0; i < infos_.size(); ++i) { |
| 168 | if (origin == infos_[i].origin) |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
| 174 | void AddDOMStorageTestData() { |
| 175 | // Note: This test depends on details of how the dom_storage library |
| 176 | // stores data in the host file system. |
| 177 | base::FilePath storage_path = |
| 178 | profile_->GetPath().AppendASCII("Local Storage"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 179 | base::CreateDirectory(storage_path); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 180 | |
| 181 | // Write some files. |
| 182 | file_util::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0); |
| 183 | file_util::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0); |
| 184 | file_util::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0); |
| 185 | |
| 186 | // Tweak their dates. |
[email protected] | c0d50816 | 2013-12-04 22:49:00 | [diff] [blame] | 187 | base::Time now = base::Time::Now(); |
| 188 | base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now); |
| 189 | |
| 190 | base::Time one_day_ago = now - base::TimeDelta::FromDays(1); |
| 191 | base::TouchFile(storage_path.Append(kDomStorageOrigin2), |
| 192 | one_day_ago, one_day_ago); |
| 193 | |
| 194 | base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60); |
| 195 | base::TouchFile(storage_path.Append(kDomStorageOrigin3), |
| 196 | sixty_days_ago, sixty_days_ago); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | private: |
| 200 | void GetLocalStorageUsage() { |
| 201 | dom_storage_context_->GetLocalStorageUsage( |
| 202 | base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage, |
| 203 | base::Unretained(this))); |
| 204 | } |
| 205 | void OnGotLocalStorageUsage( |
| 206 | const std::vector<content::LocalStorageUsageInfo>& infos) { |
| 207 | infos_ = infos; |
| 208 | await_completion_.Notify(); |
| 209 | } |
| 210 | |
| 211 | // We don't own these pointers. |
| 212 | TestBrowserContext* profile_; |
| 213 | content::DOMStorageContext* dom_storage_context_; |
| 214 | |
| 215 | std::vector<content::LocalStorageUsageInfo> infos_; |
| 216 | |
| 217 | AwaitCompletionHelper await_completion_; |
| 218 | |
| 219 | DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester); |
| 220 | }; |
| 221 | |
| 222 | bool IsWebSafeSchemeForTest(const std::string& scheme) { |
| 223 | return scheme == "http"; |
| 224 | } |
| 225 | |
| 226 | bool DoesOriginMatchForUnprotectedWeb( |
| 227 | const GURL& origin, |
| 228 | quota::SpecialStoragePolicy* special_storage_policy) { |
[email protected] | 8c63cd2 | 2013-11-08 11:31:21 | [diff] [blame] | 229 | if (IsWebSafeSchemeForTest(origin.scheme())) |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 230 | return !special_storage_policy->IsStorageProtected(origin.GetOrigin()); |
| 231 | |
[email protected] | 8c63cd2 | 2013-11-08 11:31:21 | [diff] [blame] | 232 | return false; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | bool DoesOriginMatchForBothProtectedAndUnprotectedWeb( |
| 236 | const GURL& origin, |
| 237 | quota::SpecialStoragePolicy* special_storage_policy) { |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | bool DoesOriginMatchUnprotected( |
| 242 | const GURL& origin, |
| 243 | quota::SpecialStoragePolicy* special_storage_policy) { |
| 244 | return origin.GetOrigin().scheme() != kOriginDevTools.scheme(); |
| 245 | } |
| 246 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 247 | void ClearQuotaData(content::StoragePartition* partition, |
| 248 | base::RunLoop* loop_to_quit) { |
| 249 | partition->ClearData( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 250 | kAllQuotaRemoveMask, |
| 251 | StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 252 | GURL(), StoragePartition::OriginMatcherFunction(), |
| 253 | base::Time(), base::Time::Max(), loop_to_quit->QuitClosure()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void ClearQuotaDataWithOriginMatcher( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 257 | content::StoragePartition* partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 258 | const GURL& remove_origin, |
| 259 | const StoragePartition::OriginMatcherFunction& origin_matcher, |
| 260 | const base::Time delete_begin, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 261 | base::RunLoop* loop_to_quit) { |
| 262 | partition->ClearData(kAllQuotaRemoveMask, |
| 263 | StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, |
| 264 | remove_origin, origin_matcher, delete_begin, |
| 265 | base::Time::Max(), loop_to_quit->QuitClosure()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void ClearQuotaDataForOrigin( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 269 | content::StoragePartition* partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 270 | const GURL& remove_origin, |
| 271 | const base::Time delete_begin, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 272 | base::RunLoop* loop_to_quit) { |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 273 | ClearQuotaDataWithOriginMatcher( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 274 | partition, remove_origin, |
| 275 | StoragePartition::OriginMatcherFunction(), delete_begin, |
| 276 | loop_to_quit); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void ClearQuotaDataForNonPersistent( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 280 | content::StoragePartition* partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 281 | const base::Time delete_begin, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 282 | base::RunLoop* loop_to_quit) { |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 283 | uint32 quota_storage_remove_mask_no_persistent = |
| 284 | StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL & |
| 285 | ~StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT; |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 286 | partition->ClearData( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 287 | kAllQuotaRemoveMask, quota_storage_remove_mask_no_persistent, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 288 | GURL(), StoragePartition::OriginMatcherFunction(), |
| 289 | delete_begin, base::Time::Max(), loop_to_quit->QuitClosure()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 290 | } |
| 291 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 292 | void ClearCookies(content::StoragePartition* partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 293 | const base::Time delete_begin, |
| 294 | const base::Time delete_end, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 295 | base::RunLoop* run_loop) { |
| 296 | partition->ClearData( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 297 | StoragePartition::REMOVE_DATA_MASK_COOKIES, |
| 298 | StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 299 | GURL(), StoragePartition::OriginMatcherFunction(), |
| 300 | delete_begin, delete_end, run_loop->QuitClosure()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void ClearStuff(uint32 remove_mask, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 304 | content::StoragePartition* partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 305 | const base::Time delete_begin, |
| 306 | const base::Time delete_end, |
| 307 | const StoragePartition::OriginMatcherFunction& origin_matcher, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 308 | base::RunLoop* run_loop) { |
| 309 | partition->ClearData( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 310 | remove_mask, StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 311 | GURL(), origin_matcher, delete_begin, delete_end, |
| 312 | run_loop->QuitClosure()); |
| 313 | } |
| 314 | |
| 315 | void ClearData(content::StoragePartition* partition, |
| 316 | base::RunLoop* run_loop) { |
| 317 | base::Time time; |
| 318 | partition->ClearData( |
| 319 | StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE, |
| 320 | StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, |
| 321 | GURL(), StoragePartition::OriginMatcherFunction(), |
| 322 | time, time, run_loop->QuitClosure()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 323 | } |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 324 | |
| 325 | } // namespace |
| 326 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 327 | class StoragePartitionImplTest : public testing::Test { |
| 328 | public: |
| 329 | StoragePartitionImplTest() |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 330 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 331 | browser_context_(new TestBrowserContext()) { |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 332 | } |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 333 | |
| 334 | quota::MockQuotaManager* GetMockManager() { |
| 335 | if (!quota_manager_.get()) { |
| 336 | quota_manager_ = new quota::MockQuotaManager( |
| 337 | browser_context_->IsOffTheRecord(), |
| 338 | browser_context_->GetPath(), |
| 339 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(), |
| 340 | BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 341 | browser_context_->GetSpecialStoragePolicy()); |
| 342 | } |
| 343 | return quota_manager_.get(); |
| 344 | } |
| 345 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 346 | TestBrowserContext* browser_context() { |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 347 | return browser_context_.get(); |
| 348 | } |
| 349 | |
| 350 | private: |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 351 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 352 | scoped_ptr<TestBrowserContext> browser_context_; |
| 353 | scoped_refptr<quota::MockQuotaManager> quota_manager_; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 354 | |
| 355 | DISALLOW_COPY_AND_ASSIGN(StoragePartitionImplTest); |
| 356 | }; |
| 357 | |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 358 | class StoragePartitionShaderClearTest : public testing::Test { |
| 359 | public: |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 360 | StoragePartitionShaderClearTest() |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 361 | : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 362 | browser_context_(new TestBrowserContext()) { |
| 363 | ShaderCacheFactory::GetInstance()->SetCacheInfo( |
| 364 | kDefaultClientId, |
| 365 | BrowserContext::GetDefaultStoragePartition( |
| 366 | browser_context())->GetPath()); |
| 367 | cache_ = ShaderCacheFactory::GetInstance()->Get(kDefaultClientId); |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 370 | virtual ~StoragePartitionShaderClearTest() { |
| 371 | cache_ = NULL; |
| 372 | ShaderCacheFactory::GetInstance()->RemoveCacheInfo(kDefaultClientId); |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void InitCache() { |
| 376 | net::TestCompletionCallback available_cb; |
| 377 | int rv = cache_->SetAvailableCallback(available_cb.callback()); |
| 378 | ASSERT_EQ(net::OK, available_cb.GetResult(rv)); |
| 379 | EXPECT_EQ(0, cache_->Size()); |
| 380 | |
| 381 | cache_->Cache(kCacheKey, kCacheValue); |
| 382 | |
| 383 | net::TestCompletionCallback complete_cb; |
| 384 | |
| 385 | rv = cache_->SetCacheCompleteCallback(complete_cb.callback()); |
| 386 | ASSERT_EQ(net::OK, complete_cb.GetResult(rv)); |
| 387 | } |
| 388 | |
| 389 | size_t Size() { return cache_->Size(); } |
| 390 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 391 | TestBrowserContext* browser_context() { |
| 392 | return browser_context_.get(); |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 393 | } |
| 394 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 395 | private: |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 396 | content::TestBrowserThreadBundle thread_bundle_; |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 397 | scoped_ptr<TestBrowserContext> browser_context_; |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 398 | |
| 399 | scoped_refptr<ShaderDiskCache> cache_; |
| 400 | }; |
| 401 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 402 | // Tests --------------------------------------------------------------------- |
| 403 | |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 404 | TEST_F(StoragePartitionShaderClearTest, ClearShaderCache) { |
| 405 | InitCache(); |
| 406 | EXPECT_EQ(1u, Size()); |
| 407 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 408 | base::RunLoop run_loop; |
[email protected] | ec04d3f | 2013-06-06 21:31:39 | [diff] [blame] | 409 | base::MessageLoop::current()->PostTask( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 410 | FROM_HERE, base::Bind( |
| 411 | &ClearData, |
| 412 | BrowserContext::GetDefaultStoragePartition(browser_context()), |
| 413 | &run_loop)); |
| 414 | run_loop.Run(); |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 415 | EXPECT_EQ(0u, Size()); |
| 416 | } |
| 417 | |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 418 | TEST_F(StoragePartitionImplTest, QuotaClientMaskGeneration) { |
| 419 | EXPECT_EQ(quota::QuotaClient::kFileSystem, |
| 420 | StoragePartitionImpl::GenerateQuotaClientMask( |
| 421 | StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS)); |
| 422 | EXPECT_EQ(quota::QuotaClient::kDatabase, |
| 423 | StoragePartitionImpl::GenerateQuotaClientMask( |
| 424 | StoragePartition::REMOVE_DATA_MASK_WEBSQL)); |
| 425 | EXPECT_EQ(quota::QuotaClient::kAppcache, |
| 426 | StoragePartitionImpl::GenerateQuotaClientMask( |
| 427 | StoragePartition::REMOVE_DATA_MASK_APPCACHE)); |
| 428 | EXPECT_EQ(quota::QuotaClient::kIndexedDatabase, |
| 429 | StoragePartitionImpl::GenerateQuotaClientMask( |
| 430 | StoragePartition::REMOVE_DATA_MASK_INDEXEDDB)); |
| 431 | EXPECT_EQ(quota::QuotaClient::kFileSystem | |
| 432 | quota::QuotaClient::kDatabase | |
| 433 | quota::QuotaClient::kAppcache | |
| 434 | quota::QuotaClient::kIndexedDatabase, |
| 435 | StoragePartitionImpl::GenerateQuotaClientMask( |
| 436 | StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS | |
| 437 | StoragePartition::REMOVE_DATA_MASK_WEBSQL | |
| 438 | StoragePartition::REMOVE_DATA_MASK_APPCACHE | |
| 439 | StoragePartition::REMOVE_DATA_MASK_INDEXEDDB)); |
| 440 | } |
| 441 | |
| 442 | void PopulateTestQuotaManagedPersistentData(quota::MockQuotaManager* manager) { |
| 443 | manager->AddOrigin(kOrigin2, kPersistent, kClientFile, base::Time()); |
| 444 | manager->AddOrigin(kOrigin3, kPersistent, kClientFile, |
| 445 | base::Time::Now() - base::TimeDelta::FromDays(1)); |
| 446 | |
| 447 | EXPECT_FALSE(manager->OriginHasData(kOrigin1, kPersistent, kClientFile)); |
| 448 | EXPECT_TRUE(manager->OriginHasData(kOrigin2, kPersistent, kClientFile)); |
| 449 | EXPECT_TRUE(manager->OriginHasData(kOrigin3, kPersistent, kClientFile)); |
| 450 | } |
| 451 | |
| 452 | void PopulateTestQuotaManagedTemporaryData(quota::MockQuotaManager* manager) { |
| 453 | manager->AddOrigin(kOrigin1, kTemporary, kClientFile, base::Time::Now()); |
| 454 | manager->AddOrigin(kOrigin3, kTemporary, kClientFile, |
| 455 | base::Time::Now() - base::TimeDelta::FromDays(1)); |
| 456 | |
| 457 | EXPECT_TRUE(manager->OriginHasData(kOrigin1, kTemporary, kClientFile)); |
| 458 | EXPECT_FALSE(manager->OriginHasData(kOrigin2, kTemporary, kClientFile)); |
| 459 | EXPECT_TRUE(manager->OriginHasData(kOrigin3, kTemporary, kClientFile)); |
| 460 | } |
| 461 | |
| 462 | void PopulateTestQuotaManagedData(quota::MockQuotaManager* manager) { |
| 463 | // Set up kOrigin1 with a temporary quota, kOrigin2 with a persistent |
| 464 | // quota, and kOrigin3 with both. kOrigin1 is modified now, kOrigin2 |
| 465 | // is modified at the beginning of time, and kOrigin3 is modified one day |
| 466 | // ago. |
| 467 | PopulateTestQuotaManagedPersistentData(manager); |
| 468 | PopulateTestQuotaManagedTemporaryData(manager); |
| 469 | } |
| 470 | |
| 471 | void PopulateTestQuotaManagedNonBrowsingData(quota::MockQuotaManager* manager) { |
| 472 | manager->AddOrigin(kOriginDevTools, kTemporary, kClientFile, base::Time()); |
| 473 | manager->AddOrigin(kOriginDevTools, kPersistent, kClientFile, base::Time()); |
| 474 | } |
| 475 | |
| 476 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverBoth) { |
| 477 | PopulateTestQuotaManagedData(GetMockManager()); |
| 478 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 479 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 480 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 481 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 482 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 483 | |
| 484 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 485 | base::MessageLoop::current()->PostTask( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 486 | FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop)); |
| 487 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 488 | |
| 489 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 490 | kClientFile)); |
| 491 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 492 | kClientFile)); |
| 493 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 494 | kClientFile)); |
| 495 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 496 | kClientFile)); |
| 497 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 498 | kClientFile)); |
| 499 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 500 | kClientFile)); |
| 501 | } |
| 502 | |
| 503 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyTemporary) { |
| 504 | PopulateTestQuotaManagedTemporaryData(GetMockManager()); |
| 505 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 506 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 507 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 508 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 509 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 510 | |
| 511 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 512 | base::MessageLoop::current()->PostTask( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 513 | FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop)); |
| 514 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 515 | |
| 516 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 517 | kClientFile)); |
| 518 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 519 | kClientFile)); |
| 520 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 521 | kClientFile)); |
| 522 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 523 | kClientFile)); |
| 524 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 525 | kClientFile)); |
| 526 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 527 | kClientFile)); |
| 528 | } |
| 529 | |
| 530 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverOnlyPersistent) { |
| 531 | PopulateTestQuotaManagedPersistentData(GetMockManager()); |
| 532 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 533 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 534 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 535 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 536 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 537 | |
| 538 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 539 | base::MessageLoop::current()->PostTask( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 540 | FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop)); |
| 541 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 542 | |
| 543 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 544 | kClientFile)); |
| 545 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 546 | kClientFile)); |
| 547 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 548 | kClientFile)); |
| 549 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 550 | kClientFile)); |
| 551 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 552 | kClientFile)); |
| 553 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 554 | kClientFile)); |
| 555 | } |
| 556 | |
| 557 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverNeither) { |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 558 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 559 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 560 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 561 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 562 | |
| 563 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 564 | base::MessageLoop::current()->PostTask( |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 565 | FROM_HERE, base::Bind(&ClearQuotaData, partition, &run_loop)); |
| 566 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 567 | |
| 568 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 569 | kClientFile)); |
| 570 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 571 | kClientFile)); |
| 572 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 573 | kClientFile)); |
| 574 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 575 | kClientFile)); |
| 576 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 577 | kClientFile)); |
| 578 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 579 | kClientFile)); |
| 580 | } |
| 581 | |
| 582 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForeverSpecificOrigin) { |
| 583 | PopulateTestQuotaManagedData(GetMockManager()); |
| 584 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 585 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 586 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 587 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 588 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 589 | |
| 590 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 591 | base::MessageLoop::current()->PostTask( |
| 592 | FROM_HERE, base::Bind(&ClearQuotaDataForOrigin, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 593 | partition, kOrigin1, base::Time(), |
| 594 | &run_loop)); |
| 595 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 596 | |
| 597 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 598 | kClientFile)); |
| 599 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 600 | kClientFile)); |
| 601 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 602 | kClientFile)); |
| 603 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 604 | kClientFile)); |
| 605 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 606 | kClientFile)); |
| 607 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 608 | kClientFile)); |
| 609 | } |
| 610 | |
| 611 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastHour) { |
| 612 | PopulateTestQuotaManagedData(GetMockManager()); |
| 613 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 614 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 615 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 616 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 617 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 618 | |
| 619 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 620 | base::MessageLoop::current()->PostTask( |
| 621 | FROM_HERE, base::Bind(&ClearQuotaDataForOrigin, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 622 | partition, GURL(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 623 | base::Time::Now() - base::TimeDelta::FromHours(1), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 624 | &run_loop)); |
| 625 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 626 | |
| 627 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 628 | kClientFile)); |
| 629 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 630 | kClientFile)); |
| 631 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 632 | kClientFile)); |
| 633 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 634 | kClientFile)); |
| 635 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 636 | kClientFile)); |
| 637 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 638 | kClientFile)); |
| 639 | } |
| 640 | |
| 641 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedDataForLastWeek) { |
| 642 | PopulateTestQuotaManagedData(GetMockManager()); |
| 643 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 644 | base::RunLoop run_loop; |
| 645 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 646 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 647 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 648 | GetMockManager()); |
| 649 | base::MessageLoop::current()->PostTask( |
| 650 | FROM_HERE, base::Bind(&ClearQuotaDataForNonPersistent, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 651 | partition, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 652 | base::Time::Now() - base::TimeDelta::FromDays(7), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 653 | &run_loop)); |
| 654 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 655 | |
| 656 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 657 | kClientFile)); |
| 658 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 659 | kClientFile)); |
| 660 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 661 | kClientFile)); |
| 662 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 663 | kClientFile)); |
| 664 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 665 | kClientFile)); |
| 666 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 667 | kClientFile)); |
| 668 | } |
| 669 | |
| 670 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedUnprotectedOrigins) { |
| 671 | // Protect kOrigin1. |
| 672 | scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 673 | new quota::MockSpecialStoragePolicy; |
| 674 | mock_policy->AddProtected(kOrigin1.GetOrigin()); |
| 675 | |
| 676 | PopulateTestQuotaManagedData(GetMockManager()); |
| 677 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 678 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 679 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 680 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 681 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 682 | partition->OverrideSpecialStoragePolicyForTesting(mock_policy); |
| 683 | |
| 684 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 685 | base::MessageLoop::current()->PostTask( |
| 686 | FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 687 | partition, GURL(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 688 | base::Bind(&DoesOriginMatchForUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 689 | base::Time(), &run_loop)); |
| 690 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 691 | |
| 692 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 693 | kClientFile)); |
| 694 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 695 | kClientFile)); |
| 696 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 697 | kClientFile)); |
| 698 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 699 | kClientFile)); |
| 700 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 701 | kClientFile)); |
| 702 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 703 | kClientFile)); |
| 704 | } |
| 705 | |
| 706 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedSpecificOrigin) { |
| 707 | // Protect kOrigin1. |
| 708 | scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 709 | new quota::MockSpecialStoragePolicy; |
| 710 | mock_policy->AddProtected(kOrigin1.GetOrigin()); |
| 711 | |
| 712 | PopulateTestQuotaManagedData(GetMockManager()); |
| 713 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 714 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 715 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 716 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 717 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 718 | partition->OverrideSpecialStoragePolicyForTesting(mock_policy); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 719 | |
| 720 | // Try to remove kOrigin1. Expect failure. |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 721 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 722 | base::MessageLoop::current()->PostTask( |
| 723 | FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 724 | partition, kOrigin1, |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 725 | base::Bind(&DoesOriginMatchForUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 726 | base::Time(), &run_loop)); |
| 727 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 728 | |
| 729 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 730 | kClientFile)); |
| 731 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 732 | kClientFile)); |
| 733 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 734 | kClientFile)); |
| 735 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 736 | kClientFile)); |
| 737 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 738 | kClientFile)); |
| 739 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 740 | kClientFile)); |
| 741 | } |
| 742 | |
| 743 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedProtectedOrigins) { |
| 744 | // Protect kOrigin1. |
| 745 | scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 746 | new quota::MockSpecialStoragePolicy; |
| 747 | mock_policy->AddProtected(kOrigin1.GetOrigin()); |
| 748 | |
| 749 | PopulateTestQuotaManagedData(GetMockManager()); |
| 750 | |
| 751 | // Try to remove kOrigin1. Expect success. |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 752 | base::RunLoop run_loop; |
| 753 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 754 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 755 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 756 | GetMockManager()); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 757 | partition->OverrideSpecialStoragePolicyForTesting(mock_policy); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 758 | base::MessageLoop::current()->PostTask( |
| 759 | FROM_HERE, |
| 760 | base::Bind(&ClearQuotaDataWithOriginMatcher, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 761 | partition, GURL(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 762 | base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 763 | base::Time(), &run_loop)); |
| 764 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 765 | |
| 766 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kTemporary, |
| 767 | kClientFile)); |
| 768 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kTemporary, |
| 769 | kClientFile)); |
| 770 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kTemporary, |
| 771 | kClientFile)); |
| 772 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin1, kPersistent, |
| 773 | kClientFile)); |
| 774 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin2, kPersistent, |
| 775 | kClientFile)); |
| 776 | EXPECT_FALSE(GetMockManager()->OriginHasData(kOrigin3, kPersistent, |
| 777 | kClientFile)); |
| 778 | } |
| 779 | |
| 780 | TEST_F(StoragePartitionImplTest, RemoveQuotaManagedIgnoreDevTools) { |
| 781 | PopulateTestQuotaManagedNonBrowsingData(GetMockManager()); |
| 782 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 783 | base::RunLoop run_loop; |
| 784 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 785 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 786 | partition->OverrideQuotaManagerForTesting( |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 787 | GetMockManager()); |
| 788 | base::MessageLoop::current()->PostTask( |
| 789 | FROM_HERE, base::Bind(&ClearQuotaDataWithOriginMatcher, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 790 | partition, GURL(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 791 | base::Bind(&DoesOriginMatchUnprotected), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 792 | base::Time(), &run_loop)); |
| 793 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 794 | |
| 795 | // Check that devtools data isn't removed. |
| 796 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kTemporary, |
| 797 | kClientFile)); |
| 798 | EXPECT_TRUE(GetMockManager()->OriginHasData(kOriginDevTools, kPersistent, |
| 799 | kClientFile)); |
| 800 | } |
| 801 | |
| 802 | TEST_F(StoragePartitionImplTest, RemoveCookieForever) { |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 803 | RemoveCookieTester tester(browser_context()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 804 | |
| 805 | tester.AddCookie(); |
| 806 | ASSERT_TRUE(tester.ContainsCookie()); |
| 807 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 808 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 809 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 810 | partition->SetURLRequestContext(browser_context()->GetRequestContext()); |
| 811 | |
| 812 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 813 | base::MessageLoop::current()->PostTask( |
| 814 | FROM_HERE, base::Bind(&ClearCookies, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 815 | partition, base::Time(), base::Time::Max(), |
| 816 | &run_loop)); |
| 817 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 818 | |
| 819 | EXPECT_FALSE(tester.ContainsCookie()); |
| 820 | } |
| 821 | |
| 822 | TEST_F(StoragePartitionImplTest, RemoveCookieLastHour) { |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 823 | RemoveCookieTester tester(browser_context()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 824 | |
| 825 | tester.AddCookie(); |
| 826 | ASSERT_TRUE(tester.ContainsCookie()); |
| 827 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 828 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 829 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 830 | base::Time an_hour_ago = base::Time::Now() - base::TimeDelta::FromHours(1); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 831 | partition->SetURLRequestContext(browser_context()->GetRequestContext()); |
| 832 | |
| 833 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 834 | base::MessageLoop::current()->PostTask( |
| 835 | FROM_HERE, base::Bind(&ClearCookies, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 836 | partition, an_hour_ago, base::Time::Max(), |
| 837 | &run_loop)); |
| 838 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 839 | |
| 840 | EXPECT_FALSE(tester.ContainsCookie()); |
| 841 | } |
| 842 | |
| 843 | TEST_F(StoragePartitionImplTest, RemoveUnprotectedLocalStorageForever) { |
| 844 | // Protect kOrigin1. |
| 845 | scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 846 | new quota::MockSpecialStoragePolicy; |
| 847 | mock_policy->AddProtected(kOrigin1.GetOrigin()); |
| 848 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 849 | RemoveLocalStorageTester tester(browser_context()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 850 | |
| 851 | tester.AddDOMStorageTestData(); |
| 852 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 853 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 854 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 855 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 856 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 857 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 858 | partition->OverrideSpecialStoragePolicyForTesting(mock_policy); |
| 859 | |
| 860 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 861 | base::MessageLoop::current()->PostTask( |
| 862 | FROM_HERE, |
| 863 | base::Bind(&ClearStuff, |
| 864 | StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 865 | partition, base::Time(), base::Time::Max(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 866 | base::Bind(&DoesOriginMatchForUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 867 | &run_loop)); |
| 868 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 869 | |
| 870 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 871 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 872 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 873 | } |
| 874 | |
| 875 | TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) { |
| 876 | // Protect kOrigin1. |
| 877 | scoped_refptr<quota::MockSpecialStoragePolicy> mock_policy = |
| 878 | new quota::MockSpecialStoragePolicy; |
| 879 | mock_policy->AddProtected(kOrigin1.GetOrigin()); |
| 880 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 881 | RemoveLocalStorageTester tester(browser_context()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 882 | |
| 883 | tester.AddDOMStorageTestData(); |
| 884 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 885 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 886 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 887 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 888 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 889 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
| 890 | partition->OverrideSpecialStoragePolicyForTesting(mock_policy); |
| 891 | |
| 892 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 893 | base::MessageLoop::current()->PostTask( |
| 894 | FROM_HERE, |
| 895 | base::Bind(&ClearStuff, |
| 896 | StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 897 | partition, base::Time(), base::Time::Max(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 898 | base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 899 | &run_loop)); |
| 900 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 901 | |
| 902 | // Even if kOrigin1 is protected, it will be deleted since we specify |
| 903 | // ClearData to delete protected data. |
| 904 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 905 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 906 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 907 | } |
| 908 | |
| 909 | TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) { |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 910 | RemoveLocalStorageTester tester(browser_context()); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 911 | |
| 912 | tester.AddDOMStorageTestData(); |
| 913 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 914 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 915 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 916 | |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 917 | StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( |
| 918 | BrowserContext::GetDefaultStoragePartition(browser_context())); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 919 | base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7); |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 920 | |
| 921 | base::RunLoop run_loop; |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 922 | base::MessageLoop::current()->PostTask( |
| 923 | FROM_HERE, |
| 924 | base::Bind(&ClearStuff, |
| 925 | StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 926 | partition, a_week_ago, base::Time::Max(), |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 927 | base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), |
[email protected] | 5ff8b2a | 2013-12-28 06:51:42 | [diff] [blame^] | 928 | &run_loop)); |
| 929 | run_loop.Run(); |
[email protected] | 88562b51 | 2013-11-06 21:10:44 | [diff] [blame] | 930 | |
| 931 | // kOrigin1 and kOrigin2 do not have age more than a week. |
| 932 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); |
| 933 | EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); |
| 934 | EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); |
| 935 | } |
| 936 | |
[email protected] | 877e261 | 2013-04-05 05:58:18 | [diff] [blame] | 937 | } // namespace content |