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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 EXPECT_EQ(a_parsed.port.begin, b_parsed.port.begin); | 28 EXPECT_EQ(a_parsed.port.begin, b_parsed.port.begin); |
29 EXPECT_EQ(a_parsed.port.len, b_parsed.port.len); | 29 EXPECT_EQ(a_parsed.port.len, b_parsed.port.len); |
30 EXPECT_EQ(a_parsed.path.begin, b_parsed.path.begin); | 30 EXPECT_EQ(a_parsed.path.begin, b_parsed.path.begin); |
31 EXPECT_EQ(a_parsed.path.len, b_parsed.path.len); | 31 EXPECT_EQ(a_parsed.path.len, b_parsed.path.len); |
32 EXPECT_EQ(a_parsed.query.begin, b_parsed.query.begin); | 32 EXPECT_EQ(a_parsed.query.begin, b_parsed.query.begin); |
33 EXPECT_EQ(a_parsed.query.len, b_parsed.query.len); | 33 EXPECT_EQ(a_parsed.query.len, b_parsed.query.len); |
34 EXPECT_EQ(a_parsed.ref.begin, b_parsed.ref.begin); | 34 EXPECT_EQ(a_parsed.ref.begin, b_parsed.ref.begin); |
35 EXPECT_EQ(a_parsed.ref.len, b_parsed.ref.len); | 35 EXPECT_EQ(a_parsed.ref.len, b_parsed.ref.len); |
36 } | 36 } |
37 | 37 |
38 TEST(OriginTest, UniqueOriginComparison) { | 38 TEST(OriginTest, OpaqueOriginComparison) { |
39 url::Origin unique_origin; | 39 url::Origin opaque_origin; |
40 EXPECT_EQ("", unique_origin.scheme()); | 40 EXPECT_EQ("", opaque_origin.scheme()); |
41 EXPECT_EQ("", unique_origin.host()); | 41 EXPECT_EQ("", opaque_origin.host()); |
42 EXPECT_EQ(0, unique_origin.port()); | 42 EXPECT_EQ(0, opaque_origin.port()); |
43 EXPECT_TRUE(unique_origin.unique()); | 43 EXPECT_TRUE(opaque_origin.opaque()); |
44 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | 44 EXPECT_FALSE(opaque_origin.IsSameOriginWith(opaque_origin)); |
45 | 45 |
46 const char* const urls[] = {"data:text/html,Hello!", | 46 const char* const urls[] = {"data:text/html,Hello!", |
47 "javascript:alert(1)", | 47 "javascript:alert(1)", |
48 "file://example.com:443/etc/passwd", | 48 "file://example.com:443/etc/passwd", |
49 "yay", | 49 "yay", |
50 "http::///invalid.example.com/"}; | 50 "http::///invalid.example.com/"}; |
51 | 51 |
52 for (auto* test_url : urls) { | 52 for (auto* test_url : urls) { |
53 SCOPED_TRACE(test_url); | 53 SCOPED_TRACE(test_url); |
54 GURL url(test_url); | 54 GURL url(test_url); |
55 url::Origin origin(url); | 55 url::Origin origin(url); |
56 EXPECT_EQ("", origin.scheme()); | 56 EXPECT_EQ("", origin.scheme()); |
57 EXPECT_EQ("", origin.host()); | 57 EXPECT_EQ("", origin.host()); |
58 EXPECT_EQ(0, origin.port()); | 58 EXPECT_EQ(0, origin.port()); |
59 EXPECT_TRUE(origin.unique()); | 59 EXPECT_TRUE(origin.opaque()); |
60 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 60 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
61 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); | 61 EXPECT_FALSE(opaque_origin.IsSameOriginWith(origin)); |
62 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); | 62 EXPECT_FALSE(origin.IsSameOriginWith(opaque_origin)); |
63 | 63 |
64 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 64 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
65 } | 65 } |
66 } | 66 } |
67 | 67 |
68 TEST(OriginTest, ConstructFromTuple) { | 68 TEST(OriginTest, ConstructFromTuple) { |
69 struct TestCases { | 69 struct TestCases { |
70 const char* const scheme; | 70 const char* const scheme; |
71 const char* const host; | 71 const char* const host; |
72 const uint16_t port; | 72 const uint16_t port; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 }; | 153 }; |
154 | 154 |
155 for (const auto& test_case : cases) { | 155 for (const auto& test_case : cases) { |
156 SCOPED_TRACE(test_case.url); | 156 SCOPED_TRACE(test_case.url); |
157 GURL url(test_case.url); | 157 GURL url(test_case.url); |
158 EXPECT_TRUE(url.is_valid()); | 158 EXPECT_TRUE(url.is_valid()); |
159 url::Origin origin(url); | 159 url::Origin origin(url); |
160 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); | 160 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); |
161 EXPECT_EQ(test_case.expected_host, origin.host()); | 161 EXPECT_EQ(test_case.expected_host, origin.host()); |
162 EXPECT_EQ(test_case.expected_port, origin.port()); | 162 EXPECT_EQ(test_case.expected_port, origin.port()); |
163 EXPECT_FALSE(origin.unique()); | 163 EXPECT_FALSE(origin.opaque()); |
164 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 164 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
165 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); | 165 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); |
166 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); | 166 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); |
167 | 167 |
168 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 168 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
169 } | 169 } |
170 } | 170 } |
171 | 171 |
172 TEST(OriginTest, Serialization) { | 172 TEST(OriginTest, Serialization) { |
173 struct TestCases { | 173 struct TestCases { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 }; | 237 }; |
238 | 238 |
239 for (const auto& test_case : cases) { | 239 for (const auto& test_case : cases) { |
240 SCOPED_TRACE(test_case.url); | 240 SCOPED_TRACE(test_case.url); |
241 GURL url(test_case.url); | 241 GURL url(test_case.url); |
242 EXPECT_TRUE(url.is_valid()); | 242 EXPECT_TRUE(url.is_valid()); |
243 url::Origin origin(url); | 243 url::Origin origin(url); |
244 std::string serialized = origin.Serialize(); | 244 std::string serialized = origin.Serialize(); |
245 std::string serialized_physical_origin = | 245 std::string serialized_physical_origin = |
246 origin.GetPhysicalOrigin().Serialize(); | 246 origin.GetPhysicalOrigin().Serialize(); |
247 EXPECT_FALSE(origin.unique()); | 247 EXPECT_FALSE(origin.opaque()); |
248 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin()); | 248 EXPECT_EQ(test_case.expected_suborigin, origin.suborigin()); |
249 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); | 249 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
250 | 250 |
251 EXPECT_EQ(test_case.expected, serialized); | 251 EXPECT_EQ(test_case.expected, serialized); |
252 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin); | 252 EXPECT_EQ(test_case.expected_physical_origin, serialized_physical_origin); |
253 | 253 |
254 // The '<<' operator should produce the same serialization as Serialize(). | 254 // The '<<' operator should produce the same serialization as Serialize(). |
255 std::stringstream out; | 255 std::stringstream out; |
256 out << origin; | 256 out << origin; |
257 EXPECT_EQ(test_case.expected, out.str()); | 257 EXPECT_EQ(test_case.expected, out.str()); |
258 } | 258 } |
259 | 259 |
260 const char* const failure_cases[] = { | 260 const char* const failure_cases[] = { |
261 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.", | 261 "http-so://.", "http-so://foo", "http-so://.foo", "http-so://foo.", |
262 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.", | 262 "https-so://.", "https-so://foo", "https-so://.foo", "https-so://foo.", |
263 }; | 263 }; |
264 | 264 |
265 for (auto* test_case : failure_cases) { | 265 for (auto* test_case : failure_cases) { |
266 SCOPED_TRACE(test_case); | 266 SCOPED_TRACE(test_case); |
267 GURL url(test_case); | 267 GURL url(test_case); |
268 EXPECT_TRUE(url.is_valid()); | 268 EXPECT_TRUE(url.is_valid()); |
269 url::Origin origin(url); | 269 url::Origin origin(url); |
270 std::string serialized = origin.Serialize(); | 270 std::string serialized = origin.Serialize(); |
271 std::string serialized_physical_origin = | 271 std::string serialized_physical_origin = |
272 origin.GetPhysicalOrigin().Serialize(); | 272 origin.GetPhysicalOrigin().Serialize(); |
273 EXPECT_TRUE(origin.unique()); | 273 EXPECT_TRUE(origin.opaque()); |
274 EXPECT_EQ("", origin.suborigin()); | 274 EXPECT_EQ("", origin.suborigin()); |
275 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); | 275 ExpectParsedUrlsEqual(GURL(serialized), origin.GetURL()); |
276 | 276 |
277 EXPECT_EQ("null", serialized); | 277 EXPECT_EQ("null", serialized); |
278 EXPECT_EQ("null", serialized_physical_origin); | 278 EXPECT_EQ("null", serialized_physical_origin); |
279 } | 279 } |
280 } | 280 } |
281 | 281 |
282 TEST(OriginTest, SuboriginIsSameOriginWith) { | 282 TEST(OriginTest, SuboriginIsSameOriginWith) { |
283 struct TestCases { | 283 struct TestCases { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 }; | 360 }; |
361 | 361 |
362 for (const auto& test : cases) { | 362 for (const auto& test : cases) { |
363 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 363 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
364 << test.port); | 364 << test.port); |
365 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 365 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
366 test.scheme, test.host, test.port, ""); | 366 test.scheme, test.host, test.port, ""); |
367 EXPECT_EQ(test.scheme, origin.scheme()); | 367 EXPECT_EQ(test.scheme, origin.scheme()); |
368 EXPECT_EQ(test.host, origin.host()); | 368 EXPECT_EQ(test.host, origin.host()); |
369 EXPECT_EQ(test.port, origin.port()); | 369 EXPECT_EQ(test.port, origin.port()); |
370 EXPECT_FALSE(origin.unique()); | 370 EXPECT_FALSE(origin.opaque()); |
371 EXPECT_TRUE(origin.IsSameOriginWith(origin)); | 371 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
372 | 372 |
373 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 373 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
374 } | 374 } |
375 } | 375 } |
376 | 376 |
377 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { | 377 TEST(OriginTest, UnsafelyCreateUniqueOnInvalidInput) { |
378 struct TestCases { | 378 struct TestCases { |
379 const char* scheme; | 379 const char* scheme; |
380 const char* host; | 380 const char* host; |
(...skipping 16 matching lines...) Expand all Loading... |
397 {"file", "", 80}}; | 397 {"file", "", 80}}; |
398 | 398 |
399 for (const auto& test : cases) { | 399 for (const auto& test : cases) { |
400 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 400 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
401 << test.port); | 401 << test.port); |
402 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 402 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
403 test.scheme, test.host, test.port, ""); | 403 test.scheme, test.host, test.port, ""); |
404 EXPECT_EQ("", origin.scheme()); | 404 EXPECT_EQ("", origin.scheme()); |
405 EXPECT_EQ("", origin.host()); | 405 EXPECT_EQ("", origin.host()); |
406 EXPECT_EQ(0, origin.port()); | 406 EXPECT_EQ(0, origin.port()); |
407 EXPECT_TRUE(origin.unique()); | 407 EXPECT_TRUE(origin.opaque()); |
408 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 408 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
409 | 409 |
410 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 410 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
411 } | 411 } |
412 } | 412 } |
413 | 413 |
414 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 414 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
415 struct TestCases { | 415 struct TestCases { |
416 const char* scheme; | 416 const char* scheme; |
417 size_t scheme_length; | 417 size_t scheme_length; |
418 const char* host; | 418 const char* host; |
419 size_t host_length; | 419 size_t host_length; |
420 uint16_t port; | 420 uint16_t port; |
421 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 421 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
422 {"http\0", 5, "example.com", 11, 80}, | 422 {"http\0", 5, "example.com", 11, 80}, |
423 {"\0http", 5, "example.com", 11, 80}, | 423 {"\0http", 5, "example.com", 11, 80}, |
424 {"http", 4, "example.com\0not-example.com", 27, 80}, | 424 {"http", 4, "example.com\0not-example.com", 27, 80}, |
425 {"http", 4, "example.com\0", 12, 80}, | 425 {"http", 4, "example.com\0", 12, 80}, |
426 {"http", 4, "\0example.com", 12, 80}}; | 426 {"http", 4, "\0example.com", 12, 80}}; |
427 | 427 |
428 for (const auto& test : cases) { | 428 for (const auto& test : cases) { |
429 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 429 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
430 << test.port); | 430 << test.port); |
431 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 431 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
432 std::string(test.scheme, test.scheme_length), | 432 std::string(test.scheme, test.scheme_length), |
433 std::string(test.host, test.host_length), test.port, ""); | 433 std::string(test.host, test.host_length), test.port, ""); |
434 EXPECT_EQ("", origin.scheme()); | 434 EXPECT_EQ("", origin.scheme()); |
435 EXPECT_EQ("", origin.host()); | 435 EXPECT_EQ("", origin.host()); |
436 EXPECT_EQ(0, origin.port()); | 436 EXPECT_EQ(0, origin.port()); |
437 EXPECT_TRUE(origin.unique()); | 437 EXPECT_TRUE(origin.opaque()); |
438 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 438 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
439 | 439 |
440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
441 } | 441 } |
442 } | 442 } |
443 | 443 |
444 TEST(OriginTest, DomainIs) { | 444 TEST(OriginTest, DomainIs) { |
445 const struct { | 445 const struct { |
446 const char* url; | 446 const char* url; |
447 const char* lower_ascii_domain; | 447 const char* lower_ascii_domain; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
492 GURL invalid_url("google.com"); | 492 GURL invalid_url("google.com"); |
493 ASSERT_FALSE(invalid_url.is_valid()); | 493 ASSERT_FALSE(invalid_url.is_valid()); |
494 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); | 494 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
495 | 495 |
496 // Unique origins. | 496 // Unique origins. |
497 EXPECT_FALSE(url::Origin().DomainIs("")); | 497 EXPECT_FALSE(url::Origin().DomainIs("")); |
498 EXPECT_FALSE(url::Origin().DomainIs("com")); | 498 EXPECT_FALSE(url::Origin().DomainIs("com")); |
499 } | 499 } |
500 | 500 |
501 } // namespace url | 501 } // namespace url |
OLD | NEW |