/source/third_party/chromium/base/i18n/file_util_icu_unittest.cc

https://github.com/snowlesswinter/framework · C++ · 84 lines · 60 code · 11 blank · 13 comment · 3 complexity · f1e19c119cd6effb705fc8cabee5624a MD5 · raw file

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