mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 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 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 5 | #include <stddef.h> |
| 6 | #include <stdint.h> |
| 7 | |
| 8 | #include "base/macros.h" |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | #include "url/gurl.h" |
| 11 | #include "url/scheme_host_port.h" |
| 12 | |
| 13 | namespace { |
| 14 | |
| 15 | TEST(SchemeHostPortTest, Invalid) { |
| 16 | url::SchemeHostPort invalid; |
| 17 | EXPECT_EQ("", invalid.scheme()); |
| 18 | EXPECT_EQ("", invalid.host()); |
| 19 | EXPECT_EQ(0, invalid.port()); |
| 20 | EXPECT_TRUE(invalid.IsInvalid()); |
| 21 | EXPECT_TRUE(invalid.Equals(invalid)); |
| 22 | |
| 23 | const char* urls[] = {"data:text/html,Hello!", |
| 24 | "javascript:alert(1)", |
| 25 | "file://example.com:443/etc/passwd", |
| 26 | "blob:https://example.com/uuid-goes-here", |
| 27 | "filesystem:https://example.com/temporary/yay.png"}; |
| 28 | |
| 29 | for (const auto& test : urls) { |
| 30 | SCOPED_TRACE(test); |
| 31 | GURL url(test); |
| 32 | url::SchemeHostPort tuple(url); |
| 33 | EXPECT_EQ("", tuple.scheme()); |
| 34 | EXPECT_EQ("", tuple.host()); |
| 35 | EXPECT_EQ(0, tuple.port()); |
| 36 | EXPECT_TRUE(tuple.IsInvalid()); |
| 37 | EXPECT_TRUE(tuple.Equals(tuple)); |
| 38 | EXPECT_TRUE(tuple.Equals(invalid)); |
| 39 | EXPECT_TRUE(invalid.Equals(tuple)); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | TEST(SchemeHostPortTest, ExplicitConstruction) { |
| 44 | struct TestCases { |
| 45 | const char* scheme; |
| 46 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 47 | uint16_t port; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 48 | } cases[] = { |
| 49 | {"http", "example.com", 80}, |
| 50 | {"http", "example.com", 123}, |
| 51 | {"https", "example.com", 443}, |
| 52 | {"https", "example.com", 123}, |
| 53 | {"file", "", 0}, |
| 54 | {"file", "example.com", 0}, |
| 55 | }; |
| 56 | |
| 57 | for (const auto& test : cases) { |
| 58 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 59 | << test.port); |
| 60 | url::SchemeHostPort tuple(test.scheme, test.host, test.port); |
| 61 | EXPECT_EQ(test.scheme, tuple.scheme()); |
| 62 | EXPECT_EQ(test.host, tuple.host()); |
| 63 | EXPECT_EQ(test.port, tuple.port()); |
| 64 | EXPECT_FALSE(tuple.IsInvalid()); |
| 65 | EXPECT_TRUE(tuple.Equals(tuple)); |
| 66 | } |
| 67 | } |
| 68 | |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 69 | TEST(SchemeHostPortTest, InvalidConstruction) { |
| 70 | struct TestCases { |
| 71 | const char* scheme; |
| 72 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 73 | uint16_t port; |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 74 | } cases[] = {{"", "", 0}, |
| 75 | {"data", "", 0}, |
| 76 | {"blob", "", 0}, |
| 77 | {"filesystem", "", 0}, |
| 78 | {"http", "", 80}, |
| 79 | {"data", "example.com", 80}, |
| 80 | {"http", "☃.net", 80}, |
| 81 | {"http\nmore", "example.com", 80}, |
| 82 | {"http\rmore", "example.com", 80}, |
| 83 | {"http\n", "example.com", 80}, |
| 84 | {"http\r", "example.com", 80}, |
| 85 | {"http", "example.com\nnot-example.com", 80}, |
| 86 | {"http", "example.com\rnot-example.com", 80}, |
| 87 | {"http", "example.com\n", 80}, |
| 88 | {"http", "example.com\r", 80}, |
| 89 | {"http", "example.com", 0}, |
| 90 | {"file", "", 80}}; |
| 91 | |
| 92 | for (const auto& test : cases) { |
| 93 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 94 | << test.port); |
| 95 | url::SchemeHostPort tuple(test.scheme, test.host, test.port); |
| 96 | EXPECT_EQ("", tuple.scheme()); |
| 97 | EXPECT_EQ("", tuple.host()); |
| 98 | EXPECT_EQ(0, tuple.port()); |
| 99 | EXPECT_TRUE(tuple.IsInvalid()); |
| 100 | EXPECT_TRUE(tuple.Equals(tuple)); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | TEST(SchemeHostPortTest, InvalidConstructionWithEmbeddedNulls) { |
| 105 | struct TestCases { |
| 106 | const char* scheme; |
| 107 | size_t scheme_length; |
| 108 | const char* host; |
| 109 | size_t host_length; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 110 | uint16_t port; |
mkwst | d8335d98 | 2015-07-25 05:18:48 | [diff] [blame] | 111 | } cases[] = {{"http\0more", 9, "example.com", 11, 80}, |
| 112 | {"http\0", 5, "example.com", 11, 80}, |
| 113 | {"\0http", 5, "example.com", 11, 80}, |
| 114 | {"http", 4, "example.com\0not-example.com", 27, 80}, |
| 115 | {"http", 4, "example.com\0", 12, 80}, |
| 116 | {"http", 4, "\0example.com", 12, 80}}; |
| 117 | |
| 118 | for (const auto& test : cases) { |
| 119 | SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":" |
| 120 | << test.port); |
| 121 | url::SchemeHostPort tuple(std::string(test.scheme, test.scheme_length), |
| 122 | std::string(test.host, test.host_length), |
| 123 | test.port); |
| 124 | EXPECT_EQ("", tuple.scheme()); |
| 125 | EXPECT_EQ("", tuple.host()); |
| 126 | EXPECT_EQ(0, tuple.port()); |
| 127 | EXPECT_TRUE(tuple.IsInvalid()); |
| 128 | } |
| 129 | } |
| 130 | |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 131 | TEST(SchemeHostPortTest, GURLConstruction) { |
| 132 | struct TestCases { |
| 133 | const char* url; |
| 134 | const char* scheme; |
| 135 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 136 | uint16_t port; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 137 | } cases[] = { |
| 138 | {"http://192.168.9.1/", "http", "192.168.9.1", 80}, |
| 139 | {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80}, |
| 140 | {"http://☃.net/", "http", "xn--n3h.net", 80}, |
| 141 | {"http://example.com/", "http", "example.com", 80}, |
| 142 | {"http://example.com:123/", "http", "example.com", 123}, |
| 143 | {"https://example.com/", "https", "example.com", 443}, |
| 144 | {"https://example.com:123/", "https", "example.com", 123}, |
| 145 | {"file:///etc/passwd", "file", "", 0}, |
| 146 | {"file://example.com/etc/passwd", "file", "example.com", 0}, |
| 147 | {"http://u:p@example.com/", "http", "example.com", 80}, |
| 148 | {"http://u:p@example.com/path", "http", "example.com", 80}, |
| 149 | {"http://u:p@example.com/path?123", "http", "example.com", 80}, |
| 150 | {"http://u:p@example.com/path?123#hash", "http", "example.com", 80}, |
| 151 | }; |
| 152 | |
| 153 | for (const auto& test : cases) { |
| 154 | SCOPED_TRACE(test.url); |
| 155 | GURL url(test.url); |
| 156 | EXPECT_TRUE(url.is_valid()); |
| 157 | url::SchemeHostPort tuple(url); |
| 158 | EXPECT_EQ(test.scheme, tuple.scheme()); |
| 159 | EXPECT_EQ(test.host, tuple.host()); |
| 160 | EXPECT_EQ(test.port, tuple.port()); |
| 161 | EXPECT_FALSE(tuple.IsInvalid()); |
| 162 | EXPECT_TRUE(tuple.Equals(tuple)); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | TEST(SchemeHostPortTest, Serialization) { |
| 167 | struct TestCases { |
| 168 | const char* url; |
| 169 | const char* expected; |
| 170 | } cases[] = { |
| 171 | {"http://192.168.9.1/", "http://192.168.9.1"}, |
| 172 | {"http://[2001:db8::1]/", "http://[2001:db8::1]"}, |
| 173 | {"http://☃.net/", "http://xn--n3h.net"}, |
| 174 | {"http://example.com/", "http://example.com"}, |
| 175 | {"http://example.com:123/", "http://example.com:123"}, |
| 176 | {"https://example.com/", "https://example.com"}, |
| 177 | {"https://example.com:123/", "https://example.com:123"}, |
| 178 | {"file:///etc/passwd", "file://"}, |
| 179 | {"file://example.com/etc/passwd", "file://example.com"}, |
| 180 | }; |
| 181 | |
| 182 | for (const auto& test : cases) { |
| 183 | SCOPED_TRACE(test.url); |
| 184 | GURL url(test.url); |
| 185 | url::SchemeHostPort tuple(url); |
| 186 | EXPECT_EQ(test.expected, tuple.Serialize()); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | TEST(SchemeHostPortTest, Comparison) { |
| 191 | // These tuples are arranged in increasing order: |
| 192 | struct SchemeHostPorts { |
| 193 | const char* scheme; |
| 194 | const char* host; |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame^] | 195 | uint16_t port; |
mkwst | 28c7c11 | 2015-07-14 22:41:06 | [diff] [blame] | 196 | } tuples[] = { |
| 197 | {"http", "a", 80}, |
| 198 | {"http", "b", 80}, |
| 199 | {"https", "a", 80}, |
| 200 | {"https", "b", 80}, |
| 201 | {"http", "a", 81}, |
| 202 | {"http", "b", 81}, |
| 203 | {"https", "a", 81}, |
| 204 | {"https", "b", 81}, |
| 205 | }; |
| 206 | |
| 207 | for (size_t i = 0; i < arraysize(tuples); i++) { |
| 208 | url::SchemeHostPort current(tuples[i].scheme, tuples[i].host, |
| 209 | tuples[i].port); |
| 210 | for (size_t j = i; j < arraysize(tuples); j++) { |
| 211 | url::SchemeHostPort to_compare(tuples[j].scheme, tuples[j].host, |
| 212 | tuples[j].port); |
| 213 | EXPECT_EQ(i < j, current < to_compare) << i << " < " << j; |
| 214 | EXPECT_EQ(j < i, to_compare < current) << j << " < " << i; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | } // namespace url |