Roll src/third_party/WebKit 10b2b4a:a6818f9 (svn 202548:202549)
[chromium-blink-merge.git] / url / url_util_unittest.cc
blobb89bfa162e0db7558d7e50e072b466d2f1b3d321
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.
5 #include "base/macros.h"
6 #include "testing/gtest/include/gtest/gtest.h"
7 #include "url/third_party/mozilla/url_parse.h"
8 #include "url/url_canon.h"
9 #include "url/url_canon_stdstring.h"
10 #include "url/url_test_utils.h"
11 #include "url/url_util.h"
13 namespace url {
15 TEST(URLUtilTest, FindAndCompareScheme) {
16 Component found_scheme;
18 // Simple case where the scheme is found and matches.
19 const char kStr1[] = "http://www.com/";
20 EXPECT_TRUE(FindAndCompareScheme(
21 kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
22 EXPECT_TRUE(FindAndCompareScheme(
23 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
24 EXPECT_TRUE(found_scheme == Component(0, 4));
26 // A case where the scheme is found and doesn't match.
27 EXPECT_FALSE(FindAndCompareScheme(
28 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
29 EXPECT_TRUE(found_scheme == Component(0, 4));
31 // A case where there is no scheme.
32 const char kStr2[] = "httpfoobar";
33 EXPECT_FALSE(FindAndCompareScheme(
34 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
35 EXPECT_TRUE(found_scheme == Component());
37 // When there is an empty scheme, it should match the empty scheme.
38 const char kStr3[] = ":foo.com/";
39 EXPECT_TRUE(FindAndCompareScheme(
40 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
41 EXPECT_TRUE(found_scheme == Component(0, 0));
43 // But when there is no scheme, it should fail.
44 EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
45 EXPECT_TRUE(found_scheme == Component());
47 // When there is a whitespace char in scheme, it should canonicalize the URL
48 // before comparison.
49 const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
50 EXPECT_TRUE(FindAndCompareScheme(whtspc_str,
51 static_cast<int>(strlen(whtspc_str)),
52 "javascript", &found_scheme));
53 EXPECT_TRUE(found_scheme == Component(1, 10));
55 // Control characters should be stripped out on the ends, and kept in the
56 // middle.
57 const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)";
58 EXPECT_FALSE(FindAndCompareScheme(ctrl_str,
59 static_cast<int>(strlen(ctrl_str)),
60 "javascript", &found_scheme));
61 EXPECT_TRUE(found_scheme == Component(1, 11));
64 TEST(URLUtilTest, IsStandard) {
65 const char kHTTPScheme[] = "http";
66 EXPECT_TRUE(IsStandard(kHTTPScheme, Component(0, strlen(kHTTPScheme))));
68 const char kFooScheme[] = "foo";
69 EXPECT_FALSE(IsStandard(kFooScheme, Component(0, strlen(kFooScheme))));
72 TEST(URLUtilTest, GetStandardSchemeType) {
73 url::SchemeType scheme_type;
75 const char kHTTPScheme[] = "http";
76 scheme_type = url::SCHEME_WITHOUT_AUTHORITY;
77 EXPECT_TRUE(GetStandardSchemeType(kHTTPScheme,
78 Component(0, strlen(kHTTPScheme)),
79 &scheme_type));
80 EXPECT_EQ(url::SCHEME_WITH_PORT, scheme_type);
82 const char kFilesystemScheme[] = "filesystem";
83 scheme_type = url::SCHEME_WITH_PORT;
84 EXPECT_TRUE(GetStandardSchemeType(kFilesystemScheme,
85 Component(0, strlen(kFilesystemScheme)),
86 &scheme_type));
87 EXPECT_EQ(url::SCHEME_WITHOUT_AUTHORITY, scheme_type);
89 const char kFooScheme[] = "foo";
90 scheme_type = url::SCHEME_WITH_PORT;
91 EXPECT_FALSE(GetStandardSchemeType(kFooScheme,
92 Component(0, strlen(kFooScheme)),
93 &scheme_type));
96 TEST(URLUtilTest, ReplaceComponents) {
97 Parsed parsed;
98 RawCanonOutputT<char> output;
99 Parsed new_parsed;
101 // Check that the following calls do not cause crash
102 Replacements<char> replacements;
103 replacements.SetRef("test", Component(0, 4));
104 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
105 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
106 replacements.ClearRef();
107 replacements.SetHost("test", Component(0, 4));
108 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
109 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
111 replacements.ClearHost();
112 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
113 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
114 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
115 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
118 static std::string CheckReplaceScheme(const char* base_url,
119 const char* scheme) {
120 // Make sure the input is canonicalized.
121 RawCanonOutput<32> original;
122 Parsed original_parsed;
123 Canonicalize(base_url, strlen(base_url), true, NULL, &original,
124 &original_parsed);
126 Replacements<char> replacements;
127 replacements.SetScheme(scheme, Component(0, strlen(scheme)));
129 std::string output_string;
130 StdStringCanonOutput output(&output_string);
131 Parsed output_parsed;
132 ReplaceComponents(original.data(), original.length(), original_parsed,
133 replacements, NULL, &output, &output_parsed);
135 output.Complete();
136 return output_string;
139 TEST(URLUtilTest, ReplaceScheme) {
140 EXPECT_EQ("https://google.com/",
141 CheckReplaceScheme("http://google.com/", "https"));
142 EXPECT_EQ("file://google.com/",
143 CheckReplaceScheme("http://google.com/", "file"));
144 EXPECT_EQ("http://home/Build",
145 CheckReplaceScheme("file:///Home/Build", "http"));
146 EXPECT_EQ("javascript:foo",
147 CheckReplaceScheme("about:foo", "javascript"));
148 EXPECT_EQ("://google.com/",
149 CheckReplaceScheme("http://google.com/", ""));
150 EXPECT_EQ("http://google.com/",
151 CheckReplaceScheme("about:google.com", "http"));
152 EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
154 #ifdef WIN32
155 // Magic Windows drive letter behavior when converting to a file URL.
156 EXPECT_EQ("file:///E:/foo/",
157 CheckReplaceScheme("http://localhost/e:foo/", "file"));
158 #endif
160 // This will probably change to "about://google.com/" when we fix
161 // http://crbug.com/160 which should also be an acceptable result.
162 EXPECT_EQ("about://google.com/",
163 CheckReplaceScheme("http://google.com/", "about"));
165 EXPECT_EQ("http://example.com/%20hello%20# world",
166 CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
169 TEST(URLUtilTest, DecodeURLEscapeSequences) {
170 struct DecodeCase {
171 const char* input;
172 const char* output;
173 } decode_cases[] = {
174 {"hello, world", "hello, world"},
175 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/",
176 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
177 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/",
178 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
179 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/",
180 " !\"#$%&'()*+,-.//"},
181 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/",
182 "0123456789:;<=>?/"},
183 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/",
184 "@ABCDEFGHIJKLMNO/"},
185 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/",
186 "PQRSTUVWXYZ[\\]^_/"},
187 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/",
188 "`abcdefghijklmno/"},
189 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/",
190 "pqrstuvwxyz{|}~\x7f/"},
191 // Test un-UTF-8-ization.
192 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
195 for (size_t i = 0; i < arraysize(decode_cases); i++) {
196 const char* input = decode_cases[i].input;
197 RawCanonOutputT<base::char16> output;
198 DecodeURLEscapeSequences(input, strlen(input), &output);
199 EXPECT_EQ(decode_cases[i].output,
200 test_utils::ConvertUTF16ToUTF8(base::string16(output.data(),
201 output.length())));
204 // Our decode should decode %00
205 const char zero_input[] = "%00";
206 RawCanonOutputT<base::char16> zero_output;
207 DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
208 EXPECT_NE("%00", test_utils::ConvertUTF16ToUTF8(
209 base::string16(zero_output.data(), zero_output.length())));
211 // Test the error behavior for invalid UTF-8.
212 const char invalid_input[] = "%e4%a0%e5%a5%bd";
213 const base::char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0};
214 RawCanonOutputT<base::char16> invalid_output;
215 DecodeURLEscapeSequences(invalid_input, strlen(invalid_input),
216 &invalid_output);
217 EXPECT_EQ(base::string16(invalid_expected),
218 base::string16(invalid_output.data(), invalid_output.length()));
221 TEST(URLUtilTest, TestEncodeURIComponent) {
222 struct EncodeCase {
223 const char* input;
224 const char* output;
225 } encode_cases[] = {
226 {"hello, world", "hello%2C%20world"},
227 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
228 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
229 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
230 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
231 {" !\"#$%&'()*+,-./",
232 "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
233 {"0123456789:;<=>?",
234 "0123456789%3A%3B%3C%3D%3E%3F"},
235 {"@ABCDEFGHIJKLMNO",
236 "%40ABCDEFGHIJKLMNO"},
237 {"PQRSTUVWXYZ[\\]^_",
238 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
239 {"`abcdefghijklmno",
240 "%60abcdefghijklmno"},
241 {"pqrstuvwxyz{|}~\x7f",
242 "pqrstuvwxyz%7B%7C%7D~%7F"},
245 for (size_t i = 0; i < arraysize(encode_cases); i++) {
246 const char* input = encode_cases[i].input;
247 RawCanonOutputT<char> buffer;
248 EncodeURIComponent(input, strlen(input), &buffer);
249 std::string output(buffer.data(), buffer.length());
250 EXPECT_EQ(encode_cases[i].output, output);
254 TEST(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
255 // This tests non-standard (in the sense that IsStandard() == false)
256 // hierarchical schemes.
257 struct ResolveRelativeCase {
258 const char* base;
259 const char* rel;
260 bool is_valid;
261 const char* out;
262 } resolve_non_standard_cases[] = {
263 // Resolving a relative path against a non-hierarchical URL should fail.
264 {"scheme:opaque_data", "/path", false, ""},
265 // Resolving a relative path against a non-standard authority-based base
266 // URL doesn't alter the authority section.
267 {"scheme://Authority/", "../path", true, "scheme://Authority/path"},
268 // A non-standard hierarchical base is resolved with path URL
269 // canonicalization rules.
270 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
271 {"data:/Path/../part/part2", "file.html", true,
272 "data:/Path/../part/file.html"},
273 // Path URL canonicalization rules also apply to non-standard authority-
274 // based URLs.
275 {"custom://Authority/", "file.html", true,
276 "custom://Authority/file.html"},
277 {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
278 {"custom://Authority/", "../../file.html", true,
279 "custom://Authority/file.html"},
280 {"custom://Authority/path/", "file.html", true,
281 "custom://Authority/path/file.html"},
282 {"custom://Authority:NoCanon/path/", "file.html", true,
283 "custom://Authority:NoCanon/path/file.html"},
284 // It's still possible to get an invalid path URL.
285 {"custom://Invalid:!#Auth/", "file.html", false, ""},
286 // A path with an authority section gets canonicalized under standard URL
287 // rules, even though the base was non-standard.
288 {"content://content.Provider/", "//other.Provider", true,
289 "content://other.provider/"},
290 // Resolving an absolute URL doesn't cause canonicalization of the
291 // result.
292 {"about:blank", "custom://Authority", true, "custom://Authority"},
293 // Fragment URLs can be resolved against a non-standard base.
294 {"scheme://Authority/path", "#fragment", true,
295 "scheme://Authority/path#fragment"},
296 {"scheme://Authority/", "#fragment", true, "scheme://Authority/#fragment"},
297 // Resolving should fail if the base URL is authority-based but is
298 // missing a path component (the '/' at the end).
299 {"scheme://Authority", "path", false, ""},
300 // Test resolving a fragment (only) against any kind of base-URL.
301 {"about:blank", "#id42", true, "about:blank#id42" },
302 {"about:blank", " #id42", true, "about:blank#id42" },
303 {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag" },
304 // A surprising side effect of allowing fragments to resolve against
305 // any URL scheme is we might break javascript: URLs by doing so...
306 {"javascript:alert('foo#bar')", "#badfrag", true,
307 "javascript:alert('foo#badfrag" },
308 // In this case, the backslashes will not be canonicalized because it's a
309 // non-standard URL, but they will be treated as a path separators,
310 // giving the base URL here a path of "\".
312 // The result here is somewhat arbitrary. One could argue it should be
313 // either "aaa://a\" or "aaa://a/" since the path is being replaced with
314 // the "current directory". But in the context of resolving on data URLs,
315 // adding the requested dot doesn't seem wrong either.
316 {"aaa://a\\", "aaa:.", true, "aaa://a\\." }
319 for (size_t i = 0; i < arraysize(resolve_non_standard_cases); i++) {
320 const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
321 Parsed base_parsed;
322 ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
324 std::string resolved;
325 StdStringCanonOutput output(&resolved);
326 Parsed resolved_parsed;
327 bool valid = ResolveRelative(test_data.base, strlen(test_data.base),
328 base_parsed, test_data.rel,
329 strlen(test_data.rel), NULL, &output,
330 &resolved_parsed);
331 output.Complete();
333 EXPECT_EQ(test_data.is_valid, valid) << i;
334 if (test_data.is_valid && valid)
335 EXPECT_EQ(test_data.out, resolved) << i;
339 TEST(URLUtilTest, TestNoRefComponent) {
340 // The hash-mark must be ignored when mailto: scheme is parsed,
341 // even if the URL has a base and relative part.
342 const char* base = "mailto://to/";
343 const char* rel = "any#body";
345 Parsed base_parsed;
346 ParsePathURL(base, strlen(base), false, &base_parsed);
348 std::string resolved;
349 StdStringCanonOutput output(&resolved);
350 Parsed resolved_parsed;
352 bool valid = ResolveRelative(base, strlen(base),
353 base_parsed, rel,
354 strlen(rel), NULL, &output,
355 &resolved_parsed);
356 EXPECT_TRUE(valid);
357 EXPECT_FALSE(resolved_parsed.ref.is_valid());
360 } // namespace url