blob: 27aaefb24d3988352e961ca17cd75d9412bab676 [file] [log] [blame]
Ben Murdochbb1529c2013-08-08 10:24:53 +01001// Copyright 2013 The Chromium Authors. All rights reserved.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01002// 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 Murdochca12bfa2013-07-23 11:17:05 +01008#include "base/message_loop/message_loop.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01009#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 Murdocheb525c52013-07-10 11:40:50 +010012#include "base/time/time.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010013#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)868fa2f2013-06-11 10:57:03 +010019#include "testing/gtest/include/gtest/gtest.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010020#include "webkit/browser/quota/mock_special_storage_policy.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010021
Ben Murdochbb1529c2013-08-08 10:24:53 +010022namespace content {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010023
Ben Murdochbb1529c2013-08-08 10:24:53 +010024class DOMStorageContextImplTest : public testing::Test {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010025 public:
Ben Murdochbb1529c2013-08-08 10:24:53 +010026 DOMStorageContextImplTest()
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010027 : 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)7d4cd472013-06-19 11:58:07 +010043 task_runner_ =
Ben Murdochbb1529c2013-08-08 10:24:53 +010044 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)868fa2f2013-06-11 10:57:03 +010049 }
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 Murdochbb1529c2013-08-08 10:24:53 +010057 scoped_refptr<DOMStorageContextImpl> context =
58 new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(),
59 NULL, NULL);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010060 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 Murdochbb1529c2013-08-08 10:24:53 +010070 scoped_refptr<MockDOMStorageTaskRunner> task_runner_;
71 scoped_refptr<DOMStorageContextImpl> context_;
72 DISALLOW_COPY_AND_ASSIGN(DOMStorageContextImplTest);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010073};
74
Ben Murdochbb1529c2013-08-08 10:24:53 +010075TEST_F(DOMStorageContextImplTest, Basics) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010076 // 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 Murdochbb1529c2013-08-08 10:24:53 +010094TEST_F(DOMStorageContextImplTest, UsageInfo) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010095 // 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)7d4cd472013-06-19 11:58:07 +0100104 base::NullableString16 old_value;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100105 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 Murdochbb1529c2013-08-08 10:24:53 +0100113 context_ = new DOMStorageContextImpl(temp_dir_.path(), base::FilePath(),
114 NULL, NULL);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100115 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 Murdochbb1529c2013-08-08 10:24:53 +0100128TEST_F(DOMStorageContextImplTest, SessionOnly) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100129 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)7d4cd472013-06-19 11:58:07 +0100135 base::NullableString16 old_value;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100136 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 Murdochbb1529c2013-08-08 10:24:53 +0100148TEST_F(DOMStorageContextImplTest, SetForceKeepSessionState) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100149 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)7d4cd472013-06-19 11:58:07 +0100154 base::NullableString16 old_value;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100155 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 Murdochbb1529c2013-08-08 10:24:53 +0100165TEST_F(DOMStorageContextImplTest, PersistentIds) {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100166 const int kFirstSessionStorageNamespaceId = 1;
167 const std::string kPersistentId = "persistent";
168 context_->CreateSessionNamespace(kFirstSessionStorageNamespaceId,
169 kPersistentId);
Ben Murdochbb1529c2013-08-08 10:24:53 +0100170 DOMStorageNamespace* dom_namespace =
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100171 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 Murdochbb1529c2013-08-08 10:24:53 +0100175 DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100176 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 Murdochbb1529c2013-08-08 10:24:53 +0100184 DOMStorageNamespace* cloned_dom_namespace =
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100185 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 Murdochbb1529c2013-08-08 10:24:53 +0100190 DOMStorageArea* cloned_area = cloned_dom_namespace->OpenStorageArea(kOrigin);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100191 EXPECT_EQ(kClonedPersistentId, cloned_area->persistent_namespace_id_);
192}
193
Ben Murdochbb1529c2013-08-08 10:24:53 +0100194TEST_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)868fa2f2013-06-11 10:57:03 +0100200 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 Murdochbb1529c2013-08-08 10:24:53 +0100208 DOMStorageNamespace* dom_namespace =
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100209 context_->GetStorageNamespace(kSessionStorageNamespaceId);
Ben Murdochbb1529c2013-08-08 10:24:53 +0100210 DOMStorageArea* area = dom_namespace->OpenStorageArea(kOrigin);
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100211 const base::string16 kKey(ASCIIToUTF16("foo"));
212 const base::string16 kValue(ASCIIToUTF16("bar"));
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100213 base::NullableString16 old_nullable_value;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100214 area->SetItem(kKey, kValue, &old_nullable_value);
215 dom_namespace->CloseStorageArea(area);
216
Ben Murdochbb1529c2013-08-08 10:24:53 +0100217 // Destroy and recreate the DOMStorageContextImpl.
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100218 context_->Shutdown();
219 context_ = NULL;
220 base::MessageLoop::current()->RunUntilIdle();
Ben Murdochbb1529c2013-08-08 10:24:53 +0100221 context_ = new DOMStorageContextImpl(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100222 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)7d4cd472013-06-19 11:58:07 +0100231 base::NullableString16 read_value;
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100232 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 Murdochbb1529c2013-08-08 10:24:53 +0100245 context_ = new DOMStorageContextImpl(
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100246 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 Murdochbb1529c2013-08-08 10:24:53 +0100263} // namespace content