OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/net/sqlite_persistent_cookie_store.h" | 5 #include "content/browser/net/sqlite_persistent_cookie_store.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 void AddCookie(const std::string& name, | 157 void AddCookie(const std::string& name, |
158 const std::string& value, | 158 const std::string& value, |
159 const std::string& domain, | 159 const std::string& domain, |
160 const std::string& path, | 160 const std::string& path, |
161 const base::Time& creation) { | 161 const base::Time& creation) { |
162 store_->AddCookie(net::CanonicalCookie( | 162 store_->AddCookie(net::CanonicalCookie( |
163 GURL(), name, value, domain, path, creation, creation, creation, false, | 163 GURL(), name, value, domain, path, creation, creation, creation, false, |
164 false, false, net::COOKIE_PRIORITY_DEFAULT)); | 164 false, false, net::COOKIE_PRIORITY_DEFAULT)); |
165 } | 165 } |
166 | 166 |
| 167 void AddCookieWithExpiration(const std::string& name, |
| 168 const std::string& value, |
| 169 const std::string& domain, |
| 170 const std::string& path, |
| 171 const base::Time& creation, |
| 172 const base::Time& expiration) { |
| 173 store_->AddCookie(net::CanonicalCookie( |
| 174 GURL(), name, value, domain, path, creation, expiration, creation, |
| 175 false, false, false, net::COOKIE_PRIORITY_DEFAULT)); |
| 176 } |
| 177 |
167 std::string ReadRawDBContents() { | 178 std::string ReadRawDBContents() { |
168 std::string contents; | 179 std::string contents; |
169 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), | 180 if (!base::ReadFileToString(temp_dir_.path().Append(kCookieFilename), |
170 &contents)) | 181 &contents)) |
171 return std::string(); | 182 return std::string(); |
172 return contents; | 183 return contents; |
173 } | 184 } |
174 | 185 |
175 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } | 186 void SetUp() override { ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); } |
176 | 187 |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // Now delete the cookie and check persistence again. | 261 // Now delete the cookie and check persistence again. |
251 store_->DeleteCookie(*cookies[0]); | 262 store_->DeleteCookie(*cookies[0]); |
252 DestroyStore(); | 263 DestroyStore(); |
253 STLDeleteElements(&cookies); | 264 STLDeleteElements(&cookies); |
254 | 265 |
255 // Reload and check if the cookie has been removed. | 266 // Reload and check if the cookie has been removed. |
256 CreateAndLoad(false, false, &cookies); | 267 CreateAndLoad(false, false, &cookies); |
257 ASSERT_EQ(0U, cookies.size()); | 268 ASSERT_EQ(0U, cookies.size()); |
258 } | 269 } |
259 | 270 |
| 271 TEST_F(SQLitePersistentCookieStoreTest, TestSessionCookiesDeletedOnStartup) { |
| 272 // Initialize the cookie store with 3 persistent cookies, 5 transient |
| 273 // cookies. |
| 274 InitializeStore(false, false); |
| 275 // Add persistent cookies. |
| 276 AddCookieWithExpiration("A", "B", "a1.com", "/", base::Time::Now(), |
| 277 base::Time::Now()); |
| 278 AddCookieWithExpiration("A", "B", "a2.com", "/", base::Time::Now(), |
| 279 base::Time::Now()); |
| 280 AddCookieWithExpiration("A", "B", "a3.com", "/", base::Time::Now(), |
| 281 base::Time::Now()); |
| 282 // Add transient cookies. |
| 283 AddCookieWithExpiration("A", "B", "b1.com", "/", base::Time::Now(), |
| 284 base::Time()); |
| 285 AddCookieWithExpiration("A", "B", "b2.com", "/", base::Time::Now(), |
| 286 base::Time()); |
| 287 AddCookieWithExpiration("A", "B", "b3.com", "/", base::Time::Now(), |
| 288 base::Time()); |
| 289 AddCookieWithExpiration("A", "B", "b4.com", "/", base::Time::Now(), |
| 290 base::Time()); |
| 291 AddCookieWithExpiration("A", "B", "b5.com", "/", base::Time::Now(), |
| 292 base::Time()); |
| 293 DestroyStore(); |
| 294 |
| 295 // Load the store a second time. Before the store finishes loading, add a |
| 296 // transient cookie and flush it to disk. |
| 297 store_ = new SQLitePersistentCookieStore( |
| 298 temp_dir_.path().Append(kCookieFilename), |
| 299 client_task_runner(), |
| 300 background_task_runner(), |
| 301 false, NULL, NULL); |
| 302 |
| 303 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 304 // until both Load and Flush have been posted to its task queue. |
| 305 background_task_runner()->PostTask( |
| 306 FROM_HERE, |
| 307 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 308 base::Unretained(this))); |
| 309 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 310 base::Unretained(this))); |
| 311 AddCookieWithExpiration("A", "B", "c.com", "/", base::Time::Now(), |
| 312 base::Time()); |
| 313 base::WaitableEvent event(false, false); |
| 314 store_->Flush(base::Bind(&base::WaitableEvent::Signal, |
| 315 base::Unretained(&event))); |
| 316 |
| 317 // Now the DB-thread queue contains: |
| 318 // (active:) |
| 319 // 1. Wait (on db_event) |
| 320 // (pending:) |
| 321 // 2. "Init And Chain-Load First Domain" |
| 322 // 3. Add Cookie (c.com) |
| 323 // 4. Flush Cookie (c.com) |
| 324 db_thread_event_.Signal(); |
| 325 event.Wait(); |
| 326 loaded_event_.Wait(); |
| 327 STLDeleteElements(&cookies_); |
| 328 DestroyStore(); |
| 329 |
| 330 // Load the store a third time, this time restoring session cookies. The |
| 331 // store should contain exactly 4 cookies: the 3 persistent, and "c.com", |
| 332 // which was added during the second cookie store load. |
| 333 store_ = new SQLitePersistentCookieStore( |
| 334 temp_dir_.path().Append(kCookieFilename), |
| 335 client_task_runner(), |
| 336 background_task_runner(), |
| 337 true, NULL, NULL); |
| 338 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 339 base::Unretained(this))); |
| 340 loaded_event_.Wait(); |
| 341 ASSERT_EQ(4u, cookies_.size()); |
| 342 STLDeleteElements(&cookies_); |
| 343 } |
| 344 |
260 // Test that priority load of cookies for a specfic domain key could be | 345 // Test that priority load of cookies for a specfic domain key could be |
261 // completed before the entire store is loaded | 346 // completed before the entire store is loaded |
262 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 347 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
263 InitializeStore(false, false); | 348 InitializeStore(false, false); |
264 base::Time t = base::Time::Now(); | 349 base::Time t = base::Time::Now(); |
265 AddCookie("A", "B", "foo.bar", "/", t); | 350 AddCookie("A", "B", "foo.bar", "/", t); |
266 t += base::TimeDelta::FromInternalValue(10); | 351 t += base::TimeDelta::FromInternalValue(10); |
267 AddCookie("A", "B", "www.aaa.com", "/", t); | 352 AddCookie("A", "B", "www.aaa.com", "/", t); |
268 t += base::TimeDelta::FromInternalValue(10); | 353 t += base::TimeDelta::FromInternalValue(10); |
269 AddCookie("A", "B", "travel.aaa.com", "/", t); | 354 AddCookie("A", "B", "travel.aaa.com", "/", t); |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 EXPECT_EQ(2, resultcount); | 670 EXPECT_EQ(2, resultcount); |
586 | 671 |
587 // Verify that "encrypted_value" is NOT visible in the file. | 672 // Verify that "encrypted_value" is NOT visible in the file. |
588 contents = ReadRawDBContents(); | 673 contents = ReadRawDBContents(); |
589 EXPECT_NE(0U, contents.length()); | 674 EXPECT_NE(0U, contents.length()); |
590 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); | 675 EXPECT_EQ(contents.find("encrypted_value123XYZ"), std::string::npos); |
591 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); | 676 EXPECT_EQ(contents.find("something456ABC"), std::string::npos); |
592 } | 677 } |
593 | 678 |
594 } // namespace content | 679 } // namespace content |
OLD | NEW |