Roll src/third_party/WebKit 6d85854:7e30d51 (svn 202247:202248)
[chromium-blink-merge.git] / base / i18n / file_util_icu_unittest.cc
blob8fa7f6a261dc460f2165034911bf06a9e2e95872
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"
12 namespace base {
13 namespace i18n {
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 GoodBadPairLinux {
24 const char* bad_name;
25 const char* good_name;
26 } kLinuxIllegalCharacterCases[] = {
27 {"bad*\\/file:name?.jpg", "bad---file-name-.jpg"},
28 {"**********::::.txt", "--------------.txt"},
29 {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"},
30 {" _ ", "-_-"},
31 {".", "-"},
32 {" .( ). ", "-.( ).-"},
33 {" ", "- -"},
36 TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) {
37 for (size_t i = 0; i < arraysize(kLinuxIllegalCharacterCases); ++i) {
38 std::string bad_name(kLinuxIllegalCharacterCases[i].bad_name);
39 ReplaceIllegalCharactersInPath(&bad_name, '-');
40 EXPECT_EQ(kLinuxIllegalCharacterCases[i].good_name, bad_name);
44 #endif
46 // For Mac & Windows, which both do Unicode validation on filenames. These
47 // characters are given as wide strings since its more convenient to specify
48 // unicode characters. For Mac they should be converted to UTF-8.
49 static const struct goodbad_pair {
50 const wchar_t* bad_name;
51 const wchar_t* good_name;
52 } kIllegalCharacterCases[] = {
53 {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
54 {L"**********::::.txt", L"--------------.txt"},
55 // We can't use UCNs (universal character names) for C0/C1 characters and
56 // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
57 {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
58 {L"bad*file\\?name.jpg", L"bad-file--name.jpg"},
59 {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"},
61 {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
62 {L"\u0635\u200C\u0644.mp3", L"\u0635-\u0644.mp3"},
63 {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
64 // Unassigned codepoints are ok.
65 {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
66 // Non-characters are not allowed.
67 {L"bad\uFFFFfile\U0010FFFEname.jpg", L"bad-file-name.jpg"},
68 {L"bad\uFDD0file\uFDEFname.jpg", L"bad-file-name.jpg"},
69 // CVE-2014-9390
70 {L"(\u200C.\u200D.\u200E.\u200F.\u202A.\u202B.\u202C.\u202D.\u202E.\u206A."
71 L"\u206B.\u206C.\u206D.\u206F.\uFEFF)",
72 L"(-.-.-.-.-.-.-.-.-.-.-.-.-.-.-)"},
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"- -"}
81 #if defined(OS_WIN) || defined(OS_MACOSX)
83 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
84 for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
85 #if defined(OS_WIN)
86 std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
87 ReplaceIllegalCharactersInPath(&bad_name, '-');
88 EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
89 #elif defined(OS_MACOSX)
90 std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name));
91 ReplaceIllegalCharactersInPath(&bad_name, '-');
92 EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name);
93 #endif
97 #endif
99 TEST_F(FileUtilICUTest, IsFilenameLegalTest) {
100 EXPECT_TRUE(IsFilenameLegal(string16()));
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);
106 EXPECT_TRUE(IsFilenameLegal(good_name)) << good_name;
107 if (good_name != bad_name)
108 EXPECT_FALSE(IsFilenameLegal(bad_name)) << bad_name;
112 #if defined(OS_CHROMEOS)
113 static const struct normalize_name_encoding_test_cases {
114 const char* original_path;
115 const char* normalized_path;
116 } kNormalizeFileNameEncodingTestCases[] = {
117 { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"},
118 { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo",
119 "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"},
120 { "", ""},
121 { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"}
124 TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) {
125 for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) {
126 FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path);
127 NormalizeFileNameEncoding(&path);
128 EXPECT_EQ(FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path),
129 path);
133 #endif
135 } // namespace i18n
136 } // namespace base