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

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: Maybe fix bots. 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/run_loop.h"
11 #include "base/stl_util.h"
12 #include "base/synchronization/waitable_event.h"
13 #include "base/test/sequenced_worker_pool_owner.h"
14 #include "base/threading/sequenced_worker_pool.h"
15 #include "base/time/time.h"
16 #include "content/browser/net/quota_policy_cookie_store.h"
17 #include "content/public/test/mock_special_storage_policy.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "net/base/test_data_directory.h"
20 #include "net/cookies/cookie_util.h"
21 #include "net/ssl/ssl_client_cert_type.h"
22 #include "net/test/cert_test_util.h"
23 #include "sql/statement.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25
26 namespace {
27 const base::FilePath::CharType kTestCookiesFilename[] =
28 FILE_PATH_LITERAL("Cookies");
29 }
30
31 namespace content {
32 namespace {
33
34 typedef std::vector<net::CanonicalCookie*> CanonicalCookieVector;
35
36 class QuotaPolicyCookieStoreTest : public testing::Test {
37 public:
38 QuotaPolicyCookieStoreTest()
39 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")),
40 loaded_event_(false, false) {
41 }
42
43 void OnLoaded(const CanonicalCookieVector& cookies) {
44 cookies_ = cookies;
45 loaded_event_.Signal();
46 }
47
48 void Load(CanonicalCookieVector* cookies) {
49 EXPECT_FALSE(loaded_event_.IsSignaled());
50 store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded,
51 base::Unretained(this)));
52 loaded_event_.Wait();
53 *cookies = cookies_;
54 }
55
56 protected:
57 scoped_refptr<base::SequencedTaskRunner> background_task_runner() {
58 return pool_owner_->pool()->GetSequencedTaskRunner(
59 pool_owner_->pool()->GetNamedSequenceToken("background"));
Ryan Sleevi 2015/05/12 00:48:34 There's no reason not to set these up during the c
rohitrao (ping after 24h) 2015/05/13 17:05:08 Done. It's a little complicated because we destro
Ryan Sleevi 2015/05/14 01:19:34 Yeah, but why not just do that in DestroyStore? O
rohitrao (ping after 24h) 2015/05/14 15:48:33 Ended up going back to the original behavior, most
60 }
61
62 scoped_refptr<base::SequencedTaskRunner> client_task_runner() {
63 return pool_owner_->pool()->GetSequencedTaskRunner(
64 pool_owner_->pool()->GetNamedSequenceToken("client"));
65 }
66
67 void CreateAndLoad(storage::SpecialStoragePolicy* storage_policy,
68 CanonicalCookieVector* cookies) {
69 scoped_refptr<net::SQLitePersistentCookieStore> sqlite_store(
70 new net::SQLitePersistentCookieStore(
71 temp_dir_.path().Append(kTestCookiesFilename),
72 client_task_runner(),
73 background_task_runner(),
74 true, nullptr));
75 store_ = new QuotaPolicyCookieStore(
mef 2015/05/12 21:23:27 nit: fits into one line.
rohitrao (ping after 24h) 2015/05/13 17:05:08 Fixed.
76 sqlite_store.get(), storage_policy);
77 Load(cookies);
78 }
79
80 // Adds a persistent cookie to store_.
81 void AddCookie(const std::string& name,
82 const std::string& value,
83 const std::string& domain,
84 const std::string& path,
85 const base::Time& creation) {
86 store_->AddCookie(
87 net::CanonicalCookie(
88 GURL(), name, value, domain, path, creation, creation, creation,
89 false, false, false, net::COOKIE_PRIORITY_DEFAULT));
90 }
91
92 void DestroyStore() {
93 store_ = nullptr;
94 // Ensure that |store_|'s destructor has run by shutting down the pool and
95 // then forcing the pool to be destructed. This will ensure that all the
96 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before
97 // yielding control.
98 pool_owner_->pool()->FlushForTesting();
99 pool_owner_->pool()->Shutdown();
100 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool"));
101 }
102
103 void SetUp() override {
104 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
105 }
106
107 void TearDown() override {
108 DestroyStore();
109 pool_owner_->pool()->Shutdown();
110 }
Ryan Sleevi 2015/05/12 00:48:34 No idea why this isn't simply moved to the dtor ra
rohitrao (ping after 24h) 2015/05/13 17:05:08 Acknowledged.
111
112 TestBrowserThreadBundle bundle_;
113 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_;
114 base::WaitableEvent loaded_event_;
115 base::ScopedTempDir temp_dir_;
116 scoped_refptr<QuotaPolicyCookieStore> store_;
117 CanonicalCookieVector cookies_;
118 };
119
120 // Test if data is stored as expected in the QuotaPolicy database.
121 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) {
122 CanonicalCookieVector cookies;
123 CreateAndLoad(nullptr, &cookies);
124 ASSERT_EQ(0U, cookies.size());
125
126 base::Time t = base::Time::Now();
127 AddCookie("A", "B", "foo.com", "/", t);
128 t += base::TimeDelta::FromInternalValue(10);
129 AddCookie("A", "B", "persistent.com", "/", t);
130
131 // Replace the store effectively destroying the current one and forcing it
132 // to write its data to disk. Then we can see if after loading it again it
133 // is still there.
134 DestroyStore();
135
136 // Reload and test for persistence
mef 2015/05/12 21:23:27 nit: add .
rohitrao (ping after 24h) 2015/05/13 17:05:08 Done.
137 STLDeleteElements(&cookies);
138 CreateAndLoad(nullptr, &cookies);
139 EXPECT_EQ(2U, cookies.size());
140 EXPECT_EQ("foo.com", cookies[0]->Domain());
mef 2015/05/12 21:23:27 are they guaranteed to be in this order?
rohitrao (ping after 24h) 2015/05/13 17:05:08 Nope, fixed.
141 EXPECT_EQ("persistent.com", cookies[1]->Domain());
142
143 // Now delete the cookies and check persistence again.
144 store_->DeleteCookie(*cookies[0]);
145 store_->DeleteCookie(*cookies[1]);
146 DestroyStore();
147
148 // Reload and check if the cookies have been removed.
149 STLDeleteElements(&cookies);
150 CreateAndLoad(nullptr, &cookies);
151 EXPECT_EQ(0U, cookies.size());
152 STLDeleteElements(&cookies);
153 }
154
155 // Test if data is stored as expected in the QuotaPolicy database.
156 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) {
157 CanonicalCookieVector cookies;
158 CreateAndLoad(nullptr, &cookies);
159 ASSERT_EQ(0U, cookies.size());
160
161 base::Time t = base::Time::Now();
162 AddCookie("A", "B", "foo.com", "/", t);
163 t += base::TimeDelta::FromInternalValue(10);
164 AddCookie("A", "B", "persistent.com", "/", t);
165 t += base::TimeDelta::FromInternalValue(10);
166 AddCookie("A", "B", "nonpersistent.com", "/", t);
167
168 // Replace the store effectively destroying the current one and forcing it
169 // to write its data to disk. Then we can see if after loading it again it
170 // is still there.
171 DestroyStore();
172 // Specify storage policy that makes "nonpersistent.com" session only.
173 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
174 new content::MockSpecialStoragePolicy();
175 storage_policy->AddSessionOnly(
176 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
177
178 // Reload and test for persistence
179 STLDeleteElements(&cookies);
180 CreateAndLoad(storage_policy.get(), &cookies);
181 EXPECT_EQ(3U, cookies.size());
182
183 t += base::TimeDelta::FromInternalValue(10);
184 AddCookie("A", "B", "nonpersistent.com", "/second", t);
185
186 // Now close the store, and "nonpersistent.com" should be deleted according to
187 // policy.
188 DestroyStore();
189 STLDeleteElements(&cookies);
190 CreateAndLoad(nullptr, &cookies);
191
192 EXPECT_EQ(2U, cookies.size());
193 for (const auto& cookie : cookies) {
194 EXPECT_NE("nonpersistent.com", cookie->Domain());
195 }
196 STLDeleteElements(&cookies);
197 }
198
199 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) {
200 CanonicalCookieVector cookies;
201 CreateAndLoad(nullptr, &cookies);
202 ASSERT_EQ(0U, cookies.size());
203
204 base::Time t = base::Time::Now();
205 AddCookie("A", "B", "foo.com", "/", t);
206
207 // Recreate |store_| with a storage policy that makes "nonpersistent.com"
208 // session only, but then instruct the store to forcibly keep all cookies.
209 DestroyStore();
210 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
211 new content::MockSpecialStoragePolicy();
212 storage_policy->AddSessionOnly(
213 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
214
215 // Reload and test for persistence
216 STLDeleteElements(&cookies);
217 CreateAndLoad(storage_policy.get(), &cookies);
218 EXPECT_EQ(1U, cookies.size());
219
220 t += base::TimeDelta::FromInternalValue(10);
221 AddCookie("A", "B", "persistent.com", "/", t);
222 t += base::TimeDelta::FromInternalValue(10);
223 AddCookie("A", "B", "nonpersistent.com", "/", t);
224
225 // Now close the store, but the "nonpersistent.com" cookie should not be
226 // deleted.
227 store_->SetForceKeepSessionState();
228 DestroyStore();
229 STLDeleteElements(&cookies);
230 CreateAndLoad(nullptr, &cookies);
231
232 EXPECT_EQ(3U, cookies.size());
233 STLDeleteElements(&cookies);
234 }
235
236 TEST_F(QuotaPolicyCookieStoreTest, DBGoesOutOfScope) {
237 CanonicalCookieVector cookies;
238 CreateAndLoad(nullptr, &cookies);
239 ASSERT_EQ(0U, cookies.size());
240
241 store_ = nullptr;
242 STLDeleteElements(&cookies);
243 }
244
245 } // namespace
246 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698