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 | |
82 void FetchCallback(const net::CookieList& cookies) { | 71 void FetchCallback(const net::CookieList& cookies) { |
83 ASSERT_EQ(2UL, cookies.size()); | 72 ASSERT_EQ(2UL, cookies.size()); |
84 cookie_list_ = cookies; | 73 cookie_list_ = cookies; |
85 net::CookieList::const_iterator it = cookies.begin(); | 74 net::CookieList::const_iterator it = cookies.begin(); |
86 | 75 |
87 // Correct because fetching cookies will get a sorted cookie list. | 76 // Correct because fetching cookies will get a sorted cookie list. |
88 ASSERT_TRUE(it != cookies.end()); | 77 ASSERT_TRUE(it != cookies.end()); |
89 EXPECT_EQ("www.google.com", it->Domain()); | 78 EXPECT_EQ("www.google.com", it->Domain()); |
90 EXPECT_EQ("A", it->Name()); | 79 EXPECT_EQ("A", it->Name()); |
91 | 80 |
92 ASSERT_TRUE(++it != cookies.end()); | 81 ASSERT_TRUE(++it != cookies.end()); |
93 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 82 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
94 EXPECT_EQ("B", it->Name()); | 83 EXPECT_EQ("B", it->Name()); |
95 | 84 |
96 ASSERT_TRUE(++it == cookies.end()); | 85 ASSERT_TRUE(++it == cookies.end()); |
97 MessageLoop::current()->Quit(); | 86 MessageLoop::current()->Quit(); |
98 } | 87 } |
99 | 88 |
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 | |
120 void DeleteCallback(const net::CookieList& cookies) { | 89 void DeleteCallback(const net::CookieList& cookies) { |
121 ASSERT_EQ(1UL, cookies.size()); | 90 ASSERT_EQ(1UL, cookies.size()); |
122 net::CookieList::const_iterator it = cookies.begin(); | 91 net::CookieList::const_iterator it = cookies.begin(); |
123 | 92 |
124 ASSERT_TRUE(it != cookies.end()); | 93 ASSERT_TRUE(it != cookies.end()); |
125 EXPECT_EQ("www.gmail.google.com", it->Domain()); | 94 EXPECT_EQ("www.gmail.google.com", it->Domain()); |
126 EXPECT_EQ("B", it->Name()); | 95 EXPECT_EQ("B", it->Name()); |
127 | 96 |
128 ASSERT_TRUE(++it == cookies.end()); | 97 ASSERT_TRUE(++it == cookies.end()); |
129 MessageLoop::current()->Quit(); | 98 MessageLoop::current()->Quit(); |
130 } | 99 } |
131 | 100 |
132 void CannedUniqueCallback(const net::CookieList& cookies) { | 101 void CannedUniqueCallback(const net::CookieList& cookies) { |
133 ASSERT_EQ(1UL, cookies.size()); | 102 ASSERT_EQ(1UL, cookies.size()); |
134 cookie_list_ = cookies; | 103 cookie_list_ = cookies; |
135 net::CookieList::const_iterator it = cookies.begin(); | 104 net::CookieList::const_iterator it = cookies.begin(); |
136 | 105 |
137 ASSERT_TRUE(it != cookies.end()); | 106 ASSERT_TRUE(it != cookies.end()); |
138 EXPECT_EQ("http://www.google.com/", it->Source()); | 107 EXPECT_EQ("http://www.google.com/", it->Source()); |
139 EXPECT_EQ("A", it->Name()); | 108 EXPECT_EQ("A", it->Name()); |
140 | 109 |
141 ASSERT_TRUE(++it == cookies.end()); | 110 ASSERT_TRUE(++it == cookies.end()); |
142 } | 111 } |
143 | 112 |
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 | |
162 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { | 113 void CannedDifferentFramesCallback(const net::CookieList& cookie_list) { |
163 ASSERT_EQ(3U, cookie_list.size()); | 114 ASSERT_EQ(3U, cookie_list.size()); |
164 } | 115 } |
165 | 116 |
166 protected: | 117 protected: |
167 MessageLoop message_loop_; | 118 MessageLoop message_loop_; |
168 scoped_ptr<content::TestBrowserThread> ui_thread_; | 119 scoped_ptr<content::TestBrowserThread> ui_thread_; |
169 scoped_ptr<content::TestBrowserThread> io_thread_; | 120 scoped_ptr<content::TestBrowserThread> io_thread_; |
170 scoped_ptr<TestingProfile> testing_profile_; | 121 scoped_ptr<TestingProfile> testing_profile_; |
171 | 122 |
172 net::CookieList cookie_list_; | 123 net::CookieList cookie_list_; |
173 }; | 124 }; |
174 | 125 |
175 TEST_F(BrowsingDataCookieHelperTest, FetchData) { | 126 TEST_F(BrowsingDataCookieHelperTest, FetchData) { |
176 CreateCookiesForTest(); | 127 CreateCookiesForTest(); |
177 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 128 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
178 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 129 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
179 | 130 |
180 cookie_helper->StartFetching( | 131 cookie_helper->StartFetching( |
181 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 132 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
182 base::Unretained(this))); | 133 base::Unretained(this))); |
183 | 134 |
184 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | 135 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
185 MessageLoop::current()->Run(); | 136 MessageLoop::current()->Run(); |
186 } | 137 } |
187 | 138 |
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 | |
201 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { | 139 TEST_F(BrowsingDataCookieHelperTest, DeleteCookie) { |
202 CreateCookiesForTest(); | 140 CreateCookiesForTest(); |
203 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( | 141 scoped_refptr<BrowsingDataCookieHelper> cookie_helper( |
204 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); | 142 new BrowsingDataCookieHelper(testing_profile_->GetRequestContext())); |
205 | 143 |
206 cookie_helper->StartFetching( | 144 cookie_helper->StartFetching( |
207 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, | 145 base::Bind(&BrowsingDataCookieHelperTest::FetchCallback, |
208 base::Unretained(this))); | 146 base::Unretained(this))); |
209 | 147 |
210 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. | 148 // Blocks until BrowsingDataCookieHelperTest::FetchCallback is notified. |
211 MessageLoop::current()->Run(); | 149 MessageLoop::current()->Run(); |
212 | 150 |
213 net::CanonicalCookie cookie = cookie_list_[0]; | 151 net::CanonicalCookie cookie = cookie_list_[0]; |
214 cookie_helper->DeleteCookie(cookie); | 152 cookie_helper->DeleteCookie(cookie); |
215 | 153 |
216 cookie_helper->StartFetching( | 154 cookie_helper->StartFetching( |
217 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, | 155 base::Bind(&BrowsingDataCookieHelperTest::DeleteCallback, |
218 base::Unretained(this))); | 156 base::Unretained(this))); |
219 MessageLoop::current()->Run(); | 157 MessageLoop::current()->Run(); |
220 } | 158 } |
221 | 159 |
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 | |
249 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { | 160 TEST_F(BrowsingDataCookieHelperTest, CannedUnique) { |
250 const GURL origin("http://www.google.com"); | 161 const GURL origin("http://www.google.com"); |
251 net::CookieList cookie; | 162 net::CookieList cookie; |
252 | 163 |
253 scoped_refptr<CannedBrowsingDataCookieHelper> helper( | 164 scoped_refptr<CannedBrowsingDataCookieHelper> helper( |
254 new CannedBrowsingDataCookieHelper( | 165 new CannedBrowsingDataCookieHelper( |
255 testing_profile_->GetRequestContext())); | 166 testing_profile_->GetRequestContext())); |
256 | 167 |
257 ASSERT_TRUE(helper->empty()); | 168 ASSERT_TRUE(helper->empty()); |
258 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); | 169 helper->AddChangedCookie(origin, origin, "A=1", net::CookieOptions()); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 EXPECT_EQ(1U, helper->GetCookieCount()); | 244 EXPECT_EQ(1U, helper->GetCookieCount()); |
334 helper->AddChangedCookie(frame1_url, request_url, "b=1", | 245 helper->AddChangedCookie(frame1_url, request_url, "b=1", |
335 net::CookieOptions()); | 246 net::CookieOptions()); |
336 EXPECT_EQ(2U, helper->GetCookieCount()); | 247 EXPECT_EQ(2U, helper->GetCookieCount()); |
337 helper->AddChangedCookie(frame2_url, request_url, "a=2", | 248 helper->AddChangedCookie(frame2_url, request_url, "a=2", |
338 net::CookieOptions()); | 249 net::CookieOptions()); |
339 EXPECT_EQ(2U, helper->GetCookieCount()); | 250 EXPECT_EQ(2U, helper->GetCookieCount()); |
340 } | 251 } |
341 | 252 |
342 } // namespace | 253 } // namespace |
OLD | NEW |