| 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, UniqueOriginComparison) { | 38 TEST(OriginTest, UniqueOriginComparison) { |
| 39 url::Origin unique_origin; | 39 url::Origin unique_origin; |
| 40 EXPECT_EQ("", unique_origin.scheme()); | 40 EXPECT_EQ("", unique_origin.scheme()); |
| 41 EXPECT_EQ("", unique_origin.host()); | 41 EXPECT_EQ("", unique_origin.host()); |
| 42 EXPECT_EQ(0, unique_origin.port()); | 42 EXPECT_EQ(0, unique_origin.port()); |
| 43 EXPECT_TRUE(unique_origin.unique()); | 43 EXPECT_TRUE(unique_origin.unique()); |
| 44 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); | 44 EXPECT_TRUE(unique_origin.IsSameOriginWith(unique_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 url::Origin origin_copy(origin); |
| 56 EXPECT_EQ("", origin.scheme()); | 57 EXPECT_EQ("", origin.scheme()); |
| 57 EXPECT_EQ("", origin.host()); | 58 EXPECT_EQ("", origin.host()); |
| 58 EXPECT_EQ(0, origin.port()); | 59 EXPECT_EQ(0, origin.port()); |
| 59 EXPECT_TRUE(origin.unique()); | 60 EXPECT_TRUE(origin.unique()); |
| 60 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 61 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 61 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); | 62 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); |
| 62 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); | 63 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); |
| 64 EXPECT_TRUE(origin.IsSameOriginWith(origin_copy)); |
| 63 | 65 |
| 64 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 66 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 65 } | 67 } |
| 66 } | 68 } |
| 67 | 69 |
| 68 TEST(OriginTest, ConstructFromTuple) { | 70 TEST(OriginTest, ConstructFromTuple) { |
| 69 struct TestCases { | 71 struct TestCases { |
| 70 const char* const scheme; | 72 const char* const scheme; |
| 71 const char* const host; | 73 const char* const host; |
| 72 const uint16_t port; | 74 const uint16_t port; |
| (...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 | 400 |
| 399 for (const auto& test : cases) { | 401 for (const auto& test : cases) { |
| 400 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 402 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 401 << test.port); | 403 << test.port); |
| 402 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 404 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 403 test.scheme, test.host, test.port); | 405 test.scheme, test.host, test.port); |
| 404 EXPECT_EQ("", origin.scheme()); | 406 EXPECT_EQ("", origin.scheme()); |
| 405 EXPECT_EQ("", origin.host()); | 407 EXPECT_EQ("", origin.host()); |
| 406 EXPECT_EQ(0, origin.port()); | 408 EXPECT_EQ(0, origin.port()); |
| 407 EXPECT_TRUE(origin.unique()); | 409 EXPECT_TRUE(origin.unique()); |
| 408 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 410 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 409 | 411 |
| 410 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 412 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 411 } | 413 } |
| 412 } | 414 } |
| 413 | 415 |
| 414 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { | 416 TEST(OriginTest, UnsafelyCreateUniqueViaEmbeddedNulls) { |
| 415 struct TestCases { | 417 struct TestCases { |
| 416 const char* scheme; | 418 const char* scheme; |
| 417 size_t scheme_length; | 419 size_t scheme_length; |
| 418 const char* host; | 420 const char* host; |
| 419 size_t host_length; | 421 size_t host_length; |
| 420 uint16_t port; | 422 uint16_t port; |
| 421 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, | 423 } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 422 {"http\0", 5, "example.com", 11, 80}, | 424 {"http\0", 5, "example.com", 11, 80}, |
| 423 {"\0http", 5, "example.com", 11, 80}, | 425 {"\0http", 5, "example.com", 11, 80}, |
| 424 {"http", 4, "example.com\0not-example.com", 27, 80}, | 426 {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 425 {"http", 4, "example.com\0", 12, 80}, | 427 {"http", 4, "example.com\0", 12, 80}, |
| 426 {"http", 4, "\0example.com", 12, 80}}; | 428 {"http", 4, "\0example.com", 12, 80}}; |
| 427 | 429 |
| 428 for (const auto& test : cases) { | 430 for (const auto& test : cases) { |
| 429 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" | 431 SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 430 << test.port); | 432 << test.port); |
| 431 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( | 433 url::Origin origin = url::Origin::UnsafelyCreateOriginWithoutNormalization( |
| 432 std::string(test.scheme, test.scheme_length), | 434 std::string(test.scheme, test.scheme_length), |
| 433 std::string(test.host, test.host_length), test.port); | 435 std::string(test.host, test.host_length), test.port); |
| 434 EXPECT_EQ("", origin.scheme()); | 436 EXPECT_EQ("", origin.scheme()); |
| 435 EXPECT_EQ("", origin.host()); | 437 EXPECT_EQ("", origin.host()); |
| 436 EXPECT_EQ(0, origin.port()); | 438 EXPECT_EQ(0, origin.port()); |
| 437 EXPECT_TRUE(origin.unique()); | 439 EXPECT_TRUE(origin.unique()); |
| 438 EXPECT_FALSE(origin.IsSameOriginWith(origin)); | 440 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 439 | 441 |
| 440 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); | 442 ExpectParsedUrlsEqual(GURL(origin.Serialize()), origin.GetURL()); |
| 441 } | 443 } |
| 442 } | 444 } |
| 443 | 445 |
| 444 TEST(OriginTest, DomainIs) { | 446 TEST(OriginTest, DomainIs) { |
| 445 const struct { | 447 const struct { |
| 446 const char* url; | 448 const char* url; |
| 447 const char* lower_ascii_domain; | 449 const char* lower_ascii_domain; |
| 448 bool expected_domain_is; | 450 bool expected_domain_is; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 GURL invalid_url("google.com"); | 494 GURL invalid_url("google.com"); |
| 493 ASSERT_FALSE(invalid_url.is_valid()); | 495 ASSERT_FALSE(invalid_url.is_valid()); |
| 494 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); | 496 EXPECT_FALSE(url::Origin(invalid_url).DomainIs("google.com")); |
| 495 | 497 |
| 496 // Unique origins. | 498 // Unique origins. |
| 497 EXPECT_FALSE(url::Origin().DomainIs("")); | 499 EXPECT_FALSE(url::Origin().DomainIs("")); |
| 498 EXPECT_FALSE(url::Origin().DomainIs("com")); | 500 EXPECT_FALSE(url::Origin().DomainIs("com")); |
| 499 } | 501 } |
| 500 | 502 |
| 501 } // namespace url | 503 } // namespace url |
| OLD | NEW |