Browse Source

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.
tags/debian/1.0.2
David Kalnischkies 7 years ago
parent
commit
f00832cc27
29 changed files with 1918 additions and 2086 deletions
  1. +1
    -1
      debian/control
  2. +0
    -126
      test/libapt/assert.h
  3. +114
    -0
      test/libapt/cdrom_test.cc
  4. +0
    -26
      test/libapt/cdromfindmountpointfordevice_test.cc
  5. +100
    -69
      test/libapt/cdromfindpackages_test.cc
  6. +0
    -86
      test/libapt/cdromreducesourcelist_test.cc
  7. +38
    -15
      test/libapt/commandline_test.cc
  8. +0
    -41
      test/libapt/commandlineasstring_test.cc
  9. +120
    -65
      test/libapt/compareversion_test.cc
  10. +87
    -80
      test/libapt/configuration_test.cc
  11. +77
    -0
      test/libapt/file-helpers.cc
  12. +29
    -0
      test/libapt/file-helpers.h
  13. +131
    -141
      test/libapt/fileutl_test.cc
  14. +66
    -50
      test/libapt/getarchitectures_test.cc
  15. +223
    -189
      test/libapt/getlanguages_test.cc
  16. +95
    -64
      test/libapt/getlistoffilesindir_test.cc
  17. +116
    -97
      test/libapt/globalerror_test.cc
  18. +19
    -0
      test/libapt/gtest_runner.cc
  19. +155
    -119
      test/libapt/hashsums_test.cc
  20. +38
    -35
      test/libapt/indexcopytosourcelist_test.cc
  21. +8
    -18
      test/libapt/install_progress_test.cc
  22. +63
    -121
      test/libapt/makefile
  23. +233
    -225
      test/libapt/parsedepends_test.cc
  24. +0
    -135
      test/libapt/run-tests
  25. +16
    -33
      test/libapt/sourcelist_test.cc
  26. +57
    -81
      test/libapt/strutil_test.cc
  27. +18
    -44
      test/libapt/tagfile_test.cc
  28. +114
    -119
      test/libapt/uri_test.cc
  29. +0
    -106
      test/libapt/versions.lst

+ 1
- 1
debian/control View File

@@ -9,7 +9,7 @@ Build-Depends: dpkg-dev (>= 1.15.8), debhelper (>= 8.1.3~), libdb-dev,
gettext (>= 0.12), libcurl4-gnutls-dev (>= 7.19.4~),
zlib1g-dev, libbz2-dev, liblzma-dev,
xsltproc, docbook-xsl, docbook-xml, po4a (>= 0.34-2),
autotools-dev, autoconf, automake
autotools-dev, autoconf, automake, libgtest-dev
Build-Depends-Indep: doxygen, debiandoc-sgml, graphviz
Build-Conflicts: autoconf2.13, automake1.4
Vcs-Git: git://anonscm.debian.org/apt/apt.git


+ 0
- 126
test/libapt/assert.h View File

@@ -1,126 +0,0 @@
#include <iostream>
#include <cstdlib>

#include <apt-pkg/macros.h>
#include <apt-pkg/error.h>

#if __GNUC__ >= 4
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif

#define equals(x,y) assertEquals(y, x, __LINE__)
#define equalsNot(x,y) assertEqualsNot(y, x, __LINE__)

template < typename X, typename Y >
APT_NORETURN void OutputAssertEqual(X expect, char const* compare, Y get, unsigned long const &line) {
std::cerr << "Test FAILED: »" << expect << "« " << compare << " »" << get << "« at line " << line << std::endl;
_error->DumpErrors(std::cerr);
std::exit(EXIT_FAILURE);
}

template < typename X, typename Y >
void assertEquals(X expect, Y get, unsigned long const &line) {
if (expect == get)
return;
OutputAssertEqual(expect, "==", get, line);
}

template < typename X, typename Y >
void assertEqualsNot(X expect, Y get, unsigned long const &line) {
if (expect != get)
return;
OutputAssertEqual(expect, "!=", get, line);
}

void assertEquals(unsigned int const &expect, int const &get, unsigned long const &line) {
if (get < 0)
OutputAssertEqual(expect, "==", get, line);
assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
}

void assertEquals(int const &expect, unsigned int const &get, unsigned long const &line) {
if (expect < 0)
OutputAssertEqual(expect, "==", get, line);
assertEquals<unsigned int const&, unsigned int const&>(expect, get, line);
}

void assertEquals(unsigned long const &expect, int const &get, unsigned long const &line) {
if (get < 0)
OutputAssertEqual(expect, "==", get, line);
assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
}

void assertEquals(int const &expect, unsigned long const &get, unsigned long const &line) {
if (expect < 0)
OutputAssertEqual(expect, "==", get, line);
assertEquals<unsigned long const&, unsigned long const&>(expect, get, line);
}


#define equalsOr2(x,y,z) assertEqualsOr2(y, z, x, __LINE__)

template < typename X, typename Y >
void OutputAssertEqualOr2(X expect1, X expect2, char const* compare, Y get, unsigned long const &line) {
std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« " << compare << " »" << get << "« at line " << line << std::endl;
}

template < typename X, typename Y >
void assertEqualsOr2(X expect1, X expect2, Y get, unsigned long const &line) {
if (expect1 == get || expect2 == get)
return;
OutputAssertEqualOr2(expect1, expect2, "==", get, line);
}

void assertEqualsOr2(unsigned int const &expect1, unsigned int const &expect2, int const &get, unsigned long const &line) {
if (get < 0)
OutputAssertEqualOr2(expect1, expect2, "==", get, line);
assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
}

void assertEqualsOr2(int const &expect1, int const &expect2, unsigned int const &get, unsigned long const &line) {
if (expect1 < 0 && expect2 < 0)
OutputAssertEqualOr2(expect1, expect2, "==", get, line);
assertEqualsOr2<unsigned int const&, unsigned int const&>(expect1, expect2, get, line);
}


#define equalsOr3(w,x,y,z) assertEqualsOr3(x, y, z, w, __LINE__)

template < typename X, typename Y >
void OutputAssertEqualOr3(X expect1, X expect2, X expect3, char const* compare, Y get, unsigned long const &line) {
std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« " << compare << " »" << get << "« at line " << line << std::endl;
}

template < typename X, typename Y >
void assertEqualsOr3(X expect1, X expect2, X expect3, Y get, unsigned long const &line) {
if (expect1 == get || expect2 == get || expect3 == get)
return;
OutputAssertEqualOr3(expect1, expect2, expect3, "==", get, line);
}

#define equalsOr4(v,w,x,y,z) assertEqualsOr4(w, x, y, z, v, __LINE__)

template < typename X, typename Y >
void OutputAssertEqualOr4(X expect1, X expect2, X expect3, X expect4, char const* compare, Y get, unsigned long const &line) {
std::cerr << "Test FAILED: »" << expect1 << "« or »" << expect2 << "« or »" << expect3 << "« or »" << expect4 << "« " << compare << " »" << get << "« at line " << line << std::endl;
}

template < typename X, typename Y >
void assertEqualsOr4(X expect1, X expect2, X expect3, X expect4, Y get, unsigned long const &line) {
if (expect1 == get || expect2 == get || expect3 == get || expect4 == get)
return;
OutputAssertEqualOr4(expect1, expect2, expect3, expect4, "==", get, line);
}

// simple helper to quickly output a vectors
template < typename X >
void dumpVector(X vec) {
for (typename X::const_iterator v = vec.begin();
v != vec.end(); ++v)
std::cout << *v << std::endl;
}

#if __GNUC__ >= 4
#pragma GCC diagnostic pop
#endif

+ 114
- 0
test/libapt/cdrom_test.cc View File

@@ -0,0 +1,114 @@
#include <config.h>

#include <apt-pkg/configuration.h>
#include <apt-pkg/cdrom.h>
#include <apt-pkg/cdromutl.h>
#include <apt-pkg/fileutl.h>

#include <string>
#include <string.h>
#include <vector>

#include <gtest/gtest.h>

#include "file-helpers.h"

class Cdrom : public pkgCdrom {
public:
std::vector<std::string> ReduceSourcelist(std::string CD,std::vector<std::string> List) {
pkgCdrom::ReduceSourcelist(CD, List);
return List;
}
};

TEST(CDROMTest,ReduceSourcelist)
{
Cdrom cd;
std::vector<std::string> List;
std::string CD("/media/cdrom/");

std::vector<std::string> R = cd.ReduceSourcelist(CD, List);
EXPECT_TRUE(R.empty());

List.push_back(" wheezy main");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(1, R.size());
EXPECT_EQ(" wheezy main", R[0]);

List.push_back(" wheezy main");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(1, R.size());
EXPECT_EQ(" wheezy main", R[0]);

List.push_back(" wheezy contrib");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(1, R.size());
EXPECT_EQ(" wheezy contrib main", R[0]);

List.push_back(" wheezy-update contrib");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(2, R.size());
EXPECT_EQ(" wheezy contrib main", R[0]);
EXPECT_EQ(" wheezy-update contrib", R[1]);

List.push_back(" wheezy-update contrib");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(2, R.size());
EXPECT_EQ(" wheezy contrib main", R[0]);
EXPECT_EQ(" wheezy-update contrib", R[1]);

List.push_back(" wheezy-update non-free");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(2, R.size());
EXPECT_EQ(" wheezy contrib main", R[0]);
EXPECT_EQ(" wheezy-update contrib non-free", R[1]);

List.push_back(" wheezy-update main");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(2, R.size());
EXPECT_EQ(" wheezy contrib main", R[0]);
EXPECT_EQ(" wheezy-update contrib main non-free", R[1]);

List.push_back(" wheezy non-free");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(2, R.size());
EXPECT_EQ(" wheezy contrib main non-free", R[0]);
EXPECT_EQ(" wheezy-update contrib main non-free", R[1]);

List.push_back(" sid main");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(3, R.size());
EXPECT_EQ(" sid main", R[0]);
EXPECT_EQ(" wheezy contrib main non-free", R[1]);
EXPECT_EQ(" wheezy-update contrib main non-free", R[2]);

List.push_back(" sid main-reduce");
R = cd.ReduceSourcelist(CD, List);
ASSERT_EQ(3, R.size());
EXPECT_EQ(" sid main main-reduce", R[0]);
EXPECT_EQ(" wheezy contrib main non-free", R[1]);
EXPECT_EQ(" wheezy-update contrib main non-free", R[2]);
}
TEST(CDROMTest, FindMountPointForDevice)
{
char * tempfile;
FileFd fd;
createTemporaryFile("mountpoints", fd, &tempfile,
"rootfs / rootfs rw 0 0\n"
"sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
"sysfs0 /sys0 sysfs rw,nosuid,nodev,noexec,relatime 0 0\n"
"/dev/disk/by-uuid/fadcbc52-6284-4874-aaaa-dcee1f05fe21 / ext4 rw,relatime,errors=remount-ro,data=ordered 0 0\n"
"/dev/sda1 /boot/efi vfat rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro,rw,nosuid,nodev,noexec,relatime,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=lower,quiet,utf8,errors=remount-ro 0 0\n"
"tmpfs /tmp tmpfs rw,nosuid,nodev,relatime 0 0\n");
_config->Set("Dir::state::Mountpoints", tempfile);

EXPECT_EQ("/", FindMountPointForDevice("rootfs"));
EXPECT_EQ("/", FindMountPointForDevice("/dev/disk/by-uuid/fadcbc52-6284-4874-aaaa-dcee1f05fe21"));
EXPECT_EQ("/sys", FindMountPointForDevice("sysfs"));
EXPECT_EQ("/sys0", FindMountPointForDevice("sysfs0"));
EXPECT_EQ("/boot/efi", FindMountPointForDevice("/dev/sda1"));
EXPECT_EQ("/tmp", FindMountPointForDevice("tmpfs"));

unlink(tempfile);
free(tempfile);
}

+ 0
- 26
test/libapt/cdromfindmountpointfordevice_test.cc View File

@@ -1,26 +0,0 @@
#include <config.h>

#include <apt-pkg/cdromutl.h>
#include <apt-pkg/configuration.h>

#include <string>
#include <vector>

#include "assert.h"

int main(int argc, char const *argv[]) {
if (argc != 2) {
std::cout << "One parameter expected - given " << argc << std::endl;
return 100;
}

_config->Set("Dir::state::Mountpoints", argv[1]);
equals("/", FindMountPointForDevice("rootfs"));
equals("/", FindMountPointForDevice("/dev/disk/by-uuid/fadcbc52-6284-4874-aaaa-dcee1f05fe21"));
equals("/sys", FindMountPointForDevice("sysfs"));
equals("/sys0", FindMountPointForDevice("sysfs0"));
equals("/boot/efi", FindMountPointForDevice("/dev/sda1"));
equals("/tmp", FindMountPointForDevice("tmpfs"));

return 0;
}

+ 100
- 69
test/libapt/cdromfindpackages_test.cc View File

@@ -2,6 +2,7 @@

#include <apt-pkg/cdrom.h>
#include <apt-pkg/error.h>
#include <apt-pkg/fileutl.h>

#include <algorithm>
#include <string>
@@ -9,82 +10,112 @@
#include <stddef.h>
#include <iostream>

#include "assert.h"
#include <gtest/gtest.h>

#include "file-helpers.h"

class Cdrom : public pkgCdrom {
public:
bool FindPackages(std::string const &CD,
std::vector<std::string> &List,
std::vector<std::string> &SList,
std::vector<std::string> &SigList,
std::vector<std::string> &TransList,
std::string &InfoDir) {
bool const result = pkgCdrom::FindPackages(CD, List, SList, SigList, TransList, InfoDir, NULL, 0);
std::sort(List.begin(), List.end());
std::sort(SList.begin(), SList.end());
std::sort(SigList.begin(), SigList.end());
std::sort(TransList.begin(), TransList.end());
return result;
}
public:
bool FindPackages(std::string const &CD,
std::vector<std::string> &List,
std::vector<std::string> &SList,
std::vector<std::string> &SigList,
std::vector<std::string> &TransList,
std::string &InfoDir) {
std::string const startdir = SafeGetCWD();
EXPECT_FALSE(startdir.empty());
bool const result = pkgCdrom::FindPackages(CD, List, SList, SigList, TransList, InfoDir, NULL, 0);
std::sort(List.begin(), List.end());
std::sort(SList.begin(), SList.end());
std::sort(SigList.begin(), SigList.end());
std::sort(TransList.begin(), TransList.end());
EXPECT_EQ(0, chdir(startdir.c_str()));
return result;
}

bool DropRepeats(std::vector<std::string> &List, char const *Name) {
return pkgCdrom::DropRepeats(List, Name);
}
using pkgCdrom::DropRepeats;
};

int main(int argc, char const *argv[]) {
if (argc != 2) {
std::cout << "One parameter expected - given " << argc << std::endl;
return 100;
}
TEST(CDROMTest,FindPackages)
{
std::string path;
createTemporaryDirectory("findpackage", path);

createDirectory(path, ".disk");
createDirectory(path, "pool");
createDirectory(path, "dists/stable/main/binary-i386");
createDirectory(path, "dists/stable/main/source");
createDirectory(path, "dists/stable/contrib/binary-amd64");
createDirectory(path, "dists/stable/contrib/binary-all");
createDirectory(path, "dists/unstable/main/binary-i386");
createDirectory(path, "dists/unstable/main/i18n");
createDirectory(path, "dists/unstable/main/source");
createDirectory(path, "dists/broken/non-free/source");
createFile(path, "dists/broken/.aptignr");
createFile(path, "dists/stable/main/binary-i386/Packages");
createFile(path, "dists/stable/main/binary-i386/Packages.bz2");
createFile(path, "dists/stable/main/source/Sources.xz");
createFile(path, "dists/stable/contrib/binary-amd64/Packages");
createFile(path, "dists/stable/contrib/binary-amd64/Packages.gz");
createFile(path, "dists/stable/contrib/binary-all/Packages");
createFile(path, "dists/unstable/main/binary-i386/Packages.xz");
createFile(path, "dists/unstable/main/binary-i386/Packages.lzma");
createFile(path, "dists/unstable/main/i18n/Translation-en");
createFile(path, "dists/unstable/main/i18n/Translation-de.bz2");
createFile(path, "dists/unstable/main/source/Sources.xz");
createFile(path, "dists/broken/non-free/source/Sources.gz");
createFile(path, "dists/stable/Release.gpg");
createFile(path, "dists/stable/Release");
createFile(path, "dists/unstable/InRelease");
createFile(path, "dists/broken/Release.gpg");
createLink(path, "dists/unstable", "dists/sid");

Cdrom cd;
std::vector<std::string> Packages, Sources, Signatur, Translation;
std::string InfoDir;
std::string path = argv[1];
equals(true, cd.FindPackages(path, Packages, Sources, Signatur, Translation, InfoDir));
equals(4, Packages.size());
equals(path + "/dists/sid/main/binary-i386/", Packages[0]);
equals(path + "/dists/stable/contrib/binary-amd64/", Packages[1]);
equals(path + "/dists/stable/main/binary-i386/", Packages[2]);
equals(path + "/dists/unstable/main/binary-i386/", Packages[3]);
equals(3, Sources.size());
equals(path + "/dists/sid/main/source/", Sources[0]);
equals(path + "/dists/stable/main/source/", Sources[1]);
equals(path + "/dists/unstable/main/source/", Sources[2]);
equals(3, Signatur.size());
equals(path + "/dists/sid/", Signatur[0]);
equals(path + "/dists/stable/", Signatur[1]);
equals(path + "/dists/unstable/", Signatur[2]);
equals(4, Translation.size());
equals(path + "/dists/sid/main/i18n/Translation-de", Translation[0]);
equals(path + "/dists/sid/main/i18n/Translation-en", Translation[1]);
equals(path + "/dists/unstable/main/i18n/Translation-de", Translation[2]);
equals(path + "/dists/unstable/main/i18n/Translation-en", Translation[3]);
equals(path + "/.disk/", InfoDir);
Cdrom cd;
std::vector<std::string> Packages, Sources, Signatur, Translation;
std::string InfoDir;
EXPECT_TRUE(cd.FindPackages(path, Packages, Sources, Signatur, Translation, InfoDir));
EXPECT_EQ(4, Packages.size());
EXPECT_EQ(path + "/dists/sid/main/binary-i386/", Packages[0]);
EXPECT_EQ(path + "/dists/stable/contrib/binary-amd64/", Packages[1]);
EXPECT_EQ(path + "/dists/stable/main/binary-i386/", Packages[2]);
EXPECT_EQ(path + "/dists/unstable/main/binary-i386/", Packages[3]);
EXPECT_EQ(3, Sources.size());
EXPECT_EQ(path + "/dists/sid/main/source/", Sources[0]);
EXPECT_EQ(path + "/dists/stable/main/source/", Sources[1]);
EXPECT_EQ(path + "/dists/unstable/main/source/", Sources[2]);
EXPECT_EQ(3, Signatur.size());
EXPECT_EQ(path + "/dists/sid/", Signatur[0]);
EXPECT_EQ(path + "/dists/stable/", Signatur[1]);
EXPECT_EQ(path + "/dists/unstable/", Signatur[2]);
EXPECT_EQ(4, Translation.size());
EXPECT_EQ(path + "/dists/sid/main/i18n/Translation-de", Translation[0]);
EXPECT_EQ(path + "/dists/sid/main/i18n/Translation-en", Translation[1]);
EXPECT_EQ(path + "/dists/unstable/main/i18n/Translation-de", Translation[2]);
EXPECT_EQ(path + "/dists/unstable/main/i18n/Translation-en", Translation[3]);
EXPECT_EQ(path + "/.disk/", InfoDir);

cd.DropRepeats(Packages, "Packages");
cd.DropRepeats(Sources, "Sources");
_error->PushToStack();
cd.DropRepeats(Signatur, "InRelease");
cd.DropRepeats(Signatur, "Release.gpg");
_error->RevertToStack();
_error->DumpErrors();
cd.DropRepeats(Translation, "");
cd.DropRepeats(Packages, "Packages");
cd.DropRepeats(Sources, "Sources");
_error->PushToStack();
cd.DropRepeats(Signatur, "InRelease");
cd.DropRepeats(Signatur, "Release.gpg");
_error->RevertToStack();
_error->DumpErrors();
cd.DropRepeats(Translation, "");

equals(3, Packages.size());
equals(path + "/dists/stable/contrib/binary-amd64/", Packages[0]);
equals(path + "/dists/stable/main/binary-i386/", Packages[1]);
equals(path + "/dists/unstable/main/binary-i386/", Packages[2]);
equals(2, Sources.size());
equals(path + "/dists/stable/main/source/", Sources[0]);
equals(path + "/dists/unstable/main/source/", Sources[1]);
equals(2, Signatur.size());
equals(path + "/dists/stable/", Signatur[0]);
equals(path + "/dists/unstable/", Signatur[1]);
equals(2, Translation.size());
equals(path + "/dists/unstable/main/i18n/Translation-de", Translation[0]);
equals(path + "/dists/unstable/main/i18n/Translation-en", Translation[1]);
EXPECT_EQ(3, Packages.size());
EXPECT_EQ(path + "/dists/stable/contrib/binary-amd64/", Packages[0]);
EXPECT_EQ(path + "/dists/stable/main/binary-i386/", Packages[1]);
EXPECT_EQ(path + "/dists/unstable/main/binary-i386/", Packages[2]);
EXPECT_EQ(2, Sources.size());
EXPECT_EQ(path + "/dists/stable/main/source/", Sources[0]);
EXPECT_EQ(path + "/dists/unstable/main/source/", Sources[1]);
EXPECT_EQ(2, Signatur.size());
EXPECT_EQ(path + "/dists/stable/", Signatur[0]);
EXPECT_EQ(path + "/dists/unstable/", Signatur[1]);
EXPECT_EQ(2, Translation.size());
EXPECT_EQ(path + "/dists/unstable/main/i18n/Translation-de", Translation[0]);
EXPECT_EQ(path + "/dists/unstable/main/i18n/Translation-en", Translation[1]);

return 0;
removeDirectory(path);
}

+ 0
- 86
test/libapt/cdromreducesourcelist_test.cc View File

@@ -1,86 +0,0 @@
#include <config.h>

#include <apt-pkg/cdrom.h>

#include <string>
#include <vector>

#include "assert.h"

class Cdrom : public pkgCdrom {
public:
std::vector<std::string> ReduceSourcelist(std::string CD,std::vector<std::string> List) {
pkgCdrom::ReduceSourcelist(CD, List);
return List;
}
};

int main() {
Cdrom cd;
std::vector<std::string> List;
std::string CD("/media/cdrom/");

std::vector<std::string> R = cd.ReduceSourcelist(CD, List);
equals(R.empty(), true);

List.push_back(" wheezy main");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 1);
equals(R[0], " wheezy main");

List.push_back(" wheezy main");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 1);
equals(R[0], " wheezy main");

List.push_back(" wheezy contrib");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 1);
equals(R[0], " wheezy contrib main");

List.push_back(" wheezy-update contrib");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 2);
equals(R[0], " wheezy contrib main");
equals(R[1], " wheezy-update contrib");

List.push_back(" wheezy-update contrib");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 2);
equals(R[0], " wheezy contrib main");
equals(R[1], " wheezy-update contrib");

List.push_back(" wheezy-update non-free");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 2);
equals(R[0], " wheezy contrib main");
equals(R[1], " wheezy-update contrib non-free");

List.push_back(" wheezy-update main");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 2);
equals(R[0], " wheezy contrib main");
equals(R[1], " wheezy-update contrib main non-free");

List.push_back(" wheezy non-free");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 2);
equals(R[0], " wheezy contrib main non-free");
equals(R[1], " wheezy-update contrib main non-free");

List.push_back(" sid main");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 3);
equals(R[0], " sid main");
equals(R[1], " wheezy contrib main non-free");
equals(R[2], " wheezy-update contrib main non-free");

List.push_back(" sid main-reduce");
R = cd.ReduceSourcelist(CD, List);
equals(R.size(), 3);
equals(R[0], " sid main main-reduce");
equals(R[1], " wheezy contrib main non-free");
equals(R[2], " wheezy-update contrib main non-free");

return 0;
}

+ 38
- 15
test/libapt/commandline_test.cc View File

@@ -3,33 +3,56 @@
#include <apt-pkg/cmndline.h>
#include <apt-pkg/configuration.h>

#include "assert.h"

int main()
#include <gtest/gtest.h>

class CLT: public CommandLine {
public:
std::string static AsString(const char * const * const argv,
unsigned int const argc) {
std::string const static conf = "Commandline::AsString";
_config->Clear(conf);
SaveInConfig(argc, argv);
return _config->Find(conf);
}
};

#define EXPECT_CMD(x, ...) { const char * const argv[] = { __VA_ARGS__ }; EXPECT_EQ(x, CLT::AsString(argv, sizeof(argv)/sizeof(argv[0]))); }

TEST(CommandLineTest,SaveInConfig)
{
EXPECT_CMD("apt-get install -sf",
"apt-get", "install", "-sf");
EXPECT_CMD("apt-cache -s apt -so Debug::test=Test",
"apt-cache", "-s", "apt", "-so", "Debug::test=Test");
EXPECT_CMD("apt-cache -s apt -so Debug::test=\"Das ist ein Test\"",
"apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test");
EXPECT_CMD("apt-cache -s apt --hallo test=1.0",
"apt-cache", "-s", "apt", "--hallo", "test=1.0");
}
TEST(CommandLineTest,Parsing)
{
CommandLine::Args Args[] = {
{ 't', 0, "Test::Worked", 0 },
{ 'z', "zero", "Test::Zero", 0 },
{0,0,0,0}
};
CommandLine CmdL(Args,_config);
::Configuration c;
CommandLine CmdL(Args, &c);

char const * argv[] = { "test", "--zero", "-t" };
CmdL.Parse(3 , argv);
equals(true, _config->FindB("Test::Worked", false));
equals(true, _config->FindB("Test::Zero", false));
EXPECT_TRUE(c.FindB("Test::Worked", false));
EXPECT_TRUE(c.FindB("Test::Zero", false));

_config->Clear("Test");
equals(false, _config->FindB("Test::Worked", false));
equals(false, _config->FindB("Test::Zero", false));
c.Clear("Test");
EXPECT_FALSE(c.FindB("Test::Worked", false));
EXPECT_FALSE(c.FindB("Test::Zero", false));

_config->Set("Test::Zero", true);
equals(true, _config->FindB("Test::Zero", false));
c.Set("Test::Zero", true);
EXPECT_TRUE(c.FindB("Test::Zero", false));

char const * argv2[] = { "test", "--no-zero", "-t" };
CmdL.Parse(3 , argv2);
equals(true, _config->FindB("Test::Worked", false));
equals(false, _config->FindB("Test::Zero", false));

return 0;
EXPECT_TRUE(c.FindB("Test::Worked", false));
EXPECT_FALSE(c.FindB("Test::Zero", false));
}

+ 0
- 41
test/libapt/commandlineasstring_test.cc View File

@@ -1,41 +0,0 @@
#include <config.h>

#include <apt-pkg/cmndline.h>
#include <apt-pkg/configuration.h>

#include <string>

#include "assert.h"

class CLT: public CommandLine {

public:
std::string static AsString(const char * const * const argv,
unsigned int const argc) {
std::string const static conf = "Commandline::AsString";
_config->Clear(conf);
SaveInConfig(argc, argv);
return _config->Find(conf);
}
};

#define CMD(y,z) equals(CLT::AsString(argv, y), z);

int main() {
{
const char* const argv[] = {"apt-get", "install", "-sf"};
CMD(3, "apt-get install -sf");
}
{
const char* const argv[] = {"apt-cache", "-s", "apt", "-so", "Debug::test=Test"};
CMD(5, "apt-cache -s apt -so Debug::test=Test");
}
{
const char* const argv[] = {"apt-cache", "-s", "apt", "-so", "Debug::test=Das ist ein Test"};
CMD(5, "apt-cache -s apt -so Debug::test=\"Das ist ein Test\"");
}
{
const char* const argv[] = {"apt-cache", "-s", "apt", "--hallo", "test=1.0"};
CMD(5, "apt-cache -s apt --hallo test=1.0");
}
}

+ 120
- 65
test/libapt/compareversion_test.cc View File

@@ -28,9 +28,11 @@
#include <unistd.h>
#include <sys/wait.h>

#include <gtest/gtest.h>

using namespace std;

static bool callDPkg(const char *val, const char *ref, const char &op) {
static bool callDPKG(const char *val, const char *ref, const char &op) {
pid_t Process = ExecFork();
if (Process == 0)
{
@@ -49,77 +51,130 @@ static bool callDPkg(const char *val, const char *ref, const char &op) {
return WIFEXITED(Ret) == true && WEXITSTATUS(Ret) == 0;
}

static void assertVersion(int const &CurLine, string const &A, string const &B, int const &Expected) {
int Res = debVS.CmpVersion(A.c_str(), B.c_str());
bool const dpkg = callDPkg(A.c_str(),B.c_str(), Expected);
Res = (Res < 0) ? -1 : ( (Res > 0) ? 1 : Res);

if (Res != Expected)
_error->Error("Comparison failed on line %u. '%s' '%s' '%s' %i != %i",CurLine,A.c_str(),((Expected == 1) ? "<<" : ( (Expected == 0) ? "=" : ">>")) ,B.c_str(),Res,Expected);
if (dpkg == false)
_error->Error("DPkg differ with line: %u. '%s' '%s' '%s' == false",CurLine,A.c_str(),((Expected == 1) ? "<<" : ( (Expected == 0) ? "=" : ">>")),B.c_str());
#define EXPECT_VERSION_PART(A, compare, B) \
{ \
int Res = debVS.CmpVersion(A, B); \
Res = (Res < 0) ? -1 : ( (Res > 0) ? 1 : Res); \
EXPECT_EQ(compare, Res) << "APT: A: »" << A << "« B: »" << B << "«"; \
EXPECT_PRED3(callDPKG, A, B, compare); \
}
#define EXPECT_VERSION(A, compare, B) \
EXPECT_VERSION_PART(A, compare, B); \
EXPECT_VERSION_PART(B, compare * -1, A)

// History-Remark: The versions used to be specified in a versions.lst file

enum CompareVersionType { LESS = -1, GREATER = 1, EQUAL = 0 };

static bool RunTest(const char *File)
TEST(CompareVersionTest,Basic)
{
if (FileExists(File) == false)
return _error->Error("Versiontestfile %s doesn't exist!", File);
EXPECT_VERSION("7.6p2-4", GREATER, "7.6-0");
EXPECT_VERSION("1.0.3-3", GREATER, "1.0-1");
EXPECT_VERSION("1.3", GREATER, "1.2.2-2");
EXPECT_VERSION("1.3", GREATER, "1.2.2");

ifstream F(File,ios::in);
if (!F != 0)
return false;
/* disabled as dpkg doesn't like them… (versions have to start with a number)
EXPECT_VERSION("-", LESS, ".");
EXPECT_VERSION("p", LESS, "-");
EXPECT_VERSION("a", LESS, "-");
EXPECT_VERSION("z", LESS, "-");
EXPECT_VERSION("a", LESS, ".");
EXPECT_VERSION("z", LESS, ".");
// */

char Buffer[300];
int CurLine = 0;
while (1)
{
F.getline(Buffer,sizeof(Buffer));
CurLine++;
if (F.eof() != 0)
return true;
if (!F != 0)
return _error->Error("Line %u in %s is too long",CurLine,File);

// Comment
if (Buffer[0] == '#' || Buffer[0] == 0)
continue;
// First version
char *I;
char *Start = Buffer;
for (I = Buffer; *I != 0 && *I != ' '; I++);
string A(Start, I - Start);

if (*I == 0)
return _error->Error("Invalid line %u",CurLine);
// Second version
I++;
Start = I;
for (I = Start; *I != 0 && *I != ' '; I++);
string B(Start,I - Start);
if (*I == 0 || I[1] == 0)
return _error->Error("Invalid line %u",CurLine);
// Result
I++;
int const Expected = atoi(I);
assertVersion(CurLine, A, B, Expected);
// Check the reverse as well
assertVersion(CurLine, B, A, Expected*-1);
}
/* disabled as dpkg doesn't like them… (versions have to start with a number)
EXPECT_VERSION("III-alpha9.8", LESS, "III-alpha9.8-1.5");
// */

// Test some properties of text strings
EXPECT_VERSION("0-pre", EQUAL, "0-pre");
EXPECT_VERSION("0-pre", LESS, "0-pree");

EXPECT_VERSION("1.1.6r2-2", GREATER, "1.1.6r-1");
EXPECT_VERSION("2.6b2-1", GREATER, "2.6b-2");

EXPECT_VERSION("98.1p5-1", LESS, "98.1-pre2-b6-2");
EXPECT_VERSION("0.4a6-2", GREATER, "0.4-1");

EXPECT_VERSION("1:3.0.5-2", LESS, "1:3.0.5.1");
}
TEST(CompareVersionTest,Epochs)
{
EXPECT_VERSION("1:0.4", GREATER, "10.3");
EXPECT_VERSION("1:1.25-4", LESS, "1:1.25-8");
EXPECT_VERSION("0:1.18.36", EQUAL, "1.18.36");

EXPECT_VERSION("1.18.36", GREATER, "1.18.35");
EXPECT_VERSION("0:1.18.36", GREATER, "1.18.35");
}
TEST(CompareVersionTest,Strangeness)
{
// Funky, but allowed, characters in upstream version
EXPECT_VERSION("9:1.18.36:5.4-20", LESS, "10:0.5.1-22");
EXPECT_VERSION("9:1.18.36:5.4-20", LESS, "9:1.18.36:5.5-1");
EXPECT_VERSION("9:1.18.36:5.4-20", LESS, " 9:1.18.37:4.3-22");
EXPECT_VERSION("1.18.36-0.17.35-18", GREATER, "1.18.36-19");

// Junk
EXPECT_VERSION("1:1.2.13-3", LESS, "1:1.2.13-3.1");
EXPECT_VERSION("2.0.7pre1-4", LESS, "2.0.7r-1");

int main(int argc, char *argv[])
// if a version includes a dash, it should be the debrev dash - policy says so…
EXPECT_VERSION("0:0-0-0", GREATER, "0-0");

// do we like strange versions? Yes we like strange versions…
EXPECT_VERSION("0", EQUAL, "0");
EXPECT_VERSION("0", EQUAL, "00");
}
TEST(CompareVersionTest,DebianBug)
{
// #205960
EXPECT_VERSION("3.0~rc1-1", LESS, "3.0-1");
// #573592 - debian policy 5.6.12
EXPECT_VERSION("1.0", EQUAL, "1.0-0");
EXPECT_VERSION("0.2", LESS, "1.0-0");
EXPECT_VERSION("1.0", LESS, "1.0-0+b1");
EXPECT_VERSION("1.0", GREATER, "1.0-0~");
}
TEST(CompareVersionTest,CuptTests)
{
if (argc != 2)
return 1;
else
RunTest(argv[1]);

// Print any errors or warnings found
_error->DumpErrors();
return 0;
// "steal" the testcases from (old perl) cupt
EXPECT_VERSION("1.2.3", EQUAL, "1.2.3"); // identical
EXPECT_VERSION("4.4.3-2", EQUAL, "4.4.3-2"); // identical
EXPECT_VERSION("1:2ab:5", EQUAL, "1:2ab:5"); // this is correct...
EXPECT_VERSION("7:1-a:b-5", EQUAL, "7:1-a:b-5"); // and this
EXPECT_VERSION("57:1.2.3abYZ+~-4-5", EQUAL, "57:1.2.3abYZ+~-4-5"); // and those too
EXPECT_VERSION("1.2.3", EQUAL, "0:1.2.3"); // zero epoch
EXPECT_VERSION("1.2.3", EQUAL, "1.2.3-0"); // zero revision
EXPECT_VERSION("009", EQUAL, "9"); // zeroes…
EXPECT_VERSION("009ab5", EQUAL, "9ab5"); // there as well
EXPECT_VERSION("1.2.3", LESS, "1.2.3-1"); // added non-zero revision
EXPECT_VERSION("1.2.3", LESS, "1.2.4"); // just bigger
EXPECT_VERSION("1.2.4", GREATER, "1.2.3"); // order doesn't matter
EXPECT_VERSION("1.2.24", GREATER, "1.2.3"); // bigger, eh?
EXPECT_VERSION("0.10.0", GREATER, "0.8.7"); // bigger, eh?
EXPECT_VERSION("3.2", GREATER, "2.3"); // major number rocks
EXPECT_VERSION("1.3.2a", GREATER, "1.3.2"); // letters rock
EXPECT_VERSION("0.5.0~git", LESS, "0.5.0~git2"); // numbers rock
EXPECT_VERSION("2a", LESS, "21"); // but not in all places
EXPECT_VERSION("1.3.2a", LESS, "1.3.2b"); // but there is another letter
EXPECT_VERSION("1:1.2.3", GREATER, "1.2.4"); // epoch rocks
EXPECT_VERSION("1:1.2.3", LESS, "1:1.2.4"); // bigger anyway
EXPECT_VERSION("1.2a+~bCd3", LESS, "1.2a++"); // tilde doesn't rock
EXPECT_VERSION("1.2a+~bCd3", GREATER, "1.2a+~"); // but first is longer!
EXPECT_VERSION("5:2", GREATER, "304-2"); // epoch rocks
EXPECT_VERSION("5:2", LESS, "304:2"); // so big epoch?
EXPECT_VERSION("25:2", GREATER, "3:2"); // 25 > 3, obviously
EXPECT_VERSION("1:2:123", LESS, "1:12:3"); // 12 > 2
EXPECT_VERSION("1.2-5", LESS, "1.2-3-5"); // 1.2 < 1.2-3
EXPECT_VERSION("5.10.0", GREATER, "5.005"); // preceding zeroes don't matters
EXPECT_VERSION("3a9.8", LESS, "3.10.2"); // letters are before all letter symbols
EXPECT_VERSION("3a9.8", GREATER, "3~10"); // but after the tilde
EXPECT_VERSION("1.4+OOo3.0.0~", LESS, "1.4+OOo3.0.0-4"); // another tilde check
EXPECT_VERSION("2.4.7-1", LESS, "2.4.7-z"); // revision comparing
EXPECT_VERSION("1.002-1+b2", GREATER, "1.00"); // whatever...
/* disabled as dpkg doesn't like them… (versions with illegal char)
EXPECT_VERSION("2.2.4-47978_Debian_lenny", EQUAL, "2.2.4-47978_Debian_lenny"); // and underscore...
// */
}

+ 87
- 80
test/libapt/configuration_test.cc View File

@@ -5,135 +5,142 @@
#include <string>
#include <vector>

#include "assert.h"
#include <gtest/gtest.h>

int main() {
//FIXME: Test for configuration file parsing;
// currently only integration/ tests test them implicitly

TEST(ConfigurationTest,Lists)
{
Configuration Cnf;
std::vector<std::string> fds;

Cnf.Set("APT::Keep-Fds::",28);
Cnf.Set("APT::Keep-Fds::",17);
Cnf.Set("APT::Keep-Fds::2",47);
Cnf.Set("APT::Keep-Fds::","broken");
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds[0], "28");
equals(fds[1], "17");
equals(fds[2], "47");
equals(fds[3], "broken");
equals(fds.size(), 4);
equals(Cnf.Exists("APT::Keep-Fds::2"), true);
equals(Cnf.Find("APT::Keep-Fds::2"), "47");
equals(Cnf.FindI("APT::Keep-Fds::2"), 47);
equals(Cnf.Exists("APT::Keep-Fds::3"), false);
equals(Cnf.Find("APT::Keep-Fds::3"), "");
equals(Cnf.FindI("APT::Keep-Fds::3", 56), 56);
equals(Cnf.Find("APT::Keep-Fds::3", "not-set"), "not-set");
std::vector<std::string> fds = Cnf.FindVector("APT::Keep-Fds");
ASSERT_EQ(4, fds.size());
EXPECT_EQ("28", fds[0]);
EXPECT_EQ("17", fds[1]);
EXPECT_EQ("47", fds[2]);
EXPECT_EQ("broken", fds[3]);

EXPECT_TRUE(Cnf.Exists("APT::Keep-Fds::2"));
EXPECT_EQ("47", Cnf.Find("APT::Keep-Fds::2"));
EXPECT_EQ(47, Cnf.FindI("APT::Keep-Fds::2"));
EXPECT_FALSE(Cnf.Exists("APT::Keep-Fds::3"));
EXPECT_EQ("", Cnf.Find("APT::Keep-Fds::3"));
EXPECT_EQ(56, Cnf.FindI("APT::Keep-Fds::3", 56));
EXPECT_EQ("not-set", Cnf.Find("APT::Keep-Fds::3", "not-set"));

Cnf.Clear("APT::Keep-Fds::2");
EXPECT_TRUE(Cnf.Exists("APT::Keep-Fds::2"));
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds[0], "28");
equals(fds[1], "17");
equals(fds[2], "");
equals(fds[3], "broken");
equals(fds.size(), 4);
equals(Cnf.Exists("APT::Keep-Fds::2"), true);
ASSERT_EQ(4, fds.size());
EXPECT_EQ("28", fds[0]);
EXPECT_EQ("17", fds[1]);
EXPECT_EQ("", fds[2]);
EXPECT_EQ("broken", fds[3]);

Cnf.Clear("APT::Keep-Fds",28);
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds[0], "17");
equals(fds[1], "");
equals(fds[2], "broken");
equals(fds.size(), 3);
ASSERT_EQ(3, fds.size());
EXPECT_EQ("17", fds[0]);
EXPECT_EQ("", fds[1]);
EXPECT_EQ("broken", fds[2]);

Cnf.Clear("APT::Keep-Fds","");
equals(Cnf.Exists("APT::Keep-Fds::2"), false);
EXPECT_FALSE(Cnf.Exists("APT::Keep-Fds::2"));

Cnf.Clear("APT::Keep-Fds",17);
Cnf.Clear("APT::Keep-Fds","broken");
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds.empty(), true);
EXPECT_TRUE(fds.empty());

Cnf.Set("APT::Keep-Fds::",21);
Cnf.Set("APT::Keep-Fds::",42);
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds[0], "21");
equals(fds[1], "42");
equals(fds.size(), 2);
ASSERT_EQ(2, fds.size());
EXPECT_EQ("21", fds[0]);
EXPECT_EQ("42", fds[1]);

Cnf.Clear("APT::Keep-Fds");
fds = Cnf.FindVector("APT::Keep-Fds");
equals(fds.empty(), true);
EXPECT_TRUE(fds.empty());
}
TEST(ConfigurationTest,Integers)
{
Configuration Cnf;

Cnf.CndSet("APT::Version", 42);
Cnf.CndSet("APT::Version", "66");
equals(Cnf.Find("APT::Version"), "42");
equals(Cnf.FindI("APT::Version"), 42);
equals(Cnf.Find("APT::Version", "33"), "42");
equals(Cnf.FindI("APT::Version", 33), 42);
equals(Cnf.Find("APT2::Version", "33"), "33");
equals(Cnf.FindI("APT2::Version", 33), 33);

equals(Cnf.FindFile("Dir::State"), "");
equals(Cnf.FindFile("Dir::Aptitude::State"), "");
EXPECT_EQ("42", Cnf.Find("APT::Version"));
EXPECT_EQ(42, Cnf.FindI("APT::Version"));
EXPECT_EQ("42", Cnf.Find("APT::Version", "33"));
EXPECT_EQ(42, Cnf.FindI("APT::Version", 33));
EXPECT_EQ("33", Cnf.Find("APT2::Version", "33"));
EXPECT_EQ(33, Cnf.FindI("APT2::Version", 33));
}
TEST(ConfigurationTest,DirsAndFiles)
{
Configuration Cnf;

EXPECT_EQ("", Cnf.FindFile("Dir::State"));
EXPECT_EQ("", Cnf.FindFile("Dir::Aptitude::State"));
Cnf.Set("Dir", "/srv/sid");
equals(Cnf.FindFile("Dir::State"), "");
EXPECT_EQ("", Cnf.FindFile("Dir::State"));
Cnf.Set("Dir::State", "var/lib/apt");
Cnf.Set("Dir::Aptitude::State", "var/lib/aptitude");
equals(Cnf.FindFile("Dir::State"), "/srv/sid/var/lib/apt");
equals(Cnf.FindFile("Dir::Aptitude::State"), "/srv/sid/var/lib/aptitude");
EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));

Cnf.Set("RootDir", "/");
equals(Cnf.FindFile("Dir::State"), "/srv/sid/var/lib/apt");
equals(Cnf.FindFile("Dir::Aptitude::State"), "/srv/sid/var/lib/aptitude");
EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
Cnf.Set("RootDir", "//./////.////");
equals(Cnf.FindFile("Dir::State"), "/srv/sid/var/lib/apt");
equals(Cnf.FindFile("Dir::Aptitude::State"), "/srv/sid/var/lib/aptitude");
EXPECT_EQ("/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
EXPECT_EQ("/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
Cnf.Set("RootDir", "/rootdir");
equals(Cnf.FindFile("Dir::State"), "/rootdir/srv/sid/var/lib/apt");
equals(Cnf.FindFile("Dir::Aptitude::State"), "/rootdir/srv/sid/var/lib/aptitude");
EXPECT_EQ("/rootdir/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
EXPECT_EQ("/rootdir/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));
Cnf.Set("RootDir", "/rootdir/");
equals(Cnf.FindFile("Dir::State"), "/rootdir/srv/sid/var/lib/apt");
equals(Cnf.FindFile("Dir::Aptitude::State"), "/rootdir/srv/sid/var/lib/aptitude");
EXPECT_EQ("/rootdir/srv/sid/var/lib/apt", Cnf.FindFile("Dir::State"));
EXPECT_EQ("/rootdir/srv/sid/var/lib/aptitude", Cnf.FindFile("Dir::Aptitude::State"));

Cnf.Set("Dir::State", "/dev/null");
Cnf.Set("Dir::State::lists", "lists/");
equals(Cnf.FindDir("Dir::State"), "/rootdir/dev/null");
equals(Cnf.FindDir("Dir::State::lists"), "/rootdir/dev/null");
Cnf.Set("Moo::Bar", "1");
Cnf.Clear();
equals(Cnf.Find("Moo::Bar"), "");
EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State"));
EXPECT_EQ("/rootdir/dev/null", Cnf.FindDir("Dir::State::lists"));
}
TEST(ConfigurationTest,Vector)
{
Configuration Cnf;

std::vector<std::string> vec = Cnf.FindVector("Test::Vector", "");
equals(vec.size(), 0);
EXPECT_EQ(0, vec.size());
vec = Cnf.FindVector("Test::Vector", "foo");
equals(vec.size(), 1);
equals(vec[0], "foo");
ASSERT_EQ(1, vec.size());
EXPECT_EQ("foo", vec[0]);
vec = Cnf.FindVector("Test::Vector", "foo,bar");
equals(vec.size(), 2);
equals(vec[0], "foo");
equals(vec[1], "bar");
EXPECT_EQ(2, vec.size());
EXPECT_EQ("foo", vec[0]);
EXPECT_EQ("bar", vec[1]);
Cnf.Set("Test::Vector::", "baz");
Cnf.Set("Test::Vector::", "bob");
Cnf.Set("Test::Vector::", "dob");
vec = Cnf.FindVector("Test::Vector");
equals(vec.size(), 3);
equals(vec[0], "baz");
equals(vec[1], "bob");
equals(vec[2], "dob");
ASSERT_EQ(3, vec.size());
EXPECT_EQ("baz", vec[0]);
EXPECT_EQ("bob", vec[1]);
EXPECT_EQ("dob", vec[2]);
vec = Cnf.FindVector("Test::Vector", "foo,bar");
equals(vec.size(), 3);
equals(vec[0], "baz");
equals(vec[1], "bob");
equals(vec[2], "dob");
ASSERT_EQ(3, vec.size());
EXPECT_EQ("baz", vec[0]);
EXPECT_EQ("bob", vec[1]);
EXPECT_EQ("dob", vec[2]);
Cnf.Set("Test::Vector", "abel,bravo");
vec = Cnf.FindVector("Test::Vector", "foo,bar");
equals(vec.size(), 2);
equals(vec[0], "abel");
equals(vec[1], "bravo");

//FIXME: Test for configuration file parsing;
// currently only integration/ tests test them implicitly

return 0;
ASSERT_EQ(2, vec.size());
EXPECT_EQ("abel", vec[0]);
EXPECT_EQ("bravo", vec[1]);
}

+ 77
- 0
test/libapt/file-helpers.cc View File

@@ -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);
}
}

+ 29
- 0
test/libapt/file-helpers.h View File

@@ -0,0 +1,29 @@
#ifndef APT_TESTS_FILE_HELPERS
#define APT_TESTS_FILE_HELPERS

#include <string>

#include <gtest/gtest.h>

class FileFd;

#define createTemporaryDirectory(id, dir) \
ASSERT_NO_FATAL_FAILURE(helperCreateTemporaryDirectory(id, dir))
void helperCreateTemporaryDirectory(std::string const &id, std::string &dir);
#define removeDirectory(dir) \
ASSERT_NO_FATAL_FAILURE(helperRemoveDirectory(dir))
void helperRemoveDirectory(std::string const &dir);
#define createFile(dir, name) \
ASSERT_NO_FATAL_FAILURE(helperCreateFile(dir, name))
void helperCreateFile(std::string const &dir, std::string const &name);
#define createDirectory(dir, name) \
ASSERT_NO_FATAL_FAILURE(helperCreateDirectory(dir, name))
void helperCreateDirectory(std::string const &dir, std::string const &name);
#define createLink(dir, targetname, linkname) \
ASSERT_NO_FATAL_FAILURE(helperCreateLink(dir, targetname, linkname))
void helperCreateLink(std::string const &dir, std::string const &targetname, std::string const &linkname);
#define createTemporaryFile(id, fd, filename, content) \
ASSERT_NO_FATAL_FAILURE(helperCreateTemporaryFile(id, fd, filename, content))
void helperCreateTemporaryFile(std::string const &id, FileFd &fd, char * * const filename, char const * const content);

#endif

+ 131
- 141
test/libapt/fileutl_test.cc View File

@@ -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);
}

+ 66
- 50
test/libapt/getarchitectures_test.cc View File

@@ -6,56 +6,72 @@
#include <string>
#include <vector>

#include "assert.h"
#include <gtest/gtest.h>

int main()
TEST(ArchitecturesTest,SimpleLists)
{
std::vector<std::string> vec;

_config->Set("APT::Architectures::1", "i386");
_config->Set("APT::Architectures::2", "amd64");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 2);
equals(vec[0], "i386");
equals(vec[1], "amd64");

_config->Set("APT::Architecture", "i386");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 2);
equals(vec[0], "i386");
equals(vec[1], "amd64");

_config->Set("APT::Architectures::2", "");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 1);
equals(vec[0], "i386");

_config->Set("APT::Architecture", "armel");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 2);
equals(vec[0], "armel");
equals(vec[1], "i386");

_config->Set("APT::Architectures::2", "armel");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 2);
equals(vec[0], "i386");
equals(vec[1], "armel");

_config->Set("APT::Architectures::2", "amd64");
_config->Set("APT::Architectures::3", "i386");
_config->Set("APT::Architectures::4", "armel");
_config->Set("APT::Architectures::5", "i386");
_config->Set("APT::Architectures::6", "amd64");
_config->Set("APT::Architectures::7", "armel");
_config->Set("APT::Architectures::8", "armel");
_config->Set("APT::Architectures::9", "amd64");
_config->Set("APT::Architectures::10", "amd64");
vec = APT::Configuration::getArchitectures(false);
equals(vec.size(), 3);
equals(vec[0], "i386");
equals(vec[1], "amd64");
equals(vec[2], "armel");

return 0;
_config->Clear();
std::vector<std::string> vec;

_config->Set("APT::Architectures::1", "i386");
_config->Set("APT::Architectures::2", "amd64");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("i386", vec[0]);
EXPECT_EQ("amd64", vec[1]);

_config->Set("APT::Architecture", "i386");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("i386", vec[0]);
EXPECT_EQ("amd64", vec[1]);

_config->Set("APT::Architectures::2", "");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(1, vec.size());
EXPECT_EQ("i386", vec[0]);

_config->Set("APT::Architecture", "armel");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("armel", vec[0]);
EXPECT_EQ("i386", vec[1]);

_config->Set("APT::Architectures::2", "armel");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("i386", vec[0]);
EXPECT_EQ("armel", vec[1]);

_config->Set("APT::Architectures", "armel,armhf");
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("armel", vec[0]);
EXPECT_EQ("armhf", vec[1]);
_config->Clear();
}
TEST(ArchitecturesTest,Duplicates)
{
_config->Clear();

_config->Set("APT::Architecture", "armel");
_config->Set("APT::Architectures::", "i386");
_config->Set("APT::Architectures::", "amd64");
_config->Set("APT::Architectures::", "i386");
_config->Set("APT::Architectures::", "armel");
_config->Set("APT::Architectures::", "i386");
_config->Set("APT::Architectures::", "amd64");
_config->Set("APT::Architectures::", "armel");
_config->Set("APT::Architectures::", "armel");
_config->Set("APT::Architectures::", "amd64");
_config->Set("APT::Architectures::", "amd64");
std::vector<std::string> vec = _config->FindVector("APT::Architectures");
ASSERT_EQ(10, vec.size());
vec = APT::Configuration::getArchitectures(false);
ASSERT_EQ(3, vec.size());
EXPECT_EQ("i386", vec[0]);
EXPECT_EQ("amd64", vec[1]);
EXPECT_EQ("armel", vec[2]);

_config->Clear();
}

+ 223
- 189
test/libapt/getlanguages_test.cc View File

@@ -2,199 +2,233 @@

#include <apt-pkg/aptconfiguration.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/fileutl.h>

#include "assert.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <gtest/gtest.h>

#include "file-helpers.h"

TEST(LanguagesTest,Environment)
{
_config->Clear();

char const* env[2];
env[0] = "de_DE.UTF-8";
env[1] = "";

std::vector<std::string> vec = APT::Configuration::getLanguages(false, false, env);
ASSERT_EQ(3 ,vec.size());
EXPECT_EQ("de_DE", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en", vec[2]);

// Special: Check if the cache is actually in use
env[0] = "en_GB.UTF-8";
vec = APT::Configuration::getLanguages(false, true, env);
ASSERT_EQ(3, vec.size());
EXPECT_EQ("de_DE", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en", vec[2]);

env[0] = "en_GB.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("en_GB", vec[0]);
EXPECT_EQ("en", vec[1]);

// esperanto
env[0] = "eo.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
ASSERT_EQ(2, vec.size());
EXPECT_EQ("eo", vec[0]);
EXPECT_EQ("en", vec[1]);

env[0] = "tr_DE@euro";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(3, vec.size());
EXPECT_EQ("tr_DE", vec[0]);
EXPECT_EQ("tr", vec[1]);
EXPECT_EQ("en", vec[2]);

env[0] = "de_NO";
env[1] = "de_NO:en_GB:nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(6, vec.size());
EXPECT_EQ("de_NO", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en_GB", vec[2]);
EXPECT_EQ("nb_NO", vec[3]);
EXPECT_EQ("nb", vec[4]);
EXPECT_EQ("en", vec[5]);

env[0] = "pt_PR.UTF-8";
env[1] = "";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(3, vec.size());
EXPECT_EQ("pt_PR", vec[0]);
EXPECT_EQ("pt", vec[1]);
EXPECT_EQ("en", vec[2]);

env[0] = "ast_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env); // bogus, but syntactical correct
EXPECT_EQ(3, vec.size());
EXPECT_EQ("ast_DE", vec[0]);
EXPECT_EQ("ast", vec[1]);
EXPECT_EQ("en", vec[2]);

env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(1, vec.size());
EXPECT_EQ("en", vec[0]);

_config->Set("Acquire::Languages", "none");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_TRUE(vec.empty());

_config->Set("Acquire::Languages", "environment");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(1, vec.size());
EXPECT_EQ("en", vec[0]);

_config->Set("Acquire::Languages", "de");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(1, vec.size());
EXPECT_EQ("de", vec[0]);

_config->Set("Acquire::Languages", "fr");
env[0] = "ast_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(1, vec.size());
EXPECT_EQ("fr", vec[0]);

_config->Set("Acquire::Languages", "environment,en");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(3, vec.size());
EXPECT_EQ("de_DE", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en", vec[2]);
_config->Set("Acquire::Languages", "");

_config->Set("Acquire::Languages::1", "environment");
_config->Set("Acquire::Languages::2", "en");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(3, vec.size());
EXPECT_EQ("de_DE", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en", vec[2]);

_config->Set("Acquire::Languages::3", "de");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
EXPECT_EQ(3, vec.size());
EXPECT_EQ("de_DE", vec[0]);
EXPECT_EQ("de", vec[1]);
EXPECT_EQ("en", vec[2]);

_config->Clear();
}

int main(int argc,char *argv[])
TEST(LanguagesTest,TranslationFiles)
{
if (argc != 2) {
std::cout << "One parameter expected - given " << argc << std::endl;
return 100;
}

char const* env[2];
env[0] = "de_DE.UTF-8";
env[1] = "";

std::vector<std::string> vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");

// Special: Check if the cache is actually in use
env[0] = "en_GB.UTF-8";
vec = APT::Configuration::getLanguages(false, true, env);
equals(vec.size(), 3);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");

env[0] = "en_GB.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 2);
equals(vec[0], "en_GB");
equals(vec[1], "en");

// esperanto
env[0] = "eo.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 2);
equals(vec[0], "eo");
equals(vec[1], "en");

env[0] = "tr_DE@euro";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "tr_DE");
equals(vec[1], "tr");
equals(vec[2], "en");

env[0] = "de_NO";
env[1] = "de_NO:en_GB:nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 6);
equals(vec[0], "de_NO");
equals(vec[1], "de");
equals(vec[2], "en_GB");
equals(vec[3], "nb_NO");
equals(vec[4], "nb");
equals(vec[5], "en");

env[0] = "pt_PR.UTF-8";
env[1] = "";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "pt_PR");
equals(vec[1], "pt");
equals(vec[2], "en");

env[0] = "ast_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env); // bogus, but syntactical correct
equals(vec.size(), 3);
equals(vec[0], "ast_DE");
equals(vec[1], "ast");
equals(vec[2], "en");

env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 1);
equals(vec[0], "en");

_config->Set("Acquire::Languages", "none");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 0);

_config->Set("Acquire::Languages", "environment");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 1);
equals(vec[0], "en");

_config->Set("Acquire::Languages", "de");
env[0] = "C";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 1);
equals(vec[0], "de");

_config->Set("Acquire::Languages", "fr");
env[0] = "ast_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 1);
equals(vec[0], "fr");

_config->Set("Acquire::Languages", "environment,en");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");
_config->Set("Acquire::Languages", "");

_config->Set("Acquire::Languages::1", "environment");
_config->Set("Acquire::Languages::2", "en");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");

_config->Set("Acquire::Languages::3", "de");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 3);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");

_config->Set("Dir::State::lists", argv[1]);
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 8);
equals(vec[0], "de_DE");
equals(vec[1], "de");
equals(vec[2], "en");
equals(vec[3], "none");
equalsOr4(vec[4], "pt", "tr", "ast_DE", "tlh_DE");
equalsOr4(vec[5], "tr", "pt", "ast_DE", "tlh_DE");
equalsOr4(vec[6], "tr", "pt", "ast_DE", "tlh_DE");
equalsOr4(vec[7], "tr", "pt", "ast_DE", "tlh_DE");
equalsNot(vec[4], vec[5]);
equalsNot(vec[4], vec[6]);
equalsNot(vec[4], vec[7]);
equalsNot(vec[5], vec[6]);
equalsNot(vec[5], vec[7]);
equalsNot(vec[6], vec[7]);

_config->Set("Acquire::Languages", "none");
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 1);
equals(vec[0], "none");
_config->Set("Acquire::Languages", "");

_config->Set("Dir::State::lists", "/non-existing-dir");
_config->Set("Acquire::Languages::1", "none");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 0);
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 2);
equals(vec[0], "en");
equals(vec[1], "de");

_config->Set("Acquire::Languages::1", "fr");
_config->Set("Acquire::Languages", "de_DE");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(false, false, env);
equals(vec.size(), 1);
equals(vec[0], "de_DE");

_config->Set("Acquire::Languages", "none");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 1);
equals(vec[0], "none");

_config->Set("Acquire::Languages", "");
//FIXME: Remove support for this deprecated setting
_config->Set("APT::Acquire::Translation", "ast_DE");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 2);
equals(vec[0], "ast_DE");
equals(vec[1], "en");
_config->Set("APT::Acquire::Translation", "none");
env[0] = "de_DE.UTF-8";
vec = APT::Configuration::getLanguages(true, false, env);
equals(vec.size(), 1);
equals(vec[0], "en");

return 0;
_config->Clear();
_config->Set("Acquire::Languages::1", "environment");
_config->Set("Acquire::Languages::2", "en");
_config->Set("Acquire::Languages::3", "de");

char const* env[2];
env[0] = "de_DE.UTF-8";
env[1] = "";

std::string tempdir;
createTemporaryDirectory("languages", tempdir);

#define createTranslation(lang) \
createFile(tempdir, std::string("/ftp.de.debian.org_debian_dists_sid_main_i18n_Translation-").append(lang));