summaryrefslogtreecommitdiff
path: root/test/libapt/fileutl_test.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/fileutl_test.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/fileutl_test.cc')
-rw-r--r--test/libapt/fileutl_test.cc272
1 files changed, 131 insertions, 141 deletions
diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc
index f3a3dd08e..643c02297 100644
--- a/test/libapt/fileutl_test.cc
+++ b/test/libapt/fileutl_test.cc
@@ -2,151 +2,148 @@
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>
+#include <apt-pkg/strutl.h>
#include <apt-pkg/aptconfiguration.h>
#include <string>
#include <vector>
#include <stdlib.h>
-#include <sys/stat.h>
#include <string.h>
-#include "assert.h"
+#include <gtest/gtest.h>
-static void assertStringEquals(char const * const expect, char const * const got, unsigned long const line) {
- if (strncmp(expect, got, strlen(expect)) == 0)
- return;
- OutputAssertEqual(expect, "==", got, line);
-}
-#define strequals(x,y) assertStringEquals(x, y, __LINE__)
+#include "file-helpers.h"
-static bool
-TestFileFd(mode_t const a_umask, mode_t const ExpectedFilePermission, unsigned int const filemode, APT::Configuration::Compressor const &compressor)
+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;
+ 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)
- equals(unlink(fname), 0);
+ EXPECT_EQ(0, unlink(fname));
+ FileFd f;
umask(a_umask);
- equals(f.Open(fname, filemode, compressor), true);
- equals(f.IsOpen(), true);
- equals(f.Failed(), false);
- equals(umask(a_umask), a_umask);
+ 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";
- equals(f.Write(test.c_str(), test.size()), true);
- equals(f.IsOpen(), true);
- equals(f.Failed(), false);
+ EXPECT_TRUE(f.Write(test.c_str(), test.size()));
+ EXPECT_TRUE(f.IsOpen());
+ EXPECT_FALSE(f.Failed());
f.Close();
- equals(f.IsOpen(), false);
- equals(f.Failed(), false);
-
- equals(f.Open(fname, FileFd::ReadOnly, compressor), true);
- equals(f.IsOpen(), true);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- equalsNot(f.FileSize(), 0);
- equals(f.Failed(), false);
- equalsNot(f.ModificationTime(), 0);
- equals(f.Failed(), false);
+ 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 \
+#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";
- equals(f.Read(readback, strlen(expect)), true);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- strequals(expect, readback);
- equals(strlen(expect), f.Tell());
+ 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";
- equals(f.Skip((test.size() - f.Tell()) - strlen(expect)), true);
- equals(f.Read(readback, strlen(expect)), true);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- strequals(expect, readback);
- equals(test.size(), f.Tell());
+ 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());
}
{
APT_INIT_READBACK
- equals(f.Seek(0), true);
- equals(f.Eof(), false);
- equals(f.Read(readback, 20, true), true);
- equals(f.Failed(), false);
- equals(f.Eof(), true);
- strequals(test.c_str(), readback);
- equals(f.Size(), f.Tell());
+ 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());
}
{
APT_INIT_READBACK
- equals(f.Seek(0), true);
- equals(f.Eof(), false);
- equals(f.Read(readback, test.size(), true), true);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- strequals(test.c_str(), readback);
- equals(f.Size(), f.Tell());
+ 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
- equals(f.Seek(0), true);
- equals(f.Eof(), false);
+ EXPECT_TRUE(f.Seek(0));
+ EXPECT_FALSE(f.Eof());
unsigned long long actual;
- equals(f.Read(readback, 20, &actual), true);
- equals(f.Failed(), false);
- equals(f.Eof(), true);
- equals(test.size(), actual);
- strequals(test.c_str(), readback);
- equals(f.Size(), f.Tell());
+ 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
- equals(f.Seek(0), true);
- equals(f.Eof(), false);
+ EXPECT_TRUE(f.Seek(0));
+ EXPECT_FALSE(f.Eof());
f.ReadLine(readback, 20);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- equals(test, readback);
- equals(f.Size(), f.Tell());
+ EXPECT_FALSE(f.Failed());
+ EXPECT_FALSE(f.Eof());
+ EXPECT_EQ(test, readback);
+ EXPECT_EQ(f.Size(), f.Tell());
}
{
APT_INIT_READBACK
- equals(f.Seek(0), true);
- equals(f.Eof(), false);
+ EXPECT_TRUE(f.Seek(0));
+ EXPECT_FALSE(f.Eof());
char const * const expect = "This";
f.ReadLine(readback, strlen(expect) + 1);
- equals(f.Failed(), false);
- equals(f.Eof(), false);
- strequals(expect, readback);
- equals(strlen(expect), f.Tell());
+ 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();
- equals(f.IsOpen(), false);
- equals(f.Failed(), false);
+ EXPECT_FALSE(f.IsOpen());
+ EXPECT_FALSE(f.Failed());
// regression test for permission bug LP: #1304657
- if (stat(fname, &buf) < 0)
- {
- _error->Errno("stat", "failed to stat");
- return false;
- }
- equals(unlink(fname), 0);
- equals(buf.st_mode & 0777, ExpectedFilePermission);
- return true;
+ struct stat buf;
+ EXPECT_EQ(0, stat(fname, &buf));
+ EXPECT_EQ(0, unlink(fname));
+ EXPECT_EQ(ExpectedFilePermission, buf.st_mode & 0777);
}
-static bool TestFileFd(unsigned int const filemode)
+static void TestFileFd(unsigned int const filemode)
{
std::vector<APT::Configuration::Compressor> compressors = APT::Configuration::getCompressors();
@@ -159,78 +156,71 @@ static bool TestFileFd(unsigned int const filemode)
if ((filemode & FileFd::ReadWrite) == FileFd::ReadWrite &&
(c->Name.empty() != true && c->Binary.empty() != true))
continue;
- if (TestFileFd(0002, 0664, filemode, *c) == false ||
- TestFileFd(0022, 0644, filemode, *c) == false ||
- TestFileFd(0077, 0600, filemode, *c) == false ||
- TestFileFd(0026, 0640, filemode, *c) == false)
- {
- _error->DumpErrors();
- return false;
- }
+ TestFileFd(0002, 0664, filemode, *c);
+ TestFileFd(0022, 0644, filemode, *c);
+ TestFileFd(0077, 0600, filemode, *c);
+ TestFileFd(0026, 0640, filemode, *c);
}
- return true;
}
-int main(int const argc, char const * const * const argv)
+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::string startdir;
- if (argc > 1 && DirectoryExists(argv[1]) == true) {
- startdir = SafeGetCWD();
- equals(chdir(argv[1]), 0);
- }
- if (TestFileFd(FileFd::WriteOnly | FileFd::Create) == false ||
- TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Empty) == false ||
- TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive) == false ||
- TestFileFd(FileFd::WriteOnly | FileFd::Atomic) == false ||
- TestFileFd(FileFd::WriteOnly | FileFd::Create | FileFd::Atomic) == false ||
- // short-hands for ReadWrite with these modes
- TestFileFd(FileFd::WriteEmpty) == false ||
- TestFileFd(FileFd::WriteAny) == false ||
- TestFileFd(FileFd::WriteTemp) == false ||
- TestFileFd(FileFd::WriteAtomic) == false)
- {
- return 1;
- }
- if (startdir.empty() == false)
- equals(chdir(startdir.c_str()), 0);
-
std::vector<std::string> 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());
- return 0;
+ unsetenv("TMPDIR");
+ if (old_tmpdir.empty() == false)
+ setenv("TMPDIR", old_tmpdir.c_str(), 1);
}