X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/5fafaf27a5f066dc15d96c03ef154fd1d59eb891..99fdd8034b4a5cdb0100a33d0b3d5e26079c1695:/test/libapt/fileutl_test.cc diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 607c4a195..67bd850a1 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -4,7 +4,9 @@ #include #include #include +#include +#include #include #include #include @@ -162,26 +164,34 @@ static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission static void TestFileFd(unsigned int const filemode) { - std::vector compressors = APT::Configuration::getCompressors(); - - // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries - compressors.push_back(APT::Configuration::Compressor("rev", ".reversed", "rev", NULL, NULL, 42)); - //compressors.push_back(APT::Configuration::Compressor("cat", ".ident", "cat", NULL, NULL, 42)); - - for (std::vector::const_iterator c = compressors.begin(); c != compressors.end(); ++c) + auto const compressors = APT::Configuration::getCompressors(); + EXPECT_EQ(7, compressors.size()); + bool atLeastOneWasTested = false; + for (auto const &c: compressors) { if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite && - (c->Name.empty() != true && c->Binary.empty() != true)) + (c.Name.empty() != true && c.Binary.empty() != true)) continue; - TestFileFd(0002, 0664, filemode, *c); - TestFileFd(0022, 0644, filemode, *c); - TestFileFd(0077, 0600, filemode, *c); - TestFileFd(0026, 0640, filemode, *c); + atLeastOneWasTested = true; + TestFileFd(0002, 0664, filemode, c); + TestFileFd(0022, 0644, filemode, c); + TestFileFd(0077, 0600, filemode, c); + TestFileFd(0026, 0640, filemode, c); } + EXPECT_TRUE(atLeastOneWasTested); } TEST(FileUtlTest, FileFD) { + // testing the (un)compress via pipe, as the 'real' compressors are usually built in via libraries + _config->Set("APT::Compressor::rev::Name", "rev"); + _config->Set("APT::Compressor::rev::Extension", ".reversed"); + _config->Set("APT::Compressor::rev::Binary", "rev"); + _config->Set("APT::Compressor::rev::Cost", 10); + auto const compressors = APT::Configuration::getCompressors(false); + EXPECT_EQ(7, compressors.size()); + EXPECT_TRUE(std::any_of(compressors.begin(), compressors.end(), [](APT::Configuration::Compressor const &c) { return c.Name == "rev"; })); + std::string const startdir = SafeGetCWD(); EXPECT_FALSE(startdir.empty()); std::string tempdir; @@ -206,7 +216,7 @@ TEST(FileUtlTest, Glob) { std::vector files; // normal match - files = Glob("*akefile"); + files = Glob("*MakeLists.txt"); EXPECT_EQ(1, files.size()); // not there @@ -317,6 +327,7 @@ TEST(FileUtlTest, flAbsPath) static void TestDevNullFileFd(unsigned int const filemode) { + SCOPED_TRACE(filemode); FileFd f("/dev/null", filemode); EXPECT_FALSE(f.Failed()); EXPECT_TRUE(f.IsOpen()); @@ -344,3 +355,37 @@ TEST(FileUtlTest, WorkingWithDevNull) TestDevNullFileFd(FileFd::WriteTemp); TestDevNullFileFd(FileFd::WriteAtomic); } +constexpr char const * const TESTSTRING = "This is a test"; +static void TestFailingAtomicKeepsFile(char const * const label, std::string const &filename) +{ + SCOPED_TRACE(label); + EXPECT_TRUE(FileExists(filename)); + FileFd fd; + EXPECT_TRUE(fd.Open(filename, FileFd::ReadOnly)); + char buffer[50]; + EXPECT_NE(nullptr, fd.ReadLine(buffer, sizeof(buffer))); + EXPECT_STREQ(TESTSTRING, buffer); +} +TEST(FileUtlTest, FailingAtomic) +{ + FileFd fd; + std::string filename; + createTemporaryFile("failingatomic", fd, &filename, TESTSTRING); + TestFailingAtomicKeepsFile("init", filename); + + FileFd f; + EXPECT_TRUE(f.Open(filename, FileFd::ReadWrite | FileFd::Atomic)); + f.EraseOnFailure(); + EXPECT_FALSE(f.Failed()); + EXPECT_TRUE(f.IsOpen()); + TestFailingAtomicKeepsFile("before-fail", filename); + EXPECT_TRUE(f.Write("Bad file write", 10)); + f.OpFail(); + EXPECT_TRUE(f.Failed()); + TestFailingAtomicKeepsFile("after-fail", filename); + EXPECT_TRUE(f.Close()); + TestFailingAtomicKeepsFile("closed", filename); + + if (filename.empty() == false) + unlink(filename.c_str()); +}