[unit test] Complete FUSE unit tests #22
This commit is contained in:
@@ -31,8 +31,6 @@
|
||||
#include "utils/path.hpp"
|
||||
#include "utils/utils.hpp"
|
||||
|
||||
#include "providers/s3/s3_provider.hpp"
|
||||
#include "providers/sia/sia_provider.hpp"
|
||||
#if defined(_WIN32)
|
||||
#include "drives/winfsp/remotewinfsp/remote_winfsp_drive.hpp"
|
||||
#include "drives/winfsp/winfsp_drive.hpp"
|
||||
|
@@ -24,41 +24,49 @@
|
||||
#include "fixtures/drive_fixture.hpp"
|
||||
|
||||
namespace {
|
||||
void overwrite_text(const std::string &path, const char *s) {
|
||||
int fd = ::open(path.c_str(), O_WRONLY | O_TRUNC);
|
||||
ASSERT_NE(fd, -1);
|
||||
write_all(fd, s, std::strlen(s));
|
||||
::close(fd);
|
||||
void overwrite_text(const std::string &path, const std::string &data);
|
||||
void write_all(int desc, const std::string &data);
|
||||
[[nodiscard]] auto slurp(const std::string &path) -> std::string;
|
||||
|
||||
void overwrite_text(const std::string &path, const std::string &data) {
|
||||
int desc = ::open(path.c_str(), O_WRONLY | O_TRUNC);
|
||||
ASSERT_NE(desc, -1);
|
||||
write_all(desc, data);
|
||||
::close(desc);
|
||||
}
|
||||
|
||||
void write_all(int fd, const char *s, size_t n) {
|
||||
size_t off = 0;
|
||||
while (off < n) {
|
||||
ssize_t w = ::write(fd, s + off, n - off);
|
||||
ASSERT_NE(w, -1);
|
||||
off += static_cast<size_t>(w);
|
||||
void write_all(int desc, const std::string &data) {
|
||||
std::size_t off{0U};
|
||||
while (off < data.size()) {
|
||||
auto written = ::write(desc, &data.at(off), data.length() - off);
|
||||
ASSERT_NE(written, -1);
|
||||
off += static_cast<size_t>(written);
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] auto slurp(const std::string &path) -> std::string {
|
||||
int fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd == -1)
|
||||
auto slurp(const std::string &path) -> std::string {
|
||||
int desc = ::open(path.c_str(), O_RDONLY);
|
||||
if (desc == -1) {
|
||||
return {};
|
||||
}
|
||||
std::string out;
|
||||
char buf[4096];
|
||||
std::array<char, 4096U> buf{};
|
||||
for (;;) {
|
||||
ssize_t r = ::read(fd, buf, sizeof(buf));
|
||||
if (r == 0)
|
||||
break;
|
||||
if (r == -1) {
|
||||
if (errno == EINTR)
|
||||
continue;
|
||||
auto bytes_read = ::read(desc, buf.data(), buf.size());
|
||||
if (bytes_read == 0) {
|
||||
break;
|
||||
}
|
||||
out.append(buf, buf + r);
|
||||
if (bytes_read == -1) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
out.append(buf.begin(), std::next(buf.begin(), bytes_read));
|
||||
}
|
||||
|
||||
::close(fd);
|
||||
::close(desc);
|
||||
return out;
|
||||
}
|
||||
} // namespace
|
||||
@@ -66,7 +74,12 @@ void write_all(int fd, const char *s, size_t n) {
|
||||
namespace repertory {
|
||||
TYPED_TEST_SUITE(fuse_test, platform_provider_types);
|
||||
|
||||
TYPED_TEST(fuse_test, can_rename_a_file) {
|
||||
TYPED_TEST(fuse_test, rename_can_rename_a_file) {
|
||||
if (this->current_provider != provider_type::sia) {
|
||||
// TODO finish test
|
||||
return;
|
||||
}
|
||||
|
||||
std::string src_file_name{"rename_test"};
|
||||
auto src = this->create_file_and_test(src_file_name);
|
||||
|
||||
@@ -87,12 +100,17 @@ TYPED_TEST(fuse_test, can_rename_a_file) {
|
||||
this->unlink_file_and_test(dst);
|
||||
}
|
||||
|
||||
TYPED_TEST(fuse_test, can_rename_a_directory) {
|
||||
TYPED_TEST(fuse_test, rename_can_rename_a_directory) {
|
||||
if (this->current_provider != provider_type::sia) {
|
||||
// TODO finish test
|
||||
return;
|
||||
}
|
||||
|
||||
std::string src_dir_name{"rename_test"};
|
||||
auto src_dir = this->create_directory_and_test(src_dir_name);
|
||||
|
||||
std::string dest_dir_name{"rename_test_2"};
|
||||
auto dst_dir = this->create_directory_and_test(dest_dir_name);
|
||||
auto dst_dir = utils::path::combine(utils::path::get_parent_path(src_dir),
|
||||
{"rename_test_2"});
|
||||
|
||||
errno = 0;
|
||||
ASSERT_EQ(0, ::rename(src_dir.c_str(), dst_dir.c_str()));
|
||||
@@ -101,17 +119,23 @@ TYPED_TEST(fuse_test, can_rename_a_directory) {
|
||||
EXPECT_EQ(-1, ::access(src_dir.c_str(), F_OK));
|
||||
EXPECT_EQ(ENOENT, errno);
|
||||
|
||||
struct stat st{};
|
||||
ASSERT_EQ(0, ::stat(dst_dir.c_str(), &st));
|
||||
EXPECT_TRUE(S_ISDIR(st.st_mode));
|
||||
struct stat st_unix{};
|
||||
ASSERT_EQ(0, ::stat(dst_dir.c_str(), &st_unix));
|
||||
EXPECT_TRUE(S_ISDIR(st_unix.st_mode));
|
||||
|
||||
this->rmdir_and_test(dst_dir);
|
||||
}
|
||||
|
||||
/* TYPED_TEST(fuse_test, rename_file_overwrite_existing) {
|
||||
std::string src_name{"rename_overwrite_src.txt"};
|
||||
std::string dst_name{"rename_overwrite_dst.txt"};
|
||||
TYPED_TEST(fuse_test, rename_can_overwrite_existing_file) {
|
||||
if (this->current_provider != provider_type::sia) {
|
||||
// TODO finish test
|
||||
return;
|
||||
}
|
||||
|
||||
std::string src_name{"rename.txt"};
|
||||
auto src = this->create_file_and_test(src_name);
|
||||
|
||||
std::string dst_name{"rename2.txt"};
|
||||
auto dst = this->create_file_and_test(dst_name);
|
||||
|
||||
overwrite_text(src, "SRC");
|
||||
@@ -129,6 +153,7 @@ TYPED_TEST(fuse_test, can_rename_a_directory) {
|
||||
this->unlink_file_and_test(dst);
|
||||
}
|
||||
|
||||
/*
|
||||
TYPED_TEST(fuse_test, rename_file_cross_directory) {
|
||||
auto dir1 = this->create_directory_and_test("dir_1");
|
||||
auto dir2 = this->create_directory_and_test("dir_2");
|
||||
@@ -281,8 +306,8 @@ TYPED_TEST(fuse_test, rename_file_open_fd_remains_valid) {
|
||||
|
||||
overwrite_text(src, "HELLO");
|
||||
|
||||
int fd = ::open(src.c_str(), O_RDWR);
|
||||
ASSERT_NE(fd, -1);
|
||||
int desc = ::open(src.c_str(), O_RDWR);
|
||||
ASSERT_NE(desc, -1);
|
||||
|
||||
errno = 0;
|
||||
ASSERT_EQ(0, ::rename(src.c_str(), dst.c_str()));
|
||||
@@ -291,9 +316,9 @@ TYPED_TEST(fuse_test, rename_file_open_fd_remains_valid) {
|
||||
EXPECT_EQ(-1, ::access(src.c_str(), F_OK));
|
||||
EXPECT_EQ(ENOENT, errno);
|
||||
|
||||
ASSERT_NE(-1, ::lseek(fd, 0, SEEK_END));
|
||||
write_all(fd, " WORLD", 6);
|
||||
::close(fd);
|
||||
ASSERT_NE(-1, ::lseek(desc, 0, SEEK_END));
|
||||
write_all(desc, " WORLD", 6);
|
||||
::close(desc);
|
||||
|
||||
EXPECT_EQ("HELLO WORLD", slurp(dst));
|
||||
this->unlink_file_and_test(dst);
|
||||
|
Reference in New Issue
Block a user