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

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: Review. 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"));
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(sqlite_store.get(), storage_policy);
76 Load(cookies);
77 }
78
79 // Adds a persistent cookie to store_.
80 void AddCookie(const std::string& name,
81 const std::string& value,
82 const std::string& domain,
83 const std::string& path,
84 const base::Time& creation) {
85 store_->AddCookie(
86 net::CanonicalCookie(
87 GURL(), name, value, domain, path, creation, creation, creation,
88 false, false, false, net::COOKIE_PRIORITY_DEFAULT));
89 }
90
91 void DestroyStore() {
92 store_ = nullptr;
93 // Ensure that |store_|'s destructor has run by shutting down the pool and
94 // then forcing the pool to be destructed. This will ensure that all the
95 // tasks that block pool shutdown (e.g. |store_|'s cleanup) have run before
96 // yielding control.
97 pool_owner_->pool()->FlushForTesting();
98 pool_owner_->pool()->Shutdown();
99 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool"));
100 }
101
102 void SetUp() override {
103 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
104 }
105
106 void TearDown() override {
107 DestroyStore();
108 pool_owner_->pool()->Shutdown();
109 }
110
111 TestBrowserThreadBundle bundle_;
112 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_;
113 base::WaitableEvent loaded_event_;
114 base::ScopedTempDir temp_dir_;
115 scoped_refptr<QuotaPolicyCookieStore> store_;
116 CanonicalCookieVector cookies_;
117 };
118
119 // Test if data is stored as expected in the QuotaPolicy database.
120 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) {
121 CanonicalCookieVector cookies;
122 CreateAndLoad(nullptr, &cookies);
123 ASSERT_EQ(0U, cookies.size());
124
125 base::Time t = base::Time::Now();
126 AddCookie("A", "B", "foo.com", "/", t);
127 t += base::TimeDelta::FromInternalValue(10);
128 AddCookie("A", "B", "persistent.com", "/", t);
129
130 // Replace the store effectively destroying the current one and forcing it
131 // to write its data to disk. Then we can see if after loading it again it
132 // is still there.
133 DestroyStore();
134
135 // Reload and test for persistence.
136 STLDeleteElements(&cookies);
137 CreateAndLoad(nullptr, &cookies);
138 EXPECT_EQ(2U, cookies.size());
139 bool found_foo_cookie = false;
140 bool found_persistent_cookie = false;
141 for (const auto& cookie : cookies) {
142 if (cookie->Domain() == "foo.com")
143 found_foo_cookie = true;
144 else if (cookie->Domain() == "persistent.com")
145 found_persistent_cookie = true;
146 }
147 EXPECT_TRUE(found_foo_cookie);
148 EXPECT_TRUE(found_persistent_cookie);
149
150 // Now delete the cookies and check persistence again.
151 store_->DeleteCookie(*cookies[0]);
152 store_->DeleteCookie(*cookies[1]);
153 DestroyStore();
154
155 // Reload and check if the cookies have been removed.
156 STLDeleteElements(&cookies);
157 CreateAndLoad(nullptr, &cookies);
158 EXPECT_EQ(0U, cookies.size());
159 STLDeleteElements(&cookies);
160 }
161
162 // Test if data is stored as expected in the QuotaPolicy database.
163 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) {
164 CanonicalCookieVector cookies;
165 CreateAndLoad(nullptr, &cookies);
166 ASSERT_EQ(0U, cookies.size());
167
168 base::Time t = base::Time::Now();
169 AddCookie("A", "B", "foo.com", "/", t);
170 t += base::TimeDelta::FromInternalValue(10);
171 AddCookie("A", "B", "persistent.com", "/", t);
172 t += base::TimeDelta::FromInternalValue(10);
173 AddCookie("A", "B", "nonpersistent.com", "/", t);
174
175 // Replace the store effectively destroying the current one and forcing it
176 // to write its data to disk. Then we can see if after loading it again it
177 // is still there.
178 DestroyStore();
179 // Specify storage policy that makes "nonpersistent.com" session only.
180 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
181 new content::MockSpecialStoragePolicy();
182 storage_policy->AddSessionOnly(
183 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
184
185 // Reload and test for persistence
186 STLDeleteElements(&cookies);
187 CreateAndLoad(storage_policy.get(), &cookies);
188 EXPECT_EQ(3U, cookies.size());
189
190 t += base::TimeDelta::FromInternalValue(10);
191 AddCookie("A", "B", "nonpersistent.com", "/second", t);
192
193 // Now close the store, and "nonpersistent.com" should be deleted according to
194 // policy.
195 DestroyStore();
196 STLDeleteElements(&cookies);
197 CreateAndLoad(nullptr, &cookies);
198
199 EXPECT_EQ(2U, cookies.size());
200 for (const auto& cookie : cookies) {
201 EXPECT_NE("nonpersistent.com", cookie->Domain());
202 }
203 STLDeleteElements(&cookies);
204 }
205
206 TEST_F(QuotaPolicyCookieStoreTest, ForceKeepSessionState) {
207 CanonicalCookieVector cookies;
208 CreateAndLoad(nullptr, &cookies);
209 ASSERT_EQ(0U, cookies.size());
210
211 base::Time t = base::Time::Now();
212 AddCookie("A", "B", "foo.com", "/", t);
213
214 // Recreate |store_| with a storage policy that makes "nonpersistent.com"
215 // session only, but then instruct the store to forcibly keep all cookies.
216 DestroyStore();
217 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy =
218 new content::MockSpecialStoragePolicy();
219 storage_policy->AddSessionOnly(
220 net::cookie_util::CookieOriginToURL("nonpersistent.com", false));
221
222 // Reload and test for persistence
223 STLDeleteElements(&cookies);
224 CreateAndLoad(storage_policy.get(), &cookies);
225 EXPECT_EQ(1U, cookies.size());
226
227 t += base::TimeDelta::FromInternalValue(10);
228 AddCookie("A", "B", "persistent.com", "/", t);
229 t += base::TimeDelta::FromInternalValue(10);
230 AddCookie("A", "B", "nonpersistent.com", "/", t);
231
232 // Now close the store, but the "nonpersistent.com" cookie should not be
233 // deleted.
234 store_->SetForceKeepSessionState();
235 DestroyStore();
236 STLDeleteElements(&cookies);
237 CreateAndLoad(nullptr, &cookies);
238
239 EXPECT_EQ(3U, cookies.size());
240 STLDeleteElements(&cookies);
241 }
242
243 } // namespace
244 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/net/quota_policy_cookie_store.cc ('k') | content/browser/net/sqlite_persistent_cookie_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698