X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/f22b65b47990237bd5d9a1c171919c3059fbd9b0..eed65c79322c3c79facdea44ce39033b21972e36:/test/libapt/fileutl_test.cc?ds=sidebyside diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 1d1a1a1b8..cdf7ea479 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -2,84 +2,283 @@ #include #include +#include +#include #include #include #include -#include +#include -#include "assert.h" +#include -// regression test for permission bug LP: #1304657 -static bool -TestFileFdOpenPermissions(mode_t a_umask, mode_t ExpectedFilePermission) +#include "file-helpers.h" + +static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission, + unsigned int const filemode, APT::Configuration::Compressor const &compressor) { - FileFd f; - struct stat buf; - static const char* fname = "test.txt"; + std::string trace; + strprintf(trace, "TestFileFd: Compressor: %s umask: %#o permission: %#o mode: %d", compressor.Name.c_str(), a_umask, ExpectedFilePermission, filemode); + SCOPED_TRACE(trace); + + static const char* fname = "apt-filefd-test.txt"; + if (FileExists(fname) == true) + EXPECT_EQ(0, unlink(fname)); + FileFd f; umask(a_umask); - f.Open(fname, FileFd::ReadWrite|FileFd::Atomic); + EXPECT_TRUE(f.Open(fname, filemode, compressor)); + EXPECT_TRUE(f.IsOpen()); + EXPECT_FALSE(f.Failed()); + EXPECT_EQ(umask(a_umask), a_umask); + + 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(); - if (stat(fname, &buf) < 0) + EXPECT_FALSE(f.IsOpen()); + EXPECT_FALSE(f.Failed()); + + EXPECT_TRUE(f.Open(fname, FileFd::ReadOnly, compressor)); + EXPECT_TRUE(f.IsOpen()); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_NE(0, f.FileSize()); + EXPECT_FALSE(f.Failed()); + EXPECT_NE(0, f.ModificationTime()); + EXPECT_FALSE(f.Failed()); + + // ensure the memory is as predictably messed up +#define APT_INIT_READBACK \ + char readback[20]; \ + 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))); + + { + APT_INIT_READBACK + char const * const expect = "This"; + EXPECT_TRUE(f.Read(readback, strlen(expect))); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_N_STR(expect, readback); + EXPECT_EQ(strlen(expect), f.Tell()); + } + { + APT_INIT_READBACK + char const * const expect = "test!\n"; + EXPECT_TRUE(f.Skip((test.size() - f.Tell()) - strlen(expect))); + EXPECT_TRUE(f.Read(readback, strlen(expect))); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_N_STR(expect, readback); + EXPECT_EQ(test.size(), f.Tell()); + } { - _error->Errno("stat", "failed to stat"); - _error->DumpErrors(); - return false; + APT_INIT_READBACK + EXPECT_TRUE(f.Seek(0)); + EXPECT_FALSE(f.Eof()); + EXPECT_TRUE(f.Read(readback, 20, true)); + EXPECT_FALSE(f.Failed()); + EXPECT_TRUE(f.Eof()); + EXPECT_N_STR(test.c_str(), readback); + EXPECT_EQ(f.Size(), f.Tell()); } - unlink(fname); - equals(buf.st_mode & 0777, ExpectedFilePermission); - return true; + { + APT_INIT_READBACK + EXPECT_TRUE(f.Seek(0)); + EXPECT_FALSE(f.Eof()); + EXPECT_TRUE(f.Read(readback, test.size(), true)); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_N_STR(test.c_str(), readback); + EXPECT_EQ(f.Size(), f.Tell()); + } + { + APT_INIT_READBACK + EXPECT_TRUE(f.Seek(0)); + EXPECT_FALSE(f.Eof()); + unsigned long long actual; + EXPECT_TRUE(f.Read(readback, 20, &actual)); + EXPECT_FALSE(f.Failed()); + EXPECT_TRUE(f.Eof()); + EXPECT_EQ(test.size(), actual); + EXPECT_N_STR(test.c_str(), readback); + EXPECT_EQ(f.Size(), f.Tell()); + } + { + APT_INIT_READBACK + EXPECT_TRUE(f.Seek(0)); + EXPECT_FALSE(f.Eof()); + f.ReadLine(readback, 20); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_EQ(test, readback); + EXPECT_EQ(f.Size(), f.Tell()); + } + { + APT_INIT_READBACK + EXPECT_TRUE(f.Seek(0)); + EXPECT_FALSE(f.Eof()); + char const * const expect = "This"; + f.ReadLine(readback, strlen(expect) + 1); + EXPECT_FALSE(f.Failed()); + EXPECT_FALSE(f.Eof()); + EXPECT_N_STR(expect, readback); + EXPECT_EQ(strlen(expect), f.Tell()); + } +#undef APT_INIT_READBACK + + f.Close(); + EXPECT_FALSE(f.IsOpen()); + EXPECT_FALSE(f.Failed()); + + // regression test for permission bug LP: #1304657 + struct stat buf; + EXPECT_EQ(0, stat(fname, &buf)); + EXPECT_EQ(0, unlink(fname)); + EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777); } -int main() +static void TestFileFd(unsigned int const filemode) { - std::vector files; + 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)); - if (TestFileFdOpenPermissions(0002, 0664) == false || - TestFileFdOpenPermissions(0022, 0644) == false || - TestFileFdOpenPermissions(0077, 0600) == false || - TestFileFdOpenPermissions(0026, 0640) == false) + for (std::vector::const_iterator c = compressors.begin(); c != compressors.end(); ++c) { - return 1; + if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite && + (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); } +} + +TEST(FileUtlTest, FileFD) +{ + std::string const startdir = SafeGetCWD(); + EXPECT_FALSE(startdir.empty()); + std::string tempdir; + createTemporaryDirectory("filefd", tempdir); + EXPECT_EQ(0, chdir(tempdir.c_str())); + + TestFileFd(FileFd::WriteOnly | FileFd::Create); + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty); + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive); + TestFileFd(FileFd::WriteOnly | FileFd::Atomic); + TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic); + // short-hands for ReadWrite with these modes + TestFileFd(FileFd::WriteEmpty); + TestFileFd(FileFd::WriteAny); + TestFileFd(FileFd::WriteTemp); + TestFileFd(FileFd::WriteAtomic); + EXPECT_EQ(0, chdir(startdir.c_str())); + removeDirectory(tempdir); +} +TEST(FileUtlTest, Glob) +{ + std::vector files; // normal match - files = Glob("*.lst"); - if (files.size() != 1) - { - _error->DumpErrors(); - return 1; - } + files = Glob("*akefile"); + EXPECT_EQ(1, files.size()); // not there files = Glob("xxxyyyzzz"); - if (files.size() != 0 || _error->PendingError()) - { - _error->DumpErrors(); - return 1; - } + EXPECT_TRUE(files.empty()); + EXPECT_FALSE(_error->PendingError()); // many matches (number is a bit random) files = Glob("*.cc"); - if (files.size() < 10) - { - _error->DumpErrors(); - return 1; - } + EXPECT_LT(10, files.size()); +} +TEST(FileUtlTest, GetTempDir) +{ + char const * const envtmp = getenv("TMPDIR"); + std::string old_tmpdir; + if (envtmp != NULL) + old_tmpdir = envtmp; - // GetTempDir() unsetenv("TMPDIR"); - equals(GetTempDir(), "/tmp"); + EXPECT_EQ("/tmp", GetTempDir()); setenv("TMPDIR", "", 1); - equals(GetTempDir(), "/tmp"); + EXPECT_EQ("/tmp", GetTempDir()); setenv("TMPDIR", "/not-there-no-really-not", 1); - equals(GetTempDir(), "/tmp"); + EXPECT_EQ("/tmp", GetTempDir()); setenv("TMPDIR", "/usr", 1); - equals(GetTempDir(), "/usr"); + EXPECT_EQ("/usr", GetTempDir()); + + 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 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}; + bool res = Popen(Args, Fd, Child, FileFd::ReadOnly); + Fd.Read(buf, sizeof(buf)-1, &n); + buf[n] = 0; + EXPECT_NE(n, 0); + EXPECT_EQ(res, true); + EXPECT_STREQ(buf, "meepmeep\n"); + + // wait for the child to exit and cleanup + ExecWait(Child, "PopenRead"); + 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 + res = Popen(Args, Fd, Child, FileFd::ReadWrite); + EXPECT_EQ(res, false); + _error->Discard(); + + // write something + Args[0] = "/bin/bash"; + Args[1] = "-c"; + Args[2] = "read"; + Args[3] = NULL; + res = Popen(Args, Fd, Child, FileFd::WriteOnly); + s = "\n"; + Fd.Write(s.c_str(), s.size()); + Fd.Close(); + ExecWait(Child, "PopenWrite"); +} +TEST(FileUtlTest, flAbsPath) +{ + std::string cwd = SafeGetCWD(); + int res = chdir("/bin/"); + EXPECT_EQ(res, 0); + std::string p = flAbsPath("ls"); + EXPECT_EQ(p, "/bin/ls"); - return 0; + res = chdir(cwd.c_str()); + EXPECT_EQ(res, 0); }