Home | History | Annotate | Download | only in i18n
      1 // Copyright (c) 2011 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/utf_string_conversions.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 #include "testing/platform_test.h"
     11 
     12 // file_util winds up using autoreleased objects on the Mac, so this needs
     13 // to be a PlatformTest
     14 class FileUtilICUTest : public PlatformTest {
     15 };
     16 
     17 #if defined(OS_POSIX) && !defined(OS_MACOSX)
     18 
     19 // Linux disallows some evil ASCII characters, but passes all non-ASCII.
     20 static const struct goodbad_pair {
     21   const char* bad_name;
     22   const char* good_name;
     23 } kIllegalCharacterCases[] = {
     24   {"bad*file:name?.jpg", "bad-file-name-.jpg"},
     25   {"**********::::.txt", "--------------.txt"},
     26   {"\xe9\xf0zzzz.\xff", "\xe9\xf0zzzz.\xff"},
     27 };
     28 
     29 TEST_F(FileUtilICUTest, ReplaceIllegalCharacersInPathLinuxTest) {
     30   for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
     31     std::string bad_name(kIllegalCharacterCases[i].bad_name);
     32     file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
     33     EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
     34   }
     35 }
     36 
     37 #else
     38 
     39 // For Mac & Windows, which both do Unicode validation on filenames. These
     40 // characters are given as wide strings since its more convenient to specify
     41 // unicode characters. For Mac they should be converted to UTF-8.
     42 static const struct goodbad_pair {
     43   const wchar_t* bad_name;
     44   const wchar_t* good_name;
     45 } kIllegalCharacterCases[] = {
     46   {L"bad*file:name?.jpg", L"bad-file-name-.jpg"},
     47   {L"**********::::.txt", L"--------------.txt"},
     48   // We can't use UCNs (universal character names) for C0/C1 characters and
     49   // U+007F, but \x escape is interpreted by MSVC and gcc as we intend.
     50   {L"bad\x0003\x0091 file\u200E\u200Fname.png", L"bad-- file--name.png"},
     51 #if defined(OS_WIN)
     52   {L"bad*file\\name.jpg", L"bad-file-name.jpg"},
     53   {L"\t  bad*file\\name/.jpg ", L"bad-file-name-.jpg"},
     54 #elif defined(OS_MACOSX)
     55   {L"bad*file?name.jpg", L"bad-file-name.jpg"},
     56   {L"\t  bad*file?name/.jpg ", L"bad-file-name-.jpg"},
     57 #endif
     58   {L"this_file_name is okay!.mp3", L"this_file_name is okay!.mp3"},
     59   {L"\u4E00\uAC00.mp3", L"\u4E00\uAC00.mp3"},
     60   {L"\u0635\u200C\u0644.mp3", L"\u0635\u200C\u0644.mp3"},
     61   {L"\U00010330\U00010331.mp3", L"\U00010330\U00010331.mp3"},
     62   // Unassigned codepoints are ok.
     63   {L"\u0378\U00040001.mp3", L"\u0378\U00040001.mp3"},
     64   // Non-characters are not allowed.
     65   {L"bad\uFFFFfile\U0010FFFEname.jpg ", L"bad-file-name.jpg"},
     66   {L"bad\uFDD0file\uFDEFname.jpg ", L"bad-file-name.jpg"},
     67 };
     68 
     69 TEST_F(FileUtilICUTest, ReplaceIllegalCharactersInPathTest) {
     70   for (size_t i = 0; i < arraysize(kIllegalCharacterCases); ++i) {
     71 #if defined(OS_WIN)
     72     std::wstring bad_name(kIllegalCharacterCases[i].bad_name);
     73     file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
     74     EXPECT_EQ(kIllegalCharacterCases[i].good_name, bad_name);
     75 #elif defined(OS_MACOSX)
     76     std::string bad_name(WideToUTF8(kIllegalCharacterCases[i].bad_name));
     77     file_util::ReplaceIllegalCharactersInPath(&bad_name, '-');
     78     EXPECT_EQ(WideToUTF8(kIllegalCharacterCases[i].good_name), bad_name);
     79 #endif
     80   }
     81 }
     82 
     83 #endif
     84 
     85