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 loaded_event_(false, false), | 26 event_(false, false) { |
27 key_loaded_event_(false, false), | |
28 db_thread_event_(true, false) { | |
29 } | 27 } |
30 | 28 |
| 29 protected: |
31 void OnLoaded( | 30 void OnLoaded( |
32 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | 31 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { |
33 cookies_ = cookies; | 32 cookies_ = cookies; |
34 loaded_event_.Signal(); | 33 event_.Signal(); |
35 } | 34 } |
36 | 35 |
37 void OnKeyLoaded( | 36 bool Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { |
38 const std::vector<net::CookieMonster::CanonicalCookie*>& cookies) { | 37 bool result = |
39 cookies_ = cookies; | 38 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, |
40 key_loaded_event_.Signal(); | |
41 } | |
42 | |
43 void Load(std::vector<net::CookieMonster::CanonicalCookie*>* cookies) { | |
44 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded, | |
45 base::Unretained(this))); | 39 base::Unretained(this))); |
46 loaded_event_.Wait(); | 40 event_.Wait(); |
47 *cookies = cookies_; | 41 *cookies = cookies_; |
48 } | 42 return result; |
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(); | |
54 } | 43 } |
55 | 44 |
56 virtual void SetUp() { | 45 virtual void SetUp() { |
57 ui_thread_.Start(); | 46 ui_thread_.Start(); |
58 db_thread_.Start(); | 47 db_thread_.Start(); |
59 io_thread_.Start(); | 48 io_thread_.Start(); |
60 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
61 store_ = new SQLitePersistentCookieStore( | 50 store_ = new SQLitePersistentCookieStore( |
62 temp_dir_.path().Append(chrome::kCookieFilename)); | 51 temp_dir_.path().Append(chrome::kCookieFilename)); |
63 std::vector<net::CookieMonster::CanonicalCookie*> cookies; | 52 std::vector<net::CookieMonster::CanonicalCookie*> cookies; |
64 Load(&cookies); | 53 ASSERT_TRUE(Load(&cookies)); |
65 ASSERT_EQ(0u, cookies.size()); | 54 ASSERT_EQ(0u, cookies.size()); |
66 // Make sure the store gets written at least once. | 55 // Make sure the store gets written at least once. |
67 store_->AddCookie( | 56 store_->AddCookie( |
68 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", | 57 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", |
69 "/", std::string(), std::string(), | 58 "/", std::string(), std::string(), |
70 base::Time::Now(), | 59 base::Time::Now(), |
71 base::Time::Now(), | 60 base::Time::Now(), |
72 base::Time::Now(), | 61 base::Time::Now(), |
73 false, false, true)); | 62 false, false, true)); |
74 } | 63 } |
75 | 64 |
76 protected: | |
77 BrowserThread ui_thread_; | 65 BrowserThread ui_thread_; |
78 BrowserThread db_thread_; | 66 BrowserThread db_thread_; |
79 BrowserThread io_thread_; | 67 BrowserThread io_thread_; |
80 base::WaitableEvent loaded_event_; | 68 base::WaitableEvent event_; |
81 base::WaitableEvent key_loaded_event_; | |
82 base::WaitableEvent db_thread_event_; | |
83 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; | 69 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; |
84 ScopedTempDir temp_dir_; | 70 ScopedTempDir temp_dir_; |
85 scoped_refptr<SQLitePersistentCookieStore> store_; | 71 scoped_refptr<SQLitePersistentCookieStore> store_; |
86 }; | 72 }; |
87 | 73 |
88 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { | 74 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { |
89 store_->SetClearLocalStateOnExit(false); | 75 store_->SetClearLocalStateOnExit(false); |
90 store_ = NULL; | 76 store_ = NULL; |
91 // Make sure we wait until the destructor has run. | 77 // Make sure we wait until the destructor has run. |
92 scoped_refptr<base::ThreadTestHelper> helper( | 78 scoped_refptr<base::ThreadTestHelper> helper( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 store_ = NULL; | 111 store_ = NULL; |
126 scoped_refptr<base::ThreadTestHelper> helper( | 112 scoped_refptr<base::ThreadTestHelper> helper( |
127 new base::ThreadTestHelper( | 113 new base::ThreadTestHelper( |
128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
129 // Make sure we wait until the destructor has run. | 115 // Make sure we wait until the destructor has run. |
130 ASSERT_TRUE(helper->Run()); | 116 ASSERT_TRUE(helper->Run()); |
131 store_ = new SQLitePersistentCookieStore( | 117 store_ = new SQLitePersistentCookieStore( |
132 temp_dir_.path().Append(chrome::kCookieFilename)); | 118 temp_dir_.path().Append(chrome::kCookieFilename)); |
133 | 119 |
134 // Reload and test for persistence | 120 // Reload and test for persistence |
135 Load(&cookies); | 121 ASSERT_TRUE(Load(&cookies)); |
136 ASSERT_EQ(1U, cookies.size()); | 122 ASSERT_EQ(1U, cookies.size()); |
137 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); | 123 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); |
138 ASSERT_STREQ("A", cookies[0]->Name().c_str()); | 124 ASSERT_STREQ("A", cookies[0]->Name().c_str()); |
139 ASSERT_STREQ("B", cookies[0]->Value().c_str()); | 125 ASSERT_STREQ("B", cookies[0]->Value().c_str()); |
140 | 126 |
141 // Now delete the cookie and check persistence again. | 127 // Now delete the cookie and check persistence again. |
142 store_->DeleteCookie(*cookies[0]); | 128 store_->DeleteCookie(*cookies[0]); |
143 store_ = NULL; | 129 store_ = NULL; |
144 // Make sure we wait until the destructor has run. | 130 // Make sure we wait until the destructor has run. |
145 ASSERT_TRUE(helper->Run()); | 131 ASSERT_TRUE(helper->Run()); |
146 STLDeleteContainerPointers(cookies.begin(), cookies.end()); | 132 STLDeleteContainerPointers(cookies.begin(), cookies.end()); |
147 cookies.clear(); | 133 cookies.clear(); |
148 store_ = new SQLitePersistentCookieStore( | 134 store_ = new SQLitePersistentCookieStore( |
149 temp_dir_.path().Append(chrome::kCookieFilename)); | 135 temp_dir_.path().Append(chrome::kCookieFilename)); |
150 | 136 |
151 // Reload and check if the cookie has been removed. | 137 // Reload and check if the cookie has been removed. |
152 Load(&cookies); | 138 ASSERT_TRUE(Load(&cookies)); |
153 ASSERT_EQ(0U, cookies.size()); | 139 ASSERT_EQ(0U, cookies.size()); |
154 } | 140 } |
155 | 141 |
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 | |
232 // Test that we can force the database to be written by calling Flush(). | 142 // Test that we can force the database to be written by calling Flush(). |
233 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { | 143 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { |
234 // File timestamps don't work well on all platforms, so we'll determine | 144 // File timestamps don't work well on all platforms, so we'll determine |
235 // whether the DB file has been modified by checking its size. | 145 // whether the DB file has been modified by checking its size. |
236 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); | 146 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); |
237 base::PlatformFileInfo info; | 147 base::PlatformFileInfo info; |
238 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); | 148 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); |
239 int64 base_size = info.size; | 149 int64 base_size = info.size; |
240 | 150 |
241 // Write some large cookies, so the DB will have to expand by several KB. | 151 // 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... |
291 | 201 |
292 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); | 202 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); |
293 | 203 |
294 scoped_refptr<base::ThreadTestHelper> helper( | 204 scoped_refptr<base::ThreadTestHelper> helper( |
295 new base::ThreadTestHelper( | 205 new base::ThreadTestHelper( |
296 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); | 206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); |
297 ASSERT_TRUE(helper->Run()); | 207 ASSERT_TRUE(helper->Run()); |
298 | 208 |
299 ASSERT_EQ(1, counter->callback_count()); | 209 ASSERT_EQ(1, counter->callback_count()); |
300 } | 210 } |
OLD | NEW |