test: more FUSE driver tests

This commit is contained in:
Marcus Holland-Moritz 2024-07-30 17:28:15 +02:00
parent 061f6419b6
commit ee4fe24850

View File

@ -22,6 +22,7 @@
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cerrno>
#include <chrono> #include <chrono>
#include <concepts> #include <concepts>
#include <filesystem> #include <filesystem>
@ -163,14 +164,27 @@ bool read_file(fs::path const& path, std::string& out) {
return true; return true;
} }
std::error_code get_last_error_code() {
#ifdef _WIN32
return std::error_code(::GetLastError(), std::system_category());
#else
return std::error_code(errno, std::generic_category());
#endif
}
bool read_file(fs::path const& path, std::string& out, std::error_code& ec) { bool read_file(fs::path const& path, std::string& out, std::error_code& ec) {
auto res = folly::readFile(path.string().c_str(), out); auto res = folly::readFile(path.string().c_str(), out);
if (!res) { if (!res) {
#ifdef _WIN32 ec = get_last_error_code();
ec = std::error_code(::GetLastError(), std::system_category()); }
#else return res;
ec = std::error_code(errno, std::generic_category()); }
#endif
bool write_file(fs::path const& path, std::string_view data,
std::error_code& ec) {
auto res = folly::writeFile(data, path.string().c_str());
if (!res) {
ec = get_last_error_code();
} }
return res; return res;
} }
@ -1366,6 +1380,26 @@ TEST_P(tools_test, mutating_and_error_ops) {
EXPECT_EC_UNIX_WIN(ec, ENOENT, ERROR_FILE_NOT_FOUND); EXPECT_EC_UNIX_WIN(ec, ENOENT, ERROR_FILE_NOT_FOUND);
} }
// Open non-existent file for writing
{
auto p = mountpoint / "nonexistent";
EXPECT_FALSE(fs::exists(p));
std::error_code ec;
EXPECT_FALSE(write_file(p, "hello", ec));
EXPECT_TRUE(ec);
EXPECT_EC_UNIX_MAC_WIN(ec, ENOSYS, EACCES, ERROR_ACCESS_DENIED);
}
// Open existing file for writing
{
auto p = mountpoint / "format.sh";
EXPECT_TRUE(fs::exists(p));
std::error_code ec;
EXPECT_FALSE(write_file(p, "hello", ec));
EXPECT_TRUE(ec);
EXPECT_EC_UNIX_WIN(ec, EACCES, ERROR_ACCESS_DENIED);
}
EXPECT_TRUE(runner.unmount()) << runner.cmdline(); EXPECT_TRUE(runner.unmount()) << runner.cmdline();
} }
} }