OLD | NEW |
---|---|
(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/synchronization/waitable_event.h" | |
14 #include "base/test/sequenced_worker_pool_owner.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 | |
34 class QuotaPolicyCookieStoreTest : public testing::Test { | |
35 public: | |
36 QuotaPolicyCookieStoreTest() | |
37 : pool_owner_(new base::SequencedWorkerPoolOwner(3, "Background Pool")), | |
38 loaded_event_(false, false) { | |
39 } | |
40 | |
41 void OnLoaded(const std::vector<net::CanonicalCookie*>& cookies) { | |
42 cookies_ = cookies; | |
43 loaded_event_.Signal(); | |
44 } | |
45 | |
46 void Load(std::vector<net::CanonicalCookie*>* cookies) { | |
47 EXPECT_FALSE(loaded_event_.IsSignaled()); | |
48 store_->Load(base::Bind(&QuotaPolicyCookieStoreTest::OnLoaded, | |
49 base::Unretained(this))); | |
50 loaded_event_.Wait(); | |
51 *cookies = cookies_; | |
52 } | |
53 | |
54 protected: | |
55 scoped_refptr<base::SequencedTaskRunner> background_task_runner() { | |
56 return pool_owner_->pool()->GetSequencedTaskRunner( | |
57 pool_owner_->pool()->GetNamedSequenceToken("background")); | |
58 } | |
59 | |
60 scoped_refptr<base::SequencedTaskRunner> client_task_runner() { | |
61 return pool_owner_->pool()->GetSequencedTaskRunner( | |
62 pool_owner_->pool()->GetNamedSequenceToken("client")); | |
63 } | |
64 | |
65 // Adds a persistent cookie to store_. | |
66 void AddCookie(const std::string& name, | |
67 const std::string& value, | |
68 const std::string& domain, | |
69 const std::string& path, | |
70 const base::Time& creation) { | |
71 store_->AddCookie( | |
72 net::CanonicalCookie( | |
73 GURL(), name, value, domain, path, creation, creation, creation, | |
74 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); | |
75 } | |
76 | |
77 void DestroyStore() { | |
78 store_ = NULL; | |
79 // Make sure we wait until the destructor has run by shutting down the pool | |
80 // resetting the owner (whose destructor blocks on the pool completion). | |
81 pool_owner_->pool()->Shutdown(3); | |
rohitrao (ping after 24h)
2015/03/18 14:37:34
@rsleevi For some reason, the indirection through
| |
82 pool_owner_.reset(new base::SequencedWorkerPoolOwner(3, "Background Pool")); | |
83 } | |
84 | |
85 void SetUp() override { | |
86 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
87 store_ = new QuotaPolicyCookieStore( | |
88 temp_dir_.path().Append(kTestCookiesFilename), | |
89 client_task_runner(), | |
90 background_task_runner(), | |
91 true, NULL, NULL); | |
92 std::vector<net::CanonicalCookie*> cookies; | |
93 Load(&cookies); | |
94 ASSERT_EQ(0u, cookies.size()); | |
95 // Make sure the store gets written at least once. | |
96 AddCookie("A", "B", "foo.com", "/", base::Time::Now()); | |
97 } | |
98 | |
99 void TearDown() override { | |
100 DestroyStore(); | |
101 pool_owner_->pool()->Shutdown(); | |
102 } | |
103 | |
104 base::MessageLoop main_loop_; | |
105 scoped_ptr<base::SequencedWorkerPoolOwner> pool_owner_; | |
106 base::WaitableEvent loaded_event_; | |
107 base::ScopedTempDir temp_dir_; | |
108 scoped_refptr<QuotaPolicyCookieStore> store_; | |
109 std::vector<net::CanonicalCookie*> cookies_; | |
110 }; | |
111 | |
112 // Test if data is stored as expected in the QuotaPolicy database. | |
113 TEST_F(QuotaPolicyCookieStoreTest, TestPersistence) { | |
114 AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); | |
115 | |
116 // Replace the store effectively destroying the current one and forcing it | |
117 // to write its data to disk. Then we can see if after loading it again it | |
118 // is still there. | |
119 DestroyStore(); | |
120 store_ = new QuotaPolicyCookieStore( | |
121 temp_dir_.path().Append(kTestCookiesFilename), | |
122 client_task_runner(), | |
123 background_task_runner(), | |
124 true, NULL, NULL); | |
125 | |
126 // Reload and test for persistence | |
127 std::vector<net::CanonicalCookie*> cookies; | |
128 Load(&cookies); | |
129 ASSERT_EQ(2U, cookies.size()); | |
130 EXPECT_EQ("foo.com", cookies[0]->Domain()); | |
131 EXPECT_EQ("persistent.com", cookies[1]->Domain()); | |
132 | |
133 // Now delete the cookies and check persistence again. | |
134 store_->DeleteCookie(*cookies[0]); | |
135 store_->DeleteCookie(*cookies[1]); | |
136 DestroyStore(); | |
137 | |
138 STLDeleteElements(&cookies); | |
139 cookies.clear(); | |
140 store_ = new QuotaPolicyCookieStore( | |
141 temp_dir_.path().Append(kTestCookiesFilename), | |
142 client_task_runner(), | |
143 background_task_runner(), | |
144 true, NULL, NULL); | |
145 | |
146 // Reload and check if the cert has been removed. | |
147 Load(&cookies); | |
148 ASSERT_EQ(0U, cookies.size()); | |
149 } | |
150 | |
151 // Test if data is stored as expected in the QuotaPolicy database. | |
152 TEST_F(QuotaPolicyCookieStoreTest, TestPolicy) { | |
153 AddCookie("A", "B", "persistent.com", "/", base::Time::Now()); | |
154 AddCookie("A", "B", "nonpersistent.com", "/", base::Time::Now()); | |
155 | |
156 // Replace the store effectively destroying the current one and forcing it | |
157 // to write its data to disk. Then we can see if after loading it again it | |
158 // is still there. | |
159 DestroyStore(); | |
160 // Specify storage policy that makes "nonpersistent.com" session only. | |
161 scoped_refptr<content::MockSpecialStoragePolicy> storage_policy = | |
162 new content::MockSpecialStoragePolicy(); | |
163 storage_policy->AddSessionOnly( | |
164 net::cookie_util::CookieOriginToURL("nonpersistent.com", false)); | |
165 store_ = new QuotaPolicyCookieStore( | |
166 temp_dir_.path().Append(kTestCookiesFilename), | |
167 client_task_runner(), | |
168 background_task_runner(), | |
169 true, storage_policy.get(), NULL); | |
170 | |
171 // Reload and test for persistence | |
172 std::vector<net::CanonicalCookie*> cookies; | |
173 Load(&cookies); | |
174 ASSERT_EQ(3U, cookies.size()); | |
175 | |
176 // Now close the store, and "nonpersistent.com" should be deleted according to | |
177 // policy. | |
178 DestroyStore(); | |
179 store_ = new QuotaPolicyCookieStore( | |
180 temp_dir_.path().Append(kTestCookiesFilename), | |
181 client_task_runner(), | |
182 background_task_runner(), | |
183 true, NULL, NULL); | |
184 | |
185 Load(&cookies); | |
186 ASSERT_EQ(2U, cookies.size()); | |
187 EXPECT_EQ("foo.com", cookies[0]->Domain()); | |
188 EXPECT_EQ("persistent.com", cookies[1]->Domain()); | |
189 } | |
190 | |
191 } // namespace content | |
OLD | NEW |