OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "url/origin.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 #include "url/gurl.h" |
| 8 |
| 9 namespace { |
| 10 |
| 11 TEST(OriginTest, UniqueOriginComparison) { |
| 12 url::Origin unique_origin; |
| 13 EXPECT_EQ("", unique_origin.scheme()); |
| 14 EXPECT_EQ("", unique_origin.host()); |
| 15 EXPECT_EQ(0, unique_origin.port()); |
| 16 EXPECT_TRUE(unique_origin.unique()); |
| 17 EXPECT_FALSE(unique_origin.IsSameOriginWith(unique_origin)); |
| 18 |
| 19 const char* const urls[] = {"data:text/html,Hello!", |
| 20 "javascript:alert(1)", |
| 21 "file://example.com:443/etc/passwd", |
| 22 "yay", |
| 23 "http::///invalid.example.com/"}; |
| 24 |
| 25 for (const auto& test_url : urls) { |
| 26 SCOPED_TRACE(test_url); |
| 27 GURL url(test_url); |
| 28 url::Origin origin(url); |
| 29 EXPECT_EQ("", origin.scheme()); |
| 30 EXPECT_EQ("", origin.host()); |
| 31 EXPECT_EQ(0, origin.port()); |
| 32 EXPECT_TRUE(origin.unique()); |
| 33 EXPECT_FALSE(origin.IsSameOriginWith(origin)); |
| 34 EXPECT_FALSE(unique_origin.IsSameOriginWith(origin)); |
| 35 EXPECT_FALSE(origin.IsSameOriginWith(unique_origin)); |
| 36 } |
| 37 } |
| 38 |
| 39 TEST(OriginTest, ConstructFromGURL) { |
| 40 url::Origin different_origin(GURL("https://not-in-the-list.test/")); |
| 41 |
| 42 struct TestCases { |
| 43 const char* const url; |
| 44 const char* const expected_scheme; |
| 45 const char* const expected_host; |
| 46 const uint16 expected_port; |
| 47 } cases[] = { |
| 48 // IP Addresses |
| 49 {"http://192.168.9.1/", "http", "192.168.9.1", 80}, |
| 50 {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, |
| 51 |
| 52 // Punycode |
| 53 {"http://☃.net/", "http", "xn--n3h.net", 80}, |
| 54 {"blob:http://☃.net/", "http", "xn--n3h.net", 80}, |
| 55 |
| 56 // Generic URLs |
| 57 {"http://example.com/", "http", "example.com", 80}, |
| 58 {"http://example.com:123/", "http", "example.com", 123}, |
| 59 {"https://example.com/", "https", "example.com", 443}, |
| 60 {"https://example.com:123/", "https", "example.com", 123}, |
| 61 {"http://user:pass@example.com/", "http", "example.com", 80}, |
| 62 {"http://example.com:123/?query", "http", "example.com", 123}, |
| 63 {"https://example.com/#1234", "https", "example.com", 443}, |
| 64 {"https://u:p@example.com:123/?query#1234", "https", "example.com", 123}, |
| 65 |
| 66 // Registered URLs |
| 67 {"ftp://example.com/", "ftp", "example.com", 21}, |
| 68 {"gopher://example.com/", "gopher", "example.com", 70}, |
| 69 {"ws://example.com/", "ws", "example.com", 80}, |
| 70 {"wss://example.com/", "wss", "example.com", 443}, |
| 71 |
| 72 // file: URLs |
| 73 {"file:///etc/passwd", "file", "", 0}, |
| 74 {"file://example.com/etc/passwd", "file", "example.com", 0}, |
| 75 |
| 76 // Filesystem: |
| 77 {"filesystem:http://example.com/type/", "http", "example.com", 80}, |
| 78 {"filesystem:http://example.com:123/type/", "http", "example.com", 123}, |
| 79 {"filesystem:https://example.com/type/", "https", "example.com", 443}, |
| 80 {"filesystem:https://example.com:123/type/", "https", "example.com", 123}, |
| 81 |
| 82 // Blob: |
| 83 {"blob:http://example.com/guid-goes-here", "http", "example.com", 80}, |
| 84 {"blob:http://example.com:123/guid-goes-here", "http", "example.com", 123}
, |
| 85 {"blob:https://example.com/guid-goes-here", "https", "example.com", 443}, |
| 86 {"blob:http://u:p@example.com/guid-goes-here", "http", "example.com", 80}, |
| 87 }; |
| 88 |
| 89 for (const auto& test_case : cases) { |
| 90 SCOPED_TRACE(test_case.url); |
| 91 GURL url(test_case.url); |
| 92 EXPECT_TRUE(url.is_valid()); |
| 93 url::Origin origin(url); |
| 94 EXPECT_EQ(test_case.expected_scheme, origin.scheme()); |
| 95 EXPECT_EQ(test_case.expected_host, origin.host()); |
| 96 EXPECT_EQ(test_case.expected_port, origin.port()); |
| 97 EXPECT_FALSE(origin.unique()); |
| 98 EXPECT_TRUE(origin.IsSameOriginWith(origin)); |
| 99 EXPECT_FALSE(different_origin.IsSameOriginWith(origin)); |
| 100 EXPECT_FALSE(origin.IsSameOriginWith(different_origin)); |
| 101 } |
| 102 } |
| 103 |
| 104 TEST(OriginTest, Serialization) { |
| 105 struct TestCases { |
| 106 const char* const url; |
| 107 const char* const expected; |
| 108 } cases[] = { |
| 109 {"http://192.168.9.1/", "http://192.168.9.1"}, |
| 110 {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| 111 {"http://☃.net/", "http://xn--n3h.net"}, |
| 112 {"http://example.com/", "http://example.com"}, |
| 113 {"http://example.com:123/", "http://example.com:123"}, |
| 114 {"https://example.com/", "https://example.com"}, |
| 115 {"https://example.com:123/", "https://example.com:123"}, |
| 116 {"file:///etc/passwd", "file://"}, |
| 117 {"file://example.com/etc/passwd", "file://"}, |
| 118 }; |
| 119 |
| 120 for (const auto& test_case : cases) { |
| 121 SCOPED_TRACE(test_case.url); |
| 122 GURL url(test_case.url); |
| 123 EXPECT_TRUE(url.is_valid()); |
| 124 url::Origin origin(url); |
| 125 EXPECT_EQ(test_case.expected, origin.Serialize()); |
| 126 |
| 127 // The '<<' operator should produce the same serialization as Serialize(). |
| 128 std::stringstream out; |
| 129 out << origin; |
| 130 EXPECT_EQ(test_case.expected, out.str()); |
| 131 } |
| 132 } |
| 133 |
| 134 TEST(OriginTest, Comparison) { |
| 135 // These URLs are arranged in increasing order: |
| 136 const char* const urls[] = { |
| 137 "data:uniqueness", |
| 138 "http://a:80", |
| 139 "http://b:80", |
| 140 "https://a:80", |
| 141 "https://b:80", |
| 142 "http://a:81", |
| 143 "http://b:81", |
| 144 "https://a:81", |
| 145 "https://b:81", |
| 146 }; |
| 147 |
| 148 for (size_t i = 0; i < arraysize(urls); i++) { |
| 149 GURL current_url(urls[i]); |
| 150 url::Origin current(current_url); |
| 151 for (size_t j = i; j < arraysize(urls); j++) { |
| 152 GURL compare_url(urls[j]); |
| 153 url::Origin to_compare(compare_url); |
| 154 EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; |
| 155 EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; |
| 156 } |
| 157 } |
| 158 } |
| 159 |
| 160 } // namespace url |
OLD | NEW |