| 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 | 7 |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 scoped_refptr<net::CookieMonster> cookie_monster = | 61 scoped_refptr<net::CookieMonster> cookie_monster = |
| 62 testing_profile_->GetCookieMonster(); | 62 testing_profile_->GetCookieMonster(); |
| 63 cookie_monster->SetCookieWithOptionsAsync( | 63 cookie_monster->SetCookieWithOptionsAsync( |
| 64 GURL("http://www.google.com"), "A=1", net::CookieOptions(), | 64 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
| 65 net::CookieMonster::SetCookiesCallback()); | 65 net::CookieMonster::SetCookiesCallback()); |
| 66 cookie_monster->SetCookieWithOptionsAsync( | 66 cookie_monster->SetCookieWithOptionsAsync( |
| 67 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), | 67 GURL("http://www.gmail.google.com"), "B=1", net::CookieOptions(), |
| 68 net::CookieMonster::SetCookiesCallback()); | 68 net::CookieMonster::SetCookiesCallback()); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void CreateCookiesForDomainCookieTest() { |
| 72 scoped_refptr<net::CookieMonster> cookie_monster = |
| 73 testing_profile_->GetCookieMonster(); |
| 74 cookie_monster->SetCookieWithOptionsAsync( |
| 75 GURL("http://www.google.com"), "A=1", net::CookieOptions(), |
| 76 net::CookieMonster::SetCookiesCallback()); |
| 77 cookie_monster->SetCookieWithOptionsAsync( |
| 78 GURL("http://www.google.com"), "A=2; Domain=.www.google.com ", |
| 79 net::CookieOptions(), net::CookieMonster::SetCookiesCallback()); |
| 80 } |
| 81 |
| 71 void FetchCallback(const net::CookieList& cookies) { | 82 void FetchCallback(const net::CookieList& cookies) { |
| 72 ASSERT_EQ(2UL, cookies.size()); | 83 ASSERT_EQ(2UL, cookies.size()); |
| 73 cookie_list_ = cookies; | 84 cookie_list_ = cookies; |
| 74 net::CookieList::const_iterator it = cookies.begin(); | 85 net::CookieList::const_iterator it = cookies.begin(); |
| 75 | 86 |
| 76 // Correct because fetching cookies will get a sorted cookie list. | 87 // Correct because fetching cookies will get a sorted cookie list. |
| 77 ASSERT_TRUE(it != cookies.end()); | 88 ASSERT_TRUE(it != cookies.end()); |
| 78 EXPECT_EQ("www.google.com", it->Domain()); | 89 EXPECT_EQ("www.google.com", it->Domain()); |
| 79 EXPECT_EQ("A", it->Name()); | 90 EXPECT_EQ("A", it->Name()); |
| 80 | 91 |
| 81 ASSERT_TRUE(++it != cookies.end()); | 92 ASSERT_TRUE(++it != cookies.end()); |
| 82 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 93 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 83 EXPECT_EQ("B", it->Name()); | 94 EXPECT_EQ("B", it->Name()); |
| 84 | 95 |
| 85 ASSERT_TRUE(++it == cookies.end()); | 96 ASSERT_TRUE(++it == cookies.end()); |
| 86 MessageLoop::current()->Quit(); | 97 MessageLoop::current()->Quit(); |
| 87 } | 98 } |
| 88 | 99 |
| 100 void DomainCookieCallback(const net::CookieList& cookies) { |
| 101 ASSERT_EQ(2UL, cookies.size()); |
| 102 cookie_list_ = cookies; |
| 103 net::CookieList::const_iterator it = cookies.begin(); |
| 104 |
| 105 // Correct because fetching cookies will get a sorted cookie list. |
| 106 ASSERT_TRUE(it != cookies.end()); |
| 107 EXPECT_EQ("www.google.com", it->Domain()); |
| 108 EXPECT_EQ("A", it->Name()); |
| 109 EXPECT_EQ("1", it->Value()); |
| 110 |
| 111 ASSERT_TRUE(++it != cookies.end()); |
| 112 EXPECT_EQ(".www.google.com", it->Domain()); |
| 113 EXPECT_EQ("A", it->Name()); |
| 114 EXPECT_EQ("2", it->Value()); |
| 115 |
| 116 ASSERT_TRUE(++it == cookies.end()); |
| 117 MessageLoop::current()->Quit(); |
| 118 } |
| 119 |
| 89 void DeleteCallback(const net::CookieList& cookies) { | 120 void DeleteCallback(const net::CookieList& cookies) { |
| 90 ASSERT_EQ(1UL, cookies.size()); | 121 ASSERT_EQ(1UL, cookies.size()); |
| 91 net::CookieList::const_iterator it = cookies.begin(); | 122 net::CookieList::const_iterator it = cookies.begin(); |
| 92 | 123 |
| 93 ASSERT_TRUE(it != cookies.end()); | 124 ASSERT_TRUE(it != cookies.end()); |
| 94 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 125 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
| 95 EXPECT_EQ("B", it->Name()); | 126 EXPECT_EQ("B", it->Name()); |
| 96 | 127 |
| 97 ASSERT_TRUE(++it == cookies.end()); | 128 ASSERT_TRUE(++it == cookies.end()); |
| 98 MessageLoop::current()->Quit(); | 129 MessageLoop::current()->Quit(); |
| 99 } | 130 } |
| 100 | 131 |
| 101 void CannedUniqueCallback(const net::CookieList& cookies) { | 132 void CannedUniqueCallback(const net::CookieList& cookies) { |
| 102 ASSERT_EQ(1UL, cookies.size()); | 133 ASSERT_EQ(1UL, cookies.size()); |
| 103 cookie_list_ = cookies; | 134 cookie_list_ = cookies; |
| 104 net::CookieList::const_iterator it = cookies.begin(); | 135 net::CookieList::const_iterator it = cookies.begin(); |
| 105 | 136 |
| 106 ASSERT_TRUE(it != cookies.end()); | 137 ASSERT_TRUE(it != cookies.end()); |
| 107 EXPECT_EQ("http://www.google.com/", it->Source()); | 138 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 108 EXPECT_EQ("A", it->Name()); | 139 EXPECT_EQ("A", it->Name()); |
| 109 | 140 |
| 110 ASSERT_TRUE(++it == cookies.end()); | 141 ASSERT_TRUE(++it == cookies.end()); |
| 111 } | 142 } |
| 112 | 143 |
| 144 void CannedDomainCookieCallback(const net::CookieList& cookies) { |
| 145 ASSERT_EQ(2UL, cookies.size()); |
| 146 cookie_list_ = cookies; |
| 147 net::CookieList::const_iterator it = cookies.begin(); |
| 148 |
| 149 ASSERT_TRUE(it != cookies.end()); |
| 150 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 151 EXPECT_EQ("A", it->Name()); |
| 152 EXPECT_EQ("www.google.com", it->Domain()); |
| 153 |
| 154 ASSERT_TRUE(++it != cookies.end()); |
| 155 EXPECT_EQ("http://www.google.com/", it->Source()); |
| 156 EXPECT_EQ("A", it->Name()); |
| 157 EXPECT_EQ(".www.google.com", it->Domain()); |
| 158 |
| 159 ASSERT_TRUE(++it == cookies.end()); |
| 160 } |
| 161 |
| 113 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { | 162 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { |
| 114 ASSERT_EQ(3U, cookie_list.size()); | 163 ASSERT_EQ(3U, cookie_list.size()); |
| 115 } | 164 } |
| 116 | 165 |
| 117 protected: | 166 protected: |
| 118 MessageLoop message_loop_; | 167 MessageLoop message_loop_; |
| 119 scoped_ptr<content::TestBrowserThread> ui_thread_; | 168 scoped_ptr<content::TestBrowserThread> ui_thread_; |
| 120 scoped_ptr<content::TestBrowserThread> io_thread_; | 169 scoped_ptr<content::TestBrowserThread> io_thread_; |
| 121 scoped_ptr<TestingProfile> testing_profile_; | 170 scoped_ptr<TestingProfile> testing_profile_; |
| 122 | 171 |
| 123 net::CookieList cookie_list_; | 172 net::CookieList cookie_list_; |
| 124 }; | 173 }; |
| 125 | 174 |
| 126 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | 175 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
| 127 CreateCookiesForTest(); | 176 CreateCookiesForTest(); |
| 128 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 177 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 129 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 178 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
| 130 | 179 |
| 131 cookie_helper->StartFetching( | 180 cookie_helper->StartFetching( |
| 132 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 181 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 133 base::Unretained(this))); | 182 base::Unretained(this))); |
| 134 | 183 |
| 135 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | 184 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
| 136 MessageLoop::current()->Run(); | 185 MessageLoop::current()->Run(); |
| 137 } | 186 } |
| 138 | 187 |
| 188 TEST_F(BrowsingDataCookieHelperTest, DomainCookie) { |
| 189 CreateCookiesForDomainCookieTest(); |
| 190 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 191 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
| 192 |
| 193 cookie_helper->StartFetching( |
| 194 base::Bind(&BrowsingDataCookieHelperTest::DomainCookieCallback, |
| 195 base::Unretained(this))); |
| 196 |
| 197 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
| 198 MessageLoop::current()->Run(); |
| 199 } |
| 200 |
| 139 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { | 201 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
| 140 CreateCookiesForTest(); | 202 CreateCookiesForTest(); |
| 141 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 203 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
| 142 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 204 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
| 143 | 205 |
| 144 cookie_helper->StartFetching( | 206 cookie_helper->StartFetching( |
| 145 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 207 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
| 146 base::Unretained(this))); | 208 base::Unretained(this))); |
| 147 | 209 |
| 148 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | 210 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
| 149 MessageLoop::current()->Run(); | 211 MessageLoop::current()->Run(); |
| 150 | 212 |
| 151 net::CanonicalCookie cookie = cookie_list_[0]; | 213 net::CanonicalCookie cookie = cookie_list_[0]; |
| 152 cookie_helper->DeleteCookie(cookie); | 214 cookie_helper->DeleteCookie(cookie); |
| 153 | 215 |
| 154 cookie_helper->StartFetching( | 216 cookie_helper->StartFetching( |
| 155 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, | 217 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
| 156 base::Unretained(this))); | 218 base::Unretained(this))); |
| 157 MessageLoop::current()->Run(); | 219 MessageLoop::current()->Run(); |
| 158 } | 220 } |
| 159 | 221 |
| 222 TEST_F(BrowsingDataCookieHelperTest, CannedDomainCookie) { |
| 223 const GURL origin("http://www.google.com"); |
| 224 net::CookieList cookie; |
| 225 |
| 226 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 227 new CannedBrowsingDataCookieHelper( |
| 228 testing_profile_->GetRequestContext())); |
| 229 |
| 230 ASSERT_TRUE(helper->empty()); |
| 231 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); |
| 232 helper->AddChangedCookie(origin, origin, "A=1; Domain=.www.google.com", |
| 233 net::CookieOptions()); |
| 234 |
| 235 helper->StartFetching( |
| 236 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback, |
| 237 base::Unretained(this))); |
| 238 cookie = cookie_list_; |
| 239 |
| 240 helper->Reset(); |
| 241 ASSERT_TRUE(helper->empty()); |
| 242 |
| 243 helper->AddReadCookies(origin, origin, cookie); |
| 244 helper->StartFetching( |
| 245 base::Bind(&BrowsingDataCookieHelperTest::CannedDomainCookieCallback, |
| 246 base::Unretained(this))); |
| 247 } |
| 248 |
| 160 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { | 249 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
| 161 const GURL origin("http://www.google.com"); | 250 const GURL origin("http://www.google.com"); |
| 162 net::CookieList cookie; | 251 net::CookieList cookie; |
| 163 | 252 |
| 164 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | 253 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
| 165 new CannedBrowsingDataCookieHelper( | 254 new CannedBrowsingDataCookieHelper( |
| 166 testing_profile_->GetRequestContext())); | 255 testing_profile_->GetRequestContext())); |
| 167 | 256 |
| 168 ASSERT_TRUE(helper->empty()); | 257 ASSERT_TRUE(helper->empty()); |
| 169 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); | 258 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 net::CookieOptions()); | 313 net::CookieOptions()); |
| 225 helper->AddChangedCookie(frame2_url, request_url, "c=1", | 314 helper->AddChangedCookie(frame2_url, request_url, "c=1", |
| 226 net::CookieOptions()); | 315 net::CookieOptions()); |
| 227 | 316 |
| 228 helper->StartFetching( | 317 helper->StartFetching( |
| 229 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback, | 318 base::Bind(&BrowsingDataCookieHelperTest::CannedDifferentFramesCallback, |
| 230 base::Unretained(this))); | 319 base::Unretained(this))); |
| 231 } | 320 } |
| 232 | 321 |
| 233 } // namespace | 322 } // namespace |
| OLD | NEW |