| 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 "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 #include "net/cookies/cookie_constants.h" |
| 9 #include "net/cookies/cookie_options.h" | 10 #include "net/cookies/cookie_options.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 namespace net { | 13 namespace net { |
| 13 | 14 |
| 14 TEST(CanonicalCookieTest, GetCookieSourceFromURL) { | 15 TEST(CanonicalCookieTest, GetCookieSourceFromURL) { |
| 15 EXPECT_EQ("http://example.com/", | 16 EXPECT_EQ("http://example.com/", |
| 16 CanonicalCookie::GetCookieSourceFromURL( | 17 CanonicalCookie::GetCookieSourceFromURL( |
| 17 GURL("http://example.com"))); | 18 GURL("http://example.com"))); |
| 18 EXPECT_EQ("http://example.com/", | 19 EXPECT_EQ("http://example.com/", |
| (...skipping 20 matching lines...) Expand all Loading... |
| 39 EXPECT_EQ("http://example.com/", | 40 EXPECT_EQ("http://example.com/", |
| 40 CanonicalCookie::GetCookieSourceFromURL( | 41 CanonicalCookie::GetCookieSourceFromURL( |
| 41 GURL("http://example.com/test#foo"))); | 42 GURL("http://example.com/test#foo"))); |
| 42 } | 43 } |
| 43 | 44 |
| 44 TEST(CanonicalCookieTest, Constructor) { | 45 TEST(CanonicalCookieTest, Constructor) { |
| 45 GURL url("http://www.example.com/test"); | 46 GURL url("http://www.example.com/test"); |
| 46 base::Time current_time = base::Time::Now(); | 47 base::Time current_time = base::Time::Now(); |
| 47 | 48 |
| 48 CanonicalCookie cookie(url, "A", "2", "www.example.com", "/test", | 49 CanonicalCookie cookie(url, "A", "2", "www.example.com", "/test", |
| 49 current_time, base::Time(), current_time, false, | 50 current_time, base::Time(), current_time, false, false, |
| 50 false); | 51 PRIORITY_DEFAULT); |
| 51 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); | 52 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); |
| 52 EXPECT_EQ("A", cookie.Name()); | 53 EXPECT_EQ("A", cookie.Name()); |
| 53 EXPECT_EQ("2", cookie.Value()); | 54 EXPECT_EQ("2", cookie.Value()); |
| 54 EXPECT_EQ("www.example.com", cookie.Domain()); | 55 EXPECT_EQ("www.example.com", cookie.Domain()); |
| 55 EXPECT_EQ("/test", cookie.Path()); | 56 EXPECT_EQ("/test", cookie.Path()); |
| 56 EXPECT_FALSE(cookie.IsSecure()); | 57 EXPECT_FALSE(cookie.IsSecure()); |
| 57 | 58 |
| 58 CanonicalCookie cookie2(url, | 59 CanonicalCookie cookie2(url, |
| 59 "A", | 60 "A", |
| 60 "2", | 61 "2", |
| 61 std::string(), | 62 std::string(), |
| 62 std::string(), | 63 std::string(), |
| 63 current_time, | 64 current_time, |
| 64 base::Time(), | 65 base::Time(), |
| 65 current_time, | 66 current_time, |
| 66 false, | 67 false, |
| 67 false); | 68 false, |
| 69 PRIORITY_DEFAULT); |
| 68 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); | 70 EXPECT_EQ(url.GetOrigin().spec(), cookie.Source()); |
| 69 EXPECT_EQ("A", cookie2.Name()); | 71 EXPECT_EQ("A", cookie2.Name()); |
| 70 EXPECT_EQ("2", cookie2.Value()); | 72 EXPECT_EQ("2", cookie2.Value()); |
| 71 EXPECT_EQ("", cookie2.Domain()); | 73 EXPECT_EQ("", cookie2.Domain()); |
| 72 EXPECT_EQ("", cookie2.Path()); | 74 EXPECT_EQ("", cookie2.Path()); |
| 73 EXPECT_FALSE(cookie2.IsSecure()); | 75 EXPECT_FALSE(cookie2.IsSecure()); |
| 74 | 76 |
| 75 } | 77 } |
| 76 | 78 |
| 77 TEST(CanonicalCookieTest, Create) { | 79 TEST(CanonicalCookieTest, Create) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 httponly_options.set_include_httponly(); | 115 httponly_options.set_include_httponly(); |
| 114 cookie.reset( | 116 cookie.reset( |
| 115 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, | 117 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, |
| 116 httponly_options)); | 118 httponly_options)); |
| 117 EXPECT_TRUE(cookie->IsHttpOnly()); | 119 EXPECT_TRUE(cookie->IsHttpOnly()); |
| 118 | 120 |
| 119 // Test the creating cookies using specific parameter instead of a cookie | 121 // Test the creating cookies using specific parameter instead of a cookie |
| 120 // string. | 122 // string. |
| 121 cookie.reset(CanonicalCookie::Create( | 123 cookie.reset(CanonicalCookie::Create( |
| 122 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), | 124 url, "A", "2", "www.example.com", "/test", creation_time, base::Time(), |
| 123 false, false)); | 125 false, false, PRIORITY_DEFAULT)); |
| 124 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); | 126 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); |
| 125 EXPECT_EQ("A", cookie->Name()); | 127 EXPECT_EQ("A", cookie->Name()); |
| 126 EXPECT_EQ("2", cookie->Value()); | 128 EXPECT_EQ("2", cookie->Value()); |
| 127 EXPECT_EQ(".www.example.com", cookie->Domain()); | 129 EXPECT_EQ(".www.example.com", cookie->Domain()); |
| 128 EXPECT_EQ("/test", cookie->Path()); | 130 EXPECT_EQ("/test", cookie->Path()); |
| 129 EXPECT_FALSE(cookie->IsSecure()); | 131 EXPECT_FALSE(cookie->IsSecure()); |
| 130 | 132 |
| 131 cookie.reset(CanonicalCookie::Create( | 133 cookie.reset(CanonicalCookie::Create( |
| 132 url, "A", "2", ".www.example.com", "/test", creation_time, base::Time(), | 134 url, "A", "2", ".www.example.com", "/test", creation_time, base::Time(), |
| 133 false, false)); | 135 false, false, PRIORITY_DEFAULT)); |
| 134 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); | 136 EXPECT_EQ(url.GetOrigin().spec(), cookie->Source()); |
| 135 EXPECT_EQ("A", cookie->Name()); | 137 EXPECT_EQ("A", cookie->Name()); |
| 136 EXPECT_EQ("2", cookie->Value()); | 138 EXPECT_EQ("2", cookie->Value()); |
| 137 EXPECT_EQ(".www.example.com", cookie->Domain()); | 139 EXPECT_EQ(".www.example.com", cookie->Domain()); |
| 138 EXPECT_EQ("/test", cookie->Path()); | 140 EXPECT_EQ("/test", cookie->Path()); |
| 139 EXPECT_FALSE(cookie->IsSecure()); | 141 EXPECT_FALSE(cookie->IsSecure()); |
| 140 } | 142 } |
| 141 | 143 |
| 142 TEST(CanonicalCookieTest, EmptyExpiry) { | 144 TEST(CanonicalCookieTest, EmptyExpiry) { |
| 143 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); | 145 GURL url("http://www7.ipdl.inpit.go.jp/Tokujitu/tjkta.ipdl?N0000=108"); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 base::Time creation_time = base::Time::Now(); | 183 base::Time creation_time = base::Time::Now(); |
| 182 base::Time last_access_time = creation_time; | 184 base::Time last_access_time = creation_time; |
| 183 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); | 185 base::Time expiration_time = creation_time + base::TimeDelta::FromDays(2); |
| 184 bool secure(false); | 186 bool secure(false); |
| 185 bool httponly(false); | 187 bool httponly(false); |
| 186 | 188 |
| 187 // Test that a cookie is equivalent to itself. | 189 // Test that a cookie is equivalent to itself. |
| 188 scoped_ptr<CanonicalCookie> cookie( | 190 scoped_ptr<CanonicalCookie> cookie( |
| 189 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, | 191 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, |
| 190 cookie_path, creation_time, expiration_time, | 192 cookie_path, creation_time, expiration_time, |
| 191 last_access_time, secure, httponly)); | 193 last_access_time, secure, httponly, PRIORITY_MEDIUM)); |
| 192 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); | 194 EXPECT_TRUE(cookie->IsEquivalent(*cookie)); |
| 193 | 195 |
| 194 // Test that two identical cookies are equivalent. | 196 // Test that two identical cookies are equivalent. |
| 195 scoped_ptr<CanonicalCookie> other_cookie( | 197 scoped_ptr<CanonicalCookie> other_cookie( |
| 196 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, | 198 new CanonicalCookie(url, cookie_name, cookie_value, cookie_domain, |
| 197 cookie_path, creation_time, expiration_time, | 199 cookie_path, creation_time, expiration_time, |
| 198 last_access_time, secure, httponly)); | 200 last_access_time, secure, httponly, PRIORITY_MEDIUM)); |
| 199 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 201 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 200 | 202 |
| 201 // Tests that use different variations of attribute values that | 203 // Tests that use different variations of attribute values that |
| 202 // DON'T affect cookie equivalence. | 204 // DON'T affect cookie equivalence. |
| 203 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, | 205 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, |
| 204 cookie_path, creation_time, | 206 cookie_path, creation_time, |
| 205 expiration_time, last_access_time, | 207 expiration_time, last_access_time, |
| 206 secure, httponly)); | 208 secure, httponly, PRIORITY_HIGH)); |
| 207 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 209 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 208 | 210 |
| 209 base::Time other_creation_time = | 211 base::Time other_creation_time = |
| 210 creation_time + base::TimeDelta::FromMinutes(2); | 212 creation_time + base::TimeDelta::FromMinutes(2); |
| 211 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, | 213 other_cookie.reset(new CanonicalCookie(url, cookie_name, "2", cookie_domain, |
| 212 cookie_path, other_creation_time, | 214 cookie_path, other_creation_time, |
| 213 expiration_time, last_access_time, | 215 expiration_time, last_access_time, |
| 214 secure, httponly)); | 216 secure, httponly, PRIORITY_MEDIUM)); |
| 215 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 217 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 216 | 218 |
| 217 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_name, | 219 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_name, |
| 218 cookie_domain, cookie_path, | 220 cookie_domain, cookie_path, |
| 219 creation_time, expiration_time, | 221 creation_time, expiration_time, |
| 220 last_access_time, true, httponly)); | 222 last_access_time, true, httponly, |
| 223 PRIORITY_LOW)); |
| 221 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); | 224 EXPECT_TRUE(cookie->IsEquivalent(*other_cookie)); |
| 222 | 225 |
| 223 // Tests that use different variations of attribute values that | 226 // Tests that use different variations of attribute values that |
| 224 // DO affect cookie equivalence. | 227 // DO affect cookie equivalence. |
| 225 other_cookie.reset(new CanonicalCookie(url, "B", cookie_value, cookie_domain, | 228 other_cookie.reset(new CanonicalCookie(url, "B", cookie_value, cookie_domain, |
| 226 cookie_path, creation_time, | 229 cookie_path, creation_time, |
| 227 expiration_time, last_access_time, | 230 expiration_time, last_access_time, |
| 228 secure, httponly)); | 231 secure, httponly, PRIORITY_MEDIUM)); |
| 229 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 232 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 230 | 233 |
| 231 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 234 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, |
| 232 "www.example.com", cookie_path, | 235 "www.example.com", cookie_path, |
| 233 creation_time, expiration_time, | 236 creation_time, expiration_time, |
| 234 last_access_time, secure, httponly)); | 237 last_access_time, secure, httponly, |
| 238 PRIORITY_MEDIUM)); |
| 235 EXPECT_TRUE(cookie->IsDomainCookie()); | 239 EXPECT_TRUE(cookie->IsDomainCookie()); |
| 236 EXPECT_FALSE(other_cookie->IsDomainCookie()); | 240 EXPECT_FALSE(other_cookie->IsDomainCookie()); |
| 237 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 241 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 238 | 242 |
| 239 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 243 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, |
| 240 ".example.com", cookie_path, | 244 ".example.com", cookie_path, |
| 241 creation_time, expiration_time, | 245 creation_time, expiration_time, |
| 242 last_access_time, secure, httponly)); | 246 last_access_time, secure, httponly, |
| 247 PRIORITY_MEDIUM)); |
| 243 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 248 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 244 | 249 |
| 245 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, | 250 other_cookie.reset(new CanonicalCookie(url, cookie_name, cookie_value, |
| 246 cookie_domain, "/test/0", | 251 cookie_domain, "/test/0", |
| 247 creation_time, expiration_time, | 252 creation_time, expiration_time, |
| 248 last_access_time, secure, httponly)); | 253 last_access_time, secure, httponly, |
| 254 PRIORITY_MEDIUM)); |
| 249 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); | 255 EXPECT_FALSE(cookie->IsEquivalent(*other_cookie)); |
| 250 } | 256 } |
| 251 | 257 |
| 252 TEST(CanonicalCookieTest, IsDomainMatch) { | 258 TEST(CanonicalCookieTest, IsDomainMatch) { |
| 253 GURL url("http://www.example.com/test/foo.html"); | 259 GURL url("http://www.example.com/test/foo.html"); |
| 254 base::Time creation_time = base::Time::Now(); | 260 base::Time creation_time = base::Time::Now(); |
| 255 CookieOptions options; | 261 CookieOptions options; |
| 256 | 262 |
| 257 scoped_ptr<CanonicalCookie> cookie( | 263 scoped_ptr<CanonicalCookie> cookie( |
| 258 CanonicalCookie::Create(url, "A=2", creation_time, options)); | 264 CanonicalCookie::Create(url, "A=2", creation_time, options)); |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 344 options.set_include_httponly(); | 350 options.set_include_httponly(); |
| 345 cookie.reset( | 351 cookie.reset( |
| 346 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); | 352 CanonicalCookie::Create(url, "A=2; HttpOnly", creation_time, options)); |
| 347 EXPECT_TRUE(cookie->IsHttpOnly()); | 353 EXPECT_TRUE(cookie->IsHttpOnly()); |
| 348 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); | 354 EXPECT_TRUE(cookie->IncludeForRequestURL(url, options)); |
| 349 options.set_exclude_httponly(); | 355 options.set_exclude_httponly(); |
| 350 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); | 356 EXPECT_FALSE(cookie->IncludeForRequestURL(url, options)); |
| 351 } | 357 } |
| 352 | 358 |
| 353 } // namespace net | 359 } // namespace net |
| OLD | NEW |