]> git.saurik.com Git - apt.git/blobdiff - test/libapt/fileutl_test.cc
accept ../ on the cmdline as start for a deb file as well
[apt.git] / test / libapt / fileutl_test.cc
index 57d5bbdc2852e6f1700d5c0b534820abadda039d..2bd090e5e627858a64f938b1f14a0e14c0e4c309 100644 (file)
@@ -252,46 +252,79 @@ TEST(FileUtlTest, Popen)
 
    // 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);
+   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_EQ(res, true);
    EXPECT_STREQ(buf, "meepmeep\n");
 
    // wait for the child to exit and cleanup
-   ExecWait(Child, "PopenRead");
-   Fd.Close();
+   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
-   res = Popen(Args, Fd, Child, FileFd::ReadWrite);
-   EXPECT_EQ(res, false);
-   _error->Discard();
+   _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;
-   res = Popen(Args, Fd, Child, FileFd::WriteOnly);
+   EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly));
    s = "\n";
-   Fd.Write(s.c_str(), s.size());
-   Fd.Close();
-   ExecWait(Child, "PopenWrite");
+   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("/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);
+}