def setUp(self): """Prepare the test by reading the file.""" self.file = open(apt_pkg.__file__) self.hashes = apt_pkg.Hashes(self.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_file(self): """hashes: Test apt_pkg.Hashes(file).""" self.assertEqual( self.fhashes.hashes.find("md5sum").hashvalue, self.md5) self.assertEqual(self.fhashes.hashes.find("md5sum"), apt_pkg.HashString("MD5Sum", self.md5)) self.assertEqual(self.fhashes.hashes.find("sha1"), apt_pkg.HashString("SHA1", self.sha1)) self.assertEqual(self.fhashes.hashes.find("sha256"), apt_pkg.HashString("SHA256", self.sha256))
def test_bytes(self): """hashes: Test apt_pkg.Hashes(bytes)""" self.assertEqual(self.hashes.hashes.find("md5sum").hashvalue, self.md5) self.assertEqual(self.hashes.hashes.find("md5sum"), apt_pkg.HashString("MD5Sum", self.md5)) self.assertEqual(self.hashes.hashes.find("sha1"), apt_pkg.HashString("SHA1", self.sha1)) self.assertEqual(self.hashes.hashes.find("sha256"), apt_pkg.HashString("SHA256", self.sha256)) self.assertRaises(KeyError, self.hashes.hashes.find, "md5")
def test_find(self): """Testing whether append works correctly.""" hs1 = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed") hs2 = apt_pkg.HashString("SHA1", "ef113338e654b1ada807a939ad47b3a67633391b") hsl = apt_pkg.HashStringList() hsl.append(hs1) hsl.append(hs2) self.assertEqual(hsl.find("MD5Sum").hashtype, "MD5Sum") self.assertEqual(hsl.find("SHA1").hashtype, "SHA1") self.assertEqual(hsl.find().hashtype, "SHA1")
def setUp(self): """Prepare the test by reading the file.""" testcommon.TestCase.setUp(self) self.file = open(apt_pkg.__file__) self.hashes = apt_pkg.Hashes(self.file) with warnings.catch_warnings(record=True) as caught_warnings: warnings.simplefilter("always") 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) self.assertEqual(len(caught_warnings), 3) self.assertTrue( issubclass(caught_warnings[0].category, DeprecationWarning)) self.assertTrue( issubclass(caught_warnings[1].category, DeprecationWarning)) self.assertTrue( issubclass(caught_warnings[2].category, DeprecationWarning))
def test_verify_file(self): with open(apt_pkg.__file__) as fobj: hashes = apt_pkg.Hashes(fobj) sha1 = apt_pkg.HashString("SHA1", hashes.sha1) sha256 = apt_pkg.HashString("SHA256", hashes.sha256) hsl = apt_pkg.HashStringList() hsl.append(sha1) hsl.append(sha256) self.assertTrue(hsl.verify_file(apt_pkg.__file__)) md5sum = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed") hsl.append(md5sum) self.assertFalse(hsl.verify_file(apt_pkg.__file__)) hsl2 = hashes.hashes self.assertIsInstance(hsl2, apt_pkg.HashStringList) self.assertGreater(len(hsl2), 0) self.assertTrue(hsl2.verify_file(apt_pkg.__file__))
def test_acquire_hashes(self): hs = apt_pkg.HashString("d41d8cd98f00b204e9800998ecf8427e") hsl = apt_pkg.HashStringList() hsl.append(hs) apt_pkg.AcquireFile(apt_pkg.Acquire(), "http://example.com", hash=hsl, destdir=self.file_unicode, destfile=self.file_unicode) apt_pkg.AcquireFile(apt_pkg.Acquire(), "http://example.com", hash=str(hs), destdir=self.file_unicode, destfile=self.file_unicode) self.assertRaises(TypeError, apt_pkg.AcquireFile, apt_pkg.Acquire(), "http://example.com", hash=hs, destdir=self.file_unicode, destfile=self.file_unicode)
def test_verify_file(self): with open(apt_pkg.__file__) as fobj: hashes = apt_pkg.Hashes(fobj) with warnings.catch_warnings(record=True): warnings.simplefilter("always") sha1 = hashes.hashes.find("sha1") sha256 = hashes.hashes.find("sha256") hsl = apt_pkg.HashStringList() hsl.append(sha1) hsl.append(sha256) self.assertTrue(hsl.verify_file(apt_pkg.__file__)) md5sum = apt_pkg.HashString("MD5Sum", "a60599e6200b60050d7a30721e3532ed") hsl.append(md5sum) self.assertFalse(hsl.verify_file(apt_pkg.__file__)) hsl2 = hashes.hashes self.assertIsInstance(hsl2, apt_pkg.HashStringList) self.assertGreater(len(hsl2), 0) self.assertTrue(hsl2.verify_file(apt_pkg.__file__))