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 setUp(self): """Prepare the tests, create reference values...""" testcommon.TestCase.setUp(self) 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) warnings.filterwarnings("ignore", category=DeprecationWarning)
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 check(self, directory): """Validate hashes Check if size and hashes match the expected value. @type directory: str @param directory: directory the file is located in @raise InvalidHashException: hash mismatch """ path = os.path.join(directory, self.filename) try: with open(path) as fh: size = os.fstat(fh.fileno()).st_size hashes = apt_pkg.Hashes(fh) except IOError as e: if e.errno == errno.ENOENT: raise FileDoesNotExist(self.filename) raise if size != self.size: raise InvalidHashException(self.filename, 'size', self.size, size) if hashes.md5 != self.md5sum: raise InvalidHashException(self.filename, 'md5sum', self.md5sum, hashes.md5) if hashes.sha1 != self.sha1sum: raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, hashes.sha1) if hashes.sha256 != self.sha256sum: raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, hashes.sha256)
def from_file(cls, directory, filename, section=None, priority=None): """create with values for an existing file Create a C{HashedFile} object that refers to an already existing file. @type directory: str @param directory: directory the file is located in @type filename: str @param filename: filename @type section: str or C{None} @param section: optional section as given in .changes files @type priority: str or C{None} @param priority: optional priority as given in .changes files @rtype: L{HashedFile} @return: C{HashedFile} object for the given file """ path = os.path.join(directory, filename) with open(path, 'r') as fh: size = os.fstat(fh.fileno()).st_size hashes = apt_pkg.Hashes(fh) return cls(filename, size, hashes.md5, hashes.sha1, hashes.sha256, section, priority)
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) self.md5 = self.hashes.hashes.find("md5sum") self.sha1 = self.hashes.hashes.find("sha1") self.sha256 = self.hashes.hashes.find("sha256")
def check_fh(self, fh): size = os.fstat(fh.fileno()).st_size fh.seek(0) hashes = apt_pkg.Hashes(fh) if size != self.size: raise InvalidHashException(self.filename, 'size', self.size, size) if hashes.md5 != self.md5sum: raise InvalidHashException(self.filename, 'md5sum', self.md5sum, hashes.md5) if hashes.sha1 != self.sha1sum: raise InvalidHashException(self.filename, 'sha1sum', self.sha1sum, hashes.sha1) if hashes.sha256 != self.sha256sum: raise InvalidHashException(self.filename, 'sha256sum', self.sha256sum, hashes.sha256)
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_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__))
def hashes(self): return apt_pkg.Hashes(self.fh())
def __init__(self, *args, **kwargs): self._apt_hashes = apt_pkg.Hashes(*args, **kwargs)