diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/__init__.py | 1 | ||||
| -rw-r--r-- | tests/data/aptsources/sources.list (renamed from tests/data/sources.list) | 0 | ||||
| -rw-r--r-- | tests/data/aptsources/sources.list.testDistribution (renamed from tests/data/sources.list.testDistribution) | 6 | ||||
| -rw-r--r-- | tests/data/aptsources_ports/sources.list (renamed from tests/test-data-ports/sources.list) | 0 | ||||
| -rw-r--r-- | tests/getcache_mem_corruption.py | 24 | ||||
| -rw-r--r-- | tests/old/__init__.py | 13 | ||||
| -rw-r--r-- | tests/old/apt-test.py (renamed from tests/apt-test.py) | 0 | ||||
| -rw-r--r-- | tests/old/cache.py (renamed from tests/cache.py) | 4 | ||||
| -rw-r--r-- | tests/old/depcache.py (renamed from tests/depcache.py) | 4 | ||||
| -rw-r--r-- | tests/old/hashsum_test.data (renamed from tests/hashsum_test.data) | 0 | ||||
| -rw-r--r-- | tests/old/hashsum_test_with_zero.data (renamed from tests/hashsum_test_with_zero.data) | bin | 7 -> 7 bytes | |||
| -rw-r--r-- | tests/old/lock.py (renamed from tests/lock.py) | 0 | ||||
| -rwxr-xr-x | tests/old/memleak.py (renamed from tests/memleak.py) | 8 | ||||
| -rw-r--r-- | tests/old/pkgproblemresolver.py (renamed from tests/pkgproblemresolver.py) | 6 | ||||
| -rw-r--r-- | tests/old/pkgrecords.py (renamed from tests/pkgrecords.py) | 6 | ||||
| -rw-r--r-- | tests/old/pkgsrcrecords.py (renamed from tests/pkgsrcrecords.py) | 4 | ||||
| -rwxr-xr-x | tests/old/refcount.py (renamed from tests/refcount.py) | 0 | ||||
| -rwxr-xr-x | tests/old/test_debextract.py (renamed from tests/test_debextract.py) | 0 | ||||
| -rw-r--r-- | tests/old/test_enhances.py (renamed from tests/test_enhances.py) | 0 | ||||
| -rw-r--r-- | tests/old/test_extract_archive.py (renamed from tests/test_extract_archive.py) | 0 | ||||
| -rw-r--r-- | tests/old/test_hashsums.py (renamed from tests/test_hashsums.py) | 0 | ||||
| -rw-r--r-- | tests/test_all.py | 33 | ||||
| -rw-r--r-- | tests/test_aptsources.py | 49 | ||||
| -rw-r--r-- | tests/test_aptsources_ports.py | 32 | ||||
| -rw-r--r-- | tests/test_cache_invocation.py | 30 | ||||
| -rw-r--r-- | tests/test_deps.py | 119 | ||||
| -rw-r--r-- | tests/test_hashes.py | 113 |
27 files changed, 375 insertions, 77 deletions
diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..792d6005 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +# diff --git a/tests/data/sources.list b/tests/data/aptsources/sources.list index 5481d4f0..5481d4f0 100644 --- a/tests/data/sources.list +++ b/tests/data/aptsources/sources.list diff --git a/tests/data/sources.list.testDistribution b/tests/data/aptsources/sources.list.testDistribution index 0f40e85a..79224a34 100644 --- a/tests/data/sources.list.testDistribution +++ b/tests/data/aptsources/sources.list.testDistribution @@ -6,8 +6,8 @@ deb http://de.archive.ubuntu.com/ubuntu/ hardy-updates restricted deb http://de.archive.ubuntu.com/ubuntu/ hardy-security main deb http://de.archive.ubuntu.com/ubuntu/ hardy-security multiverse deb http://ftp.debian.org/debian sid main -deb http://ubuntu.cs.uaf.edu/ubuntu/ hardy main -deb http://ubuntu.cs.uaf.edu/ubuntu/ hardy-backports main +deb http://ftp.hosteurope.de/mirror/archive.ubuntu.com/ hardy main +deb http://ftp.hosteurope.de/mirror/archive.ubuntu.com/ hardy-backports main deb http://archive.ubuntu.com/ubuntu/ intrepid main deb cdrom:[Ubuntu 8.10 _Intrepid Ibex_ - Alpha]/ intrepid main -deb cdrom:[Ubuntu 8.04 _Hardy Heron_] hardy main
\ No newline at end of file +deb cdrom:[Ubuntu 8.04 _Hardy Heron_] hardy main diff --git a/tests/test-data-ports/sources.list b/tests/data/aptsources_ports/sources.list index a6b2f6ed..a6b2f6ed 100644 --- a/tests/test-data-ports/sources.list +++ b/tests/data/aptsources_ports/sources.list diff --git a/tests/getcache_mem_corruption.py b/tests/getcache_mem_corruption.py deleted file mode 100644 index 42e9af00..00000000 --- a/tests/getcache_mem_corruption.py +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/python -import apt -import apt_pkg -import re - -import unittest - -class TestGetCache(unittest.TestCase): - - def setUp(self): - apt_pkg.InitConfig() - apt_pkg.InitSystem() - - def testWrongInvocation(self): - # wrongly invoke GetCache() rather than GetDepCache() - apt_cache = apt_pkg.GetCache() - self.assertRaises(ValueError, apt_pkg.GetCache, apt_cache) - - def testProperInvocation(self): - apt_cache = apt_pkg.GetCache(apt.progress.OpTextProgress()) - apt_depcache = apt_pkg.GetDepCache(apt_cache) - -if __name__ == "__main__": - unittest.main() diff --git a/tests/old/__init__.py b/tests/old/__init__.py new file mode 100644 index 00000000..afd0f074 --- /dev/null +++ b/tests/old/__init__.py @@ -0,0 +1,13 @@ +import os +import unittest + + + +if __name__ == '__main__': + os.chdir(os.path.dirname(__file__)) + print os.getcwd() + + for path in os.listdir('.'): + if path.endswith('.py'): + exec 'from %s import *' % path[:-3] + unittest.main() diff --git a/tests/apt-test.py b/tests/old/apt-test.py index fac2ff43..fac2ff43 100644 --- a/tests/apt-test.py +++ b/tests/old/apt-test.py diff --git a/tests/cache.py b/tests/old/cache.py index 87a544e8..f0bf6761 100644 --- a/tests/cache.py +++ b/tests/old/cache.py @@ -9,8 +9,8 @@ import sys def main(): apt_pkg.init() - cache = apt_pkg.GetCache() - depcache = apt_pkg.GetDepCache(cache) + cache = apt_pkg.Cache() + depcache = apt_pkg.DepCache(cache) depcache.Init() i=0 all=cache.PackageCount diff --git a/tests/depcache.py b/tests/old/depcache.py index 19aba680..0d59648e 100644 --- a/tests/depcache.py +++ b/tests/old/depcache.py @@ -9,8 +9,8 @@ import sys def main(): apt_pkg.init() - cache = apt_pkg.GetCache() - depcache = apt_pkg.GetDepCache(cache) + cache = apt_pkg.Cache() + depcache = apt_pkg.DepCache(cache) depcache.Init() i=0 all=cache.PackageCount diff --git a/tests/hashsum_test.data b/tests/old/hashsum_test.data index 19102815..19102815 100644 --- a/tests/hashsum_test.data +++ b/tests/old/hashsum_test.data diff --git a/tests/hashsum_test_with_zero.data b/tests/old/hashsum_test_with_zero.data Binary files differindex 2ec9a6df..2ec9a6df 100644 --- a/tests/hashsum_test_with_zero.data +++ b/tests/old/hashsum_test_with_zero.data diff --git a/tests/lock.py b/tests/old/lock.py index d45b3964..d45b3964 100644 --- a/tests/lock.py +++ b/tests/old/lock.py diff --git a/tests/memleak.py b/tests/old/memleak.py index 659091fc..5299f35f 100755 --- a/tests/memleak.py +++ b/tests/old/memleak.py @@ -29,10 +29,10 @@ for i in range(100): # no memleak, but more or less the apt.Cache.open() code for i in range(100): - cache = apt_pkg.GetCache() - depcache = apt_pkg.GetDepCache(cache) - records = apt_pkg.GetPkgRecords(cache) - list = apt_pkg.GetPkgSourceList() + cache = apt_pkg.Cache() + depcache = apt_pkg.DepCache(cache) + records = apt_pkg.PackageRecords(cache) + list = apt_pkg.SourceList() list.ReadMainList() dict = {} for pkg in cache.Packages: diff --git a/tests/pkgproblemresolver.py b/tests/old/pkgproblemresolver.py index a21d8d9d..0d6d0611 100644 --- a/tests/pkgproblemresolver.py +++ b/tests/old/pkgproblemresolver.py @@ -9,8 +9,8 @@ import sys def main(): apt_pkg.init() - cache = apt_pkg.GetCache() - depcache = apt_pkg.GetDepCache(cache) + cache = apt_pkg.Cache() + depcache = apt_pkg.DepCache(cache) depcache.Init() i=0 all=cache.PackageCount @@ -25,7 +25,7 @@ def main(): if ver is not None: depcache.MarkInstall(pkg) if depcache.BrokenCount > 0: - fixer = apt_pkg.GetPkgProblemResolver(depcache) + fixer = apt_pkg.ProblemResolver(depcache) fixer.Clear(pkg) fixer.Protect(pkg) # we first try to resolve the problem diff --git a/tests/pkgrecords.py b/tests/old/pkgrecords.py index 5866847d..2fe6ad20 100644 --- a/tests/pkgrecords.py +++ b/tests/old/pkgrecords.py @@ -10,14 +10,14 @@ import sys def main(): apt_pkg.init() - cache = apt_pkg.GetCache() - depcache = apt_pkg.GetDepCache(cache) + cache = apt_pkg.Cache() + depcache = apt_pkg.DepCache(cache) depcache.Init() i=0 print "Running PkgRecords test on all packages:" for pkg in cache.Packages: i += 1 - records = apt_pkg.GetPkgRecords(cache) + records = apt_pkg.PackageRecords(cache) if len(pkg.VersionList) == 0: #print "no available version, cruft" continue diff --git a/tests/pkgsrcrecords.py b/tests/old/pkgsrcrecords.py index 77670540..2ea9dd3a 100644 --- a/tests/pkgsrcrecords.py +++ b/tests/old/pkgsrcrecords.py @@ -10,12 +10,12 @@ import sys def main(): apt_pkg.init() - cache = apt_pkg.GetCache() + cache = apt_pkg.Cache() i=0 print "Running PkgSrcRecords test on all packages:" for x in cache.Packages: i += 1 - src = apt_pkg.GetPkgSrcRecords() + src = apt_pkg.SourceRecords() if src.Lookup(x.Name): #print src.Package pass diff --git a/tests/refcount.py b/tests/old/refcount.py index a29744ae..a29744ae 100755 --- a/tests/refcount.py +++ b/tests/old/refcount.py diff --git a/tests/test_debextract.py b/tests/old/test_debextract.py index 4ba498ae..4ba498ae 100755 --- a/tests/test_debextract.py +++ b/tests/old/test_debextract.py diff --git a/tests/test_enhances.py b/tests/old/test_enhances.py index 3eced9f9..3eced9f9 100644 --- a/tests/test_enhances.py +++ b/tests/old/test_enhances.py diff --git a/tests/test_extract_archive.py b/tests/old/test_extract_archive.py index ce202b06..ce202b06 100644 --- a/tests/test_extract_archive.py +++ b/tests/old/test_extract_archive.py diff --git a/tests/test_hashsums.py b/tests/old/test_hashsums.py index 0cf6beb7..0cf6beb7 100644 --- a/tests/test_hashsums.py +++ b/tests/old/test_hashsums.py diff --git a/tests/test_all.py b/tests/test_all.py new file mode 100644 index 00000000..d561a9ae --- /dev/null +++ b/tests/test_all.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +# Copyright (C) 2009 Julian Andres Klode <jak@debian.org> +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. +"""Run all available unit tests.""" +import os +import unittest +import sys + +if __name__ == '__main__': + sys.stderr.write("[tests] Running on %s\n" % sys.version.replace("\n", "")) + os.chdir(os.path.dirname(__file__)) + # Find the path to the built apt_pkg and apt_inst extensions + if os.path.exists("../build"): + from distutils.util import get_platform + from distutils.sysconfig import get_python_version + # Set the path to the build directory. + plat_specifier = ".%s-%s" % (get_platform(), get_python_version()) + if sys.version_info[0] >= 3 or sys.version_info[1] >= 6: + library_dir = "../build/lib%s%s" % (plat_specifier, + (sys.pydebug and "-pydebug" or "")) + else: + library_dir = "../build/lib%s%s" % ((sys.pydebug and "_d" or ""), + plat_specifier) + sys.path.insert(0, os.path.abspath(library_dir)) + + for path in os.listdir('.'): + if path.endswith('.py') and os.path.isfile(path): + exec('from %s import *' % path[:-3]) + + unittest.main() diff --git a/tests/test_aptsources.py b/tests/test_aptsources.py index 3761f3ff..767f5244 100644 --- a/tests/test_aptsources.py +++ b/tests/test_aptsources.py @@ -3,24 +3,28 @@ import unittest import os import copy -import sys -sys.path.insert(0, "../") import apt_pkg -import aptsources import aptsources.sourceslist import aptsources.distro class TestAptSources(unittest.TestCase): - def __init__(self, methodName): - unittest.TestCase.__init__(self, methodName) - apt_pkg.init() - apt_pkg.Config.Set("Dir::Etc", os.getcwd()) - apt_pkg.Config.Set("Dir::Etc::sourceparts", "/xxx") + def setUp(self): + apt_pkg.init_config() + apt_pkg.init_system() + if apt_pkg.config["APT::Architecture"] not in ('i386', 'amd64'): + apt_pkg.config.set("APT::Architecture", "i386") + apt_pkg.config.set("Dir::Etc", os.getcwd()) + apt_pkg.config.set("Dir::Etc::sourceparts", "/xxx") + if os.path.exists("../build/data/templates"): + self.templates = os.path.abspath("../build/data/templates") + else: + self.templates = "/usr/share/python-apt/templates/" def testIsMirror(self): + """aptsources: Test mirror detection.""" yes = aptsources.sourceslist.is_mirror("http://archive.ubuntu.com", "http://de.archive.ubuntu.com") no = aptsources.sourceslist.is_mirror("http://archive.ubuntu.com", @@ -29,17 +33,21 @@ class TestAptSources(unittest.TestCase): self.assertFalse(no) def testSourcesListReading(self): - apt_pkg.Config.Set("Dir::Etc::sourcelist", "data/sources.list") - sources = aptsources.sourceslist.SourcesList() + """aptsources: Test sources.list parsing.""" + apt_pkg.config.set("Dir::Etc::sourcelist", "data/aptsources/" + "sources.list") + sources = aptsources.sourceslist.SourcesList(True, self.templates) self.assertEqual(len(sources.list), 6) # test load sources.list = [] - sources.load("data/sources.list") + sources.load("data/aptsources/sources.list") self.assertEqual(len(sources.list), 6) def testSourcesListAdding(self): - apt_pkg.Config.Set("Dir::Etc::sourcelist", "data/sources.list") - sources = aptsources.sourceslist.SourcesList() + """aptsources: Test additions to sources.list""" + apt_pkg.config.set("Dir::Etc::sourcelist", "data/aptsources/" + "sources.list") + sources = aptsources.sourceslist.SourcesList(True, self.templates) # test to add something that is already there (main) before = copy.deepcopy(sources) sources.add("deb", "http://de.archive.ubuntu.com/ubuntu/", @@ -86,9 +94,10 @@ class TestAptSources(unittest.TestCase): self.assertEqual(found_universe, 1) def testMatcher(self): - apt_pkg.Config.Set("Dir::Etc::sourcelist", "data/sources.list.test" - "Distribution") - sources = aptsources.sourceslist.SourcesList() + """aptsources: Test matcher""" + apt_pkg.config.set("Dir::Etc::sourcelist", "data/aptsources/" + "sources.list.testDistribution") + sources = aptsources.sourceslist.SourcesList(True, self.templates) distro = aptsources.distro.get_distro() distro.get_sources(sources) # test if all suits of the current distro were detected correctly @@ -98,9 +107,10 @@ class TestAptSources(unittest.TestCase): self.fail("source entry '%s' has no matcher" % s) def testDistribution(self): - apt_pkg.Config.Set("Dir::Etc::sourcelist", "data/sources.list.test" - "Distribution") - sources = aptsources.sourceslist.SourcesList() + """aptsources: Test distribution detection.""" + apt_pkg.config.set("Dir::Etc::sourcelist", "data/aptsources/" + "sources.list.testDistribution") + sources = aptsources.sourceslist.SourcesList(True, self.templates) distro = aptsources.distro.get_distro() distro.get_sources(sources) # test if all suits of the current distro were detected correctly @@ -148,4 +158,5 @@ class TestAptSources(unittest.TestCase): if __name__ == "__main__": + os.chdir(os.path.dirname(__file__)) unittest.main() diff --git a/tests/test_aptsources_ports.py b/tests/test_aptsources_ports.py index 09d6e9d9..991b532a 100644 --- a/tests/test_aptsources_ports.py +++ b/tests/test_aptsources_ports.py @@ -1,30 +1,32 @@ #!/usr/bin/env python - +import os import unittest -import os -import copy -import sys -sys.path.insert(0, "../") import apt_pkg -import aptsources import aptsources.sourceslist import aptsources.distro -class TestAptSources(unittest.TestCase): +class TestAptSourcesPorts(unittest.TestCase): + """Test aptsources on ports.ubuntu.com.""" - def __init__(self, methodName): - unittest.TestCase.__init__(self, methodName) - apt_pkg.init() - apt_pkg.Config.Set("APT::Architecture", "powerpc") - apt_pkg.Config.Set("Dir::Etc", os.path.abspath("test-data-ports")) - apt_pkg.Config.Set("Dir::Etc::sourceparts", "/xxx") + def setUp(self): + apt_pkg.init_config() + apt_pkg.init_system() + apt_pkg.config.set("APT::Architecture", "powerpc") + apt_pkg.config.set("Dir::Etc", + os.path.abspath("data/aptsources_ports")) + apt_pkg.config.set("Dir::Etc::sourceparts", "/xxx") + if os.path.exists("../build/data/templates"): + self.templates = os.path.abspath("../build/data/templates") + else: + self.templates = "/usr/share/python-apt/templates/" def testMatcher(self): - apt_pkg.Config.Set("Dir::Etc::sourcelist", "sources.list") - sources = aptsources.sourceslist.SourcesList() + """aptsources_ports: Test matcher.""" + apt_pkg.config.set("Dir::Etc::sourcelist", "sources.list") + sources = aptsources.sourceslist.SourcesList(True, self.templates) distro = aptsources.distro.get_distro("Ubuntu", "hardy", "desc", "8.04") distro.get_sources(sources) diff --git a/tests/test_cache_invocation.py b/tests/test_cache_invocation.py new file mode 100644 index 00000000..6f4014de --- /dev/null +++ b/tests/test_cache_invocation.py @@ -0,0 +1,30 @@ +#!/usr/bin/python +import unittest + +import apt_pkg +import apt.progress.base + + +class TestCache(unittest.TestCase): + """Test invocation of apt_pkg.Cache()""" + + def setUp(self): + apt_pkg.init_config() + apt_pkg.init_system() + + def test_wrong_invocation(self): + """cache_invocation: Test wrong invocation.""" + apt_cache = apt_pkg.Cache(apt.progress.base.OpProgress()) + + self.assertRaises(ValueError, apt_pkg.Cache, apt_cache) + self.assertRaises(ValueError, apt_pkg.Cache, + apt.progress.base.AcquireProgress()) + self.assertRaises(ValueError, apt_pkg.Cache, 0) + + def test_proper_invocation(self): + """cache_invocation: Test correct invocation.""" + apt_cache = apt_pkg.Cache(apt.progress.base.OpProgress()) + apt_depcache = apt_pkg.DepCache(apt_cache) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_deps.py b/tests/test_deps.py new file mode 100644 index 00000000..674c9485 --- /dev/null +++ b/tests/test_deps.py @@ -0,0 +1,119 @@ +#!/usr/bin/python +# +# Copyright (C) 2010 Julian Andres Klode <jak@debian.org> +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. +"""Unit tests for verifying the correctness of check_dep, etc in apt_pkg.""" +import unittest + +import apt_pkg + + +class TestDependencies(unittest.TestCase): + + def setUp(self): + apt_pkg.init() + + def testCheckDep(self): + """dependencies: Test apt_pkg.CheckDep() for '<' and '>' + + The CheckDep function should treat '<' as '<=' and '>' as '>=', for + compatibility reasons.""" + if not hasattr(apt_pkg, 'CheckDep'): + return + self.assertFalse(apt_pkg.CheckDep("1", "<", "0")) + self.assertTrue(apt_pkg.CheckDep("1", "<", "1")) + self.assertTrue(apt_pkg.CheckDep("1", "<", "2")) + + self.assertFalse(apt_pkg.CheckDep("0", ">", "1")) + self.assertTrue(apt_pkg.CheckDep("1", ">", "1")) + self.assertTrue(apt_pkg.CheckDep("2", ">", "1")) + + def test_check_dep(self): + "dependencies: Test apt_pkg.check_dep()" + self.assertFalse(apt_pkg.check_dep("1", "<<", "0")) + self.assertFalse(apt_pkg.check_dep("1", "<<", "1")) + self.assertTrue(apt_pkg.check_dep("1", "<<", "2")) + + self.assertFalse(apt_pkg.check_dep("1", "<", "0")) + self.assertFalse(apt_pkg.check_dep("1", "<", "1")) + self.assertTrue(apt_pkg.check_dep("1", "<", "2")) + + self.assertFalse(apt_pkg.check_dep("1", "<=", "0")) + self.assertTrue(apt_pkg.check_dep("1", "<=", "1")) + self.assertTrue(apt_pkg.check_dep("1", "<=", "2")) + + self.assertFalse(apt_pkg.check_dep("0", "=", "1")) + self.assertTrue(apt_pkg.check_dep("1", "=", "1")) + self.assertFalse(apt_pkg.check_dep("2", "=", "1")) + + self.assertFalse(apt_pkg.check_dep("0", ">=", "1")) + self.assertTrue(apt_pkg.check_dep("1", ">=", "1")) + self.assertTrue(apt_pkg.check_dep("2", ">=", "1")) + + self.assertFalse(apt_pkg.check_dep("0", ">", "1")) + self.assertFalse(apt_pkg.check_dep("1", ">", "1")) + self.assertTrue(apt_pkg.check_dep("2", ">", "1")) + + self.assertFalse(apt_pkg.check_dep("0", ">>", "1")) + self.assertFalse(apt_pkg.check_dep("1", ">>", "1")) + self.assertTrue(apt_pkg.check_dep("2", ">>", "1")) + + def test_parse_depends(self): + """dependencies: Test apt_pkg.parse_depends()""" + deps = apt_pkg.parse_depends("p1a (<< 1a) | p1b (>> 1b)") + self.assertTrue(isinstance(deps, list)) + self.assertEqual(len(deps), 1) + self.assertTrue(isinstance(deps[0], list)) + self.assertEqual(len(deps[0]), 2) + self.assertEqual(len(deps[0][0]), 3) + self.assertEqual(len(deps[0][1]), 3) + self.assertEqual(deps[0][0][0], "p1a") + self.assertEqual(deps[0][0][1], "1a") + self.assertEqual(deps[0][0][2], "<") + self.assertEqual(deps[0][1][0], "p1b") + self.assertEqual(deps[0][1][1], "1b") + self.assertEqual(deps[0][1][2], ">") + + # Check that the type of comparison is parsed correctly. + self.assertEqual("<", apt_pkg.parse_depends("p1 (<< 1)")[0][0][2]) + self.assertEqual("<=", apt_pkg.parse_depends("p1 (< 1)")[0][0][2]) + self.assertEqual("<=", apt_pkg.parse_depends("p1 (<= 1)")[0][0][2]) + self.assertEqual("=", apt_pkg.parse_depends("p1 (= 1)")[0][0][2]) + self.assertEqual(">=", apt_pkg.parse_depends("p1 (>= 1)")[0][0][2]) + self.assertEqual(">=", apt_pkg.parse_depends("p1 (> 1)")[0][0][2]) + self.assertEqual(">", apt_pkg.parse_depends("p1 (>> 1)")[0][0][2]) + + def test_parse_src_depends(self): + """dependencies: Test apt_pkg.parse_src_depends().""" + # Check that architecture exclusion works + # depends_this: Current architecture is included + # depends_this_too: Another architecture is excluded + # depends_other: The current architecture is excluded + # depends_other: Another architecture is requested. + architecture = apt_pkg.config["APT::Architecture"] + depends_this = apt_pkg.parse_src_depends("p [%s]" % architecture) + depends_this_too = apt_pkg.parse_src_depends("p [!not-existing-arch]") + depends_other = apt_pkg.parse_src_depends("p [!%s]" % architecture) + depends_other_too = apt_pkg.parse_src_depends("p [not-existing-arch]") + + self.assertEqual(len(depends_this), len(depends_this_too), 1) + self.assertEqual(len(depends_other), len(depends_other_too), 0) + + def testParseDepends(self): + """dependencies: Test apt_pkg.ParseDepends().""" + if not hasattr(apt_pkg, 'ParseDepends'): + return + # Check that the type of comparison is parsed correctly. + self.assertEqual("<<", apt_pkg.ParseDepends("p1 (<< 1)")[0][0][2]) + self.assertEqual("<=", apt_pkg.ParseDepends("p1 (< 1)")[0][0][2]) + self.assertEqual("<=", apt_pkg.ParseDepends("p1 (<= 1)")[0][0][2]) + self.assertEqual("=", apt_pkg.ParseDepends("p1 (= 1)")[0][0][2]) + self.assertEqual(">=", apt_pkg.ParseDepends("p1 (>= 1)")[0][0][2]) + self.assertEqual(">=", apt_pkg.ParseDepends("p1 (> 1)")[0][0][2]) + self.assertEqual(">>", apt_pkg.ParseDepends("p1 (>> 1)")[0][0][2]) + +if __name__ == "__main__": + unittest.main() diff --git a/tests/test_hashes.py b/tests/test_hashes.py new file mode 100644 index 00000000..e0aabe09 --- /dev/null +++ b/tests/test_hashes.py @@ -0,0 +1,113 @@ +#!/usr/bin/python +# +# Copyright (C) 2009 Julian Andres Klode <jak@debian.org> +# +# Copying and distribution of this file, with or without modification, +# are permitted in any medium without royalty provided the copyright +# notice and this notice are preserved. +"""Unit tests for verifying the correctness of hashsums in apt_pkg. + +Unit tests to verify the correctness of Hashes, HashString and the various +functions like md5sum.""" +import unittest +import hashlib +import sys + +import apt_pkg + + +class TestHashes(unittest.TestCase): + """Test apt_pkg.Hashes() and the various apt_pkg.*sum() functions.""" + + def setUp(self): + """Prepare the tests, create reference values...""" + self.file = open(apt_pkg.__file__, "rb") + self.value = self.file.read() + self.hashes = apt_pkg.Hashes(self.value) + self.file.seek(0) + self.fhashes = apt_pkg.Hashes(self.file) + # Reference values. + self.md5 = hashlib.md5(self.value).hexdigest() + self.sha1 = hashlib.sha1(self.value).hexdigest() + self.sha256 = hashlib.sha256(self.value).hexdigest() + self.file.seek(0) + + def tearDown(self): + """Cleanup, Close the file object used for the tests.""" + self.file.close() + + def test_md5sum(self): + """hashes: Test apt_pkg.md5sum()""" + self.assertEqual(apt_pkg.md5sum(self.value), self.md5) + self.assertEqual(apt_pkg.md5sum(self.file), self.md5) + + def test_sha1sum(self): + """hashes: Test apt_pkg.sha1sum()""" + self.assertEqual(apt_pkg.sha1sum(self.value), self.sha1) + self.assertEqual(apt_pkg.sha1sum(self.file), self.sha1) + + def test_sha256sum(self): + """hashes: Test apt_pkg.sha256sum()""" + self.assertEqual(apt_pkg.sha256sum(self.value), self.sha256) + self.assertEqual(apt_pkg.sha256sum(self.file), self.sha256) + + def test_bytes(self): + """hashes: Test apt_pkg.Hashes(bytes)""" + self.assertEqual(self.hashes.md5, self.md5) + self.assertEqual(self.hashes.sha1, self.sha1) + self.assertEqual(self.hashes.sha256, self.sha256) + + def test_file(self): + """hashes: Test apt_pkg.Hashes(file).""" + self.assertEqual(self.hashes.md5, self.fhashes.md5) + self.assertEqual(self.hashes.sha1, self.fhashes.sha1) + self.assertEqual(self.hashes.sha256, self.fhashes.sha256) + + def test_unicode(self): + """hashes: Test apt_pkg.Hashes(unicode).""" + if sys.version_info[0] == 3: + self.assertRaises(TypeError, apt_pkg.Hashes, "D") + self.assertRaises(TypeError, apt_pkg.md5sum, "D") + self.assertRaises(TypeError, apt_pkg.sha1sum, "D") + self.assertRaises(TypeError, apt_pkg.sha256sum, "D") + else: + self.assertRaises(TypeError, apt_pkg.Hashes, unicode()) + self.assertRaises(TypeError, apt_pkg.md5sum, unicode()) + self.assertRaises(TypeError, apt_pkg.sha1sum, unicode()) + self.assertRaises(TypeError, apt_pkg.sha256sum, unicode()) + + +class TestHashString(unittest.TestCase): + """Test apt_pkg.HashString().""" + + def setUp(self): + """Prepare the test by reading the file.""" + self.hashes = apt_pkg.Hashes(open(apt_pkg.__file__)) + self.md5 = apt_pkg.HashString("MD5Sum", self.hashes.md5) + self.sha1 = apt_pkg.HashString("SHA1", self.hashes.sha1) + self.sha256 = apt_pkg.HashString("SHA256", self.hashes.sha256) + + def test_md5(self): + """hashes: Test apt_pkg.HashString().md5""" + self.assertEqual("MD5Sum:%s" % self.hashes.md5, str(self.md5)) + self.assertTrue(self.md5.verify_file(apt_pkg.__file__)) + + def test_sha1(self): + """hashes: Test apt_pkg.HashString().sha1""" + self.assertEqual("SHA1:%s" % self.hashes.sha1, str(self.sha1)) + self.assertTrue(self.sha1.verify_file(apt_pkg.__file__)) + + def test_sha256(self): + """hashes: Test apt_pkg.HashString().sha256""" + self.assertEqual("SHA256:%s" % self.hashes.sha256, str(self.sha256)) + self.assertTrue(self.sha256.verify_file(apt_pkg.__file__)) + + def test_wrong(self): + """hashes: Test apt_pkg.HashString(wrong_type).""" + self.assertRaises(TypeError, apt_pkg.HashString, 0) + if sys.version_info[0] == 3: + self.assertRaises(TypeError, apt_pkg.HashString, bytes()) + + +if __name__ == "__main__": + unittest.main() |
