Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mhx committed Dec 22, 2023
1 parent 9adf183 commit c049713
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions test/dwarfs_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/vfs.h>
#include <sys/xattr.h>
#endif

#include <folly/FileUtil.h>
#include <folly/portability/Unistd.h>

#include <boost/asio/io_service.hpp>
Expand Down Expand Up @@ -110,15 +112,19 @@ bool wait_until_file_ready(fs::path const& path,
return true;
}

bool read_file(fs::path const& path, std::string& out) {
std::ifstream ifs(path, std::ios::binary);
if (!ifs.is_open()) {
return false;
bool read_file(fs::path const& path, std::string& out,
std::error_code* ec = nullptr) {
auto res = folly::readFile(path.string().c_str(), out);
if (!res) {
if (ec) {
#ifdef _WIN32
*ec = std::error_code(::GetLastError(), std::system_category());
#else
*ec = std::error_code(errno, std::generic_category());
#endif
}
}
std::stringstream tmp;
tmp << ifs.rdbuf();
out = tmp.str();
return true;
return res;
}

struct compare_directories_result {
Expand Down Expand Up @@ -756,6 +762,14 @@ TEST_P(tools_test, end_to_end) {
<< runner.cmdline();
EXPECT_EQ(unicode_file_contents, "unicode\n") << runner.cmdline();

#ifndef _WIN32
{
struct statfs stfs;
ASSERT_EQ(0, ::statfs(mountpoint.c_str(), &stfs)) << runner.cmdline();
EXPECT_EQ(stfs.f_files, 44) << runner.cmdline();
}
#endif

EXPECT_TRUE(runner.unmount()) << runner.cmdline();
}

Expand Down Expand Up @@ -904,7 +918,7 @@ TEST_P(tools_test, end_to_end) {
EXPECT_EQ(unix, (ec).value()) << runner.cmdline() << ": " << (ec).message()
#endif

TEST_P(tools_test, mutating_ops) {
TEST_P(tools_test, mutating_and_error_ops) {
auto mode = GetParam();

std::chrono::seconds const timeout{5};
Expand Down Expand Up @@ -1062,6 +1076,24 @@ TEST_P(tools_test, mutating_ops) {
EXPECT_EC_UNIX_WIN(ec, ENOSYS, ERROR_ACCESS_DENIED);
}

// read directory as file (non-mutating)

{
std::error_code ec;
std::string tmp;
EXPECT_FALSE(read_file(mountpoint / "empty", tmp, &ec))
<< "[" << tmp << "]";
EXPECT_EC_UNIX_WIN(ec, EISDIR, ERROR_FILE_NOT_FOUND);
}

// open file as directory (non-mutating)

{
std::error_code ec;
fs::directory_iterator it{mountpoint / "format.sh", ec};
EXPECT_EC_UNIX_WIN(ec, ENOTDIR, ERROR_FILE_NOT_FOUND);
}

EXPECT_TRUE(runner.unmount()) << runner.cmdline();
}
}
Expand Down

0 comments on commit c049713

Please sign in to comment.