From 0ad0ff508bfd4ace6aea0de4f42fe5b72ef6cb54 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Tue, 22 Oct 2024 08:18:53 -0500 Subject: [PATCH] continue refactor drive tests --- .../include/fixtures/fuse_fixture.hpp | 28 ++-- .../include/fixtures/winfsp_fixture.hpp | 37 ++++- .../repertory_test/src/fuse_drive_test.cpp | 32 +++-- .../repertory_test/src/winfsp_drive_test.cpp | 131 ++---------------- 4 files changed, 78 insertions(+), 150 deletions(-) diff --git a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp index 2d6686a4..971fba75 100644 --- a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp @@ -151,11 +151,9 @@ protected: } public: - static auto create_file_and_test(std::string name) -> std::string { - auto file_path = - utils::path::combine(mount_location, { - name + std::to_string(++idx), - }); + static auto create_file_and_test(std::string &file_name) -> std::string { + file_name += std::to_string(++idx); + auto file_path = utils::path::combine(mount_location, {file_name}); auto fd = open(file_path.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP); @@ -174,11 +172,10 @@ public: return file_path; } - static auto create_root_file(std::string name) -> std::string { - auto file_path = create_file_and_test(name); + static auto create_root_file(std::string &file_name) -> std::string { + auto file_path = create_file_and_test(file_name); + auto api_path = utils::path::create_api_path(file_name); - auto api_path = - utils::path::create_api_path(utils::path::strip_to_filename(file_path)); provider->set_item_meta(api_path, { {META_UID, "0"}, {META_GID, "0"}, @@ -213,16 +210,9 @@ public: EXPECT_TRUE(unmounted); } - static void unlink_file_and_test(const std::string &file_path) { - int ret = 0; - for (auto i = 0; ((ret = unlink(file_path.c_str())) != 0) && (i < 20); - i++) { - std::this_thread::sleep_for(100ms); - } - - EXPECT_EQ(0, ret); - - std::this_thread::sleep_for(SLEEP_SECONDS); + static void unlink_file_and_test(std::string_view file_path) { + EXPECT_TRUE(utils::file::file(file_path).remove()); + EXPECT_FALSE(utils::file::file(file_path).exists()); EXPECT_FALSE(utils::file::directory(file_path).exists()); EXPECT_FALSE(utils::file::file(file_path).exists()); } diff --git a/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp b/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp index ead09360..d8470695 100644 --- a/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/winfsp_fixture.hpp @@ -29,10 +29,10 @@ #include "comm/curl/curl_comm.hpp" #include "drives/winfsp/winfsp_drive.hpp" #include "platform/platform.hpp" +#include "providers/i_provider.hpp" #include "providers/s3/s3_provider.hpp" #include "providers/sia/sia_provider.hpp" #include "types/repertory.hpp" -#include "utils/event_capture.hpp" #include "utils/file_utils.hpp" #include "utils/path.hpp" @@ -153,12 +153,37 @@ protected: } public: - static void delete_file_and_test(const std::string &file) { - event_capture ec({"file_removed"}); - EXPECT_TRUE(utils::file::file(file).remove()); - ec.wait_for_empty(); + [[nodiscard]] static auto create_file_and_test(std::string &file_name) + -> std::string { + file_name += std::to_string(++idx); + auto api_path = utils::path::create_api_path(file_name); + auto file_path = utils::path::combine(mount_location, {file_name}); - EXPECT_FALSE(utils::file::file(file).exists()); + auto handle = + ::CreateFileA(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, + CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); + EXPECT_NE(INVALID_HANDLE_VALUE, handle); + EXPECT_TRUE(::CloseHandle(handle)); + + EXPECT_TRUE(utils::file::file(file_path).exists()); + + auto opt_size = utils::file::file(file_path).size(); + EXPECT_TRUE(opt_size.has_value()); + EXPECT_EQ(0, opt_size.value()); + + std::string attr; + EXPECT_EQ(api_error::success, + provider->get_item_meta(api_path, META_ATTRIBUTES, attr)); + EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, utils::string::to_uint32(attr)); + + return file_path; + } + + static void delete_file_and_test(std::string_view file_path) { + EXPECT_TRUE(utils::file::file(file_path).remove()); + EXPECT_FALSE(utils::file::file(file_path).exists()); + EXPECT_FALSE(utils::file::directory(file_path).exists()); + EXPECT_FALSE(utils::file::file(file_path).exists()); } static void execute_mount(auto &&drive_args) { diff --git a/repertory/repertory_test/src/fuse_drive_test.cpp b/repertory/repertory_test/src/fuse_drive_test.cpp index 11e0029f..e4267920 100644 --- a/repertory/repertory_test/src/fuse_drive_test.cpp +++ b/repertory/repertory_test/src/fuse_drive_test.cpp @@ -518,7 +518,9 @@ namespace repertory { TYPED_TEST_CASE(fuse_test, fuse_provider_types); TYPED_TEST(fuse_test, can_chmod_if_owner) { - auto file_path = this->create_file_and_test("chmod_test"); + std::string file_name{"chmod_test"}; + auto file_path = this->create_file_and_test(file_name); + EXPECT_EQ(0, chmod(file_path.c_str(), S_IRUSR | S_IWUSR)); std::this_thread::sleep_for(SLEEP_SECONDS); @@ -531,7 +533,9 @@ TYPED_TEST(fuse_test, can_chmod_if_owner) { } TYPED_TEST(fuse_test, can_not_chmod_if_not_owner) { - auto file_path = this->create_root_file("chmod_test"); + std::string file_name{"chmod_test"}; + auto file_path = this->create_root_file(file_name); + EXPECT_EQ(-1, chmod(file_path.c_str(), S_IRUSR | S_IWUSR)); EXPECT_EQ(EPERM, errno); @@ -539,7 +543,9 @@ TYPED_TEST(fuse_test, can_not_chmod_if_not_owner) { } TYPED_TEST(fuse_test, can_not_chmod_setgid_if_not_root) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chmod_test"}; + auto file_path = this->create_file_and_test(file_name); + EXPECT_EQ(-1, chmod(file_path.c_str(), S_IRUSR | S_IWUSR | S_IGID)); EXPECT_EQ(EPERM, errno); @@ -547,7 +553,9 @@ TYPED_TEST(fuse_test, can_not_chmod_setgid_if_not_root) { } TYPED_TEST(fuse_test, can_not_chmod_setuid_if_not_root) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chmod_test"}; + auto file_path = this->create_file_and_test(file_name); + EXPECT_EQ(-1, chmod(file_path.c_str(), S_IRUSR | S_IWUSR | S_IUID)); EXPECT_EQ(EPERM, errno); @@ -555,7 +563,9 @@ TYPED_TEST(fuse_test, can_not_chmod_setuid_if_not_root) { } TYPED_TEST(fuse_test, can_not_chmod_set_sticky_if_not_root) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chown_test"}; + auto file_path = this->create_file_and_test(file_name); + EXPECT_EQ(-1, chmod(file_path.c_str(), S_IRUSR | S_IWUSR | S_ISVTX)); EXPECT_EQ(EPERM, errno); @@ -563,7 +573,8 @@ TYPED_TEST(fuse_test, can_not_chmod_set_sticky_if_not_root) { } TYPED_TEST(fuse_test, can_chown_group_if_owner_and_a_member_of_the_group) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chown_test"}; + auto file_path = this->create_file_and_test(file_name); struct stat64 unix_st{}; EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); @@ -581,7 +592,8 @@ TYPED_TEST(fuse_test, can_chown_group_if_owner_and_a_member_of_the_group) { TYPED_TEST(fuse_test, can_not_chown_group_if_owner_but_not_a_member_of_the_group) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chown_test"}; + auto file_path = this->create_file_and_test(file_name); struct stat64 unix_st{}; EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); @@ -598,7 +610,8 @@ TYPED_TEST(fuse_test, } TYPED_TEST(fuse_test, can_not_chown_group_if_not_the_owner) { - auto file_path = this->create_root_file("chown_test"); + std::string file_name{"chown_test"}; + auto file_path = this->create_root_file(file_name); struct stat64 unix_st{}; EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); @@ -615,7 +628,8 @@ TYPED_TEST(fuse_test, can_not_chown_group_if_not_the_owner) { } TYPED_TEST(fuse_test, can_not_chown_user_if_not_root) { - auto file_path = this->create_file_and_test("chown_test"); + std::string file_name{"chown_test"}; + auto file_path = this->create_file_and_test(file_name); struct stat64 unix_st{}; EXPECT_EQ(0, stat64(file_path.c_str(), &unix_st)); diff --git a/repertory/repertory_test/src/winfsp_drive_test.cpp b/repertory/repertory_test/src/winfsp_drive_test.cpp index b516f969..83bbe58d 100644 --- a/repertory/repertory_test/src/winfsp_drive_test.cpp +++ b/repertory/repertory_test/src/winfsp_drive_test.cpp @@ -19,101 +19,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -#if 0 #if defined(_WIN32) #include "fixtures/winfsp_fixture.hpp" namespace repertory { -// void launch_app(std::string cmd) { -// PROCESS_INFORMATION pi{}; -// STARTUPINFO si{}; -// si.cb = sizeof(si); -// -// if (!::CreateProcessA(nullptr, (LPSTR)cmd.c_str(), nullptr, nullptr, FALSE, -// CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP, nullptr, -// nullptr, &si, &pi)) { -// throw std::runtime_error("CreateProcess failed (" + -// std::to_string(::GetLastError()) + ")"); -// } -// -// ::WaitForSingleObject(pi.hProcess, INFINITE); -// DWORD code{}; -// ::GetExitCodeProcess(pi.hProcess, &code); -// -// ::CloseHandle(pi.hProcess); -// ::CloseHandle(pi.hThread); -// EXPECT_EQ(0, code); -// } -// -// E_SIMPLE1(test_begin, info, false, std::string, test_name, TN, E_FROM_STRING); -// #define TEST_HEADER(func) \ -// event_system::instance().raise( \ -// std::string(func) + \ -// "\r\n***********************\r\n***********************") -// -// static auto mount_setup(std::string &mount_point) { -// mount_point = "U:"; -// return std::vector({"unittests", "-f", mount_point}); -// } -// -// static void execute_mount(winfsp_test *test, -// const std::vector &drive_args, -// std::thread &th) { -// ASSERT_EQ(0, test->drive->mount(drive_args)); -// th.join(); -// } -// -// static void unmount(winfsp_test *test, const std::string &mount_point) { -// test->drive->shutdown(); -// auto mounted = utils::file::directory(mount_point).exists(); -// for (auto i = 0; mounted && (i < 50); i++) { -// std::this_thread::sleep_for(100ms); -// mounted = utils::file::directory(mount_point).exists(); -// } -// EXPECT_FALSE(utils::file::directory(mount_point).exists()); -// } -// -// static void root_creation_test(const std::string &mount_point) { -// TEST_HEADER(__FUNCTION__); -// WIN32_FILE_ATTRIBUTE_DATA ad{}; -// EXPECT_TRUE( -// ::GetFileAttributesEx(mount_point.c_str(), GetFileExInfoStandard, &ad)); -// EXPECT_EQ(FILE_ATTRIBUTE_DIRECTORY, ad.dwFileAttributes); -// EXPECT_EQ(0, ad.nFileSizeHigh); -// EXPECT_EQ(0, ad.nFileSizeLow); -// } -// -// static auto create_test(winfsp_test *test, const std::string &mount_point) { -// TEST_HEADER(__FUNCTION__); -// -// auto file = utils::path::combine(mount_point, {{"test_create.txt"}}); -// auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr, -// CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); -// EXPECT_NE(INVALID_HANDLE_VALUE, handle); -// EXPECT_TRUE(::CloseHandle(handle)); -// -// EXPECT_TRUE(utils::file::file(file).exists()); -// -// auto opt_size = utils::file::file(file).size(); -// EXPECT_TRUE(opt_size.has_value()); -// EXPECT_EQ(0, opt_size.value()); -// -// std::string attr; -// EXPECT_EQ(api_error::success, test->provider->get_item_meta( -// "/test_create.txt", META_ATTRIBUTES, attr)); -// EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, utils::string::to_uint32(attr)); -// -// return file; -// } -// -// static void delete_file_test(const std::string &file) { -// TEST_HEADER(__FUNCTION__); -// event_capture ec({"file_removed"}); -// EXPECT_TRUE(utils::file::file(file).remove()); -// EXPECT_FALSE(utils::file::file(file).exists()); -// } -// // static void create_directory_test(const std::string &directory) { // TEST_HEADER(__FUNCTION__); // @@ -184,7 +94,8 @@ namespace repertory { // const std::string &mount_point) { // TEST_HEADER(__FUNCTION__); // const auto file = utils::path::combine(mount_point, {"rename_file.txt"}); -// auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, nullptr, +// auto handle = ::CreateFileA(&file[0], GENERIC_READ, FILE_SHARE_READ, +// nullptr, // CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); // EXPECT_NE(INVALID_HANDLE_VALUE, handle); // EXPECT_TRUE(::CloseHandle(handle)); @@ -224,7 +135,8 @@ namespace repertory { // std::string directory = "rename_dir"; // const auto full_directory = utils::path::combine(mount_point, {directory}); // std::string directory2 = "rename_dir2"; -// const auto full_directory2 = utils::path::combine(mount_point, {directory2}); +// const auto full_directory2 = utils::path::combine(mount_point, +// {directory2}); // // EXPECT_FALSE(::PathIsDirectory(&full_directory[0])); // EXPECT_TRUE(::CreateDirectoryA(&full_directory[0], nullptr)); @@ -282,10 +194,12 @@ namespace repertory { // EXPECT_EQ(0, memcmp(&fbi, &fbi2, sizeof(FILE_BASIC_INFO))); // // std::cout << fbi.FileAttributes << " " << fbi.ChangeTime.QuadPart << " " -// << fbi.CreationTime.QuadPart << " " << fbi.LastAccessTime.QuadPart +// << fbi.CreationTime.QuadPart << " " << +// fbi.LastAccessTime.QuadPart // << " " << fbi.LastWriteTime.QuadPart << std::endl; // std::cout << fbi2.FileAttributes << " " << fbi2.ChangeTime.QuadPart << " " -// << fbi2.CreationTime.QuadPart << " " << fbi2.LastAccessTime.QuadPart +// << fbi2.CreationTime.QuadPart << " " << +// fbi2.LastAccessTime.QuadPart // << " " << fbi2.LastWriteTime.QuadPart << std::endl; // // EXPECT_TRUE(::CloseHandle(handle)); @@ -302,7 +216,8 @@ namespace repertory { // if (handle != INVALID_HANDLE_VALUE) { // const std::string data = "0123456789"; // DWORD bytes_written = 0; -// EXPECT_TRUE(::WriteFile(handle, &data[0], static_cast(data.size()), +// EXPECT_TRUE(::WriteFile(handle, &data[0], +// static_cast(data.size()), // &bytes_written, nullptr)); // EXPECT_EQ(10, bytes_written); // EXPECT_TRUE(::CloseHandle(handle)); @@ -380,34 +295,18 @@ TYPED_TEST_CASE(winfsp_test, winfsp_provider_types); TYPED_TEST(winfsp_test, root_is_created) { WIN32_FILE_ATTRIBUTE_DATA ad{}; - ASSERT_TRUE( - ::GetFileAttributesEx(mount_location.c_str(), GetFileExInfoStandard, &ad)); + ASSERT_TRUE(::GetFileAttributesEx(mount_location.c_str(), + GetFileExInfoStandard, &ad)); EXPECT_EQ(FILE_ATTRIBUTE_DIRECTORY, ad.dwFileAttributes); EXPECT_EQ(0, ad.nFileSizeHigh); EXPECT_EQ(0, ad.nFileSizeLow); } TYPED_TEST(winfsp_test, can_create_and_delete_file) { - auto file = utils::path::combine(mount_location, {"test_create.txt"}); - auto handle = ::CreateFileA(file.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, - CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr); - EXPECT_NE(INVALID_HANDLE_VALUE, handle); - EXPECT_TRUE(::CloseHandle(handle)); - - EXPECT_TRUE(utils::file::file(file).exists()); - - auto opt_size = utils::file::file(file).size(); - EXPECT_TRUE(opt_size.has_value()); - EXPECT_EQ(0, opt_size.value()); - - std::string attr; - EXPECT_EQ(api_error::success, provider->get_item_meta( - "/test_create.txt", META_ATTRIBUTES, attr)); - EXPECT_EQ(FILE_ATTRIBUTE_NORMAL, utils::string::to_uint32(attr)); - - delete_file_and_test(file); + std::string file_name{"test_create_and_delete"}; + auto file_path = create_file_and_test(file_name); + delete_file_and_test(file_path); } } // namespace repertory #endif // defined(_WIN32) -#endif // 0