From 3c2c7836834e5978c6f58605217b0bc2631ad014 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 29 Dec 2024 06:41:55 -0600 Subject: [PATCH] [Unit Test] Complete FUSE unit tests #22 --- CHANGELOG.md | 4 +++ .../src/drives/fuse/fuse_drive.cpp | 7 ++++ .../include/fixtures/fuse_fixture.hpp | 12 ++++--- .../src/fuse_drive_rdrw_test.cpp | 34 +++++++++++++++---- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cbc96af9..e6668bc1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,10 @@ ### Issues +* \#22 [Unit Test] Complete FUSE unit tests + ### Changes from v2.0.2-rc + * Updated copyright to 2018-2025 ## v2.0.2-rc @@ -13,6 +16,7 @@ * Refactored `config.json` - will need to verify configuration settings prior to mounting + ### Issues * \#12 \[Unit Test\] Complete all providers unit tests diff --git a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp index a13eb7a1..93199b86 100644 --- a/repertory/librepertory/src/drives/fuse/fuse_drive.cpp +++ b/repertory/librepertory/src/drives/fuse/fuse_drive.cpp @@ -732,6 +732,9 @@ auto fuse_drive::read_impl(std::string api_path, char *buffer, size_t read_size, if (not fm_->get_open_file(file_info->fh, false, open_file)) { return api_error::item_not_found; } + if (open_file->is_directory()) { + return api_error::directory_exists; + } auto res = check_readable(open_file->get_open_data(file_info->fh), api_error::invalid_handle); @@ -1401,6 +1404,10 @@ auto fuse_drive::write_impl(std::string /*api_path*/ return api_error::item_not_found; } + if (open_file->is_directory()) { + return api_error::directory_exists; + } + auto res = check_writeable(open_file->get_open_data(file_info->fh), api_error::invalid_handle); if (res != api_error::success) { diff --git a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp index a1ef33cf..a8298269 100644 --- a/repertory/repertory_test/include/fixtures/fuse_fixture.hpp +++ b/repertory/repertory_test/include/fixtures/fuse_fixture.hpp @@ -45,7 +45,8 @@ #endif namespace { -std::atomic idx{0U}; +std::atomic provider_idx{0U}; + constexpr const auto SLEEP_SECONDS{1.5s}; } // namespace @@ -126,6 +127,7 @@ protected: }); } + config->set_database_type(database_type::sqlite); meta = create_meta_db(*config); execute_mount(drive_args, mount_location); }; @@ -171,6 +173,7 @@ protected: }); } + config->set_database_type(database_type::sqlite); meta = create_meta_db(*config); execute_mount(drive_args, mount_location); }; @@ -197,6 +200,7 @@ protected: std::make_unique(provider_type::remote, cfg_directory); config2->set_enable_drive_events(true); config2->set_event_level(event_level::trace); + config2->set_database_type(database_type::sqlite); drive_args2 = std::vector({ "-dd", @@ -259,14 +263,14 @@ protected: public: static auto create_file_path(std::string &file_name) { - file_name += std::to_string(++idx); + file_name += std::to_string(++provider_idx); auto file_path = utils::path::combine(mount_location, {file_name}); return file_path; } static auto create_file_and_test(std::string &file_name, mode_t perms) -> std::string { - file_name += std::to_string(++idx); + file_name += std::to_string(++provider_idx); auto file_path = utils::path::combine(mount_location, {file_name}); auto handle = open(file_path.c_str(), O_CREAT | O_EXCL | O_RDWR, perms); @@ -297,7 +301,7 @@ public: static auto create_directory_and_test(std::string &dir_name, mode_t perms) -> std::string { - dir_name += std::to_string(++idx); + dir_name += std::to_string(++provider_idx); auto dir_path = utils::path::combine(mount_location, {dir_name}); mkdir(dir_path.c_str(), perms); diff --git a/repertory/repertory_test/src/fuse_drive_rdrw_test.cpp b/repertory/repertory_test/src/fuse_drive_rdrw_test.cpp index 5d6e938b..5a99bf94 100644 --- a/repertory/repertory_test/src/fuse_drive_rdrw_test.cpp +++ b/repertory/repertory_test/src/fuse_drive_rdrw_test.cpp @@ -63,8 +63,8 @@ TYPED_TEST(fuse_test, rdrw_can_read_from_offset) { data_buffer read_buffer(1U); for (std::size_t idx = 0U; idx < write_buffer.size(); ++idx) { - auto bytes_read = - pread64(handle, read_buffer.data(), read_buffer.size(), idx); + auto bytes_read = pread64(handle, read_buffer.data(), read_buffer.size(), + static_cast(idx)); EXPECT_EQ(1U, bytes_read); EXPECT_EQ(write_buffer.at(idx), read_buffer.at(0U)); @@ -89,8 +89,8 @@ TYPED_TEST(fuse_test, rdrw_can_read_from_offset_after_eof) { data_buffer read_buffer(1U); for (std::size_t idx = 0U; idx < write_buffer.size() + 1U; ++idx) { - auto bytes_read = - pread64(handle, read_buffer.data(), read_buffer.size(), idx); + auto bytes_read = pread64(handle, read_buffer.data(), read_buffer.size(), + static_cast(idx)); if (idx == write_buffer.size()) { EXPECT_EQ(0U, bytes_read); } else { @@ -140,8 +140,7 @@ TYPED_TEST(fuse_test, rdrw_can_not_read_from_wo_file) { EXPECT_EQ(write_buffer.size(), bytes_written); data_buffer read_buffer(1U); - auto bytes_read = - pread64(handle, read_buffer.data(), read_buffer.size(), idx); + auto bytes_read = pread64(handle, read_buffer.data(), read_buffer.size(), 0); EXPECT_EQ(-1, bytes_read); EXPECT_EQ(EBADF, errno); @@ -149,6 +148,29 @@ TYPED_TEST(fuse_test, rdrw_can_not_read_from_wo_file) { this->unlink_file_and_test(file_path); } + +TYPED_TEST(fuse_test, rdrw_can_not_read_or_write_to_directory) { + std::string dir_name{"create_test"}; + auto dir_path = this->create_directory_and_test(dir_name); + + auto handle = open(dir_path.c_str(), O_DIRECTORY); + ASSERT_GT(handle, -1); + + auto write_buffer = utils::generate_secure_random(8096U); + auto bytes_written = + pwrite64(handle, write_buffer.data(), write_buffer.size(), 0U); + EXPECT_EQ(-1, bytes_written); + EXPECT_EQ(EBADF, errno); + + data_buffer read_buffer(1U); + auto bytes_read = pread64(handle, read_buffer.data(), read_buffer.size(), 0); + EXPECT_EQ(-1, bytes_read); + EXPECT_EQ(EISDIR, errno); + + close(handle); + + this->rmdir_and_test(dir_path); +} } // namespace repertory #endif // !defined(_WIN32)