]> git.saurik.com Git - apt.git/blobdiff - test/libapt/fileutl_test.cc
properly format multiline error messages
[apt.git] / test / libapt / fileutl_test.cc
index a2c303768e4c6af042e3ea241610f0c52c844c73..607c4a195bbae2361267d96e11b1a32c6864c67e 100644 (file)
@@ -82,6 +82,18 @@ static void TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission
       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));
@@ -222,9 +234,13 @@ TEST(FileUtlTest, GetTempDir)
    setenv("TMPDIR", "/not-there-no-really-not", 1);
    EXPECT_EQ("/tmp", GetTempDir());
 
-   // here but not accessible for non-roots
-   setenv("TMPDIR", "/usr", 1);
-   EXPECT_EQ("/tmp", 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);
@@ -290,11 +306,41 @@ TEST(FileUtlTest, Popen)
 TEST(FileUtlTest, flAbsPath)
 {
    std::string cwd = SafeGetCWD();
-   int res = chdir("/bin/");
+   int res = chdir("/etc/");
    EXPECT_EQ(res, 0);
-   std::string p = flAbsPath("ls");
-   EXPECT_EQ(p, "/bin/ls");
+   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)
+{
+   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);
+}