From 1c0587856c9279910b21d99eb9ffa5a0e33ea19f Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Sun, 3 Apr 2022 11:58:12 +0100 Subject: [PATCH] [Minor] Add some tests --- src/libutil/cxx/locked_file.cxx | 71 +++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/libutil/cxx/locked_file.cxx b/src/libutil/cxx/locked_file.cxx index adac99468..cda05a6c3 100644 --- a/src/libutil/cxx/locked_file.cxx +++ b/src/libutil/cxx/locked_file.cxx @@ -167,4 +167,75 @@ raii_file_sink::raii_file_sink(raii_file_sink &&other) noexcept { } +namespace tests { +template +static auto test_read_file(const T& f) { + auto fd = f.get_fd(); + (void)::lseek(fd, 0, SEEK_SET); + std::string buf('\0', (std::size_t)f.get_size()); + ::read(fd, buf.data(), buf.size()); + return buf; } +template +static auto test_write_file(const T& f, const std::string_view &buf) { + auto fd = f.get_fd(); + (void)::lseek(fd, 0, SEEK_SET); + return ::write(fd, buf.data(), buf.size()); +} +auto random_fname() { + const auto *tmpdir = getenv("TMPDIR"); + if (tmpdir == nullptr) { + tmpdir = G_DIR_SEPARATOR_S "tmp"; + } + + std::string out_fname{tmpdir}; + out_fname += G_DIR_SEPARATOR_S; + + unsigned char hexbuf[32]; + rspamd_random_hex(hexbuf, sizeof(hexbuf)); + out_fname.append((const char *)hexbuf, sizeof(hexbuf)); + + return out_fname; +} +TEST_SUITE("loked files utils") { + +TEST_CASE("create and delete file") { + auto fname = random_fname(); + { + auto raii_locked_file = raii_locked_file::open(fname.c_str(), O_RDONLY); + CHECK(raii_locked_file.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + // File must be deleted after this call + CHECK(::access(fname.c_str(), R_OK) == -1); + CHECK(errno == ENOENT); + // Create one more time + { + auto raii_locked_file = raii_locked_file::open(fname.c_str(), O_RDONLY); + CHECK(raii_locked_file.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + CHECK(::access(fname.c_str(), R_OK) == -1); + CHECK(errno == ENOENT); +} + +TEST_CASE("check lock") { + auto fname = random_fname(); + { + auto raii_locked_file = raii_locked_file::open(fname.c_str(), O_RDONLY); + CHECK(raii_locked_file.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + auto raii_locked_file2 = raii_locked_file::open(fname.c_str(), O_RDONLY); + CHECK(!raii_locked_file2.has_value()); + CHECK(::access(fname.c_str(), R_OK) == 0); + } + // File must be deleted after this call + CHECK(::access(fname.c_str(), R_OK) == -1); + CHECK(errno == ENOENT); +} + +} // TEST_SUITE + +} // namespace tests + +} // namespace rspamd::util -- 2.39.5