blob: ae380f1742ef766dd6c1910c24e4f0e706a8f63d [file] [log] [blame]
[email protected]51bcc5d2013-04-24 01:41:371// 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]e7bba5f82013-04-10 20:10:524
avic0c60312015-12-21 21:03:505#include <stddef.h>
6
viettrungluu4b6915862014-10-16 03:42:497#include "base/macros.h"
[email protected]e7bba5f82013-04-10 20:10:528#include "testing/gtest/include/gtest/gtest.h"
tfarina018de6e2015-05-26 17:41:209#include "url/third_party/mozilla/url_parse.h"
[email protected]318076b2013-04-18 21:19:4510#include "url/url_canon.h"
11#include "url/url_canon_stdstring.h"
[email protected]318076b2013-04-18 21:19:4512#include "url/url_test_utils.h"
13#include "url/url_util.h"
[email protected]e7bba5f82013-04-10 20:10:5214
[email protected]0318f922014-04-22 00:09:2315namespace url {
16
Nick Carter123ca192018-03-30 23:25:3617class 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
29TEST_F(URLUtilTest, FindAndCompareScheme) {
[email protected]0318f922014-04-22 00:09:2330 Component found_scheme;
[email protected]e7bba5f82013-04-10 20:10:5231
32 // Simple case where the scheme is found and matches.
33 const char kStr1[] = "http://www.com/";
[email protected]0318f922014-04-22 00:09:2334 EXPECT_TRUE(FindAndCompareScheme(
[email protected]e7bba5f82013-04-10 20:10:5235 kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
[email protected]0318f922014-04-22 00:09:2336 EXPECT_TRUE(FindAndCompareScheme(
[email protected]e7bba5f82013-04-10 20:10:5237 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
[email protected]0318f922014-04-22 00:09:2338 EXPECT_TRUE(found_scheme == Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:5239
40 // A case where the scheme is found and doesn't match.
[email protected]0318f922014-04-22 00:09:2341 EXPECT_FALSE(FindAndCompareScheme(
[email protected]e7bba5f82013-04-10 20:10:5242 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
[email protected]0318f922014-04-22 00:09:2343 EXPECT_TRUE(found_scheme == Component(0, 4));
[email protected]e7bba5f82013-04-10 20:10:5244
45 // A case where there is no scheme.
46 const char kStr2[] = "httpfoobar";
[email protected]0318f922014-04-22 00:09:2347 EXPECT_FALSE(FindAndCompareScheme(
[email protected]e7bba5f82013-04-10 20:10:5248 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
[email protected]0318f922014-04-22 00:09:2349 EXPECT_TRUE(found_scheme == Component());
[email protected]e7bba5f82013-04-10 20:10:5250
51 // When there is an empty scheme, it should match the empty scheme.
52 const char kStr3[] = ":foo.com/";
[email protected]0318f922014-04-22 00:09:2353 EXPECT_TRUE(FindAndCompareScheme(
[email protected]e7bba5f82013-04-10 20:10:5254 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
[email protected]0318f922014-04-22 00:09:2355 EXPECT_TRUE(found_scheme == Component(0, 0));
[email protected]e7bba5f82013-04-10 20:10:5256
57 // But when there is no scheme, it should fail.
[email protected]0318f922014-04-22 00:09:2358 EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
59 EXPECT_TRUE(found_scheme == Component());
[email protected]e7bba5f82013-04-10 20:10:5260
qyearsley2bc727d2015-08-14 20:17:1561 // When there is a whitespace char in scheme, it should canonicalize the URL
[email protected]e7bba5f82013-04-10 20:10:5262 // before comparison.
63 const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
[email protected]0318f922014-04-22 00:09:2364 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]e7bba5f82013-04-10 20:10:5268
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]0318f922014-04-22 00:09:2372 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]e7bba5f82013-04-10 20:10:5276}
77
Nick Carter123ca192018-03-30 23:25:3678TEST_F(URLUtilTest, IsStandard) {
tyoshino11a7c9fe2015-08-19 08:51:4679 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 Carter123ca192018-03-30 23:25:3686TEST_F(URLUtilTest, IsReferrerScheme) {
lizeb5120f6dc2016-02-19 09:29:4487 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 Carter123ca192018-03-30 23:25:3694TEST_F(URLUtilTest, AddReferrerScheme) {
lizeb5120f6dc2016-02-19 09:29:4495 const char kFooScheme[] = "foo";
96 EXPECT_FALSE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
Nick Carter123ca192018-03-30 23:25:3697 AddReferrerScheme(kFooScheme, url::SCHEME_WITH_HOST);
lizeb5120f6dc2016-02-19 09:29:4498 EXPECT_TRUE(IsReferrerScheme(kFooScheme, Component(0, strlen(kFooScheme))));
99}
100
Nick Carter123ca192018-03-30 23:25:36101TEST_F(URLUtilTest, GetStandardSchemeType) {
tyoshino11a7c9fe2015-08-19 08:51:46102 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 Carter123ca192018-03-30 23:25:36109 EXPECT_EQ(url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION, scheme_type);
tyoshino11a7c9fe2015-08-19 08:51:46110
111 const char kFilesystemScheme[] = "filesystem";
Nick Carter123ca192018-03-30 23:25:36112 scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
tyoshino11a7c9fe2015-08-19 08:51:46113 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 Carter123ca192018-03-30 23:25:36119 scheme_type = url::SCHEME_WITH_HOST_PORT_AND_USER_INFORMATION;
tyoshino11a7c9fe2015-08-19 08:51:46120 EXPECT_FALSE(GetStandardSchemeType(kFooScheme,
121 Component(0, strlen(kFooScheme)),
122 &scheme_type));
123}
124
Nick Carter123ca192018-03-30 23:25:36125TEST_F(URLUtilTest, ReplaceComponents) {
[email protected]0318f922014-04-22 00:09:23126 Parsed parsed;
127 RawCanonOutputT<char> output;
128 Parsed new_parsed;
[email protected]e7bba5f82013-04-10 20:10:52129
130 // Check that the following calls do not cause crash
[email protected]0318f922014-04-22 00:09:23131 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]e7bba5f82013-04-10 20:10:52135 replacements.ClearRef();
[email protected]0318f922014-04-22 00:09:23136 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]e7bba5f82013-04-10 20:10:52139
140 replacements.ClearHost();
[email protected]0318f922014-04-22 00:09:23141 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]e7bba5f82013-04-10 20:10:52145}
146
147static std::string CheckReplaceScheme(const char* base_url,
148 const char* scheme) {
149 // Make sure the input is canonicalized.
[email protected]0318f922014-04-22 00:09:23150 RawCanonOutput<32> original;
151 Parsed original_parsed;
152 Canonicalize(base_url, strlen(base_url), true, NULL, &original,
153 &original_parsed);
[email protected]e7bba5f82013-04-10 20:10:52154
[email protected]0318f922014-04-22 00:09:23155 Replacements<char> replacements;
156 replacements.SetScheme(scheme, Component(0, strlen(scheme)));
[email protected]e7bba5f82013-04-10 20:10:52157
158 std::string output_string;
[email protected]0318f922014-04-22 00:09:23159 StdStringCanonOutput output(&output_string);
160 Parsed output_parsed;
161 ReplaceComponents(original.data(), original.length(), original_parsed,
162 replacements, NULL, &output, &output_parsed);
[email protected]e7bba5f82013-04-10 20:10:52163
164 output.Complete();
165 return output_string;
166}
167
Nick Carter123ca192018-03-30 23:25:36168TEST_F(URLUtilTest, ReplaceScheme) {
[email protected]e7bba5f82013-04-10 20:10:52169 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]369e84f72013-11-23 01:53:52193
Mike West01c25d42017-12-12 09:31:00194 EXPECT_EQ("http://example.com/%20hello%20#%20world",
[email protected]369e84f72013-11-23 01:53:52195 CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
[email protected]e7bba5f82013-04-10 20:10:52196}
197
Nick Carter123ca192018-03-30 23:25:36198TEST_F(URLUtilTest, DecodeURLEscapeSequences) {
[email protected]e7bba5f82013-04-10 20:10:52199 struct DecodeCase {
200 const char* input;
201 const char* output;
Kent Tamuradadd77a12018-01-18 01:36:09202 DecodeURLResult result;
[email protected]e7bba5f82013-04-10 20:10:52203 } decode_cases[] = {
Kent Tamuradadd77a12018-01-18 01:36:09204 {"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]e7bba5f82013-04-10 20:10:52226 };
227
viettrungluu4b6915862014-10-16 03:42:49228 for (size_t i = 0; i < arraysize(decode_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52229 const char* input = decode_cases[i].input;
[email protected]0318f922014-04-22 00:09:23230 RawCanonOutputT<base::char16> output;
Kent Tamuradadd77a12018-01-18 01:36:09231 EXPECT_EQ(decode_cases[i].result,
232 DecodeURLEscapeSequences(input, strlen(input), &output));
[email protected]e7bba5f82013-04-10 20:10:52233 EXPECT_EQ(decode_cases[i].output,
brettw1b8582f2016-11-03 20:37:17234 base::UTF16ToUTF8(base::string16(output.data(),
235 output.length())));
[email protected]e7bba5f82013-04-10 20:10:52236 }
237
238 // Our decode should decode %00
239 const char zero_input[] = "%00";
[email protected]0318f922014-04-22 00:09:23240 RawCanonOutputT<base::char16> zero_output;
241 DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
brettw1b8582f2016-11-03 20:37:17242 EXPECT_NE("%00", base::UTF16ToUTF8(
[email protected]0318f922014-04-22 00:09:23243 base::string16(zero_output.data(), zero_output.length())));
[email protected]e7bba5f82013-04-10 20:10:52244
245 // Test the error behavior for invalid UTF-8.
Kent Tamuradadd77a12018-01-18 01:36:09246 {
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]e7bba5f82013-04-10 20:10:52267}
268
Nick Carter123ca192018-03-30 23:25:36269TEST_F(URLUtilTest, TestEncodeURIComponent) {
[email protected]e7bba5f82013-04-10 20:10:52270 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]e60479fb2013-09-24 03:18:40280 "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
[email protected]e7bba5f82013-04-10 20:10:52281 {"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
viettrungluu4b6915862014-10-16 03:42:49293 for (size_t i = 0; i < arraysize(encode_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52294 const char* input = encode_cases[i].input;
[email protected]0318f922014-04-22 00:09:23295 RawCanonOutputT<char> buffer;
296 EncodeURIComponent(input, strlen(input), &buffer);
[email protected]e7bba5f82013-04-10 20:10:52297 std::string output(buffer.data(), buffer.length());
298 EXPECT_EQ(encode_cases[i].output, output);
299 }
300}
301
Nick Carter123ca192018-03-30 23:25:36302TEST_F(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
tyoshino11a7c9fe2015-08-19 08:51:46303 // This tests non-standard (in the sense that IsStandard() == false)
[email protected]e7bba5f82013-04-10 20:10:52304 // 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
bnc9d5d1412014-10-29 16:37:43317 // canonicalization rules.
[email protected]e7bba5f82013-04-10 20:10:52318 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
[email protected]f3e84332013-08-16 11:55:54319 {"data:/Path/../part/part2", "file.html", true,
320 "data:/Path/../part/file.html"},
[email protected]e7bba5f82013-04-10 20:10:52321 // Path URL canonicalization rules also apply to non-standard authority-
322 // based URLs.
[email protected]f3e84332013-08-16 11:55:54323 {"custom://Authority/", "file.html", true,
324 "custom://Authority/file.html"},
[email protected]e7bba5f82013-04-10 20:10:52325 {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
[email protected]f3e84332013-08-16 11:55:54326 {"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]e7bba5f82013-04-10 20:10:52332 // 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]f3e84332013-08-16 11:55:54336 {"content://content.Provider/", "//other.Provider", true,
337 "content://other.provider/"},
[email protected]e7bba5f82013-04-10 20:10:52338 // Resolving an absolute URL doesn't cause canonicalization of the
339 // result.
340 {"about:blank", "custom://Authority", true, "custom://Authority"},
[email protected]f3e84332013-08-16 11:55:54341 // 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]e7bba5f82013-04-10 20:10:52345 // 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]369e84f72013-11-23 01:53:52348 // 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" },
brettwe66ce872015-02-18 01:51:33356 // 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]e7bba5f82013-04-10 20:10:52365 };
366
viettrungluu4b6915862014-10-16 03:42:49367 for (size_t i = 0; i < arraysize(resolve_non_standard_cases); i++) {
[email protected]e7bba5f82013-04-10 20:10:52368 const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
[email protected]0318f922014-04-22 00:09:23369 Parsed base_parsed;
370 ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
[email protected]e7bba5f82013-04-10 20:10:52371
372 std::string resolved;
[email protected]0318f922014-04-22 00:09:23373 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]e7bba5f82013-04-10 20:10:52379 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]0318f922014-04-22 00:09:23386
Nick Carter123ca192018-03-30 23:25:36387TEST_F(URLUtilTest, TestNoRefComponent) {
qyearsley2bc727d2015-08-14 20:17:15388 // The hash-mark must be ignored when mailto: scheme is parsed,
389 // even if the URL has a base and relative part.
zherczeg.u-szeged1e2171c2014-12-04 11:52:36390 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 Carter123ca192018-03-30 23:25:36408TEST_F(URLUtilTest, PotentiallyDanglingMarkup) {
mkwstc9d6c1b2017-05-18 15:05:22409 struct ResolveRelativeCase {
410 const char* base;
411 const char* rel;
Mike West9e5ae902017-05-24 15:17:50412 bool potentially_dangling_markup;
mkwstc9d6c1b2017-05-18 15:05:22413 const char* out;
414 } cases[] = {
Mike West9e5ae902017-05-24 15:17:50415 {"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"},
mkwstc9d6c1b2017-05-18 15:05:22434 };
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 West9e5ae902017-05-24 15:17:50450 EXPECT_EQ(test.potentially_dangling_markup,
451 resolved_parsed.potentially_dangling_markup);
mkwstc9d6c1b2017-05-18 15:05:22452 EXPECT_EQ(test.out, resolved);
453 }
454}
455
Nick Carter123ca192018-03-30 23:25:36456TEST_F(URLUtilTest, TestDomainIs) {
pkalinnikov054f4032016-08-31 10:54:17457 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]0318f922014-04-22 00:09:23499} // namespace url