OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/base/filename_util.h" | 5 #include "net/base/filename_util.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 int lineno; | 25 int lineno; |
26 const char* url; | 26 const char* url; |
27 const char* content_disp_header; | 27 const char* content_disp_header; |
28 const char* referrer_charset; | 28 const char* referrer_charset; |
29 const char* suggested_filename; | 29 const char* suggested_filename; |
30 const char* mime_type; | 30 const char* mime_type; |
31 const wchar_t* default_filename; | 31 const wchar_t* default_filename; |
32 const wchar_t* expected_filename; | 32 const wchar_t* expected_filename; |
33 }; | 33 }; |
34 | 34 |
| 35 // The expected filenames are coded as wchar_t for convenience. |
| 36 std::wstring FilePathAsWString(const base::FilePath& path) { |
| 37 #if defined(OS_WIN) |
| 38 return path.value(); |
| 39 #else |
| 40 return base::UTF8ToWide(path.value()); |
| 41 #endif |
| 42 } |
| 43 base::FilePath WStringAsFilePath(const std::wstring& str) { |
| 44 #if defined(OS_WIN) |
| 45 return base::FilePath(str); |
| 46 #else |
| 47 return base::FilePath(base::WideToUTF8(str)); |
| 48 #endif |
| 49 } |
| 50 |
35 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) { | 51 void RunGenerateFileNameTestCase(const GenerateFilenameCase* test_case) { |
36 std::string default_filename(base::WideToUTF8(test_case->default_filename)); | 52 std::string default_filename(base::WideToUTF8(test_case->default_filename)); |
37 base::FilePath file_path = GenerateFileName( | 53 base::FilePath file_path = GenerateFileName( |
38 GURL(test_case->url), test_case->content_disp_header, | 54 GURL(test_case->url), test_case->content_disp_header, |
39 test_case->referrer_charset, test_case->suggested_filename, | 55 test_case->referrer_charset, test_case->suggested_filename, |
40 test_case->mime_type, default_filename); | 56 test_case->mime_type, default_filename); |
41 EXPECT_EQ(test_case->expected_filename, | 57 EXPECT_EQ(test_case->expected_filename, FilePathAsWString(file_path)) |
42 file_util::FilePathAsWString(file_path)) | |
43 << "test case at line number: " << test_case->lineno; | 58 << "test case at line number: " << test_case->lineno; |
44 } | 59 } |
45 | 60 |
46 } // namespace | 61 } // namespace |
47 | 62 |
48 static const base::FilePath::CharType* kSafePortableBasenames[] = { | 63 static const base::FilePath::CharType* kSafePortableBasenames[] = { |
49 FILE_PATH_LITERAL("a"), | 64 FILE_PATH_LITERAL("a"), |
50 FILE_PATH_LITERAL("a.txt"), | 65 FILE_PATH_LITERAL("a.txt"), |
51 FILE_PATH_LITERAL("a b.txt"), | 66 FILE_PATH_LITERAL("a b.txt"), |
52 FILE_PATH_LITERAL("a-b.txt"), | 67 FILE_PATH_LITERAL("a-b.txt"), |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" | 179 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" |
165 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, | 180 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, |
166 #endif | 181 #endif |
167 }; | 182 }; |
168 | 183 |
169 // First, we'll test that we can round-trip all of the above cases of URLs | 184 // First, we'll test that we can round-trip all of the above cases of URLs |
170 base::FilePath output; | 185 base::FilePath output; |
171 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { | 186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { |
172 // convert to the file URL | 187 // convert to the file URL |
173 GURL file_url(FilePathToFileURL( | 188 GURL file_url(FilePathToFileURL( |
174 file_util::WStringAsFilePath(round_trip_cases[i].file))); | 189 WStringAsFilePath(round_trip_cases[i].file))); |
175 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); | 190 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); |
176 | 191 |
177 // Back to the filename. | 192 // Back to the filename. |
178 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); | 193 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); |
179 EXPECT_EQ(round_trip_cases[i].file, file_util::FilePathAsWString(output)); | 194 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); |
180 } | 195 } |
181 | 196 |
182 // Test that various file: URLs get decoded into the correct file type | 197 // Test that various file: URLs get decoded into the correct file type |
183 FileCase url_cases[] = { | 198 FileCase url_cases[] = { |
184 #if defined(OS_WIN) | 199 #if defined(OS_WIN) |
185 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"}, | 200 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"}, |
186 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"}, | 201 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"}, |
187 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, | 202 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, |
188 {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"}, | 203 {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"}, |
189 {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"}, | 204 {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"}, |
(...skipping 18 matching lines...) Expand all Loading... |
208 // slashes. | 223 // slashes. |
209 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 224 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
210 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, | 225 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, |
211 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 226 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
212 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, | 227 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, |
213 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 228 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
214 #endif | 229 #endif |
215 }; | 230 }; |
216 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { | 231 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { |
217 FileURLToFilePath(GURL(url_cases[i].url), &output); | 232 FileURLToFilePath(GURL(url_cases[i].url), &output); |
218 EXPECT_EQ(url_cases[i].file, file_util::FilePathAsWString(output)); | 233 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); |
219 } | 234 } |
220 | 235 |
221 // Unfortunately, UTF8ToWide discards invalid UTF8 input. | 236 // Unfortunately, UTF8ToWide discards invalid UTF8 input. |
222 #ifdef BUG_878908_IS_FIXED | 237 #ifdef BUG_878908_IS_FIXED |
223 // Test that no conversion happens if the UTF-8 input is invalid, and that | 238 // Test that no conversion happens if the UTF-8 input is invalid, and that |
224 // the input is preserved in UTF-8 | 239 // the input is preserved in UTF-8 |
225 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; | 240 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; |
226 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; | 241 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; |
227 EXPECT_TRUE(FileURLToFilePath( | 242 EXPECT_TRUE(FileURLToFilePath( |
228 GURL(std::string(invalid_utf8)), &output)); | 243 GURL(std::string(invalid_utf8)), &output)); |
(...skipping 1414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1643 RunGenerateFileNameTestCase(&generation_tests[i]); | 1658 RunGenerateFileNameTestCase(&generation_tests[i]); |
1644 | 1659 |
1645 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) { | 1660 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) { |
1646 GenerateFilenameCase test_case = generation_tests[i]; | 1661 GenerateFilenameCase test_case = generation_tests[i]; |
1647 test_case.referrer_charset = "GBK"; | 1662 test_case.referrer_charset = "GBK"; |
1648 RunGenerateFileNameTestCase(&test_case); | 1663 RunGenerateFileNameTestCase(&test_case); |
1649 } | 1664 } |
1650 } | 1665 } |
1651 | 1666 |
1652 } // namespace net | 1667 } // namespace net |
OLD | NEW |