diff options
Diffstat (limited to 'test/libapt')
-rw-r--r-- | test/libapt/acqprogress_test.cc | 170 | ||||
-rw-r--r-- | test/libapt/cdrom_test.cc | 5 | ||||
-rw-r--r-- | test/libapt/commandline_test.cc | 97 | ||||
-rw-r--r-- | test/libapt/fileutl_test.cc | 80 | ||||
-rw-r--r-- | test/libapt/hashsums_test.cc | 188 | ||||
-rw-r--r-- | test/libapt/indexcopytosourcelist_test.cc | 2 | ||||
-rw-r--r-- | test/libapt/makefile | 14 | ||||
-rw-r--r-- | test/libapt/parsedepends_test.cc | 33 | ||||
-rw-r--r-- | test/libapt/sourcelist_test.cc | 2 | ||||
-rw-r--r-- | test/libapt/strutil_test.cc | 165 | ||||
-rw-r--r-- | test/libapt/tagfile_test.cc | 185 | ||||
-rw-r--r-- | test/libapt/tagsection_test.cc | 270 | ||||
-rw-r--r-- | test/libapt/uri_test.cc | 12 |
13 files changed, 1182 insertions, 41 deletions
diff --git a/test/libapt/acqprogress_test.cc b/test/libapt/acqprogress_test.cc new file mode 100644 index 000000000..288e05aca --- /dev/null +++ b/test/libapt/acqprogress_test.cc @@ -0,0 +1,170 @@ +#include <config.h> +#include <apt-pkg/acquire.h> +#include <apt-pkg/acquire-item.h> +#include <apt-pkg/configuration.h> +#include <apt-private/acqprogress.h> +#include <string> +#include <sstream> +#include <gtest/gtest.h> + +class TestItem: public pkgAcquire::Item +{ +public: + TestItem(pkgAcquire * const Acq) : pkgAcquire::Item(Acq, "", NULL) {} + + virtual std::string DescURI() { return ""; } + +}; + +TEST(AcqProgress, IMSHit) +{ + std::ostringstream out; + unsigned int width = 80; + AcqTextStatus Stat(out, width, 0); + Stat.Start(); + + pkgAcquire::ItemDesc hit; + hit.URI = "http://example.org/file"; + hit.Description = "Example File from example.org"; + hit.ShortDesc = "Example File"; + hit.Owner = NULL; + + EXPECT_EQ("", out.str()); + Stat.IMSHit(hit); + EXPECT_EQ("Hit Example File from example.org\n", out.str()); + Stat.IMSHit(hit); + EXPECT_EQ("Hit Example File from example.org\n" + "Hit Example File from example.org\n", out.str()); + Stat.Stop(); + EXPECT_EQ("Hit Example File from example.org\n" + "Hit Example File from example.org\n", out.str()); +} +TEST(AcqProgress, FetchNoFileSize) +{ + std::ostringstream out; + unsigned int width = 80; + AcqTextStatus Stat(out, width, 0); + Stat.Start(); + + pkgAcquire Acq(&Stat); + pkgAcquire::ItemDesc fetch; + fetch.URI = "http://example.org/file"; + fetch.Description = "Example File from example.org"; + fetch.ShortDesc = "Example File"; + TestItem fetchO(&Acq); + fetch.Owner = &fetchO; + + EXPECT_EQ("", out.str()); + Stat.Fetch(fetch); + EXPECT_EQ("Get:1 Example File from example.org\n", out.str()); + Stat.Fetch(fetch); + EXPECT_EQ("Get:1 Example File from example.org\n" + "Get:2 Example File from example.org\n", out.str()); + Stat.Stop(); + EXPECT_EQ("Get:1 Example File from example.org\n" + "Get:2 Example File from example.org\n", out.str()); +} +TEST(AcqProgress, FetchFileSize) +{ + std::ostringstream out; + unsigned int width = 80; + AcqTextStatus Stat(out, width, 0); + Stat.Start(); + + pkgAcquire Acq(&Stat); + pkgAcquire::ItemDesc fetch; + fetch.URI = "http://example.org/file"; + fetch.Description = "Example File from example.org"; + fetch.ShortDesc = "Example File"; + TestItem fetchO(&Acq); + fetchO.FileSize = 100; + fetch.Owner = &fetchO; + + EXPECT_EQ("", out.str()); + Stat.Fetch(fetch); + EXPECT_EQ("Get:1 Example File from example.org [100 B]\n", out.str()); + fetchO.FileSize = 42; + Stat.Fetch(fetch); + EXPECT_EQ("Get:1 Example File from example.org [100 B]\n" + "Get:2 Example File from example.org [42 B]\n", out.str()); + Stat.Stop(); + EXPECT_EQ("Get:1 Example File from example.org [100 B]\n" + "Get:2 Example File from example.org [42 B]\n", out.str()); +} +TEST(AcqProgress, Fail) +{ + std::ostringstream out; + unsigned int width = 80; + AcqTextStatus Stat(out, width, 0); + Stat.Start(); + + pkgAcquire Acq(&Stat); + pkgAcquire::ItemDesc fetch; + fetch.URI = "http://example.org/file"; + fetch.Description = "Example File from example.org"; + fetch.ShortDesc = "Example File"; + TestItem fetchO(&Acq); + fetchO.FileSize = 100; + fetchO.Status = pkgAcquire::Item::StatIdle; + fetch.Owner = &fetchO; + + EXPECT_EQ("", out.str()); + Stat.Fail(fetch); + EXPECT_EQ("", out.str()); + fetchO.Status = pkgAcquire::Item::StatDone; + Stat.Fail(fetch); + EXPECT_EQ("Ign Example File from example.org\n", out.str()); + fetchO.Status = pkgAcquire::Item::StatError; + fetchO.ErrorText = "An error test!"; + Stat.Fail(fetch); + EXPECT_EQ("Ign Example File from example.org\n" + "Err Example File from example.org\n" + " An error test!\n", out.str()); + _config->Set("Acquire::Progress::Ignore::ShowErrorText", true); + fetchO.Status = pkgAcquire::Item::StatDone; + Stat.Fail(fetch); + EXPECT_EQ("Ign Example File from example.org\n" + "Err Example File from example.org\n" + " An error test!\n" + "Ign Example File from example.org\n" + " An error test!\n", out.str()); + _config->Set("Acquire::Progress::Ignore::ShowErrorText", true); + Stat.Stop(); + EXPECT_EQ("Ign Example File from example.org\n" + "Err Example File from example.org\n" + " An error test!\n" + "Ign Example File from example.org\n" + " An error test!\n", out.str()); +} +TEST(AcqProgress, Pulse) +{ + std::ostringstream out; + unsigned int width = 80; + AcqTextStatus Stat(out, width, 0); + _config->Set("APT::Sandbox::User", ""); // ensure we aren't sandboxing + + pkgAcquire Acq(&Stat); + pkgAcquire::ItemDesc fetch; + fetch.URI = "http://example.org/file"; + fetch.Description = "Example File from example.org"; + fetch.ShortDesc = "Example File"; + TestItem fetchO(&Acq); + fetchO.FileSize = 100; + fetchO.Status = pkgAcquire::Item::StatFetching; + fetch.Owner = &fetchO; + + // make screen smaller and bigger again while running + EXPECT_TRUE(Stat.Pulse(&Acq)); + EXPECT_EQ("\r0% [Working]", out.str()); + width = 8; + EXPECT_TRUE(Stat.Pulse(&Acq)); + EXPECT_EQ("\r0% [Working]" + "\r " + "\r0% [Work", out.str()); + width = 80; + EXPECT_TRUE(Stat.Pulse(&Acq)); + EXPECT_EQ("\r0% [Working]" + "\r " + "\r0% [Work" + "\r0% [Working]", out.str()); +} diff --git a/test/libapt/cdrom_test.cc b/test/libapt/cdrom_test.cc index 626ef538e..7257eaf1b 100644 --- a/test/libapt/cdrom_test.cc +++ b/test/libapt/cdrom_test.cc @@ -91,7 +91,7 @@ TEST(CDROMTest,ReduceSourcelist) } TEST(CDROMTest, FindMountPointForDevice) { - char * tempfile; + char * tempfile = NULL; FileFd fd; createTemporaryFile("mountpoints", fd, &tempfile, "rootfs / rootfs rw 0 0\n" @@ -109,6 +109,7 @@ TEST(CDROMTest, FindMountPointForDevice) EXPECT_EQ("/boot/efi", FindMountPointForDevice("/dev/sda1")); EXPECT_EQ("/tmp", FindMountPointForDevice("tmpfs")); - unlink(tempfile); + if (tempfile != NULL) + unlink(tempfile); free(tempfile); } diff --git a/test/libapt/commandline_test.cc b/test/libapt/commandline_test.cc index 26e80bfde..627f1b486 100644 --- a/test/libapt/commandline_test.cc +++ b/test/libapt/commandline_test.cc @@ -2,6 +2,7 @@ #include <apt-pkg/cmndline.h> #include <apt-pkg/configuration.h> +#include <apt-private/private-cmndline.h> #include <gtest/gtest.h> @@ -56,3 +57,99 @@ TEST(CommandLineTest,Parsing) EXPECT_TRUE(c.FindB("Test::Worked", false)); EXPECT_FALSE(c.FindB("Test::Zero", false)); } + +TEST(CommandLineTest, BoolParsing) +{ + CommandLine::Args Args[] = { + { 't', 0, "Test::Worked", 0 }, + {0,0,0,0} + }; + ::Configuration c; + CommandLine CmdL(Args, &c); + + // the commandline parser used to use strtol() on the argument + // to check if the argument is a boolean expression - that + // stopped after the "0". this test ensures that we always check + // that the entire string was consumed by strtol + { + char const * argv[] = { "show", "-t", "0ad" }; + bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv); + EXPECT_TRUE(res); + ASSERT_EQ(std::string(CmdL.FileList[0]), "0ad"); + } + + { + char const * argv[] = { "show", "-t", "0", "ad" }; + bool res = CmdL.Parse(sizeof(argv)/sizeof(char*), argv); + EXPECT_TRUE(res); + ASSERT_EQ(std::string(CmdL.FileList[0]), "ad"); + } + +} + +bool DoVoid(CommandLine &) { return false; } + +TEST(CommandLineTest,GetCommand) +{ + CommandLine::Dispatch Cmds[] = { {"install",&DoVoid}, {"remove", &DoVoid}, {0,0} }; + { + char const * argv[] = { "apt-get", "-t", "unstable", "remove", "-d", "foo" }; + char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv); + EXPECT_STREQ("remove", com); + std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com); + ::Configuration c; + CommandLine CmdL(Args.data(), &c); + ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv)); + EXPECT_EQ(c.Find("APT::Default-Release"), "unstable"); + EXPECT_TRUE(c.FindB("APT::Get::Download-Only")); + ASSERT_EQ(2, CmdL.FileSize()); + EXPECT_EQ(std::string(CmdL.FileList[0]), "remove"); + EXPECT_EQ(std::string(CmdL.FileList[1]), "foo"); + } + { + char const * argv[] = {"apt-get", "-t", "unstable", "remove", "--", "-d", "foo" }; + char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv); + EXPECT_STREQ("remove", com); + std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com); + ::Configuration c; + CommandLine CmdL(Args.data(), &c); + ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv)); + EXPECT_EQ(c.Find("APT::Default-Release"), "unstable"); + EXPECT_FALSE(c.FindB("APT::Get::Download-Only")); + ASSERT_EQ(3, CmdL.FileSize()); + EXPECT_EQ(std::string(CmdL.FileList[0]), "remove"); + EXPECT_EQ(std::string(CmdL.FileList[1]), "-d"); + EXPECT_EQ(std::string(CmdL.FileList[2]), "foo"); + } + { + char const * argv[] = {"apt-get", "-t", "unstable", "--", "remove", "-d", "foo" }; + char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv); + EXPECT_STREQ("remove", com); + std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com); + ::Configuration c; + CommandLine CmdL(Args.data(), &c); + ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv)); + EXPECT_EQ(c.Find("APT::Default-Release"), "unstable"); + EXPECT_FALSE(c.FindB("APT::Get::Download-Only")); + ASSERT_EQ(CmdL.FileSize(), 3); + EXPECT_EQ(std::string(CmdL.FileList[0]), "remove"); + EXPECT_EQ(std::string(CmdL.FileList[1]), "-d"); + EXPECT_EQ(std::string(CmdL.FileList[2]), "foo"); + } + { + char const * argv[] = {"apt-get", "install", "-t", "unstable", "--", "remove", "-d", "foo" }; + char const * com = CommandLine::GetCommand(Cmds, sizeof(argv)/sizeof(argv[0]), argv); + EXPECT_STREQ("install", com); + std::vector<CommandLine::Args> Args = getCommandArgs("apt-get", com); + ::Configuration c; + CommandLine CmdL(Args.data(), &c); + ASSERT_TRUE(CmdL.Parse(sizeof(argv)/sizeof(argv[0]), argv)); + EXPECT_EQ(c.Find("APT::Default-Release"), "unstable"); + EXPECT_FALSE(c.FindB("APT::Get::Download-Only")); + ASSERT_EQ(CmdL.FileSize(), 4); + EXPECT_EQ(std::string(CmdL.FileList[0]), "install"); + EXPECT_EQ(std::string(CmdL.FileList[1]), "remove"); + EXPECT_EQ(std::string(CmdL.FileList[2]), "-d"); + EXPECT_EQ(std::string(CmdL.FileList[3]), "foo"); + } +} diff --git a/test/libapt/fileutl_test.cc b/test/libapt/fileutl_test.cc index 643c02297..a2c303768 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"; @@ -217,10 +222,79 @@ 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) 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<std::string> 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}; + 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_STREQ(buf, "meepmeep\n"); + + // wait for the child to exit and cleanup + 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 + _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; + EXPECT_TRUE(Popen(Args, Fd, Child, FileFd::WriteOnly)); + s = "\n"; + 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/"); + EXPECT_EQ(res, 0); + std::string p = flAbsPath("ls"); + EXPECT_EQ(p, "/bin/ls"); + + res = chdir(cwd.c_str()); + EXPECT_EQ(res, 0); +} diff --git a/test/libapt/hashsums_test.cc b/test/libapt/hashsums_test.cc index c06d85e03..edcd8a11a 100644 --- a/test/libapt/hashsums_test.cc +++ b/test/libapt/hashsums_test.cc @@ -1,5 +1,6 @@ #include <config.h> +#include <apt-pkg/configuration.h> #include <apt-pkg/md5.h> #include <apt-pkg/sha1.h> #include <apt-pkg/sha2.h> @@ -162,24 +163,58 @@ TEST(HashSumsTest, FileBased) FileFd fd(__FILE__, FileFd::ReadOnly); EXPECT_TRUE(fd.IsOpen()); + std::string FileSize; + strprintf(FileSize, "%llu", fd.FileSize()); { Hashes hashes; hashes.AddFD(fd.Fd()); - EXPECT_EQ(md5.Value(), hashes.MD5.Result().Value()); - EXPECT_EQ(sha1.Value(), hashes.SHA1.Result().Value()); - EXPECT_EQ(sha256.Value(), hashes.SHA256.Result().Value()); - EXPECT_EQ(sha512.Value(), hashes.SHA512.Result().Value()); + HashStringList list = hashes.GetHashStringList(); + EXPECT_FALSE(list.empty()); + EXPECT_EQ(5, list.size()); + EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); + EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue()); + EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue()); + EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); + EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); } - unsigned long sz = fd.FileSize(); + unsigned long long sz = fd.FileSize(); fd.Seek(0); { Hashes hashes; hashes.AddFD(fd.Fd(), sz); - EXPECT_EQ(md5.Value(), hashes.MD5.Result().Value()); - EXPECT_EQ(sha1.Value(), hashes.SHA1.Result().Value()); - EXPECT_EQ(sha256.Value(), hashes.SHA256.Result().Value()); - EXPECT_EQ(sha512.Value(), hashes.SHA512.Result().Value()); + HashStringList list = hashes.GetHashStringList(); + EXPECT_FALSE(list.empty()); + EXPECT_EQ(5, list.size()); + EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); + EXPECT_EQ(sha1.Value(), list.find("SHA1")->HashValue()); + EXPECT_EQ(sha256.Value(), list.find("SHA256")->HashValue()); + EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); + EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); + } + fd.Seek(0); + { + Hashes hashes(Hashes::MD5SUM | Hashes::SHA512SUM); + hashes.AddFD(fd); + HashStringList list = hashes.GetHashStringList(); + EXPECT_FALSE(list.empty()); + EXPECT_EQ(3, list.size()); + EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); + EXPECT_EQ(NULL, list.find("SHA1")); + EXPECT_EQ(NULL, list.find("SHA256")); + EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); + EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); + fd.Seek(0); + Hashes hashes2(list); + hashes2.AddFD(fd); + list = hashes2.GetHashStringList(); + EXPECT_FALSE(list.empty()); + EXPECT_EQ(3, list.size()); + EXPECT_EQ(md5.Value(), list.find("MD5Sum")->HashValue()); + EXPECT_EQ(NULL, list.find("SHA1")); + EXPECT_EQ(NULL, list.find("SHA256")); + EXPECT_EQ(sha512.Value(), list.find("SHA512")->HashValue()); + EXPECT_EQ(FileSize, list.find("Checksum-FileSize")->HashValue()); } fd.Seek(0); { @@ -207,16 +242,127 @@ TEST(HashSumsTest, FileBased) } fd.Close(); - { - HashString sha2("SHA256", sha256.Value()); - EXPECT_TRUE(sha2.VerifyFile(__FILE__)); - } - { - HashString sha2("SHA512", sha512.Value()); - EXPECT_TRUE(sha2.VerifyFile(__FILE__)); - } - { - HashString sha2("SHA256:" + sha256.Value()); - EXPECT_TRUE(sha2.VerifyFile(__FILE__)); - } + HashString sha2file("SHA512", sha512.Value()); + EXPECT_TRUE(sha2file.VerifyFile(__FILE__)); + HashString sha2wrong("SHA512", "00000000000"); + EXPECT_FALSE(sha2wrong.VerifyFile(__FILE__)); + EXPECT_EQ(sha2file, sha2file); + EXPECT_TRUE(sha2file == sha2file); + EXPECT_NE(sha2file, sha2wrong); + EXPECT_TRUE(sha2file != sha2wrong); + + HashString sha2big("SHA256", sha256.Value()); + EXPECT_TRUE(sha2big.VerifyFile(__FILE__)); + HashString sha2small("sha256:" + sha256.Value()); + EXPECT_TRUE(sha2small.VerifyFile(__FILE__)); + EXPECT_EQ(sha2big, sha2small); + EXPECT_TRUE(sha2big == sha2small); + EXPECT_FALSE(sha2big != sha2small); + + HashStringList hashes; + EXPECT_TRUE(hashes.empty()); + EXPECT_TRUE(hashes.push_back(sha2file)); + EXPECT_FALSE(hashes.empty()); + EXPECT_EQ(1, hashes.size()); + + HashStringList wrong; + EXPECT_TRUE(wrong.push_back(sha2wrong)); + EXPECT_NE(wrong, hashes); + EXPECT_FALSE(wrong == hashes); + EXPECT_TRUE(wrong != hashes); + + HashStringList similar; + EXPECT_TRUE(similar.push_back(sha2big)); + EXPECT_NE(similar, hashes); + EXPECT_FALSE(similar == hashes); + EXPECT_TRUE(similar != hashes); + + EXPECT_TRUE(hashes.push_back(sha2big)); + EXPECT_EQ(2, hashes.size()); + EXPECT_TRUE(hashes.push_back(sha2small)); + EXPECT_EQ(2, hashes.size()); + EXPECT_FALSE(hashes.push_back(sha2wrong)); + EXPECT_EQ(2, hashes.size()); + EXPECT_TRUE(hashes.VerifyFile(__FILE__)); + + EXPECT_EQ(similar, hashes); + EXPECT_TRUE(similar == hashes); + EXPECT_FALSE(similar != hashes); + similar.clear(); + EXPECT_TRUE(similar.empty()); + EXPECT_EQ(0, similar.size()); + EXPECT_NE(similar, hashes); + EXPECT_FALSE(similar == hashes); + EXPECT_TRUE(similar != hashes); +} +TEST(HashSumsTest, HashStringList) +{ + _config->Clear("Acquire::ForceHash"); + + HashStringList list; + EXPECT_TRUE(list.empty()); + EXPECT_FALSE(list.usable()); + EXPECT_EQ(0, list.size()); + EXPECT_EQ(NULL, list.find(NULL)); + EXPECT_EQ(NULL, list.find("")); + EXPECT_EQ(NULL, list.find("MD5Sum")); + + // empty lists aren't equal + HashStringList list2; + EXPECT_FALSE(list == list2); + EXPECT_TRUE(list != list2); + + // some hashes don't really contribute to usability + list.push_back(HashString("Checksum-FileSize", "29")); + EXPECT_FALSE(list.empty()); + EXPECT_FALSE(list.usable()); + + Hashes hashes; + hashes.Add("The quick brown fox jumps over the lazy dog"); + list = hashes.GetHashStringList(); + EXPECT_FALSE(list.empty()); + EXPECT_TRUE(list.usable()); + EXPECT_EQ(5, list.size()); + EXPECT_TRUE(NULL != list.find(NULL)); + EXPECT_TRUE(NULL != list.find("")); + EXPECT_TRUE(NULL != list.find("MD5Sum")); + EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); + EXPECT_TRUE(NULL == list.find("ROT26")); + + _config->Set("Acquire::ForceHash", "MD5Sum"); + EXPECT_FALSE(list.empty()); + EXPECT_TRUE(list.usable()); + EXPECT_EQ(5, list.size()); + EXPECT_TRUE(NULL != list.find(NULL)); + EXPECT_TRUE(NULL != list.find("")); + EXPECT_TRUE(NULL != list.find("MD5Sum")); + EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); + EXPECT_TRUE(NULL == list.find("ROT26")); + + _config->Set("Acquire::ForceHash", "ROT26"); + EXPECT_FALSE(list.empty()); + EXPECT_FALSE(list.usable()); + EXPECT_EQ(5, list.size()); + EXPECT_TRUE(NULL == list.find(NULL)); + EXPECT_TRUE(NULL == list.find("")); + EXPECT_TRUE(NULL != list.find("MD5Sum")); + EXPECT_TRUE(NULL != list.find("Checksum-FileSize")); + EXPECT_TRUE(NULL == list.find("ROT26")); + + _config->Clear("Acquire::ForceHash"); + + list2.push_back(*list.find("MD5Sum")); + EXPECT_TRUE(list == list2); + EXPECT_FALSE(list != list2); + + // introduce a mismatch to the list + list2.push_back(HashString("SHA1", "cacecbd74968bc90ea3342767e6b94f46ddbcafc")); + EXPECT_FALSE(list == list2); + EXPECT_TRUE(list != list2); + + _config->Set("Acquire::ForceHash", "MD5Sum"); + EXPECT_TRUE(list == list2); + EXPECT_FALSE(list != list2); + + _config->Clear("Acquire::ForceHash"); } diff --git a/test/libapt/indexcopytosourcelist_test.cc b/test/libapt/indexcopytosourcelist_test.cc index bec87601f..1b0427564 100644 --- a/test/libapt/indexcopytosourcelist_test.cc +++ b/test/libapt/indexcopytosourcelist_test.cc @@ -16,7 +16,7 @@ class NoCopy : public IndexCopy { return Path; } bool GetFile(std::string &/*Filename*/, unsigned long long &/*Size*/) { return false; } - bool RewriteEntry(FILE * /*Target*/, std::string /*File*/) { return false; } + bool RewriteEntry(FileFd & /*Target*/, std::string const &/*File*/) { return false; } const char *GetFileName() { return NULL; } const char *Type() { return NULL; } diff --git a/test/libapt/makefile b/test/libapt/makefile index 69a13fd92..61a8aaf31 100644 --- a/test/libapt/makefile +++ b/test/libapt/makefile @@ -8,14 +8,15 @@ APT_DOMAIN=none include ../../buildlib/defaults.mak .PHONY: test +ifeq (file-okay,$(shell $(CC) -I $(BASE)/build/include -M gtest_runner.cc >/dev/null 2>&1 && echo 'file-okay')) test: $(BIN)/gtest$(BASENAME) MALLOC_PERTURB_=21 MALLOC_CHECK_=2 LD_LIBRARY_PATH=$(LIB) $(BIN)/gtest$(BASENAME) $(BIN)/gtest$(BASENAME): $(LIB)/gtest.a PROGRAM = gtest${BASENAME} -SLIBS = -lapt-pkg -pthread $(LIB)/gtest.a -LIB_MAKES = apt-pkg/makefile +SLIBS = -lapt-pkg -lapt-private -pthread $(LIB)/gtest.a +LIB_MAKES = apt-pkg/makefile apt-private/makefile SOURCE = gtest_runner.cc $(wildcard *-helpers.cc *_test.cc) include $(PROGRAM_H) @@ -71,3 +72,12 @@ $(LIB)/gtest.a: $(OBJ)/gtest-all.o echo Building static library $@ -rm -f $@ $(AR) $(ARFLAGS) $@ $^ + +else +test: + @echo "APT uses Googles C++ testing framework for its unit tests" + @echo "On Debian systems this is available in the 'libgtest-dev' package." + @echo "Please install it before attempting to run the unit tests." + $(CC) -I $(BASE)/build/include -M gtest_runner.cc + exit 100 +endif diff --git a/test/libapt/parsedepends_test.cc b/test/libapt/parsedepends_test.cc index 1e0afb66c..f644599bd 100644 --- a/test/libapt/parsedepends_test.cc +++ b/test/libapt/parsedepends_test.cc @@ -23,7 +23,7 @@ static void parseDependency(bool const StripMultiArch, bool const ParseArchFlag "libdb-dev:any, " "gettext:native (<= 0.12), " "libcurl4-gnutls-dev:native | libcurl3-gnutls-dev (>> 7.15.5), " - "debiandoc-sgml, " + "docbook-xml, " "apt (>= 0.7.25), " "not-for-me [ !amd64 ], " "only-for-me [ amd64 ], " @@ -33,9 +33,10 @@ static void parseDependency(bool const StripMultiArch, bool const ParseArchFlag "os-for-me [ linux-any ], " "cpu-not-for-me [ any-armel ], " "os-not-for-me [ kfreebsd-any ], " - "not-in-stage1 <!profile.stage1>, " - "not-in-stage1-or-nodoc <!profile.nodoc !profile.stage1>, " - "only-in-stage1 <unknown.unknown profile.stage1>, " + "not-in-stage1 <!stage1>, " + "not-stage1-and-not-nodoc <!nodoc !stage1>, " + "not-stage1-or-not-nodoc <!nodoc> <!stage1>, " + "unknown-profile <unknown stage1>, " "overlord-dev:any (= 7.15.3~) | overlord-dev:native (>> 7.15.5), " ; @@ -82,7 +83,7 @@ static void parseDependency(bool const StripMultiArch, bool const ParseArchFlag EXPECT_EQ(Null | pkgCache::Dep::Greater, Op); Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList); - EXPECT_EQ("debiandoc-sgml", Package); + EXPECT_EQ("docbook-xml", Package); EXPECT_EQ("", Version); EXPECT_EQ(Null | pkgCache::Dep::NoOp, Op); @@ -184,7 +185,7 @@ static void parseDependency(bool const StripMultiArch, bool const ParseArchFlag if (ParseRestrictionsList == true) { Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList); - EXPECT_EQ("", Package); // not-in-stage1-or-in-nodoc + EXPECT_EQ("", Package); // not-stage1-and-not-nodoc } else { EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList)); Start = strstr(Start, ","); @@ -193,7 +194,16 @@ static void parseDependency(bool const StripMultiArch, bool const ParseArchFlag if (ParseRestrictionsList == true) { Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList); - EXPECT_EQ("only-in-stage1", Package); + EXPECT_EQ("not-stage1-or-not-nodoc", Package); + } else { + EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList)); + Start = strstr(Start, ","); + Start++; + } + + if (ParseRestrictionsList == true) { + Start = debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList); + EXPECT_EQ("", Package); // unknown-profile } else { EXPECT_EQ(true, 0 == debListParser::ParseDepends(Start, End, Package, Version, Op, ParseArchFlags, StripMultiArch, ParseRestrictionsList)); Start = strstr(Start, ","); @@ -232,10 +242,11 @@ test: SCOPED_TRACE(std::string("ParseRestrictionsList: ") + (ParseRestrictionsList ? "true" : "false")); parseDependency(StripMultiArch, ParseArchFlags, ParseRestrictionsList); } - if (StripMultiArch == false) - if (ParseArchFlags == false) - ParseRestrictionsList = !ParseRestrictionsList; - ParseArchFlags = !ParseArchFlags; + if (StripMultiArch == false) { + if (ParseArchFlags == false) + ParseRestrictionsList = !ParseRestrictionsList; + ParseArchFlags = !ParseArchFlags; + } StripMultiArch = !StripMultiArch; runner++; diff --git a/test/libapt/sourcelist_test.cc b/test/libapt/sourcelist_test.cc index eb2d76c43..747ab4957 100644 --- a/test/libapt/sourcelist_test.cc +++ b/test/libapt/sourcelist_test.cc @@ -20,7 +20,7 @@ class SourceList : public pkgSourceList { TEST(SourceListTest,ParseFileDeb822) { FileFd fd; - char * tempfile; + char * tempfile = NULL; createTemporaryFile("parsefiledeb822", fd, &tempfile, "Types: deb\n" "URIs: http://ftp.debian.org/debian\n" diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc index bc004fd66..23dc08727 100644 --- a/test/libapt/strutil_test.cc +++ b/test/libapt/strutil_test.cc @@ -1,10 +1,13 @@ #include <config.h> #include <apt-pkg/strutl.h> +#include <apt-pkg/fileutl.h> #include <string> #include <vector> #include <gtest/gtest.h> +#include "file-helpers.h" + TEST(StrUtilTest,DeEscapeString) { // nothing special @@ -19,6 +22,21 @@ TEST(StrUtilTest,DeEscapeString) EXPECT_EQ("foo\\ x", DeEscapeString("foo\\\\ x")); EXPECT_EQ("\\foo\\", DeEscapeString("\\\\foo\\\\")); } +TEST(StrUtilTest,StringStrip) +{ + EXPECT_EQ("", APT::String::Strip("")); + EXPECT_EQ("foobar", APT::String::Strip("foobar")); + EXPECT_EQ("foo bar", APT::String::Strip("foo bar")); + + EXPECT_EQ("", APT::String::Strip(" ")); + EXPECT_EQ("", APT::String::Strip(" \r\n \t ")); + + EXPECT_EQ("foo bar", APT::String::Strip("foo bar ")); + EXPECT_EQ("foo bar", APT::String::Strip("foo bar \r\n \t ")); + EXPECT_EQ("foo bar", APT::String::Strip("\r\n \t foo bar")); + EXPECT_EQ("bar foo", APT::String::Strip("\r\n \t bar foo \r\n \t ")); + EXPECT_EQ("bar \t\r\n foo", APT::String::Strip("\r\n \t bar \t\r\n foo \r\n \t ")); +} TEST(StrUtilTest,StringSplitBasic) { std::vector<std::string> result = StringSplit("", ""); @@ -70,3 +88,150 @@ TEST(StrUtilTest,EndsWith) EXPECT_FALSE(Endswith("abcd", "x")); EXPECT_FALSE(Endswith("abcd", "abcndefg")); } +TEST(StrUtilTest,StartsWith) +{ + using APT::String::Startswith; + EXPECT_TRUE(Startswith("abcd", "a")); + EXPECT_TRUE(Startswith("abcd", "ab")); + EXPECT_TRUE(Startswith("abcd", "abcd")); + EXPECT_FALSE(Startswith("abcd", "x")); + EXPECT_FALSE(Startswith("abcd", "abcndefg")); +} +TEST(StrUtilTest,TimeToStr) +{ + EXPECT_EQ("0s", TimeToStr(0)); + EXPECT_EQ("42s", TimeToStr(42)); + EXPECT_EQ("9min 21s", TimeToStr((9*60) + 21)); + EXPECT_EQ("20min 42s", TimeToStr((20*60) + 42)); + EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21)); + EXPECT_EQ("10h 42min 21s", TimeToStr((10*3600) + (42*60) + 21)); + EXPECT_EQ("1988d 3h 29min 7s", TimeToStr((1988*86400) + (3*3600) + (29*60) + 7)); + + EXPECT_EQ("59s", TimeToStr(59)); + EXPECT_EQ("60s", TimeToStr(60)); + EXPECT_EQ("1min 1s", TimeToStr(61)); + EXPECT_EQ("59min 59s", TimeToStr(3599)); + EXPECT_EQ("60min 0s", TimeToStr(3600)); + EXPECT_EQ("1h 0min 1s", TimeToStr(3601)); + EXPECT_EQ("1h 1min 0s", TimeToStr(3660)); + EXPECT_EQ("23h 59min 59s", TimeToStr(86399)); + EXPECT_EQ("24h 0min 0s", TimeToStr(86400)); + EXPECT_EQ("1d 0h 0min 1s", TimeToStr(86401)); + EXPECT_EQ("1d 0h 1min 0s", TimeToStr(86460)); +} +TEST(StrUtilTest,SubstVar) +{ + EXPECT_EQ("", SubstVar("", "fails", "passes")); + EXPECT_EQ("test ", SubstVar("test fails", "fails", "")); + EXPECT_EQ("test passes", SubstVar("test passes", "", "fails")); + + EXPECT_EQ("test passes", SubstVar("test passes", "fails", "passes")); + EXPECT_EQ("test passes", SubstVar("test fails", "fails", "passes")); + + EXPECT_EQ("starts with", SubstVar("beginnt with", "beginnt", "starts")); + EXPECT_EQ("beginnt with", SubstVar("starts with", "starts", "beginnt")); + EXPECT_EQ("is in middle", SubstVar("is in der middle", "in der", "in")); + EXPECT_EQ("is in der middle", SubstVar("is in middle", "in", "in der")); + EXPECT_EQ("does end", SubstVar("does enden", "enden", "end")); + EXPECT_EQ("does enden", SubstVar("does end", "end", "enden")); + + EXPECT_EQ("abc", SubstVar("abc", "d", "a")); + EXPECT_EQ("abc", SubstVar("abd", "d", "c")); + EXPECT_EQ("abc", SubstVar("adc", "d", "b")); + EXPECT_EQ("abc", SubstVar("dbc", "d", "a")); + + EXPECT_EQ("b", SubstVar("b", "aa", "a")); + EXPECT_EQ("bb", SubstVar("bb", "aa", "a")); + EXPECT_EQ("bbb", SubstVar("bbb", "aa", "a")); + + EXPECT_EQ("aa", SubstVar("aaaa", "aa", "a")); + EXPECT_EQ("aaaa", SubstVar("aa", "a", "aa")); + EXPECT_EQ("aaaa", SubstVar("aaaa", "a", "a")); + EXPECT_EQ("a a a a ", SubstVar("aaaa", "a", "a ")); + + EXPECT_EQ(" bb bb bb bb ", SubstVar(" a a a a ", "a", "bb")); + EXPECT_EQ(" bb bb bb bb ", SubstVar(" aaa aaa aaa aaa ", "aaa", "bb")); + EXPECT_EQ(" bb a bb a bb a bb ", SubstVar(" aaa a aaa a aaa a aaa ", "aaa", "bb")); + +} +TEST(StrUtilTest,Base64Encode) +{ + EXPECT_EQ("QWxhZGRpbjpvcGVuIHNlc2FtZQ==", Base64Encode("Aladdin:open sesame")); + EXPECT_EQ("cGxlYXN1cmUu", Base64Encode("pleasure.")); + EXPECT_EQ("bGVhc3VyZS4=", Base64Encode("leasure.")); + EXPECT_EQ("ZWFzdXJlLg==", Base64Encode("easure.")); + EXPECT_EQ("YXN1cmUu", Base64Encode("asure.")); + EXPECT_EQ("c3VyZS4=", Base64Encode("sure.")); + EXPECT_EQ("dXJlLg==", Base64Encode("ure.")); + EXPECT_EQ("cmUu", Base64Encode("re.")); + EXPECT_EQ("ZS4=", Base64Encode("e.")); + EXPECT_EQ("Lg==", Base64Encode(".")); + EXPECT_EQ("", Base64Encode("")); +} +void ReadMessagesTestWithNewLine(char const * const nl, char const * const ab) +{ + SCOPED_TRACE(SubstVar(SubstVar(nl, "\n", "n"), "\r", "r") + " # " + ab); + FileFd fd; + std::string pkgA = "Package: pkgA\n" + "Version: 1\n" + "Size: 100\n" + "Description: aaa\n" + " aaa"; + std::string pkgB = "Package: pkgB\n" + "Version: 1\n" + "Flag: no\n" + "Description: bbb"; + std::string pkgC = "Package: pkgC\n" + "Version: 2\n" + "Flag: yes\n" + "Description:\n" + " ccc"; + + createTemporaryFile("readmessage", fd, NULL, (pkgA + nl + pkgB + nl + pkgC + nl).c_str()); + std::vector<std::string> list; + EXPECT_TRUE(ReadMessages(fd.Fd(), list)); + EXPECT_EQ(3, list.size()); + EXPECT_EQ(pkgA, list[0]); + EXPECT_EQ(pkgB, list[1]); + EXPECT_EQ(pkgC, list[2]); + + size_t const msgsize = 63990; + createTemporaryFile("readmessage", fd, NULL, NULL); + for (size_t j = 0; j < msgsize; ++j) + fd.Write(ab, strlen(ab)); + for (size_t i = 0; i < 21; ++i) + { + std::string msg; + strprintf(msg, "msgsize=%zu i=%zu", msgsize, i); + SCOPED_TRACE(msg); + fd.Seek((msgsize + (i - 1)) * strlen(ab)); + fd.Write(ab, strlen(ab)); + fd.Write(nl, strlen(nl)); + fd.Seek(0); + list.clear(); + EXPECT_TRUE(ReadMessages(fd.Fd(), list)); + EXPECT_EQ(1, list.size()); + EXPECT_EQ((msgsize + i) * strlen(ab), list[0].length()); + EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab)); + } + + list.clear(); + fd.Write(pkgA.c_str(), pkgA.length()); + fd.Write(nl, strlen(nl)); + fd.Seek(0); + EXPECT_TRUE(ReadMessages(fd.Fd(), list)); + EXPECT_EQ(2, list.size()); + EXPECT_EQ((msgsize + 20) * strlen(ab), list[0].length()); + EXPECT_EQ(std::string::npos, list[0].find_first_not_of(ab)); + EXPECT_EQ(pkgA, list[1]); + + + fd.Close(); +} +TEST(StrUtilTest,ReadMessages) +{ + ReadMessagesTestWithNewLine("\n\n", "a"); + ReadMessagesTestWithNewLine("\r\n\r\n", "a"); + ReadMessagesTestWithNewLine("\n\n", "ab"); + ReadMessagesTestWithNewLine("\r\n\r\n", "ab"); +} diff --git a/test/libapt/tagfile_test.cc b/test/libapt/tagfile_test.cc index 1bac75b55..d7030f41a 100644 --- a/test/libapt/tagfile_test.cc +++ b/test/libapt/tagfile_test.cc @@ -7,6 +7,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <sstream> #include <gtest/gtest.h> @@ -33,4 +34,188 @@ TEST(TagFileTest,SingleField) EXPECT_FALSE(section.Exists("FieldB-12345678")); // There is only one section in this tag file EXPECT_FALSE(tfile.Step(section)); + + // Now we scan an empty section to test reset + ASSERT_TRUE(section.Scan("\n\n", 2, true)); + EXPECT_EQ(0, section.Count()); + EXPECT_FALSE(section.Exists("FieldA-12345678")); + EXPECT_FALSE(section.Exists("FieldB-12345678")); +} + +TEST(TagFileTest,MultipleSections) +{ + FileFd fd; + createTemporaryFile("bigsection", fd, NULL, "Package: pkgA\n" + "Version: 1\n" + "Size: 100\n" + "Description: aaa\n" + " aaa\n" + "\n" + "Package: pkgB\n" + "Version: 1\n" + "Flag: no\n" + "Description: bbb\n" + "\n" + "Package: pkgC\n" + "Version: 2\n" + "Flag: yes\n" + "Description:\n" + " ccc\n" + ); + + pkgTagFile tfile(&fd); + pkgTagSection section; + EXPECT_FALSE(section.Exists("Version")); + + EXPECT_TRUE(tfile.Step(section)); + EXPECT_EQ(4, section.Count()); + EXPECT_TRUE(section.Exists("Version")); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("Size")); + EXPECT_FALSE(section.Exists("Flag")); + EXPECT_TRUE(section.Exists("Description")); + EXPECT_EQ("pkgA", section.FindS("Package")); + EXPECT_EQ("1", section.FindS("Version")); + EXPECT_EQ(1, section.FindULL("Version")); + EXPECT_EQ(100, section.FindULL("Size")); + unsigned long Flags = 1; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(1, Flags); + Flags = 0; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(0, Flags); + EXPECT_EQ("aaa\n aaa", section.FindS("Description")); + + + EXPECT_TRUE(tfile.Step(section)); + EXPECT_EQ(4, section.Count()); + EXPECT_TRUE(section.Exists("Version")); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("Size")); + EXPECT_TRUE(section.Exists("Flag")); + EXPECT_TRUE(section.Exists("Description")); + EXPECT_EQ("pkgB", section.FindS("Package")); + EXPECT_EQ("1", section.FindS("Version")); + EXPECT_EQ(1, section.FindULL("Version")); + EXPECT_EQ(0, section.FindULL("Size")); + Flags = 1; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(0, Flags); + Flags = 0; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(0, Flags); + EXPECT_EQ("bbb", section.FindS("Description")); + + EXPECT_TRUE(tfile.Step(section)); + EXPECT_EQ(4, section.Count()); + EXPECT_TRUE(section.Exists("Version")); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("Size")); + EXPECT_TRUE(section.Exists("Flag")); + EXPECT_TRUE(section.Exists("Description")); + EXPECT_EQ("pkgC", section.FindS("Package")); + EXPECT_EQ("2", section.FindS("Version")); + EXPECT_EQ(2, section.FindULL("Version")); + Flags = 0; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(1, Flags); + Flags = 1; + EXPECT_TRUE(section.FindFlag("Flag", Flags, 1)); + EXPECT_EQ(1, Flags); + EXPECT_EQ("ccc", section.FindS("Description")); + + // There is no section left in this tag file + EXPECT_FALSE(tfile.Step(section)); +} + +TEST(TagFileTest,BigSection) +{ + size_t const count = 500; + std::stringstream content; + for (size_t i = 0; i < count; ++i) + content << "Field-" << i << ": " << (2000 + i) << std::endl; + + FileFd fd; + createTemporaryFile("bigsection", fd, NULL, content.str().c_str()); + + pkgTagFile tfile(&fd); + pkgTagSection section; + EXPECT_TRUE(tfile.Step(section)); + + EXPECT_EQ(count, section.Count()); + for (size_t i = 0; i < count; ++i) + { + std::stringstream name; + name << "Field-" << i; + EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist"; + EXPECT_EQ((2000 + i), section.FindULL(name.str().c_str())); + } + + // There is only one section in this tag file + EXPECT_FALSE(tfile.Step(section)); +} + +TEST(TagFileTest, PickedUpFromPreviousCall) +{ + size_t const count = 500; + std::stringstream contentstream; + for (size_t i = 0; i < count; ++i) + contentstream << "Field-" << i << ": " << (2000 + i) << std::endl; + contentstream << std::endl << std::endl; + std::string content = contentstream.str(); + + pkgTagSection section; + EXPECT_FALSE(section.Scan(content.c_str(), content.size()/2)); + EXPECT_NE(0, section.Count()); + EXPECT_NE(count, section.Count()); + EXPECT_TRUE(section.Scan(content.c_str(), content.size(), false)); + EXPECT_EQ(count, section.Count()); + + for (size_t i = 0; i < count; ++i) + { + std::stringstream name; + name << "Field-" << i; + EXPECT_TRUE(section.Exists(name.str().c_str())) << name.str() << " does not exist"; + EXPECT_EQ((2000 + i), section.FindULL(name.str().c_str())); + } +} + +TEST(TagFileTest, SpacesEverywhere) +{ + std::string content = + "Package: pkgA\n" + "Package: pkgB\n" + "NoSpaces:yes\n" + "TagSpaces\t :yes\n" + "ValueSpaces: \tyes\n" + "BothSpaces \t:\t yes\n" + "TrailingSpaces: yes\t \n" + "Naming Space: yes\n" + "Naming Spaces: yes\n" + "Package : pkgC \n" + "Multi-Colon::yes:\n" + "\n\n"; + + pkgTagSection section; + EXPECT_TRUE(section.Scan(content.c_str(), content.size())); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("NoSpaces")); + EXPECT_TRUE(section.Exists("TagSpaces")); + EXPECT_TRUE(section.Exists("ValueSpaces")); + EXPECT_TRUE(section.Exists("BothSpaces")); + EXPECT_TRUE(section.Exists("TrailingSpaces")); + EXPECT_TRUE(section.Exists("Naming Space")); + EXPECT_TRUE(section.Exists("Naming Spaces")); + EXPECT_TRUE(section.Exists("Multi-Colon")); + EXPECT_EQ("pkgC", section.FindS("Package")); + EXPECT_EQ("yes", section.FindS("NoSpaces")); + EXPECT_EQ("yes", section.FindS("TagSpaces")); + EXPECT_EQ("yes", section.FindS("ValueSpaces")); + EXPECT_EQ("yes", section.FindS("BothSpaces")); + EXPECT_EQ("yes", section.FindS("TrailingSpaces")); + EXPECT_EQ("yes", section.FindS("Naming Space")); + EXPECT_EQ("yes", section.FindS("Naming Spaces")); + EXPECT_EQ(":yes:", section.FindS("Multi-Colon")); + // overridden values are still present, but not really accessible + EXPECT_EQ(11, section.Count()); } diff --git a/test/libapt/tagsection_test.cc b/test/libapt/tagsection_test.cc new file mode 100644 index 000000000..f250177af --- /dev/null +++ b/test/libapt/tagsection_test.cc @@ -0,0 +1,270 @@ +#include <config.h> + +#include <apt-pkg/fileutl.h> +#include <apt-pkg/tagfile.h> + +#include <string> +#include <sstream> + +#include <gtest/gtest.h> + +#include "file-helpers.h" + +std::string packageValue = "aaaa"; +std::string typoValue = "aa\n" + " .\n" + " cc"; +std::string typoRawValue = "\n " + typoValue; +std::string overrideValue = "1"; +/* + std::cerr << "FILECONTENT: »"; + char buffer[3000]; + while (fd.ReadLine(buffer, sizeof(buffer))) + std::cerr << buffer; + std::cerr << "«" << std::endl;; +*/ + +void setupTestcaseStart(FileFd &fd, pkgTagSection §ion, std::string &content) +{ + createTemporaryFile("writesection", fd, NULL, NULL); + content = "Package: " + packageValue + "\n" + "TypoA:\n " + typoValue + "\n" + "Override: " + overrideValue + "\n" + "Override-Backup: " + overrideValue + "\n" + "\n"; + EXPECT_TRUE(section.Scan(content.c_str(), content.length(), true)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoA")); + EXPECT_EQ(typoRawValue, section.FindRawS("TypoA")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteUnmodified) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + EXPECT_TRUE(section.Write(fd)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoA")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteUnmodifiedOrder) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + char const * const order[] = { "Package", "TypoA", "Override", NULL }; + EXPECT_TRUE(section.Write(fd, order)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoA")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteUnmodifiedOrderReversed) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + char const * const order[] = { "Override", "TypoA", "Package", NULL }; + EXPECT_TRUE(section.Write(fd, order)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoA")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteUnmodifiedOrderNotAll) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + char const * const order[] = { "Override", NULL }; + EXPECT_TRUE(section.Write(fd, order)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoA")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteNoOrderRename) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Rename("TypoA", "TypoB")); + EXPECT_TRUE(section.Write(fd, NULL, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("TypoB")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoB")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteNoOrderRemove) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Remove("TypoA")); + rewrite.push_back(pkgTagSection::Tag::Rewrite("Override", "")); + EXPECT_TRUE(section.Write(fd, NULL, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("TypoA")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_FALSE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(2, section.Count()); +} +TEST(TagSectionTest,WriteNoOrderRewrite) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Rewrite("Override", "42")); + EXPECT_TRUE(section.Write(fd, NULL, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(42, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteOrderRename) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Rename("TypoA", "TypoB")); + char const * const order[] = { "Package", "TypoA", "Override", NULL }; + EXPECT_TRUE(section.Write(fd, order, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("TypoA")); + EXPECT_TRUE(section.Exists("TypoB")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(typoValue, section.FindS("TypoB")); + EXPECT_EQ(1, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} +TEST(TagSectionTest,WriteOrderRemove) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Remove("TypoA")); + rewrite.push_back(pkgTagSection::Tag::Rewrite("Override", "")); + char const * const order[] = { "Package", "TypoA", "Override", NULL }; + EXPECT_TRUE(section.Write(fd, order, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_FALSE(section.Exists("TypoA")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_FALSE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(2, section.Count()); +} +TEST(TagSectionTest,WriteOrderRewrite) +{ + FileFd fd; + pkgTagSection section; + std::string content; + setupTestcaseStart(fd, section, content); + std::vector<pkgTagSection::Tag> rewrite; + rewrite.push_back(pkgTagSection::Tag::Rewrite("Override", "42")); + char const * const order[] = { "Package", "TypoA", "Override", NULL }; + EXPECT_TRUE(section.Write(fd, order, rewrite)); + EXPECT_TRUE(fd.Seek(0)); + pkgTagFile tfile(&fd); + ASSERT_TRUE(tfile.Step(section)); + EXPECT_TRUE(section.Exists("Package")); + EXPECT_TRUE(section.Exists("TypoA")); + EXPECT_FALSE(section.Exists("TypoB")); + EXPECT_TRUE(section.Exists("Override")); + EXPECT_TRUE(section.Exists("Override-Backup")); + EXPECT_EQ(packageValue, section.FindS("Package")); + EXPECT_EQ(42, section.FindI("Override")); + EXPECT_EQ(1, section.FindI("Override-Backup")); + EXPECT_EQ(4, section.Count()); +} diff --git a/test/libapt/uri_test.cc b/test/libapt/uri_test.cc index 1662f51f0..5d5ae9679 100644 --- a/test/libapt/uri_test.cc +++ b/test/libapt/uri_test.cc @@ -12,6 +12,7 @@ TEST(URITest, BasicHTTP) EXPECT_EQ(90, U.Port); EXPECT_EQ("www.debian.org", U.Host); EXPECT_EQ("/temp/test", U.Path); + EXPECT_EQ("http://www.debian.org:90/temp/test", (std::string)U); // Login data U = URI("http://jgg:foo@ualberta.ca/blah"); EXPECT_EQ("http", U.Access); @@ -20,6 +21,7 @@ TEST(URITest, BasicHTTP) EXPECT_EQ(0, U.Port); EXPECT_EQ("ualberta.ca", U.Host); EXPECT_EQ("/blah", U.Path); + EXPECT_EQ("http://jgg:foo@ualberta.ca/blah", (std::string)U); } TEST(URITest, SingeSlashFile) { @@ -30,6 +32,7 @@ TEST(URITest, SingeSlashFile) EXPECT_EQ(0, U.Port); EXPECT_EQ("", U.Host); EXPECT_EQ("/usr/bin/foo", U.Path); + EXPECT_EQ("file:/usr/bin/foo", (std::string)U); } TEST(URITest, BasicCDROM) { @@ -40,6 +43,7 @@ TEST(URITest, BasicCDROM) EXPECT_EQ(0, U.Port); EXPECT_EQ("Moo Cow Rom", U.Host); EXPECT_EQ("/debian", U.Path); + EXPECT_EQ("cdrom://Moo Cow Rom/debian", (std::string)U); } TEST(URITest, RelativeGzip) { @@ -50,6 +54,7 @@ TEST(URITest, RelativeGzip) EXPECT_EQ(0, U.Port); EXPECT_EQ(".", U.Host); EXPECT_EQ("/bar/cow", U.Path); + EXPECT_EQ("gzip://./bar/cow", (std::string)U); } TEST(URITest, NoSlashFTP) { @@ -60,6 +65,7 @@ TEST(URITest, NoSlashFTP) EXPECT_EQ(0, U.Port); EXPECT_EQ("ftp.fr.debian.org", U.Host); EXPECT_EQ("/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", U.Path); + EXPECT_EQ("ftp://ftp.fr.debian.org/debian/pool/main/x/xtel/xtel_3.2.1-15_i386.deb", (std::string)U); } TEST(URITest, RFC2732) { @@ -70,6 +76,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(0, U.Port); EXPECT_EQ("1080::8:800:200C:417A", U.Host); EXPECT_EQ("/foo", U.Path); + EXPECT_EQ("http://[1080::8:800:200C:417A]/foo", (std::string)U); // with port U = URI("http://[::FFFF:129.144.52.38]:80/index.html"); EXPECT_EQ("http", U.Access); @@ -78,6 +85,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(80, U.Port); EXPECT_EQ("::FFFF:129.144.52.38", U.Host); EXPECT_EQ("/index.html", U.Path); + EXPECT_EQ("http://[::FFFF:129.144.52.38]:80/index.html", (std::string)U); // extra colon U = URI("http://[::FFFF:129.144.52.38:]:80/index.html"); EXPECT_EQ("http", U.Access); @@ -86,6 +94,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(80, U.Port); EXPECT_EQ("::FFFF:129.144.52.38:", U.Host); EXPECT_EQ("/index.html", U.Path); + EXPECT_EQ("http://[::FFFF:129.144.52.38:]:80/index.html", (std::string)U); // extra colon port U = URI("http://[::FFFF:129.144.52.38:]/index.html"); EXPECT_EQ("http", U.Access); @@ -94,6 +103,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(0, U.Port); EXPECT_EQ("::FFFF:129.144.52.38:", U.Host); EXPECT_EQ("/index.html", U.Path); + EXPECT_EQ("http://[::FFFF:129.144.52.38:]/index.html", (std::string)U); // My Evil Corruption of RFC 2732 to handle CDROM names! // Fun for the whole family! */ U = URI("cdrom:[The Debian 1.2 disk, 1/2 R1:6]/debian/"); @@ -103,6 +113,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(0, U.Port); EXPECT_EQ("The Debian 1.2 disk, 1/2 R1:6", U.Host); EXPECT_EQ("/debian/", U.Path); + EXPECT_EQ("cdrom://[The Debian 1.2 disk, 1/2 R1:6]/debian/", (std::string)U); // no brackets U = URI("cdrom:Foo Bar Cow/debian/"); EXPECT_EQ("cdrom", U.Access); @@ -111,6 +122,7 @@ TEST(URITest, RFC2732) EXPECT_EQ(0, U.Port); EXPECT_EQ("Foo Bar Cow", U.Host); EXPECT_EQ("/debian/", U.Path); + EXPECT_EQ("cdrom://Foo Bar Cow/debian/", (std::string)U); // percent encoded U = URI("ftp://foo:b%40r@example.org"); EXPECT_EQ("foo", U.User); |