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