scheme_host_port_unittest.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. #include "url/scheme_host_port.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include "testing/gtest/include/gtest/gtest.h"
  8. #include "url/gurl.h"
  9. #include "url/url_util.h"
  10. namespace {
  11. class SchemeHostPortTest : public testing::Test {
  12. public:
  13. SchemeHostPortTest() = default;
  14. SchemeHostPortTest(const SchemeHostPortTest&) = delete;
  15. SchemeHostPortTest& operator=(const SchemeHostPortTest&) = delete;
  16. ~SchemeHostPortTest() override = default;
  17. private:
  18. url::ScopedSchemeRegistryForTests scoped_registry_;
  19. };
  20. void ExpectParsedUrlsEqual(const GURL& a, const GURL& b) {
  21. EXPECT_EQ(a, b);
  22. const url::Parsed& a_parsed = a.parsed_for_possibly_invalid_spec();
  23. const url::Parsed& b_parsed = b.parsed_for_possibly_invalid_spec();
  24. EXPECT_EQ(a_parsed.scheme.begin, b_parsed.scheme.begin);
  25. EXPECT_EQ(a_parsed.scheme.len, b_parsed.scheme.len);
  26. EXPECT_EQ(a_parsed.username.begin, b_parsed.username.begin);
  27. EXPECT_EQ(a_parsed.username.len, b_parsed.username.len);
  28. EXPECT_EQ(a_parsed.password.begin, b_parsed.password.begin);
  29. EXPECT_EQ(a_parsed.password.len, b_parsed.password.len);
  30. EXPECT_EQ(a_parsed.host.begin, b_parsed.host.begin);
  31. EXPECT_EQ(a_parsed.host.len, b_parsed.host.len);
  32. EXPECT_EQ(a_parsed.port.begin, b_parsed.port.begin);
  33. EXPECT_EQ(a_parsed.port.len, b_parsed.port.len);
  34. EXPECT_EQ(a_parsed.path.begin, b_parsed.path.begin);
  35. EXPECT_EQ(a_parsed.path.len, b_parsed.path.len);
  36. EXPECT_EQ(a_parsed.query.begin, b_parsed.query.begin);
  37. EXPECT_EQ(a_parsed.query.len, b_parsed.query.len);
  38. EXPECT_EQ(a_parsed.ref.begin, b_parsed.ref.begin);
  39. EXPECT_EQ(a_parsed.ref.len, b_parsed.ref.len);
  40. }
  41. TEST_F(SchemeHostPortTest, Invalid) {
  42. url::SchemeHostPort invalid;
  43. EXPECT_EQ("", invalid.scheme());
  44. EXPECT_EQ("", invalid.host());
  45. EXPECT_EQ(0, invalid.port());
  46. EXPECT_FALSE(invalid.IsValid());
  47. EXPECT_EQ(invalid, invalid);
  48. const char* urls[] = {
  49. // about:, data:, javascript: and other no-access schemes translate into
  50. // an invalid SchemeHostPort
  51. "about:blank", "about:blank#ref", "about:blank?query=123", "about:srcdoc",
  52. "about:srcdoc#ref", "about:srcdoc?query=123", "data:text/html,Hello!",
  53. "javascript:alert(1)",
  54. // GURLs where GURL::is_valid returns false translate into an invalid
  55. // SchemeHostPort.
  56. "file://example.com:443/etc/passwd", "#!^%!$!&*",
  57. // These schemes do not follow the generic URL syntax, so make sure we
  58. // treat them as invalid (scheme, host, port) tuples (even though such
  59. // URLs' _Origin_ might have a (scheme, host, port) tuple, they themselves
  60. // do not). This is only *implicitly* checked in the code, by means of
  61. // blob schemes not being standard, and filesystem schemes having type
  62. // SCHEME_WITHOUT_AUTHORITY. If conditions change such that the implicit
  63. // checks no longer hold, this policy should be made explicit.
  64. "blob:https://example.com/uuid-goes-here",
  65. "filesystem:https://example.com/temporary/yay.png"};
  66. for (auto* test : urls) {
  67. SCOPED_TRACE(test);
  68. GURL url(test);
  69. url::SchemeHostPort tuple(url);
  70. EXPECT_EQ("", tuple.scheme());
  71. EXPECT_EQ("", tuple.host());
  72. EXPECT_EQ(0, tuple.port());
  73. EXPECT_FALSE(tuple.IsValid());
  74. EXPECT_EQ(tuple, tuple);
  75. EXPECT_EQ(tuple, invalid);
  76. EXPECT_EQ(invalid, tuple);
  77. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  78. }
  79. }
  80. TEST_F(SchemeHostPortTest, ExplicitConstruction) {
  81. struct TestCases {
  82. const char* scheme;
  83. const char* host;
  84. uint16_t port;
  85. } cases[] = {
  86. {"http", "example.com", 80},
  87. {"http", "example.com", 123},
  88. {"http", "example.com", 0}, // 0 is a valid port for http.
  89. {"https", "example.com", 443},
  90. {"https", "example.com", 123},
  91. {"file", "", 0}, // 0 indicates "no port" for file: scheme.
  92. {"file", "example.com", 0},
  93. };
  94. for (const auto& test : cases) {
  95. SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
  96. << test.port);
  97. url::SchemeHostPort tuple(test.scheme, test.host, test.port);
  98. EXPECT_EQ(test.scheme, tuple.scheme());
  99. EXPECT_EQ(test.host, tuple.host());
  100. EXPECT_EQ(test.port, tuple.port());
  101. EXPECT_TRUE(tuple.IsValid());
  102. EXPECT_EQ(tuple, tuple);
  103. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  104. }
  105. }
  106. TEST_F(SchemeHostPortTest, InvalidConstruction) {
  107. struct TestCases {
  108. const char* scheme;
  109. const char* host;
  110. uint16_t port;
  111. } cases[] = {{"", "", 0},
  112. {"data", "", 0},
  113. {"blob", "", 0},
  114. {"filesystem", "", 0},
  115. {"http", "", 80},
  116. {"data", "example.com", 80},
  117. {"http", "☃.net", 80},
  118. {"http\nmore", "example.com", 80},
  119. {"http\rmore", "example.com", 80},
  120. {"http\n", "example.com", 80},
  121. {"http\r", "example.com", 80},
  122. {"http", "example.com\nnot-example.com", 80},
  123. {"http", "example.com\rnot-example.com", 80},
  124. {"http", "example.com\n", 80},
  125. {"http", "example.com\r", 80},
  126. {"file", "", 80}}; // Can''t have a port for file: scheme.
  127. for (const auto& test : cases) {
  128. SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
  129. << test.port);
  130. url::SchemeHostPort tuple(test.scheme, test.host, test.port);
  131. EXPECT_EQ("", tuple.scheme());
  132. EXPECT_EQ("", tuple.host());
  133. EXPECT_EQ(0, tuple.port());
  134. EXPECT_FALSE(tuple.IsValid());
  135. EXPECT_EQ(tuple, tuple);
  136. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  137. }
  138. }
  139. TEST_F(SchemeHostPortTest, InvalidConstructionWithEmbeddedNulls) {
  140. struct TestCases {
  141. const char* scheme;
  142. size_t scheme_length;
  143. const char* host;
  144. size_t host_length;
  145. uint16_t port;
  146. } cases[] = {{"http\0more", 9, "example.com", 11, 80},
  147. {"http\0", 5, "example.com", 11, 80},
  148. {"\0http", 5, "example.com", 11, 80},
  149. {"http", 4, "example.com\0not-example.com", 27, 80},
  150. {"http", 4, "example.com\0", 12, 80},
  151. {"http", 4, "\0example.com", 12, 80}};
  152. for (const auto& test : cases) {
  153. SCOPED_TRACE(testing::Message() << test.scheme << "://" << test.host << ":"
  154. << test.port);
  155. url::SchemeHostPort tuple(std::string(test.scheme, test.scheme_length),
  156. std::string(test.host, test.host_length),
  157. test.port);
  158. EXPECT_EQ("", tuple.scheme());
  159. EXPECT_EQ("", tuple.host());
  160. EXPECT_EQ(0, tuple.port());
  161. EXPECT_FALSE(tuple.IsValid());
  162. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  163. }
  164. }
  165. TEST_F(SchemeHostPortTest, GURLConstruction) {
  166. struct TestCases {
  167. const char* url;
  168. const char* scheme;
  169. const char* host;
  170. uint16_t port;
  171. } cases[] = {
  172. {"http://192.168.9.1/", "http", "192.168.9.1", 80},
  173. {"http://[2001:db8::1]/", "http", "[2001:db8::1]", 80},
  174. {"http://☃.net/", "http", "xn--n3h.net", 80},
  175. {"http://example.com/", "http", "example.com", 80},
  176. {"http://example.com:123/", "http", "example.com", 123},
  177. {"https://example.com/", "https", "example.com", 443},
  178. {"https://example.com:123/", "https", "example.com", 123},
  179. {"file:///etc/passwd", "file", "", 0},
  180. {"file://example.com/etc/passwd", "file", "example.com", 0},
  181. {"http://u:p@example.com/", "http", "example.com", 80},
  182. {"http://u:p@example.com/path", "http", "example.com", 80},
  183. {"http://u:p@example.com/path?123", "http", "example.com", 80},
  184. {"http://u:p@example.com/path?123#hash", "http", "example.com", 80},
  185. };
  186. for (const auto& test : cases) {
  187. SCOPED_TRACE(test.url);
  188. GURL url(test.url);
  189. EXPECT_TRUE(url.is_valid());
  190. url::SchemeHostPort tuple(url);
  191. EXPECT_EQ(test.scheme, tuple.scheme());
  192. EXPECT_EQ(test.host, tuple.host());
  193. EXPECT_EQ(test.port, tuple.port());
  194. EXPECT_TRUE(tuple.IsValid());
  195. EXPECT_EQ(tuple, tuple);
  196. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  197. }
  198. }
  199. TEST_F(SchemeHostPortTest, Serialization) {
  200. struct TestCases {
  201. const char* url;
  202. const char* expected;
  203. } cases[] = {
  204. {"http://192.168.9.1/", "http://192.168.9.1"},
  205. {"http://[2001:db8::1]/", "http://[2001:db8::1]"},
  206. {"http://☃.net/", "http://xn--n3h.net"},
  207. {"http://example.com/", "http://example.com"},
  208. {"http://example.com:123/", "http://example.com:123"},
  209. {"https://example.com/", "https://example.com"},
  210. {"https://example.com:123/", "https://example.com:123"},
  211. {"file:///etc/passwd", "file://"},
  212. {"file://example.com/etc/passwd", "file://example.com"},
  213. {"https://example.com:0/", "https://example.com:0"},
  214. };
  215. for (const auto& test : cases) {
  216. SCOPED_TRACE(test.url);
  217. GURL url(test.url);
  218. url::SchemeHostPort tuple(url);
  219. EXPECT_EQ(test.expected, tuple.Serialize());
  220. ExpectParsedUrlsEqual(GURL(tuple.Serialize()), tuple.GetURL());
  221. }
  222. }
  223. TEST_F(SchemeHostPortTest, Comparison) {
  224. // These tuples are arranged in increasing order:
  225. struct SchemeHostPorts {
  226. const char* scheme;
  227. const char* host;
  228. uint16_t port;
  229. } tuples[] = {
  230. {"http", "a", 80},
  231. {"http", "b", 80},
  232. {"https", "a", 80},
  233. {"https", "b", 80},
  234. {"http", "a", 81},
  235. {"http", "b", 81},
  236. {"https", "a", 81},
  237. {"https", "b", 81},
  238. };
  239. for (size_t i = 0; i < std::size(tuples); i++) {
  240. url::SchemeHostPort current(tuples[i].scheme, tuples[i].host,
  241. tuples[i].port);
  242. for (size_t j = i; j < std::size(tuples); j++) {
  243. url::SchemeHostPort to_compare(tuples[j].scheme, tuples[j].host,
  244. tuples[j].port);
  245. EXPECT_EQ(i < j, current < to_compare) << i << " < " << j;
  246. EXPECT_EQ(j < i, to_compare < current) << j << " < " << i;
  247. }
  248. }
  249. }
  250. // Some schemes have optional authority. Make sure that GURL conversion from
  251. // SchemeHostPort is not opinionated in that regard. For more info, See
  252. // crbug.com/820194, where we considered all SchemeHostPorts with
  253. // SCHEME_WITH_HOST (i.e., without ports) as valid with empty hosts, even though
  254. // most are not (e.g. chrome URLs).
  255. TEST_F(SchemeHostPortTest, EmptyHostGurlConversion) {
  256. url::AddStandardScheme("chrome", url::SCHEME_WITH_HOST);
  257. GURL chrome_url("chrome:");
  258. EXPECT_FALSE(chrome_url.is_valid());
  259. url::SchemeHostPort chrome_tuple("chrome", "", 0);
  260. EXPECT_FALSE(chrome_tuple.GetURL().is_valid());
  261. ExpectParsedUrlsEqual(GURL(chrome_tuple.Serialize()), chrome_tuple.GetURL());
  262. ExpectParsedUrlsEqual(chrome_url, chrome_tuple.GetURL());
  263. }
  264. } // namespace url