mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 8 | #include "base/logging.h" |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 9 | #include "base/macros.h" |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 10 | #include "testing/gtest/include/gtest/gtest.h" |
| 11 | #include "url/gurl.h" |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 12 | #include "url/origin.h" |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 13 | |
| 14 | namespace { |
| 15 | |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 16 | void ExpectParsedUrlsEqual(const GURL& a, const GURL& b) { |
| 17 | EXPECT_EQ(a, b); |
| 18 | const url::Parsed& a_parsed = a.parsed_for_possibly_invalid_spec(); |
| 19 | const url::Parsed& b_parsed = b.parsed_for_possibly_invalid_spec(); |
csharrison | 6fbc9fa | 2016-10-08 03:31:50 | [diff] [blame] | 20 | EXPECT_EQ(a_parsed.scheme.begin, b_parsed.scheme.begin); |
| 21 | EXPECT_EQ(a_parsed.scheme.len, b_parsed.scheme.len); |
| 22 | EXPECT_EQ(a_parsed.username.begin, b_parsed.username.begin); |
| 23 | EXPECT_EQ(a_parsed.username.len, b_parsed.username.len); |
| 24 | EXPECT_EQ(a_parsed.password.begin, b_parsed.password.begin); |
| 25 | EXPECT_EQ(a_parsed.password.len, b_parsed.password.len); |
| 26 | EXPECT_EQ(a_parsed.host.begin, b_parsed.host.begin); |
| 27 | EXPECT_EQ(a_parsed.host.len, b_parsed.host.len); |
| 28 | EXPECT_EQ(a_parsed.port.begin, b_parsed.port.begin); |
| 29 | EXPECT_EQ(a_parsed.port.len, b_parsed.port.len); |
| 30 | EXPECT_EQ(a_parsed.path.begin, b_parsed.path.begin); |
| 31 | EXPECT_EQ(a_parsed.path.len, b_parsed.path.len); |
| 32 | EXPECT_EQ(a_parsed.query.begin, b_parsed.query.begin); |
| 33 | EXPECT_EQ(a_parsed.query.len, b_parsed.query.len); |
| 34 | EXPECT_EQ(a_parsed.ref.begin, b_parsed.ref.begin); |
| 35 | EXPECT_EQ(a_parsed.ref.len, b_parsed.ref.len); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 36 | } |
| 37 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 38 | TEST(OriginTest, UniqueOriginComparison) { |
| 39 | url::Origin unique_origin; |
| 40 | EXPECT_EQ("", unique_origin.scheme()); |
| 41 | EXPECT_EQ("", unique_origin.host()); |
| 42 | EXPECT_EQ(0, unique_origin.port()); |
| 43 | EXPECT_TRUE(unique_origin.unique()); |
| 44 | EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); |
| 45 | |
| 46 | const char* const urls[] = {"data:text/html,Hello!", |
| 47 | "javascript:alert(1)", |
| 48 | "file://example.com:443/etc/passwd", |
| 49 | "yay", |
| 50 | "http::///invalid.example.com/"}; |
| 51 | |
vmpstr | 981aa5a | 2016-07-14 01:05:08 | [diff] [blame] | 52 | for (auto* test_url : urls) { |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 53 | SCOPED_TRACE(test_url); |
| 54 | GURL url(test_url); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 55 | url::Origin origin = url::Origin::Create(url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 56 | EXPECT_EQ("", origin.scheme()); |
| 57 | EXPECT_EQ("", origin.host()); |
| 58 | EXPECT_EQ(0, origin.port()); |
| 59 | EXPECT_TRUE(origin.unique()); |
| 60 | EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 61 | EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); |
| 62 | EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 63 | |
| 64 | ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
jww | 908428c | 2016-10-26 21:51:46 | [diff] [blame] | 68 | TEST(OriginTest, ConstructFromTuple) { |
| 69 | struct TestCases { |
| 70 | const char* const scheme; |
| 71 | const char* const host; |
| 72 | const uint16_t port; |
| 73 | const char* const suborigin; |
| 74 | } cases[] = { |
| 75 | {"http", "example.com", 80, ""}, |
| 76 | {"http", "example.com", 123, ""}, |
| 77 | {"https", "example.com", 443, ""}, |
| 78 | {"http-so", "foobar.example.com", 80, "foobar"}, |
| 79 | {"http-so", "foobar.example.com", 123, "foobar"}, |
| 80 | {"https-so", "foobar.example.com", 443, "foobar"}, |
| 81 | }; |
| 82 | |
| 83 | for (const auto& test_case : cases) { |
| 84 | testing::Message scope_message; |
| 85 | if (test_case.suborigin != std::string()) { |
| 86 | scope_message << test_case.scheme << "-so://" << test_case.suborigin |
| 87 | << "." << test_case.host << ":" << test_case.port; |
| 88 | } else { |
| 89 | scope_message << test_case.scheme << "://" << test_case.host << ":" |
| 90 | << test_case.port; |
| 91 | } |
| 92 | SCOPED_TRACE(scope_message); |
jww | 908428c | 2016-10-26 21:51:46 | [diff] [blame] | 93 | url::Origin origin_with_suborigin = |
| 94 | url::Origin::CreateFromNormalizedTupleWithSuborigin( |
| 95 | test_case.scheme, test_case.host, test_case.port, |
| 96 | test_case.suborigin); |
| 97 | |
jww | 908428c | 2016-10-26 21:51:46 | [diff] [blame] | 98 | EXPECT_EQ(test_case.scheme, origin_with_suborigin.scheme()); |
| 99 | EXPECT_EQ(test_case.host, origin_with_suborigin.host()); |
| 100 | EXPECT_EQ(test_case.port, origin_with_suborigin.port()); |
| 101 | EXPECT_EQ(test_case.suborigin, origin_with_suborigin.suborigin()); |
| 102 | } |
| 103 | } |
| 104 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 105 | TEST(OriginTest, ConstructFromGURL) { |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 106 | url::Origin different_origin = |
| 107 | url::Origin::Create(GURL("https://not-in-the-list.test/")); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 108 | |
| 109 | struct TestCases { |
| 110 | const char* const url; |
| 111 | const char* const expected_scheme; |
| 112 | const char* const expected_host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 113 | const uint16_t expected_port; |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 114 | } cases[] = { |
| 115 | // IP Addresses |
| 116 | {"http://192.168.9.1/", "http", "192.168.9.1", 80}, |
| 117 | {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, |
| 118 | |
| 119 | // Punycode |
| 120 | {"http://☃.net/", "http", "xn--n3h.net", 80}, |
| 121 | {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, |
| 122 | |
| 123 | // Generic URLs |
| 124 | {"http://example.com/", "http", "example.com", 80}, |
| 125 | {"http://example.com:123/", "http", "example.com", 123}, |
| 126 | {"https://example.com/", "https", "example.com", 443}, |
| 127 | {"https://example.com:123/", "https", "example.com", 123}, |
| 128 | {"http://user:[email protected]/", "http", "example.com", 80}, |
| 129 | {"http://example.com:123/?query", "http", "example.com", 123}, |
| 130 | {"https://example.com/#1234", "https", "example.com", 443}, |
| 131 | {"https://u:[email protected]:123/?query#1234", "https", "example.com", 123}, |
| 132 | |
| 133 | // Registered URLs |
| 134 | {"ftp://example.com/", "ftp", "example.com", 21}, |
| 135 | {"gopher://example.com/", "gopher", "example.com", 70}, |
| 136 | {"ws://example.com/", "ws", "example.com", 80}, |
| 137 | {"wss://example.com/", "wss", "example.com", 443}, |
| 138 | |
| 139 | // file: URLs |
| 140 | {"file:///etc/passwd", "file", "", 0}, |
| 141 | {"file://example.com/etc/passwd", "file", "example.com", 0}, |
| 142 | |
| 143 | // Filesystem: |
| 144 | {"filesystem:http://example.com/type/", "http", "example.com", 80}, |
| 145 | {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, |
| 146 | {"filesystem:https://example.com/type/", "https", "example.com", 443}, |
| 147 | {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, |
| 148 | |
| 149 | // Blob: |
| 150 | {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, |
| 151 | {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123}, |
| 152 | {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, |
| 153 | {"blob:http://u:[email protected]/guid-goes-here", "http", "example.com", 80}, |
| 154 | }; |
| 155 | |
| 156 | for (const auto& test_case : cases) { |
| 157 | SCOPED_TRACE(test_case.url); |
| 158 | GURL url(test_case.url); |
| 159 | EXPECT_TRUE(url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 160 | url::Origin origin = url::Origin::Create(url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 161 | EXPECT_EQ(test_case.expected_scheme, origin.scheme()); |
| 162 | EXPECT_EQ(test_case.expected_host, origin.host()); |
| 163 | EXPECT_EQ(test_case.expected_port, origin.port()); |
| 164 | EXPECT_FALSE(origin.unique()); |
| 165 | EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 166 | EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); |
| 167 | EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 168 | |
| 169 | ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
| 173 | TEST(OriginTest, Serialization) { |
| 174 | struct TestCases { |
| 175 | const char* const url; |
| 176 | const char* const expected; |
| 177 | } cases[] = { |
| 178 | {"http://192.168.9.1/", "http://192.168.9.1"}, |
| 179 | {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| 180 | {"http://☃.net/", "http://xn--n3h.net"}, |
| 181 | {"http://example.com/", "http://example.com"}, |
| 182 | {"http://example.com:123/", "http://example.com:123"}, |
| 183 | {"https://example.com/", "https://example.com"}, |
| 184 | {"https://example.com:123/", "https://example.com:123"}, |
| 185 | {"file:///etc/passwd", "file://"}, |
| 186 | {"file://example.com/etc/passwd", "file://"}, |
| 187 | }; |
| 188 | |
| 189 | for (const auto& test_case : cases) { |
| 190 | SCOPED_TRACE(test_case.url); |
| 191 | GURL url(test_case.url); |
| 192 | EXPECT_TRUE(url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 193 | url::Origin origin = url::Origin::Create(url); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 194 | EXPECT_TRUE(origin.suborigin().empty()); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 195 | std::string serialized = origin.Serialize(); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 196 | std::string serialized_physical_origin = |
| 197 | origin.GetPhysicalOrigin().Serialize(); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 198 | ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 199 | |
| 200 | EXPECT_EQ(test_case.expected, serialized); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 201 | EXPECT_EQ(test_case.expected, serialized_physical_origin); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 202 | |
| 203 | // The '<<' operator should produce the same serialization as Serialize(). |
| 204 | std::stringstream out; |
| 205 | out << origin; |
| 206 | EXPECT_EQ(test_case.expected, out.str()); |
| 207 | } |
| 208 | } |
| 209 | |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 210 | TEST(OriginTest, SuboriginSerialization) { |
| 211 | struct TestCases { |
| 212 | const char* const url; |
| 213 | const char* const expected; |
| 214 | const char* const expected_physical_origin; |
| 215 | const char* const expected_suborigin; |
| 216 | } cases[] = { |
| 217 | {"http-so://foobar.example.com/", "http-so://foobar.example.com", |
| 218 | "http://example.com", "foobar"}, |
| 219 | {"http-so://foobar.example.com:123/", "http-so://foobar.example.com:123", |
| 220 | "http://example.com:123", "foobar"}, |
| 221 | {"https-so://foobar.example.com/", "https-so://foobar.example.com", |
| 222 | "https://example.com", "foobar"}, |
| 223 | {"https-so://foobar.example.com:123/", |
| 224 | "https-so://foobar.example.com:123", "https://example.com:123", |
| 225 | "foobar"}, |
| 226 | {"http://example.com/", "http://example.com", "http://example.com", ""}, |
| 227 | {"http-so://foobar.example.com/some/path", "http-so://foobar.example.com", |
| 228 | "http://example.com", "foobar"}, |
| 229 | {"http-so://foobar.example.com/some/path?query", |
| 230 | "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 231 | {"http-so://foobar.example.com/some/path#fragment", |
| 232 | "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 233 | {"http-so://foobar.example.com/some/path?query#fragment", |
| 234 | "http-so://foobar.example.com", "http://example.com", "foobar"}, |
| 235 | {"http-so://foobar.example.com:1234/some/path?query#fragment", |
| 236 | "http-so://foobar.example.com:1234", "http://example.com:1234", |
| 237 | "foobar"}, |
| 238 | }; |
| 239 | |
| 240 | for (const auto& test_case : cases) { |
| 241 | SCOPED_TRACE(test_case.url); |
| 242 | GURL url(test_case.url); |
| 243 | EXPECT_TRUE(url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 244 | url::Origin origin = url::Origin::Create(url); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 245 | std::string serialized = origin.Serialize(); |
| 246 | std::string serialized_physical_origin = |
| 247 | origin.GetPhysicalOrigin().Serialize(); |
| 248 | EXPECT_FALSE(origin.unique()); |
| 249 | EXPECT_EQ(test_case.expected_suborigin, origin.suborigin()); |
| 250 | ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 251 | |
| 252 | EXPECT_EQ(test_case.expected, serialized); |
| 253 | EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin); |
| 254 | |
| 255 | // The '<<' operator should produce the same serialization as Serialize(). |
| 256 | std::stringstream out; |
| 257 | out << origin; |
| 258 | EXPECT_EQ(test_case.expected, out.str()); |
| 259 | } |
| 260 | |
| 261 | const char* const failure_cases[] = { |
| 262 | "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.", |
| 263 | "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.", |
| 264 | }; |
| 265 | |
vmpstr | 6d9996c8 | 2017-02-23 00:43:25 | [diff] [blame] | 266 | for (auto* test_case : failure_cases) { |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 267 | SCOPED_TRACE(test_case); |
| 268 | GURL url(test_case); |
| 269 | EXPECT_TRUE(url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 270 | url::Origin origin = url::Origin::Create(url); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 271 | std::string serialized = origin.Serialize(); |
| 272 | std::string serialized_physical_origin = |
| 273 | origin.GetPhysicalOrigin().Serialize(); |
| 274 | EXPECT_TRUE(origin.unique()); |
| 275 | EXPECT_EQ("", origin.suborigin()); |
| 276 | ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 277 | |
| 278 | EXPECT_EQ("null", serialized); |
| 279 | EXPECT_EQ("null", serialized_physical_origin); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | TEST(OriginTest, SuboriginIsSameOriginWith) { |
| 284 | struct TestCases { |
| 285 | const char* const url1; |
| 286 | const char* const url2; |
| 287 | bool is_same_origin; |
| 288 | bool is_same_physical_origin; |
| 289 | } cases[]{ |
| 290 | {"http-so://foobar1.example.com/", "http-so://foobar1.example.com", true, |
| 291 | true}, |
| 292 | {"http-so://foobar2.example.com/", "https-so://foobar2.example.com", |
| 293 | false, false}, |
| 294 | {"http-so://foobar3.example.com/", "http://example.com", false, true}, |
| 295 | {"https-so://foobar4.example.com/", "https-so://foobar4.example.com", |
| 296 | true, true}, |
| 297 | {"https-so://foobar5.example.com/", "https://example.com", false, true}, |
| 298 | {"http-so://foobar6.example.com/", "http-so://bazbar.example.com", false, |
| 299 | true}, |
| 300 | {"http-so://foobar7.example.com/", "http-so://foobar7.google.com", false, |
| 301 | false}, |
| 302 | }; |
| 303 | |
| 304 | for (const auto& test_case : cases) { |
| 305 | SCOPED_TRACE(test_case.url1); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 306 | url::Origin origin1 = url::Origin::Create(GURL(test_case.url1)); |
| 307 | url::Origin origin2 = url::Origin::Create(GURL(test_case.url2)); |
jww | 0448040 | 2016-10-25 02:50:33 | [diff] [blame] | 308 | |
| 309 | EXPECT_TRUE(origin1.IsSameOriginWith(origin1)); |
| 310 | EXPECT_TRUE(origin2.IsSameOriginWith(origin2)); |
| 311 | EXPECT_EQ(test_case.is_same_origin, origin1.IsSameOriginWith(origin2)); |
| 312 | EXPECT_EQ(test_case.is_same_origin, origin2.IsSameOriginWith(origin1)); |
| 313 | |
| 314 | EXPECT_TRUE(origin1.IsSamePhysicalOriginWith(origin1)); |
| 315 | EXPECT_TRUE(origin2.IsSamePhysicalOriginWith(origin2)); |
| 316 | EXPECT_EQ(test_case.is_same_physical_origin, |
| 317 | origin1.IsSamePhysicalOriginWith(origin2)); |
| 318 | EXPECT_EQ(test_case.is_same_physical_origin, |
| 319 | origin2.IsSamePhysicalOriginWith(origin1)); |
| 320 | } |
| 321 | } |
| 322 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 323 | TEST(OriginTest, Comparison) { |
| 324 | // These URLs are arranged in increasing order: |
| 325 | const char* const urls[] = { |
| 326 | "data:uniqueness", |
| 327 | "http://a:80", |
| 328 | "http://b:80", |
| 329 | "https://a:80", |
| 330 | "https://b:80", |
| 331 | "http://a:81", |
| 332 | "http://b:81", |
| 333 | "https://a:81", |
| 334 | "https://b:81", |
| 335 | }; |
| 336 | |
| 337 | for (size_t i = 0; i < arraysize(urls); i++) { |
| 338 | GURL current_url(urls[i]); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 339 | url::Origin current = url::Origin::Create(current_url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 340 | for (size_t j = i; j < arraysize(urls); j++) { |
| 341 | GURL compare_url(urls[j]); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 342 | url::Origin to_compare = url::Origin::Create(compare_url); |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 343 | EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; |
| 344 | EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 349 | TEST(OriginTest, UnsafelyCreate) { |
| 350 | struct TestCase { |
| 351 | const char* scheme; |
| 352 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 353 | uint16_t port; |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 354 | } cases[] = { |
| 355 | {"http", "example.com", 80}, |
| 356 | {"http", "example.com", 123}, |
| 357 | {"https", "example.com", 443}, |
| 358 | {"https", "example.com", 123}, |
| 359 | {"file", "", 0}, |
| 360 | {"file", "example.com", 0}, |
| 361 | }; |
| 362 | |
| 363 | for (const auto& test : cases) { |
| 364 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 365 | << test.port); |
| 366 | url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
mek | 92c25968 | 2017-05-09 18:01:06 | [diff] [blame] | 367 | test.scheme, test.host, test.port, ""); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 368 | EXPECT_EQ(test.scheme, origin.scheme()); |
| 369 | EXPECT_EQ(test.host, origin.host()); |
| 370 | EXPECT_EQ(test.port, origin.port()); |
| 371 | EXPECT_FALSE(origin.unique()); |
| 372 | EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 373 | |
| 374 | ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { |
| 379 | struct TestCases { |
| 380 | const char* scheme; |
| 381 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 382 | uint16_t port; |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 383 | } cases[] = {{"", "", 0}, |
| 384 | {"data", "", 0}, |
| 385 | {"blob", "", 0}, |
| 386 | {"filesystem", "", 0}, |
| 387 | {"data", "example.com", 80}, |
| 388 | {"http", "☃.net", 80}, |
| 389 | {"http\nmore", "example.com", 80}, |
| 390 | {"http\rmore", "example.com", 80}, |
| 391 | {"http\n", "example.com", 80}, |
| 392 | {"http\r", "example.com", 80}, |
| 393 | {"http", "example.com\nnot-example.com", 80}, |
| 394 | {"http", "example.com\rnot-example.com", 80}, |
| 395 | {"http", "example.com\n", 80}, |
| 396 | {"http", "example.com\r", 80}, |
| 397 | {"http", "example.com", 0}, |
| 398 | {"file", "", 80}}; |
| 399 | |
| 400 | for (const auto& test : cases) { |
| 401 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 402 | << test.port); |
| 403 | url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
mek | 92c25968 | 2017-05-09 18:01:06 | [diff] [blame] | 404 | test.scheme, test.host, test.port, ""); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 405 | EXPECT_EQ("", origin.scheme()); |
| 406 | EXPECT_EQ("", origin.host()); |
| 407 | EXPECT_EQ(0, origin.port()); |
| 408 | EXPECT_TRUE(origin.unique()); |
| 409 | EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 410 | |
| 411 | ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
| 415 | TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 416 | struct TestCases { |
| 417 | const char* scheme; |
| 418 | size_t scheme_length; |
| 419 | const char* host; |
| 420 | size_t host_length; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 421 | uint16_t port; |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 422 | } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 423 | {"http\0", 5, "example.com", 11, 80}, |
| 424 | {"\0http", 5, "example.com", 11, 80}, |
| 425 | {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 426 | {"http", 4, "example.com\0", 12, 80}, |
| 427 | {"http", 4, "\0example.com", 12, 80}}; |
| 428 | |
| 429 | for (const auto& test : cases) { |
| 430 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 431 | << test.port); |
| 432 | url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 433 | std::string(test.scheme, test.scheme_length), |
mek | 92c25968 | 2017-05-09 18:01:06 | [diff] [blame] | 434 | std::string(test.host, test.host_length), test.port, ""); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 435 | EXPECT_EQ("", origin.scheme()); |
| 436 | EXPECT_EQ("", origin.host()); |
| 437 | EXPECT_EQ(0, origin.port()); |
| 438 | EXPECT_TRUE(origin.unique()); |
| 439 | EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
csharrison | 048bee1 | 2016-10-04 00:08:21 | [diff] [blame] | 440 | |
| 441 | ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
pkalinnikov | 054f403 | 2016-08-31 10:54:17 | [diff] [blame] | 445 | TEST(OriginTest, DomainIs) { |
| 446 | const struct { |
| 447 | const char* url; |
| 448 | const char* lower_ascii_domain; |
| 449 | bool expected_domain_is; |
| 450 | } kTestCases[] = { |
| 451 | {"http://google.com/foo", "google.com", true}, |
| 452 | {"http://www.google.com:99/foo", "google.com", true}, |
| 453 | {"http://www.google.com.cn/foo", "google.com", false}, |
| 454 | {"http://www.google.comm", "google.com", false}, |
| 455 | {"http://www.iamnotgoogle.com/foo", "google.com", false}, |
| 456 | {"http://www.google.com/foo", "Google.com", false}, |
| 457 | |
| 458 | // If the host ends with a dot, it matches domains with or without a dot. |
| 459 | {"http://www.google.com./foo", "google.com", true}, |
| 460 | {"http://www.google.com./foo", "google.com.", true}, |
| 461 | {"http://www.google.com./foo", ".com", true}, |
| 462 | {"http://www.google.com./foo", ".com.", true}, |
| 463 | |
| 464 | // But, if the host doesn't end with a dot and the input domain does, then |
| 465 | // it's considered to not match. |
| 466 | {"http://google.com/foo", "google.com.", false}, |
| 467 | |
| 468 | // If the host ends with two dots, it doesn't match. |
| 469 | {"http://www.google.com../foo", "google.com", false}, |
| 470 | |
| 471 | // Filesystem scheme. |
| 472 | {"filesystem:http://www.google.com:99/foo/", "google.com", true}, |
| 473 | {"filesystem:http://www.iamnotgoogle.com/foo/", "google.com", false}, |
| 474 | |
| 475 | // File scheme. |
| 476 | {"file:///home/user/text.txt", "", false}, |
| 477 | {"file:///home/user/text.txt", "txt", false}, |
| 478 | }; |
| 479 | |
| 480 | for (const auto& test_case : kTestCases) { |
| 481 | SCOPED_TRACE(testing::Message() << "(url, domain): (" << test_case.url |
| 482 | << ", " << test_case.lower_ascii_domain |
| 483 | << ")"); |
| 484 | GURL url(test_case.url); |
| 485 | ASSERT_TRUE(url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 486 | url::Origin origin = url::Origin::Create(url); |
pkalinnikov | 054f403 | 2016-08-31 10:54:17 | [diff] [blame] | 487 | |
| 488 | EXPECT_EQ(test_case.expected_domain_is, |
| 489 | origin.DomainIs(test_case.lower_ascii_domain)); |
| 490 | } |
| 491 | |
| 492 | // If the URL is invalid, DomainIs returns false. |
| 493 | GURL invalid_url("google.com"); |
| 494 | ASSERT_FALSE(invalid_url.is_valid()); |
Daniel Cheng | 88186bd5 | 2017-10-20 08:14:46 | [diff] [blame] | 495 | EXPECT_FALSE(url::Origin::Create(invalid_url).DomainIs("google.com")); |
pkalinnikov | 054f403 | 2016-08-31 10:54:17 | [diff] [blame] | 496 | |
| 497 | // Unique origins. |
| 498 | EXPECT_FALSE(url::Origin().DomainIs("")); |
| 499 | EXPECT_FALSE(url::Origin().DomainIs("com")); |
| 500 | } |
| 501 | |
mkwst | 9f2cc89 | 2015-07-22 06:03:25 | [diff] [blame] | 502 | } // namespace url |