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 "net/cookies/canonical_cookie.h" | 5 #include "net/cookies/canonical_cookie.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "net/cookies/cookie_constants.h" | 8 #include "net/cookies/cookie_constants.h" |
9 #include "net/cookies/cookie_options.h" | 9 #include "net/cookies/cookie_options.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 TEST(CanonicalCookieTest, GetCookieSourceFromURL) { | 15 TEST(CanonicalCookieTest, GetCookieSourceFromURL) { |
16 EXPECT_EQ("http://example.com/", | 16 EXPECT_EQ( |
17 CanonicalCookie::GetCookieSourceFromURL( | 17 "http://example.com/", |
18 GURL("http://example.com"))); | 18 CanonicalCookie::GetCookieSourceFromURL(GURL("http://example.com"))); |
19 EXPECT_EQ("http://example.com/", | 19 EXPECT_EQ( |
20 CanonicalCookie::GetCookieSourceFromURL( | 20 "http://example.com/", |
21 GURL("http://example.com/"))); | 21 CanonicalCookie::GetCookieSourceFromURL(GURL("http://example.com/"))); |
22 EXPECT_EQ("http://example.com/", | 22 EXPECT_EQ( |
23 CanonicalCookie::GetCookieSourceFromURL( | 23 "http://example.com/", |
24 GURL("http://example.com/test"))); | 24 CanonicalCookie::GetCookieSourceFromURL(GURL("http://example.com/test"))); |
25 EXPECT_EQ("file:///tmp/test.html", | 25 EXPECT_EQ( |
26 CanonicalCookie::GetCookieSourceFromURL( | 26 "file:///tmp/test.html", |
27 GURL("file:///tmp/test.html"))); | 27 CanonicalCookie::GetCookieSourceFromURL(GURL("file:///tmp/test.html"))); |
28 EXPECT_EQ("http://example.com/", | 28 EXPECT_EQ("http://example.com/", |
29 CanonicalCookie::GetCookieSourceFromURL( | 29 CanonicalCookie::GetCookieSourceFromURL( |
30 GURL("http://example.com:1234/"))); | 30 GURL("http://example.com:1234/"))); |
31 EXPECT_EQ("http://example.com/", | 31 EXPECT_EQ( |
32 CanonicalCookie::GetCookieSourceFromURL( | 32 "http://example.com/", |
33 GURL("https://example.com/"))); | 33 CanonicalCookie::GetCookieSourceFromURL(GURL("https://example.com/"))); |
34 EXPECT_EQ("http://example.com/", | 34 EXPECT_EQ("http://example.com/", |
35 CanonicalCookie::GetCookieSourceFromURL( | 35 CanonicalCookie::GetCookieSourceFromURL( |
36 GURL("http://user:pwd@example.com/"))); | 36 GURL("http://user:pwd@example.com/"))); |
37 EXPECT_EQ("http://example.com/", | 37 EXPECT_EQ("http://example.com/", |
38 CanonicalCookie::GetCookieSourceFromURL( | 38 CanonicalCookie::GetCookieSourceFromURL( |
39 GURL("http://example.com/test?foo"))); | 39 GURL("http://example.com/test?foo"))); |
40 EXPECT_EQ("http://example.com/", | 40 EXPECT_EQ("http://example.com/", |
41 CanonicalCookie::GetCookieSourceFromURL( | 41 CanonicalCookie::GetCookieSourceFromURL( |
42 GURL("http://example.com/test#foo"))); | 42 GURL("http://example.com/test#foo"))); |
43 } | 43 } |
44 | 44 |
45 TEST(CanonicalCookieTest, Constructor) { | 45 TEST(CanonicalCookieTest, Constructor) { |
46 GURL url("http://www.example.com/test"); | 46 GURL url("http://www.example.com/test"); |
47 base::Time current_time = base::Time::Now(); | 47 base::Time current_time = base::Time::Now(); |
48 | 48 |
49 CanonicalCookie cookie(url, "A", "2", "www.example.com", "/test", | 49 CanonicalCookie cookie(url, |
50 current_time, base::Time(), current_time, false, false, | 50 "A", |
| 51 "2", |
| 52 "www.example.com", |
| 53 "/test", |
| 54 current_time, |
| 55 base::Time(), |
| 56 current_time, |
| 57 false, |
| 58 false, |
51 COOKIE_PRIORITY_DEFAULT); | 59 COOKIE_PRIORITY_DEFAULT); |
52 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); | 60 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); |
53 EXPECT_EQ("A", cookie.Name()); | 61 EXPECT_EQ("A", cookie.Name()); |
54 EXPECT_EQ("2", cookie.Value()); | 62 EXPECT_EQ("2", cookie.Value()); |
55 EXPECT_EQ("www.example.com", cookie.Domain()); | 63 EXPECT_EQ("www.example.com", cookie.Domain()); |
56 EXPECT_EQ("/test", cookie.Path()); | 64 EXPECT_EQ("/test", cookie.Path()); |
57 EXPECT_FALSE(cookie.IsSecure()); | 65 EXPECT_FALSE(cookie.IsSecure()); |
58 | 66 |
59 CanonicalCookie cookie2(url, | 67 CanonicalCookie cookie2(url, |
60 "A", | 68 "A", |
61 "2", | 69 "2", |
62 std::string(), | 70 std::string(), |
63 std::string(), | 71 std::string(), |
64 current_time, | 72 current_time, |
65 base::Time(), | 73 base::Time(), |
66 current_time, | 74 current_time, |
67 false, | 75 false, |
68 false, | 76 false, |
69 COOKIE_PRIORITY_DEFAULT); | 77 COOKIE_PRIORITY_DEFAULT); |
70 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); | 78 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); |
71 EXPECT_EQ("A", cookie2.Name()); | 79 EXPECT_EQ("A", cookie2.Name()); |
72 EXPECT_EQ("2", cookie2.Value()); | 80 EXPECT_EQ("2", cookie2.Value()); |
73 EXPECT_EQ("", cookie2.Domain()); | 81 EXPECT_EQ("", cookie2.Domain()); |
74 EXPECT_EQ("", cookie2.Path()); | 82 EXPECT_EQ("", cookie2.Path()); |
75 EXPECT_FALSE(cookie2.IsSecure()); | 83 EXPECT_FALSE(cookie2.IsSecure()); |
76 | |
77 } | 84 } |
78 | 85 |
79 TEST(CanonicalCookieTest, Create) { | 86 TEST(CanonicalCookieTest, Create) { |
80 // Test creating cookies from a cookie string. | 87 // Test creating cookies from a cookie string. |
81 GURL url("http://www.example.com/test/foo.html"); | 88 GURL url("http://www.example.com/test/foo.html"); |
82 base::Time creation_time = base::Time::Now(); | 89 base::Time creation_time = base::Time::Now(); |
83 CookieOptions options; | 90 CookieOptions options; |
84 | 91 |
85 scoped_ptr<CanonicalCookie> cookie( | 92 scoped_ptr<CanonicalCookie> cookie( |
86 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 93 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
87 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); | 94 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); |
88 EXPECT_EQ("A", cookie->Name()); | 95 EXPECT_EQ("A", cookie->Name()); |
89 EXPECT_EQ("2", cookie->Value()); | 96 EXPECT_EQ("2", cookie->Value()); |
90 EXPECT_EQ("www.example.com", cookie->Domain()); | 97 EXPECT_EQ("www.example.com", cookie->Domain()); |
91 EXPECT_EQ("/test", cookie->Path()); | 98 EXPECT_EQ("/test", cookie->Path()); |
92 EXPECT_FALSE(cookie->IsSecure()); | 99 EXPECT_FALSE(cookie->IsSecure()); |
93 | 100 |
94 GURL url2("http://www.foo.com"); | 101 GURL url2("http://www.foo.com"); |
95 cookie.reset(CanonicalCookie::Create(url2, "B=1", creation_time, options)); | 102 cookie.reset(CanonicalCookie::Create(url2, "B=1", creation_time, options)); |
96 EXPECT_EQ(url2.GetOrigin().spec(), cookie->Source()); | 103 EXPECT_EQ(url2.GetOrigin().spec(), cookie->Source()); |
97 EXPECT_EQ("B", cookie->Name()); | 104 EXPECT_EQ("B", cookie->Name()); |
98 EXPECT_EQ("1", cookie->Value()); | 105 EXPECT_EQ("1", cookie->Value()); |
99 EXPECT_EQ("www.foo.com", cookie->Domain()); | 106 EXPECT_EQ("www.foo.com", cookie->Domain()); |
100 EXPECT_EQ("/", cookie->Path()); | 107 EXPECT_EQ("/", cookie->Path()); |
101 EXPECT_FALSE(cookie->IsSecure()); | 108 EXPECT_FALSE(cookie->IsSecure()); |
102 | 109 |
103 // Test creating secure cookies. RFC 6265 allows insecure urls to set secure | 110 // Test creating secure cookies. RFC 6265 allows insecure urls to set secure |
104 // cookies. | 111 // cookies. |
105 cookie.reset( | 112 cookie.reset( |
106 CanonicalCookie::Create(url, "A=2; Secure", creation_time, options)); | 113 CanonicalCookie::Create(url, "A=2; Secure", creation_time, options)); |
107 EXPECT_TRUE(cookie.get()); | 114 EXPECT_TRUE(cookie.get()); |
108 EXPECT_TRUE(cookie->IsSecure()); | 115 EXPECT_TRUE(cookie->IsSecure()); |
109 | 116 |
110 // Test creating http only cookies. | 117 // Test creating http only cookies. |
111 cookie.reset( | 118 cookie.reset( |
112 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); | 119 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); |
113 EXPECT_FALSE(cookie.get()); | 120 EXPECT_FALSE(cookie.get()); |
114 CookieOptions httponly_options; | 121 CookieOptions httponly_options; |
115 httponly_options.set_include_httponly(); | 122 httponly_options.set_include_httponly(); |
116 cookie.reset( | 123 cookie.reset(CanonicalCookie::Create( |
117 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, | 124 url, "A=2; HttpOnly", creation_time, httponly_options)); |
118 httponly_options)); | |
119 EXPECT_TRUE(cookie->IsHttpOnly()); | 125 EXPECT_TRUE(cookie->IsHttpOnly()); |
120 | 126 |
121 // Test the creating cookies using specific parameter instead of a cookie | 127 // Test the creating cookies using specific parameter instead of a cookie |
122 // string. | 128 // string. |
123 cookie.reset(CanonicalCookie::Create( | 129 cookie.reset(CanonicalCookie::Create(url, |
124 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), | 130 "A", |
125 false, false, COOKIE_PRIORITY_DEFAULT)); | 131 "2", |
| 132 "www.example.com", |
| 133 "/test", |
| 134 creation_time, |
| 135 base::Time(), |
| 136 false, |
| 137 false, |
| 138 COOKIE_PRIORITY_DEFAULT)); |
126 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); | 139 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); |
127 EXPECT_EQ("A", cookie->Name()); | 140 EXPECT_EQ("A", cookie->Name()); |
128 EXPECT_EQ("2", cookie->Value()); | 141 EXPECT_EQ("2", cookie->Value()); |
129 EXPECT_EQ(".www.example.com", cookie->Domain()); | 142 EXPECT_EQ(".www.example.com", cookie->Domain()); |
130 EXPECT_EQ("/test", cookie->Path()); | 143 EXPECT_EQ("/test", cookie->Path()); |
131 EXPECT_FALSE(cookie->IsSecure()); | 144 EXPECT_FALSE(cookie->IsSecure()); |
132 | 145 |
133 cookie.reset(CanonicalCookie::Create( | 146 cookie.reset(CanonicalCookie::Create(url, |
134 url, "A", "2", ".www.example.com", "/test", creation_time, base::Time(), | 147 "A", |
135 false, false, COOKIE_PRIORITY_DEFAULT)); | 148 "2", |
| 149 ".www.example.com", |
| 150 "/test", |
| 151 creation_time, |
| 152 base::Time(), |
| 153 false, |
| 154 false, |
| 155 COOKIE_PRIORITY_DEFAULT)); |
136 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); | 156 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); |
137 EXPECT_EQ("A", cookie->Name()); | 157 EXPECT_EQ("A", cookie->Name()); |
138 EXPECT_EQ("2", cookie->Value()); | 158 EXPECT_EQ("2", cookie->Value()); |
139 EXPECT_EQ(".www.example.com", cookie->Domain()); | 159 EXPECT_EQ(".www.example.com", cookie->Domain()); |
140 EXPECT_EQ("/test", cookie->Path()); | 160 EXPECT_EQ("/test", cookie->Path()); |
141 EXPECT_FALSE(cookie->IsSecure()); | 161 EXPECT_FALSE(cookie->IsSecure()); |
142 } | 162 } |
143 | 163 |
144 TEST(CanonicalCookieTest, EmptyExpiry) { | 164 TEST(CanonicalCookieTest, EmptyExpiry) { |
145 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); | 165 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); |
146 base::Time creation_time = base::Time::Now(); | 166 base::Time creation_time = base::Time::Now(); |
147 CookieOptions options; | 167 CookieOptions options; |
148 | 168 |
149 std::string cookie_line = | 169 std::string cookie_line = |
150 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires="; | 170 "ACSTM=20130308043820420042; path=/; domain=ipdl.inpit.go.jp; Expires="; |
151 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( | 171 scoped_ptr<CanonicalCookie> cookie( |
152 url, cookie_line, creation_time, options)); | 172 CanonicalCookie::Create(url, cookie_line, creation_time, options)); |
153 EXPECT_TRUE(cookie.get()); | 173 EXPECT_TRUE(cookie.get()); |
154 EXPECT_FALSE(cookie->IsPersistent()); | 174 EXPECT_FALSE(cookie->IsPersistent()); |
155 EXPECT_FALSE(cookie->IsExpired(creation_time)); | 175 EXPECT_FALSE(cookie->IsExpired(creation_time)); |
156 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); | 176 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); |
157 | 177 |
158 // With a stale server time | 178 // With a stale server time |
159 options.set_server_time(creation_time - base::TimeDelta::FromHours(1)); | 179 options.set_server_time(creation_time - base::TimeDelta::FromHours(1)); |
160 cookie.reset(CanonicalCookie::Create( | 180 cookie.reset( |
161 url, cookie_line, creation_time, options)); | 181 CanonicalCookie::Create(url, cookie_line, creation_time, options)); |
162 EXPECT_TRUE(cookie.get()); | 182 EXPECT_TRUE(cookie.get()); |
163 EXPECT_FALSE(cookie->IsPersistent()); | 183 EXPECT_FALSE(cookie->IsPersistent()); |
164 EXPECT_FALSE(cookie->IsExpired(creation_time)); | 184 EXPECT_FALSE(cookie->IsExpired(creation_time)); |
165 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); | 185 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); |
166 | 186 |
167 // With a future server time | 187 // With a future server time |
168 options.set_server_time(creation_time + base::TimeDelta::FromHours(1)); | 188 options.set_server_time(creation_time + base::TimeDelta::FromHours(1)); |
169 cookie.reset(CanonicalCookie::Create( | 189 cookie.reset( |
170 url, cookie_line, creation_time, options)); | 190 CanonicalCookie::Create(url, cookie_line, creation_time, options)); |
171 EXPECT_TRUE(cookie.get()); | 191 EXPECT_TRUE(cookie.get()); |
172 EXPECT_FALSE(cookie->IsPersistent()); | 192 EXPECT_FALSE(cookie->IsPersistent()); |
173 EXPECT_FALSE(cookie->IsExpired(creation_time)); | 193 EXPECT_FALSE(cookie->IsExpired(creation_time)); |
174 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); | 194 EXPECT_EQ(base::Time(), cookie->ExpiryDate()); |
175 } | 195 } |
176 | 196 |
177 TEST(CanonicalCookieTest, IsEquivalent) { | 197 TEST(CanonicalCookieTest, IsEquivalent) { |
178 GURL url("http://www.example.com/"); | 198 GURL url("http://www.example.com/"); |
179 std::string cookie_name = "A"; | 199 std::string cookie_name = "A"; |
180 std::string cookie_value = "2EDA-EF"; | 200 std::string cookie_value = "2EDA-EF"; |
181 std::string cookie_domain = ".www.example.com"; | 201 std::string cookie_domain = ".www.example.com"; |
182 std::string cookie_path = "/"; | 202 std::string cookie_path = "/"; |
183 base::Time creation_time = base::Time::Now(); | 203 base::Time creation_time = base::Time::Now(); |
184 base::Time last_access_time = creation_time; | 204 base::Time last_access_time = creation_time; |
185 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | 205 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); |
186 bool secure(false); | 206 bool secure(false); |
187 bool httponly(false); | 207 bool httponly(false); |
188 | 208 |
189 // Test that a cookie is equivalent to itself. | 209 // Test that a cookie is equivalent to itself. |
190 scoped_ptr<CanonicalCookie> cookie( | 210 scoped_ptr<CanonicalCookie> cookie( |
191 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, | 211 new CanonicalCookie(url, |
192 cookie_path, creation_time, expiration_time, | 212 cookie_name, |
193 last_access_time, secure, httponly, | 213 cookie_value, |
| 214 cookie_domain, |
| 215 cookie_path, |
| 216 creation_time, |
| 217 expiration_time, |
| 218 last_access_time, |
| 219 secure, |
| 220 httponly, |
194 COOKIE_PRIORITY_MEDIUM)); | 221 COOKIE_PRIORITY_MEDIUM)); |
195 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); | 222 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); |
196 | 223 |
197 // Test that two identical cookies are equivalent. | 224 // Test that two identical cookies are equivalent. |
198 scoped_ptr<CanonicalCookie> other_cookie( | 225 scoped_ptr<CanonicalCookie> other_cookie( |
199 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, | 226 new CanonicalCookie(url, |
200 cookie_path, creation_time, expiration_time, | 227 cookie_name, |
201 last_access_time, secure, httponly, | 228 cookie_value, |
| 229 cookie_domain, |
| 230 cookie_path, |
| 231 creation_time, |
| 232 expiration_time, |
| 233 last_access_time, |
| 234 secure, |
| 235 httponly, |
202 COOKIE_PRIORITY_MEDIUM)); | 236 COOKIE_PRIORITY_MEDIUM)); |
203 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 237 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
204 | 238 |
205 // Tests that use different variations of attribute values that | 239 // Tests that use different variations of attribute values that |
206 // DON'T affect cookie equivalence. | 240 // DON'T affect cookie equivalence. |
207 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, | 241 other_cookie.reset(new CanonicalCookie(url, |
208 cookie_path, creation_time, | 242 cookie_name, |
209 expiration_time, last_access_time, | 243 "2", |
210 secure, httponly, | 244 cookie_domain, |
| 245 cookie_path, |
| 246 creation_time, |
| 247 expiration_time, |
| 248 last_access_time, |
| 249 secure, |
| 250 httponly, |
211 COOKIE_PRIORITY_HIGH)); | 251 COOKIE_PRIORITY_HIGH)); |
212 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 252 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
213 | 253 |
214 base::Time other_creation_time = | 254 base::Time other_creation_time = |
215 creation_time + base::TimeDelta::FromMinutes(2); | 255 creation_time + base::TimeDelta::FromMinutes(2); |
216 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, | 256 other_cookie.reset(new CanonicalCookie(url, |
217 cookie_path, other_creation_time, | 257 cookie_name, |
218 expiration_time, last_access_time, | 258 "2", |
219 secure, httponly, | 259 cookie_domain, |
| 260 cookie_path, |
| 261 other_creation_time, |
| 262 expiration_time, |
| 263 last_access_time, |
| 264 secure, |
| 265 httponly, |
220 COOKIE_PRIORITY_MEDIUM)); | 266 COOKIE_PRIORITY_MEDIUM)); |
221 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 267 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
222 | 268 |
223 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_name, | 269 other_cookie.reset(new CanonicalCookie(url, |
224 cookie_domain, cookie_path, | 270 cookie_name, |
225 creation_time, expiration_time, | 271 cookie_name, |
226 last_access_time, true, httponly, | 272 cookie_domain, |
| 273 cookie_path, |
| 274 creation_time, |
| 275 expiration_time, |
| 276 last_access_time, |
| 277 true, |
| 278 httponly, |
227 COOKIE_PRIORITY_LOW)); | 279 COOKIE_PRIORITY_LOW)); |
228 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 280 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
229 | 281 |
230 // Tests that use different variations of attribute values that | 282 // Tests that use different variations of attribute values that |
231 // DO affect cookie equivalence. | 283 // DO affect cookie equivalence. |
232 other_cookie.reset(new CanonicalCookie(url, "B", cookie_value, cookie_domain, | 284 other_cookie.reset(new CanonicalCookie(url, |
233 cookie_path, creation_time, | 285 "B", |
234 expiration_time, last_access_time, | 286 cookie_value, |
235 secure, httponly, | 287 cookie_domain, |
| 288 cookie_path, |
| 289 creation_time, |
| 290 expiration_time, |
| 291 last_access_time, |
| 292 secure, |
| 293 httponly, |
236 COOKIE_PRIORITY_MEDIUM)); | 294 COOKIE_PRIORITY_MEDIUM)); |
237 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 295 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
238 | 296 |
239 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 297 other_cookie.reset(new CanonicalCookie(url, |
240 "www.example.com", cookie_path, | 298 cookie_name, |
241 creation_time, expiration_time, | 299 cookie_value, |
242 last_access_time, secure, httponly, | 300 "www.example.com", |
| 301 cookie_path, |
| 302 creation_time, |
| 303 expiration_time, |
| 304 last_access_time, |
| 305 secure, |
| 306 httponly, |
243 COOKIE_PRIORITY_MEDIUM)); | 307 COOKIE_PRIORITY_MEDIUM)); |
244 EXPECT_TRUE(cookie->IsDomainCookie()); | 308 EXPECT_TRUE(cookie->IsDomainCookie()); |
245 EXPECT_FALSE(other_cookie->IsDomainCookie()); | 309 EXPECT_FALSE(other_cookie->IsDomainCookie()); |
246 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 310 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
247 | 311 |
248 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 312 other_cookie.reset(new CanonicalCookie(url, |
249 ".example.com", cookie_path, | 313 cookie_name, |
250 creation_time, expiration_time, | 314 cookie_value, |
251 last_access_time, secure, httponly, | 315 ".example.com", |
| 316 cookie_path, |
| 317 creation_time, |
| 318 expiration_time, |
| 319 last_access_time, |
| 320 secure, |
| 321 httponly, |
252 COOKIE_PRIORITY_MEDIUM)); | 322 COOKIE_PRIORITY_MEDIUM)); |
253 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 323 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
254 | 324 |
255 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 325 other_cookie.reset(new CanonicalCookie(url, |
256 cookie_domain, "/test/0", | 326 cookie_name, |
257 creation_time, expiration_time, | 327 cookie_value, |
258 last_access_time, secure, httponly, | 328 cookie_domain, |
| 329 "/test/0", |
| 330 creation_time, |
| 331 expiration_time, |
| 332 last_access_time, |
| 333 secure, |
| 334 httponly, |
259 COOKIE_PRIORITY_MEDIUM)); | 335 COOKIE_PRIORITY_MEDIUM)); |
260 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 336 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
261 } | 337 } |
262 | 338 |
263 TEST(CanonicalCookieTest, IsDomainMatch) { | 339 TEST(CanonicalCookieTest, IsDomainMatch) { |
264 GURL url("http://www.example.com/test/foo.html"); | 340 GURL url("http://www.example.com/test/foo.html"); |
265 base::Time creation_time = base::Time::Now(); | 341 base::Time creation_time = base::Time::Now(); |
266 CookieOptions options; | 342 CookieOptions options; |
267 | 343 |
268 scoped_ptr<CanonicalCookie> cookie( | 344 scoped_ptr<CanonicalCookie> cookie( |
269 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 345 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
270 EXPECT_TRUE(cookie->IsHostCookie()); | 346 EXPECT_TRUE(cookie->IsHostCookie()); |
271 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 347 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
272 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 348 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
273 EXPECT_FALSE(cookie->IsDomainMatch("foo.www.example.com")); | 349 EXPECT_FALSE(cookie->IsDomainMatch("foo.www.example.com")); |
274 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); | 350 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); |
275 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); | 351 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); |
276 | 352 |
277 cookie.reset( | 353 cookie.reset(CanonicalCookie::Create( |
278 CanonicalCookie::Create(url, "A=2; Domain=www.example.com", creation_time, | 354 url, "A=2; Domain=www.example.com", creation_time, options)); |
279 options)); | |
280 EXPECT_TRUE(cookie->IsDomainCookie()); | 355 EXPECT_TRUE(cookie->IsDomainCookie()); |
281 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 356 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
282 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 357 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
283 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); | 358 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); |
284 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); | 359 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); |
285 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); | 360 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); |
286 | 361 |
287 cookie.reset( | 362 cookie.reset(CanonicalCookie::Create( |
288 CanonicalCookie::Create(url, "A=2; Domain=.www.example.com", | 363 url, "A=2; Domain=.www.example.com", creation_time, options)); |
289 creation_time, options)); | |
290 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 364 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
291 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); | 365 EXPECT_TRUE(cookie->IsDomainMatch("www.example.com")); |
292 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); | 366 EXPECT_TRUE(cookie->IsDomainMatch("foo.www.example.com")); |
293 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); | 367 EXPECT_FALSE(cookie->IsDomainMatch("www0.example.com")); |
294 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); | 368 EXPECT_FALSE(cookie->IsDomainMatch("example.com")); |
295 } | 369 } |
296 | 370 |
297 TEST(CanonicalCookieTest, IsOnPath) { | 371 TEST(CanonicalCookieTest, IsOnPath) { |
298 base::Time creation_time = base::Time::Now(); | 372 base::Time creation_time = base::Time::Now(); |
299 CookieOptions options; | 373 CookieOptions options; |
300 | 374 |
301 scoped_ptr<CanonicalCookie> cookie( | 375 scoped_ptr<CanonicalCookie> cookie(CanonicalCookie::Create( |
302 CanonicalCookie::Create(GURL("http://www.example.com"), | 376 GURL("http://www.example.com"), "A=2", creation_time, options)); |
303 "A=2", creation_time, options)); | |
304 EXPECT_TRUE(cookie->IsOnPath("/")); | 377 EXPECT_TRUE(cookie->IsOnPath("/")); |
305 EXPECT_TRUE(cookie->IsOnPath("/test")); | 378 EXPECT_TRUE(cookie->IsOnPath("/test")); |
306 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); | 379 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); |
307 | 380 |
308 // Test the empty string edge case. | 381 // Test the empty string edge case. |
309 EXPECT_FALSE(cookie->IsOnPath(std::string())); | 382 EXPECT_FALSE(cookie->IsOnPath(std::string())); |
310 | 383 |
311 cookie.reset( | 384 cookie.reset( |
312 CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"), | 385 CanonicalCookie::Create(GURL("http://www.example.com/test/foo.html"), |
313 "A=2", creation_time, options)); | 386 "A=2", |
| 387 creation_time, |
| 388 options)); |
314 EXPECT_FALSE(cookie->IsOnPath("/")); | 389 EXPECT_FALSE(cookie->IsOnPath("/")); |
315 EXPECT_TRUE(cookie->IsOnPath("/test")); | 390 EXPECT_TRUE(cookie->IsOnPath("/test")); |
316 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); | 391 EXPECT_TRUE(cookie->IsOnPath("/test/bar.html")); |
317 EXPECT_TRUE(cookie->IsOnPath("/test/sample/bar.html")); | 392 EXPECT_TRUE(cookie->IsOnPath("/test/sample/bar.html")); |
318 } | 393 } |
319 | 394 |
320 TEST(CanonicalCookieTest, IncludeForRequestURL) { | 395 TEST(CanonicalCookieTest, IncludeForRequestURL) { |
321 GURL url("http://www.example.com"); | 396 GURL url("http://www.example.com"); |
322 base::Time creation_time = base::Time::Now(); | 397 base::Time creation_time = base::Time::Now(); |
323 CookieOptions options; | 398 CookieOptions options; |
324 | 399 |
325 scoped_ptr<CanonicalCookie> cookie( | 400 scoped_ptr<CanonicalCookie> cookie( |
326 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 401 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
327 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); | 402 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); |
328 EXPECT_TRUE(cookie->IncludeForRequestURL( | 403 EXPECT_TRUE(cookie->IncludeForRequestURL( |
329 GURL("http://www.example.com/foo/bar"), options)); | 404 GURL("http://www.example.com/foo/bar"), options)); |
330 EXPECT_TRUE(cookie->IncludeForRequestURL( | 405 EXPECT_TRUE(cookie->IncludeForRequestURL( |
331 GURL("https://www.example.com/foo/bar"), options)); | 406 GURL("https://www.example.com/foo/bar"), options)); |
332 EXPECT_FALSE(cookie->IncludeForRequestURL(GURL("https://sub.example.com"), | 407 EXPECT_FALSE( |
333 options)); | 408 cookie->IncludeForRequestURL(GURL("https://sub.example.com"), options)); |
334 EXPECT_FALSE(cookie->IncludeForRequestURL(GURL("https://sub.www.example.com"), | 409 EXPECT_FALSE(cookie->IncludeForRequestURL(GURL("https://sub.www.example.com"), |
335 options)); | 410 options)); |
336 | 411 |
337 // Test that cookie with a cookie path that does not match the url path are | 412 // Test that cookie with a cookie path that does not match the url path are |
338 // not included. | 413 // not included. |
339 cookie.reset(CanonicalCookie::Create(url, "A=2; Path=/foo/bar", creation_time, | 414 cookie.reset(CanonicalCookie::Create( |
340 options)); | 415 url, "A=2; Path=/foo/bar", creation_time, options)); |
341 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); | 416 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); |
342 EXPECT_TRUE(cookie->IncludeForRequestURL( | 417 EXPECT_TRUE(cookie->IncludeForRequestURL( |
343 GURL("http://www.example.com/foo/bar/index.html"), options)); | 418 GURL("http://www.example.com/foo/bar/index.html"), options)); |
344 | 419 |
345 // Test that a secure cookie is not included for a non secure URL. | 420 // Test that a secure cookie is not included for a non secure URL. |
346 GURL secure_url("https://www.example.com"); | 421 GURL secure_url("https://www.example.com"); |
347 cookie.reset(CanonicalCookie::Create(secure_url, "A=2; Secure", creation_time, | 422 cookie.reset(CanonicalCookie::Create( |
348 options)); | 423 secure_url, "A=2; Secure", creation_time, options)); |
349 EXPECT_TRUE(cookie->IsSecure()); | 424 EXPECT_TRUE(cookie->IsSecure()); |
350 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); | 425 EXPECT_TRUE(cookie->IncludeForRequestURL(secure_url, options)); |
351 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); | 426 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); |
352 | 427 |
353 // Test that http only cookies are only included if the include httponly flag | 428 // Test that http only cookies are only included if the include httponly flag |
354 // is set on the cookie options. | 429 // is set on the cookie options. |
355 options.set_include_httponly(); | 430 options.set_include_httponly(); |
356 cookie.reset( | 431 cookie.reset( |
357 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); | 432 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); |
358 EXPECT_TRUE(cookie->IsHttpOnly()); | 433 EXPECT_TRUE(cookie->IsHttpOnly()); |
359 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); | 434 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); |
360 options.set_exclude_httponly(); | 435 options.set_exclude_httponly(); |
361 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); | 436 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); |
362 } | 437 } |
363 | 438 |
364 } // namespace net | 439 } // namespace net |
OLD | NEW |