Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(340)

Side by Side Diff: content/browser/storage_partition_impl_unittest.cc

Issue 1979733002: Add ability to clear content licenses (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: FileTaskRunner Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/macros.h" 10 #include "base/macros.h"
(...skipping 12 matching lines...) Expand all
23 #include "content/public/test/test_browser_thread.h" 23 #include "content/public/test/test_browser_thread.h"
24 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "content/public/test/test_browser_thread_bundle.h"
25 #include "net/base/test_completion_callback.h" 25 #include "net/base/test_completion_callback.h"
26 #include "net/cookies/canonical_cookie.h" 26 #include "net/cookies/canonical_cookie.h"
27 #include "net/cookies/cookie_store.h" 27 #include "net/cookies/cookie_store.h"
28 #include "net/url_request/url_request_context.h" 28 #include "net/url_request/url_request_context.h"
29 #include "net/url_request/url_request_context_getter.h" 29 #include "net/url_request/url_request_context_getter.h"
30 #include "storage/browser/quota/quota_manager.h" 30 #include "storage/browser/quota/quota_manager.h"
31 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
32 32
33 #if defined(ENABLE_PLUGINS)
34 #include "ppapi/shared_impl/ppapi_constants.h"
35 #include "storage/browser/fileapi/async_file_util.h"
36 #include "storage/browser/fileapi/file_system_context.h"
37 #include "storage/browser/fileapi/file_system_operation_context.h"
38 #include "storage/browser/fileapi/isolated_context.h"
39 #endif // defined(ENABLE_PLUGINS)
40
33 using net::CanonicalCookie; 41 using net::CanonicalCookie;
34 42
35 namespace content { 43 namespace content {
36 namespace { 44 namespace {
37 45
38 const int kDefaultClientId = 42; 46 const int kDefaultClientId = 42;
39 const char kCacheKey[] = "key"; 47 const char kCacheKey[] = "key";
40 const char kCacheValue[] = "cached value"; 48 const char kCacheValue[] = "cached value";
41 49
42 const char kTestOrigin1[] = "http://host1:1/"; 50 const char kTestOrigin1[] = "http://host1:1/";
43 const char kTestOrigin2[] = "http://host2:1/"; 51 const char kTestOrigin2[] = "http://host2:1/";
44 const char kTestOrigin3[] = "http://host3:1/"; 52 const char kTestOrigin3[] = "http://host3:1/";
45 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/"; 53 const char kTestOriginDevTools[] = "chrome-devtools://abcdefghijklmnopqrstuvw/";
46 54
55 #if defined(ENABLE_PLUGINS)
56 const char kWidevineCdmPluginId[] = "application_x-ppapi-widevine-cdm";
57 const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm";
58 #endif // defined(ENABLE_PLUGINS)
59
47 const GURL kOrigin1(kTestOrigin1); 60 const GURL kOrigin1(kTestOrigin1);
48 const GURL kOrigin2(kTestOrigin2); 61 const GURL kOrigin2(kTestOrigin2);
49 const GURL kOrigin3(kTestOrigin3); 62 const GURL kOrigin3(kTestOrigin3);
50 const GURL kOriginDevTools(kTestOriginDevTools); 63 const GURL kOriginDevTools(kTestOriginDevTools);
51 64
52 const base::FilePath::CharType kDomStorageOrigin1[] = 65 const base::FilePath::CharType kDomStorageOrigin1[] =
53 FILE_PATH_LITERAL("http_host1_1.localstorage"); 66 FILE_PATH_LITERAL("http_host1_1.localstorage");
54 67
55 const base::FilePath::CharType kDomStorageOrigin2[] = 68 const base::FilePath::CharType kDomStorageOrigin2[] =
56 FILE_PATH_LITERAL("http_host2_1.localstorage"); 69 FILE_PATH_LITERAL("http_host2_1.localstorage");
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 TestBrowserContext* profile_; 237 TestBrowserContext* profile_;
225 content::DOMStorageContext* dom_storage_context_; 238 content::DOMStorageContext* dom_storage_context_;
226 239
227 std::vector<content::LocalStorageUsageInfo> infos_; 240 std::vector<content::LocalStorageUsageInfo> infos_;
228 241
229 AwaitCompletionHelper await_completion_; 242 AwaitCompletionHelper await_completion_;
230 243
231 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester); 244 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
232 }; 245 };
233 246
247 #if defined(ENABLE_PLUGINS)
248 class RemovePluginPrivateDataTester {
249 public:
250 explicit RemovePluginPrivateDataTester(
251 storage::FileSystemContext* filesystem_context)
252 : filesystem_context_(filesystem_context) {}
253
254 // Add some files to the PluginPrivateFileSystem. They are created as follows:
255 // kOrigin1 - ClearKey - 1 file - timestamp 10 days ago
256 // kOrigin2 - Widevine - 2 files - timestamps now and 60 days ago
257 void AddPluginPrivateTestData() {
258 base::Time now = base::Time::Now();
259 base::Time ten_days_ago = now - base::TimeDelta::FromDays(10);
260 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
261
262 // Create a PluginPrivateFileSystem for ClearKey and add a single file
263 // with a timestamp of 1 day ago.
264 std::string clearkey_fsid =
265 CreateFileSystem(kClearKeyCdmPluginId, kOrigin1);
266 storage::FileSystemURL clearkey_file =
267 CreateFile(kOrigin1, clearkey_fsid, "foo");
268 SetFileTimestamp(clearkey_file, ten_days_ago);
269
270 // Create a second PluginPrivateFileSystem for Widevine and add two files
271 // with different times.
272 std::string widevine_fsid =
273 CreateFileSystem(kWidevineCdmPluginId, kOrigin2);
274 storage::FileSystemURL widevine_file1 =
275 CreateFile(kOrigin2, widevine_fsid, "bar1");
276 storage::FileSystemURL widevine_file2 =
277 CreateFile(kOrigin2, widevine_fsid, "bar2");
278 SetFileTimestamp(widevine_file1, now);
279 SetFileTimestamp(widevine_file2, sixty_days_ago);
280 }
281
282 // Returns true, if the given origin exists in a PluginPrivateFileSystem.
283 bool DataExistsForOrigin(const GURL& origin) {
284 storage::FileSystemBackend* backend =
285 filesystem_context_->GetFileSystemBackend(
286 storage::kFileSystemTypePluginPrivate);
287 storage::FileSystemQuotaUtil* quota_util = backend->GetQuotaUtil();
288
289 // Determine the set of origins used.
290 std::set<GURL> origins;
291 quota_util->GetOriginsForTypeOnFileTaskRunner(
292 storage::kFileSystemTypePluginPrivate, &origins);
293 return origins.find(origin) != origins.end();
294 }
295
296 private:
297 // Creates a PluginPrivateFileSystem for the |plugin_name| and |origin|
298 // provided. Returns the file system ID for the created
299 // PluginPrivateFileSystem.
300 std::string CreateFileSystem(const std::string& plugin_name,
301 const GURL& origin) {
302 AwaitCompletionHelper await_completion;
303 std::string fsid = storage::IsolatedContext::GetInstance()
304 ->RegisterFileSystemForVirtualPath(
305 storage::kFileSystemTypePluginPrivate,
306 ppapi::kPluginPrivateRootName, base::FilePath());
307 EXPECT_TRUE(storage::ValidateIsolatedFileSystemId(fsid));
308 filesystem_context_->OpenPluginPrivateFileSystem(
309 origin, storage::kFileSystemTypePluginPrivate, fsid, plugin_name,
310 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT,
311 base::Bind(&RemovePluginPrivateDataTester::OnFileSystemOpened,
312 base::Unretained(this), &await_completion));
313 await_completion.BlockUntilNotified();
314 return fsid;
315 }
316
317 // Creates a file named |file_name| in the PluginPrivateFileSystem identified
318 // by |origin| and |fsid|. Returns the URL for the created file. The file
319 // must not already exist or the test will fail.
320 storage::FileSystemURL CreateFile(const GURL& origin,
321 const std::string& fsid,
322 const std::string& file_name) {
323 AwaitCompletionHelper await_completion;
324 std::string root = storage::GetIsolatedFileSystemRootURIString(
325 origin, fsid, ppapi::kPluginPrivateRootName);
326 storage::FileSystemURL file_url =
327 filesystem_context_->CrackURL(GURL(root + file_name));
328 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
329 storage::kFileSystemTypePluginPrivate);
330 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
331 base::WrapUnique(
332 new storage::FileSystemOperationContext(filesystem_context_));
333 operation_context->set_allowed_bytes_growth(
334 storage::QuotaManager::kNoLimit);
335 file_util->EnsureFileExists(
336 std::move(operation_context), file_url,
337 base::Bind(&RemovePluginPrivateDataTester::OnFileCreated,
338 base::Unretained(this), &await_completion));
339 await_completion.BlockUntilNotified();
340 return file_url;
341 }
342
343 // Sets the last_access_time and last_modified_time to |time_stamp| on the
344 // file specified by |file_url|. The file must already exist.
345 void SetFileTimestamp(const storage::FileSystemURL& file_url,
346 const base::Time& time_stamp) {
347 AwaitCompletionHelper await_completion;
348 storage::AsyncFileUtil* file_util = filesystem_context_->GetAsyncFileUtil(
349 storage::kFileSystemTypePluginPrivate);
350 std::unique_ptr<storage::FileSystemOperationContext> operation_context =
351 base::WrapUnique(
352 new storage::FileSystemOperationContext(filesystem_context_));
353 file_util->Touch(std::move(operation_context), file_url, time_stamp,
354 time_stamp,
355 base::Bind(&RemovePluginPrivateDataTester::OnFileTouched,
356 base::Unretained(this), &await_completion));
357 await_completion.BlockUntilNotified();
358 }
359
360 void OnFileSystemOpened(AwaitCompletionHelper* await_completion,
361 base::File::Error result) {
362 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
363 await_completion->Notify();
364 }
365
366 void OnFileCreated(AwaitCompletionHelper* await_completion,
367 base::File::Error result,
368 bool created) {
369 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
370 EXPECT_TRUE(created);
371 await_completion->Notify();
372 }
373
374 void OnFileTouched(AwaitCompletionHelper* await_completion,
375 base::File::Error result) {
376 EXPECT_EQ(base::File::FILE_OK, result) << base::File::ErrorToString(result);
377 await_completion->Notify();
378 }
379
380 // We don't own this pointer.
381 storage::FileSystemContext* filesystem_context_;
382
383 DISALLOW_COPY_AND_ASSIGN(RemovePluginPrivateDataTester);
384 };
385 #endif // defined(ENABLE_PLUGINS)
386
234 bool IsWebSafeSchemeForTest(const std::string& scheme) { 387 bool IsWebSafeSchemeForTest(const std::string& scheme) {
235 return scheme == "http"; 388 return scheme == "http";
236 } 389 }
237 390
238 bool DoesOriginMatchForUnprotectedWeb( 391 bool DoesOriginMatchForUnprotectedWeb(
239 const GURL& origin, 392 const GURL& origin,
240 storage::SpecialStoragePolicy* special_storage_policy) { 393 storage::SpecialStoragePolicy* special_storage_policy) {
241 if (IsWebSafeSchemeForTest(origin.scheme())) 394 if (IsWebSafeSchemeForTest(origin.scheme()))
242 return !special_storage_policy->IsStorageProtected(origin.GetOrigin()); 395 return !special_storage_policy->IsStorageProtected(origin.GetOrigin());
243 396
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 void ClearData(content::StoragePartition* partition, 490 void ClearData(content::StoragePartition* partition,
338 base::RunLoop* run_loop) { 491 base::RunLoop* run_loop) {
339 base::Time time; 492 base::Time time;
340 partition->ClearData( 493 partition->ClearData(
341 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE, 494 StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE,
342 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, 495 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
343 GURL(), StoragePartition::OriginMatcherFunction(), 496 GURL(), StoragePartition::OriginMatcherFunction(),
344 time, time, run_loop->QuitClosure()); 497 time, time, run_loop->QuitClosure());
345 } 498 }
346 499
500 #if defined(ENABLE_PLUGINS)
501 void ClearPluginPrivateData(content::StoragePartition* partition,
502 const GURL& storage_origin,
503 const base::Time delete_begin,
504 const base::Time delete_end,
505 base::RunLoop* run_loop) {
506 partition->ClearData(
507 StoragePartitionImpl::REMOVE_DATA_MASK_PLUGIN_PRIVATE_DATA,
508 StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, storage_origin,
509 StoragePartition::OriginMatcherFunction(), delete_begin, delete_end,
510 run_loop->QuitClosure());
511 }
512 #endif // defined(ENABLE_PLUGINS)
513
347 } // namespace 514 } // namespace
348 515
349 class StoragePartitionImplTest : public testing::Test { 516 class StoragePartitionImplTest : public testing::Test {
350 public: 517 public:
351 StoragePartitionImplTest() 518 StoragePartitionImplTest()
352 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), 519 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP |
353 browser_context_(new TestBrowserContext()) { 520 content::TestBrowserThreadBundle::REAL_FILE_THREAD),
354 } 521 browser_context_(new TestBrowserContext()) {}
355 522
356 MockQuotaManager* GetMockManager() { 523 MockQuotaManager* GetMockManager() {
357 if (!quota_manager_.get()) { 524 if (!quota_manager_.get()) {
358 quota_manager_ = new MockQuotaManager( 525 quota_manager_ = new MockQuotaManager(
359 browser_context_->IsOffTheRecord(), 526 browser_context_->IsOffTheRecord(),
360 browser_context_->GetPath(), 527 browser_context_->GetPath(),
361 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(), 528 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO).get(),
362 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), 529 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(),
363 browser_context_->GetSpecialStoragePolicy()); 530 browser_context_->GetSpecialStoragePolicy());
364 } 531 }
(...skipping 603 matching lines...) Expand 10 before | Expand all | Expand 10 after
968 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), 1135 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
969 &run_loop)); 1136 &run_loop));
970 run_loop.Run(); 1137 run_loop.Run();
971 1138
972 // kOrigin1 and kOrigin2 do not have age more than a week. 1139 // kOrigin1 and kOrigin2 do not have age more than a week.
973 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1140 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
974 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1141 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
975 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1142 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
976 } 1143 }
977 1144
1145 #if defined(ENABLE_PLUGINS)
1146 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForever) {
1147 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
1148 BrowserContext::GetDefaultStoragePartition(browser_context()));
1149
1150 RemovePluginPrivateDataTester tester(partition->GetFileSystemContext());
1151 tester.AddPluginPrivateTestData();
1152 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin1));
1153 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin2));
1154
1155 base::RunLoop run_loop;
1156 base::ThreadTaskRunnerHandle::Get()->PostTask(
1157 FROM_HERE, base::Bind(&ClearPluginPrivateData, partition, GURL(),
1158 base::Time(), base::Time::Max(), &run_loop));
1159 run_loop.Run();
1160
1161 EXPECT_FALSE(tester.DataExistsForOrigin(kOrigin1));
1162 EXPECT_FALSE(tester.DataExistsForOrigin(kOrigin2));
1163 }
1164
1165 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataLastWeek) {
1166 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
1167 BrowserContext::GetDefaultStoragePartition(browser_context()));
1168 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
1169
1170 RemovePluginPrivateDataTester tester(partition->GetFileSystemContext());
1171 tester.AddPluginPrivateTestData();
1172 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin1));
1173 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin2));
1174
1175 base::RunLoop run_loop;
1176 base::ThreadTaskRunnerHandle::Get()->PostTask(
1177 FROM_HERE, base::Bind(&ClearPluginPrivateData, partition, GURL(),
1178 a_week_ago, base::Time::Max(), &run_loop));
1179 run_loop.Run();
1180
1181 // Origin1 has 1 file from 10 days ago, so it should remain around.
1182 // Origin2 has a current file, so it should be removed (even though the
1183 // second file is much older).
1184 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin1));
1185 EXPECT_FALSE(tester.DataExistsForOrigin(kOrigin2));
1186 }
1187
1188 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForOrigin) {
1189 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
1190 BrowserContext::GetDefaultStoragePartition(browser_context()));
1191
1192 RemovePluginPrivateDataTester tester(partition->GetFileSystemContext());
1193 tester.AddPluginPrivateTestData();
1194 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin1));
1195 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin2));
1196
1197 base::RunLoop run_loop;
1198 base::ThreadTaskRunnerHandle::Get()->PostTask(
1199 FROM_HERE, base::Bind(&ClearPluginPrivateData, partition, kOrigin1,
1200 base::Time(), base::Time::Max(), &run_loop));
1201 run_loop.Run();
1202
1203 // Only Origin1 should be deleted.
1204 EXPECT_FALSE(tester.DataExistsForOrigin(kOrigin1));
1205 EXPECT_TRUE(tester.DataExistsForOrigin(kOrigin2));
1206 }
1207 #endif // defined(ENABLE_PLUGINS)
1208
978 TEST(StoragePartitionImplStaticTest, CreatePredicateForHostCookies) { 1209 TEST(StoragePartitionImplStaticTest, CreatePredicateForHostCookies) {
979 GURL url("http://www.example.com/"); 1210 GURL url("http://www.example.com/");
980 GURL url2("https://www.example.com/"); 1211 GURL url2("https://www.example.com/");
981 GURL url3("https://www.google.com/"); 1212 GURL url3("https://www.google.com/");
982 1213
983 net::CookieOptions options; 1214 net::CookieOptions options;
984 net::CookieStore::CookiePredicate predicate = 1215 net::CookieStore::CookiePredicate predicate =
985 StoragePartitionImpl::CreatePredicateForHostCookies(url); 1216 StoragePartitionImpl::CreatePredicateForHostCookies(url);
986 1217
987 base::Time now = base::Time::Now(); 1218 base::Time now = base::Time::Now();
988 std::vector<std::unique_ptr<CanonicalCookie>> valid_cookies; 1219 std::vector<std::unique_ptr<CanonicalCookie>> valid_cookies;
989 valid_cookies.push_back(CanonicalCookie::Create(url, "A=B", now, options)); 1220 valid_cookies.push_back(CanonicalCookie::Create(url, "A=B", now, options));
990 valid_cookies.push_back(CanonicalCookie::Create(url, "C=F", now, options)); 1221 valid_cookies.push_back(CanonicalCookie::Create(url, "C=F", now, options));
991 // We should match a different scheme with the same host. 1222 // We should match a different scheme with the same host.
992 valid_cookies.push_back(CanonicalCookie::Create(url2, "A=B", now, options)); 1223 valid_cookies.push_back(CanonicalCookie::Create(url2, "A=B", now, options));
993 1224
994 std::vector<std::unique_ptr<CanonicalCookie>> invalid_cookies; 1225 std::vector<std::unique_ptr<CanonicalCookie>> invalid_cookies;
995 // We don't match domain cookies. 1226 // We don't match domain cookies.
996 invalid_cookies.push_back( 1227 invalid_cookies.push_back(
997 CanonicalCookie::Create(url2, "A=B;domain=.example.com", now, options)); 1228 CanonicalCookie::Create(url2, "A=B;domain=.example.com", now, options));
998 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options)); 1229 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options));
999 1230
1000 for (const auto& cookie : valid_cookies) 1231 for (const auto& cookie : valid_cookies)
1001 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString(); 1232 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString();
1002 for (const auto& cookie : invalid_cookies) 1233 for (const auto& cookie : invalid_cookies)
1003 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString(); 1234 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString();
1004 } 1235 }
1005 1236
1006 } // namespace content 1237 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698