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 "chrome/browser/browsing_data/browsing_data_cookie_helper.h" | 5 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/run_loop.h" |
9 #include "chrome/test/base/testing_profile.h" | 9 #include "chrome/test/base/testing_profile.h" |
10 #include "content/public/test/test_browser_thread.h" | |
11 #include "content/public/test/test_browser_thread_bundle.h" | 10 #include "content/public/test/test_browser_thread_bundle.h" |
12 #include "net/cookies/canonical_cookie.h" | 11 #include "net/cookies/canonical_cookie.h" |
13 #include "net/cookies/parsed_cookie.h" | 12 #include "net/cookies/parsed_cookie.h" |
14 #include "net/url_request/url_request_context_getter.h" | 13 #include "net/url_request/url_request_context_getter.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
16 | 15 |
17 using content::BrowserThread; | |
18 | |
19 namespace { | 16 namespace { |
20 | 17 |
21 class BrowsingDataCookieHelperTest : public testing::Test { | 18 class BrowsingDataCookieHelperTest : public testing::Test { |
22 public: | 19 public: |
23 void SetUpOnIOThread() { | 20 BrowsingDataCookieHelperTest() |
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 21 : testing_profile_(new TestingProfile()) { |
25 // This is a workaround for a bug in the TestingProfile. | |
26 // The URLRequestContext will be created by GetCookieMonster on the UI | |
27 // thread, if it does not already exist. But it must be created on the IO | |
28 // thread or else it will DCHECK upon destruction. | |
29 // Force it to be created here. | |
30 testing_profile_->CreateRequestContext(); | |
31 testing_profile_->GetRequestContext()->GetURLRequestContext(); | |
32 } | |
33 | |
34 virtual void SetUp() { | |
35 testing_profile_.reset(new TestingProfile()); | |
36 BrowserThread::PostTask( | |
37 BrowserThread::IO, FROM_HERE, | |
38 base::Bind(&BrowsingDataCookieHelperTest::SetUpOnIOThread, | |
39 base::Unretained(this))); | |
40 base::MessageLoop::current()->RunUntilIdle(); | |
41 } | |
42 | |
43 virtual void TearDown() { | |
44 // This must be reset before the IO thread stops, because the | |
45 // URLRequestContextGetter forces its own deletion to occur on that thread. | |
46 testing_profile_->ResetRequestContext(); | |
47 } | 22 } |
48 | 23 |
49 void CreateCookiesForTest() { | 24 void CreateCookiesForTest() { |
50 scoped_refptr<net::CookieMonster> cookie_monster = | 25 scoped_refptr<net::CookieMonster> cookie_monster = |
51 testing_profile_->GetCookieMonster(); | 26 testing_profile_->GetCookieMonster(); |
52 cookie_monster->SetCookieWithOptionsAsync( | 27 cookie_monster->SetCookieWithOptionsAsync( |
53 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | 28 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
54 net::CookieMonster::SetCookiesCallback()); | 29 net::CookieMonster::SetCookiesCallback()); |
55 cookie_monster->SetCookieWithOptionsAsync( | 30 cookie_monster->SetCookieWithOptionsAsync( |
56 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), | 31 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), |
(...skipping 19 matching lines...) Expand all Loading... |
76 // Correct because fetching cookies will get a sorted cookie list. | 51 // Correct because fetching cookies will get a sorted cookie list. |
77 ASSERT_TRUE(it != cookies.end()); | 52 ASSERT_TRUE(it != cookies.end()); |
78 EXPECT_EQ("www.google.com", it->Domain()); | 53 EXPECT_EQ("www.google.com", it->Domain()); |
79 EXPECT_EQ("A", it->Name()); | 54 EXPECT_EQ("A", it->Name()); |
80 | 55 |
81 ASSERT_TRUE(++it != cookies.end()); | 56 ASSERT_TRUE(++it != cookies.end()); |
82 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 57 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
83 EXPECT_EQ("B", it->Name()); | 58 EXPECT_EQ("B", it->Name()); |
84 | 59 |
85 ASSERT_TRUE(++it == cookies.end()); | 60 ASSERT_TRUE(++it == cookies.end()); |
86 base::MessageLoop::current()->Quit(); | |
87 } | 61 } |
88 | 62 |
89 void DomainCookieCallback(const net::CookieList& cookies) { | 63 void DomainCookieCallback(const net::CookieList& cookies) { |
90 ASSERT_EQ(2UL, cookies.size()); | 64 ASSERT_EQ(2UL, cookies.size()); |
91 cookie_list_ = cookies; | 65 cookie_list_ = cookies; |
92 net::CookieList::const_iterator it = cookies.begin(); | 66 net::CookieList::const_iterator it = cookies.begin(); |
93 | 67 |
94 // Correct because fetching cookies will get a sorted cookie list. | 68 // Correct because fetching cookies will get a sorted cookie list. |
95 ASSERT_TRUE(it != cookies.end()); | 69 ASSERT_TRUE(it != cookies.end()); |
96 EXPECT_EQ("www.google.com", it->Domain()); | 70 EXPECT_EQ("www.google.com", it->Domain()); |
97 EXPECT_EQ("A", it->Name()); | 71 EXPECT_EQ("A", it->Name()); |
98 EXPECT_EQ("1", it->Value()); | 72 EXPECT_EQ("1", it->Value()); |
99 | 73 |
100 ASSERT_TRUE(++it != cookies.end()); | 74 ASSERT_TRUE(++it != cookies.end()); |
101 EXPECT_EQ(".www.google.com", it->Domain()); | 75 EXPECT_EQ(".www.google.com", it->Domain()); |
102 EXPECT_EQ("A", it->Name()); | 76 EXPECT_EQ("A", it->Name()); |
103 EXPECT_EQ("2", it->Value()); | 77 EXPECT_EQ("2", it->Value()); |
104 | 78 |
105 ASSERT_TRUE(++it == cookies.end()); | 79 ASSERT_TRUE(++it == cookies.end()); |
106 base::MessageLoop::current()->Quit(); | |
107 } | 80 } |
108 | 81 |
109 void DeleteCallback(const net::CookieList& cookies) { | 82 void DeleteCallback(const net::CookieList& cookies) { |
110 ASSERT_EQ(1UL, cookies.size()); | 83 ASSERT_EQ(1UL, cookies.size()); |
111 net::CookieList::const_iterator it = cookies.begin(); | 84 net::CookieList::const_iterator it = cookies.begin(); |
112 | 85 |
113 ASSERT_TRUE(it != cookies.end()); | 86 ASSERT_TRUE(it != cookies.end()); |
114 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 87 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
115 EXPECT_EQ("B", it->Name()); | 88 EXPECT_EQ("B", it->Name()); |
116 | 89 |
117 ASSERT_TRUE(++it == cookies.end()); | 90 ASSERT_TRUE(++it == cookies.end()); |
118 base::MessageLoop::current()->Quit(); | |
119 } | 91 } |
120 | 92 |
121 void CannedUniqueCallback(const net::CookieList& cookies) { | 93 void CannedUniqueCallback(const net::CookieList& cookies) { |
122 EXPECT_EQ(1UL, cookies.size()); | 94 EXPECT_EQ(1UL, cookies.size()); |
123 cookie_list_ = cookies; | 95 cookie_list_ = cookies; |
124 net::CookieList::const_iterator it = cookies.begin(); | 96 net::CookieList::const_iterator it = cookies.begin(); |
125 | 97 |
126 ASSERT_TRUE(it != cookies.end()); | 98 ASSERT_TRUE(it != cookies.end()); |
127 EXPECT_EQ("http://www.google.com/", it->Source()); | 99 EXPECT_EQ("http://www.google.com/", it->Source()); |
128 EXPECT_EQ("www.google.com", it->Domain()); | 100 EXPECT_EQ("www.google.com", it->Domain()); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 }; | 177 }; |
206 | 178 |
207 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | 179 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
208 CreateCookiesForTest(); | 180 CreateCookiesForTest(); |
209 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 181 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
210 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 182 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
211 | 183 |
212 cookie_helper->StartFetching( | 184 cookie_helper->StartFetching( |
213 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 185 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
214 base::Unretained(this))); | 186 base::Unretained(this))); |
215 | 187 base::RunLoop().RunUntilIdle(); |
216 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | |
217 base::MessageLoop::current()->Run(); | |
218 } | 188 } |
219 | 189 |
220 TEST_F(BrowsingDataCookieHelperTest, DomainCookie) { | 190 TEST_F(BrowsingDataCookieHelperTest, DomainCookie) { |
221 CreateCookiesForDomainCookieTest(); | 191 CreateCookiesForDomainCookieTest(); |
222 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 192 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
223 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 193 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
224 | 194 |
225 cookie_helper->StartFetching( | 195 cookie_helper->StartFetching( |
226 base::Bind(&BrowsingDataCookieHelperTest::DomainCookieCallback, | 196 base::Bind(&BrowsingDataCookieHelperTest::DomainCookieCallback, |
227 base::Unretained(this))); | 197 base::Unretained(this))); |
228 | 198 base::RunLoop().RunUntilIdle(); |
229 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | |
230 base::MessageLoop::current()->Run(); | |
231 } | 199 } |
232 | 200 |
233 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { | 201 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
234 CreateCookiesForTest(); | 202 CreateCookiesForTest(); |
235 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 203 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
236 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 204 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
237 | 205 |
238 cookie_helper->StartFetching( | 206 cookie_helper->StartFetching( |
239 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 207 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
240 base::Unretained(this))); | 208 base::Unretained(this))); |
241 | 209 base::RunLoop().RunUntilIdle(); |
242 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | |
243 base::MessageLoop::current()->Run(); | |
244 | 210 |
245 net::CanonicalCookie cookie = cookie_list_[0]; | 211 net::CanonicalCookie cookie = cookie_list_[0]; |
246 cookie_helper->DeleteCookie(cookie); | 212 cookie_helper->DeleteCookie(cookie); |
247 | 213 |
248 cookie_helper->StartFetching( | 214 cookie_helper->StartFetching( |
249 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, | 215 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
250 base::Unretained(this))); | 216 base::Unretained(this))); |
251 base::MessageLoop::current()->Run(); | 217 base::RunLoop().RunUntilIdle(); |
252 } | 218 } |
253 | 219 |
254 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) { | 220 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) { |
255 const GURL origin("http://www.google.com"); | 221 const GURL origin("http://www.google.com"); |
256 net::CookieList cookie; | 222 net::CookieList cookie; |
257 | 223 |
258 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | 224 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
259 new CannedBrowsingDataCookieHelper( | 225 new CannedBrowsingDataCookieHelper( |
260 testing_profile_->GetRequestContext())); | 226 testing_profile_->GetRequestContext())); |
261 | 227 |
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 // "A=1; | 434 // "A=1; |
469 // "A=3; Domain=www.google.com" | 435 // "A=3; Domain=www.google.com" |
470 // Add a domain cookie and check if it increases the cookie count. | 436 // Add a domain cookie and check if it increases the cookie count. |
471 helper->AddChangedCookie(frame2_url, frame1_url, | 437 helper->AddChangedCookie(frame2_url, frame1_url, |
472 cookie_pair4 + "; Domain=" + cookie_domain, | 438 cookie_pair4 + "; Domain=" + cookie_domain, |
473 net::CookieOptions()); | 439 net::CookieOptions()); |
474 EXPECT_EQ(5U, helper->GetCookieCount()); | 440 EXPECT_EQ(5U, helper->GetCookieCount()); |
475 } | 441 } |
476 | 442 |
477 } // namespace | 443 } // namespace |
OLD | NEW |