OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 namespace i18n { | 13 namespace i18n { |
14 | 14 |
15 // file_util winds up using autoreleased objects on the Mac, so this needs | 15 // file_util winds up using autoreleased objects on the Mac, so this needs |
16 // to be a PlatformTest | 16 // to be a PlatformTest |
17 class FileUtilICUTest : public PlatformTest { | 17 class FileUtilICUTest : public PlatformTest { |
18 }; | 18 }; |
19 | 19 |
20 #if defined(OS_POSIX) && !defined(OS_MACOSX) | 20 #if defined(OS_POSIX) && !defined(OS_MACOSX) |
21 | 21 |
22 // Linux disallows some evil ASCII characters, but passes all non-ASCII. | 22 // Linux disallows some evil ASCII characters, but passes all non-ASCII. |
23 static const struct goodbad_pair { | 23 static const struct GoodBadPairLinux { |
24 const char* bad_name; | 24 const char* bad_name; |
25 const char* good_name; | 25 const char* good_name; |
26 } kIllegalCharacterCases[] = { | 26 } kLinuxIllegalCharacterCases[] = { |
27 {"bad*file:name?.jpg", "bad-file-name-.jpg"}, | 27 {"bad*\\/file:name?.jpg", "bad---file-name-.jpg"}, |
28 {"**********::::.txt", "--------------.txt"}, | 28 {"**********::::.txt", "--------------.txt"}, |
29 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"}, | 29 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"}, |
| 30 {" _ ", "-_-"}, |
| 31 {".", "-"}, |
| 32 {" .( ). ", "-.( ).-"}, |
| 33 {" ", "- -"}, |
30 }; | 34 }; |
31 | 35 |
32 TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) { | 36 TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) { |
33 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) { | 37 for (size_t i = 0; i < arraysize(kLinuxIllegalCharacterCases); ++i) { |
34 std::string bad_name(kIllegalCharacterCases[i].bad_name); | 38 std::string bad_name(kLinuxIllegalCharacterCases[i].bad_name); |
35 ReplaceIllegalCharactersInPath(&bad_name, '-'); | 39 ReplaceIllegalCharactersInPath(&bad_name, '-'); |
36 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); | 40 EXPECT_EQ(kLinuxIllegalCharacterCases[i].good_name, bad_name); |
37 } | 41 } |
38 } | 42 } |
39 | 43 |
40 #else | 44 #endif |
41 | 45 |
42 // For Mac & Windows, which both do Unicode validation on filenames. These | 46 // For Mac & Windows, which both do Unicode validation on filenames. These |
43 // characters are given as wide strings since its more convenient to specify | 47 // characters are given as wide strings since its more convenient to specify |
44 // unicode characters. For Mac they should be converted to UTF-8. | 48 // unicode characters. For Mac they should be converted to UTF-8. |
45 static const struct goodbad_pair { | 49 static const struct goodbad_pair { |
46 const wchar_t* bad_name; | 50 const wchar_t* bad_name; |
47 const wchar_t* good_name; | 51 const wchar_t* good_name; |
48 } kIllegalCharacterCases[] = { | 52 } kIllegalCharacterCases[] = { |
49 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, | 53 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"}, |
50 {L"**********::::.txt", L"--------------.txt"}, | 54 {L"**********::::.txt", L"--------------.txt"}, |
51 // We can't use UCNs (universal character names) for C0/C1 characters and | 55 // We can't use UCNs (universal character names) for C0/C1 characters and |
52 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. | 56 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend. |
53 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, | 57 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"}, |
54 #if defined(OS_WIN) | 58 {L"bad*file\\?name.jpg", L"bad-file--name.jpg"}, |
55 {L"bad*file\\name.jpg", L"bad-file-name.jpg"}, | 59 {L"\t bad*file\\name/.jpg", L"- bad-file-name-.jpg"}, |
56 {L"\t bad*file\\name/.jpg ", L"bad-file-name-.jpg"}, | 60 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, |
57 #elif defined(OS_MACOSX) | 61 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, |
58 {L"bad*file?name.jpg", L"bad-file-name.jpg"}, | 62 {L"\u0635\u200C\u0644.mp3", L"\u0635-\u0644.mp3"}, |
59 {L"\t bad*file?name/.jpg ", L"bad-file-name-.jpg"}, | 63 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, |
60 #endif | 64 // Unassigned codepoints are ok. |
61 {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"}, | 65 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, |
62 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"}, | 66 // Non-characters are not allowed. |
63 {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"}, | 67 {L"bad\uFFFFfile\U0010FFFEname.jpg", L"bad-file-name.jpg"}, |
64 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"}, | 68 {L"bad\uFDD0file\uFDEFname.jpg", L"bad-file-name.jpg"}, |
65 // Unassigned codepoints are ok. | 69 // CVE-2014-9390 |
66 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"}, | 70 {L"(\u200C.\u200D.\u200E.\u200F.\u202A.\u202B.\u202C.\u202D.\u202E.\u206A." |
67 // Non-characters are not allowed. | 71 L"\u206B.\u206C.\u206D.\u206F.\uFEFF)", |
68 {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"}, | 72 L"(-.-.-.-.-.-.-.-.-.-.-.-.-.-.-)"}, |
69 {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"}, | 73 {L"config~1", L"config-1"}, |
| 74 {L" _ ", L"-_-"}, |
| 75 {L" ", L"-"}, |
| 76 {L"\u2008.(\u2007).\u3000", L"-.(\u2007).-"}, |
| 77 {L" ", L"- -"}, |
| 78 {L". ", L"- -"} |
70 }; | 79 }; |
71 | 80 |
| 81 #if defined(OS_WIN) || defined(OS_MACOSX) |
| 82 |
72 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) { | 83 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) { |
73 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) { | 84 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) { |
74 #if defined(OS_WIN) | 85 #if defined(OS_WIN) |
75 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); | 86 std::wstring bad_name(kIllegalCharacterCases[i].bad_name); |
76 ReplaceIllegalCharactersInPath(&bad_name, '-'); | 87 ReplaceIllegalCharactersInPath(&bad_name, '-'); |
77 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); | 88 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name); |
78 #elif defined(OS_MACOSX) | 89 #elif defined(OS_MACOSX) |
79 std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name)); | 90 std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name)); |
80 ReplaceIllegalCharactersInPath(&bad_name, '-'); | 91 ReplaceIllegalCharactersInPath(&bad_name, '-'); |
81 EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name); | 92 EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name); |
82 #endif | 93 #endif |
83 } | 94 } |
84 } | 95 } |
85 | 96 |
86 #endif | 97 #endif |
87 | 98 |
| 99 TEST_F(FileUtilICUTest, IsFilenameLegalTest) { |
| 100 EXPECT_TRUE(IsFilenameLegal(string16())); |
| 101 |
| 102 for (const auto& test_case : kIllegalCharacterCases) { |
| 103 string16 bad_name = WideToUTF16(test_case.bad_name); |
| 104 string16 good_name = WideToUTF16(test_case.good_name); |
| 105 |
| 106 EXPECT_TRUE(IsFilenameLegal(good_name)) << good_name; |
| 107 if (good_name != bad_name) |
| 108 EXPECT_FALSE(IsFilenameLegal(bad_name)) << bad_name; |
| 109 } |
| 110 } |
| 111 |
88 #if defined(OS_CHROMEOS) | 112 #if defined(OS_CHROMEOS) |
89 static const struct normalize_name_encoding_test_cases { | 113 static const struct normalize_name_encoding_test_cases { |
90 const char* original_path; | 114 const char* original_path; |
91 const char* normalized_path; | 115 const char* normalized_path; |
92 } kNormalizeFileNameEncodingTestCases[] = { | 116 } kNormalizeFileNameEncodingTestCases[] = { |
93 { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"}, | 117 { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"}, |
94 { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo", | 118 { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo", |
95 "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"}, | 119 "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"}, |
96 { "", ""}, | 120 { "", ""}, |
97 { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"} | 121 { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"} |
98 }; | 122 }; |
99 | 123 |
100 TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) { | 124 TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) { |
101 for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) { | 125 for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) { |
102 FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path); | 126 FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path); |
103 NormalizeFileNameEncoding(&path); | 127 NormalizeFileNameEncoding(&path); |
104 EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path), | 128 EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path), |
105 path); | 129 path); |
106 } | 130 } |
107 } | 131 } |
108 | 132 |
109 #endif | 133 #endif |
110 | 134 |
111 } // namespace i18n | 135 } // namespace i18n |
112 } // namespace base | 136 } // namespace base |
OLD | NEW |