Home | History | Annotate | Download | only in i18n
      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.
      4 
      5 #include "base/i18n/file_util_icu.h"
      6 
      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"
     11 
     12 namespace base {
     13 namespace i18n {
     14 
     15 // file_util winds up using autoreleased objects on the Mac, so this needs
     16 // to be a PlatformTest
     17 class FileUtilICUTest : public PlatformTest {
     18 };
     19 
     20 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     21 
     22 // Linux disallows some evil ASCII characters, but passes all non-ASCII.
     23 static const struct goodbad_pair {
     24   const char* bad_name;
     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"},
     30 };
     31 
     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);
     37   }
     38 }
     39 
     40 #else
     41 
     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"},
     54 #if defined(OS_WIN)
     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"},
     60 #endif
     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"},
     70 };
     71 
     72 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
     73   for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
     74 #if defined(OS_WIN)
     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);
     82 #endif
     83   }
     84 }
     85 
     86 #endif
     87 
     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"},
     96   { "", ""},
     97   { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"}
     98 };
     99 
    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),
    105               path);
    106   }
    107 }
    108 
    109 #endif
    110 
    111 }  // namespace i18n
    112 }  // namespace base
    113