From 7ad2a3477f39e2eac221c53e5f94954f481eb1b4 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 28 Apr 2014 16:43:58 +0200 Subject: Implement Popen() execv helper to avoid popen() --- test/libapt/fileutl_test.cc | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 643c02297..c2a43eda7 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -224,3 +224,51 @@ TEST(FileUtlTest, GetTempDir) 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"); +} -- cgit v1.2.3 From 53ac87ac9c27af39df062516aab5dce880af107a Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 28 Apr 2014 17:24:35 +0200 Subject: add flAbsPath() as a wrapper to realpath() --- test/libapt/fileutl_test.cc | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 643c02297..9c7e1630a 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -224,3 +224,10 @@ TEST(FileUtlTest, GetTempDir) if (old_tmpdir.empty() == false) setenv("TMPDIR", old_tmpdir.c_str(), 1); } +TEST(FileUtlTest, flAbsPath) +{ + int res = chdir("/bin/"); + EXPECT_EQ(res, 0); + std::string p = flAbsPath("ls"); + EXPECT_EQ(p, "/bin/ls"); +} -- cgit v1.2.3 From a111a024a30b805fc3f2b8a5b4db8d0c26c10fb8 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 28 Apr 2014 17:44:34 +0200 Subject: fix tests --- test/libapt/fileutl_test.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 9c7e1630a..15b57983b 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -226,8 +226,12 @@ TEST(FileUtlTest, GetTempDir) } 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"); + + res = chdir(cwd.c_str()); + EXPECT_EQ(res, 0); } -- cgit v1.2.3 From 0d303f1764645284b33924c9be8bf29f0a32ca5c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 20 Oct 2014 12:00:46 +0200 Subject: test if TMPDIR is accessible before using Private temporary directories as created by e.g. libpam-tmpdir are nice, but they are also very effective in preventing our priviledge dropping to work as TMPDIR will be set to a directory only root has access to, so working with it as _apt will fail. We circumvent this by extending our check for a usable TMPDIR setting by checking access rights. Closes: 765951 --- test/libapt/fileutl_test.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index cdf7ea479..8d47c5098 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -217,8 +217,16 @@ 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("/usr", GetTempDir()); + 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) -- cgit v1.2.3 From ad5ffe1ffb203eb58e5e25fe6f4c0dabc53253c9 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 5 Nov 2014 18:42:56 +0100 Subject: Division by result of sizeof(). memset() expects a size in bytes "did you intend to multiply instead?" is what cppcheck helpful says and it is absolutely right. Doesn't make a whole lot of a difference though as we are talking about 'char' in this testcase, but just to be sure. Reported-By: cppcheck Git-Dch: Ignore --- test/libapt/fileutl_test.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 8d47c5098..57d5bbdc2 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -53,11 +53,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]; \ - 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))); - + { + 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"; -- cgit v1.2.3 From 23fb7b2cbbda62c99a199d29ad62205b23d35af4 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 5 Nov 2014 18:56:12 +0100 Subject: (style) Variable 'res' is assigned a value that is never used Checking the return value of this (and many other calls) in this testcase is a good idea, so we do it now. Reported-By: cppcheck Git-Dch: Ignore --- test/libapt/fileutl_test.cc | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'test/libapt/fileutl_test.cc') diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 57d5bbdc2..a2c303768 100644 --- a/test/libapt/fileutl_test.cc +++ b/test/libapt/fileutl_test.cc @@ -252,37 +252,40 @@ 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) { -- cgit v1.2.3