| 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 23 matching lines...) Expand all Loading... |
| 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, OpaqueOriginComparison) { | 38 TEST(OriginTest, OpaqueOriginComparison) { |
| 39 url::Origin opaque_origin; | 39 url::Origin opaque_origin; |
| 40 EXPECT_EQ("", opaque_origin.scheme()); | 40 EXPECT_EQ("", opaque_origin.scheme()); |
| 41 EXPECT_EQ("", opaque_origin.host()); | 41 EXPECT_EQ("", opaque_origin.host()); |
| 42 EXPECT_EQ(0, opaque_origin.port()); | 42 EXPECT_EQ(0, opaque_origin.port()); |
| 43 EXPECT_TRUE(opaque_origin.opaque()); | 43 EXPECT_TRUE(opaque_origin.opaque()); |
| 44 EXPECT_FALSE(opaque_origin.IsSameOriginWith(opaque_origin)); | 44 // An opaque origin is same origin with itself |
| 45 EXPECT_TRUE(opaque_origin.IsSameOriginWith(opaque_origin)); |
| 46 // An opaque origin is not same origin with any other opaque origin |
| 47 url::Origin second_opaque_origin; |
| 48 EXPECT_FALSE(opaque_origin.IsSameOriginWith(second_opaque_origin)); |
| 49 EXPECT_FALSE(second_opaque_origin.IsSameOriginWith(opaque_origin)); |
| 45 | 50 |
| 46 const char* const urls[] = {"data:text/html,Hello!", | 51 const char* const urls[] = {"data:text/html,Hello!", |
| 47 "javascript:alert(1)", | 52 "javascript:alert(1)", |
| 48 "file://example.com:443/etc/passwd", | 53 "file://example.com:443/etc/passwd", |
| 49 "yay", | 54 "yay", |
| 50 "http::///invalid.example.com/"}; | 55 "http::///invalid.example.com/"}; |
| 51 | 56 |
| 52 for (auto* test_url : urls) { | 57 for (auto* test_url : urls) { |
| 53 SCOPED_TRACE(test_url); | 58 SCOPED_TRACE(test_url); |
| 54 GURL url(test_url); | 59 GURL url(test_url); |
| 55 url::Origin origin(url); | 60 url::Origin origin(url); |
| 56 EXPECT_EQ("", origin.scheme()); | 61 EXPECT_EQ("", origin.scheme()); |
| 57 EXPECT_EQ("", origin.host()); | 62 EXPECT_EQ("", origin.host()); |
| 58 EXPECT_EQ(0, origin.port()); | 63 EXPECT_EQ(0, origin.port()); |
| 59 EXPECT_TRUE(origin.opaque()); | 64 EXPECT_TRUE(origin.opaque()); |
| 60 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 65 |
| 66 // An opaque origin is same origin with itself |
| 67 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 68 |
| 69 // An opaque origin is not same origin with any other opaque origin |
| 61 EXPECT_FALSE(opaque_origin.IsSameOriginWith(origin)); | 70 EXPECT_FALSE(opaque_origin.IsSameOriginWith(origin)); |
| 62 EXPECT_FALSE(origin.IsSameOriginWith(opaque_origin)); | 71 EXPECT_FALSE(origin.IsSameOriginWith(opaque_origin)); |
| 63 | 72 |
| 73 // A second opaque origin from the same source string is not same origin |
| 74 // with the first. |
| 75 url::Origin duplicated_origin(url); |
| 76 EXPECT_FALSE(origin.IsSameOriginWith(duplicated_origin)); |
| 77 EXPECT_FALSE(duplicated_origin.IsSameOriginWith(origin)); |
| 78 |
| 79 // A copy of an opaque origin is not same origin with the original. |
| 80 url::Origin copied_origin(origin); |
| 81 EXPECT_FALSE(origin.IsSameOriginWith(copied_origin)); |
| 82 EXPECT_FALSE(copied_origin.IsSameOriginWith(origin)); |
| 83 |
| 64 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 84 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 65 } | 85 } |
| 66 } | 86 } |
| 67 | 87 |
| 68 TEST(OriginTest, ConstructFromTuple) { | 88 TEST(OriginTest, ConstructFromTuple) { |
| 69 struct TestCases { | 89 struct TestCases { |
| 70 const char* const scheme; | 90 const char* const scheme; |
| 71 const char* const host; | 91 const char* const host; |
| 72 const uint16_t port; | 92 const uint16_t port; |
| 73 const char* const suborigin; | 93 const char* const suborigin; |
| (...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 | 418 |
| 399 for (const auto& test : cases) { | 419 for (const auto& test : cases) { |
| 400 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 420 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 401 << test.port); | 421 << test.port); |
| 402 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 422 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 403 test.scheme, test.host, test.port, ""); | 423 test.scheme, test.host, test.port, ""); |
| 404 EXPECT_EQ("", origin.scheme()); | 424 EXPECT_EQ("", origin.scheme()); |
| 405 EXPECT_EQ("", origin.host()); | 425 EXPECT_EQ("", origin.host()); |
| 406 EXPECT_EQ(0, origin.port()); | 426 EXPECT_EQ(0, origin.port()); |
| 407 EXPECT_TRUE(origin.opaque()); | 427 EXPECT_TRUE(origin.opaque()); |
| 408 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 428 |
| 429 // An opaque origin is same origin with itself |
| 430 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 431 |
| 432 // A second opaque origin from the same URL components is not same origin |
| 433 // with the first. |
| 434 url::Origin duplicated_origin = |
| 435 url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 436 test.scheme, test.host, test.port, ""); |
| 437 EXPECT_FALSE(origin.IsSameOriginWith(duplicated_origin)); |
| 438 EXPECT_FALSE(duplicated_origin.IsSameOriginWith(origin)); |
| 409 | 439 |
| 410 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 411 } | 441 } |
| 412 } | 442 } |
| 413 | 443 |
| 414 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 444 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 415 struct TestCases { | 445 struct TestCases { |
| 416 const char* scheme; | 446 const char* scheme; |
| 417 size_t scheme_length; | 447 size_t scheme_length; |
| 418 const char* host; | 448 const char* host; |
| 419 size_t host_length; | 449 size_t host_length; |
| 420 uint16_t port; | 450 uint16_t port; |
| 421 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 451 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 422 {"http\0", 5, "example.com", 11, 80}, | 452 {"http\0", 5, "example.com", 11, 80}, |
| 423 {"\0http", 5, "example.com", 11, 80}, | 453 {"\0http", 5, "example.com", 11, 80}, |
| 424 {"http", 4, "example.com\0not-example.com", 27, 80}, | 454 {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 425 {"http", 4, "example.com\0", 12, 80}, | 455 {"http", 4, "example.com\0", 12, 80}, |
| 426 {"http", 4, "\0example.com", 12, 80}}; | 456 {"http", 4, "\0example.com", 12, 80}}; |
| 427 | 457 |
| 428 for (const auto& test : cases) { | 458 for (const auto& test : cases) { |
| 429 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 459 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 430 << test.port); | 460 << test.port); |
| 431 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 461 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 432 std::string(test.scheme, test.scheme_length), | 462 std::string(test.scheme, test.scheme_length), |
| 433 std::string(test.host, test.host_length), test.port, ""); | 463 std::string(test.host, test.host_length), test.port, ""); |
| 434 EXPECT_EQ("", origin.scheme()); | 464 EXPECT_EQ("", origin.scheme()); |
| 435 EXPECT_EQ("", origin.host()); | 465 EXPECT_EQ("", origin.host()); |
| 436 EXPECT_EQ(0, origin.port()); | 466 EXPECT_EQ(0, origin.port()); |
| 437 EXPECT_TRUE(origin.opaque()); | 467 EXPECT_TRUE(origin.opaque()); |
| 438 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 468 |
| 469 // An opaque origin is same origin with itself |
| 470 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 471 |
| 472 // A second opaque origin from the same URL components is not same origin |
| 473 // with the first. |
| 474 url::Origin duplicated_origin = |
| 475 url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 476 std::string(test.scheme, test.scheme_length), |
| 477 std::string(test.host, test.host_length), test.port, ""); |
| 478 EXPECT_FALSE(origin.IsSameOriginWith(duplicated_origin)); |
| 479 EXPECT_FALSE(duplicated_origin.IsSameOriginWith(origin)); |
| 439 | 480 |
| 440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 481 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 441 } | 482 } |
| 442 } | 483 } |
| 443 | 484 |
| 444 TEST(OriginTest, DomainIs) { | 485 TEST(OriginTest, DomainIs) { |
| 445 const struct { | 486 const struct { |
| 446 const char* url; | 487 const char* url; |
| 447 const char* lower_ascii_domain; | 488 const char* lower_ascii_domain; |
| 448 bool expected_domain_is; | 489 bool expected_domain_is; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 GURL invalid_url("google.com"); | 533 GURL invalid_url("google.com"); |
| 493 ASSERT_FALSE(invalid_url.is_valid()); | 534 ASSERT_FALSE(invalid_url.is_valid()); |
| 494 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); | 535 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
| 495 | 536 |
| 496 // Unique origins. | 537 // Unique origins. |
| 497 EXPECT_FALSE(url::Origin().DomainIs("")); | 538 EXPECT_FALSE(url::Origin().DomainIs("")); |
| 498 EXPECT_FALSE(url::Origin().DomainIs("com")); | 539 EXPECT_FALSE(url::Origin().DomainIs("com")); |
| 499 } | 540 } |
| 500 | 541 |
| 501 } // namespace url | 542 } // namespace url |
| OLD | NEW |