| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <windows.h> |
| 6 | 7 |
| 7 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 8 #include "base/file_path.h" | |
| 9 #include "base/file_util.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 12 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 13 #include "chrome/browser/url_fixer_upper.h" | 12 #include "chrome/browser/url_fixer_upper.h" |
| 14 #include "chrome/common/chrome_paths.h" | 13 #include "chrome/common/chrome_paths.h" |
| 15 #include "googleurl/src/url_parse.h" | 14 #include "googleurl/src/url_parse.h" |
| 16 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
| 17 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 18 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 url_parse::Component(41, 6), // path | 88 url_parse::Component(41, 6), // path |
| 90 url_parse::Component(48, 3), // query | 89 url_parse::Component(48, 3), // query |
| 91 url_parse::Component(52, 3), // ref | 90 url_parse::Component(52, 3), // ref |
| 92 }, | 91 }, |
| 93 }; | 92 }; |
| 94 | 93 |
| 95 TEST(URLFixerUpperTest, SegmentURL) { | 94 TEST(URLFixerUpperTest, SegmentURL) { |
| 96 std::wstring result; | 95 std::wstring result; |
| 97 url_parse::Parsed parts; | 96 url_parse::Parsed parts; |
| 98 | 97 |
| 99 for (size_t i = 0; i < arraysize(segment_cases); ++i) { | 98 for (int i = 0; i < arraysize(segment_cases); ++i) { |
| 100 segment_case value = segment_cases[i]; | 99 segment_case value = segment_cases[i]; |
| 101 result = URLFixerUpper::SegmentURL(value.input, &parts); | 100 result = URLFixerUpper::SegmentURL(value.input, &parts); |
| 102 EXPECT_EQ(value.result, result); | 101 EXPECT_EQ(value.result, result); |
| 103 EXPECT_EQ(value.scheme, parts.scheme); | 102 EXPECT_EQ(value.scheme, parts.scheme); |
| 104 EXPECT_EQ(value.username, parts.username); | 103 EXPECT_EQ(value.username, parts.username); |
| 105 EXPECT_EQ(value.password, parts.password); | 104 EXPECT_EQ(value.password, parts.password); |
| 106 EXPECT_EQ(value.host, parts.host); | 105 EXPECT_EQ(value.host, parts.host); |
| 107 EXPECT_EQ(value.port, parts.port); | 106 EXPECT_EQ(value.port, parts.port); |
| 108 EXPECT_EQ(value.path, parts.path); | 107 EXPECT_EQ(value.path, parts.path); |
| 109 EXPECT_EQ(value.query, parts.query); | 108 EXPECT_EQ(value.query, parts.query); |
| 110 EXPECT_EQ(value.ref, parts.ref); | 109 EXPECT_EQ(value.ref, parts.ref); |
| 111 } | 110 } |
| 112 } | 111 } |
| 113 | 112 |
| 114 // Creates a file and returns its full name as well as the decomposed | 113 // Creates a file and returns its full name as well as the decomposed |
| 115 // version. Example: | 114 // version. Example: |
| 116 // full_path = "c:\foo\bar.txt" | 115 // full_path = "c:\foo\bar.txt" |
| 117 // dir = "c:\foo" | 116 // dir = "c:\foo" |
| 118 // file_name = "bar.txt" | 117 // file_name = "bar.txt" |
| 119 static bool MakeTempFile(const std::wstring& dir, | 118 static bool MakeTempFile(const std::wstring& dir, |
| 120 const std::wstring& file_name, | 119 const std::wstring& file_name, |
| 121 std::wstring* full_path) { | 120 std::wstring* full_path) { |
| 122 FilePath dir_path = FilePath::FromWStringHack(dir); | 121 *full_path = dir + L"\\" + file_name; |
| 123 FilePath file_name_path = FilePath::FromWStringHack(file_name); | 122 |
| 124 FilePath path = dir_path.Append(file_name_path.value()); | 123 HANDLE hfile = CreateFile(full_path->c_str(), GENERIC_READ | GENERIC_WRITE, |
| 125 if (!file_util::CreateTemporaryFileName(&path)) | 124 0, NULL, CREATE_ALWAYS, 0, NULL); |
| 125 if (hfile == NULL || hfile == INVALID_HANDLE_VALUE) |
| 126 return false; | 126 return false; |
| 127 *full_path = path.ToWStringHack(); | 127 CloseHandle(hfile); |
| 128 return true; | 128 return true; |
| 129 } | 129 } |
| 130 | 130 |
| 131 // Returns true if the given URL is a file: URL that matches the given file | 131 // Returns true if the given URL is a file: URL that matches the given file |
| 132 static bool IsMatchingFileURL(const std::wstring& url, | 132 static bool IsMatchingFileURL(const std::wstring& url, |
| 133 const std::wstring& full_file_path) { | 133 const std::wstring& full_file_path) { |
| 134 if (url.length() <= 8) | 134 if (url.length() <= 8) |
| 135 return false; | 135 return false; |
| 136 if (std::wstring(L"file:///") != url.substr(0, 8)) | 136 if (std::wstring(L"file:///") != url.substr(0, 8)) |
| 137 return false; // no file:/// prefix | 137 return false; // no file:/// prefix |
| 138 if (url.find('\\') != std::wstring::npos) | 138 if (url.find('\\') != std::wstring::npos) |
| 139 return false; // contains backslashes | 139 return false; // contains backslashes |
| 140 | 140 |
| 141 std::wstring derived_path; | 141 std::wstring derived_path; |
| 142 net::FileURLToFilePath(GURL(WideToUTF8(url)), &derived_path); | 142 net::FileURLToFilePath(GURL(url), &derived_path); |
| 143 return (derived_path.length() == full_file_path.length()) && | 143 return (derived_path.length() == full_file_path.length()) && |
| 144 std::equal(derived_path.begin(), derived_path.end(), | 144 std::equal(derived_path.begin(), derived_path.end(), |
| 145 full_file_path.begin(), CaseInsensitiveCompare<wchar_t>()); | 145 full_file_path.begin(), CaseInsensitiveCompare<wchar_t>()); |
| 146 } | 146 } |
| 147 | 147 |
| 148 struct fixup_case { | 148 struct fixup_case { |
| 149 const std::wstring input; | 149 const std::wstring input; |
| 150 const std::wstring desired_tld; | 150 const std::wstring desired_tld; |
| 151 const std::wstring output; | 151 const std::wstring output; |
| 152 } fixup_cases[] = { | 152 } fixup_cases[] = { |
| (...skipping 18 matching lines...) Expand all Loading... |
| 171 {L"\x6C34.com", L"", L"http://\x6C34.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 | 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. | 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/"
}, | 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"}, | 175 //{L"file:///c:/foo/bar%20baz.txt", L"", L"file:///C:/foo/bar%20baz.txt"}, |
| 176 }; | 176 }; |
| 177 | 177 |
| 178 TEST(URLFixerUpperTest, FixupURL) { | 178 TEST(URLFixerUpperTest, FixupURL) { |
| 179 std::wstring output; | 179 std::wstring output; |
| 180 | 180 |
| 181 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { | 181 for (int i = 0; i < arraysize(fixup_cases); ++i) { |
| 182 fixup_case value = fixup_cases[i]; | 182 fixup_case value = fixup_cases[i]; |
| 183 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); | 183 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 184 EXPECT_EQ(value.output, output); | 184 EXPECT_EQ(value.output, output); |
| 185 } | 185 } |
| 186 | 186 |
| 187 // Check the TLD-appending functionality | 187 // Check the TLD-appending functionality |
| 188 fixup_case tld_cases[] = { | 188 fixup_case tld_cases[] = { |
| 189 {L"google", L"com", L"http://www.google.com/"}, | 189 {L"google", L"com", L"http://www.google.com/"}, |
| 190 {L"google.", L"com", L"http://www.google.com/"}, | 190 {L"google.", L"com", L"http://www.google.com/"}, |
| 191 {L"google..", L"com", L"http://www.google.com/"}, | 191 {L"google..", L"com", L"http://www.google.com/"}, |
| 192 {L".google", L"com", L"http://www.google.com/"}, | 192 {L".google", L"com", L"http://www.google.com/"}, |
| 193 {L"www.google", L"com", L"http://www.google.com/"}, | 193 {L"www.google", L"com", L"http://www.google.com/"}, |
| 194 {L"google.com", L"com", L"http://google.com/"}, | 194 {L"google.com", L"com", L"http://google.com/"}, |
| 195 {L"http://google", L"com", L"http://www.google.com/"}, | 195 {L"http://google", L"com", L"http://www.google.com/"}, |
| 196 {L"..google..", L"com", L"http://www.google.com/"}, | 196 {L"..google..", L"com", L"http://www.google.com/"}, |
| 197 {L"http://www.google", L"com", L"http://www.google.com/"}, | 197 {L"http://www.google", L"com", L"http://www.google.com/"}, |
| 198 {L"google/foo", L"com", L"http://www.google.com/foo"}, | 198 {L"google/foo", L"com", L"http://www.google.com/foo"}, |
| 199 {L"google.com/foo", L"com", L"http://google.com/foo"}, | 199 {L"google.com/foo", L"com", L"http://google.com/foo"}, |
| 200 {L"google/?foo=.com", L"com", L"http://www.google.com/?foo=.com"}, | 200 {L"google/?foo=.com", L"com", L"http://www.google.com/?foo=.com"}, |
| 201 {L"www.google/?foo=www.", L"com", L"http://www.google.com/?foo=www."}, | 201 {L"www.google/?foo=www.", L"com", L"http://www.google.com/?foo=www."}, |
| 202 {L"google.com/?foo=.com", L"com", L"http://google.com/?foo=.com"}, | 202 {L"google.com/?foo=.com", L"com", L"http://google.com/?foo=.com"}, |
| 203 {L"http://www.google.com", L"com", L"http://www.google.com/"}, | 203 {L"http://www.google.com", L"com", L"http://www.google.com/"}, |
| 204 {L"google:123", L"com", L"http://www.google.com:123/"}, | 204 {L"google:123", L"com", L"http://www.google.com:123/"}, |
| 205 {L"http://google:123", L"com", L"http://www.google.com:123/"}, | 205 {L"http://google:123", L"com", L"http://www.google.com:123/"}, |
| 206 }; | 206 }; |
| 207 for (size_t i = 0; i < arraysize(tld_cases); ++i) { | 207 for (int i = 0; i < arraysize(tld_cases); ++i) { |
| 208 fixup_case value = tld_cases[i]; | 208 fixup_case value = tld_cases[i]; |
| 209 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); | 209 output = URLFixerUpper::FixupURL(value.input, value.desired_tld); |
| 210 EXPECT_EQ(value.output, output); | 210 EXPECT_EQ(value.output, output); |
| 211 } | 211 } |
| 212 } | 212 } |
| 213 | 213 |
| 214 // Test different types of file inputs to URIFixerUpper::FixupURL. This | 214 // Test different types of file inputs to URIFixerUpper::FixupURL. This |
| 215 // doesn't go into the nice array of fixups above since the file input | 215 // doesn't go into the nice array of fixups above since the file input |
| 216 // has to exist. | 216 // has to exist. |
| 217 TEST(URLFixerUpperTest, FixupFile) { | 217 TEST(URLFixerUpperTest, FixupFile) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 // {L"file://C:/foo/bar", L"", L"file:///C:/foo/bar"}, | 253 // {L"file://C:/foo/bar", L"", L"file:///C:/foo/bar"}, |
| 254 // {L"file:c:", L"", L"file:///c:/"}, | 254 // {L"file:c:", L"", L"file:///c:/"}, |
| 255 // {L"file:c:WINDOWS", L"", L"file:///c:/WINDOWS"}, | 255 // {L"file:c:WINDOWS", L"", L"file:///c:/WINDOWS"}, |
| 256 // {L"file:c|Program Files", L"", L"file:///c:/Program Files"}, | 256 // {L"file:c|Program Files", L"", L"file:///c:/Program Files"}, |
| 257 // {L"file:///foo:/bar", L"", L"file://foo/bar"}, | 257 // {L"file:///foo:/bar", L"", L"file://foo/bar"}, |
| 258 // {L"file:/file", L"", L"file://file/"}, | 258 // {L"file:/file", L"", L"file://file/"}, |
| 259 // {L"file:////////c:\\foo", L"", L"file:///c:/foo"}, | 259 // {L"file:////////c:\\foo", L"", L"file:///c:/foo"}, |
| 260 // {L"file://server/folder/file", L"", L"file://server/folder/file"}, | 260 // {L"file://server/folder/file", L"", L"file://server/folder/file"}, |
| 261 // {L"file:/\\/server\\folder/file", L"", L"file://server/folder/file"}, | 261 // {L"file:/\\/server\\folder/file", L"", L"file://server/folder/file"}, |
| 262 }; | 262 }; |
| 263 for (size_t i = 0; i < arraysize(file_cases); i++) { | 263 for (int i = 0; i < arraysize(file_cases); i++) { |
| 264 fixedup = URLFixerUpper::FixupURL(file_cases[i].input, | 264 fixedup = URLFixerUpper::FixupURL(file_cases[i].input, |
| 265 file_cases[i].desired_tld); | 265 file_cases[i].desired_tld); |
| 266 EXPECT_EQ(file_cases[i].output, fixedup); | 266 EXPECT_EQ(file_cases[i].output, fixedup); |
| 267 } | 267 } |
| 268 | 268 |
| 269 EXPECT_TRUE(file_util::Delete(original, false)); | 269 EXPECT_TRUE(DeleteFile(original.c_str())); |
| 270 } | 270 } |
| 271 | 271 |
| 272 TEST(URLFixerUpperTest, FixupRelativeFile) { | 272 TEST(URLFixerUpperTest, FixupRelativeFile) { |
| 273 std::wstring full_path, dir; | 273 std::wstring full_path, dir; |
| 274 std::wstring file_part(L"url_fixer_upper_existing_file.txt"); | 274 std::wstring file_part(L"url_fixer_upper_existing_file.txt"); |
| 275 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); | 275 ASSERT_TRUE(PathService::Get(chrome::DIR_APP, &dir)); |
| 276 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); | 276 ASSERT_TRUE(MakeTempFile(dir, file_part, &full_path)); |
| 277 | 277 |
| 278 // make sure we pass through good URLs | 278 // make sure we pass through good URLs |
| 279 std::wstring fixedup; | 279 std::wstring fixedup; |
| 280 for (size_t i = 0; i < arraysize(fixup_cases); ++i) { | 280 for (int i = 0; i < arraysize(fixup_cases); ++i) { |
| 281 fixup_case value = fixup_cases[i]; | 281 fixup_case value = fixup_cases[i]; |
| 282 fixedup = URLFixerUpper::FixupRelativeFile(dir, value.input); | 282 fixedup = URLFixerUpper::FixupRelativeFile(dir, value.input); |
| 283 EXPECT_EQ(value.output, fixedup); | 283 EXPECT_EQ(value.output, fixedup); |
| 284 } | 284 } |
| 285 | 285 |
| 286 // make sure the existing file got fixed-up to a file URL, and that there | 286 // make sure the existing file got fixed-up to a file URL, and that there |
| 287 // are no backslashes | 287 // are no backslashes |
| 288 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); | 288 fixedup = URLFixerUpper::FixupRelativeFile(dir, file_part); |
| 289 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | 289 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); |
| 290 EXPECT_TRUE(file_util::Delete(full_path, false)); | 290 EXPECT_TRUE(DeleteFile(full_path.c_str())); |
| 291 | 291 |
| 292 // create a filename we know doesn't exist and make sure it doesn't get | 292 // create a filename we know doesn't exist and make sure it doesn't get |
| 293 // fixed up to a file URL | 293 // fixed up to a file URL |
| 294 std::wstring nonexistent_file(L"url_fixer_upper_nonexistent_file.txt"); | 294 std::wstring nonexistent_file(L"url_fixer_upper_nonexistent_file.txt"); |
| 295 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); | 295 fixedup = URLFixerUpper::FixupRelativeFile(dir, nonexistent_file); |
| 296 EXPECT_NE(std::wstring(L"file:///"), fixedup.substr(0, 8)); | 296 EXPECT_NE(std::wstring(L"file:///"), fixedup.substr(0, 8)); |
| 297 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); | 297 EXPECT_FALSE(IsMatchingFileURL(fixedup, nonexistent_file)); |
| 298 | 298 |
| 299 // make a subdir to make sure relative paths with directories work, also | 299 // make a subdir to make sure relative paths with directories work, also |
| 300 // test spaces: "app_dir\url fixer-upper dir\url fixer-upper existing file.txt
" | 300 // test spaces: "app_dir\url fixer-upper dir\url fixer-upper existing file.txt
" |
| 301 std::wstring sub_dir(L"url fixer-upper dir"); | 301 std::wstring sub_dir(L"url fixer-upper dir"); |
| 302 std::wstring sub_file(L"url fixer-upper existing file.txt"); | 302 std::wstring sub_file(L"url fixer-upper existing file.txt"); |
| 303 std::wstring new_dir = dir + L"\\" + sub_dir; | 303 std::wstring new_dir = dir + L"\\" + sub_dir; |
| 304 ASSERT_TRUE(file_util::CreateDirectory(new_dir)); | 304 CreateDirectory(new_dir.c_str(), NULL); |
| 305 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); | 305 ASSERT_TRUE(MakeTempFile(new_dir, sub_file, &full_path)); |
| 306 | 306 |
| 307 // test file in the subdir | 307 // test file in the subdir |
| 308 std::wstring relative_file = sub_dir + L"\\" + sub_file; | 308 std::wstring relative_file = sub_dir + L"\\" + sub_file; |
| 309 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | 309 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
| 310 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | 310 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); |
| 311 | 311 |
| 312 // test file in the subdir with different slashes and escaping | 312 // test file in the subdir with different slashes and escaping |
| 313 relative_file = sub_dir + L"/" + sub_file; | 313 relative_file = sub_dir + L"/" + sub_file; |
| 314 ReplaceSubstringsAfterOffset(&relative_file, 0, L" ", L"%20"); | 314 ReplaceSubstringsAfterOffset(&relative_file, 0, L" ", L"%20"); |
| 315 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | 315 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
| 316 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | 316 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); |
| 317 | 317 |
| 318 // test relative directories and duplicate slashes | 318 // test relative directories and duplicate slashes |
| 319 // (should resolve to the same file as above) | 319 // (should resolve to the same file as above) |
| 320 relative_file = sub_dir + L"\\../" + sub_dir + L"\\\\\\.\\" + sub_file; | 320 relative_file = sub_dir + L"\\../" + sub_dir + L"\\\\\\.\\" + sub_file; |
| 321 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); | 321 fixedup = URLFixerUpper::FixupRelativeFile(dir, relative_file); |
| 322 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); | 322 EXPECT_PRED2(IsMatchingFileURL, fixedup, full_path); |
| 323 | 323 |
| 324 // done with the subdir | 324 // done with the subdir |
| 325 EXPECT_TRUE(file_util::Delete(new_dir, true)); | 325 EXPECT_TRUE(DeleteFile(full_path.c_str())); |
| 326 EXPECT_TRUE(RemoveDirectory(new_dir.c_str())); |
| 326 } | 327 } |
| 327 | 328 |
| OLD | NEW |