| 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 "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" |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "base/test/thread_test_helper.h" | 13 #include "base/test/thread_test_helper.h" |
| 14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "chrome/browser/net/clear_on_exit_policy.h" |
| 15 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 16 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
| 16 #include "chrome/common/chrome_constants.h" | 17 #include "chrome/common/chrome_constants.h" |
| 17 #include "content/test/test_browser_thread.h" | 18 #include "content/test/test_browser_thread.h" |
| 18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
| 19 #include "sql/connection.h" | 20 #include "sql/connection.h" |
| 20 #include "sql/meta_table.h" | 21 #include "sql/meta_table.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 23 #include "webkit/quota/mock_special_storage_policy.h" |
| 22 | 24 |
| 23 using content::BrowserThread; | 25 using content::BrowserThread; |
| 24 | 26 |
| 25 class SQLitePersistentCookieStoreTest : public testing::Test { | 27 class SQLitePersistentCookieStoreTest : public testing::Test { |
| 26 public: | 28 public: |
| 27 SQLitePersistentCookieStoreTest() | 29 SQLitePersistentCookieStoreTest() |
| 28 : ui_thread_(BrowserThread::UI), | 30 : ui_thread_(BrowserThread::UI), |
| 29 db_thread_(BrowserThread::DB), | 31 db_thread_(BrowserThread::DB), |
| 30 io_thread_(BrowserThread::IO), | 32 io_thread_(BrowserThread::IO), |
| 31 loaded_event_(false, false), | 33 loaded_event_(false, false), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 60 new base::ThreadTestHelper( | 62 new base::ThreadTestHelper( |
| 61 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 63 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 62 ASSERT_TRUE(helper->Run()); | 64 ASSERT_TRUE(helper->Run()); |
| 63 } | 65 } |
| 64 | 66 |
| 65 void CreateAndLoad( | 67 void CreateAndLoad( |
| 66 bool restore_old_session_cookies, | 68 bool restore_old_session_cookies, |
| 67 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 69 std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
| 68 store_ = new SQLitePersistentCookieStore( | 70 store_ = new SQLitePersistentCookieStore( |
| 69 temp_dir_.path().Append(chrome::kCookieFilename), | 71 temp_dir_.path().Append(chrome::kCookieFilename), |
| 70 restore_old_session_cookies); | 72 restore_old_session_cookies, |
| 73 NULL); |
| 71 Load(cookies); | 74 Load(cookies); |
| 72 } | 75 } |
| 73 | 76 |
| 74 void InitializeStore(bool restore_old_session_cookies) { | 77 void InitializeStore(bool restore_old_session_cookies) { |
| 75 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 78 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 76 CreateAndLoad(restore_old_session_cookies, &cookies); | 79 CreateAndLoad(restore_old_session_cookies, &cookies); |
| 77 ASSERT_EQ(0u, cookies.size()); | 80 ASSERT_EQ(0u, cookies.size()); |
| 78 } | 81 } |
| 79 | 82 |
| 80 // We have to create this method to wrap WaitableEvent::Wait, since we cannot | 83 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
| (...skipping 30 matching lines...) Expand all Loading... |
| 111 base::WaitableEvent key_loaded_event_; | 114 base::WaitableEvent key_loaded_event_; |
| 112 base::WaitableEvent db_thread_event_; | 115 base::WaitableEvent db_thread_event_; |
| 113 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 116 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| 114 ScopedTempDir temp_dir_; | 117 ScopedTempDir temp_dir_; |
| 115 scoped_refptr<SQLitePersistentCookieStore> store_; | 118 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 116 }; | 119 }; |
| 117 | 120 |
| 118 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 121 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
| 119 InitializeStore(false); | 122 InitializeStore(false); |
| 120 // Put some data - any data - on disk, to have something to keep. | 123 // Put some data - any data - on disk, to have something to keep. |
| 121 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 124 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 122 store_->SetClearLocalStateOnExit(false); | 125 store_->SetClearLocalStateOnExit(false); |
| 123 DestroyStore(); | 126 DestroyStore(); |
| 124 | 127 |
| 125 ASSERT_TRUE(file_util::PathExists( | 128 ASSERT_TRUE(file_util::PathExists( |
| 126 temp_dir_.path().Append(chrome::kCookieFilename))); | 129 temp_dir_.path().Append(chrome::kCookieFilename))); |
| 127 ASSERT_TRUE(file_util::Delete( | 130 ASSERT_TRUE(file_util::Delete( |
| 128 temp_dir_.path().Append(chrome::kCookieFilename), false)); | 131 temp_dir_.path().Append(chrome::kCookieFilename), false)); |
| 129 } | 132 } |
| 130 | 133 |
| 131 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { | 134 TEST_F(SQLitePersistentCookieStoreTest, RemoveOnDestruction) { |
| 132 InitializeStore(false); | 135 InitializeStore(false); |
| 133 // Put some data - any data - on disk, to have something to remove. | 136 // Put some data - any data - on disk, to have something to remove. |
| 134 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 137 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 135 store_->SetClearLocalStateOnExit(true); | 138 store_->SetClearLocalStateOnExit(true); |
| 136 DestroyStore(); | 139 DestroyStore(); |
| 137 | 140 |
| 138 ASSERT_FALSE(file_util::PathExists( | 141 ASSERT_FALSE(file_util::PathExists( |
| 139 temp_dir_.path().Append(chrome::kCookieFilename))); | 142 temp_dir_.path().Append(chrome::kCookieFilename))); |
| 140 } | 143 } |
| 141 | 144 |
| 142 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { | 145 TEST_F(SQLitePersistentCookieStoreTest, TestInvalidMetaTableRecovery) { |
| 143 InitializeStore(false); | 146 InitializeStore(false); |
| 144 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 147 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 145 DestroyStore(); | 148 DestroyStore(); |
| 146 | 149 |
| 147 // Load up the store and verify that it has good data in it. | 150 // Load up the store and verify that it has good data in it. |
| 148 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 151 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 149 CreateAndLoad(false, &cookies); | 152 CreateAndLoad(false, &cookies); |
| 150 ASSERT_EQ(1U, cookies.size()); | 153 ASSERT_EQ(1U, cookies.size()); |
| 151 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 154 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
| 152 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 155 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
| 153 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 156 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
| 154 DestroyStore(); | 157 DestroyStore(); |
| 155 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 158 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 156 cookies.clear(); | 159 cookies.clear(); |
| 157 | 160 |
| 158 // Now corrupt the meta table. | 161 // Now corrupt the meta table. |
| 159 { | 162 { |
| 160 sql::Connection db; | 163 sql::Connection db; |
| 161 ASSERT_TRUE(db.Open(temp_dir_.path().Append(chrome::kCookieFilename))); | 164 ASSERT_TRUE(db.Open(temp_dir_.path().Append(chrome::kCookieFilename))); |
| 162 sql::MetaTable meta_table_; | 165 sql::MetaTable meta_table_; |
| 163 meta_table_.Init(&db, 1, 1); | 166 meta_table_.Init(&db, 1, 1); |
| 164 ASSERT_TRUE(db.Execute("DELETE FROM meta")); | 167 ASSERT_TRUE(db.Execute("DELETE FROM meta")); |
| 165 db.Close(); | 168 db.Close(); |
| 166 } | 169 } |
| 167 | 170 |
| 168 // Upon loading, the database should be reset to a good, blank state. | 171 // Upon loading, the database should be reset to a good, blank state. |
| 169 CreateAndLoad(false, &cookies); | 172 CreateAndLoad(false, &cookies); |
| 170 ASSERT_EQ(0U, cookies.size()); | 173 ASSERT_EQ(0U, cookies.size()); |
| 171 | 174 |
| 172 // Verify that, after, recovery, the database persists properly. | 175 // Verify that, after, recovery, the database persists properly. |
| 173 AddCookie("X", "Y", "http://foo.bar", "/", base::Time::Now()); | 176 AddCookie("X", "Y", "foo.bar", "/", base::Time::Now()); |
| 174 DestroyStore(); | 177 DestroyStore(); |
| 175 CreateAndLoad(false, &cookies); | 178 CreateAndLoad(false, &cookies); |
| 176 ASSERT_EQ(1U, cookies.size()); | 179 ASSERT_EQ(1U, cookies.size()); |
| 177 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 180 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
| 178 ASSERT_STREQ("X", cookies[0]->Name().c_str()); | 181 ASSERT_STREQ("X", cookies[0]->Name().c_str()); |
| 179 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); | 182 ASSERT_STREQ("Y", cookies[0]->Value().c_str()); |
| 180 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 183 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 181 cookies.clear(); | 184 cookies.clear(); |
| 182 } | 185 } |
| 183 | 186 |
| 184 // Test if data is stored as expected in the SQLite database. | 187 // Test if data is stored as expected in the SQLite database. |
| 185 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { | 188 TEST_F(SQLitePersistentCookieStoreTest, TestPersistance) { |
| 186 InitializeStore(false); | 189 InitializeStore(false); |
| 187 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 190 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 188 // Replace the store effectively destroying the current one and forcing it | 191 // Replace the store effectively destroying the current one and forcing it |
| 189 // to write its data to disk. Then we can see if after loading it again it | 192 // to write its data to disk. Then we can see if after loading it again it |
| 190 // is still there. | 193 // is still there. |
| 191 DestroyStore(); | 194 DestroyStore(); |
| 192 // Reload and test for persistence | 195 // Reload and test for persistence |
| 193 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 196 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 194 CreateAndLoad(false, &cookies); | 197 CreateAndLoad(false, &cookies); |
| 195 ASSERT_EQ(1U, cookies.size()); | 198 ASSERT_EQ(1U, cookies.size()); |
| 196 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 199 ASSERT_STREQ("foo.bar", cookies[0]->Domain().c_str()); |
| 197 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 200 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
| 198 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 201 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
| 199 | 202 |
| 200 // Now delete the cookie and check persistence again. | 203 // Now delete the cookie and check persistence again. |
| 201 store_->DeleteCookie(*cookies[0]); | 204 store_->DeleteCookie(*cookies[0]); |
| 202 DestroyStore(); | 205 DestroyStore(); |
| 203 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 206 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 204 cookies.clear(); | 207 cookies.clear(); |
| 205 | 208 |
| 206 // Reload and check if the cookie has been removed. | 209 // Reload and check if the cookie has been removed. |
| 207 CreateAndLoad(false, &cookies); | 210 CreateAndLoad(false, &cookies); |
| 208 ASSERT_EQ(0U, cookies.size()); | 211 ASSERT_EQ(0U, cookies.size()); |
| 209 } | 212 } |
| 210 | 213 |
| 211 // Test that priority load of cookies for a specfic domain key could be | 214 // Test that priority load of cookies for a specfic domain key could be |
| 212 // completed before the entire store is loaded | 215 // completed before the entire store is loaded |
| 213 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | 216 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
| 214 InitializeStore(false); | 217 InitializeStore(false); |
| 215 base::Time t = base::Time::Now(); | 218 base::Time t = base::Time::Now(); |
| 216 AddCookie("A", "B", "http://foo.bar", "/", t); | 219 AddCookie("A", "B", "foo.bar", "/", t); |
| 217 t += base::TimeDelta::FromInternalValue(10); | 220 t += base::TimeDelta::FromInternalValue(10); |
| 218 AddCookie("A", "B", "www.aaa.com", "/", t); | 221 AddCookie("A", "B", "www.aaa.com", "/", t); |
| 219 t += base::TimeDelta::FromInternalValue(10); | 222 t += base::TimeDelta::FromInternalValue(10); |
| 220 AddCookie("A", "B", "travel.aaa.com", "/", t); | 223 AddCookie("A", "B", "travel.aaa.com", "/", t); |
| 221 t += base::TimeDelta::FromInternalValue(10); | 224 t += base::TimeDelta::FromInternalValue(10); |
| 222 AddCookie("A", "B", "www.bbb.com", "/", t); | 225 AddCookie("A", "B", "www.bbb.com", "/", t); |
| 223 DestroyStore(); | 226 DestroyStore(); |
| 224 | 227 |
| 225 store_ = new SQLitePersistentCookieStore( | 228 store_ = new SQLitePersistentCookieStore( |
| 226 temp_dir_.path().Append(chrome::kCookieFilename), false); | 229 temp_dir_.path().Append(chrome::kCookieFilename), false, NULL); |
| 227 // Posting a blocking task to db_thread_ makes sure that the DB thread waits | 230 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 228 // until both Load and LoadCookiesForKey have been posted to its task queue. | 231 // until both Load and LoadCookiesForKey have been posted to its task queue. |
| 229 BrowserThread::PostTask( | 232 BrowserThread::PostTask( |
| 230 BrowserThread::DB, FROM_HERE, | 233 BrowserThread::DB, FROM_HERE, |
| 231 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, | 234 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 232 base::Unretained(this))); | 235 base::Unretained(this))); |
| 233 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 236 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 234 base::Unretained(this))); | 237 base::Unretained(this))); |
| 235 store_->LoadCookiesForKey("aaa.com", | 238 store_->LoadCookiesForKey("aaa.com", |
| 236 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, | 239 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 258 ASSERT_EQ(cookies_loaded.find("www.aaa.com") != cookies_loaded.end(), true); | 261 ASSERT_EQ(cookies_loaded.find("www.aaa.com") != cookies_loaded.end(), true); |
| 259 ASSERT_EQ(cookies_loaded.find("travel.aaa.com") != cookies_loaded.end(), | 262 ASSERT_EQ(cookies_loaded.find("travel.aaa.com") != cookies_loaded.end(), |
| 260 true); | 263 true); |
| 261 | 264 |
| 262 db_thread_event_.Signal(); | 265 db_thread_event_.Signal(); |
| 263 loaded_event_.Wait(); | 266 loaded_event_.Wait(); |
| 264 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator | 267 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator |
| 265 it = cookies_.begin(); it != cookies_.end(); ++it) | 268 it = cookies_.begin(); it != cookies_.end(); ++it) |
| 266 cookies_loaded.insert((*it)->Domain().c_str()); | 269 cookies_loaded.insert((*it)->Domain().c_str()); |
| 267 ASSERT_EQ(4U, cookies_loaded.size()); | 270 ASSERT_EQ(4U, cookies_loaded.size()); |
| 268 ASSERT_EQ(cookies_loaded.find("http://foo.bar") != cookies_loaded.end(), | 271 ASSERT_EQ(cookies_loaded.find("foo.bar") != cookies_loaded.end(), |
| 269 true); | 272 true); |
| 270 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | 273 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
| 271 } | 274 } |
| 272 | 275 |
| 273 // Test that we can force the database to be written by calling Flush(). | 276 // Test that we can force the database to be written by calling Flush(). |
| 274 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 277 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
| 275 InitializeStore(false); | 278 InitializeStore(false); |
| 276 // File timestamps don't work well on all platforms, so we'll determine | 279 // File timestamps don't work well on all platforms, so we'll determine |
| 277 // whether the DB file has been modified by checking its size. | 280 // whether the DB file has been modified by checking its size. |
| 278 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 281 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
| 279 base::PlatformFileInfo info; | 282 base::PlatformFileInfo info; |
| 280 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 283 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
| 281 int64 base_size = info.size; | 284 int64 base_size = info.size; |
| 282 | 285 |
| 283 // Write some large cookies, so the DB will have to expand by several KB. | 286 // Write some large cookies, so the DB will have to expand by several KB. |
| 284 for (char c = 'a'; c < 'z'; ++c) { | 287 for (char c = 'a'; c < 'z'; ++c) { |
| 285 // Each cookie needs a unique timestamp for creation_utc (see DB schema). | 288 // Each cookie needs a unique timestamp for creation_utc (see DB schema). |
| 286 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); | 289 base::Time t = base::Time::Now() + base::TimeDelta::FromMicroseconds(c); |
| 287 std::string name(1, c); | 290 std::string name(1, c); |
| 288 std::string value(1000, c); | 291 std::string value(1000, c); |
| 289 AddCookie(name, value, "http://foo.bar", "/", t); | 292 AddCookie(name, value, "foo.bar", "/", t); |
| 290 } | 293 } |
| 291 | 294 |
| 292 // Call Flush() and wait until the DB thread is idle. | 295 // Call Flush() and wait until the DB thread is idle. |
| 293 store_->Flush(base::Closure()); | 296 store_->Flush(base::Closure()); |
| 294 scoped_refptr<base::ThreadTestHelper> helper( | 297 scoped_refptr<base::ThreadTestHelper> helper( |
| 295 new base::ThreadTestHelper( | 298 new base::ThreadTestHelper( |
| 296 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 299 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 297 ASSERT_TRUE(helper->Run()); | 300 ASSERT_TRUE(helper->Run()); |
| 298 | 301 |
| 299 // We forced a write, so now the file will be bigger. | 302 // We forced a write, so now the file will be bigger. |
| (...skipping 18 matching lines...) Expand all Loading... |
| 318 friend class base::RefCountedThreadSafe<CallbackCounter>; | 321 friend class base::RefCountedThreadSafe<CallbackCounter>; |
| 319 ~CallbackCounter() {} | 322 ~CallbackCounter() {} |
| 320 | 323 |
| 321 volatile int callback_count_; | 324 volatile int callback_count_; |
| 322 }; | 325 }; |
| 323 | 326 |
| 324 // Test that we can get a completion callback after a Flush(). | 327 // Test that we can get a completion callback after a Flush(). |
| 325 TEST_F(SQLitePersistentCookieStoreTest, TestFlushCompletionCallback) { | 328 TEST_F(SQLitePersistentCookieStoreTest, TestFlushCompletionCallback) { |
| 326 InitializeStore(false); | 329 InitializeStore(false); |
| 327 // Put some data - any data - on disk, so that Flush is not a no-op. | 330 // Put some data - any data - on disk, so that Flush is not a no-op. |
| 328 AddCookie("A", "B", "http://foo.bar", "/", base::Time::Now()); | 331 AddCookie("A", "B", "foo.bar", "/", base::Time::Now()); |
| 329 | 332 |
| 330 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); | 333 scoped_refptr<CallbackCounter> counter(new CallbackCounter()); |
| 331 | 334 |
| 332 // Callback shouldn't be invoked until we call Flush(). | 335 // Callback shouldn't be invoked until we call Flush(). |
| 333 ASSERT_EQ(0, counter->callback_count()); | 336 ASSERT_EQ(0, counter->callback_count()); |
| 334 | 337 |
| 335 store_->Flush(base::Bind(&CallbackCounter::Callback, counter.get())); | 338 store_->Flush(base::Bind(&CallbackCounter::Callback, counter.get())); |
| 336 | 339 |
| 337 scoped_refptr<base::ThreadTestHelper> helper( | 340 scoped_refptr<base::ThreadTestHelper> helper( |
| 338 new base::ThreadTestHelper( | 341 new base::ThreadTestHelper( |
| 339 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 342 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 340 ASSERT_TRUE(helper->Run()); | 343 ASSERT_TRUE(helper->Run()); |
| 341 | 344 |
| 342 ASSERT_EQ(1, counter->callback_count()); | 345 ASSERT_EQ(1, counter->callback_count()); |
| 343 } | 346 } |
| 344 | 347 |
| 345 // Test loading old session cookies from the disk. | 348 // Test loading old session cookies from the disk. |
| 346 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { | 349 TEST_F(SQLitePersistentCookieStoreTest, TestLoadOldSessionCookies) { |
| 347 InitializeStore(true); | 350 InitializeStore(true); |
| 348 | 351 |
| 349 // Add a session cookie. | 352 // Add a session cookie. |
| 350 store_->AddCookie( | 353 store_->AddCookie( |
| 351 net::CookieMonster::CanonicalCookie( | 354 net::CookieMonster::CanonicalCookie( |
| 352 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), | 355 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 353 std::string(), base::Time::Now(), base::Time::Now(), | 356 std::string(), base::Time::Now(), base::Time::Now(), |
| 354 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 357 base::Time::Now(), false, false, true, false /*is_persistent*/)); |
| 355 | 358 |
| 356 // Force the store to write its data to the disk. | 359 // Force the store to write its data to the disk. |
| 357 DestroyStore(); | 360 DestroyStore(); |
| 358 | 361 |
| 359 // Create a store that loads session cookies and test that the session cookie | 362 // Create a store that loads session cookies and test that the session cookie |
| 360 // was loaded. | 363 // was loaded. |
| 361 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 364 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 362 CreateAndLoad(true, &cookies); | 365 CreateAndLoad(true, &cookies); |
| 363 | 366 |
| 364 ASSERT_EQ(1U, cookies.size()); | 367 ASSERT_EQ(1U, cookies.size()); |
| 365 ASSERT_STREQ("http://sessioncookie.com", cookies[0]->Domain().c_str()); | 368 ASSERT_STREQ("sessioncookie.com", cookies[0]->Domain().c_str()); |
| 366 ASSERT_STREQ("C", cookies[0]->Name().c_str()); | 369 ASSERT_STREQ("C", cookies[0]->Name().c_str()); |
| 367 ASSERT_STREQ("D", cookies[0]->Value().c_str()); | 370 ASSERT_STREQ("D", cookies[0]->Value().c_str()); |
| 368 | 371 |
| 369 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 372 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 370 cookies.clear(); | 373 cookies.clear(); |
| 371 } | 374 } |
| 372 | 375 |
| 373 // Test loading old session cookies from the disk. | 376 // Test loading old session cookies from the disk. |
| 374 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { | 377 TEST_F(SQLitePersistentCookieStoreTest, TestDontLoadOldSessionCookies) { |
| 375 InitializeStore(true); | 378 InitializeStore(true); |
| 376 | 379 |
| 377 // Add a session cookie. | 380 // Add a session cookie. |
| 378 store_->AddCookie( | 381 store_->AddCookie( |
| 379 net::CookieMonster::CanonicalCookie( | 382 net::CookieMonster::CanonicalCookie( |
| 380 GURL(), "C", "D", "http://sessioncookie.com", "/", std::string(), | 383 GURL(), "C", "D", "sessioncookie.com", "/", std::string(), |
| 381 std::string(), base::Time::Now(), base::Time::Now(), | 384 std::string(), base::Time::Now(), base::Time::Now(), |
| 382 base::Time::Now(), false, false, true, false /*is_persistent*/)); | 385 base::Time::Now(), false, false, true, false /*is_persistent*/)); |
| 383 | 386 |
| 384 // Force the store to write its data to the disk. | 387 // Force the store to write its data to the disk. |
| 385 DestroyStore(); | 388 DestroyStore(); |
| 386 | 389 |
| 387 // Create a store that doesn't load old session cookies and test that the | 390 // Create a store that doesn't load old session cookies and test that the |
| 388 // session cookie was not loaded. | 391 // session cookie was not loaded. |
| 389 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 392 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 390 CreateAndLoad(false, &cookies); | 393 CreateAndLoad(false, &cookies); |
| 391 ASSERT_EQ(0U, cookies.size()); | 394 ASSERT_EQ(0U, cookies.size()); |
| 392 | 395 |
| 393 // The store should also delete the session cookie. Wait until that has been | 396 // The store should also delete the session cookie. Wait until that has been |
| 394 // done. | 397 // done. |
| 395 DestroyStore(); | 398 DestroyStore(); |
| 396 | 399 |
| 397 // Create a store that loads old session cookies and test that the session | 400 // Create a store that loads old session cookies and test that the session |
| 398 // cookie is gone. | 401 // cookie is gone. |
| 399 CreateAndLoad(true, &cookies); | 402 CreateAndLoad(true, &cookies); |
| 400 ASSERT_EQ(0U, cookies.size()); | 403 ASSERT_EQ(0U, cookies.size()); |
| 401 } | 404 } |
| 402 | 405 |
| 403 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) { | 406 TEST_F(SQLitePersistentCookieStoreTest, PersistHasExpiresAndIsPersistent) { |
| 404 InitializeStore(true); | 407 InitializeStore(true); |
| 405 | 408 |
| 406 // Add a session cookie with has_expires = false, and another session cookie | 409 // Add a session cookie with has_expires = false, and another session cookie |
| 407 // with has_expires = true. | 410 // with has_expires = true. |
| 408 store_->AddCookie( | 411 store_->AddCookie( |
| 409 net::CookieMonster::CanonicalCookie( | 412 net::CookieMonster::CanonicalCookie( |
| 410 GURL(), "session-hasexpires", "val", "http://sessioncookie.com", "/", | 413 GURL(), "session-hasexpires", "val", "sessioncookie.com", "/", |
| 411 std::string(), std::string(), | 414 std::string(), std::string(), |
| 412 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(), | 415 base::Time::Now() - base::TimeDelta::FromDays(3), base::Time::Now(), |
| 413 base::Time::Now(), false, false, true /* has_expires */, | 416 base::Time::Now(), false, false, true /* has_expires */, |
| 414 false /* is_persistent */)); | 417 false /* is_persistent */)); |
| 415 store_->AddCookie( | 418 store_->AddCookie( |
| 416 net::CookieMonster::CanonicalCookie( | 419 net::CookieMonster::CanonicalCookie( |
| 417 GURL(), "session-noexpires", "val", "http://sessioncookie.com", "/", | 420 GURL(), "session-noexpires", "val", "sessioncookie.com", "/", |
| 418 std::string(), std::string(), | 421 std::string(), std::string(), |
| 419 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(), | 422 base::Time::Now() - base::TimeDelta::FromDays(2), base::Time::Now(), |
| 420 base::Time::Now(), false, false, false /* has_expires */, | 423 base::Time::Now(), false, false, false /* has_expires */, |
| 421 false /* is_persistent */)); | 424 false /* is_persistent */)); |
| 422 // Add a persistent cookie. | 425 // Add a persistent cookie. |
| 423 store_->AddCookie( | 426 store_->AddCookie( |
| 424 net::CookieMonster::CanonicalCookie( | 427 net::CookieMonster::CanonicalCookie( |
| 425 GURL(), "persistent", "val", "http://sessioncookie.com", "/", | 428 GURL(), "persistent", "val", "sessioncookie.com", "/", |
| 426 std::string(), std::string(), | 429 std::string(), std::string(), |
| 427 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), | 430 base::Time::Now() - base::TimeDelta::FromDays(1), base::Time::Now(), |
| 428 base::Time::Now(), false, false, true /* has_expires */, | 431 base::Time::Now(), false, false, true /* has_expires */, |
| 429 true /* is_persistent */)); | 432 true /* is_persistent */)); |
| 430 | 433 |
| 431 // Force the store to write its data to the disk. | 434 // Force the store to write its data to the disk. |
| 432 DestroyStore(); | 435 DestroyStore(); |
| 433 | 436 |
| 434 // Create a store that loads session cookies and test that the the DoesExpire | 437 // Create a store that loads session cookies and test that the the DoesExpire |
| 435 // and IsPersistent attributes are restored. | 438 // and IsPersistent attributes are restored. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 447 | 450 |
| 448 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); | 451 EXPECT_FALSE(cookie_map["session-noexpires"]->DoesExpire()); |
| 449 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); | 452 EXPECT_FALSE(cookie_map["session-noexpires"]->IsPersistent()); |
| 450 | 453 |
| 451 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); | 454 EXPECT_TRUE(cookie_map["persistent"]->DoesExpire()); |
| 452 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); | 455 EXPECT_TRUE(cookie_map["persistent"]->IsPersistent()); |
| 453 | 456 |
| 454 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 457 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 455 cookies.clear(); | 458 cookies.clear(); |
| 456 } | 459 } |
| 460 |
| 461 namespace { |
| 462 |
| 463 // True if the given cookie is in the vector. |
| 464 bool IsCookiePresent( |
| 465 std::vector<net::CookieMonster::CanonicalCookie*>* cookies, |
| 466 const std::string& domain, |
| 467 const std::string& name, |
| 468 const std::string& value, |
| 469 bool secure) { |
| 470 for (unsigned i = 0; i < cookies->size(); ++i) { |
| 471 if ((*cookies)[i]->Domain() == domain && |
| 472 (*cookies)[i]->Name() == name && |
| 473 (*cookies)[i]->Value() == value && |
| 474 (*cookies)[i]->IsSecure() == secure) { |
| 475 return true; |
| 476 } |
| 477 } |
| 478 return false; |
| 479 } |
| 480 |
| 481 } // namespace |
| 482 |
| 483 // Test deleting cookies as mandated by the clear on exit policy on shutdown. |
| 484 TEST_F(SQLitePersistentCookieStoreTest, TestClearOnExitPolicy) { |
| 485 std::string protected_origin("protected.com"); |
| 486 std::string session_origin("session.com"); |
| 487 std::string other_origin("other.com"); |
| 488 scoped_refptr<quota::MockSpecialStoragePolicy> storage_policy = |
| 489 new quota::MockSpecialStoragePolicy; |
| 490 scoped_refptr<ClearOnExitPolicy> clear_policy = |
| 491 new ClearOnExitPolicy(storage_policy.get()); |
| 492 storage_policy->AddProtected(GURL(std::string("http://") + protected_origin)); |
| 493 storage_policy->AddSessionOnly( |
| 494 GURL(std::string("http://") + protected_origin)); |
| 495 storage_policy->AddSessionOnly( |
| 496 GURL(std::string("http://") + session_origin)); |
| 497 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 498 store_ = new SQLitePersistentCookieStore( |
| 499 temp_dir_.path().Append(chrome::kCookieFilename), |
| 500 false, |
| 501 clear_policy.get()); |
| 502 Load(&cookies); |
| 503 ASSERT_EQ(0U, cookies.size()); |
| 504 |
| 505 // Add some cookies. |
| 506 base::Time t = base::Time::Now(); |
| 507 AddCookie("A", "1", protected_origin, "/", t); |
| 508 t += base::TimeDelta::FromInternalValue(10); |
| 509 AddCookie("B", "2", session_origin, "/", t); |
| 510 t += base::TimeDelta::FromInternalValue(10); |
| 511 AddCookie("C", "3", other_origin, "/", t); |
| 512 // A secure cookie on session_origin. |
| 513 t += base::TimeDelta::FromInternalValue(10); |
| 514 store_->AddCookie( |
| 515 net::CookieMonster::CanonicalCookie(GURL(), "D", "4", session_origin, "/", |
| 516 std::string(), std::string(), |
| 517 t, t, t, |
| 518 true, false, true, true)); |
| 519 |
| 520 // Force the store to write its data to the disk. |
| 521 DestroyStore(); |
| 522 |
| 523 // Create a store test that the cookie on session_origin does not exist. |
| 524 store_ = new SQLitePersistentCookieStore( |
| 525 temp_dir_.path().Append(chrome::kCookieFilename), |
| 526 false, |
| 527 clear_policy.get()); |
| 528 Load(&cookies); |
| 529 |
| 530 EXPECT_EQ(3U, cookies.size()); |
| 531 EXPECT_TRUE(IsCookiePresent(&cookies, protected_origin, "A", "1", false)); |
| 532 EXPECT_TRUE(IsCookiePresent(&cookies, other_origin, "C", "3", false)); |
| 533 EXPECT_TRUE(IsCookiePresent(&cookies, session_origin, "D", "4", true)); |
| 534 |
| 535 DestroyStore(); |
| 536 } |
| OLD | NEW |