Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: chrome/browser/net/sqlite_persistent_cookie_store_unittest.cc

Issue 7864008: Split initial load of cookies by domains (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
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),
28 event_key_(false, 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 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 event_key_.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 event_.Wait();
41 *cookies = cookies_; 47 *cookies = cookies_;
42 return result; 48 }
49
50 void Sleep() {
51 base::PlatformThread::Sleep(1000);
43 } 52 }
44 53
45 virtual void SetUp() { 54 virtual void SetUp() {
46 ui_thread_.Start(); 55 ui_thread_.Start();
47 db_thread_.Start(); 56 db_thread_.Start();
48 io_thread_.Start(); 57 io_thread_.Start();
49 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 58 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
50 store_ = new SQLitePersistentCookieStore( 59 store_ = new SQLitePersistentCookieStore(
51 temp_dir_.path().Append(chrome::kCookieFilename)); 60 temp_dir_.path().Append(chrome::kCookieFilename));
52 std::vector<net::CookieMonster::CanonicalCookie*> cookies; 61 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
53 ASSERT_TRUE(Load(&cookies)); 62 Load(&cookies);
54 ASSERT_EQ(0u, cookies.size()); 63 ASSERT_EQ(0u, cookies.size());
55 // Make sure the store gets written at least once. 64 // Make sure the store gets written at least once.
56 store_->AddCookie( 65 store_->AddCookie(
57 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar", 66 net::CookieMonster::CanonicalCookie(GURL(), "A", "B", "http://foo.bar",
58 "/", std::string(), std::string(), 67 "/", std::string(), std::string(),
59 base::Time::Now(), 68 base::Time::Now(),
60 base::Time::Now(), 69 base::Time::Now(),
61 base::Time::Now(), 70 base::Time::Now(),
62 false, false, true)); 71 false, false, true));
63 } 72 }
64 73
74 protected:
65 BrowserThread ui_thread_; 75 BrowserThread ui_thread_;
66 BrowserThread db_thread_; 76 BrowserThread db_thread_;
67 BrowserThread io_thread_; 77 BrowserThread io_thread_;
68 base::WaitableEvent event_; 78 base::WaitableEvent event_;
79 base::WaitableEvent event_key_;
69 std::vector<net::CookieMonster::CanonicalCookie*> cookies_; 80 std::vector<net::CookieMonster::CanonicalCookie*> cookies_;
70 ScopedTempDir temp_dir_; 81 ScopedTempDir temp_dir_;
71 scoped_refptr<SQLitePersistentCookieStore> store_; 82 scoped_refptr<SQLitePersistentCookieStore> store_;
72 }; 83 };
73 84
74 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) { 85 TEST_F(SQLitePersistentCookieStoreTest, KeepOnDestruction) {
75 store_->SetClearLocalStateOnExit(false); 86 store_->SetClearLocalStateOnExit(false);
76 store_ = NULL; 87 store_ = NULL;
77 // Make sure we wait until the destructor has run. 88 // Make sure we wait until the destructor has run.
78 scoped_refptr<base::ThreadTestHelper> helper( 89 scoped_refptr<base::ThreadTestHelper> helper(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 store_ = NULL; 122 store_ = NULL;
112 scoped_refptr<base::ThreadTestHelper> helper( 123 scoped_refptr<base::ThreadTestHelper> helper(
113 new base::ThreadTestHelper( 124 new base::ThreadTestHelper(
114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); 125 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
115 // Make sure we wait until the destructor has run. 126 // Make sure we wait until the destructor has run.
116 ASSERT_TRUE(helper->Run()); 127 ASSERT_TRUE(helper->Run());
117 store_ = new SQLitePersistentCookieStore( 128 store_ = new SQLitePersistentCookieStore(
118 temp_dir_.path().Append(chrome::kCookieFilename)); 129 temp_dir_.path().Append(chrome::kCookieFilename));
119 130
120 // Reload and test for persistence 131 // Reload and test for persistence
121 ASSERT_TRUE(Load(&cookies)); 132 Load(&cookies);
122 ASSERT_EQ(1U, cookies.size()); 133 ASSERT_EQ(1U, cookies.size());
123 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str()); 134 ASSERT_STREQ("http://foo.bar", cookies[0]->Domain().c_str());
124 ASSERT_STREQ("A", cookies[0]->Name().c_str()); 135 ASSERT_STREQ("A", cookies[0]->Name().c_str());
125 ASSERT_STREQ("B", cookies[0]->Value().c_str()); 136 ASSERT_STREQ("B", cookies[0]->Value().c_str());
126 137
127 // Now delete the cookie and check persistence again. 138 // Now delete the cookie and check persistence again.
128 store_->DeleteCookie(*cookies[0]); 139 store_->DeleteCookie(*cookies[0]);
129 store_ = NULL; 140 store_ = NULL;
130 // Make sure we wait until the destructor has run. 141 // Make sure we wait until the destructor has run.
131 ASSERT_TRUE(helper->Run()); 142 ASSERT_TRUE(helper->Run());
132 STLDeleteContainerPointers(cookies.begin(), cookies.end()); 143 STLDeleteContainerPointers(cookies.begin(), cookies.end());
133 cookies.clear(); 144 cookies.clear();
134 store_ = new SQLitePersistentCookieStore( 145 store_ = new SQLitePersistentCookieStore(
135 temp_dir_.path().Append(chrome::kCookieFilename)); 146 temp_dir_.path().Append(chrome::kCookieFilename));
136 147
137 // Reload and check if the cookie has been removed. 148 // Reload and check if the cookie has been removed.
138 ASSERT_TRUE(Load(&cookies)); 149 Load(&cookies);
139 ASSERT_EQ(0U, cookies.size()); 150 ASSERT_EQ(0U, cookies.size());
140 } 151 }
141 152
153 // Test that priority load of cookies for a specfic domain key could be
erikwright (departed) 2011/10/04 19:55:02 This test is very clever and effective. In general
guohui 2011/10/06 15:40:00 Done.
154 // completed before the entire store is loaded
155 TEST_F(SQLitePersistentCookieStoreTest, TestLoadCookiesForKey) {
156 std::vector<net::CookieMonster::CanonicalCookie*> cookies;
157 base::Time t = base::Time::Now() + base::TimeDelta::FromInternalValue(10);
158 store_->AddCookie(
159 net::CookieMonster::CanonicalCookie(GURL(), "A", "B",
160 "www.aaa.com", "/",
erikwright (departed) 2011/10/04 19:55:02 Add a note that "a foo.bar cookie was already adde
guohui 2011/10/06 15:40:00 Done.
161 std::string(), std::string(),
162 t, t, t, false, false, true));
163 t += base::TimeDelta::FromInternalValue(10);
164 store_->AddCookie(
165 net::CookieMonster::CanonicalCookie(GURL(), "A", "B",
166 "www.bbb.com", "/",
167 std::string(), std::string(),
168 t, t, t, false, false, true));
169 store_ = NULL;
170
171 scoped_refptr<base::ThreadTestHelper> helper(
172 new base::ThreadTestHelper(
173 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
174 // Make sure we wait until the destructor has run.
175 ASSERT_TRUE(helper->Run());
176
177 store_ = new SQLitePersistentCookieStore(
178 temp_dir_.path().Append(chrome::kCookieFilename));
179 db_thread_.PostTask(BrowserThread::DB, FROM_HERE,
180 base::Bind(&SQLitePersistentCookieStoreTest::Sleep,
181 base::Unretained(this)));
182
183 store_->Load(base::Bind(&SQLitePersistentCookieStoreTest::OnLoaded,
184 base::Unretained(this)));
185
186 store_->LoadCookiesForKey("aaa.com",
187 base::Bind(&SQLitePersistentCookieStoreTest::OnKeyLoaded,
188 base::Unretained(this)));
189 db_thread_.PostTask(BrowserThread::DB, FROM_HERE,
190 base::Bind(&SQLitePersistentCookieStoreTest::Sleep,
191 base::Unretained(this)));
192
193 event_key_.Wait();
194 ASSERT_EQ(event_.IsSignaled(), false);
erikwright (departed) 2011/10/04 19:55:02 If the order is guaranteed to be the order of crea
guohui 2011/10/06 15:40:00 Done.
195 ASSERT_EQ(2U, cookies_.size());
196 ASSERT_STREQ("http://foo.bar", cookies_[0]->Domain().c_str());
197 ASSERT_STREQ("www.aaa.com", cookies_[1]->Domain().c_str());
198
199 event_.Wait();
200 ASSERT_EQ(1U, cookies_.size());
201 ASSERT_STREQ("www.bbb.com", cookies_[0]->Domain().c_str());
202 // Reload and te
erikwright (departed) 2011/10/04 19:55:02 Remove or correct the comment on line 202.
guohui 2011/10/06 15:40:00 Done.
203 }
204
142 // Test that we can force the database to be written by calling Flush(). 205 // Test that we can force the database to be written by calling Flush().
143 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) { 206 TEST_F(SQLitePersistentCookieStoreTest, TestFlush) {
144 // File timestamps don't work well on all platforms, so we'll determine 207 // 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. 208 // whether the DB file has been modified by checking its size.
146 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename); 209 FilePath path = temp_dir_.path().Append(chrome::kCookieFilename);
147 base::PlatformFileInfo info; 210 base::PlatformFileInfo info;
148 ASSERT_TRUE(file_util::GetFileInfo(path, &info)); 211 ASSERT_TRUE(file_util::GetFileInfo(path, &info));
149 int64 base_size = info.size; 212 int64 base_size = info.size;
150 213
151 // Write some large cookies, so the DB will have to expand by several KB. 214 // 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
201 264
202 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback)); 265 store_->Flush(NewRunnableMethod(counter.get(), &CallbackCounter::Callback));
203 266
204 scoped_refptr<base::ThreadTestHelper> helper( 267 scoped_refptr<base::ThreadTestHelper> helper(
205 new base::ThreadTestHelper( 268 new base::ThreadTestHelper(
206 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB))); 269 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB)));
207 ASSERT_TRUE(helper->Run()); 270 ASSERT_TRUE(helper->Run());
208 271
209 ASSERT_EQ(1, counter->callback_count()); 272 ASSERT_EQ(1, counter->callback_count());
210 } 273 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698