summaryrefslogtreecommitdiff
path: root/test/libapt/file-helpers.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2014-04-16 17:09:37 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2014-04-16 18:36:14 +0200
commitf00832cc273e52a47fb88e49849891b771de4e17 (patch)
treeeedd6b1e1c873c7e3e8f614a0ac8ca5c3b7e37b9 /test/libapt/file-helpers.cc
parentbb93178b8b5c2f8021977dbc34066f0d0fb8b9b9 (diff)
use Google C++ Testing Framework for libapt tests
My commit 45df0ad2 from 26. Nov 2009 had a little remark: "The commit also includes a very very simple testapp." This was never intended to be permanent, but as usually… The commit adds the needed make magic to compile gtest statically as it is required and links it against a small runner. All previous testcase binaries are reimplemented in gtest and combined in this runner. While most code is a 1:1 translation some had to be rewritten like compareversion_test.cc, but the coverage remains the same.
Diffstat (limited to 'test/libapt/file-helpers.cc')
-rw-r--r--test/libapt/file-helpers.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/test/libapt/file-helpers.cc b/test/libapt/file-helpers.cc
new file mode 100644
index 000000000..5edb9a9fe
--- /dev/null
+++ b/test/libapt/file-helpers.cc
@@ -0,0 +1,77 @@
+#include <apt-pkg/fileutl.h>
+
+#include <string>
+
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+
+#include <gtest/gtest.h>
+
+#include "file-helpers.h"
+
+void helperCreateTemporaryDirectory(std::string const &id, std::string &dir)
+{
+ std::string const strtempdir = GetTempDir().append("/apt-tests-").append(id).append(".XXXXXX");
+ char * tempdir = strdup(strtempdir.c_str());
+ ASSERT_STREQ(tempdir, mkdtemp(tempdir));
+ dir = tempdir;
+ free(tempdir);
+}
+void helperRemoveDirectory(std::string const &dir)
+{
+ // basic sanity check to avoid removing random directories based on earlier failures
+ if (dir.find("/apt-tests-") == std::string::npos || dir.find_first_of("*?") != std::string::npos)
+ FAIL() << "Directory '" << dir << "' seems invalid. It is therefore not removed!";
+ else
+ ASSERT_EQ(0, system(std::string("rm -rf ").append(dir).c_str()));
+}
+void helperCreateFile(std::string const &dir, std::string const &name)
+{
+ std::string file = dir;
+ file.append("/");
+ file.append(name);
+ int const fd = creat(file.c_str(), 0600);
+ ASSERT_NE(-1, fd);
+ close(fd);
+}
+void helperCreateDirectory(std::string const &dir, std::string const &name)
+{
+ std::string file = dir;
+ file.append("/");
+ file.append(name);
+ ASSERT_TRUE(CreateDirectory(dir, file));
+}
+void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname)
+{
+ std::string target = dir;
+ target.append("/");
+ target.append(targetname);
+ std::string link = dir;
+ link.append("/");
+ link.append(linkname);
+ ASSERT_EQ(0, symlink(target.c_str(), link.c_str()));
+}
+void helperCreateTemporaryFile(std::string const &id, FileFd &fd, char * * const filename, char const * const content)
+{
+ std::string name("apt-test-");
+ name.append(id).append(".XXXXXXXX");
+ char * tempfile = strdup(name.c_str());
+ int tempfile_fd = mkstemp(tempfile);
+ ASSERT_NE(-1, tempfile_fd);
+ if (filename != NULL)
+ *filename = tempfile;
+ else
+ {
+ unlink(tempfile);
+ free(tempfile);
+ }
+
+ EXPECT_TRUE(fd.OpenDescriptor(tempfile_fd, FileFd::ReadWrite));
+ if (content != NULL)
+ {
+ ASSERT_TRUE(fd.Write(content, strlen(content)));
+ fd.Seek(0);
+ }
+}