Chromium Code Reviews| 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/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
| 12 #include "base/test/thread_test_helper.h" | 12 #include "base/test/thread_test_helper.h" |
| 13 #include "base/time.h" | 13 #include "base/time.h" |
| 14 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" | 14 #include "chrome/browser/net/sqlite_persistent_cookie_store.h" |
| 15 #include "chrome/common/chrome_constants.h" | 15 #include "chrome/common/chrome_constants.h" |
| 16 #include "content/browser/browser_thread.h" | 16 #include "content/browser/browser_thread.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/platform_test.h" | |
|
erikwright (departed)
2011/10/06 17:16:47
platform_test is probably not required anymore?
guohui
2011/10/06 18:34:54
Done.
| |
| 19 | 20 |
| 20 class SQLitePersistentCookieStoreTest : public testing::Test { | 21 class SQLitePersistentCookieStoreTest : public testing::Test { |
| 21 public: | 22 public: |
| 22 SQLitePersistentCookieStoreTest() | 23 SQLitePersistentCookieStoreTest() |
| 23 : ui_thread_(BrowserThread::UI), | 24 : ui_thread_(BrowserThread::UI), |
| 24 db_thread_(BrowserThread::DB), | 25 db_thread_(BrowserThread::DB), |
| 25 io_thread_(BrowserThread::IO), | 26 io_thread_(BrowserThread::IO), |
| 26 event_(false, false) { | 27 event_(false, false), |
|
erikwright (departed)
2011/10/06 17:16:47
Remame these to "loaded_event_", "key_loaded_event
guohui
2011/10/06 18:34:54
Done.
| |
| 28 event_key_(false, false), | |
| 29 event_db_(true, false) { | |
| 27 } | 30 } |
| 28 | 31 |
| 29 protected: | |
| 30 void OnLoaded( | 32 void OnLoaded( |
| 31 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | 33 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
| 32 cookies_ = cookies; | 34 cookies_ = cookies; |
| 33 event_.Signal(); | 35 event_.Signal(); |
| 34 } | 36 } |
| 35 | 37 |
| 36 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 38 void OnKeyLoaded( |
| 37 bool result = | 39 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
| 38 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 40 cookies_ = cookies; |
| 41 event_key_.Signal(); | |
| 42 } | |
| 43 | |
| 44 void Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | |
| 45 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | |
| 39 base::Unretained(this))); | 46 base::Unretained(this))); |
| 40 event_.Wait(); | 47 event_.Wait(); |
| 41 *cookies = cookies_; | 48 *cookies = cookies_; |
| 42 return result; | 49 } |
| 50 | |
| 51 void Wait() { | |
|
erikwright (departed)
2011/10/06 17:16:47
For clarity of who is waiting on/signaling what, e
guohui
2011/10/06 18:34:54
Done.
| |
| 52 event_db_.Wait(); | |
| 43 } | 53 } |
| 44 | 54 |
| 45 virtual void SetUp() { | 55 virtual void SetUp() { |
| 46 ui_thread_.Start(); | 56 ui_thread_.Start(); |
| 47 db_thread_.Start(); | 57 db_thread_.Start(); |
| 48 io_thread_.Start(); | 58 io_thread_.Start(); |
| 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 59 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 50 store_ = new SQLitePersistentCookieStore( | 60 store_ = new SQLitePersistentCookieStore( |
| 51 temp_dir_.path().Append(chrome::kCookieFilename)); | 61 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 52 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 62 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
| 53 ASSERT_TRUE(Load(&cookies)); | 63 Load(&cookies); |
| 54 ASSERT_EQ(0u, cookies.size()); | 64 ASSERT_EQ(0u, cookies.size()); |
| 55 // Make sure the store gets written at least once. | 65 // Make sure the store gets written at least once. |
| 56 store_->AddCookie( | 66 store_->AddCookie( |
| 57 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | 67 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", |
| 58 "/", std::string(), std::string(), | 68 "/", std::string(), std::string(), |
| 59 base::Time::Now(), | 69 base::Time::Now(), |
| 60 base::Time::Now(), | 70 base::Time::Now(), |
| 61 base::Time::Now(), | 71 base::Time::Now(), |
| 62 false, false, true)); | 72 false, false, true)); |
| 63 } | 73 } |
| 64 | 74 |
| 75 protected: | |
| 65 BrowserThread ui_thread_; | 76 BrowserThread ui_thread_; |
| 66 BrowserThread db_thread_; | 77 BrowserThread db_thread_; |
| 67 BrowserThread io_thread_; | 78 BrowserThread io_thread_; |
| 68 base::WaitableEvent event_; | 79 base::WaitableEvent event_; |
| 80 base::WaitableEvent event_key_; | |
| 81 base::WaitableEvent event_db_; | |
| 69 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 82 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
| 70 ScopedTempDir temp_dir_; | 83 ScopedTempDir temp_dir_; |
| 71 scoped_refptr<SQLitePersistentCookieStore> store_; | 84 scoped_refptr<SQLitePersistentCookieStore> store_; |
| 72 }; | 85 }; |
| 73 | 86 |
| 74 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 87 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
| 75 store_->SetClearLocalStateOnExit(false); | 88 store_->SetClearLocalStateOnExit(false); |
| 76 store_ = NULL; | 89 store_ = NULL; |
| 77 // Make sure we wait until the destructor has run. | 90 // Make sure we wait until the destructor has run. |
| 78 scoped_refptr<base::ThreadTestHelper> helper( | 91 scoped_refptr<base::ThreadTestHelper> helper( |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 111 store_ = NULL; | 124 store_ = NULL; |
| 112 scoped_refptr<base::ThreadTestHelper> helper( | 125 scoped_refptr<base::ThreadTestHelper> helper( |
| 113 new base::ThreadTestHelper( | 126 new base::ThreadTestHelper( |
| 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 127 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 115 // Make sure we wait until the destructor has run. | 128 // Make sure we wait until the destructor has run. |
| 116 ASSERT_TRUE(helper->Run()); | 129 ASSERT_TRUE(helper->Run()); |
| 117 store_ = new SQLitePersistentCookieStore( | 130 store_ = new SQLitePersistentCookieStore( |
| 118 temp_dir_.path().Append(chrome::kCookieFilename)); | 131 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 119 | 132 |
| 120 // Reload and test for persistence | 133 // Reload and test for persistence |
| 121 ASSERT_TRUE(Load(&cookies)); | 134 Load(&cookies); |
| 122 ASSERT_EQ(1U, cookies.size()); | 135 ASSERT_EQ(1U, cookies.size()); |
| 123 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 136 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
| 124 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 137 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
| 125 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 138 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
| 126 | 139 |
| 127 // Now delete the cookie and check persistence again. | 140 // Now delete the cookie and check persistence again. |
| 128 store_->DeleteCookie(*cookies[0]); | 141 store_->DeleteCookie(*cookies[0]); |
| 129 store_ = NULL; | 142 store_ = NULL; |
| 130 // Make sure we wait until the destructor has run. | 143 // Make sure we wait until the destructor has run. |
| 131 ASSERT_TRUE(helper->Run()); | 144 ASSERT_TRUE(helper->Run()); |
| 132 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 145 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
| 133 cookies.clear(); | 146 cookies.clear(); |
| 134 store_ = new SQLitePersistentCookieStore( | 147 store_ = new SQLitePersistentCookieStore( |
| 135 temp_dir_.path().Append(chrome::kCookieFilename)); | 148 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 136 | 149 |
| 137 // Reload and check if the cookie has been removed. | 150 // Reload and check if the cookie has been removed. |
| 138 ASSERT_TRUE(Load(&cookies)); | 151 Load(&cookies); |
| 139 ASSERT_EQ(0U, cookies.size()); | 152 ASSERT_EQ(0U, cookies.size()); |
| 140 } | 153 } |
| 141 | 154 |
| 155 // Test that priority load of cookies for a specfic domain key could be | |
| 156 // completed before the entire store is loaded | |
| 157 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { | |
| 158 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | |
| 159 base::Time t = base::Time::Now() + base::TimeDelta::FromInternalValue(10); | |
| 160 // A foo.bar cookie was already added in setup. | |
| 161 store_->AddCookie( | |
| 162 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", | |
| 163 "www.aaa.com", "/", | |
| 164 std::string(), std::string(), | |
| 165 t, t, t, false, false, true)); | |
| 166 t += base::TimeDelta::FromInternalValue(10); | |
| 167 store_->AddCookie( | |
| 168 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", | |
| 169 "www.bbb.com", "/", | |
| 170 std::string(), std::string(), | |
| 171 t, t, t, false, false, true)); | |
| 172 store_ = NULL; | |
| 173 | |
| 174 scoped_refptr<base::ThreadTestHelper> helper( | |
| 175 new base::ThreadTestHelper( | |
| 176 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | |
| 177 // Make sure we wait until the destructor has run. | |
| 178 ASSERT_TRUE(helper->Run()); | |
| 179 | |
| 180 // The store is expected to have three cookies, each with a distinct eTLD+1. | |
| 181 store_ = new SQLitePersistentCookieStore( | |
| 182 temp_dir_.path().Append(chrome::kCookieFilename)); | |
| 183 // The event db_thread_ makes sure that the DB thread waits until both Load | |
|
erikwright (departed)
2011/10/06 17:16:47
"The event db_thread_ ..." => "Posting a blocking
guohui
2011/10/06 18:34:54
Done.
| |
| 184 // and LoadCookiesForKey have been posted to its task queue. | |
| 185 db_thread_.PostTask(BrowserThread::DB, FROM_HERE, | |
| 186 base::Bind(&SQLitePersistentCookieStoreTest::Wait, base::Unretained(this))); | |
| 187 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | |
| 188 base::Unretained(this))); | |
| 189 store_->LoadCookiesForKey("aaa.com", | |
| 190 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, | |
| 191 base::Unretained(this))); | |
| 192 db_thread_.PostTask(BrowserThread::DB, FROM_HERE, | |
| 193 base::Bind(&SQLitePersistentCookieStoreTest::Wait, base::Unretained(this))); | |
| 194 | |
| 195 event_db_.Signal(); | |
|
erikwright (departed)
2011/10/06 17:16:47
In the sample code I provided, there were some com
guohui
2011/10/06 18:34:54
Done.
| |
| 196 event_key_.Wait(); | |
| 197 ASSERT_EQ(event_.IsSignaled(), false); | |
| 198 std::set<std::string> cookies_loaded; | |
| 199 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator | |
| 200 it = cookies_.begin(); it != cookies_.end(); ++it) | |
| 201 cookies_loaded.insert((*it)->Domain().c_str()); | |
| 202 ASSERT_EQ(cookies_loaded.find("www.aaa.com") != cookies_loaded.end(), true); | |
| 203 | |
|
erikwright (departed)
2011/10/06 17:16:47
Can you also assert that cookies_loaded.size() is
guohui
2011/10/06 18:34:54
Done.
| |
| 204 event_db_.Signal(); | |
| 205 event_.Wait(); | |
| 206 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator | |
| 207 it = cookies_.begin(); it != cookies_.end(); ++it) | |
| 208 cookies_loaded.insert((*it)->Domain().c_str()); | |
| 209 ASSERT_EQ(3U, cookies_loaded.size()); | |
| 210 ASSERT_EQ(cookies_loaded.find("http://foo.bar") != cookies_loaded.end(), | |
| 211 true); | |
| 212 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); | |
| 213 } | |
| 214 | |
| 142 // Test that we can force the database to be written by calling Flush(). | 215 // Test that we can force the database to be written by calling Flush(). |
| 143 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 216 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
| 144 // File timestamps don't work well on all platforms, so we'll determine | 217 // File timestamps don't work well on all platforms, so we'll determine |
| 145 // whether the DB file has been modified by checking its size. | 218 // whether the DB file has been modified by checking its size. |
| 146 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 219 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
| 147 base::PlatformFileInfo info; | 220 base::PlatformFileInfo info; |
| 148 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 221 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
| 149 int64 base_size = info.size; | 222 int64 base_size = info.size; |
| 150 | 223 |
| 151 // Write some large cookies, so the DB will have to expand by several KB. | 224 // Write some large cookies, so the DB will have to expand by several KB. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 201 | 274 |
| 202 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); | 275 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); |
| 203 | 276 |
| 204 scoped_refptr<base::ThreadTestHelper> helper( | 277 scoped_refptr<base::ThreadTestHelper> helper( |
| 205 new base::ThreadTestHelper( | 278 new base::ThreadTestHelper( |
| 206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 279 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 207 ASSERT_TRUE(helper->Run()); | 280 ASSERT_TRUE(helper->Run()); |
| 208 | 281 |
| 209 ASSERT_EQ(1, counter->callback_count()); | 282 ASSERT_EQ(1, counter->callback_count()); |
| 210 } | 283 } |
| OLD | NEW |