From 851b6a097165af4d51c0db01b5e05256e5006896 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 11:00:48 +0200 Subject: Adding upstream version 2.6.1. Signed-off-by: Daniel Baumann --- test/libapt/file-helpers.cc | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 test/libapt/file-helpers.cc (limited to 'test/libapt/file-helpers.cc') diff --git a/test/libapt/file-helpers.cc b/test/libapt/file-helpers.cc new file mode 100644 index 0000000..bb7052b --- /dev/null +++ b/test/libapt/file-helpers.cc @@ -0,0 +1,85 @@ +#include + +#include + +#include + +#include +#include +#include +#include + +#include + +#include "file-helpers.h" + +void helperCreateTemporaryDirectory(std::string const &id, std::string &dir) +{ + std::string const strtempdir = GetTempDir().append("/apt-tests-").append(id).append(".XXXXXX"); + char * tempdir = strdup(strtempdir.c_str()); + ASSERT_STREQ(tempdir, mkdtemp(tempdir)); + dir = tempdir; + free(tempdir); +} +void helperRemoveDirectory(std::string const &dir) +{ + // basic sanity check to avoid removing random directories based on earlier failures + if (dir.find("/apt-tests-") == std::string::npos || dir.find_first_of("*?") != std::string::npos) + FAIL() << "Directory '" << dir << "' seems invalid. It is therefore not removed!"; + else + ASSERT_EQ(0, system(std::string("rm -rf ").append(dir).c_str())); +} +void helperCreateFile(std::string const &dir, std::string const &name) +{ + std::string file = dir; + file.append("/"); + file.append(name); + int const fd = creat(file.c_str(), 0600); + ASSERT_NE(-1, fd); + close(fd); +} +void helperCreateDirectory(std::string const &dir, std::string const &name) +{ + std::string file = dir; + file.append("/"); + file.append(name); + ASSERT_TRUE(CreateDirectory(dir, file)); +} +void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname) +{ + std::string target = dir; + target.append("/"); + target.append(targetname); + std::string link = dir; + link.append("/"); + link.append(linkname); + ASSERT_EQ(0, symlink(target.c_str(), link.c_str())); +} + +void openTemporaryFile(std::string const &id, FileFd &fd, char const * const content, bool const ImmediateUnlink) +{ + EXPECT_NE(nullptr, GetTempFile("apt-" + id, ImmediateUnlink, &fd)); + EXPECT_TRUE(ImmediateUnlink || not fd.Name().empty()); + if (content != nullptr) + { + EXPECT_TRUE(fd.Write(content, strlen(content))); + EXPECT_TRUE(fd.Sync()); + fd.Seek(0); + } +} +ScopedFileDeleter::ScopedFileDeleter(std::string const &filename) : _filename{filename} {} +ScopedFileDeleter::ScopedFileDeleter(ScopedFileDeleter &&sfd) = default; +ScopedFileDeleter& ScopedFileDeleter::operator=(ScopedFileDeleter &&sfd) = default; +ScopedFileDeleter::~ScopedFileDeleter() { + if (not _filename.empty()) + RemoveFile("ScopedFileDeleter", _filename.c_str()); +} +[[nodiscard]] ScopedFileDeleter createTemporaryFile(std::string const &id, char const * const content) +{ + FileFd fd; + openTemporaryFile(id, fd, content, false); + EXPECT_TRUE(fd.IsOpen()); + EXPECT_TRUE(fd.Close()); + EXPECT_FALSE(fd.Name().empty()); + return ScopedFileDeleter{fd.Name()}; +} -- cgit v1.2.3