OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "base/i18n/file_util_icu.h" | 5 #include "base/i18n/file_util_icu.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
11 | 12 |
12 // file_util winds up using autoreleased objects on the Mac, so this needs | 13 // file_util winds up using autoreleased objects on the Mac, so this needs |
13 // to be a PlatformTest | 14 // to be a PlatformTest |
14 class FileUtilICUTest : public PlatformTest { | 15 class FileUtilICUTest : public PlatformTest { |
15 protected: | 16 protected: |
16 virtual void SetUp() { | 17 virtual void SetUp() { |
17 PlatformTest::SetUp(); | 18 PlatformTest::SetUp(); |
18 // Name a subdirectory of the temp directory. | 19 // Name a subdirectory of the temp directory. |
19 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); | 20 ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_)); |
20 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest")); | 21 test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest")); |
21 | 22 |
22 // Create a fresh, empty copy of this directory. | 23 // Create a fresh, empty copy of this directory. |
23 file_util::Delete(test_dir_, true); | 24 file_util::Delete(test_dir_, true); |
24 file_util::CreateDirectory(test_dir_); | 25 file_util::CreateDirectory(test_dir_); |
25 } | 26 } |
26 virtual void TearDown() { | 27 virtual void TearDown() { |
27 PlatformTest::TearDown(); | 28 PlatformTest::TearDown(); |
28 // Clean up test directory | 29 // Clean up test directory |
29 ASSERT_TRUE(file_util::Delete(test_dir_, true)); | 30 ASSERT_TRUE(file_util::Delete(test_dir_, true)); |
30 ASSERT_FALSE(file_util::PathExists(test_dir_)); | 31 ASSERT_FALSE(file_util::PathExists(test_dir_)); |
31 } | 32 } |
32 | 33 |
33 // the path to temporary directory used to contain the test operations | 34 // the path to temporary directory used to contain the test operations |
34 FilePath test_dir_; | 35 FilePath test_dir_; |
35 }; | 36 }; |
36 | 37 |
| 38 #if defined(OS_LINUX) |
| 39 |
| 40 // Linux disallows some evil ASCII characters, but passes all non-ASCII. |
37 static const struct goodbad_pair { | 41 static const struct goodbad_pair { |
38 std::wstring bad_name; | 42 const char* bad_name; |
39 std::wstring good_name; | 43 const char* good_name; |
| 44 } kIllegalCharacterCases[] = { |
| 45 {"bad*file:name?.jpg", "bad-file-name-.jpg"}, |
| 46 {"**********::::.txt", "--------------.txt"}, |
| 47 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"}, |
| 48 }; |
| 49 |
| 50 TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) { |
| 51 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
| 52 std::string bad_name(kIllegalCharacterCases[i].bad_name); |
| 53 file_util::ReplaceIllegalCharactersInPath(&bad_name, '-'); |
| 54 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
| 55 } |
| 56 } |
| 57 |
| 58 #else |
| 59 |
| 60 // For Mac & Windows, which both do Unicode validation on filenames. These |
| 61 // characters are given as wide strings since its more convenient to specify |
| 62 // unicode characters. For Mac they should be converted to UTF-8. |
| 63 static const struct goodbad_pair { |
| 64 const wchar_t* bad_name; |
| 65 const wchar_t* good_name; |
40 } kIllegalCharacterCases[] = { | 66 } kIllegalCharacterCases[] = { |
41 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, | 67 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, |
42 {L"**********::::.txt", L"--------------.txt"}, | 68 {L"**********::::.txt", L"--------------.txt"}, |
43 // We can't use UCNs (universal character names) for C0/C1 characters and | 69 // We can't use UCNs (universal character names) for C0/C1 characters and |
44 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. | 70 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. |
45 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, | 71 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, |
46 #if defined(OS_WIN) | 72 #if defined(OS_WIN) |
47 {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, | 73 {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, |
48 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, | 74 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, |
49 #elif defined(OS_POSIX) | 75 #elif defined(OS_MACOSX) |
50 {L"bad*file?name.jpg", L"bad-file-name.jpg"}, | 76 {L"bad*file?name.jpg", L"bad-file-name.jpg"}, |
51 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"}, | 77 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"}, |
52 #endif | 78 #endif |
53 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, | 79 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, |
54 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, | 80 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, |
55 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, | 81 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, |
56 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, | 82 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, |
57 // Unassigned codepoints are ok. | 83 // Unassigned codepoints are ok. |
58 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, | 84 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, |
59 // Non-characters are not allowed. | 85 // Non-characters are not allowed. |
60 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, | 86 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, |
61 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"}, | 87 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"}, |
62 }; | 88 }; |
63 | 89 |
64 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersTest) { | 90 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) { |
65 for (unsigned int i = 0; i < arraysize(kIllegalCharacterCases); ++i) { | 91 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
| 92 #if defined(OS_WIN) |
66 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); | 93 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); |
67 file_util::ReplaceIllegalCharacters(&bad_name, L'-'); | 94 file_util::ReplaceIllegalCharactersInPath(&bad_name, '-'); |
68 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); | 95 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
| 96 #elif defined(OS_MACOSX) |
| 97 std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name)); |
| 98 file_util::ReplaceIllegalCharactersInPath(&bad_name, '-'); |
| 99 EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name); |
| 100 #endif |
69 } | 101 } |
70 } | 102 } |
71 | 103 |
| 104 #endif |
| 105 |
OLD | NEW |