From 9c3e464ce4b924d2b0e3d976f05c6e0d3fc2bb21 Mon Sep 17 00:00:00 2001 From: "Scott E. Graves" Date: Sun, 10 Nov 2024 15:45:25 -0600 Subject: [PATCH] fuse unit tests and fixes --- .../src/fuse_drive_access_test.cpp | 174 ++++++------------ 1 file changed, 58 insertions(+), 116 deletions(-) diff --git a/repertory/repertory_test/src/fuse_drive_access_test.cpp b/repertory/repertory_test/src/fuse_drive_access_test.cpp index f1178ea3..1bde67d1 100644 --- a/repertory/repertory_test/src/fuse_drive_access_test.cpp +++ b/repertory/repertory_test/src/fuse_drive_access_test.cpp @@ -23,6 +23,53 @@ #include "fixtures/fuse_fixture.hpp" +namespace { +const auto access_permutations = { + // clang-format off + std::make_tuple(0000, R_OK, -1, EACCES), // No permissions, R_OK + std::make_tuple(0000, W_OK, -1, EACCES), // No permissions, W_OK + std::make_tuple(0000, X_OK, -1, EACCES), // No permissions, X_OK + std::make_tuple(0444, R_OK, 0, 0), // Read-only, R_OK + std::make_tuple(0444, W_OK, -1, EACCES), // Read-only, W_OK + std::make_tuple(0444, X_OK, -1, EACCES), // Read-only, X_OK + std::make_tuple(0222, R_OK, -1, EACCES), // Write-only, R_OK + std::make_tuple(0222, W_OK, 0, 0), // Write-only, W_OK + std::make_tuple(0222, X_OK, -1, EACCES), // Write-only, X_OK + std::make_tuple(0111, R_OK, -1, EACCES), // Execute-only, R_OK + std::make_tuple(0111, W_OK, -1, EACCES), // Execute-only, W_OK + std::make_tuple(0111, X_OK, 0, 0), // Execute-only, X_OK + std::make_tuple(0666, R_OK | W_OK, 0, 0), // Read and write, R_OK | W_OK + std::make_tuple(0666, R_OK | X_OK, -1, EACCES), // Read and write, R_OK | X_OK + std::make_tuple(0777, R_OK | W_OK | X_OK, 0, 0), // All permissions + std::make_tuple(0555, R_OK | X_OK, 0, 0), // Read and execute, R_OK | X_OK + std::make_tuple(0555, W_OK, -1, EACCES) // Read and execute, W_OK + // clang-format on +}; + +void perform_access_test(auto &&permutation, auto &&item_path) { + mode_t permissions{}; + int access_mode{}; + int expected_result{}; + int expected_errno{}; + std::tie(permissions, access_mode, expected_result, expected_errno) = + permutation; + + EXPECT_EQ(0, chmod(item_path.c_str(), permissions)); + + auto result = access(item_path.c_str(), access_mode); + + EXPECT_EQ(result, expected_result) + << "Expected access result to be " << expected_result + << " for permissions " << std::oct << permissions << " and access mode " + << access_mode; + if (result == -1) { + EXPECT_EQ(errno, expected_errno) + << "Expected errno to be " << expected_errno << " for permissions " + << std::oct << permissions; + } +} +} // namespace + namespace repertory { TYPED_TEST_CASE(fuse_test, fuse_provider_types); @@ -43,69 +90,6 @@ TYPED_TEST(fuse_test, access_can_check_if_directory_exists) { this->rmdir_and_test(dir_path); } -TYPED_TEST(fuse_test, access_can_check_directory_read_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, access(dir_path.c_str(), R_OK)); - - this->rmdir_and_test(dir_path); -} - -TYPED_TEST(fuse_test, access_can_check_directory_does_not_have_read_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, chmod(dir_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(dir_path.c_str(), R_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); - - this->rmdir_and_test(dir_path); -} - -TYPED_TEST(fuse_test, access_can_check_directory_write_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, access(dir_path.c_str(), W_OK)); - - this->rmdir_and_test(dir_path); -} - -TYPED_TEST(fuse_test, access_can_check_directory_does_not_have_write_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, chmod(dir_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(dir_path.c_str(), W_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); - - this->rmdir_and_test(dir_path); -} - -TYPED_TEST(fuse_test, access_can_check_directory_execute_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, access(dir_path.c_str(), X_OK)); - - this->rmdir_and_test(dir_path); -} - -TYPED_TEST(fuse_test, access_can_check_directory_does_not_have_execute_access) { - std::string dir_name{"access_test"}; - auto dir_path = this->create_directory_and_test(dir_name); - - EXPECT_EQ(0, chmod(dir_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(dir_path.c_str(), X_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); - - this->rmdir_and_test(dir_path); -} - TYPED_TEST(fuse_test, access_can_check_if_file_exists) { std::string file_name{"access_test"}; auto file_path = this->create_file_and_test(file_name); @@ -115,66 +99,24 @@ TYPED_TEST(fuse_test, access_can_check_if_file_exists) { this->unlink_file_and_test(file_path); } -TYPED_TEST(fuse_test, access_can_check_file_read_access) { - std::string file_name{"access_test"}; - auto file_path = this->create_file_and_test(file_name); +TYPED_TEST(fuse_test, access_directory_permutations_test) { + std::string dir_name{"access_test"}; + auto dir_path = this->create_directory_and_test(dir_name); - EXPECT_EQ(0, access(file_path.c_str(), R_OK)); + for (auto &&permutation : access_permutations) { + perform_access_test(permutation, dir_path); + } - this->unlink_file_and_test(file_path); + this->rmdir_and_test(dir_path); } -TYPED_TEST(fuse_test, access_can_check_file_does_not_have_read_access) { +TYPED_TEST(fuse_test, access_file_permutations_test) { std::string file_name{"access_test"}; auto file_path = this->create_file_and_test(file_name); - EXPECT_EQ(0, chmod(file_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(file_path.c_str(), R_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); - - this->unlink_file_and_test(file_path); -} - -TYPED_TEST(fuse_test, access_can_check_file_write_access) { - std::string file_name{"access_test"}; - auto file_path = this->create_file_and_test(file_name); - - EXPECT_EQ(0, access(file_path.c_str(), W_OK)); - - this->unlink_file_and_test(file_path); -} - -TYPED_TEST(fuse_test, access_can_check_file_does_not_have_write_access) { - std::string file_name{"access_test"}; - auto file_path = this->create_file_and_test(file_name); - - EXPECT_EQ(0, chmod(file_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(file_path.c_str(), W_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); - - this->unlink_file_and_test(file_path); -} - -TYPED_TEST(fuse_test, access_can_check_file_execute_access) { - std::string file_name{"access_test"}; - auto file_path = this->create_file_and_test(file_name); - - EXPECT_EQ(0, chmod(file_path.c_str(), 0700)); - EXPECT_EQ(0, access(file_path.c_str(), X_OK)); - - this->unlink_file_and_test(file_path); -} - -TYPED_TEST(fuse_test, access_can_check_file_does_not_have_execute_access) { - std::string file_name{"access_test"}; - auto file_path = this->create_file_and_test(file_name); - - EXPECT_EQ(0, chmod(file_path.c_str(), 0000)); - - EXPECT_EQ(-1, access(file_path.c_str(), X_OK)); - EXPECT_EQ(EACCES, utils::get_last_error_code()); + for (auto &&permutation : access_permutations) { + perform_access_test(permutation, file_path); + } this->unlink_file_and_test(file_path); }