license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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. |
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] | f870a32 | 2009-01-16 21:47:27 | [diff] [blame] | 11 | #include "chrome/browser/net/url_fixer_upper.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "chrome/common/chrome_paths.h" |
| 13 | #include "googleurl/src/url_parse.h" |
| 14 | #include "googleurl/src/gurl.h" |
| 15 | #include "net/base/net_util.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace { |
| 19 | class URLFixerUpperTest : public testing::Test { |
| 20 | }; |
| 21 | }; |
| 22 | |
| 23 | std::ostream& operator<<(std::ostream& os, const url_parse::Component& part) { |
| 24 | return os << "(begin=" << part.begin << ", len=" << part.len << ")"; |
| 25 | } |
| 26 | |
| 27 | struct segment_case { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 28 | const std::string input; |
| 29 | const std::string result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | const url_parse::Component scheme; |
| 31 | const url_parse::Component username; |
| 32 | const url_parse::Component password; |
| 33 | const url_parse::Component host; |
| 34 | const url_parse::Component port; |
| 35 | const url_parse::Component path; |
| 36 | const url_parse::Component query; |
| 37 | const url_parse::Component ref; |
| 38 | }; |
| 39 | |
| 40 | static const segment_case segment_cases[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 41 | { "http://www.google.com/", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | url_parse::Component(0, 4), // scheme |
| 43 | url_parse::Component(), // username |
| 44 | url_parse::Component(), // password |
| 45 | url_parse::Component(7, 14), // host |
| 46 | url_parse::Component(), // port |
| 47 | url_parse::Component(21, 1), // path |
| 48 | url_parse::Component(), // query |
| 49 | url_parse::Component(), // ref |
| 50 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 51 | { "aBoUt:vErSiOn", "about", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | url_parse::Component(0, 5), // scheme |
| 53 | url_parse::Component(), // username |
| 54 | url_parse::Component(), // password |
| 55 | url_parse::Component(), // host |
| 56 | url_parse::Component(), // port |
| 57 | url_parse::Component(), // path |
| 58 | url_parse::Component(), // query |
| 59 | url_parse::Component(), // ref |
| 60 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 61 | { " www.google.com:124?foo#", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | url_parse::Component(), // scheme |
| 63 | url_parse::Component(), // username |
| 64 | url_parse::Component(), // password |
| 65 | url_parse::Component(4, 14), // host |
| 66 | url_parse::Component(19, 3), // port |
| 67 | url_parse::Component(), // path |
| 68 | url_parse::Component(23, 3), // query |
| 69 | url_parse::Component(27, 0), // ref |
| 70 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 71 | { "[email protected]", "http", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | url_parse::Component(), // scheme |
| 73 | url_parse::Component(0, 4), // username |
| 74 | url_parse::Component(), // password |
| 75 | url_parse::Component(5, 14), // host |
| 76 | url_parse::Component(), // port |
| 77 | url_parse::Component(), // path |
| 78 | url_parse::Component(), // query |
| 79 | url_parse::Component(), // ref |
| 80 | }, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 81 | { "ftp:/user:P:[email protected]...::23///pub?foo#bar", "ftp", |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | url_parse::Component(0, 3), // scheme |
| 83 | url_parse::Component(5, 4), // username |
| 84 | url_parse::Component(10, 7), // password |
| 85 | url_parse::Component(18, 20), // host |
| 86 | url_parse::Component(39, 2), // port |
| 87 | url_parse::Component(41, 6), // path |
| 88 | url_parse::Component(48, 3), // query |
| 89 | url_parse::Component(52, 3), // ref |
| 90 | }, |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame^] | 91 | { "[2001:db8::1]/path", "http", |
| 92 | url_parse::Component(), // scheme |
| 93 | url_parse::Component(), // username |
| 94 | url_parse::Component(), // password |
| 95 | url_parse::Component(0, 13), // host |
| 96 | url_parse::Component(), // port |
| 97 | url_parse::Component(13, 5), // path |
| 98 | url_parse::Component(), // query |
| 99 | url_parse::Component(), // ref |
| 100 | }, |
| 101 | { "[::1]", "http", |
| 102 | url_parse::Component(), // scheme |
| 103 | url_parse::Component(), // username |
| 104 | url_parse::Component(), // password |
| 105 | url_parse::Component(0, 5), // host |
| 106 | url_parse::Component(), // port |
| 107 | url_parse::Component(), // path |
| 108 | url_parse::Component(), // query |
| 109 | url_parse::Component(), // ref |
| 110 | }, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | TEST(URLFixerUpperTest, SegmentURL) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 114 | std::string result; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | url_parse::Parsed parts; |
| 116 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 117 | for (size_t i = 0; i < arraysize(segment_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | segment_case value = segment_cases[i]; |
| 119 | result = URLFixerUpper::SegmentURL(value.input, &parts); |
| 120 | EXPECT_EQ(value.result, result); |
| 121 | EXPECT_EQ(value.scheme, parts.scheme); |
| 122 | EXPECT_EQ(value.username, parts.username); |
| 123 | EXPECT_EQ(value.password, parts.password); |
| 124 | EXPECT_EQ(value.host, parts.host); |
| 125 | EXPECT_EQ(value.port, parts.port); |
| 126 | EXPECT_EQ(value.path, parts.path); |
| 127 | EXPECT_EQ(value.query, parts.query); |
| 128 | EXPECT_EQ(value.ref, parts.ref); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Creates a file and returns its full name as well as the decomposed |
| 133 | // version. Example: |
| 134 | // full_path = "c:\foo\bar.txt" |
| 135 | // dir = "c:\foo" |
| 136 | // file_name = "bar.txt" |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 137 | static bool MakeTempFile(const FilePath& dir, |
| 138 | const FilePath& file_name, |
| 139 | FilePath* full_path) { |
| 140 | *full_path = dir.Append(file_name); |
| 141 | return file_util::WriteFile(full_path->ToWStringHack(), NULL, 0) == 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // 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] | 145 | static bool IsMatchingFileURL(const std::string& url, |
| 146 | const FilePath& full_file_path) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | if (url.length() <= 8) |
| 148 | return false; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 149 | if (std::string("file:///") != url.substr(0, 8)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 150 | return false; // no file:/// prefix |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 151 | if (url.find('\\') != std::string::npos) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | return false; // contains backslashes |
| 153 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 154 | FilePath derived_path; |
[email protected] | 5674185 | 2008-12-17 19:04:50 | [diff] [blame] | 155 | net::FileURLToFilePath(GURL(url), &derived_path); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 156 | |
| 157 | FilePath::StringType derived_path_str = derived_path.value(); |
| 158 | return (derived_path_str.length() == full_file_path.value().length()) && |
| 159 | std::equal(derived_path_str.begin(), |
| 160 | derived_path_str.end(), |
| 161 | full_file_path.value().begin(), |
| 162 | CaseInsensitiveCompare<FilePath::CharType>()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | struct fixup_case { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 166 | const std::string input; |
| 167 | const std::string desired_tld; |
| 168 | const std::string output; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | } fixup_cases[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 170 | {"www.google.com", "", "http://www.google.com/"}, |
| 171 | {" www.google.com ", "", "http://www.google.com/"}, |
| 172 | {" foo.com/asdf bar", "", "http://foo.com/asdf bar"}, |
| 173 | {"..www.google.com..", "", "http://www.google.com./"}, |
| 174 | {"http://......", "", "http://....../"}, |
| 175 | {"http://host.com:ninety-two/", "", "http://host.com/"}, |
| 176 | {"http://host.com:ninety-two?foo", "", "http://host.com/?foo"}, |
| 177 | {"google.com:123", "", "http://google.com:123/"}, |
| 178 | {"about:", "", "about:"}, |
| 179 | {"about:version", "", "about:version"}, |
| 180 | {"www:123", "", "http://www:123/"}, |
| 181 | {" www:123", "", "http://www:123/"}, |
| 182 | {"www.google.com?foo", "", "http://www.google.com/?foo"}, |
| 183 | {"www.google.com#foo", "", "http://www.google.com/#foo"}, |
| 184 | {"www.google.com?", "", "http://www.google.com/?"}, |
| 185 | {"www.google.com#", "", "http://www.google.com/#"}, |
| 186 | {"www.google.com:123?foo#bar", "", "http://www.google.com:123/?foo#bar"}, |
| 187 | {"[email protected]", "", "http://[email protected]/"}, |
| 188 | {"\xE6\xB0\xB4.com" , "", "http://\xE6\xB0\xB4.com/"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | // It would be better if this next case got treated as http, but I don't see |
| 190 | // 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] | 191 | {"user:[email protected]:8080/", "", "user:[email protected]:8080/"}, |
| 192 | //{"file:///c:/foo/bar%20baz.txt", "", "file:///C:/foo/bar%20baz.txt"}, |
| 193 | {"ftp.google.com", "", "ftp://ftp.google.com/"}, |
| 194 | {" ftp.google.com", "", "ftp://ftp.google.com/"}, |
| 195 | {"FTP.GooGle.com", "", "ftp://FTP.GooGle.com/"}, |
| 196 | {"ftpblah.google.com", "", "http://ftpblah.google.com/"}, |
| 197 | {"ftp", "", "http://ftp/"}, |
| 198 | {"google.ftp.com", "", "http://google.ftp.com/"}, |
[email protected] | 90f933a | 2009-03-05 03:41:51 | [diff] [blame] | 199 | // URLs which end with 0x85 (NEL in ISO-8859). |
| 200 | { "http://google.com/search?q=\xd0\x85", "", |
| 201 | "http://google.com/search?q=\xd0\x85" |
| 202 | }, |
| 203 | { "http://google.com/search?q=\xec\x97\x85", "", |
| 204 | "http://google.com/search?q=\xec\x97\x85" |
| 205 | }, |
| 206 | { "http://google.com/search?q=\xf0\x90\x80\x85", "", |
| 207 | "http://google.com/search?q=\xf0\x90\x80\x85" |
| 208 | }, |
| 209 | // URLs which end with 0xA0 (non-break space in ISO-8859). |
| 210 | { "http://google.com/search?q=\xd0\xa0", "", |
| 211 | "http://google.com/search?q=\xd0\xa0" |
| 212 | }, |
| 213 | { "http://google.com/search?q=\xec\x97\xa0", "", |
| 214 | "http://google.com/search?q=\xec\x97\xa0" |
| 215 | }, |
| 216 | { "http://google.com/search?q=\xf0\x90\x80\xa0", "", |
| 217 | "http://google.com/search?q=\xf0\x90\x80\xa0" |
| 218 | }, |
[email protected] | 818071ce | 2009-05-18 01:25:25 | [diff] [blame^] | 219 | // URLs containing IPv6 literals. |
| 220 | {"[2001:db8::2]", "", "http://[2001:db8::2]/"}, |
| 221 | {"[::]:80", "", "http://[::]:80/"}, |
| 222 | {"[::]:80/path", "", "http://[::]:80/path"}, |
| 223 | // TODO(pmarks): Maybe we should parse bare IPv6 literals someday. |
| 224 | {"::1", "", "::1"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | TEST(URLFixerUpperTest, FixupURL) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 228 | std::string output; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 229 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 230 | for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 231 | fixup_case value = fixup_cases[i]; |
| 232 | output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 233 | EXPECT_EQ(value.output, output); |
| 234 | } |
| 235 | |
| 236 | // Check the TLD-appending functionality |
| 237 | fixup_case tld_cases[] = { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 238 | {"google", "com", "http://www.google.com/"}, |
| 239 | {"google.", "com", "http://www.google.com/"}, |
| 240 | {"google..", "com", "http://www.google.com/"}, |
| 241 | {".google", "com", "http://www.google.com/"}, |
| 242 | {"www.google", "com", "http://www.google.com/"}, |
| 243 | {"google.com", "com", "http://google.com/"}, |
| 244 | {"http://google", "com", "http://www.google.com/"}, |
| 245 | {"..google..", "com", "http://www.google.com/"}, |
| 246 | {"http://www.google", "com", "http://www.google.com/"}, |
| 247 | {"google/foo", "com", "http://www.google.com/foo"}, |
| 248 | {"google.com/foo", "com", "http://google.com/foo"}, |
| 249 | {"google/?foo=.com", "com", "http://www.google.com/?foo=.com"}, |
| 250 | {"www.google/?foo=www.", "com", "http://www.google.com/?foo=www."}, |
| 251 | {"google.com/?foo=.com", "com", "http://google.com/?foo=.com"}, |
| 252 | {"http://www.google.com", "com", "http://www.google.com/"}, |
| 253 | {"google:123", "com", "http://www.google.com:123/"}, |
| 254 | {"http://google:123", "com", "http://www.google.com:123/"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | }; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 256 | for (size_t i = 0; i < arraysize(tld_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 257 | fixup_case value = tld_cases[i]; |
| 258 | output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 259 | EXPECT_EQ(value.output, output); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Test different types of file inputs to URIFixerUpper::FixupURL. This |
| 264 | // doesn't go into the nice array of fixups above since the file input |
| 265 | // has to exist. |
| 266 | TEST(URLFixerUpperTest, FixupFile) { |
| 267 | // this "original" filename is the one we tweak to get all the variations |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 268 | FilePath dir; |
| 269 | FilePath original; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 270 | ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 271 | ASSERT_TRUE(MakeTempFile( |
| 272 | dir, |
| 273 | FilePath(FILE_PATH_LITERAL("url fixer upper existing file.txt")), |
| 274 | &original)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 275 | |
| 276 | // reference path |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 277 | std::string golden = net::FilePathToFileURL(original).spec(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 278 | |
| 279 | // c:\foo\bar.txt -> file:///c:/foo/bar.txt (basic) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 280 | #if defined(OS_WIN) |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 281 | std::string fixedup = URLFixerUpper::FixupURL(WideToUTF8(original.value()), |
| 282 | ""); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 283 | #elif defined(OS_POSIX) |
| 284 | std::string fixedup = URLFixerUpper::FixupURL(original.value(), ""); |
| 285 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 286 | EXPECT_EQ(golden, fixedup); |
| 287 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 288 | // TODO(port): Make some equivalent tests for posix. |
| 289 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | // 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] | 291 | std::string cur(WideToUTF8(original.value())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | EXPECT_EQ(':', cur[1]); |
| 293 | cur[1] = '|'; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 294 | fixedup = URLFixerUpper::FixupURL(cur, ""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 295 | EXPECT_EQ(golden, fixedup); |
| 296 | |
| 297 | fixup_case file_cases[] = { |
| 298 | // File URLs go through GURL, which tries to escape intelligently. |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 299 | {"c:\\This%20is a non-existent file.txt", "", |
| 300 | "file:///C:/This%2520is%20a%20non-existent%20file.txt"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | |
| 302 | // \\foo\bar.txt -> file://foo/bar.txt |
| 303 | // UNC paths, this file won't exist, but since there are no escapes, it |
| 304 | // should be returned just converted to a file: URL. |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 305 | {"\\\\SomeNonexistentHost\\foo\\bar.txt", "", |
| 306 | "file://somenonexistenthost/foo/bar.txt"}, |
| 307 | {"//SomeNonexistentHost\\foo/bar.txt", "", |
| 308 | "file://somenonexistenthost/foo/bar.txt"}, |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 309 | {"file:///C:/foo/bar", "", "file:///C:/foo/bar"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | |
| 311 | // These are fixups we don't do, but could consider: |
| 312 | // |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 313 | // {"file://C:/foo/bar", "", "file:///C:/foo/bar"}, |
| 314 | // {"file:c:", "", "file:///c:/"}, |
| 315 | // {"file:c:WINDOWS", "", "file:///c:/WINDOWS"}, |
| 316 | // {"file:c|Program Files", "", "file:///c:/Program Files"}, |
| 317 | // {"file:///foo:/bar", "", "file://foo/bar"}, |
| 318 | // {"file:/file", "", "file://file/"}, |
| 319 | // {"file:////////c:\\foo", "", "file:///c:/foo"}, |
| 320 | // {"file://server/folder/file", "", "file://server/folder/file"}, |
| 321 | // {"file:/\\/server\\folder/file", "", "file://server/folder/file"}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 322 | }; |
[email protected] | ba1321d1 | 2009-04-21 22:42:29 | [diff] [blame] | 323 | #elif defined(OS_POSIX) |
| 324 | fixup_case file_cases[] = { |
| 325 | // File URLs go through GURL, which tries to escape intelligently. |
| 326 | {"/This%20is a non-existent file.txt", "", |
| 327 | "file:///This%2520is%20a%20non-existent%20file.txt"}, |
| 328 | // A plain "/" refers to the root. |
| 329 | {"/", "", |
| 330 | "file:///"}, |
| 331 | }; |
| 332 | #endif |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 333 | for (size_t i = 0; i < arraysize(file_cases); i++) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | fixedup = URLFixerUpper::FixupURL(file_cases[i].input, |
| 335 | file_cases[i].desired_tld); |
| 336 | EXPECT_EQ(file_cases[i].output, fixedup); |
| 337 | } |
| 338 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 339 | EXPECT_TRUE(file_util::Delete(original, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | TEST(URLFixerUpperTest, FixupRelativeFile) { |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 343 | FilePath full_path, dir; |
| 344 | FilePath file_part(FILE_PATH_LITERAL("url_fixer_upper_existing_file.txt")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
| 346 | ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); |
| 347 | |
| 348 | // make sure we pass through good URLs |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 349 | std::string fixedup; |
| 350 | for (size_t i = 0; i < arraysize(fixup_cases); ++i) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 351 | fixup_case value = fixup_cases[i]; |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 352 | #if defined(OS_WIN) |
| 353 | FilePath input(UTF8ToWide(value.input)); |
| 354 | #elif defined(OS_POSIX) |
| 355 | FilePath input(value.input); |
| 356 | #endif |
| 357 | fixedup = URLFixerUpper::FixupRelativeFile(dir, input); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 358 | EXPECT_EQ(value.output, fixedup); |
| 359 | } |
| 360 | |
| 361 | // make sure the existing file got fixed-up to a file URL, and that there |
| 362 | // are no backslashes |
| 363 | fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 364 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
| 365 | EXPECT_TRUE(file_util::Delete(full_path, false)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | |
| 367 | // create a filename we know doesn't exist and make sure it doesn't get |
| 368 | // fixed up to a file URL |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 369 | FilePath nonexistent_file( |
| 370 | FILE_PATH_LITERAL("url_fixer_upper_nonexistent_file.txt")); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 372 | EXPECT_NE(std::string("file:///"), fixedup.substr(0, 8)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 373 | EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
| 374 | |
| 375 | // make a subdir to make sure relative paths with directories work, also |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 376 | // test spaces: |
| 377 | // "app_dir\url fixer-upper dir\url fixer-upper existing file.txt" |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 378 | FilePath sub_dir(FILE_PATH_LITERAL("url fixer-upper dir")); |
| 379 | FilePath sub_file(FILE_PATH_LITERAL("url fixer-upper existing file.txt")); |
| 380 | FilePath new_dir = dir.Append(sub_dir); |
| 381 | file_util::CreateDirectory(new_dir); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 382 | ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
| 383 | |
| 384 | // test file in the subdir |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 385 | FilePath relative_file = sub_dir.Append(sub_file); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 387 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 388 | |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 389 | // test file in the subdir with different slashes and escaping. |
| 390 | FilePath::StringType relative_file_str = sub_dir.value() + |
| 391 | FILE_PATH_LITERAL("/") + sub_file.value(); |
| 392 | ReplaceSubstringsAfterOffset(&relative_file_str, 0, |
| 393 | FILE_PATH_LITERAL(" "), FILE_PATH_LITERAL("%20")); |
| 394 | fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); |
| 395 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 396 | |
| 397 | // test relative directories and duplicate slashes |
| 398 | // (should resolve to the same file as above) |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 399 | relative_file_str = sub_dir.value() + FILE_PATH_LITERAL("/../") + |
| 400 | sub_dir.value() + FILE_PATH_LITERAL("///./") + sub_file.value(); |
| 401 | fixedup = URLFixerUpper::FixupRelativeFile(dir, FilePath(relative_file_str)); |
| 402 | EXPECT_TRUE(IsMatchingFileURL(fixedup, full_path)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 403 | |
| 404 | // done with the subdir |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 405 | EXPECT_TRUE(file_util::Delete(full_path, false)); |
| 406 | EXPECT_TRUE(file_util::Delete(new_dir, true)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 407 | } |