fs_win_ut.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "fs.h"
  2. #include "fs_win.h"
  3. #include <library/cpp/testing/unittest/registar.h>
  4. #include "fileapi.h"
  5. #include "file.h"
  6. #include "fstat.h"
  7. #include "win_undef.h"
  8. #include <util/charset/wide.h>
  9. #include <util/folder/path.h>
  10. #include <util/generic/string.h>
  11. static void Touch(const TFsPath& path) {
  12. TFile file(path, CreateAlways | WrOnly);
  13. file.Write("1115", 4);
  14. }
  15. static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) {
  16. wstr.resize(str.size());
  17. size_t written = 0;
  18. if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) {
  19. return nullptr;
  20. }
  21. wstr.erase(written);
  22. static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)");
  23. return (const WCHAR*)wstr.data();
  24. }
  25. Y_UNIT_TEST_SUITE(TFsWinTest) {
  26. Y_UNIT_TEST(TestRemoveDirWithROFiles) {
  27. TFsPath dir1 = "dir1";
  28. NFsPrivate::WinRemove(dir1);
  29. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  30. UNIT_ASSERT(NFsPrivate::WinMakeDirectory(dir1));
  31. UNIT_ASSERT(TFileStat(dir1).IsDir());
  32. TFsPath file1 = dir1 / "file.txt";
  33. Touch(file1);
  34. UNIT_ASSERT(NFsPrivate::WinExists(file1));
  35. {
  36. TUtf16String wstr;
  37. LPCWSTR wname = UTF8ToWCHAR(static_cast<const TString&>(file1), wstr);
  38. UNIT_ASSERT(wname);
  39. WIN32_FILE_ATTRIBUTE_DATA fad;
  40. fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY;
  41. ::SetFileAttributesW(wname, fad.dwFileAttributes);
  42. }
  43. NFsPrivate::WinRemove(dir1);
  44. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  45. }
  46. Y_UNIT_TEST(TestRemoveReadOnlyDir) {
  47. TFsPath dir1 = "dir1";
  48. NFsPrivate::WinRemove(dir1);
  49. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  50. UNIT_ASSERT(NFsPrivate::WinMakeDirectory(dir1));
  51. UNIT_ASSERT(TFileStat(dir1).IsDir());
  52. {
  53. TUtf16String wstr;
  54. LPCWSTR wname = UTF8ToWCHAR(static_cast<const TString&>(dir1), wstr);
  55. UNIT_ASSERT(wname);
  56. WIN32_FILE_ATTRIBUTE_DATA fad;
  57. fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY;
  58. ::SetFileAttributesW(wname, fad.dwFileAttributes);
  59. }
  60. NFsPrivate::WinRemove(dir1);
  61. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  62. }
  63. } // Y_UNIT_TEST_SUITE(TFsWinTest)