fs_win_ut.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. wstr.erase(written);
  21. static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)");
  22. return (const WCHAR*)wstr.data();
  23. }
  24. Y_UNIT_TEST_SUITE(TFsWinTest) {
  25. Y_UNIT_TEST(TestRemoveDirWithROFiles) {
  26. TFsPath dir1 = "dir1";
  27. NFsPrivate::WinRemove(dir1);
  28. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  29. UNIT_ASSERT(NFsPrivate::WinMakeDirectory(dir1));
  30. UNIT_ASSERT(TFileStat(dir1).IsDir());
  31. TFsPath file1 = dir1 / "file.txt";
  32. Touch(file1);
  33. UNIT_ASSERT(NFsPrivate::WinExists(file1));
  34. {
  35. TUtf16String wstr;
  36. LPCWSTR wname = UTF8ToWCHAR(static_cast<const TString&>(file1), wstr);
  37. UNIT_ASSERT(wname);
  38. WIN32_FILE_ATTRIBUTE_DATA fad;
  39. fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY;
  40. ::SetFileAttributesW(wname, fad.dwFileAttributes);
  41. }
  42. NFsPrivate::WinRemove(dir1);
  43. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  44. }
  45. Y_UNIT_TEST(TestRemoveReadOnlyDir) {
  46. TFsPath dir1 = "dir1";
  47. NFsPrivate::WinRemove(dir1);
  48. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  49. UNIT_ASSERT(NFsPrivate::WinMakeDirectory(dir1));
  50. UNIT_ASSERT(TFileStat(dir1).IsDir());
  51. {
  52. TUtf16String wstr;
  53. LPCWSTR wname = UTF8ToWCHAR(static_cast<const TString&>(dir1), wstr);
  54. UNIT_ASSERT(wname);
  55. WIN32_FILE_ATTRIBUTE_DATA fad;
  56. fad.dwFileAttributes = FILE_ATTRIBUTE_READONLY;
  57. ::SetFileAttributesW(wname, fad.dwFileAttributes);
  58. }
  59. NFsPrivate::WinRemove(dir1);
  60. UNIT_ASSERT(!NFsPrivate::WinExists(dir1));
  61. }
  62. } // Y_UNIT_TEST_SUITE(TFsWinTest)