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 | 19 |
20 class SQLitePersistentCookieStoreTest : public testing::Test { | 20 class SQLitePersistentCookieStoreTest : public testing::Test { |
21 public: | 21 public: |
22 SQLitePersistentCookieStoreTest() | 22 SQLitePersistentCookieStoreTest() |
23 : ui_thread_(BrowserThread::UI), | 23 : ui_thread_(BrowserThread::UI), |
24 db_thread_(BrowserThread::DB), | 24 db_thread_(BrowserThread::DB), |
25 io_thread_(BrowserThread::IO), | 25 io_thread_(BrowserThread::IO), |
26 event_(false, false) { | 26 loaded_event_(false, false), |
| 27 key_loaded_event_(false, false), |
| 28 db_thread_event_(true, false) { |
27 } | 29 } |
28 | 30 |
29 protected: | |
30 void OnLoaded( | 31 void OnLoaded( |
31 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | 32 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
32 cookies_ = cookies; | 33 cookies_ = cookies; |
33 event_.Signal(); | 34 loaded_event_.Signal(); |
34 } | 35 } |
35 | 36 |
36 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | 37 void OnKeyLoaded( |
37 bool result = | 38 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
38 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | 39 cookies_ = cookies; |
| 40 key_loaded_event_.Signal(); |
| 41 } |
| 42 |
| 43 void Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
| 44 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
39 base::Unretained(this))); | 45 base::Unretained(this))); |
40 event_.Wait(); | 46 loaded_event_.Wait(); |
41 *cookies = cookies_; | 47 *cookies = cookies_; |
42 return result; | 48 } |
| 49 |
| 50 // We have to create this method to wrap WaitableEvent::Wait, since we cannot |
| 51 // bind a non-void returning method as a Closure. |
| 52 void WaitOnDBEvent() { |
| 53 db_thread_event_.Wait(); |
43 } | 54 } |
44 | 55 |
45 virtual void SetUp() { | 56 virtual void SetUp() { |
46 ui_thread_.Start(); | 57 ui_thread_.Start(); |
47 db_thread_.Start(); | 58 db_thread_.Start(); |
48 io_thread_.Start(); | 59 io_thread_.Start(); |
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
50 store_ = new SQLitePersistentCookieStore( | 61 store_ = new SQLitePersistentCookieStore( |
51 temp_dir_.path().Append(chrome::kCookieFilename)); | 62 temp_dir_.path().Append(chrome::kCookieFilename)); |
52 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 63 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
53 ASSERT_TRUE(Load(&cookies)); | 64 Load(&cookies); |
54 ASSERT_EQ(0u, cookies.size()); | 65 ASSERT_EQ(0u, cookies.size()); |
55 // Make sure the store gets written at least once. | 66 // Make sure the store gets written at least once. |
56 store_->AddCookie( | 67 store_->AddCookie( |
57 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | 68 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", |
58 "/", std::string(), std::string(), | 69 "/", std::string(), std::string(), |
59 base::Time::Now(), | 70 base::Time::Now(), |
60 base::Time::Now(), | 71 base::Time::Now(), |
61 base::Time::Now(), | 72 base::Time::Now(), |
62 false, false, true)); | 73 false, false, true)); |
63 } | 74 } |
64 | 75 |
| 76 protected: |
65 BrowserThread ui_thread_; | 77 BrowserThread ui_thread_; |
66 BrowserThread db_thread_; | 78 BrowserThread db_thread_; |
67 BrowserThread io_thread_; | 79 BrowserThread io_thread_; |
68 base::WaitableEvent event_; | 80 base::WaitableEvent loaded_event_; |
| 81 base::WaitableEvent key_loaded_event_; |
| 82 base::WaitableEvent db_thread_event_; |
69 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 83 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
70 ScopedTempDir temp_dir_; | 84 ScopedTempDir temp_dir_; |
71 scoped_refptr<SQLitePersistentCookieStore> store_; | 85 scoped_refptr<SQLitePersistentCookieStore> store_; |
72 }; | 86 }; |
73 | 87 |
74 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 88 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
75 store_->SetClearLocalStateOnExit(false); | 89 store_->SetClearLocalStateOnExit(false); |
76 store_ = NULL; | 90 store_ = NULL; |
77 // Make sure we wait until the destructor has run. | 91 // Make sure we wait until the destructor has run. |
78 scoped_refptr<base::ThreadTestHelper> helper( | 92 scoped_refptr<base::ThreadTestHelper> helper( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 store_ = NULL; | 125 store_ = NULL; |
112 scoped_refptr<base::ThreadTestHelper> helper( | 126 scoped_refptr<base::ThreadTestHelper> helper( |
113 new base::ThreadTestHelper( | 127 new base::ThreadTestHelper( |
114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
115 // Make sure we wait until the destructor has run. | 129 // Make sure we wait until the destructor has run. |
116 ASSERT_TRUE(helper->Run()); | 130 ASSERT_TRUE(helper->Run()); |
117 store_ = new SQLitePersistentCookieStore( | 131 store_ = new SQLitePersistentCookieStore( |
118 temp_dir_.path().Append(chrome::kCookieFilename)); | 132 temp_dir_.path().Append(chrome::kCookieFilename)); |
119 | 133 |
120 // Reload and test for persistence | 134 // Reload and test for persistence |
121 ASSERT_TRUE(Load(&cookies)); | 135 Load(&cookies); |
122 ASSERT_EQ(1U, cookies.size()); | 136 ASSERT_EQ(1U, cookies.size()); |
123 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 137 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
124 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 138 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
125 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 139 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
126 | 140 |
127 // Now delete the cookie and check persistence again. | 141 // Now delete the cookie and check persistence again. |
128 store_->DeleteCookie(*cookies[0]); | 142 store_->DeleteCookie(*cookies[0]); |
129 store_ = NULL; | 143 store_ = NULL; |
130 // Make sure we wait until the destructor has run. | 144 // Make sure we wait until the destructor has run. |
131 ASSERT_TRUE(helper->Run()); | 145 ASSERT_TRUE(helper->Run()); |
132 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 146 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
133 cookies.clear(); | 147 cookies.clear(); |
134 store_ = new SQLitePersistentCookieStore( | 148 store_ = new SQLitePersistentCookieStore( |
135 temp_dir_.path().Append(chrome::kCookieFilename)); | 149 temp_dir_.path().Append(chrome::kCookieFilename)); |
136 | 150 |
137 // Reload and check if the cookie has been removed. | 151 // Reload and check if the cookie has been removed. |
138 ASSERT_TRUE(Load(&cookies)); | 152 Load(&cookies); |
139 ASSERT_EQ(0U, cookies.size()); | 153 ASSERT_EQ(0U, cookies.size()); |
140 } | 154 } |
141 | 155 |
| 156 // Test that priority load of cookies for a specfic domain key could be |
| 157 // completed before the entire store is loaded |
| 158 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) { |
| 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 "travel.aaa.com", "/", |
| 170 std::string(), std::string(), |
| 171 t, t, t, false, false, true)); |
| 172 t += base::TimeDelta::FromInternalValue(10); |
| 173 store_->AddCookie( |
| 174 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", |
| 175 "www.bbb.com", "/", |
| 176 std::string(), std::string(), |
| 177 t, t, t, false, false, true)); |
| 178 store_ = NULL; |
| 179 |
| 180 scoped_refptr<base::ThreadTestHelper> helper( |
| 181 new base::ThreadTestHelper( |
| 182 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
| 183 // Make sure we wait until the destructor has run. |
| 184 ASSERT_TRUE(helper->Run()); |
| 185 |
| 186 store_ = new SQLitePersistentCookieStore( |
| 187 temp_dir_.path().Append(chrome::kCookieFilename)); |
| 188 // Posting a blocking task to db_thread_ makes sure that the DB thread waits |
| 189 // until both Load and LoadCookiesForKey have been posted to its task queue. |
| 190 db_thread_.PostTask(BrowserThread::DB, FROM_HERE, |
| 191 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 192 base::Unretained(this))); |
| 193 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
| 194 base::Unretained(this))); |
| 195 store_->LoadCookiesForKey("aaa.com", |
| 196 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded, |
| 197 base::Unretained(this))); |
| 198 db_thread_.PostTask(BrowserThread::DB, FROM_HERE, |
| 199 base::Bind(&SQLitePersistentCookieStoreTest::WaitOnDBEvent, |
| 200 base::Unretained(this))); |
| 201 |
| 202 // Now the DB-thread queue contains: |
| 203 // (active:) |
| 204 // 1. Wait (on db_event) |
| 205 // (pending:) |
| 206 // 2. "Init And Chain-Load First Domain" |
| 207 // 3. Priority Load (aaa.com) |
| 208 // 4. Wait (on db_event) |
| 209 db_thread_event_.Signal(); |
| 210 key_loaded_event_.Wait(); |
| 211 ASSERT_EQ(loaded_event_.IsSignaled(), false); |
| 212 std::set<std::string> cookies_loaded; |
| 213 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator |
| 214 it = cookies_.begin(); it != cookies_.end(); ++it) |
| 215 cookies_loaded.insert((*it)->Domain().c_str()); |
| 216 ASSERT_GT(4U, cookies_loaded.size()); |
| 217 ASSERT_EQ(cookies_loaded.find("www.aaa.com") != cookies_loaded.end(), true); |
| 218 ASSERT_EQ(cookies_loaded.find("travel.aaa.com") != cookies_loaded.end(), |
| 219 true); |
| 220 |
| 221 db_thread_event_.Signal(); |
| 222 loaded_event_.Wait(); |
| 223 for (std::vector<net::CookieMonster::CanonicalCookie*>::iterator |
| 224 it = cookies_.begin(); it != cookies_.end(); ++it) |
| 225 cookies_loaded.insert((*it)->Domain().c_str()); |
| 226 ASSERT_EQ(4U, cookies_loaded.size()); |
| 227 ASSERT_EQ(cookies_loaded.find("http://foo.bar") != cookies_loaded.end(), |
| 228 true); |
| 229 ASSERT_EQ(cookies_loaded.find("www.bbb.com") != cookies_loaded.end(), true); |
| 230 } |
| 231 |
142 // Test that we can force the database to be written by calling Flush(). | 232 // Test that we can force the database to be written by calling Flush(). |
143 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 233 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
144 // File timestamps don't work well on all platforms, so we'll determine | 234 // 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. | 235 // whether the DB file has been modified by checking its size. |
146 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 236 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
147 base::PlatformFileInfo info; | 237 base::PlatformFileInfo info; |
148 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 238 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
149 int64 base_size = info.size; | 239 int64 base_size = info.size; |
150 | 240 |
151 // Write some large cookies, so the DB will have to expand by several KB. | 241 // 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 | 291 |
202 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); | 292 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); |
203 | 293 |
204 scoped_refptr<base::ThreadTestHelper> helper( | 294 scoped_refptr<base::ThreadTestHelper> helper( |
205 new base::ThreadTestHelper( | 295 new base::ThreadTestHelper( |
206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 296 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
207 ASSERT_TRUE(helper->Run()); | 297 ASSERT_TRUE(helper->Run()); |
208 | 298 |
209 ASSERT_EQ(1, counter->callback_count()); | 299 ASSERT_EQ(1, counter->callback_count()); |
210 } | 300 } |
OLD | NEW |