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/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.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 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", | 176 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", |
177 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" | 177 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" |
178 "%91%E9%A1%B5.doc"}, | 178 "%91%E9%A1%B5.doc"}, |
179 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" | 179 {L"/plane1/\x1D400\x1D401.txt", // Math alphabet "AB" |
180 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, | 180 "file:///plane1/%F0%9D%90%80%F0%9D%90%81.txt"}, |
181 #endif | 181 #endif |
182 }; | 182 }; |
183 | 183 |
184 // 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 |
185 base::FilePath output; | 185 base::FilePath output; |
186 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { | 186 for (size_t i = 0; i < arraysize(round_trip_cases); i++) { |
187 // convert to the file URL | 187 // convert to the file URL |
188 GURL file_url(FilePathToFileURL( | 188 GURL file_url(FilePathToFileURL( |
189 WStringAsFilePath(round_trip_cases[i].file))); | 189 WStringAsFilePath(round_trip_cases[i].file))); |
190 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); | 190 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); |
191 | 191 |
192 // Back to the filename. | 192 // Back to the filename. |
193 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); | 193 EXPECT_TRUE(FileURLToFilePath(file_url, &output)); |
194 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); | 194 EXPECT_EQ(round_trip_cases[i].file, FilePathAsWString(output)); |
195 } | 195 } |
196 | 196 |
(...skipping 24 matching lines...) Expand all Loading... |
221 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, | 221 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, |
222 // We get these wrong because GURL turns back slashes into forward | 222 // We get these wrong because GURL turns back slashes into forward |
223 // slashes. | 223 // slashes. |
224 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 224 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
225 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, | 225 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, |
226 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 226 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
227 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, | 227 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, |
228 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, | 228 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
229 #endif | 229 #endif |
230 }; | 230 }; |
231 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { | 231 for (size_t i = 0; i < arraysize(url_cases); i++) { |
232 FileURLToFilePath(GURL(url_cases[i].url), &output); | 232 FileURLToFilePath(GURL(url_cases[i].url), &output); |
233 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); | 233 EXPECT_EQ(url_cases[i].file, FilePathAsWString(output)); |
234 } | 234 } |
235 | 235 |
236 // Unfortunately, UTF8ToWide discards invalid UTF8 input. | 236 // Unfortunately, UTF8ToWide discards invalid UTF8 input. |
237 #ifdef BUG_878908_IS_FIXED | 237 #ifdef BUG_878908_IS_FIXED |
238 // 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 |
239 // the input is preserved in UTF-8 | 239 // the input is preserved in UTF-8 |
240 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; | 240 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; |
241 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; | 241 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 FILE_PATH_LITERAL("/bar.jpg") | 402 FILE_PATH_LITERAL("/bar.jpg") |
403 }, | 403 }, |
404 { | 404 { |
405 "image/jpeg", | 405 "image/jpeg", |
406 FILE_PATH_LITERAL("/bar.jpeg"), | 406 FILE_PATH_LITERAL("/bar.jpeg"), |
407 FILE_PATH_LITERAL("/bar.jpeg") | 407 FILE_PATH_LITERAL("/bar.jpeg") |
408 }, | 408 }, |
409 #endif // !defined(OS_WIN) | 409 #endif // !defined(OS_WIN) |
410 }; | 410 }; |
411 | 411 |
412 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(safe_tests); ++i) { | 412 for (size_t i = 0; i < arraysize(safe_tests); ++i) { |
413 base::FilePath file_path(safe_tests[i].filename); | 413 base::FilePath file_path(safe_tests[i].filename); |
414 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); | 414 GenerateSafeFileName(safe_tests[i].mime_type, false, &file_path); |
415 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) | 415 EXPECT_EQ(safe_tests[i].expected_filename, file_path.value()) |
416 << "Iteration " << i; | 416 << "Iteration " << i; |
417 } | 417 } |
418 } | 418 } |
419 | 419 |
420 TEST(FilenameUtilTest, GenerateFileName) { | 420 TEST(FilenameUtilTest, GenerateFileName) { |
421 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 421 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) |
422 // This test doesn't run when the locale is not UTF-8 because some of the | 422 // This test doesn't run when the locale is not UTF-8 because some of the |
(...skipping 1221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1644 "", | 1644 "", |
1645 "", | 1645 "", |
1646 "", | 1646 "", |
1647 "image/jpeg", | 1647 "image/jpeg", |
1648 L"foo\xe4", | 1648 L"foo\xe4", |
1649 L"foo\xe4.txt" | 1649 L"foo\xe4.txt" |
1650 }, | 1650 }, |
1651 #endif | 1651 #endif |
1652 }; | 1652 }; |
1653 | 1653 |
1654 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(selection_tests); ++i) | 1654 for (size_t i = 0; i < arraysize(selection_tests); ++i) |
1655 RunGenerateFileNameTestCase(&selection_tests[i]); | 1655 RunGenerateFileNameTestCase(&selection_tests[i]); |
1656 | 1656 |
1657 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) | 1657 for (size_t i = 0; i < arraysize(generation_tests); ++i) |
1658 RunGenerateFileNameTestCase(&generation_tests[i]); | 1658 RunGenerateFileNameTestCase(&generation_tests[i]); |
1659 | 1659 |
1660 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(generation_tests); ++i) { | 1660 for (size_t i = 0; i < arraysize(generation_tests); ++i) { |
1661 GenerateFilenameCase test_case = generation_tests[i]; | 1661 GenerateFilenameCase test_case = generation_tests[i]; |
1662 test_case.referrer_charset = "GBK"; | 1662 test_case.referrer_charset = "GBK"; |
1663 RunGenerateFileNameTestCase(&test_case); | 1663 RunGenerateFileNameTestCase(&test_case); |
1664 } | 1664 } |
1665 } | 1665 } |
1666 | 1666 |
1667 } // namespace net | 1667 } // namespace net |
OLD | NEW |