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

Side by Side Diff: content/browser/net/quota_policy_cookie_store_unittest.cc

Issue 1016643004: Moves SQLitePersistentCookieStore to net/extras/sqlite. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cookies
Patch Set: Use WeakPtr. Created 5 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
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/files/file_util.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "base/test/sequenced_worker_pool_owner.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/threading/sequenced_worker_pool.h"
16 #include "base/time/time.h"
17 #include "content/browser/net/quota_policy_cookie_store.h"
18 #include "content/public/test/mock_special_storage_policy.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "net/base/test_data_directory.h"
21 #include "net/cookies/cookie_util.h"
22 #include "net/ssl/ssl_client_cert_type.h"
23 #include "net/test/cert_test_util.h"
24 #include "sql/statement.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace {
28 const base::FilePath::CharType kTestCookiesFilename[] =
29 FILE_PATH_LITERAL("Cookies");
30 }
31
32 namespace content {
33 namespace {
34
35 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector;
36
37 class QuotaPolicyCookieStoreTest : public testing::Test {
38 public:
39 QuotaPolicyCookieStoreTest()
40 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")) {
41 }
42
43 void OnLoaded(const CanonicalCookieVector& cookies) {
44 cookies_ = cookies;
45 base::MessageLoop::current()->Quit();
46 }
47
48 void Load(CanonicalCookieVector* cookies) {
49 store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded,
50 base::Unretained(this)));
51 base::MessageLoop::current()->Run();
Ryan Sleevi 2015/05/14 01:19:35 note for future: Use RunLoops for this
rohitrao (ping after 24h) 2015/05/14 15:48:34 Acknowledged.
52 *cookies = cookies_;
53 }
54
55 void FailIfOnLoadedCalled(const CanonicalCookieVector& cookies) {
56 FAIL();
57 }
58
59 protected:
60 scoped_refptr<base::SequencedTaskRunner> background_task_runner() {
61 if (!background_task_runner_.get()) {
62 background_task_runner_ = pool_owner_->pool()->GetSequencedTaskRunner(
63 pool_owner_->pool()->GetNamedSequenceToken("background"));
64 }
65 return background_task_runner_;
66 }
67
68 scoped_refptr<base::SequencedTaskRunner> client_task_runner() {
69 if (!client_task_runner_.get()) {
70 client_task_runner_ = base::ThreadTaskRunnerHandle::Get();
71 }
72 return client_task_runner_;
73 }
74
75 void CreateAndLoad(storage::SpecialStoragePolicy* storage_policy,
76 CanonicalCookieVector* cookies) {
77 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store(
78 new net::SQLitePersistentCookieStore(
79 temp_dir_.path().Append(kTestCookiesFilename),
80 client_task_runner(),
81 background_task_runner(),
82 true, nullptr));
83 store_ = new QuotaPolicyCookieStore(sqlite_store.get(), storage_policy);
84 Load(cookies);
85 }
86
87 // Adds a persistent cookie to store_.
88 void AddCookie(const std::string& name,
89 const std::string& value,
90 const std::string& domain,
91 const std::string& path,
92 const base::Time& creation) {
93 store_->AddCookie(
94 net::CanonicalCookie(
95 GURL(), name, value, domain, path, creation, creation, creation,
96 false, false, false, net::COOKIE_PRIORITY_DEFAULT));
97 }
98
99 void DestroyStore() {
100 store_ = nullptr;
101 // Ensure that |store_|'s destructor has run by shutting down the pool and
102 // then forcing the pool to be destructed. This will ensure that all the
103 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before
104 // yielding control.
105 background_task_runner_ = nullptr;
106 client_task_runner_ = nullptr;
107 pool_owner_->pool()->FlushForTesting();
108 pool_owner_->pool()->Shutdown();
109 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool"));
110 }
111
112 void SetUp() override {
113 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
114 }
115
116 void TearDown() override {
117 DestroyStore();
118 pool_owner_->pool()->Shutdown();
119 }
120
121 TestBrowserThreadBundle bundle_;
122 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_;
123 scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
124 scoped_refptr<base::SequencedTaskRunner> client_task_runner_;
125 base::ScopedTempDir temp_dir_;
126 scoped_refptr<QuotaPolicyCookieStore> store_;
127 CanonicalCookieVector cookies_;
128 };
129
130 // Test if data is stored as expected in the QuotaPolicy database.
131 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) {
132 CanonicalCookieVector cookies;
133 CreateAndLoad(nullptr, &cookies);
134 ASSERT_EQ(0U, cookies.size());
135
136 base::Time t = base::Time::Now();
137 AddCookie("A", "B", "foo.com", "/", t);
138 t += base::TimeDelta::FromInternalValue(10);
139 AddCookie("A", "B", "persistent.com", "/", t);
140
141 // Replace the store effectively destroying the current one and forcing it
142 // to write its data to disk. Then we can see if after loading it again it
143 // is still there.
144 DestroyStore();
145
146 // Reload and test for persistence.
147 STLDeleteElements(&cookies);
148 CreateAndLoad(nullptr, &cookies);
149 EXPECT_EQ(2U, cookies.size());
150 bool found_foo_cookie = false;
151 bool found_persistent_cookie = false;
152 for (const auto& cookie : cookies) {
153 if (cookie->Domain() == "foo.com")
154 found_foo_cookie = true;
155 else if (cookie->Domain() == "persistent.com")
156 found_persistent_cookie = true;
157 }
158 EXPECT_TRUE(found_foo_cookie);
159 EXPECT_TRUE(found_persistent_cookie);
Ryan Sleevi 2015/05/14 01:19:35 For this case, I doubt it's fully justified, but g
rohitrao (ping after 24h) 2015/05/14 15:48:34 Acknowledged.
160
161 // Now delete the cookies and check persistence again.
162 store_->DeleteCookie(*cookies[0]);
163 store_->DeleteCookie(*cookies[1]);
164 DestroyStore();
165
166 // Reload and check if the cookies have been removed.
167 STLDeleteElements(&cookies);
168 CreateAndLoad(nullptr, &cookies);
169 EXPECT_EQ(0U, cookies.size());
170 STLDeleteElements(&cookies);
171 }
172
173 // Test if data is stored as expected in the QuotaPolicy database.
174 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) {
175 CanonicalCookieVector cookies;
176 CreateAndLoad(nullptr, &cookies);
177 ASSERT_EQ(0U, cookies.size());
178
179 base::Time t = base::Time::Now();
180 AddCookie("A", "B", "foo.com", "/", t);
181 t += base::TimeDelta::FromInternalValue(10);
182 AddCookie("A", "B", "persistent.com", "/", t);
183 t += base::TimeDelta::FromInternalValue(10);
184 AddCookie("A", "B", "nonpersistent.com", "/", t);
185
186 // Replace the store effectively destroying the current one and forcing it
187 // to write its data to disk. Then we can see if after loading it again it
188 // is still there.
189 DestroyStore();
190 // Specify storage policy that makes "nonpersistent.com" session only.
191 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
192 new content::MockSpecialStoragePolicy();
193 storage_policy->AddSessionOnly(
194 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
195
196 // Reload and test for persistence
197 STLDeleteElements(&cookies);
198 CreateAndLoad(storage_policy.get(), &cookies);
199 EXPECT_EQ(3U, cookies.size());
200
201 t += base::TimeDelta::FromInternalValue(10);
202 AddCookie("A", "B", "nonpersistent.com", "/second", t);
203
204 // Now close the store, and "nonpersistent.com" should be deleted according to
205 // policy.
206 DestroyStore();
207 STLDeleteElements(&cookies);
208 CreateAndLoad(nullptr, &cookies);
209
210 EXPECT_EQ(2U, cookies.size());
211 for (const auto& cookie : cookies) {
212 EXPECT_NE("nonpersistent.com", cookie->Domain());
213 }
214 STLDeleteElements(&cookies);
215 }
216
217 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) {
218 CanonicalCookieVector cookies;
219 CreateAndLoad(nullptr, &cookies);
220 ASSERT_EQ(0U, cookies.size());
221
222 base::Time t = base::Time::Now();
223 AddCookie("A", "B", "foo.com", "/", t);
224
225 // Recreate |store_| with a storage policy that makes "nonpersistent.com"
226 // session only, but then instruct the store to forcibly keep all cookies.
227 DestroyStore();
228 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
229 new content::MockSpecialStoragePolicy();
230 storage_policy->AddSessionOnly(
231 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
232
233 // Reload and test for persistence
234 STLDeleteElements(&cookies);
235 CreateAndLoad(storage_policy.get(), &cookies);
236 EXPECT_EQ(1U, cookies.size());
237
238 t += base::TimeDelta::FromInternalValue(10);
239 AddCookie("A", "B", "persistent.com", "/", t);
240 t += base::TimeDelta::FromInternalValue(10);
241 AddCookie("A", "B", "nonpersistent.com", "/", t);
242
243 // Now close the store, but the "nonpersistent.com" cookie should not be
244 // deleted.
245 store_->SetForceKeepSessionState();
246 DestroyStore();
247 STLDeleteElements(&cookies);
248 CreateAndLoad(nullptr, &cookies);
249
250 EXPECT_EQ(3U, cookies.size());
251 STLDeleteElements(&cookies);
252 }
253
254 // Tests that the store does not keep a strong reference to itself when creating
255 // the callback for Load().
256 TEST_F(QuotaPolicyCookieStoreTest, DBGoesOutOfScope) {
257 // Used to determine whether |quota_store| is destroyed when it goes out of
258 // scope.
259 base::WeakPtr<QuotaPolicyCookieStore> weak_store;
260 {
261 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store(
262 new net::SQLitePersistentCookieStore(
263 temp_dir_.path().Append(kTestCookiesFilename),
264 client_task_runner(),
265 background_task_runner(),
266 true, nullptr));
267 scoped_refptr<QuotaPolicyCookieStore> quota_store(
268 new QuotaPolicyCookieStore(sqlite_store.get(), nullptr));
269 quota_store->Load(
270 base::Bind(&QuotaPolicyCookieStoreTest::FailIfOnLoadedCalled,
271 base::Unretained(this)));
272
273 weak_store = quota_store->AsWeakPtr();
274 // Allow |quota_store| to go out of scope and be destroyed here.
275 }
276
277 // If |weak_store| is NULL, then |quota_store| was destroyed when it went out
278 // of scope and Load() could not have kept a strong reference to the store.
279 EXPECT_EQ(NULL, weak_store.get());
280 }
281
282 } // namespace
283 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698