]> git.saurik.com Git - apt.git/blobdiff - test/libapt/fileutl_test.cc
show the conflicting distribution warning again
[apt.git] / test / libapt / fileutl_test.cc
index 643c02297fbc2e8851775cceb84fb36582beea9f..67bd850a123811149d72ab7c5b1c919fac1ffeea 100644 (file)
@@ -4,7 +4,9 @@
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/aptconfiguration.h>
 #include <apt-pkg/fileutl.h>
 #include <apt-pkg/strutl.h>
 #include <apt-pkg/aptconfiguration.h>
+#include <apt-pkg/configuration.h>
 
 
+#include <algorithm>
 #include <string>
 #include <vector>
 #include <stdlib.h>
 #include <string>
 #include <vector>
 #include <stdlib.h>
@@ -53,11 +55,16 @@ static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission
    // ensure the memory is as predictably messed up
 #define APT_INIT_READBACK \
    char readback[20]; \
    // ensure the memory is as predictably messed up
 #define APT_INIT_READBACK \
    char readback[20]; \
-   memset(readback, 'D', sizeof(readback)/sizeof(readback[0])); \
+   memset(readback, 'D', sizeof(readback)*sizeof(readback[0])); \
    readback[19] = '\0';
 #define EXPECT_N_STR(expect, actual) \
    EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
    readback[19] = '\0';
 #define EXPECT_N_STR(expect, actual) \
    EXPECT_EQ(0, strncmp(expect, actual, strlen(expect)));
-
+   {
+      APT_INIT_READBACK
+      char const * const expect = "DDDDDDDDDDDDDDDDDDD";
+      EXPECT_STREQ(expect,readback);
+      EXPECT_N_STR(expect, readback);
+   }
    {
       APT_INIT_READBACK
       char const * const expect = "This";
    {
       APT_INIT_READBACK
       char const * const expect = "This";
@@ -77,6 +84,18 @@ static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission
       EXPECT_N_STR(expect, readback);
       EXPECT_EQ(test.size(), f.Tell());
    }
       EXPECT_N_STR(expect, readback);
       EXPECT_EQ(test.size(), f.Tell());
    }
+   // Non-zero backwards seek
+   {
+      APT_INIT_READBACK
+      char const * const expect = "is";
+      EXPECT_EQ(test.size(), f.Tell());
+      EXPECT_TRUE(f.Seek(5));
+      EXPECT_TRUE(f.Read(readback, strlen(expect)));
+      EXPECT_FALSE(f.Failed());
+      EXPECT_FALSE(f.Eof());
+      EXPECT_N_STR(expect, readback);
+      EXPECT_EQ(7, f.Tell());
+   }
    {
       APT_INIT_READBACK
       EXPECT_TRUE(f.Seek(0));
    {
       APT_INIT_READBACK
       EXPECT_TRUE(f.Seek(0));
@@ -145,26 +164,34 @@ static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission
 
 static void TestFileFd(unsigned int const filemode)
 {
 
 static void TestFileFd(unsigned int const filemode)
 {
-   std::vector<APT::Configuration::Compressor> 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<APT::Configuration::Compressor>::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 &&
    {
       if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
-           (c->Name.empty() != true && c->Binary.empty() != true))
+           (c.Name.empty() != true && c.Binary.empty() != true))
         continue;
         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)
 {
 }
 
 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;
    std::string const startdir = SafeGetCWD();
    EXPECT_FALSE(startdir.empty());
    std::string tempdir;
@@ -189,7 +216,7 @@ TEST(FileUtlTest, Glob)
 {
    std::vector<std::string> files;
    // normal match
 {
    std::vector<std::string> files;
    // normal match
-   files = Glob("*akefile");
+   files = Glob("*MakeLists.txt");
    EXPECT_EQ(1, files.size());
 
    // not there
    EXPECT_EQ(1, files.size());
 
    // not there
@@ -217,10 +244,148 @@ TEST(FileUtlTest, GetTempDir)
    setenv("TMPDIR", "/not-there-no-really-not", 1);
    EXPECT_EQ("/tmp", GetTempDir());
 
    setenv("TMPDIR", "/not-there-no-really-not", 1);
    EXPECT_EQ("/tmp", GetTempDir());
 
-   setenv("TMPDIR", "/usr", 1);
-   EXPECT_EQ("/usr", GetTempDir());
+   // root can access everything, so /usr will be accepted
+   if (geteuid() != 0)
+   {
+       // here but not accessible for non-roots
+       setenv("TMPDIR", "/usr", 1);
+       EXPECT_EQ("/tmp", GetTempDir());
+   }
+
+   // files are no good for tmpdirs, too
+   setenv("TMPDIR", "/dev/null", 1);
+   EXPECT_EQ("/tmp", GetTempDir());
+
+   setenv("TMPDIR", "/var/tmp", 1);
+   EXPECT_EQ("/var/tmp", GetTempDir());
 
    unsetenv("TMPDIR");
    if (old_tmpdir.empty() == false)
       setenv("TMPDIR", old_tmpdir.c_str(), 1);
 }
 
    unsetenv("TMPDIR");
    if (old_tmpdir.empty() == false)
       setenv("TMPDIR", old_tmpdir.c_str(), 1);
 }
+TEST(FileUtlTest, Popen)
+{
+   FileFd Fd;
+   pid_t Child;
+   char buf[1024];
+   std::string s;
+   unsigned long long n = 0;
+   std::vector<std::string> OpenFds;
+
+   // count Fds to ensure we don't have a resource leak
+   if(FileExists("/proc/self/fd"))
+      OpenFds = Glob("/proc/self/fd/*");
+
+   // output something
+   const char* Args[10] = {"/bin/echo", "meepmeep", NULL};
+   EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::ReadOnly));
+   EXPECT_TRUE(Fd.Read(buf, sizeof(buf)-1, &n));
+   buf[n] = 0;
+   EXPECT_NE(n, 0);
+   EXPECT_STREQ(buf, "meepmeep\n");
+
+   // wait for the child to exit and cleanup
+   EXPECT_TRUE(ExecWait(Child, "PopenRead"));
+   EXPECT_TRUE(Fd.Close());
+
+   // ensure that after a close all is good again
+   if(FileExists("/proc/self/fd"))
+      EXPECT_EQ(Glob("/proc/self/fd/*").size(), OpenFds.size());
+
+   // ReadWrite is not supported
+   _error->PushToStack();
+   EXPECT_FALSE(Popen(Args, Fd, Child, FileFd::ReadWrite));
+   EXPECT_FALSE(Fd.IsOpen());
+   EXPECT_FALSE(Fd.Failed());
+   EXPECT_TRUE(_error->PendingError());
+   _error->RevertToStack();
+
+   // write something
+   Args[0] = "/bin/bash";
+   Args[1] = "-c";
+   Args[2] = "read";
+   Args[3] = NULL;
+   EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly));
+   s = "\n";
+   EXPECT_TRUE(Fd.Write(s.c_str(), s.length()));
+   EXPECT_TRUE(Fd.Close());
+   EXPECT_FALSE(Fd.IsOpen());
+   EXPECT_FALSE(Fd.Failed());
+   EXPECT_TRUE(ExecWait(Child, "PopenWrite"));
+}
+TEST(FileUtlTest, flAbsPath)
+{
+   std::string cwd = SafeGetCWD();
+   int res = chdir("/etc/");
+   EXPECT_EQ(res, 0);
+   std::string p = flAbsPath("passwd");
+   EXPECT_EQ(p, "/etc/passwd");
+
+   res = chdir(cwd.c_str());
+   EXPECT_EQ(res, 0);
+}
+
+static void TestDevNullFileFd(unsigned int const filemode)
+{
+   SCOPED_TRACE(filemode);
+   FileFd f("/dev/null", filemode);
+   EXPECT_FALSE(f.Failed());
+   EXPECT_TRUE(f.IsOpen());
+   EXPECT_TRUE(f.IsOpen());
+
+   std::string test = "This is a test!\n";
+   EXPECT_TRUE(f.Write(test.c_str(), test.size()));
+   EXPECT_TRUE(f.IsOpen());
+   EXPECT_FALSE(f.Failed());
+
+   f.Close();
+   EXPECT_FALSE(f.IsOpen());
+   EXPECT_FALSE(f.Failed());
+}
+TEST(FileUtlTest, WorkingWithDevNull)
+{
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Atomic);
+   TestDevNullFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic);
+   // short-hands for ReadWrite with these modes
+   TestDevNullFileFd(FileFd::WriteEmpty);
+   TestDevNullFileFd(FileFd::WriteAny);
+   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());
+}