1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/i18n/file_util_icu.h"
9 #include "base/files/file_util.h"
10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "build/build_config.h"
13 #include "build/chromeos_buildflags.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "testing/platform_test.h"
20 // file_util winds up using autoreleased objects on the Mac, so this needs
21 // to be a PlatformTest
22 class FileUtilICUTest : public PlatformTest {
25 #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_APPLE)
27 // On linux, file path is parsed and filtered as UTF-8.
28 static const struct GoodBadPairLinux {
30 const char* good_name;
31 } kLinuxIllegalCharacterCases[] = {
32 {"bad*\\/file:name?.jpg", "bad---file-name-.jpg"},
33 {"**********::::.txt", "--------------.txt"},
34 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"},
37 {" .( ). ", "-.( ).-"},
41 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathLinuxTest) {
42 for (auto i : kLinuxIllegalCharacterCases) {
43 std::string bad_name(i.bad_name);
44 ReplaceIllegalCharactersInPath(&bad_name, '-');
45 EXPECT_EQ(i.good_name, bad_name);
51 // For Mac & Windows, which both do Unicode validation on filenames. These
52 // characters are given as UTF-16 strings since its more convenient to specify
53 // unicode characters. For Mac they should be converted to UTF-8, for Windows to
55 static const struct FileUtilICUTestCases {
56 const char16_t* bad_name;
57 const char16_t* good_name_with_dash;
58 const char16_t* good_name_with_space;
59 } kIllegalCharacterCases[] = {
60 {u"bad*file:name?.jpg", u"bad-file-name-.jpg", u"bad file name .jpg"},
61 {u"**********::::.txt", u"--------------.txt", u"_.txt"},
62 // We can't use UCNs (universal character names) for C0/C1 characters and
63 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
64 {u"bad\x0003\x0091 file\u200E\u200Fname.png", u"bad-- file--name.png",
65 u"bad file name.png"},
66 {u"bad*file\\?name.jpg", u"bad-file--name.jpg", u"bad file name.jpg"},
67 {u"\t bad*file\\name/.jpg", u"- bad-file-name-.jpg",
68 u"bad file name .jpg"},
69 {u"this_file_name is okay!.mp3", u"this_file_name is okay!.mp3",
70 u"this_file_name is okay!.mp3"},
71 {u"\u4E00\uAC00.mp3", u"\u4E00\uAC00.mp3", u"\u4E00\uAC00.mp3"},
72 {u"\u0635\u200C\u0644.mp3", u"\u0635-\u0644.mp3", u"\u0635 \u0644.mp3"},
73 {u"\U00010330\U00010331.mp3", u"\U00010330\U00010331.mp3",
74 u"\U00010330\U00010331.mp3"},
75 // Unassigned codepoints are ok.
76 {u"\u0378\U00040001.mp3", u"\u0378\U00040001.mp3", u"\u0378\U00040001.mp3"},
77 // Non-characters are not allowed.
78 {u"bad\uFFFFfile\U0010FFFEname.jpg", u"bad-file-name.jpg",
79 u"bad file name.jpg"},
80 {u"bad\uFDD0file\uFDEFname.jpg", u"bad-file-name.jpg",
81 u"bad file name.jpg"},
83 {u"(\u200C.\u200D.\u200E.\u200F.\u202A.\u202B.\u202C.\u202D.\u202E.\u206A."
84 u"\u206B.\u206C.\u206D.\u206F.\uFEFF)",
85 u"(-.-.-.-.-.-.-.-.-.-.-.-.-.-.-)", u"( . . . . . . . . . . . . . . )"},
86 {u" _ ", u"-_-", u"_"},
88 {u"\u2008.(\u2007).\u3000", u"-.(\u2007).-", u"(\u2007)"},
89 {u" ", u"- -", u"_ _"},
90 {u". ", u"- -", u"_. _"},
92 // '~' is only invalid on Windows, and only if the file name could possibly
93 // be an 8.3 short name.
94 {u"config~1", u"config-1", u"config 1"},
95 {u"config~1.txt", u"config-1.txt", u"config 1.txt"},
97 {u"config~1", u"config~1", u"config~1"},
98 {u"config~1.txt", u"config~1.txt", u"config~1.txt"},
100 // Tildes are always illegal at ends.
101 {u"~config1.txt", u"-config1.txt", u"config1.txt"},
102 {u"config1.txt~", u"config1.txt-", u"config1.txt"},
103 // Some characters, such as spaces, are not allowed in 8.3 short names.
104 // Don't replace the '~' if these characters are present.
105 {u"conf g~1", u"conf g~1", u"conf g~1"},
106 {u"conf,g~1.txt", u"conf,g~1.txt", u"conf,g~1.txt"},
107 // File names with periods in invalid positions are not legal 8.3 names.
108 {u"conf~1.jpeg", u"conf~1.jpeg", u"conf~1.jpeg"},
109 {u"config~12.md", u"config~12.md", u"config~12.md"},
110 // Short names without a '~' character are allowed.
111 {u"config.txt", u"config.txt", u"config.txt"},
112 // Names long enough to not be short names are allowed.
113 {u"config~12.txt", u"config~12.txt", u"config~12.txt"},
114 {u"config~1VeryLongCannotBeShortNameOK.txt",
115 u"config~1VeryLongCannotBeShortNameOK.txt",
116 u"config~1VeryLongCannotBeShortNameOK.txt"},
117 // Base name is longer than 8 characters, without a dot.
118 {u"config~1txt", u"config~1txt", u"config~1txt"},
120 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_APPLE) || BUILDFLAG(IS_POSIX)
122 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
123 for (auto i : kIllegalCharacterCases) {
124 #if BUILDFLAG(IS_WIN)
125 std::wstring bad_name = UTF16ToWide(i.bad_name);
126 ReplaceIllegalCharactersInPath(&bad_name, '-');
127 EXPECT_EQ(UTF16ToWide(i.good_name_with_dash), bad_name);
129 std::string bad_name = UTF16ToUTF8(i.bad_name);
130 ReplaceIllegalCharactersInPath(&bad_name, '-');
131 EXPECT_EQ(UTF16ToUTF8(i.good_name_with_dash), bad_name);
136 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathWithIllegalEndCharTest) {
137 for (auto i : kIllegalCharacterCases) {
138 #if BUILDFLAG(IS_WIN)
139 std::wstring bad_name = UTF16ToWide(i.bad_name);
140 ReplaceIllegalCharactersInPath(&bad_name, ' ');
141 EXPECT_EQ(UTF16ToWide(i.good_name_with_space), bad_name);
143 std::string bad_name(UTF16ToUTF8(i.bad_name));
144 ReplaceIllegalCharactersInPath(&bad_name, ' ');
145 EXPECT_EQ(UTF16ToUTF8(i.good_name_with_space), bad_name);
152 TEST_F(FileUtilICUTest, IsFilenameLegalTest) {
153 EXPECT_TRUE(IsFilenameLegal(std::u16string()));
155 for (const auto& test_case : kIllegalCharacterCases) {
156 std::u16string bad_name = test_case.bad_name;
157 std::u16string good_name = test_case.good_name_with_dash;
159 EXPECT_TRUE(IsFilenameLegal(good_name)) << good_name;
160 if (good_name != bad_name)
161 EXPECT_FALSE(IsFilenameLegal(bad_name)) << bad_name;
165 #if BUILDFLAG(IS_CHROMEOS_ASH)
166 static const struct normalize_name_encoding_test_cases {
167 const char* original_path;
168 const char* normalized_path;
169 } kNormalizeFileNameEncodingTestCases[] = {
170 { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"},
171 { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo",
172 "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"},
174 { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"}
177 TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) {
178 for (size_t i = 0; i < std::size(kNormalizeFileNameEncodingTestCases); i++) {
179 FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path);
180 NormalizeFileNameEncoding(&path);
181 EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path),