diff options
-rw-r--r-- | gemato/verify.py | 26 | ||||
-rw-r--r-- | tests/test_verify.py | 30 |
2 files changed, 56 insertions, 0 deletions
diff --git a/gemato/verify.py b/gemato/verify.py index 888af57..bde971e 100644 --- a/gemato/verify.py +++ b/gemato/verify.py @@ -121,3 +121,29 @@ def verify_path(path, e): return (False, diff) return (True, []) + + +class ManifestMismatch(Exception): + """ + An exception raised for verification failure. + """ + + def __init__(self, path, entry, diff): + msg = "Manifest mismatch for {}".format(path) + for k, exp, got in diff: + msg += "\n {}: expected: {}, have: {}".format(k, exp, got) + super(ManifestMismatch, self).__init__(msg) + self.path = path + self.entry = entry + self.diff = diff + + +def assert_path_verifies(path, e): + """ + Verify the path @path against entry @e. Raises an exception if it + does not pass the verification. + """ + + ret, diff = verify_path(path, e) + if not ret: + raise ManifestMismatch(path, e, diff) diff --git a/tests/test_verify.py b/tests/test_verify.py index 432e0ac..e49e98a 100644 --- a/tests/test_verify.py +++ b/tests/test_verify.py @@ -369,3 +369,33 @@ class ProcFileVerificationTest(unittest.TestCase): ('OPTIONAL', os.path.basename(self.path))) self.assertEqual(gemato.verify.verify_path(self.path, e), (False, [('__exists__', False, True)])) + + +class ExceptionVerificationTest(object): + def setUp(self): + TEST_STRING = b'The quick brown fox jumps over the lazy dog' + self.f = tempfile.NamedTemporaryFile() + self.f.write(TEST_STRING) + self.f.flush() + self.path = self.f.name + + def tearDown(self): + self.f.close() + + def testDATA(self): + e = gemato.manifest.ManifestEntryDATA.from_list( + ('DATA', os.path.basename(self.path), '43')) + gemato.verify.assert_path_verifies(self.path, e) + + def testChecksumDATA(self): + e = gemato.manifest.ManifestEntryDATA.from_list( + ('DATA', os.path.basename(self.path), '43', + 'MD5', '9e107d9d372bb6826bd81d3542a419d6', + 'SHA1', '2fd4e1c67a2d28fced849ee1bb76e7391b93eb12')) + gemato.verify.assert_path_verifies(self.path, e) + + def testWrongSizeDATA(self): + e = gemato.manifest.ManifestEntryDATA.from_list( + ('DATA', os.path.basename(self.path), '0')) + self.assertRaises(gemato.verify.ManifestMismatch, + gemato.verify.assert_path_verifies, self.path, e) |