file_util_icu_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/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// file_util winds up using autoreleased objects on the Mac, so this needs
13// to be a PlatformTest
14class 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.
20static 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
29TEST_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.
42static 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
69TEST_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#if defined(OS_CHROMEOS)
86static const struct normalize_name_encoding_test_cases {
87  const char* original_path;
88  const char* normalized_path;
89} kNormalizeFileNameEncodingTestCases[] = {
90  { "foo_na\xcc\x88me.foo", "foo_n\xc3\xa4me.foo"},
91  { "foo_dir_na\xcc\x88me/foo_na\xcc\x88me.foo",
92    "foo_dir_na\xcc\x88me/foo_n\xc3\xa4me.foo"},
93  { "", ""},
94  { "foo_dir_na\xcc\x88me/", "foo_dir_n\xc3\xa4me"}
95};
96
97TEST_F(FileUtilICUTest, NormalizeFileNameEncoding) {
98  for (size_t i = 0; i < arraysize(kNormalizeFileNameEncodingTestCases); i++) {
99    base::FilePath path(kNormalizeFileNameEncodingTestCases[i].original_path);
100    file_util::NormalizeFileNameEncoding(&path);
101    EXPECT_EQ(
102        base::FilePath(kNormalizeFileNameEncodingTestCases[i].normalized_path),
103        path);
104  }
105}
106
107#endif
108