OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 key_loaded_event_.Signal(); | 43 key_loaded_event_.Signal(); |
44 } | 44 } |
45 | 45 |
46 void Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 46 void Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
47 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 47 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
48 base::Unretained(this))); | 48 base::Unretained(this))); |
49 loaded_event_.Wait(); | 49 loaded_event_.Wait(); |
50 *cookies = cookies_; | 50 *cookies = cookies_; |
51 } | 51 } |
52 | 52 |
53 void DestroyStore() { | |
54 store_ = NULL; | |
55 // Make sure we wait until the destructor has run. | |
Randy Smith (Not in Mondays)
2012/03/01 22:12:10
nit, suggestion: It's not obvious from the name wh
erikwright (departed)
2012/03/02 03:20:31
Done.
| |
56 scoped_refptr<base::ThreadTestHelper> helper( | |
57 new base::ThreadTestHelper( | |
58 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
59 ASSERT_TRUE(helper->Run()); | |
60 } | |
61 | |
62 void CreateAndLoad( | |
63 bool restore_old_session_cookies, | |
64 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | |
65 store_ = new SQLitePersistentCookieStore( | |
66 temp_dir_.path().Append(chrome::kCookieFilename), | |
67 restore_old_session_cookies); | |
68 Load(cookies); | |
69 } | |
70 | |
71 void InitializeStore(bool restore_old_session_cookies) { | |
72 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
73 CreateAndLoad(restore_old_session_cookies, &cookies); | |
74 ASSERT_EQ(0u, cookies.size()); | |
75 } | |
76 | |
53 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 77 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
54 // bind a non-void returning method as a Closure. | 78 // bind a non-void returning method as a Closure. |
55 void WaitOnDBEvent() { | 79 void WaitOnDBEvent() { |
56 db_thread_event_.Wait(); | 80 db_thread_event_.Wait(); |
57 } | 81 } |
58 | 82 |
83 // Adds a persistent cookie to store_. | |
84 void AddCookie(const std::string& name, | |
85 const std::string& value, | |
86 const std::string& domain, | |
87 const std::string& path, | |
88 const base::Time& creation) { | |
89 store_->AddCookie( | |
90 net::CookieMonster::CanonicalCookie(GURL(), name, value, domain, path, | |
91 std::string(), std::string(), | |
92 creation, creation, creation, | |
93 false, false, true, true)); | |
94 } | |
95 | |
59 virtual void SetUp() { | 96 virtual void SetUp() { |
60 ui_thread_.Start(); | 97 ui_thread_.Start(); |
61 db_thread_.Start(); | 98 db_thread_.Start(); |
62 io_thread_.Start(); | 99 io_thread_.Start(); |
63 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 100 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
64 store_ = new SQLitePersistentCookieStore( | |
65 temp_dir_.path().Append(chrome::kCookieFilename), false); | |
66 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
67 Load(&cookies); | |
68 ASSERT_EQ(0u, cookies.size()); | |
69 // Make sure the store gets written at least once. | |
70 store_->AddCookie( | |
71 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | |
72 "/", std::string(), std::string(), | |
73 base::Time::Now(), | |
74 base::Time::Now(), | |
75 base::Time::Now(), | |
76 false, false, true, true)); | |
77 } | 101 } |
78 | 102 |
79 protected: | 103 protected: |
80 content::TestBrowserThread ui_thread_; | 104 content::TestBrowserThread ui_thread_; |
81 content::TestBrowserThread db_thread_; | 105 content::TestBrowserThread db_thread_; |
82 content::TestBrowserThread io_thread_; | 106 content::TestBrowserThread io_thread_; |
83 base::WaitableEvent loaded_event_; | 107 base::WaitableEvent loaded_event_; |
84 base::WaitableEvent key_loaded_event_; | 108 base::WaitableEvent key_loaded_event_; |
85 base::WaitableEvent db_thread_event_; | 109 base::WaitableEvent db_thread_event_; |
86 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 110 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
87 ScopedTempDir temp_dir_; | 111 ScopedTempDir temp_dir_; |
88 scoped_refptr<SQLitePersistentCookieStore> store_; | 112 scoped_refptr<SQLitePersistentCookieStore> store_; |
89 }; | 113 }; |
90 | 114 |
91 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 115 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
116 InitializeStore(false); | |
117 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | |
Randy Smith (Not in Mondays)
2012/03/01 22:12:10
Hmmm. This is only a suggestion (up to you) but t
erikwright (departed)
2012/03/02 03:20:31
Done.
| |
92 store_->SetClearLocalStateOnExit(false); | 118 store_->SetClearLocalStateOnExit(false); |
93 store_ = NULL; | 119 DestroyStore(); |
94 // Make sure we wait until the destructor has run. | |
95 scoped_refptr<base::ThreadTestHelper> helper( | |
96 new base::ThreadTestHelper( | |
97 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
98 ASSERT_TRUE(helper->Run()); | |
99 | 120 |
100 ASSERT_TRUE(file_util::PathExists( | 121 ASSERT_TRUE(file_util::PathExists( |
101 temp_dir_.path().Append(chrome::kCookieFilename))); | 122 temp_dir_.path().Append(chrome::kCookieFilename))); |
102 ASSERT_TRUE(file_util::Delete( | 123 ASSERT_TRUE(file_util::Delete( |
103 temp_dir_.path().Append(chrome::kCookieFilename), false)); | 124 temp_dir_.path().Append(chrome::kCookieFilename), false)); |
104 } | 125 } |
105 | 126 |
106 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { | 127 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { |
128 InitializeStore(false); | |
129 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | |
107 store_->SetClearLocalStateOnExit(true); | 130 store_->SetClearLocalStateOnExit(true); |
108 // Replace the store effectively destroying the current one and forcing it | 131 DestroyStore(); |
109 // to write it's data to disk. Then we can see if after loading it again it | |
110 // is still there. | |
111 store_ = NULL; | |
112 // Make sure we wait until the destructor has run. | |
113 scoped_refptr<base::ThreadTestHelper> helper( | |
114 new base::ThreadTestHelper( | |
115 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
116 ASSERT_TRUE(helper->Run()); | |
117 | 132 |
118 ASSERT_FALSE(file_util::PathExists( | 133 ASSERT_FALSE(file_util::PathExists( |
119 temp_dir_.path().Append(chrome::kCookieFilename))); | 134 temp_dir_.path().Append(chrome::kCookieFilename))); |
120 } | 135 } |
121 | 136 |
122 // Test if data is stored as expected in the SQLite database. | 137 // Test if data is stored as expected in the SQLite database. |
123 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { | 138 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { |
124 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 139 InitializeStore(false); |
140 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | |
125 // Replace the store effectively destroying the current one and forcing it | 141 // Replace the store effectively destroying the current one and forcing it |
126 // to write it's data to disk. Then we can see if after loading it again it | 142 // to write it's data to disk. Then we can see if after loading it again it |
127 // is still there. | 143 // is still there. |
128 store_ = NULL; | 144 DestroyStore(); |
129 scoped_refptr<base::ThreadTestHelper> helper( | |
130 new base::ThreadTestHelper( | |
131 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
132 // Make sure we wait until the destructor has run. | |
133 ASSERT_TRUE(helper->Run()); | |
134 store_ = new SQLitePersistentCookieStore( | |
135 temp_dir_.path().Append(chrome::kCookieFilename), false); | |
136 | |
137 // Reload and test for persistence | 145 // Reload and test for persistence |
138 Load(&cookies); | 146 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
147 CreateAndLoad(false, &cookies); | |
139 ASSERT_EQ(1U, cookies.size()); | 148 ASSERT_EQ(1U, cookies.size()); |
140 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 149 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
141 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 150 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
142 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 151 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
143 | 152 |
144 // Now delete the cookie and check persistence again. | 153 // Now delete the cookie and check persistence again. |
145 store_->DeleteCookie(*cookies[0]); | 154 store_->DeleteCookie(*cookies[0]); |
146 store_ = NULL; | 155 DestroyStore(); |
147 // Make sure we wait until the destructor has run. | |
148 ASSERT_TRUE(helper->Run()); | |
149 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 156 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
150 cookies.clear(); | 157 cookies.clear(); |
151 store_ = new SQLitePersistentCookieStore( | |
152 temp_dir_.path().Append(chrome::kCookieFilename), false); | |
153 | 158 |
154 // Reload and check if the cookie has been removed. | 159 // Reload and check if the cookie has been removed. |
155 Load(&cookies); | 160 CreateAndLoad(false, &cookies); |
156 ASSERT_EQ(0U, cookies.size()); | 161 ASSERT_EQ(0U, cookies.size()); |
157 } | 162 } |
158 | 163 |
159 // Test that priority load of cookies for a specfic domain key could be | 164 // Test that priority load of cookies for a specfic domain key could be |
160 // completed before the entire store is loaded | 165 // completed before the entire store is loaded |
161 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 166 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
162 base::Time t = base::Time::Now() + base::TimeDelta::FromInternalValue(10); | 167 InitializeStore(false); |
163 // A foo.bar cookie was already added in setup. | 168 base::Time t = base::Time::Now(); |
164 store_->AddCookie( | 169 AddCookie("A", "B", "http://foo.bar", "/", t); |
165 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", | |
166 "www.aaa.com", "/", | |
167 std::string(), std::string(), | |
168 t, t, t, false, false, true, true)); | |
169 t += base::TimeDelta::FromInternalValue(10); | 170 t += base::TimeDelta::FromInternalValue(10); |
170 store_->AddCookie( | 171 AddCookie("A", "B", "www.aaa.com", "/", t); |
171 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", | |
172 "travel.aaa.com", "/", | |
173 std::string(), std::string(), | |
174 t, t, t, false, false, true, true)); | |
175 t += base::TimeDelta::FromInternalValue(10); | 172 t += base::TimeDelta::FromInternalValue(10); |
176 store_->AddCookie( | 173 AddCookie("A", "B", "travel.aaa.com", "/", t); |
177 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", | 174 t += base::TimeDelta::FromInternalValue(10); |
178 "www.bbb.com", "/", | 175 AddCookie("A", "B", "www.bbb.com", "/", t); |
179 std::string(), std::string(), | 176 DestroyStore(); |
180 t, t, t, false, false, true, true)); | |
181 store_ = NULL; | |
182 | |
183 scoped_refptr<base::ThreadTestHelper> helper( | |
184 new base::ThreadTestHelper( | |
185 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
186 // Make sure we wait until the destructor has run. | |
187 ASSERT_TRUE(helper->Run()); | |
188 | 177 |
189 store_ = new SQLitePersistentCookieStore( | 178 store_ = new SQLitePersistentCookieStore( |
190 temp_dir_.path().Append(chrome::kCookieFilename), false); | 179 temp_dir_.path().Append(chrome::kCookieFilename), false); |
191 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 180 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
192 // until both Load and LoadCookiesForKey have been posted to its task queue. | 181 // until both Load and LoadCookiesForKey have been posted to its task queue. |
193 BrowserThread::PostTask( | 182 BrowserThread::PostTask( |
194 BrowserThread::DB, FROM_HERE, | 183 BrowserThread::DB, FROM_HERE, |
195 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 184 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
196 base::Unretained(this))); | 185 base::Unretained(this))); |
197 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 186 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
229 it = cookies_.begin(); it != cookies_.end(); ++it) | 218 it = cookies_.begin(); it != cookies_.end(); ++it) |
230 cookies_loaded.insert((*it)->Domain().c_str()); | 219 cookies_loaded.insert((*it)->Domain().c_str()); |
231 ASSERT_EQ(4U, cookies_loaded.size()); | 220 ASSERT_EQ(4U, cookies_loaded.size()); |
232 ASSERT_EQ(cookies_loaded.find("http://foo.bar") != cookies_loaded.end(), | 221 ASSERT_EQ(cookies_loaded.find("http://foo.bar") != cookies_loaded.end(), |
233 true); | 222 true); |
234 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | 223 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
235 } | 224 } |
236 | 225 |
237 // Test that we can force the database to be written by calling Flush(). | 226 // Test that we can force the database to be written by calling Flush(). |
238 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 227 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
228 InitializeStore(false); | |
239 // File timestamps don't work well on all platforms, so we'll determine | 229 // File timestamps don't work well on all platforms, so we'll determine |
240 // whether the DB file has been modified by checking its size. | 230 // whether the DB file has been modified by checking its size. |
241 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 231 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
242 base::PlatformFileInfo info; | 232 base::PlatformFileInfo info; |
243 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 233 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
244 int64 base_size = info.size; | 234 int64 base_size = info.size; |
245 | 235 |
246 // Write some large cookies, so the DB will have to expand by several KB. | 236 // Write some large cookies, so the DB will have to expand by several KB. |
247 for (char c = 'a'; c < 'z'; ++c) { | 237 for (char c = 'a'; c < 'z'; ++c) { |
248 // Each cookie needs a unique timestamp for creation_utc (see DB schema). | 238 // Each cookie needs a unique timestamp for creation_utc (see DB schema). |
249 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); | 239 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); |
250 std::string name(1, c); | 240 std::string name(1, c); |
251 std::string value(1000, c); | 241 std::string value(1000, c); |
252 store_->AddCookie( | 242 AddCookie(name, value, "http://foo.bar", "/", t); |
253 net::CookieMonster::CanonicalCookie(GURL(), name, value, | |
254 "http://foo.bar", "/", | |
255 std::string(), std::string(), | |
256 t, t, t, | |
257 false, false, true, true)); | |
258 } | 243 } |
259 | 244 |
260 // Call Flush() and wait until the DB thread is idle. | 245 // Call Flush() and wait until the DB thread is idle. |
261 store_->Flush(base::Closure()); | 246 store_->Flush(base::Closure()); |
262 scoped_refptr<base::ThreadTestHelper> helper( | 247 scoped_refptr<base::ThreadTestHelper> helper( |
263 new base::ThreadTestHelper( | 248 new base::ThreadTestHelper( |
264 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 249 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
265 ASSERT_TRUE(helper->Run()); | 250 ASSERT_TRUE(helper->Run()); |
266 | 251 |
267 // We forced a write, so now the file will be bigger. | 252 // We forced a write, so now the file will be bigger. |
(...skipping 14 matching lines...) Expand all Loading... | |
282 return callback_count_; | 267 return callback_count_; |
283 } | 268 } |
284 | 269 |
285 private: | 270 private: |
286 friend class base::RefCountedThreadSafe<CallbackCounter>; | 271 friend class base::RefCountedThreadSafe<CallbackCounter>; |
287 volatile int callback_count_; | 272 volatile int callback_count_; |
288 }; | 273 }; |
289 | 274 |
290 // Test that we can get a completion callback after a Flush(). | 275 // Test that we can get a completion callback after a Flush(). |
291 TEST_F(SQLitePersistentCookieStoreTest, TestFlushCompletionCallback) { | 276 TEST_F(SQLitePersistentCookieStoreTest, TestFlushCompletionCallback) { |
277 InitializeStore(false); | |
278 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | |
279 | |
292 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); | 280 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); |
293 | 281 |
294 // Callback shouldn't be invoked until we call Flush(). | 282 // Callback shouldn't be invoked until we call Flush(). |
295 ASSERT_EQ(0, counter->callback_count()); | 283 ASSERT_EQ(0, counter->callback_count()); |
296 | 284 |
297 store_->Flush(base::Bind(&CallbackCounter::Callback, counter.get())); | 285 store_->Flush(base::Bind(&CallbackCounter::Callback, counter.get())); |
298 | 286 |
299 scoped_refptr<base::ThreadTestHelper> helper( | 287 scoped_refptr<base::ThreadTestHelper> helper( |
300 new base::ThreadTestHelper( | 288 new base::ThreadTestHelper( |
301 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 289 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
302 ASSERT_TRUE(helper->Run()); | 290 ASSERT_TRUE(helper->Run()); |
303 | 291 |
304 ASSERT_EQ(1, counter->callback_count()); | 292 ASSERT_EQ(1, counter->callback_count()); |
305 } | 293 } |
306 | 294 |
307 // Test loading old session cookies from the disk. | 295 // Test loading old session cookies from the disk. |
308 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 296 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
309 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 297 InitializeStore(true); |
310 | |
311 // Use a separate cookie store for this test. | |
312 ScopedTempDir temp_dir; | |
313 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
314 store_ = new SQLitePersistentCookieStore( | |
315 temp_dir.path().Append(chrome::kCookieFilename), true); | |
316 // Make sure the database is initialized. | |
317 Load(&cookies); | |
318 ASSERT_EQ(0u, cookies.size()); | |
319 | 298 |
320 // Add a session cookie. | 299 // Add a session cookie. |
321 store_->AddCookie( | 300 store_->AddCookie( |
322 net::CookieMonster::CanonicalCookie( | 301 net::CookieMonster::CanonicalCookie( |
323 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), | 302 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), |
324 std::string(), base::Time::Now(), base::Time::Now(), | 303 std::string(), base::Time::Now(), base::Time::Now(), |
325 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 304 base::Time::Now(), false, false, true, false /*is_persistent*/)); |
326 | 305 |
327 // Force the store to write its data to the disk. | 306 // Force the store to write its data to the disk. |
328 store_ = NULL; | 307 DestroyStore(); |
329 scoped_refptr<base::ThreadTestHelper> helper( | |
330 new base::ThreadTestHelper( | |
331 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
332 // Make sure we wait until the destructor has run. | |
333 ASSERT_TRUE(helper->Run()); | |
334 | 308 |
335 // Create a store that loads session cookies. | 309 // Create a store that loads session cookies and test that the session cookie |
336 store_ = new SQLitePersistentCookieStore( | 310 // was loaded. |
337 temp_dir.path().Append(chrome::kCookieFilename), true); | 311 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
312 CreateAndLoad(true, &cookies); | |
338 | 313 |
339 // Reload and test that the session cookie was loaded. | |
340 Load(&cookies); | |
341 ASSERT_EQ(1U, cookies.size()); | 314 ASSERT_EQ(1U, cookies.size()); |
342 ASSERT_STREQ("http://sessioncookie.com", cookies[0]->Domain().c_str()); | 315 ASSERT_STREQ("http://sessioncookie.com", cookies[0]->Domain().c_str()); |
343 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 316 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
344 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 317 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
318 | |
319 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | |
320 cookies.clear(); | |
345 } | 321 } |
346 | 322 |
347 // Test loading old session cookies from the disk. | 323 // Test loading old session cookies from the disk. |
348 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 324 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
349 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 325 InitializeStore(true); |
350 // Use a separate cookie store for this test. | |
351 ScopedTempDir temp_dir; | |
352 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
353 store_ = new SQLitePersistentCookieStore( | |
354 temp_dir.path().Append(chrome::kCookieFilename), true); | |
355 // Make sure the database is initialized. | |
356 Load(&cookies); | |
357 ASSERT_EQ(0u, cookies.size()); | |
358 | 326 |
359 // Add a session cookie. | 327 // Add a session cookie. |
360 store_->AddCookie( | 328 store_->AddCookie( |
361 net::CookieMonster::CanonicalCookie( | 329 net::CookieMonster::CanonicalCookie( |
362 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), | 330 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), |
363 std::string(), base::Time::Now(), base::Time::Now(), | 331 std::string(), base::Time::Now(), base::Time::Now(), |
364 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 332 base::Time::Now(), false, false, true, false /*is_persistent*/)); |
365 | 333 |
366 // Force the store to write its data to the disk. | 334 // Force the store to write its data to the disk. |
367 store_ = NULL; | 335 DestroyStore(); |
368 scoped_refptr<base::ThreadTestHelper> helper( | |
369 new base::ThreadTestHelper( | |
370 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
371 // Make sure we wait until the destructor has run. | |
372 ASSERT_TRUE(helper->Run()); | |
373 | 336 |
374 // Create a store that doesn't load old session cookies. | 337 // Create a store that doesn't load old session cookies and test that the |
375 store_ = new SQLitePersistentCookieStore( | 338 // session cookie was not loaded. |
376 temp_dir.path().Append(chrome::kCookieFilename), false); | 339 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
377 | 340 CreateAndLoad(false, &cookies); |
378 // Reload and test that the session cookie was not loaded. | |
379 Load(&cookies); | |
380 ASSERT_EQ(0U, cookies.size()); | 341 ASSERT_EQ(0U, cookies.size()); |
381 | 342 |
382 // The store should also delete the session cookie. Wait until that has been | 343 // The store should also delete the session cookie. Wait until that has been |
383 // done. | 344 // done. |
384 store_ = NULL; | 345 DestroyStore(); |
385 helper = new base::ThreadTestHelper( | |
386 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)); | |
387 ASSERT_TRUE(helper->Run()); | |
388 | 346 |
389 // Create a store that loads old session cookies. | 347 // Create a store that loads old session cookies and test that the session |
390 store_ = new SQLitePersistentCookieStore( | 348 // cookie is gone. |
391 temp_dir.path().Append(chrome::kCookieFilename), true); | 349 CreateAndLoad(true, &cookies); |
392 | |
393 // Reload and test that the session cookie is gone. | |
394 Load(&cookies); | |
395 ASSERT_EQ(0U, cookies.size()); | 350 ASSERT_EQ(0U, cookies.size()); |
396 } | 351 } |
397 | 352 |
398 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) { | 353 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) { |
399 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 354 InitializeStore(true); |
400 | |
401 // Use a separate cookie store for this test. | |
402 ScopedTempDir temp_dir; | |
403 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
404 store_ = new SQLitePersistentCookieStore( | |
405 temp_dir.path().Append(chrome::kCookieFilename), true); | |
406 // Make sure the database is initialized. | |
407 Load(&cookies); | |
408 ASSERT_EQ(0u, cookies.size()); | |
409 | 355 |
410 // Add a session cookie with has_expires = false, and another session cookie | 356 // Add a session cookie with has_expires = false, and another session cookie |
411 // with has_expires = true. | 357 // with has_expires = true. |
412 store_->AddCookie( | 358 store_->AddCookie( |
413 net::CookieMonster::CanonicalCookie( | 359 net::CookieMonster::CanonicalCookie( |
414 GURL(), "session-hasexpires", "val", "http://sessioncookie.com", "/", | 360 GURL(), "session-hasexpires", "val", "http://sessioncookie.com", "/", |
415 std::string(), std::string(), | 361 std::string(), std::string(), |
416 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(), | 362 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(), |
417 base::Time::Now(), false, false, true /* has_expires */, | 363 base::Time::Now(), false, false, true /* has_expires */, |
418 false /* is_persistent */)); | 364 false /* is_persistent */)); |
419 store_->AddCookie( | 365 store_->AddCookie( |
420 net::CookieMonster::CanonicalCookie( | 366 net::CookieMonster::CanonicalCookie( |
421 GURL(), "session-noexpires", "val", "http://sessioncookie.com", "/", | 367 GURL(), "session-noexpires", "val", "http://sessioncookie.com", "/", |
422 std::string(), std::string(), | 368 std::string(), std::string(), |
423 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(), | 369 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(), |
424 base::Time::Now(), false, false, false /* has_expires */, | 370 base::Time::Now(), false, false, false /* has_expires */, |
425 false /* is_persistent */)); | 371 false /* is_persistent */)); |
426 // Add a persistent cookie. | 372 // Add a persistent cookie. |
427 store_->AddCookie( | 373 store_->AddCookie( |
428 net::CookieMonster::CanonicalCookie( | 374 net::CookieMonster::CanonicalCookie( |
429 GURL(), "persistent", "val", "http://sessioncookie.com", "/", | 375 GURL(), "persistent", "val", "http://sessioncookie.com", "/", |
430 std::string(), std::string(), | 376 std::string(), std::string(), |
431 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 377 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), |
432 base::Time::Now(), false, false, true /* has_expires */, | 378 base::Time::Now(), false, false, true /* has_expires */, |
433 true /* is_persistent */)); | 379 true /* is_persistent */)); |
434 | 380 |
435 // Force the store to write its data to the disk. | 381 // Force the store to write its data to the disk. |
436 store_ = NULL; | 382 DestroyStore(); |
437 scoped_refptr<base::ThreadTestHelper> helper( | |
438 new base::ThreadTestHelper( | |
439 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
440 // Make sure we wait until the destructor has run. | |
441 ASSERT_TRUE(helper->Run()); | |
442 | 383 |
443 // Create a store that loads session cookies. | 384 // Create a store that loads session cookies and test that the the DoesExpire |
444 store_ = new SQLitePersistentCookieStore( | 385 // and IsPersistent attributes are restored. |
445 temp_dir.path().Append(chrome::kCookieFilename), true); | 386 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
446 | 387 CreateAndLoad(true, &cookies); |
447 // Reload and test that the the DoesExpire and IsPersistent attributes are | |
448 // restored. | |
449 Load(&cookies); | |
450 ASSERT_EQ(3U, cookies.size()); | 388 ASSERT_EQ(3U, cookies.size()); |
451 | 389 |
452 std::map<std::string, net::CookieMonster::CanonicalCookie*> cookie_map; | 390 std::map<std::string, net::CookieMonster::CanonicalCookie*> cookie_map; |
453 std::vector<net::CookieMonster::CanonicalCookie*>::const_iterator it; | 391 std::vector<net::CookieMonster::CanonicalCookie*>::const_iterator it; |
454 for (it = cookies.begin(); it != cookies.end(); ++it) | 392 for (it = cookies.begin(); it != cookies.end(); ++it) |
455 cookie_map[(*it)->Name()] = *it; | 393 cookie_map[(*it)->Name()] = *it; |
456 | 394 |
457 EXPECT_TRUE(cookie_map["session-hasexpires"]->DoesExpire()); | 395 EXPECT_TRUE(cookie_map["session-hasexpires"]->DoesExpire()); |
458 EXPECT_FALSE(cookie_map["session-hasexpires"]->IsPersistent()); | 396 EXPECT_FALSE(cookie_map["session-hasexpires"]->IsPersistent()); |
459 | 397 |
460 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); | 398 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); |
461 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); | 399 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); |
462 | 400 |
463 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); | 401 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); |
464 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); | 402 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); |
403 | |
404 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | |
405 cookies.clear(); | |
465 } | 406 } |
OLD | NEW |