[email protected] | 51bcc5d | 2013-04-24 01:41:37 | [diff] [blame] | 1 | // Copyright 2013 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. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 4 | |
avi | c0c6031 | 2015-12-21 21:03:50 | [diff] [blame] | 5 | #include <stddef.h> |
| 6 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 7 | #include "base/macros.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 8 | #include "testing/gtest/include/gtest/gtest.h" |
tfarina | 018de6e | 2015-05-26 17:41:20 | [diff] [blame] | 9 | #include "url/third_party/mozilla/url_parse.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 10 | #include "url/url_canon.h" |
| 11 | #include "url/url_canon_stdstring.h" |
[email protected] | 318076b | 2013-04-18 21:19:45 | [diff] [blame] | 12 | #include "url/url_test_utils.h" |
| 13 | #include "url/url_util.h" |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 14 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 15 | namespace url { |
| 16 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 17 | class URLUtilTest : public testing::Test { |
| 18 | public: |
| 19 | URLUtilTest() = default; |
| 20 | ~URLUtilTest() override { |
| 21 | // Reset any added schemes. |
| 22 | Shutdown(); |
| 23 | } |
| 24 | |
| 25 | private: |
| 26 | DISALLOW_COPY_AND_ASSIGN(URLUtilTest); |
| 27 | }; |
| 28 | |
| 29 | TEST_F(URLUtilTest, FindAndCompareScheme) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 30 | Component found_scheme; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 31 | |
| 32 | // Simple case where the scheme is found and matches. |
| 33 | const char kStr1[] = "http://www.com/"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 34 | EXPECT_TRUE(FindAndCompareScheme( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 35 | kStr1, static_cast<int>(strlen(kStr1)), "http", NULL)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 36 | EXPECT_TRUE(FindAndCompareScheme( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 37 | kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 38 | EXPECT_TRUE(found_scheme == Component(0, 4)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 39 | |
| 40 | // A case where the scheme is found and doesn't match. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 41 | EXPECT_FALSE(FindAndCompareScheme( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 42 | kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 43 | EXPECT_TRUE(found_scheme == Component(0, 4)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 44 | |
| 45 | // A case where there is no scheme. |
| 46 | const char kStr2[] = "httpfoobar"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 47 | EXPECT_FALSE(FindAndCompareScheme( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 48 | kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 49 | EXPECT_TRUE(found_scheme == Component()); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 50 | |
| 51 | // When there is an empty scheme, it should match the empty scheme. |
| 52 | const char kStr3[] = ":foo.com/"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 53 | EXPECT_TRUE(FindAndCompareScheme( |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 54 | kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme)); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 55 | EXPECT_TRUE(found_scheme == Component(0, 0)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 56 | |
| 57 | // But when there is no scheme, it should fail. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 58 | EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme)); |
| 59 | EXPECT_TRUE(found_scheme == Component()); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 60 | |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 61 | // When there is a whitespace char in scheme, it should canonicalize the URL |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 62 | // before comparison. |
| 63 | const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 64 | EXPECT_TRUE(FindAndCompareScheme(whtspc_str, |
| 65 | static_cast<int>(strlen(whtspc_str)), |
| 66 | "javascript", &found_scheme)); |
| 67 | EXPECT_TRUE(found_scheme == Component(1, 10)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 68 | |
| 69 | // Control characters should be stripped out on the ends, and kept in the |
| 70 | // middle. |
| 71 | const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 72 | EXPECT_FALSE(FindAndCompareScheme(ctrl_str, |
| 73 | static_cast<int>(strlen(ctrl_str)), |
| 74 | "javascript", &found_scheme)); |
| 75 | EXPECT_TRUE(found_scheme == Component(1, 11)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 76 | } |
| 77 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 78 | TEST_F(URLUtilTest, IsStandard) { |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 79 | const char kHTTPScheme[] = "http"; |
| 80 | EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme)))); |
| 81 | |
| 82 | const char kFooScheme[] = "foo"; |
| 83 | EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme)))); |
| 84 | } |
| 85 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 86 | TEST_F(URLUtilTest, IsReferrerScheme) { |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 87 | const char kHTTPScheme[] = "http"; |
| 88 | EXPECT_TRUE(IsReferrerScheme(kHTTPScheme, Component(0, strlen(kHTTPScheme)))); |
| 89 | |
| 90 | const char kFooScheme[] = "foo"; |
| 91 | EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme)))); |
| 92 | } |
| 93 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 94 | TEST_F(URLUtilTest, AddReferrerScheme) { |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 95 | const char kFooScheme[] = "foo"; |
| 96 | EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme)))); |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 97 | AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST); |
lizeb | 5120f6dc | 2016-02-19 09:29:44 | [diff] [blame] | 98 | EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme)))); |
| 99 | } |
| 100 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 101 | TEST_F(URLUtilTest, GetStandardSchemeType) { |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 102 | url::SchemeType scheme_type; |
| 103 | |
| 104 | const char kHTTPScheme[] = "http"; |
| 105 | scheme_type = url::SCHEME_WITHOUT_AUTHORITY; |
| 106 | EXPECT_TRUE(GetStandardSchemeType(kHTTPScheme, |
| 107 | Component(0, strlen(kHTTPScheme)), |
| 108 | &scheme_type)); |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 109 | EXPECT_EQ(url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, scheme_type); |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 110 | |
| 111 | const char kFilesystemScheme[] = "filesystem"; |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 112 | scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION; |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 113 | EXPECT_TRUE(GetStandardSchemeType(kFilesystemScheme, |
| 114 | Component(0, strlen(kFilesystemScheme)), |
| 115 | &scheme_type)); |
| 116 | EXPECT_EQ(url::SCHEME_WITHOUT_AUTHORITY, scheme_type); |
| 117 | |
| 118 | const char kFooScheme[] = "foo"; |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 119 | scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION; |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 120 | EXPECT_FALSE(GetStandardSchemeType(kFooScheme, |
| 121 | Component(0, strlen(kFooScheme)), |
| 122 | &scheme_type)); |
| 123 | } |
| 124 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 125 | TEST_F(URLUtilTest, ReplaceComponents) { |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 126 | Parsed parsed; |
| 127 | RawCanonOutputT<char> output; |
| 128 | Parsed new_parsed; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 129 | |
| 130 | // Check that the following calls do not cause crash |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 131 | Replacements<char> replacements; |
| 132 | replacements.SetRef("test", Component(0, 4)); |
| 133 | ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed); |
| 134 | ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 135 | replacements.ClearRef(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 136 | replacements.SetHost("test", Component(0, 4)); |
| 137 | ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed); |
| 138 | ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 139 | |
| 140 | replacements.ClearHost(); |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 141 | ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed); |
| 142 | ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed); |
| 143 | ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed); |
| 144 | ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static std::string CheckReplaceScheme(const char* base_url, |
| 148 | const char* scheme) { |
| 149 | // Make sure the input is canonicalized. |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 150 | RawCanonOutput<32> original; |
| 151 | Parsed original_parsed; |
| 152 | Canonicalize(base_url, strlen(base_url), true, NULL, &original, |
| 153 | &original_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 154 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 155 | Replacements<char> replacements; |
| 156 | replacements.SetScheme(scheme, Component(0, strlen(scheme))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 157 | |
| 158 | std::string output_string; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 159 | StdStringCanonOutput output(&output_string); |
| 160 | Parsed output_parsed; |
| 161 | ReplaceComponents(original.data(), original.length(), original_parsed, |
| 162 | replacements, NULL, &output, &output_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 163 | |
| 164 | output.Complete(); |
| 165 | return output_string; |
| 166 | } |
| 167 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 168 | TEST_F(URLUtilTest, ReplaceScheme) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 169 | EXPECT_EQ("https://google.com/", |
| 170 | CheckReplaceScheme("http://google.com/", "https")); |
| 171 | EXPECT_EQ("file://google.com/", |
| 172 | CheckReplaceScheme("http://google.com/", "file")); |
| 173 | EXPECT_EQ("http://home/Build", |
| 174 | CheckReplaceScheme("file:///Home/Build", "http")); |
| 175 | EXPECT_EQ("javascript:foo", |
| 176 | CheckReplaceScheme("about:foo", "javascript")); |
| 177 | EXPECT_EQ("://google.com/", |
| 178 | CheckReplaceScheme("http://google.com/", "")); |
| 179 | EXPECT_EQ("http://google.com/", |
| 180 | CheckReplaceScheme("about:google.com", "http")); |
| 181 | EXPECT_EQ("http:", CheckReplaceScheme("", "http")); |
| 182 | |
| 183 | #ifdef WIN32 |
| 184 | // Magic Windows drive letter behavior when converting to a file URL. |
| 185 | EXPECT_EQ("file:///E:/foo/", |
| 186 | CheckReplaceScheme("http://localhost/e:foo/", "file")); |
| 187 | #endif |
| 188 | |
| 189 | // This will probably change to "about://google.com/" when we fix |
| 190 | // http://crbug.com/160 which should also be an acceptable result. |
| 191 | EXPECT_EQ("about://google.com/", |
| 192 | CheckReplaceScheme("http://google.com/", "about")); |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 193 | |
Mike West | 01c25d4 | 2017-12-12 09:31:00 | [diff] [blame] | 194 | EXPECT_EQ("http://example.com/%20hello%20#%20world", |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 195 | CheckReplaceScheme("myscheme:example.com/ hello # world ", "http")); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 196 | } |
| 197 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 198 | TEST_F(URLUtilTest, DecodeURLEscapeSequences) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 199 | struct DecodeCase { |
| 200 | const char* input; |
| 201 | const char* output; |
Kent Tamura | dadd77a1 | 2018-01-18 01:36:09 | [diff] [blame] | 202 | DecodeURLResult result; |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 203 | } decode_cases[] = { |
Kent Tamura | dadd77a1 | 2018-01-18 01:36:09 | [diff] [blame] | 204 | {"hello, world", "hello, world", DecodeURLResult::kAsciiOnly}, |
| 205 | {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/", |
| 206 | "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/", |
| 207 | DecodeURLResult::kAsciiOnly}, |
| 208 | {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/", |
| 209 | "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/", |
| 210 | DecodeURLResult::kAsciiOnly}, |
| 211 | {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/", |
| 212 | " !\"#$%&'()*+,-.//", DecodeURLResult::kAsciiOnly}, |
| 213 | {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/", "0123456789:;<=>?/", |
| 214 | DecodeURLResult::kAsciiOnly}, |
| 215 | {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/", "@ABCDEFGHIJKLMNO/", |
| 216 | DecodeURLResult::kAsciiOnly}, |
| 217 | {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/", |
| 218 | "PQRSTUVWXYZ[\\]^_/", DecodeURLResult::kAsciiOnly}, |
| 219 | {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/", "`abcdefghijklmno/", |
| 220 | DecodeURLResult::kAsciiOnly}, |
| 221 | {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/", |
| 222 | "pqrstuvwxyz{|}~\x7f/", DecodeURLResult::kAsciiOnly}, |
| 223 | // Test un-UTF-8-ization. |
| 224 | {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd", |
| 225 | DecodeURLResult::kUTF8}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 226 | }; |
| 227 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 228 | for (size_t i = 0; i < arraysize(decode_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 229 | const char* input = decode_cases[i].input; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 230 | RawCanonOutputT<base::char16> output; |
Kent Tamura | dadd77a1 | 2018-01-18 01:36:09 | [diff] [blame] | 231 | EXPECT_EQ(decode_cases[i].result, |
| 232 | DecodeURLEscapeSequences(input, strlen(input), &output)); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 233 | EXPECT_EQ(decode_cases[i].output, |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 234 | base::UTF16ToUTF8(base::string16(output.data(), |
| 235 | output.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // Our decode should decode %00 |
| 239 | const char zero_input[] = "%00"; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 240 | RawCanonOutputT<base::char16> zero_output; |
| 241 | DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output); |
brettw | 1b8582f | 2016-11-03 20:37:17 | [diff] [blame] | 242 | EXPECT_NE("%00", base::UTF16ToUTF8( |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 243 | base::string16(zero_output.data(), zero_output.length()))); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 244 | |
| 245 | // Test the error behavior for invalid UTF-8. |
Kent Tamura | dadd77a1 | 2018-01-18 01:36:09 | [diff] [blame] | 246 | { |
| 247 | const char invalid_input[] = "%e4%a0%e5%a5%bd"; |
| 248 | const base::char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0}; |
| 249 | RawCanonOutputT<base::char16> invalid_output; |
| 250 | EXPECT_EQ(DecodeURLResult::kMixed, |
| 251 | DecodeURLEscapeSequences(invalid_input, strlen(invalid_input), |
| 252 | &invalid_output)); |
| 253 | EXPECT_EQ(base::string16(invalid_expected), |
| 254 | base::string16(invalid_output.data(), invalid_output.length())); |
| 255 | } |
| 256 | { |
| 257 | const char invalid_input[] = "%e4%a0%e5%bd"; |
| 258 | const base::char16 invalid_expected[5] = {0x00e4, 0x00a0, 0x00e5, 0x00bd, |
| 259 | 0}; |
| 260 | RawCanonOutputT<base::char16> invalid_output; |
| 261 | EXPECT_EQ(DecodeURLResult::kIsomorphic, |
| 262 | DecodeURLEscapeSequences(invalid_input, strlen(invalid_input), |
| 263 | &invalid_output)); |
| 264 | EXPECT_EQ(base::string16(invalid_expected), |
| 265 | base::string16(invalid_output.data(), invalid_output.length())); |
| 266 | } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 267 | } |
| 268 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 269 | TEST_F(URLUtilTest, TestEncodeURIComponent) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 270 | struct EncodeCase { |
| 271 | const char* input; |
| 272 | const char* output; |
| 273 | } encode_cases[] = { |
| 274 | {"hello, world", "hello%2C%20world"}, |
| 275 | {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F", |
| 276 | "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"}, |
| 277 | {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F", |
| 278 | "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"}, |
| 279 | {" !\"#$%&'()*+,-./", |
[email protected] | e60479fb | 2013-09-24 03:18:40 | [diff] [blame] | 280 | "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 281 | {"0123456789:;<=>?", |
| 282 | "0123456789%3A%3B%3C%3D%3E%3F"}, |
| 283 | {"@ABCDEFGHIJKLMNO", |
| 284 | "%40ABCDEFGHIJKLMNO"}, |
| 285 | {"PQRSTUVWXYZ[\\]^_", |
| 286 | "PQRSTUVWXYZ%5B%5C%5D%5E_"}, |
| 287 | {"`abcdefghijklmno", |
| 288 | "%60abcdefghijklmno"}, |
| 289 | {"pqrstuvwxyz{|}~\x7f", |
| 290 | "pqrstuvwxyz%7B%7C%7D~%7F"}, |
| 291 | }; |
| 292 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 293 | for (size_t i = 0; i < arraysize(encode_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 294 | const char* input = encode_cases[i].input; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 295 | RawCanonOutputT<char> buffer; |
| 296 | EncodeURIComponent(input, strlen(input), &buffer); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 297 | std::string output(buffer.data(), buffer.length()); |
| 298 | EXPECT_EQ(encode_cases[i].output, output); |
| 299 | } |
| 300 | } |
| 301 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 302 | TEST_F(URLUtilTest, TestResolveRelativeWithNonStandardBase) { |
tyoshino | 11a7c9fe | 2015-08-19 08:51:46 | [diff] [blame] | 303 | // This tests non-standard (in the sense that IsStandard() == false) |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 304 | // hierarchical schemes. |
| 305 | struct ResolveRelativeCase { |
| 306 | const char* base; |
| 307 | const char* rel; |
| 308 | bool is_valid; |
| 309 | const char* out; |
| 310 | } resolve_non_standard_cases[] = { |
| 311 | // Resolving a relative path against a non-hierarchical URL should fail. |
| 312 | {"scheme:opaque_data", "/path", false, ""}, |
| 313 | // Resolving a relative path against a non-standard authority-based base |
| 314 | // URL doesn't alter the authority section. |
| 315 | {"scheme://Authority/", "../path", true, "scheme://Authority/path"}, |
| 316 | // A non-standard hierarchical base is resolved with path URL |
bnc | 9d5d141 | 2014-10-29 16:37:43 | [diff] [blame] | 317 | // canonicalization rules. |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 318 | {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"}, |
[email protected] | f3e8433 | 2013-08-16 11:55:54 | [diff] [blame] | 319 | {"data:/Path/../part/part2", "file.html", true, |
| 320 | "data:/Path/../part/file.html"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 321 | // Path URL canonicalization rules also apply to non-standard authority- |
| 322 | // based URLs. |
[email protected] | f3e8433 | 2013-08-16 11:55:54 | [diff] [blame] | 323 | {"custom://Authority/", "file.html", true, |
| 324 | "custom://Authority/file.html"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 325 | {"custom://Authority/", "other://Auth/", true, "other://Auth/"}, |
[email protected] | f3e8433 | 2013-08-16 11:55:54 | [diff] [blame] | 326 | {"custom://Authority/", "../../file.html", true, |
| 327 | "custom://Authority/file.html"}, |
| 328 | {"custom://Authority/path/", "file.html", true, |
| 329 | "custom://Authority/path/file.html"}, |
| 330 | {"custom://Authority:NoCanon/path/", "file.html", true, |
| 331 | "custom://Authority:NoCanon/path/file.html"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 332 | // It's still possible to get an invalid path URL. |
| 333 | {"custom://Invalid:!#Auth/", "file.html", false, ""}, |
| 334 | // A path with an authority section gets canonicalized under standard URL |
| 335 | // rules, even though the base was non-standard. |
[email protected] | f3e8433 | 2013-08-16 11:55:54 | [diff] [blame] | 336 | {"content://content.Provider/", "//other.Provider", true, |
| 337 | "content://other.provider/"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 338 | // Resolving an absolute URL doesn't cause canonicalization of the |
| 339 | // result. |
| 340 | {"about:blank", "custom://Authority", true, "custom://Authority"}, |
[email protected] | f3e8433 | 2013-08-16 11:55:54 | [diff] [blame] | 341 | // Fragment URLs can be resolved against a non-standard base. |
| 342 | {"scheme://Authority/path", "#fragment", true, |
| 343 | "scheme://Authority/path#fragment"}, |
| 344 | {"scheme://Authority/", "#fragment", true, "scheme://Authority/#fragment"}, |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 345 | // Resolving should fail if the base URL is authority-based but is |
| 346 | // missing a path component (the '/' at the end). |
| 347 | {"scheme://Authority", "path", false, ""}, |
[email protected] | 369e84f7 | 2013-11-23 01:53:52 | [diff] [blame] | 348 | // Test resolving a fragment (only) against any kind of base-URL. |
| 349 | {"about:blank", "#id42", true, "about:blank#id42" }, |
| 350 | {"about:blank", " #id42", true, "about:blank#id42" }, |
| 351 | {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag" }, |
| 352 | // A surprising side effect of allowing fragments to resolve against |
| 353 | // any URL scheme is we might break javascript: URLs by doing so... |
| 354 | {"javascript:alert('foo#bar')", "#badfrag", true, |
| 355 | "javascript:alert('foo#badfrag" }, |
brettw | e66ce87 | 2015-02-18 01:51:33 | [diff] [blame] | 356 | // In this case, the backslashes will not be canonicalized because it's a |
| 357 | // non-standard URL, but they will be treated as a path separators, |
| 358 | // giving the base URL here a path of "\". |
| 359 | // |
| 360 | // The result here is somewhat arbitrary. One could argue it should be |
| 361 | // either "aaa://a\" or "aaa://a/" since the path is being replaced with |
| 362 | // the "current directory". But in the context of resolving on data URLs, |
| 363 | // adding the requested dot doesn't seem wrong either. |
| 364 | {"aaa://a\\", "aaa:.", true, "aaa://a\\." } |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 365 | }; |
| 366 | |
viettrungluu | 4b691586 | 2014-10-16 03:42:49 | [diff] [blame] | 367 | for (size_t i = 0; i < arraysize(resolve_non_standard_cases); i++) { |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 368 | const ResolveRelativeCase& test_data = resolve_non_standard_cases[i]; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 369 | Parsed base_parsed; |
| 370 | ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 371 | |
| 372 | std::string resolved; |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 373 | StdStringCanonOutput output(&resolved); |
| 374 | Parsed resolved_parsed; |
| 375 | bool valid = ResolveRelative(test_data.base, strlen(test_data.base), |
| 376 | base_parsed, test_data.rel, |
| 377 | strlen(test_data.rel), NULL, &output, |
| 378 | &resolved_parsed); |
[email protected] | e7bba5f8 | 2013-04-10 20:10:52 | [diff] [blame] | 379 | output.Complete(); |
| 380 | |
| 381 | EXPECT_EQ(test_data.is_valid, valid) << i; |
| 382 | if (test_data.is_valid && valid) |
| 383 | EXPECT_EQ(test_data.out, resolved) << i; |
| 384 | } |
| 385 | } |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 386 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 387 | TEST_F(URLUtilTest, TestNoRefComponent) { |
qyearsley | 2bc727d | 2015-08-14 20:17:15 | [diff] [blame] | 388 | // The hash-mark must be ignored when mailto: scheme is parsed, |
| 389 | // even if the URL has a base and relative part. |
zherczeg.u-szeged | 1e2171c | 2014-12-04 11:52:36 | [diff] [blame] | 390 | const char* base = "mailto://to/"; |
| 391 | const char* rel = "any#body"; |
| 392 | |
| 393 | Parsed base_parsed; |
| 394 | ParsePathURL(base, strlen(base), false, &base_parsed); |
| 395 | |
| 396 | std::string resolved; |
| 397 | StdStringCanonOutput output(&resolved); |
| 398 | Parsed resolved_parsed; |
| 399 | |
| 400 | bool valid = ResolveRelative(base, strlen(base), |
| 401 | base_parsed, rel, |
| 402 | strlen(rel), NULL, &output, |
| 403 | &resolved_parsed); |
| 404 | EXPECT_TRUE(valid); |
| 405 | EXPECT_FALSE(resolved_parsed.ref.is_valid()); |
| 406 | } |
| 407 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 408 | TEST_F(URLUtilTest, PotentiallyDanglingMarkup) { |
mkwst | c9d6c1b | 2017-05-18 15:05:22 | [diff] [blame] | 409 | struct ResolveRelativeCase { |
| 410 | const char* base; |
| 411 | const char* rel; |
Mike West | 9e5ae90 | 2017-05-24 15:17:50 | [diff] [blame] | 412 | bool potentially_dangling_markup; |
mkwst | c9d6c1b | 2017-05-18 15:05:22 | [diff] [blame] | 413 | const char* out; |
| 414 | } cases[] = { |
Mike West | 9e5ae90 | 2017-05-24 15:17:50 | [diff] [blame] | 415 | {"https://example.com/", "/path<", false, "https://example.com/path%3C"}, |
| 416 | {"https://example.com/", "\n/path<", true, "https://example.com/path%3C"}, |
| 417 | {"https://example.com/", "\r/path<", true, "https://example.com/path%3C"}, |
| 418 | {"https://example.com/", "\t/path<", true, "https://example.com/path%3C"}, |
| 419 | {"https://example.com/", "/pa\nth<", true, "https://example.com/path%3C"}, |
| 420 | {"https://example.com/", "/pa\rth<", true, "https://example.com/path%3C"}, |
| 421 | {"https://example.com/", "/pa\tth<", true, "https://example.com/path%3C"}, |
| 422 | {"https://example.com/", "/path\n<", true, "https://example.com/path%3C"}, |
| 423 | {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"}, |
| 424 | {"https://example.com/", "/path\r<", true, "https://example.com/path%3C"}, |
| 425 | {"https://example.com/", "\n/<path", true, "https://example.com/%3Cpath"}, |
| 426 | {"https://example.com/", "\r/<path", true, "https://example.com/%3Cpath"}, |
| 427 | {"https://example.com/", "\t/<path", true, "https://example.com/%3Cpath"}, |
| 428 | {"https://example.com/", "/<pa\nth", true, "https://example.com/%3Cpath"}, |
| 429 | {"https://example.com/", "/<pa\rth", true, "https://example.com/%3Cpath"}, |
| 430 | {"https://example.com/", "/<pa\tth", true, "https://example.com/%3Cpath"}, |
| 431 | {"https://example.com/", "/<path\n", true, "https://example.com/%3Cpath"}, |
| 432 | {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"}, |
| 433 | {"https://example.com/", "/<path\r", true, "https://example.com/%3Cpath"}, |
mkwst | c9d6c1b | 2017-05-18 15:05:22 | [diff] [blame] | 434 | }; |
| 435 | |
| 436 | for (const auto& test : cases) { |
| 437 | SCOPED_TRACE(::testing::Message() << test.base << ", " << test.rel); |
| 438 | Parsed base_parsed; |
| 439 | ParseStandardURL(test.base, strlen(test.base), &base_parsed); |
| 440 | |
| 441 | std::string resolved; |
| 442 | StdStringCanonOutput output(&resolved); |
| 443 | Parsed resolved_parsed; |
| 444 | bool valid = |
| 445 | ResolveRelative(test.base, strlen(test.base), base_parsed, test.rel, |
| 446 | strlen(test.rel), NULL, &output, &resolved_parsed); |
| 447 | ASSERT_TRUE(valid); |
| 448 | output.Complete(); |
| 449 | |
Mike West | 9e5ae90 | 2017-05-24 15:17:50 | [diff] [blame] | 450 | EXPECT_EQ(test.potentially_dangling_markup, |
| 451 | resolved_parsed.potentially_dangling_markup); |
mkwst | c9d6c1b | 2017-05-18 15:05:22 | [diff] [blame] | 452 | EXPECT_EQ(test.out, resolved); |
| 453 | } |
| 454 | } |
| 455 | |
Nick Carter | 123ca19 | 2018-03-30 23:25:36 | [diff] [blame^] | 456 | TEST_F(URLUtilTest, TestDomainIs) { |
pkalinnikov | 054f403 | 2016-08-31 10:54:17 | [diff] [blame] | 457 | const struct { |
| 458 | const char* canonicalized_host; |
| 459 | const char* lower_ascii_domain; |
| 460 | bool expected_domain_is; |
| 461 | } kTestCases[] = { |
| 462 | {"google.com", "google.com", true}, |
| 463 | {"www.google.com", "google.com", true}, // Subdomain is ignored. |
| 464 | {"www.google.com.cn", "google.com", false}, // Different TLD. |
| 465 | {"www.google.comm", "google.com", false}, |
| 466 | {"www.iamnotgoogle.com", "google.com", false}, // Different hostname. |
| 467 | {"www.google.com", "Google.com", false}, // The input is not lower-cased. |
| 468 | |
| 469 | // If the host ends with a dot, it matches domains with or without a dot. |
| 470 | {"www.google.com.", "google.com", true}, |
| 471 | {"www.google.com.", "google.com.", true}, |
| 472 | {"www.google.com.", ".com", true}, |
| 473 | {"www.google.com.", ".com.", true}, |
| 474 | |
| 475 | // But, if the host doesn't end with a dot and the input domain does, then |
| 476 | // it's considered to not match. |
| 477 | {"www.google.com", "google.com.", false}, |
| 478 | |
| 479 | // If the host ends with two dots, it doesn't match. |
| 480 | {"www.google.com..", "google.com", false}, |
| 481 | |
| 482 | // Empty parameters. |
| 483 | {"www.google.com", "", false}, |
| 484 | {"", "www.google.com", false}, |
| 485 | {"", "", false}, |
| 486 | }; |
| 487 | |
| 488 | for (const auto& test_case : kTestCases) { |
| 489 | SCOPED_TRACE(testing::Message() << "(host, domain): (" |
| 490 | << test_case.canonicalized_host << ", " |
| 491 | << test_case.lower_ascii_domain << ")"); |
| 492 | |
| 493 | EXPECT_EQ( |
| 494 | test_case.expected_domain_is, |
| 495 | DomainIs(test_case.canonicalized_host, test_case.lower_ascii_domain)); |
| 496 | } |
| 497 | } |
| 498 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 499 | } // namespace url |