Don't send a SHChangeNotify for creating an app icon when creating a shortcut.
[chromium-blink-merge.git] / url / url_util_unittest.cc
blob73ff93b8a1912c7e712cf411742f9553cbafd0d7
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/url_canon.h"
8 #include "url/url_canon_stdstring.h"
9 #include "url/url_parse.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, ReplaceComponents) {
65 Parsed parsed;
66 RawCanonOutputT<char> output;
67 Parsed new_parsed;
69 // Check that the following calls do not cause crash
70 Replacements<char> replacements;
71 replacements.SetRef("test", Component(0, 4));
72 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
73 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
74 replacements.ClearRef();
75 replacements.SetHost("test", Component(0, 4));
76 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
77 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
79 replacements.ClearHost();
80 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
81 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
82 ReplaceComponents(NULL, 0, parsed, replacements, NULL, &output, &new_parsed);
83 ReplaceComponents("", 0, parsed, replacements, NULL, &output, &new_parsed);
86 static std::string CheckReplaceScheme(const char* base_url,
87 const char* scheme) {
88 // Make sure the input is canonicalized.
89 RawCanonOutput<32> original;
90 Parsed original_parsed;
91 Canonicalize(base_url, strlen(base_url), true, NULL, &original,
92 &original_parsed);
94 Replacements<char> replacements;
95 replacements.SetScheme(scheme, Component(0, strlen(scheme)));
97 std::string output_string;
98 StdStringCanonOutput output(&output_string);
99 Parsed output_parsed;
100 ReplaceComponents(original.data(), original.length(), original_parsed,
101 replacements, NULL, &output, &output_parsed);
103 output.Complete();
104 return output_string;
107 TEST(URLUtilTest, ReplaceScheme) {
108 EXPECT_EQ("https://google.com/",
109 CheckReplaceScheme("http://google.com/", "https"));
110 EXPECT_EQ("file://google.com/",
111 CheckReplaceScheme("http://google.com/", "file"));
112 EXPECT_EQ("http://home/Build",
113 CheckReplaceScheme("file:///Home/Build", "http"));
114 EXPECT_EQ("javascript:foo",
115 CheckReplaceScheme("about:foo", "javascript"));
116 EXPECT_EQ("://google.com/",
117 CheckReplaceScheme("http://google.com/", ""));
118 EXPECT_EQ("http://google.com/",
119 CheckReplaceScheme("about:google.com", "http"));
120 EXPECT_EQ("http:", CheckReplaceScheme("", "http"));
122 #ifdef WIN32
123 // Magic Windows drive letter behavior when converting to a file URL.
124 EXPECT_EQ("file:///E:/foo/",
125 CheckReplaceScheme("http://localhost/e:foo/", "file"));
126 #endif
128 // This will probably change to "about://google.com/" when we fix
129 // http://crbug.com/160 which should also be an acceptable result.
130 EXPECT_EQ("about://google.com/",
131 CheckReplaceScheme("http://google.com/", "about"));
133 EXPECT_EQ("http://example.com/%20hello%20# world",
134 CheckReplaceScheme("myscheme:example.com/ hello # world ", "http"));
137 TEST(URLUtilTest, DecodeURLEscapeSequences) {
138 struct DecodeCase {
139 const char* input;
140 const char* output;
141 } decode_cases[] = {
142 {"hello, world", "hello, world"},
143 {"%01%02%03%04%05%06%07%08%09%0a%0B%0C%0D%0e%0f/",
144 "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0B\x0C\x0D\x0e\x0f/"},
145 {"%10%11%12%13%14%15%16%17%18%19%1a%1B%1C%1D%1e%1f/",
146 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1B\x1C\x1D\x1e\x1f/"},
147 {"%20%21%22%23%24%25%26%27%28%29%2a%2B%2C%2D%2e%2f/",
148 " !\"#$%&'()*+,-.//"},
149 {"%30%31%32%33%34%35%36%37%38%39%3a%3B%3C%3D%3e%3f/",
150 "0123456789:;<=>?/"},
151 {"%40%41%42%43%44%45%46%47%48%49%4a%4B%4C%4D%4e%4f/",
152 "@ABCDEFGHIJKLMNO/"},
153 {"%50%51%52%53%54%55%56%57%58%59%5a%5B%5C%5D%5e%5f/",
154 "PQRSTUVWXYZ[\\]^_/"},
155 {"%60%61%62%63%64%65%66%67%68%69%6a%6B%6C%6D%6e%6f/",
156 "`abcdefghijklmno/"},
157 {"%70%71%72%73%74%75%76%77%78%79%7a%7B%7C%7D%7e%7f/",
158 "pqrstuvwxyz{|}~\x7f/"},
159 // Test un-UTF-8-ization.
160 {"%e4%bd%a0%e5%a5%bd", "\xe4\xbd\xa0\xe5\xa5\xbd"},
163 for (size_t i = 0; i < arraysize(decode_cases); i++) {
164 const char* input = decode_cases[i].input;
165 RawCanonOutputT<base::char16> output;
166 DecodeURLEscapeSequences(input, strlen(input), &output);
167 EXPECT_EQ(decode_cases[i].output,
168 test_utils::ConvertUTF16ToUTF8(base::string16(output.data(),
169 output.length())));
172 // Our decode should decode %00
173 const char zero_input[] = "%00";
174 RawCanonOutputT<base::char16> zero_output;
175 DecodeURLEscapeSequences(zero_input, strlen(zero_input), &zero_output);
176 EXPECT_NE("%00", test_utils::ConvertUTF16ToUTF8(
177 base::string16(zero_output.data(), zero_output.length())));
179 // Test the error behavior for invalid UTF-8.
180 const char invalid_input[] = "%e4%a0%e5%a5%bd";
181 const base::char16 invalid_expected[4] = {0x00e4, 0x00a0, 0x597d, 0};
182 RawCanonOutputT<base::char16> invalid_output;
183 DecodeURLEscapeSequences(invalid_input, strlen(invalid_input),
184 &invalid_output);
185 EXPECT_EQ(base::string16(invalid_expected),
186 base::string16(invalid_output.data(), invalid_output.length()));
189 TEST(URLUtilTest, TestEncodeURIComponent) {
190 struct EncodeCase {
191 const char* input;
192 const char* output;
193 } encode_cases[] = {
194 {"hello, world", "hello%2C%20world"},
195 {"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
196 "%01%02%03%04%05%06%07%08%09%0A%0B%0C%0D%0E%0F"},
197 {"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F",
198 "%10%11%12%13%14%15%16%17%18%19%1A%1B%1C%1D%1E%1F"},
199 {" !\"#$%&'()*+,-./",
200 "%20!%22%23%24%25%26%27()*%2B%2C-.%2F"},
201 {"0123456789:;<=>?",
202 "0123456789%3A%3B%3C%3D%3E%3F"},
203 {"@ABCDEFGHIJKLMNO",
204 "%40ABCDEFGHIJKLMNO"},
205 {"PQRSTUVWXYZ[\\]^_",
206 "PQRSTUVWXYZ%5B%5C%5D%5E_"},
207 {"`abcdefghijklmno",
208 "%60abcdefghijklmno"},
209 {"pqrstuvwxyz{|}~\x7f",
210 "pqrstuvwxyz%7B%7C%7D~%7F"},
213 for (size_t i = 0; i < arraysize(encode_cases); i++) {
214 const char* input = encode_cases[i].input;
215 RawCanonOutputT<char> buffer;
216 EncodeURIComponent(input, strlen(input), &buffer);
217 std::string output(buffer.data(), buffer.length());
218 EXPECT_EQ(encode_cases[i].output, output);
222 TEST(URLUtilTest, TestResolveRelativeWithNonStandardBase) {
223 // This tests non-standard (in the sense that GIsStandard() == false)
224 // hierarchical schemes.
225 struct ResolveRelativeCase {
226 const char* base;
227 const char* rel;
228 bool is_valid;
229 const char* out;
230 } resolve_non_standard_cases[] = {
231 // Resolving a relative path against a non-hierarchical URL should fail.
232 {"scheme:opaque_data", "/path", false, ""},
233 // Resolving a relative path against a non-standard authority-based base
234 // URL doesn't alter the authority section.
235 {"scheme://Authority/", "../path", true, "scheme://Authority/path"},
236 // A non-standard hierarchical base is resolved with path URL
237 // canonicalization rules.
238 {"data:/Blah:Blah/", "file.html", true, "data:/Blah:Blah/file.html"},
239 {"data:/Path/../part/part2", "file.html", true,
240 "data:/Path/../part/file.html"},
241 // Path URL canonicalization rules also apply to non-standard authority-
242 // based URLs.
243 {"custom://Authority/", "file.html", true,
244 "custom://Authority/file.html"},
245 {"custom://Authority/", "other://Auth/", true, "other://Auth/"},
246 {"custom://Authority/", "../../file.html", true,
247 "custom://Authority/file.html"},
248 {"custom://Authority/path/", "file.html", true,
249 "custom://Authority/path/file.html"},
250 {"custom://Authority:NoCanon/path/", "file.html", true,
251 "custom://Authority:NoCanon/path/file.html"},
252 // It's still possible to get an invalid path URL.
253 {"custom://Invalid:!#Auth/", "file.html", false, ""},
254 // A path with an authority section gets canonicalized under standard URL
255 // rules, even though the base was non-standard.
256 {"content://content.Provider/", "//other.Provider", true,
257 "content://other.provider/"},
258 // Resolving an absolute URL doesn't cause canonicalization of the
259 // result.
260 {"about:blank", "custom://Authority", true, "custom://Authority"},
261 // Fragment URLs can be resolved against a non-standard base.
262 {"scheme://Authority/path", "#fragment", true,
263 "scheme://Authority/path#fragment"},
264 {"scheme://Authority/", "#fragment", true, "scheme://Authority/#fragment"},
265 // Resolving should fail if the base URL is authority-based but is
266 // missing a path component (the '/' at the end).
267 {"scheme://Authority", "path", false, ""},
268 // Test resolving a fragment (only) against any kind of base-URL.
269 {"about:blank", "#id42", true, "about:blank#id42" },
270 {"about:blank", " #id42", true, "about:blank#id42" },
271 {"about:blank#oldfrag", "#newfrag", true, "about:blank#newfrag" },
272 // A surprising side effect of allowing fragments to resolve against
273 // any URL scheme is we might break javascript: URLs by doing so...
274 {"javascript:alert('foo#bar')", "#badfrag", true,
275 "javascript:alert('foo#badfrag" },
276 // In this case, the backslashes will not be canonicalized because it's a
277 // non-standard URL, but they will be treated as a path separators,
278 // giving the base URL here a path of "\".
280 // The result here is somewhat arbitrary. One could argue it should be
281 // either "aaa://a\" or "aaa://a/" since the path is being replaced with
282 // the "current directory". But in the context of resolving on data URLs,
283 // adding the requested dot doesn't seem wrong either.
284 {"aaa://a\\", "aaa:.", true, "aaa://a\\." }
287 for (size_t i = 0; i < arraysize(resolve_non_standard_cases); i++) {
288 const ResolveRelativeCase& test_data = resolve_non_standard_cases[i];
289 Parsed base_parsed;
290 ParsePathURL(test_data.base, strlen(test_data.base), false, &base_parsed);
292 std::string resolved;
293 StdStringCanonOutput output(&resolved);
294 Parsed resolved_parsed;
295 bool valid = ResolveRelative(test_data.base, strlen(test_data.base),
296 base_parsed, test_data.rel,
297 strlen(test_data.rel), NULL, &output,
298 &resolved_parsed);
299 output.Complete();
301 EXPECT_EQ(test_data.is_valid, valid) << i;
302 if (test_data.is_valid && valid)
303 EXPECT_EQ(test_data.out, resolved) << i;
307 TEST(URLUtilTest, TestNoRefComponent) {
308 // The hash-mark must be ignored when mailto: scheme is
309 // parsed, even if the url has a base and relative part.
310 const char* base = "mailto://to/";
311 const char* rel = "any#body";
313 Parsed base_parsed;
314 ParsePathURL(base, strlen(base), false, &base_parsed);
316 std::string resolved;
317 StdStringCanonOutput output(&resolved);
318 Parsed resolved_parsed;
320 bool valid = ResolveRelative(base, strlen(base),
321 base_parsed, rel,
322 strlen(rel), NULL, &output,
323 &resolved_parsed);
324 EXPECT_TRUE(valid);
325 EXPECT_FALSE(resolved_parsed.ref.is_valid());
328 } // namespace url