• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "base/i18n/file_util_icu.h"
6 
7 #include "base/file_util.h"
8 #include "base/path_service.h"
9 #include "base/utf_string_conversions.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "testing/platform_test.h"
12 
13 // file_util winds up using autoreleased objects on the Mac, so this needs
14 // to be a PlatformTest
15 class FileUtilICUTest : public PlatformTest {
16  protected:
SetUp()17   virtual void SetUp() {
18     PlatformTest::SetUp();
19     // Name a subdirectory of the temp directory.
20     ASSERT_TRUE(PathService::Get(base::DIR_TEMP, &test_dir_));
21     test_dir_ = test_dir_.Append(FILE_PATH_LITERAL("FileUtilTest"));
22 
23     // Create a fresh, empty copy of this directory.
24     file_util::Delete(test_dir_, true);
25     file_util::CreateDirectory(test_dir_);
26   }
TearDown()27   virtual void TearDown() {
28     PlatformTest::TearDown();
29     // Clean up test directory
30     ASSERT_TRUE(file_util::Delete(test_dir_, true));
31     ASSERT_FALSE(file_util::PathExists(test_dir_));
32   }
33 
34   // the path to temporary directory used to contain the test operations
35   FilePath test_dir_;
36 };
37 
38 #if defined(OS_POSIX) && !defined(OS_MACOSX)
39 
40 // Linux disallows some evil ASCII characters, but passes all non-ASCII.
41 static const struct goodbad_pair {
42   const char* bad_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 
TEST_F(FileUtilICUTest,ReplaceIllegalCharacersInPathLinuxTest)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;
66 } kIllegalCharacterCases[] = {
67   {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
68   {L"**********::::.txt", L"--------------.txt"},
69   // We can't use UCNs (universal character names) for C0/C1 characters and
70   // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
71   {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
72 #if defined(OS_WIN)
73   {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
74   {L"\t  bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
75 #elif defined(OS_MACOSX)
76   {L"bad*file?name.jpg", L"bad-file-name.jpg"},
77   {L"\t  bad*file?name/.jpg ", L"bad-file-name-.jpg"},
78 #endif
79   {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
80   {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
81   {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
82   {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
83   // Unassigned codepoints are ok.
84   {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
85   // Non-characters are not allowed.
86   {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
87   {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"},
88 };
89 
TEST_F(FileUtilICUTest,ReplaceIllegalCharactersInPathTest)90 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
91   for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
92 #if defined(OS_WIN)
93     std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
94     file_util::ReplaceIllegalCharactersInPath(&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
101   }
102 }
103 
104 #endif
105 
106