summaryrefslogtreecommitdiff
path: root/tests/test_hashes.py
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2009-07-17 19:40:47 +0200
committerJulian Andres Klode <jak@debian.org>2009-07-17 19:40:47 +0200
commitcbd822c9dfe8cf18d828bd558290f15775b4d837 (patch)
treeb9ee1d702dd85a5dc2cbd9303e7f31ca6b8ffa52 /tests/test_hashes.py
parent9bd7c1dcf9942d6ce294aa5ac58d90d6c1aa9f51 (diff)
downloadpython-apt-cbd822c9dfe8cf18d828bd558290f15775b4d837.tar.gz
tests: First work on the new testsuite.
The module tests.test_all will run all available tests. Each test placed herein must be using unittest and provide at least 1 test case. It must be prefixed with test_.
Diffstat (limited to 'tests/test_hashes.py')
-rw-r--r--tests/test_hashes.py93
1 files changed, 93 insertions, 0 deletions
diff --git a/tests/test_hashes.py b/tests/test_hashes.py
new file mode 100644
index 00000000..24e25f9c
--- /dev/null
+++ b/tests/test_hashes.py
@@ -0,0 +1,93 @@
+#!/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 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):
+ """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):
+ """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):
+ """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):
+ """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):
+ """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)
+
+
+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):
+ """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):
+ """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):
+ """Test apt_pkg.HashString().sha256"""
+ self.assertEqual("SHA256:%s" % self.hashes.sha256, str(self.sha256))
+ self.assertTrue(self.sha256.verify_file(apt_pkg.__file__))
+
+
+if __name__ == "__main__":
+ unittest.main()