| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.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 #include "url/origin.h" | 12 #include "url/origin.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 void ExpectParsedComponentEqual(const url::Component& a, |
| 17 const url::Component& b) { |
| 18 EXPECT_EQ(a.begin, b.begin); |
| 19 EXPECT_EQ(a.len, b.len); |
| 20 } |
| 21 |
| 22 void ExpectParsedUrlsEqual(const GURL& a, const GURL& b) { |
| 23 EXPECT_EQ(a, b); |
| 24 const url::Parsed& a_parsed = a.parsed_for_possibly_invalid_spec(); |
| 25 const url::Parsed& b_parsed = b.parsed_for_possibly_invalid_spec(); |
| 26 ExpectParsedComponentEqual(a_parsed.scheme, b_parsed.scheme); |
| 27 ExpectParsedComponentEqual(a_parsed.username, b_parsed.username); |
| 28 ExpectParsedComponentEqual(a_parsed.password, b_parsed.password); |
| 29 ExpectParsedComponentEqual(a_parsed.host, b_parsed.host); |
| 30 ExpectParsedComponentEqual(a_parsed.port, b_parsed.port); |
| 31 ExpectParsedComponentEqual(a_parsed.path, b_parsed.path); |
| 32 ExpectParsedComponentEqual(a_parsed.query, b_parsed.query); |
| 33 ExpectParsedComponentEqual(a_parsed.ref, b_parsed.ref); |
| 34 } |
| 35 |
| 16 TEST(OriginTest, UniqueOriginComparison) { | 36 TEST(OriginTest, UniqueOriginComparison) { |
| 17 url::Origin unique_origin; | 37 url::Origin unique_origin; |
| 18 EXPECT_EQ("", unique_origin.scheme()); | 38 EXPECT_EQ("", unique_origin.scheme()); |
| 19 EXPECT_EQ("", unique_origin.host()); | 39 EXPECT_EQ("", unique_origin.host()); |
| 20 EXPECT_EQ(0, unique_origin.port()); | 40 EXPECT_EQ(0, unique_origin.port()); |
| 21 EXPECT_TRUE(unique_origin.unique()); | 41 EXPECT_TRUE(unique_origin.unique()); |
| 22 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | 42 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); |
| 23 | 43 |
| 24 const char* const urls[] = {"data:text/html,Hello!", | 44 const char* const urls[] = {"data:text/html,Hello!", |
| 25 "javascript:alert(1)", | 45 "javascript:alert(1)", |
| 26 "file://example.com:443/etc/passwd", | 46 "file://example.com:443/etc/passwd", |
| 27 "yay", | 47 "yay", |
| 28 "http::///invalid.example.com/"}; | 48 "http::///invalid.example.com/"}; |
| 29 | 49 |
| 30 for (auto* test_url : urls) { | 50 for (auto* test_url : urls) { |
| 31 SCOPED_TRACE(test_url); | 51 SCOPED_TRACE(test_url); |
| 32 GURL url(test_url); | 52 GURL url(test_url); |
| 33 url::Origin origin(url); | 53 url::Origin origin(url); |
| 34 EXPECT_EQ("", origin.scheme()); | 54 EXPECT_EQ("", origin.scheme()); |
| 35 EXPECT_EQ("", origin.host()); | 55 EXPECT_EQ("", origin.host()); |
| 36 EXPECT_EQ(0, origin.port()); | 56 EXPECT_EQ(0, origin.port()); |
| 37 EXPECT_TRUE(origin.unique()); | 57 EXPECT_TRUE(origin.unique()); |
| 38 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 58 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 39 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); | 59 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); |
| 40 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); | 60 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); |
| 61 |
| 62 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 41 } | 63 } |
| 42 } | 64 } |
| 43 | 65 |
| 44 TEST(OriginTest, ConstructFromGURL) { | 66 TEST(OriginTest, ConstructFromGURL) { |
| 45 url::Origin different_origin(GURL("https://not-in-the-list.test/")); | 67 url::Origin different_origin(GURL("https://not-in-the-list.test/")); |
| 46 | 68 |
| 47 struct TestCases { | 69 struct TestCases { |
| 48 const char* const url; | 70 const char* const url; |
| 49 const char* const expected_scheme; | 71 const char* const expected_scheme; |
| 50 const char* const expected_host; | 72 const char* const expected_host; |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 GURL url(test_case.url); | 118 GURL url(test_case.url); |
| 97 EXPECT_TRUE(url.is_valid()); | 119 EXPECT_TRUE(url.is_valid()); |
| 98 url::Origin origin(url); | 120 url::Origin origin(url); |
| 99 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); | 121 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); |
| 100 EXPECT_EQ(test_case.expected_host, origin.host()); | 122 EXPECT_EQ(test_case.expected_host, origin.host()); |
| 101 EXPECT_EQ(test_case.expected_port, origin.port()); | 123 EXPECT_EQ(test_case.expected_port, origin.port()); |
| 102 EXPECT_FALSE(origin.unique()); | 124 EXPECT_FALSE(origin.unique()); |
| 103 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 125 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 104 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); | 126 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); |
| 105 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); | 127 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); |
| 128 |
| 129 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 106 } | 130 } |
| 107 } | 131 } |
| 108 | 132 |
| 109 TEST(OriginTest, Serialization) { | 133 TEST(OriginTest, Serialization) { |
| 110 struct TestCases { | 134 struct TestCases { |
| 111 const char* const url; | 135 const char* const url; |
| 112 const char* const expected; | 136 const char* const expected; |
| 113 } cases[] = { | 137 } cases[] = { |
| 114 {"http://192.168.9.1/", "http://192.168.9.1"}, | 138 {"http://192.168.9.1/", "http://192.168.9.1"}, |
| 115 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, | 139 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| 116 {"http://☃.net/", "http://xn--n3h.net"}, | 140 {"http://☃.net/", "http://xn--n3h.net"}, |
| 117 {"http://example.com/", "http://example.com"}, | 141 {"http://example.com/", "http://example.com"}, |
| 118 {"http://example.com:123/", "http://example.com:123"}, | 142 {"http://example.com:123/", "http://example.com:123"}, |
| 119 {"https://example.com/", "https://example.com"}, | 143 {"https://example.com/", "https://example.com"}, |
| 120 {"https://example.com:123/", "https://example.com:123"}, | 144 {"https://example.com:123/", "https://example.com:123"}, |
| 121 {"file:///etc/passwd", "file://"}, | 145 {"file:///etc/passwd", "file:///"}, |
| 122 {"file://example.com/etc/passwd", "file://"}, | 146 {"file://example.com/etc/passwd", "file:///"}, |
| 123 }; | 147 }; |
| 124 | 148 |
| 125 for (const auto& test_case : cases) { | 149 for (const auto& test_case : cases) { |
| 126 SCOPED_TRACE(test_case.url); | 150 SCOPED_TRACE(test_case.url); |
| 127 GURL url(test_case.url); | 151 GURL url(test_case.url); |
| 128 EXPECT_TRUE(url.is_valid()); | 152 EXPECT_TRUE(url.is_valid()); |
| 129 url::Origin origin(url); | 153 url::Origin origin(url); |
| 130 EXPECT_EQ(test_case.expected, origin.Serialize()); | 154 std::string serialized = origin.Serialize(); |
| 155 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
| 156 |
| 157 EXPECT_EQ(test_case.expected, serialized); |
| 131 | 158 |
| 132 // The '<<' operator should produce the same serialization as Serialize(). | 159 // The '<<' operator should produce the same serialization as Serialize(). |
| 133 std::stringstream out; | 160 std::stringstream out; |
| 134 out << origin; | 161 out << origin; |
| 135 EXPECT_EQ(test_case.expected, out.str()); | 162 EXPECT_EQ(test_case.expected, out.str()); |
| 136 } | 163 } |
| 137 } | 164 } |
| 138 | 165 |
| 139 TEST(OriginTest, Comparison) { | 166 TEST(OriginTest, Comparison) { |
| 140 // These URLs are arranged in increasing order: | 167 // These URLs are arranged in increasing order: |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 for (const auto& test : cases) { | 206 for (const auto& test : cases) { |
| 180 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 207 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 181 << test.port); | 208 << test.port); |
| 182 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 209 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 183 test.scheme, test.host, test.port); | 210 test.scheme, test.host, test.port); |
| 184 EXPECT_EQ(test.scheme, origin.scheme()); | 211 EXPECT_EQ(test.scheme, origin.scheme()); |
| 185 EXPECT_EQ(test.host, origin.host()); | 212 EXPECT_EQ(test.host, origin.host()); |
| 186 EXPECT_EQ(test.port, origin.port()); | 213 EXPECT_EQ(test.port, origin.port()); |
| 187 EXPECT_FALSE(origin.unique()); | 214 EXPECT_FALSE(origin.unique()); |
| 188 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 215 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 216 |
| 217 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 189 } | 218 } |
| 190 } | 219 } |
| 191 | 220 |
| 192 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { | 221 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { |
| 193 struct TestCases { | 222 struct TestCases { |
| 194 const char* scheme; | 223 const char* scheme; |
| 195 const char* host; | 224 const char* host; |
| 196 uint16_t port; | 225 uint16_t port; |
| 197 } cases[] = {{"", "", 0}, | 226 } cases[] = {{"", "", 0}, |
| 198 {"data", "", 0}, | 227 {"data", "", 0}, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 214 for (const auto& test : cases) { | 243 for (const auto& test : cases) { |
| 215 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 244 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 216 << test.port); | 245 << test.port); |
| 217 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 246 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 218 test.scheme, test.host, test.port); | 247 test.scheme, test.host, test.port); |
| 219 EXPECT_EQ("", origin.scheme()); | 248 EXPECT_EQ("", origin.scheme()); |
| 220 EXPECT_EQ("", origin.host()); | 249 EXPECT_EQ("", origin.host()); |
| 221 EXPECT_EQ(0, origin.port()); | 250 EXPECT_EQ(0, origin.port()); |
| 222 EXPECT_TRUE(origin.unique()); | 251 EXPECT_TRUE(origin.unique()); |
| 223 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 252 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 253 |
| 254 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 224 } | 255 } |
| 225 } | 256 } |
| 226 | 257 |
| 227 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 258 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 228 struct TestCases { | 259 struct TestCases { |
| 229 const char* scheme; | 260 const char* scheme; |
| 230 size_t scheme_length; | 261 size_t scheme_length; |
| 231 const char* host; | 262 const char* host; |
| 232 size_t host_length; | 263 size_t host_length; |
| 233 uint16_t port; | 264 uint16_t port; |
| 234 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 265 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 235 {"http\0", 5, "example.com", 11, 80}, | 266 {"http\0", 5, "example.com", 11, 80}, |
| 236 {"\0http", 5, "example.com", 11, 80}, | 267 {"\0http", 5, "example.com", 11, 80}, |
| 237 {"http", 4, "example.com\0not-example.com", 27, 80}, | 268 {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 238 {"http", 4, "example.com\0", 12, 80}, | 269 {"http", 4, "example.com\0", 12, 80}, |
| 239 {"http", 4, "\0example.com", 12, 80}}; | 270 {"http", 4, "\0example.com", 12, 80}}; |
| 240 | 271 |
| 241 for (const auto& test : cases) { | 272 for (const auto& test : cases) { |
| 242 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 273 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 243 << test.port); | 274 << test.port); |
| 244 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 275 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 245 std::string(test.scheme, test.scheme_length), | 276 std::string(test.scheme, test.scheme_length), |
| 246 std::string(test.host, test.host_length), test.port); | 277 std::string(test.host, test.host_length), test.port); |
| 247 EXPECT_EQ("", origin.scheme()); | 278 EXPECT_EQ("", origin.scheme()); |
| 248 EXPECT_EQ("", origin.host()); | 279 EXPECT_EQ("", origin.host()); |
| 249 EXPECT_EQ(0, origin.port()); | 280 EXPECT_EQ(0, origin.port()); |
| 250 EXPECT_TRUE(origin.unique()); | 281 EXPECT_TRUE(origin.unique()); |
| 251 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 282 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 283 |
| 284 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 252 } | 285 } |
| 253 } | 286 } |
| 254 | 287 |
| 255 TEST(OriginTest, DomainIs) { | 288 TEST(OriginTest, DomainIs) { |
| 256 const struct { | 289 const struct { |
| 257 const char* url; | 290 const char* url; |
| 258 const char* lower_ascii_domain; | 291 const char* lower_ascii_domain; |
| 259 bool expected_domain_is; | 292 bool expected_domain_is; |
| 260 } kTestCases[] = { | 293 } kTestCases[] = { |
| 261 {"http://google.com/foo", "google.com", true}, | 294 {"http://google.com/foo", "google.com", true}, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 GURL invalid_url("google.com"); | 336 GURL invalid_url("google.com"); |
| 304 ASSERT_FALSE(invalid_url.is_valid()); | 337 ASSERT_FALSE(invalid_url.is_valid()); |
| 305 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); | 338 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
| 306 | 339 |
| 307 // Unique origins. | 340 // Unique origins. |
| 308 EXPECT_FALSE(url::Origin().DomainIs("")); | 341 EXPECT_FALSE(url::Origin().DomainIs("")); |
| 309 EXPECT_FALSE(url::Origin().DomainIs("com")); | 342 EXPECT_FALSE(url::Origin().DomainIs("com")); |
| 310 } | 343 } |
| 311 | 344 |
| 312 } // namespace url | 345 } // namespace url |
| OLD | NEW |