[email protected] | 13044cd | 2010-02-10 18:56:18 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #include <stdlib.h> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include "base/basictypes.h" |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 8 | #include "base/file_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | #include "base/path_service.h" |
| 10 | #include "base/string_util.h" |
[email protected] | d55194ca | 2010-03-11 18:25:45 | [diff] [blame] | 11 | #include "base/utf_string_conversions.h" |
[email protected] | f870a32 | 2009-01-16 21:47:27 | [diff] [blame] | 12 | #include "chrome/browser/net/url_fixer_upper.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 29 | const std::string input; |
| 30 | const std::string result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 42 | { "http://www.google.com/", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 52 | { "aBoUt:vErSiOn", "about", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 62 | { " www.google.com:124?foo#", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 72 | { "[email protected]", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 82 | { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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 | }, |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 92 | { "[2001:db8::1]/path", "http", |
| 93 | url_parse::Component(), // scheme |
| 94 | url_parse::Component(), // username |
| 95 | url_parse::Component(), // password |
| 96 | url_parse::Component(0, 13), // host |
| 97 | url_parse::Component(), // port |
| 98 | url_parse::Component(13, 5), // path |
| 99 | url_parse::Component(), // query |
| 100 | url_parse::Component(), // ref |
| 101 | }, |
| 102 | { "[::1]", "http", |
| 103 | url_parse::Component(), // scheme |
| 104 | url_parse::Component(), // username |
| 105 | url_parse::Component(), // password |
| 106 | url_parse::Component(0, 5), // host |
| 107 | url_parse::Component(), // port |
| 108 | url_parse::Component(), // path |
| 109 | url_parse::Component(), // query |
| 110 | url_parse::Component(), // ref |
| 111 | }, |
[email protected] | c199130 | 2009-06-04 03:57:39 | [diff] [blame] | 112 | // Incomplete IPv6 addresses (will not canonicalize). |
| 113 | { "[2001:4860:", "http", |
| 114 | url_parse::Component(), // scheme |
| 115 | url_parse::Component(), // username |
| 116 | url_parse::Component(), // password |
| 117 | url_parse::Component(0, 11), // host |
| 118 | url_parse::Component(), // port |
| 119 | url_parse::Component(), // path |
| 120 | url_parse::Component(), // query |
| 121 | url_parse::Component(), // ref |
| 122 | }, |
| 123 | { "[2001:4860:/foo", "http", |
| 124 | url_parse::Component(), // scheme |
| 125 | url_parse::Component(), // username |
| 126 | url_parse::Component(), // password |
| 127 | url_parse::Component(0, 11), // host |
| 128 | url_parse::Component(), // port |
| 129 | url_parse::Component(11, 4), // path |
| 130 | url_parse::Component(), // query |
| 131 | url_parse::Component(), // ref |
| 132 | }, |
| 133 | { "http://:b005::68]", "http", |
| 134 | url_parse::Component(0, 4), // scheme |
| 135 | url_parse::Component(), // username |
| 136 | url_parse::Component(), // password |
| 137 | url_parse::Component(7, 10), // host |
| 138 | url_parse::Component(), // port |
| 139 | url_parse::Component(), // path |
| 140 | url_parse::Component(), // query |
| 141 | url_parse::Component(), // ref |
| 142 | }, |
| 143 | // Can't do anything useful with this. |
| 144 | { ":b005::68]", "", |
| 145 | url_parse::Component(0, 0), // scheme |
| 146 | url_parse::Component(), // username |
| 147 | url_parse::Component(), // password |
| 148 | url_parse::Component(), // host |
| 149 | url_parse::Component(), // port |
| 150 | url_parse::Component(), // path |
| 151 | url_parse::Component(), // query |
| 152 | url_parse::Component(), // ref |
| 153 | }, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | TEST(URLFixerUpperTest, SegmentURL) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 157 | std::string result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | url_parse::Parsed parts; |
| 159 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 160 | for (size_t i = 0; i < arraysize(segment_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | segment_case value = segment_cases[i]; |
| 162 | result = URLFixerUpper::SegmentURL(value.input, &parts); |
| 163 | EXPECT_EQ(value.result, result); |
| 164 | EXPECT_EQ(value.scheme, parts.scheme); |
| 165 | EXPECT_EQ(value.username, parts.username); |
| 166 | EXPECT_EQ(value.password, parts.password); |
| 167 | EXPECT_EQ(value.host, parts.host); |
| 168 | EXPECT_EQ(value.port, parts.port); |
| 169 | EXPECT_EQ(value.path, parts.path); |
| 170 | EXPECT_EQ(value.query, parts.query); |
| 171 | EXPECT_EQ(value.ref, parts.ref); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // Creates a file and returns its full name as well as the decomposed |
| 176 | // version. Example: |
| 177 | // full_path = "c:\foo\bar.txt" |
| 178 | // dir = "c:\foo" |
| 179 | // file_name = "bar.txt" |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 180 | static bool MakeTempFile(const FilePath& dir, |
| 181 | const FilePath& file_name, |
| 182 | FilePath* full_path) { |
| 183 | *full_path = dir.Append(file_name); |
[email protected] | 7ff3f63 | 2009-10-13 18:43:35 | [diff] [blame] | 184 | return file_util::WriteFile(*full_path, "", 0) == 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Returns true if the given URL is a file: URL that matches the given file |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 188 | static bool IsMatchingFileURL(const std::string& url, |
| 189 | const FilePath& full_file_path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | if (url.length() <= 8) |
| 191 | return false; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 192 | if (std::string("file:///") != url.substr(0, 8)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | return false; // no file:/// prefix |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 194 | if (url.find('\\') != std::string::npos) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 195 | return false; // contains backslashes |
| 196 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 197 | FilePath derived_path; |
[email protected] | 5674185 | 2008-12-17 19:04:50 | [diff] [blame] | 198 | net::FileURLToFilePath(GURL(url), &derived_path); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 199 | |
[email protected] | eccb9d1 | 2009-10-28 05:40:09 | [diff] [blame] | 200 | return FilePath::CompareEqualIgnoreCase(derived_path.value(), |
| 201 | full_file_path.value()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | struct fixup_case { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 205 | const std::string input; |
| 206 | const std::string desired_tld; |
| 207 | const std::string output; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | } fixup_cases[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 209 | {"www.google.com", "", "http://www.google.com/"}, |
| 210 | {" www.google.com ", "", "http://www.google.com/"}, |
| 211 | {" foo.com/asdf bar", "", "http://foo.com/asdf bar"}, |
| 212 | {"..www.google.com..", "", "http://www.google.com./"}, |
| 213 | {"http://......", "", "http://....../"}, |
[email protected] | ce85f60 | 2009-11-07 01:34:53 | [diff] [blame] | 214 | {"http://host.com:ninety-two/", "", "http://host.com:ninety-two/"}, |
| 215 | {"http://host.com:ninety-two?foo", "", "http://host.com:ninety-two/?foo"}, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 216 | {"google.com:123", "", "http://google.com:123/"}, |
| 217 | {"about:", "", "about:"}, |
| 218 | {"about:version", "", "about:version"}, |
| 219 | {"www:123", "", "http://www:123/"}, |
| 220 | {" www:123", "", "http://www:123/"}, |
| 221 | {"www.google.com?foo", "", "http://www.google.com/?foo"}, |
| 222 | {"www.google.com#foo", "", "http://www.google.com/#foo"}, |
| 223 | {"www.google.com?", "", "http://www.google.com/?"}, |
| 224 | {"www.google.com#", "", "http://www.google.com/#"}, |
| 225 | {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"}, |
| 226 | {"[email protected]", "", "http://[email protected]/"}, |
| 227 | {"\xE6\xB0\xB4.com" , "", "http://\xE6\xB0\xB4.com/"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 228 | // It would be better if this next case got treated as http, but I don't see |
| 229 | // a clean way to guess this isn't the new-and-exciting "user" scheme. |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 230 | {"user:[email protected]:8080/", "", "user:[email protected]:8080/"}, |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 231 | // {"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"}, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 232 | {"ftp.google.com", "", "ftp://ftp.google.com/"}, |
| 233 | {" ftp.google.com", "", "ftp://ftp.google.com/"}, |
| 234 | {"FTP.GooGle.com", "", "ftp://FTP.GooGle.com/"}, |
| 235 | {"ftpblah.google.com", "", "http://ftpblah.google.com/"}, |
| 236 | {"ftp", "", "http://ftp/"}, |
| 237 | {"google.ftp.com", "", "http://google.ftp.com/"}, |
[email protected] | 90f933a | 2009-03-05 03:41:51 | [diff] [blame] | 238 | // URLs which end with 0x85 (NEL in ISO-8859). |
| 239 | { "http://google.com/search?q=\xd0\x85", "", |
| 240 | "http://google.com/search?q=\xd0\x85" |
| 241 | }, |
| 242 | { "http://google.com/search?q=\xec\x97\x85", "", |
| 243 | "http://google.com/search?q=\xec\x97\x85" |
| 244 | }, |
| 245 | { "http://google.com/search?q=\xf0\x90\x80\x85", "", |
| 246 | "http://google.com/search?q=\xf0\x90\x80\x85" |
| 247 | }, |
| 248 | // URLs which end with 0xA0 (non-break space in ISO-8859). |
| 249 | { "http://google.com/search?q=\xd0\xa0", "", |
| 250 | "http://google.com/search?q=\xd0\xa0" |
| 251 | }, |
| 252 | { "http://google.com/search?q=\xec\x97\xa0", "", |
| 253 | "http://google.com/search?q=\xec\x97\xa0" |
| 254 | }, |
| 255 | { "http://google.com/search?q=\xf0\x90\x80\xa0", "", |
| 256 | "http://google.com/search?q=\xf0\x90\x80\xa0" |
| 257 | }, |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame] | 258 | // URLs containing IPv6 literals. |
| 259 | {"[2001:db8::2]", "", "http://[2001:db8::2]/"}, |
| 260 | {"[::]:80", "", "http://[::]:80/"}, |
| 261 | {"[::]:80/path", "", "http://[::]:80/path"}, |
| 262 | // TODO(pmarks): Maybe we should parse bare IPv6 literals someday. |
| 263 | {"::1", "", "::1"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | TEST(URLFixerUpperTest, FixupURL) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 267 | std::string output; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 268 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 269 | for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | fixup_case value = fixup_cases[i]; |
| 271 | output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 272 | EXPECT_EQ(value.output, output); |
| 273 | } |
| 274 | |
| 275 | // Check the TLD-appending functionality |
| 276 | fixup_case tld_cases[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 277 | {"google", "com", "http://www.google.com/"}, |
| 278 | {"google.", "com", "http://www.google.com/"}, |
| 279 | {"google..", "com", "http://www.google.com/"}, |
| 280 | {".google", "com", "http://www.google.com/"}, |
| 281 | {"www.google", "com", "http://www.google.com/"}, |
| 282 | {"google.com", "com", "http://google.com/"}, |
| 283 | {"http://google", "com", "http://www.google.com/"}, |
| 284 | {"..google..", "com", "http://www.google.com/"}, |
| 285 | {"http://www.google", "com", "http://www.google.com/"}, |
[email protected] | 7e56381 | 2010-03-22 20:05:59 | [diff] [blame^] | 286 | {"9999999999999999", "com", "http://www.9999999999999999.com/"}, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 287 | {"google/foo", "com", "http://www.google.com/foo"}, |
| 288 | {"google.com/foo", "com", "http://google.com/foo"}, |
| 289 | {"google/?foo=.com", "com", "http://www.google.com/?foo=.com"}, |
| 290 | {"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."}, |
| 291 | {"google.com/?foo=.com", "com", "http://google.com/?foo=.com"}, |
| 292 | {"http://www.google.com", "com", "http://www.google.com/"}, |
| 293 | {"google:123", "com", "http://www.google.com:123/"}, |
| 294 | {"http://google:123", "com", "http://www.google.com:123/"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | }; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 296 | for (size_t i = 0; i < arraysize(tld_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 297 | fixup_case value = tld_cases[i]; |
| 298 | output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 299 | EXPECT_EQ(value.output, output); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | // Test different types of file inputs to URIFixerUpper::FixupURL. This |
| 304 | // doesn't go into the nice array of fixups above since the file input |
| 305 | // has to exist. |
| 306 | TEST(URLFixerUpperTest, FixupFile) { |
| 307 | // this "original" filename is the one we tweak to get all the variations |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 308 | FilePath dir; |
| 309 | FilePath original; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 311 | ASSERT_TRUE(MakeTempFile( |
| 312 | dir, |
| 313 | FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), |
| 314 | &original)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 315 | |
| 316 | // reference path |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 317 | std::string golden = net::FilePathToFileURL(original).spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | |
| 319 | // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 320 | #if defined(OS_WIN) |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 321 | std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()), |
| 322 | ""); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 323 | #elif defined(OS_POSIX) |
| 324 | std::string fixedup = URLFixerUpper::FixupURL(original.value(), ""); |
| 325 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 326 | EXPECT_EQ(golden, fixedup); |
| 327 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 328 | // TODO(port): Make some equivalent tests for posix. |
| 329 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | // c|/foo\bar.txt -> file:///c:/foo/bar.txt (pipe allowed instead of colon) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 331 | std::string cur(WideToUTF8(original.value())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | EXPECT_EQ(':', cur[1]); |
| 333 | cur[1] = '|'; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 334 | fixedup = URLFixerUpper::FixupURL(cur, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 335 | EXPECT_EQ(golden, fixedup); |
| 336 | |
| 337 | fixup_case file_cases[] = { |
| 338 | // File URLs go through GURL, which tries to escape intelligently. |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 339 | {"c:\\This%20is a non-existent file.txt", "", |
| 340 | "file:///C:/This%2520is%20a%20non-existent%20file.txt"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | |
| 342 | // \\foo\bar.txt -> file://foo/bar.txt |
| 343 | // UNC paths, this file won't exist, but since there are no escapes, it |
| 344 | // should be returned just converted to a file: URL. |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 345 | {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", |
| 346 | "file://somenonexistenthost/foo/bar.txt"}, |
[email protected] | 7fc13ed | 2010-03-06 05:06:20 | [diff] [blame] | 347 | // We do this strictly, like IE8, which only accepts this form using |
| 348 | // backslashes and not forward ones. Its a bit weird that the host/path is |
| 349 | // "more canonicalized" in the UNC case above, and in the http case it |
| 350 | // isn't lowercased, etc. That level of canonicalization will happen when |
| 351 | // it's actually turned into a GURL, so we don't care about it here. Turning |
| 352 | // "//foo" into "http" matches Firefox and IE, silly though it may seem |
| 353 | // (it falls out of adding "http" as the default protocol if you haven't |
| 354 | // entered one). |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 355 | {"//SomeNonexistentHost\\foo/bar.txt", "", |
[email protected] | 7fc13ed | 2010-03-06 05:06:20 | [diff] [blame] | 356 | "http://SomeNonexistentHost\\foo/bar.txt"}, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 357 | {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | |
| 359 | // These are fixups we don't do, but could consider: |
| 360 | // |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 361 | // {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, |
| 362 | // {"file:c:", "", "file:///c:/"}, |
| 363 | // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"}, |
| 364 | // {"file:c|Program Files", "", "file:///c:/Program Files"}, |
| 365 | // {"file:///foo:/bar", "", "file://foo/bar"}, |
| 366 | // {"file:/file", "", "file://file/"}, |
| 367 | // {"file:////////c:\\foo", "", "file:///c:/foo"}, |
| 368 | // {"file://server/folder/file", "", "file://server/folder/file"}, |
| 369 | // {"file:/\\/server\\folder/file", "", "file://server/folder/file"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | }; |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 371 | #elif defined(OS_POSIX) |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 372 | |
| 373 | #if defined(OS_MACOSX) |
| 374 | #define HOME "/Users/" |
| 375 | #else |
| 376 | #define HOME "/home/" |
| 377 | #endif |
| 378 | URLFixerUpper::home_directory_override = "/foo"; |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 379 | fixup_case file_cases[] = { |
| 380 | // File URLs go through GURL, which tries to escape intelligently. |
| 381 | {"/This%20is a non-existent file.txt", "", |
| 382 | "file:///This%2520is%20a%20non-existent%20file.txt"}, |
| 383 | // A plain "/" refers to the root. |
| 384 | {"/", "", |
| 385 | "file:///"}, |
[email protected] | 762c554 | 2009-10-21 16:45:38 | [diff] [blame] | 386 | |
| 387 | // These rely on the above home_directory_override. |
| 388 | {"~", "", |
| 389 | "file:///foo"}, |
| 390 | {"~/bar", "", |
| 391 | "file:///foo/bar"}, |
| 392 | |
| 393 | // References to other users' homedirs. |
| 394 | {"~foo", "", |
| 395 | "file://" HOME "foo"}, |
| 396 | {"~x/blah", "", |
| 397 | "file://" HOME "x/blah"}, |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 398 | }; |
| 399 | #endif |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 400 | for (size_t i = 0; i < arraysize(file_cases); i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 401 | fixedup = URLFixerUpper::FixupURL(file_cases[i].input, |
| 402 | file_cases[i].desired_tld); |
| 403 | EXPECT_EQ(file_cases[i].output, fixedup); |
| 404 | } |
| 405 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 406 | EXPECT_TRUE(file_util::Delete(original, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | TEST(URLFixerUpperTest, FixupRelativeFile) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 410 | FilePath full_path, dir; |
| 411 | FilePath file_part(FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
| 413 | ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); |
[email protected] | 13044cd | 2010-02-10 18:56:18 | [diff] [blame] | 414 | ASSERT_TRUE(file_util::AbsolutePath(&full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | |
| 416 | // make sure we pass through good URLs |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 417 | std::string fixedup; |
| 418 | for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | fixup_case value = fixup_cases[i]; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 420 | #if defined(OS_WIN) |
| 421 | FilePath input(UTF8ToWide(value.input)); |
| 422 | #elif defined(OS_POSIX) |
| 423 | FilePath input(value.input); |
| 424 | #endif |
| 425 | fixedup = URLFixerUpper::FixupRelativeFile(dir, input); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | EXPECT_EQ(value.output, fixedup); |
| 427 | } |
| 428 | |
| 429 | // make sure the existing file got fixed-up to a file URL, and that there |
| 430 | // are no backslashes |
| 431 | fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 432 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
| 433 | EXPECT_TRUE(file_util::Delete(full_path, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | |
| 435 | // create a filename we know doesn't exist and make sure it doesn't get |
| 436 | // fixed up to a file URL |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 437 | FilePath nonexistent_file( |
| 438 | FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 440 | EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
| 442 | |
| 443 | // make a subdir to make sure relative paths with directories work, also |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 444 | // test spaces: |
| 445 | // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 446 | FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); |
| 447 | FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt")); |
| 448 | FilePath new_dir = dir.Append(sub_dir); |
| 449 | file_util::CreateDirectory(new_dir); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
[email protected] | 13044cd | 2010-02-10 18:56:18 | [diff] [blame] | 451 | ASSERT_TRUE(file_util::AbsolutePath(&full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | |
| 453 | // test file in the subdir |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 454 | FilePath relative_file = sub_dir.Append(sub_file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 455 | fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 456 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 458 | // test file in the subdir with different slashes and escaping. |
| 459 | FilePath::StringType relative_file_str = sub_dir.value() + |
| 460 | FILE_PATH_LITERAL("/") + sub_file.value(); |
| 461 | ReplaceSubstringsAfterOffset(&relative_file_str, 0, |
| 462 | FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); |
| 463 | fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); |
| 464 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 465 | |
| 466 | // test relative directories and duplicate slashes |
| 467 | // (should resolve to the same file as above) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 468 | relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + |
| 469 | sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); |
| 470 | fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); |
| 471 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 472 | |
| 473 | // done with the subdir |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 474 | EXPECT_TRUE(file_util::Delete(full_path, false)); |
| 475 | EXPECT_TRUE(file_util::Delete(new_dir, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 476 | } |