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
3 // found in the LICENSE file.
5 #include "base/i18n/file_util_icu.h"
7 #include "base/files/file_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
15 // file_util winds up using autoreleased objects on the Mac, so this needs
16 // to be a PlatformTest
17 class FileUtilICUTest
: public PlatformTest
{
20 #if defined(OS_POSIX) && !defined(OS_MACOSX)
22 // Linux disallows some evil ASCII characters, but passes all non-ASCII.
23 static const struct goodbad_pair
{
25 const char* good_name
;
26 } kIllegalCharacterCases
[] = {
27 {"bad*file:name?.jpg", "bad-file-name-.jpg"},
28 {"**********::::.txt", "--------------.txt"},
29 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"},
32 TEST_F(FileUtilICUTest
, ReplaceIllegalCharacersInPathLinuxTest
) {
33 for (size_t i
= 0; i
< arraysize(kIllegalCharacterCases
); ++i
) {
34 std::string
bad_name(kIllegalCharacterCases
[i
].bad_name
);
35 ReplaceIllegalCharactersInPath(&bad_name
, '-');
36 EXPECT_EQ(kIllegalCharacterCases
[i
].good_name
, bad_name
);
42 // For Mac & Windows, which both do Unicode validation on filenames. These
43 // characters are given as wide strings since its more convenient to specify
44 // unicode characters. For Mac they should be converted to UTF-8.
45 static const struct goodbad_pair
{
46 const wchar_t* bad_name
;
47 const wchar_t* good_name
;
48 } kIllegalCharacterCases
[] = {
49 {L
"bad*file:name?.jpg", L
"bad-file-name-.jpg"},
50 {L
"**********::::.txt", L
"--------------.txt"},
51 // 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.
53 {L
"bad\x0003\x0091 file\u200E\u200Fname.png", L
"bad-- file--name.png"},
55 {L
"bad*file\\name.jpg", L
"bad-file-name.jpg"},
56 {L
"\t bad*file\\name/.jpg ", L
"bad-file-name-.jpg"},
57 #elif defined(OS_MACOSX)
58 {L
"bad*file?name.jpg", L
"bad-file-name.jpg"},
59 {L
"\t bad*file?name/.jpg ", L
"bad-file-name-.jpg"},
61 {L
"this_file_name is okay!.mp3", L
"this_file_name is okay!.mp3"},
62 {L
"\u4E00\uAC00.mp3", L
"\u4E00\uAC00.mp3"},
63 {L
"\u0635\u200C\u0644.mp3", L
"\u0635\u200C\u0644.mp3"},
64 {L
"\U00010330\U00010331.mp3", L
"\U00010330\U00010331.mp3"},
65 // Unassigned codepoints are ok.
66 {L
"\u0378\U00040001.mp3", L
"\u0378\U00040001.mp3"},
67 // Non-characters are not allowed.
68 {L
"bad\uFFFFfile\U0010FFFEname.jpg ", L
"bad-file-name.jpg"},
69 {L
"bad\uFDD0file\uFDEFname.jpg ", L
"bad-file-name.jpg"},
72 TEST_F(FileUtilICUTest
, ReplaceIllegalCharactersInPathTest
) {
73 for (size_t i
= 0; i
< arraysize(kIllegalCharacterCases
); ++i
) {
75 std::wstring
bad_name(kIllegalCharacterCases
[i
].bad_name
);
76 ReplaceIllegalCharactersInPath(&bad_name
, '-');
77 EXPECT_EQ(kIllegalCharacterCases
[i
].good_name
, bad_name
);
78 #elif defined(OS_MACOSX)
79 std::string
bad_name(WideToUTF8(kIllegalCharacterCases
[i
].bad_name
));
80 ReplaceIllegalCharactersInPath(&bad_name
, '-');
81 EXPECT_EQ(WideToUTF8(kIllegalCharacterCases
[i
].good_name
), bad_name
);
88 #if defined(OS_CHROMEOS)
89 static const struct normalize_name_encoding_test_cases
{
90 const char* original_path
;
91 const char* normalized_path
;
92 } kNormalizeFileNameEncodingTestCases
[] = {
93 { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"},
94 { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo",
95 "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"},
97 { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"}
100 TEST_F(FileUtilICUTest
, NormalizeFileNameEncoding
) {
101 for (size_t i
= 0; i
< arraysize(kNormalizeFileNameEncodingTestCases
); i
++) {
102 FilePath
path(kNormalizeFileNameEncodingTestCases
[i
].original_path
);
103 NormalizeFileNameEncoding(&path
);
104 EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases
[i
].normalized_path
),