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

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

Issue 2847013002: Switch to mojo localstorage backend by default. (Closed)
Patch Set: minor cleanup Created 3 years, 7 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 db_binding_.Bind(
207 profile_->GetPath().AppendASCII("Local Storage"); 203 static_cast<DOMStorageContextWrapper*>(dom_storage_context_)
208 base::CreateDirectory(storage_path); 204 ->LocalStorageDatabaseRequestForTesting());
michaeln 2017/05/26 19:12:44 This is such a bizarre way to inject a mock instan
Marijn Kruisselbrink 2017/05/26 23:43:02 Generally there is two directions in which you can
michaeln 2017/05/27 01:59:03 thnx
209 205
210 // Write some files. 206 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 207
215 // Tweak their dates.
216 base::Time now = base::Time::Now(); 208 base::Time now = base::Time::Now();
217 base::TouchFile(storage_path.Append(kDomStorageOrigin1), now, now); 209 data.set_last_modified(now.ToInternalValue());
210 data.set_size_bytes(16);
211 mock_data_[CreateMetaDataKey(url::Origin(kOrigin1))] =
212 leveldb::StdStringToUint8Vector(data.SerializeAsString());
213 mock_data_[CreateDataKey(url::Origin(kOrigin1))] = {};
218 214
219 base::Time one_day_ago = now - base::TimeDelta::FromDays(1); 215 base::Time one_day_ago = now - base::TimeDelta::FromDays(1);
220 base::TouchFile(storage_path.Append(kDomStorageOrigin2), 216 data.set_last_modified(one_day_ago.ToInternalValue());
221 one_day_ago, one_day_ago); 217 mock_data_[CreateMetaDataKey(url::Origin(kOrigin2))] =
218 leveldb::StdStringToUint8Vector(data.SerializeAsString());
219 mock_data_[CreateDataKey(url::Origin(kOrigin2))] = {};
222 220
223 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60); 221 base::Time sixty_days_ago = now - base::TimeDelta::FromDays(60);
224 base::TouchFile(storage_path.Append(kDomStorageOrigin3), 222 data.set_last_modified(sixty_days_ago.ToInternalValue());
225 sixty_days_ago, sixty_days_ago); 223 mock_data_[CreateMetaDataKey(url::Origin(kOrigin3))] =
224 leveldb::StdStringToUint8Vector(data.SerializeAsString());
225 mock_data_[CreateDataKey(url::Origin(kOrigin3))] = {};
226 } 226 }
227 227
228 private: 228 private:
229 std::vector<uint8_t> CreateDataKey(const url::Origin& origin) {
230 auto serialized_origin =
231 leveldb::StdStringToUint8Vector(origin.Serialize());
232 std::vector<uint8_t> key = {'_'};
233 key.insert(key.end(), serialized_origin.begin(), serialized_origin.end());
234 key.push_back(0);
235 key.push_back('X');
236 return key;
237 }
238
239 std::vector<uint8_t> CreateMetaDataKey(const url::Origin& origin) {
240 const uint8_t kMetaPrefix[] = {'M', 'E', 'T', 'A', ':'};
241 auto serialized_origin =
242 leveldb::StdStringToUint8Vector(origin.Serialize());
243 std::vector<uint8_t> key;
244 key.reserve(arraysize(kMetaPrefix) + serialized_origin.size());
245 key.insert(key.end(), kMetaPrefix, kMetaPrefix + arraysize(kMetaPrefix));
246 key.insert(key.end(), serialized_origin.begin(), serialized_origin.end());
247 return key;
248 }
249
229 void GetLocalStorageUsage() { 250 void GetLocalStorageUsage() {
230 dom_storage_context_->GetLocalStorageUsage( 251 dom_storage_context_->GetLocalStorageUsage(
231 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage, 252 base::Bind(&RemoveLocalStorageTester::OnGotLocalStorageUsage,
232 base::Unretained(this))); 253 base::Unretained(this)));
233 } 254 }
255
234 void OnGotLocalStorageUsage( 256 void OnGotLocalStorageUsage(
235 const std::vector<content::LocalStorageUsageInfo>& infos) { 257 const std::vector<content::LocalStorageUsageInfo>& infos) {
236 infos_ = infos; 258 infos_ = infos;
237 await_completion_.Notify(); 259 await_completion_.Notify();
238 } 260 }
239 261
240 // We don't own these pointers. 262 // We don't own these pointers.
241 TestBrowserContext* profile_;
242 content::DOMStorageContext* dom_storage_context_; 263 content::DOMStorageContext* dom_storage_context_;
243 264
265 std::map<std::vector<uint8_t>, std::vector<uint8_t>> mock_data_;
266 MockLevelDBDatabase mock_db_;
267 mojo::AssociatedBinding<leveldb::mojom::LevelDBDatabase> db_binding_;
268
244 std::vector<content::LocalStorageUsageInfo> infos_; 269 std::vector<content::LocalStorageUsageInfo> infos_;
245 270
246 AwaitCompletionHelper await_completion_; 271 AwaitCompletionHelper await_completion_;
247 272
248 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester); 273 DISALLOW_COPY_AND_ASSIGN(RemoveLocalStorageTester);
249 }; 274 };
250 275
251 #if BUILDFLAG(ENABLE_PLUGINS) 276 #if BUILDFLAG(ENABLE_PLUGINS)
252 class RemovePluginPrivateDataTester { 277 class RemovePluginPrivateDataTester {
253 public: 278 public:
(...skipping 894 matching lines...) Expand 10 before | Expand all | Expand 10 after
1148 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get()); 1173 partition->OverrideSpecialStoragePolicyForTesting(mock_policy.get());
1149 1174
1150 base::RunLoop run_loop; 1175 base::RunLoop run_loop;
1151 base::ThreadTaskRunnerHandle::Get()->PostTask( 1176 base::ThreadTaskRunnerHandle::Get()->PostTask(
1152 FROM_HERE, 1177 FROM_HERE,
1153 base::Bind(&ClearStuff, 1178 base::Bind(&ClearStuff,
1154 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1179 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1155 partition, base::Time(), base::Time::Max(), 1180 partition, base::Time(), base::Time::Max(),
1156 base::Bind(&DoesOriginMatchForUnprotectedWeb), &run_loop)); 1181 base::Bind(&DoesOriginMatchForUnprotectedWeb), &run_loop));
1157 run_loop.Run(); 1182 run_loop.Run();
1183 // ClearData only guarantees that tasks to delete data are scheduled when its
1184 // callback is invoked. It doesn't guarantee data has actually been cleared.
1185 // So run all scheduled tasks to make sure data is cleared.
1186 base::RunLoop().RunUntilIdle();
1158 1187
1159 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1188 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
1160 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1189 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1161 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1190 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
1162 } 1191 }
1163 1192
1164 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) { 1193 TEST_F(StoragePartitionImplTest, RemoveProtectedLocalStorageForever) {
1165 // Protect kOrigin1. 1194 // Protect kOrigin1.
1166 scoped_refptr<MockSpecialStoragePolicy> mock_policy = 1195 scoped_refptr<MockSpecialStoragePolicy> mock_policy =
1167 new MockSpecialStoragePolicy; 1196 new MockSpecialStoragePolicy;
(...skipping 12 matching lines...) Expand all
1180 1209
1181 base::RunLoop run_loop; 1210 base::RunLoop run_loop;
1182 base::ThreadTaskRunnerHandle::Get()->PostTask( 1211 base::ThreadTaskRunnerHandle::Get()->PostTask(
1183 FROM_HERE, 1212 FROM_HERE,
1184 base::Bind(&ClearStuff, 1213 base::Bind(&ClearStuff,
1185 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1214 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1186 partition, base::Time(), base::Time::Max(), 1215 partition, base::Time(), base::Time::Max(),
1187 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), 1216 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
1188 &run_loop)); 1217 &run_loop));
1189 run_loop.Run(); 1218 run_loop.Run();
1219 // ClearData only guarantees that tasks to delete data are scheduled when its
1220 // callback is invoked. It doesn't guarantee data has actually been cleared.
1221 // So run all scheduled tasks to make sure data is cleared.
1222 base::RunLoop().RunUntilIdle();
1190 1223
1191 // Even if kOrigin1 is protected, it will be deleted since we specify 1224 // Even if kOrigin1 is protected, it will be deleted since we specify
1192 // ClearData to delete protected data. 1225 // ClearData to delete protected data.
1193 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1226 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
1194 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1227 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1195 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1228 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin3));
1196 } 1229 }
1197 1230
1198 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) { 1231 TEST_F(StoragePartitionImplTest, RemoveLocalStorageForLastWeek) {
1199 RemoveLocalStorageTester tester(browser_context()); 1232 RemoveLocalStorageTester tester(browser_context());
1200 1233
1201 tester.AddDOMStorageTestData(); 1234 tester.AddDOMStorageTestData();
1202 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1235 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin1));
1203 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1236 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin2));
1204 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1237 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
1205 1238
1206 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( 1239 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>(
1207 BrowserContext::GetDefaultStoragePartition(browser_context())); 1240 BrowserContext::GetDefaultStoragePartition(browser_context()));
1208 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7); 1241 base::Time a_week_ago = base::Time::Now() - base::TimeDelta::FromDays(7);
1209 1242
1210 base::RunLoop run_loop; 1243 base::RunLoop run_loop;
1211 base::ThreadTaskRunnerHandle::Get()->PostTask( 1244 base::ThreadTaskRunnerHandle::Get()->PostTask(
1212 FROM_HERE, 1245 FROM_HERE,
1213 base::Bind(&ClearStuff, 1246 base::Bind(&ClearStuff,
1214 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE, 1247 StoragePartitionImpl::REMOVE_DATA_MASK_LOCAL_STORAGE,
1215 partition, a_week_ago, base::Time::Max(), 1248 partition, a_week_ago, base::Time::Max(),
1216 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb), 1249 base::Bind(&DoesOriginMatchForBothProtectedAndUnprotectedWeb),
1217 &run_loop)); 1250 &run_loop));
1218 run_loop.Run(); 1251 run_loop.Run();
1252 // ClearData only guarantees that tasks to delete data are scheduled when its
1253 // callback is invoked. It doesn't guarantee data has actually been cleared.
1254 // So run all scheduled tasks to make sure data is cleared.
1255 base::RunLoop().RunUntilIdle();
1219 1256
1220 // kOrigin1 and kOrigin2 do not have age more than a week. 1257 // kOrigin1 and kOrigin2 do not have age more than a week.
1221 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1)); 1258 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin1));
1222 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2)); 1259 EXPECT_FALSE(tester.DOMStorageExistsForOrigin(kOrigin2));
1223 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3)); 1260 EXPECT_TRUE(tester.DOMStorageExistsForOrigin(kOrigin3));
1224 } 1261 }
1225 1262
1226 #if BUILDFLAG(ENABLE_PLUGINS) 1263 #if BUILDFLAG(ENABLE_PLUGINS)
1227 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForever) { 1264 TEST_F(StoragePartitionImplTest, RemovePluginPrivateDataForever) {
1228 StoragePartitionImpl* partition = static_cast<StoragePartitionImpl*>( 1265 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)); 1404 CanonicalCookie::Create(url2, "A=B;domain=.example.com", now, options));
1368 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options)); 1405 invalid_cookies.push_back(CanonicalCookie::Create(url3, "A=B", now, options));
1369 1406
1370 for (const auto& cookie : valid_cookies) 1407 for (const auto& cookie : valid_cookies)
1371 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString(); 1408 EXPECT_TRUE(predicate.Run(*cookie)) << cookie->DebugString();
1372 for (const auto& cookie : invalid_cookies) 1409 for (const auto& cookie : invalid_cookies)
1373 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString(); 1410 EXPECT_FALSE(predicate.Run(*cookie)) << cookie->DebugString();
1374 } 1411 }
1375 1412
1376 } // namespace content 1413 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698