Blink roll f2c66a2:e5f2db4 (svn 182211:182214)
[chromium-blink-merge.git] / url / url_util_unittest.cc
blobf84b70d0cfab12e14878782f6331c42f8e40a968
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 "testing/gtest/include/gtest/gtest.h"
6 #include "url/url_canon.h"
7 #include "url/url_canon_stdstring.h"
8 #include "url/url_parse.h"
9 #include "url/url_test_utils.h"
10 #include "url/url_util.h"
12 namespace url {
14 TEST(URLUtilTest, FindAndCompareScheme) {
15 Component found_scheme;
17 // Simple case where the scheme is found and matches.
18 const char kStr1[] = "http://www.com/";
19 EXPECT_TRUE(FindAndCompareScheme(
20 kStr1, static_cast<int>(strlen(kStr1)), "http", NULL));
21 EXPECT_TRUE(FindAndCompareScheme(
22 kStr1, static_cast<int>(strlen(kStr1)), "http", &found_scheme));
23 EXPECT_TRUE(found_scheme == Component(0, 4));
25 // A case where the scheme is found and doesn't match.
26 EXPECT_FALSE(FindAndCompareScheme(
27 kStr1, static_cast<int>(strlen(kStr1)), "https", &found_scheme));
28 EXPECT_TRUE(found_scheme == Component(0, 4));
30 // A case where there is no scheme.
31 const char kStr2[] = "httpfoobar";
32 EXPECT_FALSE(FindAndCompareScheme(
33 kStr2, static_cast<int>(strlen(kStr2)), "http", &found_scheme));
34 EXPECT_TRUE(found_scheme == Component());
36 // When there is an empty scheme, it should match the empty scheme.
37 const char kStr3[] = ":foo.com/";
38 EXPECT_TRUE(FindAndCompareScheme(
39 kStr3, static_cast<int>(strlen(kStr3)), "", &found_scheme));
40 EXPECT_TRUE(found_scheme == Component(0, 0));
42 // But when there is no scheme, it should fail.
43 EXPECT_FALSE(FindAndCompareScheme("", 0, "", &found_scheme));
44 EXPECT_TRUE(found_scheme == Component());
46 // When there is a whitespace char in scheme, it should canonicalize the url
47 // before comparison.
48 const char whtspc_str[] = " \r\n\tjav\ra\nscri\tpt:alert(1)";
49 EXPECT_TRUE(FindAndCompareScheme(whtspc_str,
50 static_cast<int>(strlen(whtspc_str)),
51 "javascript", &found_scheme));
52 EXPECT_TRUE(found_scheme == Component(1, 10));
54 // Control characters should be stripped out on the ends, and kept in the
55 // middle.
56 const char ctrl_str[] = "\02jav\02scr\03ipt:alert(1)";
57 EXPECT_FALSE(FindAndCompareScheme(ctrl_str,
58 static_cast<int>(strlen(ctrl_str)),
59 "javascript", &found_scheme));
60 EXPECT_TRUE(found_scheme == Component(1, 11));
63 TEST(URLUtilTest, ReplaceComponents) {
64 Parsed parsed;
65 RawCanonOutputT<char> output;
66 Parsed new_parsed;
68 // Check that the following calls do not cause crash
69 Replacements<char> replacements;
70 replacements.SetRef("test", Component(0, 4));
71 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
72 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
73 replacements.ClearRef();
74 replacements.SetHost("test", Component(0, 4));
75 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
76 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
78 replacements.ClearHost();
79 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
80 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
81 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
82 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
85 static std::string CheckReplaceScheme(const char* base_url,
86 const char* scheme) {
87 // Make sure the input is canonicalized.
88 RawCanonOutput<32> original;
89 Parsed original_parsed;
90 Canonicalize(base_url, strlen(base_url), true, NULL, &original,
91 &original_parsed);
93 Replacements<char> replacements;
94 replacements.SetScheme(scheme, Component(0, strlen(scheme)));
96 std::string output_string;
97 StdStringCanonOutput output(&output_string);
98 Parsed output_parsed;
99 ReplaceComponents(original.data(), original.length(), original_parsed,
100 replacements, NULL, &output, &output_parsed);
102 output.Complete();
103 return output_string;
106 TEST(URLUtilTest, ReplaceScheme) {
107 EXPECT_EQ("https://google.com/",
108 CheckReplaceScheme("http://google.com/", "https"));
109 EXPECT_EQ("file://google.com/",
110 CheckReplaceScheme("http://google.com/", "file"));
111 EXPECT_EQ("http://home/Build",
112 CheckReplaceScheme("file:///Home/Build", "http"));
113 EXPECT_EQ("javascript:foo",
114 CheckReplaceScheme("about:foo", "javascript"));
115 EXPECT_EQ("://google.com/",
116 CheckReplaceScheme("http://google.com/", ""));
117 EXPECT_EQ("http://google.com/",
118 CheckReplaceScheme("about:google.com", "http"));
119 EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
121 #ifdef WIN32
122 // Magic Windows drive letter behavior when converting to a file URL.
123 EXPECT_EQ("file:///E:/foo/",
124 CheckReplaceScheme("http://localhost/e:foo/", "file"));
125 #endif
127 // This will probably change to "about://google.com/" when we fix
128 // http://crbug.com/160 which should also be an acceptable result.
129 EXPECT_EQ("about://google.com/",
130 CheckReplaceScheme("http://google.com/", "about"));
132 EXPECT_EQ("http://example.com/%20hello%20# world",
133 CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
136 TEST(URLUtilTest, DecodeURLEscapeSequences) {
137 struct DecodeCase {
138 const char* input;
139 const char* output;
140 } decode_cases[] = {
141 {"hello, world", "hello, world"},
142 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/",
143 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
144 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/",
145 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
146 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/",
147 " !\"#$%&'()*+,-.//"},
148 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/",
149 "0123456789:;<=>?/"},
150 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/",
151 "@ABCDEFGHIJKLMNO/"},
152 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/",
153 "PQRSTUVWXYZ[\\]^_/"},
154 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/",
155 "`abcdefghijklmno/"},
156 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/",
157 "pqrstuvwxyz{|}~\x7f/"},
158 // Test un-UTF-8-ization.
159 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
162 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(decode_cases); i++) {
163 const char* input = decode_cases[i].input;
164 RawCanonOutputT<base::char16> output;
165 DecodeURLEscapeSequences(input, strlen(input), &output);
166 EXPECT_EQ(decode_cases[i].output,
167 test_utils::ConvertUTF16ToUTF8(base::string16(output.data(),
168 output.length())));
171 // Our decode should decode %00
172 const char zero_input[] = "%00";
173 RawCanonOutputT<base::char16> zero_output;
174 DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
175 EXPECT_NE("%00", test_utils::ConvertUTF16ToUTF8(
176 base::string16(zero_output.data(), zero_output.length())));
178 // Test the error behavior for invalid UTF-8.
179 const char invalid_input[] = "%e4%a0%e5%a5%bd";
180 const base::char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0};
181 RawCanonOutputT<base::char16> invalid_output;
182 DecodeURLEscapeSequences(invalid_input, strlen(invalid_input),
183 &invalid_output);
184 EXPECT_EQ(base::string16(invalid_expected),
185 base::string16(invalid_output.data(), invalid_output.length()));
188 TEST(URLUtilTest, TestEncodeURIComponent) {
189 struct EncodeCase {
190 const char* input;
191 const char* output;
192 } encode_cases[] = {
193 {"hello, world", "hello%2C%20world"},
194 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
195 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
196 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
197 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
198 {" !\"#$%&'()*+,-./",
199 "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
200 {"0123456789:;<=>?",
201 "0123456789%3A%3B%3C%3D%3E%3F"},
202 {"@ABCDEFGHIJKLMNO",
203 "%40ABCDEFGHIJKLMNO"},
204 {"PQRSTUVWXYZ[\\]^_",
205 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
206 {"`abcdefghijklmno",
207 "%60abcdefghijklmno"},
208 {"pqrstuvwxyz{|}~\x7f",
209 "pqrstuvwxyz%7B%7C%7D~%7F"},
212 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(encode_cases); i++) {
213 const char* input = encode_cases[i].input;
214 RawCanonOutputT<char> buffer;
215 EncodeURIComponent(input, strlen(input), &buffer);
216 std::string output(buffer.data(), buffer.length());
217 EXPECT_EQ(encode_cases[i].output, output);
221 TEST(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
222 // This tests non-standard (in the sense that GIsStandard() == false)
223 // hierarchical schemes.
224 struct ResolveRelativeCase {
225 const char* base;
226 const char* rel;
227 bool is_valid;
228 const char* out;
229 } resolve_non_standard_cases[] = {
230 // Resolving a relative path against a non-hierarchical URL should fail.
231 {"scheme:opaque_data", "/path", false, ""},
232 // Resolving a relative path against a non-standard authority-based base
233 // URL doesn't alter the authority section.
234 {"scheme://Authority/", "../path", true, "scheme://Authority/path"},
235 // A non-standard hierarchical base is resolved with path URL
236 // canoncialization rules.
237 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
238 {"data:/Path/../part/part2", "file.html", true,
239 "data:/Path/../part/file.html"},
240 // Path URL canonicalization rules also apply to non-standard authority-
241 // based URLs.
242 {"custom://Authority/", "file.html", true,
243 "custom://Authority/file.html"},
244 {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
245 {"custom://Authority/", "../../file.html", true,
246 "custom://Authority/file.html"},
247 {"custom://Authority/path/", "file.html", true,
248 "custom://Authority/path/file.html"},
249 {"custom://Authority:NoCanon/path/", "file.html", true,
250 "custom://Authority:NoCanon/path/file.html"},
251 // It's still possible to get an invalid path URL.
252 {"custom://Invalid:!#Auth/", "file.html", false, ""},
253 // A path with an authority section gets canonicalized under standard URL
254 // rules, even though the base was non-standard.
255 {"content://content.Provider/", "//other.Provider", true,
256 "content://other.provider/"},
257 // Resolving an absolute URL doesn't cause canonicalization of the
258 // result.
259 {"about:blank", "custom://Authority", true, "custom://Authority"},
260 // Fragment URLs can be resolved against a non-standard base.
261 {"scheme://Authority/path", "#fragment", true,
262 "scheme://Authority/path#fragment"},
263 {"scheme://Authority/", "#fragment", true, "scheme://Authority/#fragment"},
264 // Resolving should fail if the base URL is authority-based but is
265 // missing a path component (the '/' at the end).
266 {"scheme://Authority", "path", false, ""},
267 // Test resolving a fragment (only) against any kind of base-URL.
268 {"about:blank", "#id42", true, "about:blank#id42" },
269 {"about:blank", " #id42", true, "about:blank#id42" },
270 {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag" },
271 // A surprising side effect of allowing fragments to resolve against
272 // any URL scheme is we might break javascript: URLs by doing so...
273 {"javascript:alert('foo#bar')", "#badfrag", true,
274 "javascript:alert('foo#badfrag" },
277 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(resolve_non_standard_cases); i++) {
278 const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
279 Parsed base_parsed;
280 ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
282 std::string resolved;
283 StdStringCanonOutput output(&resolved);
284 Parsed resolved_parsed;
285 bool valid = ResolveRelative(test_data.base, strlen(test_data.base),
286 base_parsed, test_data.rel,
287 strlen(test_data.rel), NULL, &output,
288 &resolved_parsed);
289 output.Complete();
291 EXPECT_EQ(test_data.is_valid, valid) << i;
292 if (test_data.is_valid && valid)
293 EXPECT_EQ(test_data.out, resolved) << i;
297 } // namespace url