Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 1 | // Copyright 2013 The Chromium Authors. All rights reserved. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/bind.h" |
| 6 | #include "base/file_util.h" |
| 7 | #include "base/files/scoped_temp_dir.h" |
Ben Murdoch | ca12bfa | 2013-07-23 11:17:05 +0100 | [diff] [blame] | 8 | #include "base/message_loop/message_loop.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/message_loop/message_loop_proxy.h" |
| 10 | #include "base/strings/utf_string_conversions.h" |
| 11 | #include "base/threading/sequenced_worker_pool.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 12 | #include "base/time/time.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 13 | #include "content/browser/dom_storage/dom_storage_area.h" |
| 14 | #include "content/browser/dom_storage/dom_storage_context_impl.h" |
| 15 | #include "content/browser/dom_storage/dom_storage_namespace.h" |
| 16 | #include "content/browser/dom_storage/dom_storage_task_runner.h" |
| 17 | #include "content/public/browser/local_storage_usage_info.h" |
| 18 | #include "content/public/browser/session_storage_usage_info.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 19 | #include "testing/gtest/include/gtest/gtest.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 20 | #include "webkit/browser/quota/mock_special_storage_policy.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 21 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 22 | namespace content { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 23 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 24 | class DOMStorageContextImplTest : public testing::Test { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 25 | public: |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 26 | DOMStorageContextImplTest() |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 27 | : kOrigin(GURL("http://dom_storage/")), |
| 28 | kKey(ASCIIToUTF16("key")), |
| 29 | kValue(ASCIIToUTF16("value")), |
| 30 | kDontIncludeFileInfo(false), |
| 31 | kDoIncludeFileInfo(true) { |
| 32 | } |
| 33 | |
| 34 | const GURL kOrigin; |
| 35 | const base::string16 kKey; |
| 36 | const base::string16 kValue; |
| 37 | const bool kDontIncludeFileInfo; |
| 38 | const bool kDoIncludeFileInfo; |
| 39 | |
| 40 | virtual void SetUp() { |
| 41 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 42 | storage_policy_ = new quota::MockSpecialStoragePolicy; |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 43 | task_runner_ = |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 44 | new MockDOMStorageTaskRunner(base::MessageLoopProxy::current().get()); |
| 45 | context_ = new DOMStorageContextImpl(temp_dir_.path(), |
| 46 | base::FilePath(), |
| 47 | storage_policy_.get(), |
| 48 | task_runner_.get()); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | virtual void TearDown() { |
| 52 | base::MessageLoop::current()->RunUntilIdle(); |
| 53 | } |
| 54 | |
| 55 | void VerifySingleOriginRemains(const GURL& origin) { |
| 56 | // Use a new instance to examine the contexts of temp_dir_. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 57 | scoped_refptr<DOMStorageContextImpl> context = |
| 58 | new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(), |
| 59 | NULL, NULL); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 60 | std::vector<LocalStorageUsageInfo> infos; |
| 61 | context->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
| 62 | ASSERT_EQ(1u, infos.size()); |
| 63 | EXPECT_EQ(origin, infos[0].origin); |
| 64 | } |
| 65 | |
| 66 | protected: |
| 67 | base::MessageLoop message_loop_; |
| 68 | base::ScopedTempDir temp_dir_; |
| 69 | scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy_; |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 70 | scoped_refptr<MockDOMStorageTaskRunner> task_runner_; |
| 71 | scoped_refptr<DOMStorageContextImpl> context_; |
| 72 | DISALLOW_COPY_AND_ASSIGN(DOMStorageContextImplTest); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 75 | TEST_F(DOMStorageContextImplTest, Basics) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 76 | // This test doesn't do much, checks that the constructor |
| 77 | // initializes members properly and that invoking methods |
| 78 | // on a newly created object w/o any data on disk do no harm. |
| 79 | EXPECT_EQ(temp_dir_.path(), context_->localstorage_directory()); |
| 80 | EXPECT_EQ(base::FilePath(), context_->sessionstorage_directory()); |
| 81 | EXPECT_EQ(storage_policy_.get(), context_->special_storage_policy_.get()); |
| 82 | context_->PurgeMemory(); |
| 83 | context_->DeleteLocalStorage(GURL("http://chromium.org/")); |
| 84 | const int kFirstSessionStorageNamespaceId = 1; |
| 85 | EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)); |
| 86 | EXPECT_FALSE(context_->GetStorageNamespace(kFirstSessionStorageNamespaceId)); |
| 87 | EXPECT_EQ(kFirstSessionStorageNamespaceId, context_->AllocateSessionId()); |
| 88 | std::vector<LocalStorageUsageInfo> infos; |
| 89 | context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
| 90 | EXPECT_TRUE(infos.empty()); |
| 91 | context_->Shutdown(); |
| 92 | } |
| 93 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 94 | TEST_F(DOMStorageContextImplTest, UsageInfo) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 95 | // Should be empty initially |
| 96 | std::vector<LocalStorageUsageInfo> infos; |
| 97 | context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
| 98 | EXPECT_TRUE(infos.empty()); |
| 99 | context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); |
| 100 | EXPECT_TRUE(infos.empty()); |
| 101 | |
| 102 | // Put some data into local storage and shutdown the context |
| 103 | // to ensure data is written to disk. |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 104 | base::NullableString16 old_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 105 | EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 106 | OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 107 | context_->Shutdown(); |
| 108 | context_ = NULL; |
| 109 | base::MessageLoop::current()->RunUntilIdle(); |
| 110 | |
| 111 | // Create a new context that points to the same directory, see that |
| 112 | // it knows about the origin that we stored data for. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 113 | context_ = new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(), |
| 114 | NULL, NULL); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 115 | context_->GetLocalStorageUsage(&infos, kDontIncludeFileInfo); |
| 116 | EXPECT_EQ(1u, infos.size()); |
| 117 | EXPECT_EQ(kOrigin, infos[0].origin); |
| 118 | EXPECT_EQ(0u, infos[0].data_size); |
| 119 | EXPECT_EQ(base::Time(), infos[0].last_modified); |
| 120 | infos.clear(); |
| 121 | context_->GetLocalStorageUsage(&infos, kDoIncludeFileInfo); |
| 122 | EXPECT_EQ(1u, infos.size()); |
| 123 | EXPECT_EQ(kOrigin, infos[0].origin); |
| 124 | EXPECT_NE(0u, infos[0].data_size); |
| 125 | EXPECT_NE(base::Time(), infos[0].last_modified); |
| 126 | } |
| 127 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 128 | TEST_F(DOMStorageContextImplTest, SessionOnly) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 129 | const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
| 130 | storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
| 131 | |
| 132 | // Store data for a normal and a session-only origin and then |
| 133 | // invoke Shutdown() which should delete data for session-only |
| 134 | // origins. |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 135 | base::NullableString16 old_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 136 | EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 137 | OpenStorageArea(kOrigin)->SetItem(kKey, kValue, &old_value)); |
| 138 | EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 139 | OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
| 140 | context_->Shutdown(); |
| 141 | context_ = NULL; |
| 142 | base::MessageLoop::current()->RunUntilIdle(); |
| 143 | |
| 144 | // Verify that the session-only origin data is gone. |
| 145 | VerifySingleOriginRemains(kOrigin); |
| 146 | } |
| 147 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 148 | TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 149 | const GURL kSessionOnlyOrigin("http://www.sessiononly.com/"); |
| 150 | storage_policy_->AddSessionOnly(kSessionOnlyOrigin); |
| 151 | |
| 152 | // Store data for a session-only origin, setup to save session data, then |
| 153 | // shutdown. |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 154 | base::NullableString16 old_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 155 | EXPECT_TRUE(context_->GetStorageNamespace(kLocalStorageNamespaceId)-> |
| 156 | OpenStorageArea(kSessionOnlyOrigin)->SetItem(kKey, kValue, &old_value)); |
| 157 | context_->SetForceKeepSessionState(); // Should override clear behavior. |
| 158 | context_->Shutdown(); |
| 159 | context_ = NULL; |
| 160 | base::MessageLoop::current()->RunUntilIdle(); |
| 161 | |
| 162 | VerifySingleOriginRemains(kSessionOnlyOrigin); |
| 163 | } |
| 164 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 165 | TEST_F(DOMStorageContextImplTest, PersistentIds) { |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 166 | const int kFirstSessionStorageNamespaceId = 1; |
| 167 | const std::string kPersistentId = "persistent"; |
| 168 | context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId, |
| 169 | kPersistentId); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 170 | DOMStorageNamespace* dom_namespace = |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 171 | context_->GetStorageNamespace(kFirstSessionStorageNamespaceId); |
| 172 | ASSERT_TRUE(dom_namespace); |
| 173 | EXPECT_EQ(kPersistentId, dom_namespace->persistent_namespace_id()); |
| 174 | // Verify that the areas inherit the persistent ID. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 175 | DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 176 | EXPECT_EQ(kPersistentId, area->persistent_namespace_id_); |
| 177 | |
| 178 | // Verify that the persistent IDs are handled correctly when cloning. |
| 179 | const int kClonedSessionStorageNamespaceId = 2; |
| 180 | const std::string kClonedPersistentId = "cloned"; |
| 181 | context_->CloneSessionNamespace(kFirstSessionStorageNamespaceId, |
| 182 | kClonedSessionStorageNamespaceId, |
| 183 | kClonedPersistentId); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 184 | DOMStorageNamespace* cloned_dom_namespace = |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 185 | context_->GetStorageNamespace(kClonedSessionStorageNamespaceId); |
| 186 | ASSERT_TRUE(dom_namespace); |
| 187 | EXPECT_EQ(kClonedPersistentId, |
| 188 | cloned_dom_namespace->persistent_namespace_id()); |
| 189 | // Verify that the areas inherit the persistent ID. |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 190 | DOMStorageArea* cloned_area = cloned_dom_namespace->OpenStorageArea(kOrigin); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 191 | EXPECT_EQ(kClonedPersistentId, cloned_area->persistent_namespace_id_); |
| 192 | } |
| 193 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 194 | TEST_F(DOMStorageContextImplTest, DeleteSessionStorage) { |
| 195 | // Create a DOMStorageContextImpl which will save sessionStorage on disk. |
| 196 | context_ = new DOMStorageContextImpl(temp_dir_.path(), |
| 197 | temp_dir_.path(), |
| 198 | storage_policy_.get(), |
| 199 | task_runner_.get()); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 200 | context_->SetSaveSessionStorageOnDisk(); |
| 201 | ASSERT_EQ(temp_dir_.path(), context_->sessionstorage_directory()); |
| 202 | |
| 203 | // Write data. |
| 204 | const int kSessionStorageNamespaceId = 1; |
| 205 | const std::string kPersistentId = "persistent"; |
| 206 | context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
| 207 | kPersistentId); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 208 | DOMStorageNamespace* dom_namespace = |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 209 | context_->GetStorageNamespace(kSessionStorageNamespaceId); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 210 | DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin); |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 211 | const base::string16 kKey(ASCIIToUTF16("foo")); |
| 212 | const base::string16 kValue(ASCIIToUTF16("bar")); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 213 | base::NullableString16 old_nullable_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 214 | area->SetItem(kKey, kValue, &old_nullable_value); |
| 215 | dom_namespace->CloseStorageArea(area); |
| 216 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 217 | // Destroy and recreate the DOMStorageContextImpl. |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 218 | context_->Shutdown(); |
| 219 | context_ = NULL; |
| 220 | base::MessageLoop::current()->RunUntilIdle(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 221 | context_ = new DOMStorageContextImpl( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 222 | temp_dir_.path(), temp_dir_.path(), |
| 223 | storage_policy_.get(), task_runner_.get()); |
| 224 | context_->SetSaveSessionStorageOnDisk(); |
| 225 | |
| 226 | // Read the data back. |
| 227 | context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
| 228 | kPersistentId); |
| 229 | dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
| 230 | area = dom_namespace->OpenStorageArea(kOrigin); |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 231 | base::NullableString16 read_value; |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 232 | read_value = area->GetItem(kKey); |
| 233 | EXPECT_EQ(kValue, read_value.string()); |
| 234 | dom_namespace->CloseStorageArea(area); |
| 235 | |
| 236 | SessionStorageUsageInfo info; |
| 237 | info.origin = kOrigin; |
| 238 | info.persistent_namespace_id = kPersistentId; |
| 239 | context_->DeleteSessionStorage(info); |
| 240 | |
| 241 | // Destroy and recreate again. |
| 242 | context_->Shutdown(); |
| 243 | context_ = NULL; |
| 244 | base::MessageLoop::current()->RunUntilIdle(); |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 245 | context_ = new DOMStorageContextImpl( |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 246 | temp_dir_.path(), temp_dir_.path(), |
| 247 | storage_policy_.get(), task_runner_.get()); |
| 248 | context_->SetSaveSessionStorageOnDisk(); |
| 249 | |
| 250 | // Now there should be no data. |
| 251 | context_->CreateSessionNamespace(kSessionStorageNamespaceId, |
| 252 | kPersistentId); |
| 253 | dom_namespace = context_->GetStorageNamespace(kSessionStorageNamespaceId); |
| 254 | area = dom_namespace->OpenStorageArea(kOrigin); |
| 255 | read_value = area->GetItem(kKey); |
| 256 | EXPECT_TRUE(read_value.is_null()); |
| 257 | dom_namespace->CloseStorageArea(area); |
| 258 | context_->Shutdown(); |
| 259 | context_ = NULL; |
| 260 | base::MessageLoop::current()->RunUntilIdle(); |
| 261 | } |
| 262 | |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 263 | } // namespace content |