OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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. | |
4 | |
5 #include <stdlib.h> | |
6 #include <windows.h> | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/logging.h" | |
10 #include "base/path_service.h" | |
11 #include "base/string_util.h" | |
12 #include "chrome/browser/url_fixer_upper.h" | |
13 #include "chrome/common/chrome_paths.h" | |
14 #include "googleurl/src/url_parse.h" | |
15 #include "googleurl/src/gurl.h" | |
16 #include "net/base/net_util.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 namespace { | |
20 class URLFixerUpperTest : public testing::Test { | |
21 }; | |
22 }; | |
23 | |
24 std::ostream& operator<<(std::ostream& os, const url_parse::Component& part) { | |
25 return os << "(begin=" << part.begin << ", len=" << part.len << ")"; | |
26 } | |
27 | |
28 struct segment_case { | |
29 const std::wstring input; | |
30 const std::wstring result; | |
31 const url_parse::Component scheme; | |
32 const url_parse::Component username; | |
33 const url_parse::Component password; | |
34 const url_parse::Component host; | |
35 const url_parse::Component port; | |
36 const url_parse::Component path; | |
37 const url_parse::Component query; | |
38 const url_parse::Component ref; | |
39 }; | |
40 | |
41 static const segment_case segment_cases[] = { | |
42 { L"http://www.google.com/", L"http", | |
43 url_parse::Component(0, 4), // scheme | |
44 url_parse::Component(), // username | |
45 url_parse::Component(), // password | |
46 url_parse::Component(7, 14), // host | |
47 url_parse::Component(), // port | |
48 url_parse::Component(21, 1), // path | |
49 url_parse::Component(), // query | |
50 url_parse::Component(), // ref | |
51 }, | |
52 { L"aBoUt:vErSiOn", L"about", | |
53 url_parse::Component(0, 5), // scheme | |
54 url_parse::Component(), // username | |
55 url_parse::Component(), // password | |
56 url_parse::Component(), // host | |
57 url_parse::Component(), // port | |
58 url_parse::Component(), // path | |
59 url_parse::Component(), // query | |
60 url_parse::Component(), // ref | |
61 }, | |
62 { L" www.google.com:124?foo#", L"http", | |
63 url_parse::Component(), // scheme | |
64 url_parse::Component(), // username | |
65 url_parse::Component(), // password | |
66 url_parse::Component(4, 14), // host | |
67 url_parse::Component(19, 3), // port | |
68 url_parse::Component(), // path | |
69 url_parse::Component(23, 3), // query | |
70 url_parse::Component(27, 0), // ref | |
71 }, | |
72 { L"user@www.google.com", L"http", | |
73 url_parse::Component(), // scheme | |
74 url_parse::Component(0, 4), // username | |
75 url_parse::Component(), // password | |
76 url_parse::Component(5, 14), // host | |
77 url_parse::Component(), // port | |
78 url_parse::Component(), // path | |
79 url_parse::Component(), // query | |
80 url_parse::Component(), // ref | |
81 }, | |
82 { L"ftp:/user:P:a$$Wd@..ftp.google.com...::23///pub?foo#bar", L"ftp", | |
83 url_parse::Component(0, 3), // scheme | |
84 url_parse::Component(5, 4), // username | |
85 url_parse::Component(10, 7), // password | |
86 url_parse::Component(18, 20), // host | |
87 url_parse::Component(39, 2), // port | |
88 url_parse::Component(41, 6), // path | |
89 url_parse::Component(48, 3), // query | |
90 url_parse::Component(52, 3), // ref | |
91 }, | |
92 }; | |
93 | |
94 TEST(URLFixerUpperTest, SegmentURL) { | |
95 std::wstring result; | |
96 url_parse::Parsed parts; | |
97 | |
98 for (int i = 0; i < arraysize(segment_cases); ++i) { | |
99 segment_case value = segment_cases[i]; | |
100 result = URLFixerUpper::SegmentURL(value.input, &parts); | |
101 EXPECT_EQ(value.result, result); | |
102 EXPECT_EQ(value.scheme, parts.scheme); | |
103 EXPECT_EQ(value.username, parts.username); | |
104 EXPECT_EQ(value.password, parts.password); | |
105 EXPECT_EQ(value.host, parts.host); | |
106 EXPECT_EQ(value.port, parts.port); | |
107 EXPECT_EQ(value.path, parts.path); | |
108 EXPECT_EQ(value.query, parts.query); | |
109 EXPECT_EQ(value.ref, parts.ref); | |
110 } | |
111 } | |
112 | |
113 // Creates a file and returns its full name as well as the decomposed | |
114 // version. Example: | |
115 // full_path = "c:\foo\bar.txt" | |
116 // dir = "c:\foo" | |
117 // file_name = "bar.txt" | |
118 static bool MakeTempFile(const std::wstring& dir, | |
119 const std::wstring& file_name, | |
120 std::wstring* full_path) { | |
121 *full_path = dir + L"\\" + file_name; | |
122 | |
123 HANDLE hfile = CreateFile(full_path->c_str(), GENERIC_READ | GENERIC_WRITE, | |
124 0, NULL, CREATE_ALWAYS, 0, NULL); | |
125 if (hfile == NULL || hfile == INVALID_HANDLE_VALUE) | |
126 return false; | |
127 CloseHandle(hfile); | |
128 return true; | |
129 } | |
130 | |
131 // Returns true if the given URL is a file: URL that matches the given file | |
132 static bool IsMatchingFileURL(const std::wstring& url, | |
133 const std::wstring& full_file_path) { | |
134 if (url.length() <= 8) | |
135 return false; | |
136 if (std::wstring(L"file:///") != url.substr(0, 8)) | |
137 return false; // no file:/// prefix | |
138 if (url.find('\\') != std::wstring::npos) | |
139 return false; // contains backslashes | |
140 | |
141 std::wstring derived_path; | |
142 net::FileURLToFilePath(GURL(url), &derived_path); | |
143 return (derived_path.length() == full_file_path.length()) && | |
144 std::equal(derived_path.begin(), derived_path.end(), | |
145 full_file_path.begin(), CaseInsensitiveCompare<wchar_t>()); | |
146 } | |
147 | |
148 struct fixup_case { | |
149 const std::wstring input; | |
150 const std::wstring desired_tld; | |
151 const std::wstring output; | |
152 } fixup_cases[] = { | |
153 {L"www.google.com", L"", L"http://www.google.com/"}, | |
154 {L" www.google.com ", L"", L"http://www.google.com/"}, | |
155 {L" foo.com/asdf bar", L"", L"http://foo.com/asdf bar"}, | |
156 {L"..www.google.com..", L"", L"http://www.google.com./"}, | |
157 {L"http://......", L"", L"http://....../"}, | |
158 {L"http://host.com:ninety-two/", L"", L"http://host.com/"}, | |
159 {L"http://host.com:ninety-two?foo", L"", L"http://host.com/?foo"}, | |
160 {L"google.com:123", L"", L"http://google.com:123/"}, | |
161 {L"about:", L"", L"about:"}, | |
162 {L"about:version", L"", L"about:version"}, | |
163 {L"www:123", L"", L"http://www:123/"}, | |
164 {L" www:123", L"", L"http://www:123/"}, | |
165 {L"www.google.com?foo", L"", L"http://www.google.com/?foo"}, | |
166 {L"www.google.com#foo", L"", L"http://www.google.com/#foo"}, | |
167 {L"www.google.com?", L"", L"http://www.google.com/?"}, | |
168 {L"www.google.com#", L"", L"http://www.google.com/#"}, | |
169 {L"www.google.com:123?foo#bar", L"", L"http://www.google.com:123/?foo#bar"}, | |
170 {L"user@www.google.com", L"", L"http://user@www.google.com/"}, | |
171 {L"\x6C34.com", L"", L"http://\x6C34.com/" }, | |
172 // It would be better if this next case got treated as http, but I don't see | |
173 // a clean way to guess this isn't the new-and-exciting "user" scheme. | |
174 {L"user:passwd@www.google.com:8080/", L"", L"user:passwd@www.google.com:8080/"
}, | |
175 //{L"file:///c:/foo/bar%20baz.txt", L"", L"file:///C:/foo/bar%20baz.txt"}, | |
176 {L"ftp.google.com", L"", L"ftp://ftp.google.com/"}, | |
177 {L" ftp.google.com", L"", L"ftp://ftp.google.com/"}, | |
178 {L"FTP.GooGle.com", L"", L"ftp://FTP.GooGle.com/"}, | |
179 {L"ftpblah.google.com", L"", L"http://ftpblah.google.com/"}, | |
180 {L"ftp", L"", L"http://ftp/"}, | |
181 {L"google.ftp.com", L"", L"http://google.ftp.com/"}, | |
182 }; | |
183 | |
184 TEST(URLFixerUpperTest, FixupURL) { | |
185 std::wstring output; | |
186 | |
187 for (int i = 0; i < arraysize(fixup_cases); ++i) { | |
188 fixup_case value = fixup_cases[i]; | |
189 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); | |
190 EXPECT_EQ(value.output, output); | |
191 } | |
192 | |
193 // Check the TLD-appending functionality | |
194 fixup_case tld_cases[] = { | |
195 {L"google", L"com", L"http://www.google.com/"}, | |
196 {L"google.", L"com", L"http://www.google.com/"}, | |
197 {L"google..", L"com", L"http://www.google.com/"}, | |
198 {L".google", L"com", L"http://www.google.com/"}, | |
199 {L"www.google", L"com", L"http://www.google.com/"}, | |
200 {L"google.com", L"com", L"http://google.com/"}, | |
201 {L"http://google", L"com", L"http://www.google.com/"}, | |
202 {L"..google..", L"com", L"http://www.google.com/"}, | |
203 {L"http://www.google", L"com", L"http://www.google.com/"}, | |
204 {L"google/foo", L"com", L"http://www.google.com/foo"}, | |
205 {L"google.com/foo", L"com", L"http://google.com/foo"}, | |
206 {L"google/?foo=.com", L"com", L"http://www.google.com/?foo=.com"}, | |
207 {L"www.google/?foo=www.", L"com", L"http://www.google.com/?foo=www."}, | |
208 {L"google.com/?foo=.com", L"com", L"http://google.com/?foo=.com"}, | |
209 {L"http://www.google.com", L"com", L"http://www.google.com/"}, | |
210 {L"google:123", L"com", L"http://www.google.com:123/"}, | |
211 {L"http://google:123", L"com", L"http://www.google.com:123/"}, | |
212 }; | |
213 for (int i = 0; i < arraysize(tld_cases); ++i) { | |
214 fixup_case value = tld_cases[i]; | |
215 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); | |
216 EXPECT_EQ(value.output, output); | |
217 } | |
218 } | |
219 | |
220 // Test different types of file inputs to URIFixerUpper::FixupURL. This | |
221 // doesn't go into the nice array of fixups above since the file input | |
222 // has to exist. | |
223 TEST(URLFixerUpperTest, FixupFile) { | |
224 // this "original" filename is the one we tweak to get all the variations | |
225 std::wstring dir; | |
226 std::wstring original; | |
227 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); | |
228 ASSERT_TRUE(MakeTempFile(dir, L"url fixer upper existing file.txt", | |
229 &original)); | |
230 | |
231 // reference path | |
232 std::wstring golden = | |
233 UTF8ToWide(net::FilePathToFileURL(original).spec()); | |
234 | |
235 // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) | |
236 std::wstring fixedup = URLFixerUpper::FixupURL(original, L""); | |
237 EXPECT_EQ(golden, fixedup); | |
238 | |
239 // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) | |
240 std::wstring cur(original); | |
241 EXPECT_EQ(':', cur[1]); | |
242 cur[1] = '|'; | |
243 fixedup = URLFixerUpper::FixupURL(cur, L""); | |
244 EXPECT_EQ(golden, fixedup); | |
245 | |
246 fixup_case file_cases[] = { | |
247 // File URLs go through GURL, which tries to escape intelligently. | |
248 {L"c:\\This%20is a non-existent file.txt", L"", L"file:///C:/This%2520is%20a
%20non-existent%20file.txt"}, | |
249 | |
250 // \\foo\bar.txt -> file://foo/bar.txt | |
251 // UNC paths, this file won't exist, but since there are no escapes, it | |
252 // should be returned just converted to a file: URL. | |
253 {L"\\\\SomeNonexistentHost\\foo\\bar.txt", L"", L"file://somenonexistenthost
/foo/bar.txt"}, | |
254 {L"//SomeNonexistentHost\\foo/bar.txt", L"", L"file://somenonexistenthost/fo
o/bar.txt"}, | |
255 {L"file:///C:/foo/bar", L"", L"file:///C:/foo/bar"}, | |
256 | |
257 // These are fixups we don't do, but could consider: | |
258 // | |
259 // {L"file://C:/foo/bar", L"", L"file:///C:/foo/bar"}, | |
260 // {L"file:c:", L"", L"file:///c:/"}, | |
261 // {L"file:c:WINDOWS", L"", L"file:///c:/WINDOWS"}, | |
262 // {L"file:c|Program Files", L"", L"file:///c:/Program Files"}, | |
263 // {L"file:///foo:/bar", L"", L"file://foo/bar"}, | |
264 // {L"file:/file", L"", L"file://file/"}, | |
265 // {L"file:////////c:\\foo", L"", L"file:///c:/foo"}, | |
266 // {L"file://server/folder/file", L"", L"file://server/folder/file"}, | |
267 // {L"file:/\\/server\\folder/file", L"", L"file://server/folder/file"}, | |
268 }; | |
269 for (int i = 0; i < arraysize(file_cases); i++) { | |
270 fixedup = URLFixerUpper::FixupURL(file_cases[i].input, | |
271 file_cases[i].desired_tld); | |
272 EXPECT_EQ(file_cases[i].output, fixedup); | |
273 } | |
274 | |
275 EXPECT_TRUE(DeleteFile(original.c_str())); | |
276 } | |
277 | |
278 TEST(URLFixerUpperTest, FixupRelativeFile) { | |
279 std::wstring full_path, dir; | |
280 std::wstring file_part(L"url_fixer_upper_existing_file.txt"); | |
281 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); | |
282 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); | |
283 | |
284 // make sure we pass through good URLs | |
285 std::wstring fixedup; | |
286 for (int i = 0; i < arraysize(fixup_cases); ++i) { | |
287 fixup_case value = fixup_cases[i]; | |
288 fixedup = URLFixerUpper::FixupRelativeFile(dir, value.input); | |
289 EXPECT_EQ(value.output, fixedup); | |
290 } | |
291 | |
292 // make sure the existing file got fixed-up to a file URL, and that there | |
293 // are no backslashes | |
294 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); | |
295 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | |
296 EXPECT_TRUE(DeleteFile(full_path.c_str())); | |
297 | |
298 // create a filename we know doesn't exist and make sure it doesn't get | |
299 // fixed up to a file URL | |
300 std::wstring nonexistent_file(L"url_fixer_upper_nonexistent_file.txt"); | |
301 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); | |
302 EXPECT_NE(std::wstring(L"file:///"), fixedup.substr(0, 8)); | |
303 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); | |
304 | |
305 // make a subdir to make sure relative paths with directories work, also | |
306 // test spaces: "app_dir\url fixer-upper dir\url fixer-upper existing file.txt
" | |
307 std::wstring sub_dir(L"url fixer-upper dir"); | |
308 std::wstring sub_file(L"url fixer-upper existing file.txt"); | |
309 std::wstring new_dir = dir + L"\\" + sub_dir; | |
310 CreateDirectory(new_dir.c_str(), NULL); | |
311 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); | |
312 | |
313 // test file in the subdir | |
314 std::wstring relative_file = sub_dir + L"\\" + sub_file; | |
315 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | |
316 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | |
317 | |
318 // test file in the subdir with different slashes and escaping | |
319 relative_file = sub_dir + L"/" + sub_file; | |
320 ReplaceSubstringsAfterOffset(&relative_file, 0, L" ", L"%20"); | |
321 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | |
322 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | |
323 | |
324 // test relative directories and duplicate slashes | |
325 // (should resolve to the same file as above) | |
326 relative_file = sub_dir + L"\\../" + sub_dir + L"\\\\\\.\\" + sub_file; | |
327 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | |
328 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | |
329 | |
330 // done with the subdir | |
331 EXPECT_TRUE(DeleteFile(full_path.c_str())); | |
332 EXPECT_TRUE(RemoveDirectory(new_dir.c_str())); | |
333 } | |
334 | |
OLD | NEW |