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