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

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

Issue 2847013002: Switch to mojo localstorage backend by default. (Closed)
Patch Set: rebase Created 3 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"
11 #include "base/message_loop/message_loop.h" 11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h" 12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
14 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
15 #include "base/threading/thread_task_runner_handle.h" 15 #include "base/threading/thread_task_runner_handle.h"
16 #include "components/leveldb/public/cpp/util.h"
16 #include "content/browser/browser_thread_impl.h" 17 #include "content/browser/browser_thread_impl.h"
18 #include "content/browser/dom_storage/local_storage_database.pb.h"
17 #include "content/browser/gpu/shader_cache_factory.h" 19 #include "content/browser/gpu/shader_cache_factory.h"
18 #include "content/browser/storage_partition_impl.h" 20 #include "content/browser/storage_partition_impl.h"
19 #include "content/public/browser/local_storage_usage_info.h" 21 #include "content/public/browser/local_storage_usage_info.h"
20 #include "content/public/browser/storage_partition.h" 22 #include "content/public/browser/storage_partition.h"
21 #include "content/public/test/test_browser_context.h" 23 #include "content/public/test/test_browser_context.h"
22 #include "content/public/test/test_browser_thread.h" 24 #include "content/public/test/test_browser_thread.h"
23 #include "content/public/test/test_browser_thread_bundle.h" 25 #include "content/public/test/test_browser_thread_bundle.h"
26 #include "content/test/mock_leveldb_database.h"
24 #include "net/base/test_completion_callback.h" 27 #include "net/base/test_completion_callback.h"
25 #include "net/cookies/canonical_cookie.h" 28 #include "net/cookies/canonical_cookie.h"
26 #include "net/cookies/cookie_store.h" 29 #include "net/cookies/cookie_store.h"
27 #include "net/url_request/url_request_context.h" 30 #include "net/url_request/url_request_context.h"
28 #include "net/url_request/url_request_context_getter.h" 31 #include "net/url_request/url_request_context_getter.h"
29 #include "ppapi/features/features.h" 32 #include "ppapi/features/features.h"
30 #include "storage/browser/quota/quota_manager.h" 33 #include "storage/browser/quota/quota_manager.h"
31 #include "storage/browser/test/mock_quota_manager.h" 34 #include "storage/browser/test/mock_quota_manager.h"
32 #include "storage/browser/test/mock_special_storage_policy.h" 35 #include "storage/browser/test/mock_special_storage_policy.h"
33 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 25 matching lines...) Expand all
59 #if BUILDFLAG(ENABLE_PLUGINS) 62 #if BUILDFLAG(ENABLE_PLUGINS)
60 const char kWidevineCdmPluginId[] = "application_x-ppapi-widevine-cdm"; 63 const char kWidevineCdmPluginId[] = "application_x-ppapi-widevine-cdm";
61 const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm"; 64 const char kClearKeyCdmPluginId[] = "application_x-ppapi-clearkey-cdm";
62 #endif // BUILDFLAG(ENABLE_PLUGINS) 65 #endif // BUILDFLAG(ENABLE_PLUGINS)
63 66
64 const GURL kOrigin1(kTestOrigin1); 67 const GURL kOrigin1(kTestOrigin1);
65 const GURL kOrigin2(kTestOrigin2); 68 const GURL kOrigin2(kTestOrigin2);
66 const GURL kOrigin3(kTestOrigin3); 69 const GURL kOrigin3(kTestOrigin3);
67 const GURL kOriginDevTools(kTestOriginDevTools); 70 const GURL kOriginDevTools(kTestOriginDevTools);
68 71
69 const base::FilePath::CharType kDomStorageOrigin1[] =
70 FILE_PATH_LITERAL("http_host1_1.localstorage");
71
72 const base::FilePath::CharType kDomStorageOrigin2[] =
73 FILE_PATH_LITERAL("http_host2_1.localstorage");
74
75 const base::FilePath::CharType kDomStorageOrigin3[] =
76 FILE_PATH_LITERAL("http_host3_1.localstorage");
77
78 const storage::StorageType kTemporary = storage::kStorageTypeTemporary; 72 const storage::StorageType kTemporary = storage::kStorageTypeTemporary;
79 const storage::StorageType kPersistent = storage::kStorageTypePersistent; 73 const storage::StorageType kPersistent = storage::kStorageTypePersistent;
80 74
81 const storage::QuotaClient::ID kClientFile = storage::QuotaClient::kFileSystem; 75 const storage::QuotaClient::ID kClientFile = storage::QuotaClient::kFileSystem;
82 76
83 const uint32_t kAllQuotaRemoveMask = 77 const uint32_t kAllQuotaRemoveMask =
84 StoragePartition::REMOVE_DATA_MASK_APPCACHE | 78 StoragePartition::REMOVE_DATA_MASK_APPCACHE |
85 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS | 79 StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS |
86 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB | 80 StoragePartition::REMOVE_DATA_MASK_INDEXEDDB |
87 StoragePartition::REMOVE_DATA_MASK_WEBSQL; 81 StoragePartition::REMOVE_DATA_MASK_WEBSQL;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 bool get_cookie_success_; 170 bool get_cookie_success_;
177 AwaitCompletionHelper await_completion_; 171 AwaitCompletionHelper await_completion_;
178 net::CookieStore* cookie_store_; 172 net::CookieStore* cookie_store_;
179 173
180 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester); 174 DISALLOW_COPY_AND_ASSIGN(RemoveCookieTester);
181 }; 175 };
182 176
183 class RemoveLocalStorageTester { 177 class RemoveLocalStorageTester {
184 public: 178 public:
185 explicit RemoveLocalStorageTester(TestBrowserContext* profile) 179 explicit RemoveLocalStorageTester(TestBrowserContext* profile)
186 : profile_(profile), dom_storage_context_(NULL) { 180 : dom_storage_context_(NULL),
181 mock_db_(&mock_data_),
182 db_binding_(&mock_db_) {
187 dom_storage_context_ = 183 dom_storage_context_ =
188 content::BrowserContext::GetDefaultStoragePartition(profile)-> 184 content::BrowserContext::GetDefaultStoragePartition(profile)->
189 GetDOMStorageContext(); 185 GetDOMStorageContext();
190 } 186 }
191 187
192 // Returns true, if the given origin URL exists. 188 // Returns true, if the given origin URL exists.
193 bool DOMStorageExistsForOrigin(const GURL& origin) { 189 bool DOMStorageExistsForOrigin(const GURL& origin) {
194 GetLocalStorageUsage(); 190 GetLocalStorageUsage();
195 await_completion_.BlockUntilNotified(); 191 await_completion_.BlockUntilNotified();
196 for (size_t i = 0; i < infos_.size(); ++i) { 192 for (size_t i = 0; i < infos_.size(); ++i) {
197 if (origin == infos_[i].origin) 193 if (origin == infos_[i].origin)
198 return true; 194 return true;
199 } 195 }
200 return false; 196 return false;
201 } 197 }
202 198
203 void AddDOMStorageTestData() { 199 void AddDOMStorageTestData() {
204 // Note: This test depends on details of how the dom_storage library 200 // Note: This test depends on details of how the dom_storage library
205 // stores data in the host file system. 201 // stores data in the database.
206 base::FilePath storage_path = 202 leveldb::mojom::LevelDBDatabaseAssociatedPtr database_ptr;
207 profile_->GetPath().AppendASCII("Local Storage"); 203 leveldb::mojom::LevelDBDatabaseAssociatedRequest request =
208 base::CreateDirectory(storage_path); 204 MakeIsolatedRequest(&database_ptr);
205 static_cast<DOMStorageContextWrapper*>(dom_storage_context_)
206 ->SetLocalStorageDatabaseForTesting(std::move(database_ptr));
207 db_binding_.Bind(std::move(request));
209 208
210 // Write some files. 209 LocalStorageOriginMetaData data;
211 base::WriteFile(storage_path.Append(kDomStorageOrigin1), NULL, 0);
212 base::WriteFile(storage_path.Append(kDomStorageOrigin2), NULL, 0);
213 base::WriteFile(storage_path.Append(kDomStorageOrigin3), NULL, 0);
214 210
215 // Tweak their dates.
216 base::Time now = base::Time::Now(); 211 base::Time now = base::Time::Now();
217 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now); 212 data.set_last_modified(now.ToInternalValue());
213 data.set_size_bytes(16);
214 mock_data_[CreateMetaDataKey(url::Origin(kOrigin1))] =
215 leveldb::StdStringToUint8Vector(data.SerializeAsString());
216 mock_data_[CreateDataKey(url::Origin(kOrigin1))] = {};
218 217
219 base::Time one_day_ago = now - base::TimeDelta::FromDays(1); 218 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
220 base::TouchFile(storage_path.Append(kDomStorageOrigin2), 219 data.set_last_modified(one_day_ago.ToInternalValue());
221 one_day_ago, one_day_ago); 220 mock_data_[CreateMetaDataKey(url::Origin(kOrigin2))] =
221 leveldb::StdStringToUint8Vector(data.SerializeAsString());
222 mock_data_[CreateDataKey(url::Origin(kOrigin2))] = {};
222 223
223 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60); 224 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
224 base::TouchFile(storage_path.Append(kDomStorageOrigin3), 225 data.set_last_modified(sixty_days_ago.ToInternalValue());
225 sixty_days_ago, sixty_days_ago); 226 mock_data_[CreateMetaDataKey(url::Origin(kOrigin3))] =
227 leveldb::StdStringToUint8Vector(data.SerializeAsString());
228 mock_data_[CreateDataKey(url::Origin(kOrigin3))] = {};
226 } 229 }
227 230
228 private: 231 private:
232 std::vector<uint8_t> CreateDataKey(const url::Origin& origin) {
233 auto serialized_origin =
234 leveldb::StdStringToUint8Vector(origin.Serialize());
235 std::vector<uint8_t> key = {'_'};
236 key.insert(key.end(), serialized_origin.begin(), serialized_origin.end());
237 key.push_back(0);
238 key.push_back('X');
239 return key;
240 }
241
242 std::vector<uint8_t> CreateMetaDataKey(const url::Origin& origin) {
243 const uint8_t kMetaPrefix[] = {'M', 'E', 'T', 'A', ':'};
244 auto serialized_origin =
245 leveldb::StdStringToUint8Vector(origin.Serialize());
246 std::vector<uint8_t> key;
247 key.reserve(arraysize(kMetaPrefix) + serialized_origin.size());
248 key.insert(key.end(), kMetaPrefix, kMetaPrefix + arraysize(kMetaPrefix));
249 key.insert(key.end(), serialized_origin.begin(), serialized_origin.end());
250 return key;
251 }
252
229 void GetLocalStorageUsage() { 253 void GetLocalStorageUsage() {
230 dom_storage_context_->GetLocalStorageUsage( 254 dom_storage_context_->GetLocalStorageUsage(
231 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage, 255 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
232 base::Unretained(this))); 256 base::Unretained(this)));
233 } 257 }
258
234 void OnGotLocalStorageUsage( 259 void OnGotLocalStorageUsage(
235 const std::vector<content::LocalStorageUsageInfo>& infos) { 260 const std::vector<content::LocalStorageUsageInfo>& infos) {
236 infos_ = infos; 261 infos_ = infos;
237 await_completion_.Notify(); 262 await_completion_.Notify();
238 } 263 }
239 264
240 // We don't own these pointers. 265 // We don't own these pointers.
241 TestBrowserContext* profile_;
242 content::DOMStorageContext* dom_storage_context_; 266 content::DOMStorageContext* dom_storage_context_;
243 267
268 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
269 MockLevelDBDatabase mock_db_;
270 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_;
271
244 std::vector<content::LocalStorageUsageInfo> infos_; 272 std::vector<content::LocalStorageUsageInfo> infos_;
245 273
246 AwaitCompletionHelper await_completion_; 274 AwaitCompletionHelper await_completion_;
247 275
248 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester); 276 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
249 }; 277 };
250 278
251 #if BUILDFLAG(ENABLE_PLUGINS) 279 #if BUILDFLAG(ENABLE_PLUGINS)
252 class RemovePluginPrivateDataTester { 280 class RemovePluginPrivateDataTester {
253 public: 281 public:
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get()); 1176 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
1149 1177
1150 base::RunLoop run_loop; 1178 base::RunLoop run_loop;
1151 base::ThreadTaskRunnerHandle::Get()->PostTask( 1179 base::ThreadTaskRunnerHandle::Get()->PostTask(
1152 FROM_HERE, 1180 FROM_HERE,
1153 base::Bind(&ClearStuff, 1181 base::Bind(&ClearStuff,
1154 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1182 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1155 partition, base::Time(), base::Time::Max(), 1183 partition, base::Time(), base::Time::Max(),
1156 base::Bind(&DoesOriginMatchForUnprotectedWeb), &run_loop)); 1184 base::Bind(&DoesOriginMatchForUnprotectedWeb), &run_loop));
1157 run_loop.Run(); 1185 run_loop.Run();
1186 // ClearData only guarantees that tasks to delete data are scheduled when its
1187 // callback is invoked. It doesn't guarantee data has actually been cleared.
1188 // So run all scheduled tasks to make sure data is cleared.
1189 base::RunLoop().RunUntilIdle();
1158 1190
1159 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1191 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
1160 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1192 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1161 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1193 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
1162 } 1194 }
1163 1195
1164 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) { 1196 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
1165 // Protect kOrigin1. 1197 // Protect kOrigin1.
1166 scoped_refptr<MockSpecialStoragePolicy> mock_policy = 1198 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
1167 new MockSpecialStoragePolicy; 1199 new MockSpecialStoragePolicy;
(...skipping 12 matching lines...) Expand all
1180 1212
1181 base::RunLoop run_loop; 1213 base::RunLoop run_loop;
1182 base::ThreadTaskRunnerHandle::Get()->PostTask( 1214 base::ThreadTaskRunnerHandle::Get()->PostTask(
1183 FROM_HERE, 1215 FROM_HERE,
1184 base::Bind(&ClearStuff, 1216 base::Bind(&ClearStuff,
1185 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1217 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1186 partition, base::Time(), base::Time::Max(), 1218 partition, base::Time(), base::Time::Max(),
1187 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), 1219 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
1188 &run_loop)); 1220 &run_loop));
1189 run_loop.Run(); 1221 run_loop.Run();
1222 // ClearData only guarantees that tasks to delete data are scheduled when its
1223 // callback is invoked. It doesn't guarantee data has actually been cleared.
1224 // So run all scheduled tasks to make sure data is cleared.
1225 base::RunLoop().RunUntilIdle();
1190 1226
1191 // Even if kOrigin1 is protected, it will be deleted since we specify 1227 // Even if kOrigin1 is protected, it will be deleted since we specify
1192 // ClearData to delete protected data. 1228 // ClearData to delete protected data.
1193 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1229 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
1194 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1230 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1195 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1231 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
1196 } 1232 }
1197 1233
1198 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) { 1234 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
1199 RemoveLocalStorageTester tester(browser_context()); 1235 RemoveLocalStorageTester tester(browser_context());
1200 1236
1201 tester.AddDOMStorageTestData(); 1237 tester.AddDOMStorageTestData();
1202 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1238 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
1203 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1239 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
1204 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1240 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
1205 1241
1206 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( 1242 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
1207 BrowserContext::GetDefaultStoragePartition(browser_context())); 1243 BrowserContext::GetDefaultStoragePartition(browser_context()));
1208 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7); 1244 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
1209 1245
1210 base::RunLoop run_loop; 1246 base::RunLoop run_loop;
1211 base::ThreadTaskRunnerHandle::Get()->PostTask( 1247 base::ThreadTaskRunnerHandle::Get()->PostTask(
1212 FROM_HERE, 1248 FROM_HERE,
1213 base::Bind(&ClearStuff, 1249 base::Bind(&ClearStuff,
1214 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1250 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1215 partition, a_week_ago, base::Time::Max(), 1251 partition, a_week_ago, base::Time::Max(),
1216 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), 1252 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
1217 &run_loop)); 1253 &run_loop));
1218 run_loop.Run(); 1254 run_loop.Run();
1255 // ClearData only guarantees that tasks to delete data are scheduled when its
1256 // callback is invoked. It doesn't guarantee data has actually been cleared.
1257 // So run all scheduled tasks to make sure data is cleared.
1258 base::RunLoop().RunUntilIdle();
1219 1259
1220 // kOrigin1 and kOrigin2 do not have age more than a week. 1260 // kOrigin1 and kOrigin2 do not have age more than a week.
1221 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1261 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
1222 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1262 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1223 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1263 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
1224 } 1264 }
1225 1265
1226 #if BUILDFLAG(ENABLE_PLUGINS) 1266 #if BUILDFLAG(ENABLE_PLUGINS)
1227 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForever) { 1267 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForever) {
1228 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( 1268 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1367 CanonicalCookie::Create(url2, "A=B;domain=.example.com", now, options)); 1407 CanonicalCookie::Create(url2, "A=B;domain=.example.com", now, options));
1368 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options)); 1408 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options));
1369 1409
1370 for (const auto& cookie : valid_cookies) 1410 for (const auto& cookie : valid_cookies)
1371 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString(); 1411 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString();
1372 for (const auto& cookie : invalid_cookies) 1412 for (const auto& cookie : invalid_cookies)
1373 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString(); 1413 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString();
1374 } 1414 }
1375 1415
1376 } // namespace content 1416 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698