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