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