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