[unit test] Complete FUSE unit tests #22
Some checks reported errors
Blockstorage/repertory/pipeline/head This commit looks good
BlockStorage/repertory/pipeline/head Something is wrong with the build of this commit

This commit is contained in:
2025-09-20 17:26:14 -05:00
parent 52e673a9ee
commit d4aba93051
3 changed files with 137 additions and 0 deletions

View File

@@ -547,6 +547,20 @@ public:
<< ") failed: " << std::strerror(errno);
return st_unix.st_size;
}
static auto read_dirnames(DIR *dir) -> std::set<std::string> {
std::set<std::string> names;
while (auto *entry = ::readdir(dir)) {
const auto *name = entry->d_name;
if (std::strcmp(name, ".") == 0 || std::strcmp(name, "..") == 0) {
continue;
}
names.emplace(name);
}
return names;
}
#endif // !defined(_WIN32)
};

View File

@@ -0,0 +1,118 @@
/*
Copyright <2018-2025> <scott.e.graves@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#if !defined(_WIN32)
#include "fixtures/drive_fixture.hpp"
namespace repertory {
TYPED_TEST_SUITE(fuse_test, platform_provider_types);
TYPED_TEST(fuse_test, directory_can_read_empty_directory) {
std::string dir_name{"directory"};
auto dir = this->create_directory_and_test(dir_name);
auto *dir_ptr = ::opendir(dir.c_str());
ASSERT_NE(dir_ptr, nullptr);
auto names = this->read_dirnames(dir_ptr);
EXPECT_TRUE(names.empty());
EXPECT_EQ(0, ::closedir(dir_ptr));
this->rmdir_and_test(dir);
}
TYPED_TEST(fuse_test, directory_can_read_populated_directory) {
std::string dir_name{"directory"};
auto dir = this->create_directory_and_test(dir_name);
auto file_name_1{dir_name + "/file_a"};
auto src_1 = this->create_file_and_test(file_name_1);
auto file_name_2{dir_name + "/file_b"};
auto src_2 = this->create_file_and_test(file_name_2);
auto sub_dir_name{dir_name + "/subdir_a"};
auto sub_dir = this->create_directory_and_test(sub_dir_name);
auto *dir_ptr = ::opendir(dir.c_str());
ASSERT_NE(dir_ptr, nullptr);
auto names = this->read_dirnames(dir_ptr);
EXPECT_TRUE(names.contains(utils::path::strip_to_file_name(src_1)));
EXPECT_TRUE(names.contains(utils::path::strip_to_file_name(src_2)));
EXPECT_TRUE(names.contains(utils::path::strip_to_file_name(sub_dir)));
::rewinddir(dir_ptr);
auto names2 = this->read_dirnames(dir_ptr);
EXPECT_EQ(names, names2);
EXPECT_EQ(0, ::closedir(dir_ptr));
this->unlink_file_and_test(src_1);
this->unlink_file_and_test(src_2);
this->rmdir_and_test(sub_dir);
this->rmdir_and_test(dir);
}
TYPED_TEST(fuse_test, directory_opendir_fails_for_file) {
std::string file_name{"directory"};
auto src = this->create_file_and_test(file_name);
errno = 0;
auto *dir_ptr = ::opendir(src.c_str());
EXPECT_EQ(dir_ptr, nullptr);
EXPECT_EQ(errno, ENOTDIR);
this->unlink_file_and_test(src);
}
TYPED_TEST(fuse_test, directory_opendir_fails_if_directory_does_not_exist) {
std::string file_name{"directory"};
auto dir = this->create_file_path(file_name);
errno = 0;
auto *dir_ptr = ::opendir(dir.c_str());
EXPECT_EQ(dir_ptr, nullptr);
EXPECT_EQ(errno, ENOENT);
}
TYPED_TEST(fuse_test, directory_can_opendir_after_closedir) {
std::string dir_name{"directory"};
auto dir = this->create_directory_and_test(dir_name);
auto *dir_ptr = ::opendir(dir.c_str());
ASSERT_NE(dir_ptr, nullptr);
(void)this->read_dirnames(dir_ptr);
EXPECT_EQ(0, ::closedir(dir_ptr));
dir_ptr = ::opendir(dir.c_str());
ASSERT_NE(dir_ptr, nullptr);
EXPECT_EQ(0, ::closedir(dir_ptr));
this->rmdir_and_test(dir);
}
} // namespace repertory
#endif // !defined(_WIN32)

View File

@@ -105,6 +105,11 @@ TYPED_TEST(fuse_test, fallocate_then_ftruncate_makes_size_visible) {
EXPECT_EQ(0, ::ftruncate(desc, len));
#else // !defined(__APPLE__)
auto res = ::posix_fallocate(desc, 0, len);
if (res == EOPNOTSUPP) {
::close(desc);
this->unlink_file_and_test(src);
return;
}
EXPECT_EQ(0, res);
EXPECT_EQ(0, ::ftruncate(desc, len / 2));
EXPECT_EQ(0, ::ftruncate(desc, len));