diff options
author | Michał Górny <mgorny@gentoo.org> | 2018-02-01 22:14:44 +0100 |
---|---|---|
committer | Michał Górny <mgorny@gentoo.org> | 2018-02-01 22:14:44 +0100 |
commit | 0169372ad1782281dc7af802aa5806135947399b (patch) | |
tree | 9168b930fa452f53aa010b6e850304e7dc4710f9 | |
parent | 2c7c78cfc98444ecc30b5c14f9f3b688f243674e (diff) | |
download | gemato-0169372ad1782281dc7af802aa5806135947399b.tar.gz |
openpgp: Replace RuntimeError with dedicated for import/refresh errors
-rw-r--r-- | gemato/exceptions.py | 18 | ||||
-rw-r--r-- | gemato/openpgp.py | 6 | ||||
-rw-r--r-- | tests/test_openpgp.py | 33 |
3 files changed, 31 insertions, 26 deletions
diff --git a/gemato/exceptions.py b/gemato/exceptions.py index f565cd5..d544f62 100644 --- a/gemato/exceptions.py +++ b/gemato/exceptions.py @@ -111,6 +111,24 @@ class OpenPGPRuntimeError(GematoException): self.output = output +class OpenPGPKeyImportError(OpenPGPRuntimeError): + """ + An exception raised when key import fails. + """ + + def __str__(self): + return "OpenPGP key import failed:\n{}".format(self.output) + + +class OpenPGPKeyRefreshError(OpenPGPRuntimeError): + """ + An exception raised when keyring refresh (update) fails. + """ + + def __str__(self): + return "OpenPGP keyring refresh failed:\n{}".format(self.output) + + class OpenPGPVerificationFailure(OpenPGPRuntimeError): """ An exception raised when OpenPGP verification fails. diff --git a/gemato/openpgp.py b/gemato/openpgp.py index c97aeb2..c27eef4 100644 --- a/gemato/openpgp.py +++ b/gemato/openpgp.py @@ -217,12 +217,14 @@ disable-scdaemon def import_key(self, keyfile): exitst, out, err = self._spawn_gpg(['--import'], keyfile.read()) if exitst != 0: - raise RuntimeError('Unable to import key: {}'.format(err.decode('utf8'))) + raise gemato.exceptions.OpenPGPKeyImportError( + 'Unable to import key: {}'.format(err.decode('utf8'))) def refresh_keys(self): exitst, out, err = self._spawn_gpg(['--refresh-keys'], '') if exitst != 0: - raise RuntimeError('Unable to refresh keys: {}'.format(err.decode('utf8'))) + raise gemato.exceptions.OpenPGPKeyRefreshError( + 'Unable to refresh keys: {}'.format(err.decode('utf8'))) @property def home(self): diff --git a/tests/test_openpgp.py b/tests/test_openpgp.py index 9e7dd12..bbb6a8e 100644 --- a/tests/test_openpgp.py +++ b/tests/test_openpgp.py @@ -375,12 +375,9 @@ class OpenPGPCorrectKeyTest(unittest.TestCase): self.env = gemato.openpgp.OpenPGPEnvironment() try: self.env.import_key(io.BytesIO(PUBLIC_KEY)) - except gemato.exceptions.OpenPGPNoImplementation as e: + except gemato.exceptions.OpenPGPRuntimeError as e: self.env.close() raise unittest.SkipTest(str(e)) - except RuntimeError: - self.env.close() - raise unittest.SkipTest('Unable to import OpenPGP key') def tearDown(self): self.env.close() @@ -605,12 +602,9 @@ class OpenPGPExpiredKeyTest(OpenPGPNoKeyTest): self.env = gemato.openpgp.OpenPGPEnvironment() try: self.env.import_key(io.BytesIO(EXPIRED_PUBLIC_KEY)) - except gemato.exceptions.OpenPGPNoImplementation as e: + except gemato.exceptions.OpenPGPRuntimeError as e: self.env.close() raise unittest.SkipTest(str(e)) - except RuntimeError: - self.env.close() - raise unittest.SkipTest('Unable to import OpenPGP key') def tearDown(self): self.env.close() @@ -627,12 +621,9 @@ class OpenPGPRevokedKeyTest(OpenPGPNoKeyTest): self.env = gemato.openpgp.OpenPGPEnvironment() try: self.env.import_key(io.BytesIO(REVOKED_PUBLIC_KEY)) - except gemato.exceptions.OpenPGPNoImplementation as e: + except gemato.exceptions.OpenPGPRuntimeError as e: self.env.close() raise unittest.SkipTest(str(e)) - except RuntimeError: - self.env.close() - raise unittest.SkipTest('Unable to import OpenPGP key') def tearDown(self): self.env.close() @@ -649,12 +640,9 @@ class OpenPGPExpiredSignatureTest(unittest.TestCase): self.env = gemato.openpgp.OpenPGPEnvironment() try: self.env.import_key(io.BytesIO(PUBLIC_KEY)) - except gemato.exceptions.OpenPGPNoImplementation as e: + except gemato.exceptions.OpenPGPRuntimeError as e: self.env.close() raise unittest.SkipTest(str(e)) - except RuntimeError: - self.env.close() - raise unittest.SkipTest('Unable to import OpenPGP key') def tearDown(self): self.env.close() @@ -756,7 +744,7 @@ class OpenPGPContextManagerTest(unittest.TestCase): def test_import_malformed_key(self): with gemato.openpgp.OpenPGPEnvironment() as env: try: - self.assertRaises(RuntimeError, + self.assertRaises(gemato.exceptions.OpenPGPKeyImportError, env.import_key, io.BytesIO(MALFORMED_PUBLIC_KEY)) except gemato.exceptions.OpenPGPNoImplementation as e: @@ -765,7 +753,7 @@ class OpenPGPContextManagerTest(unittest.TestCase): def test_import_no_keys(self): with gemato.openpgp.OpenPGPEnvironment() as env: try: - self.assertRaises(RuntimeError, + self.assertRaises(gemato.exceptions.OpenPGPKeyImportError, env.import_key, io.BytesIO(b'')) except gemato.exceptions.OpenPGPNoImplementation as e: @@ -785,8 +773,8 @@ class OpenPGPContextManagerTest(unittest.TestCase): try: try: env.import_key(io.BytesIO(PUBLIC_KEY)) - except RuntimeError: - raise unittest.SkipTest('Unable to import OpenPGP key') + except gemato.exceptions.OpenPGPRuntimeError as e: + raise unittest.SkipTest(str(e)) sig = env.verify_file(f) self.assertEqual(sig.fingerprint, KEY_FINGERPRINT) @@ -818,12 +806,9 @@ class OpenPGPPrivateKeyTest(unittest.TestCase): self.env = gemato.openpgp.OpenPGPEnvironment() try: self.env.import_key(io.BytesIO(PRIVATE_KEY)) - except gemato.exceptions.OpenPGPNoImplementation as e: + except gemato.exceptions.OpenPGPRuntimeError as e: self.env.close() raise unittest.SkipTest(str(e)) - except RuntimeError: - self.env.close() - raise unittest.SkipTest('Unable to import OpenPGP key') def tearDown(self): self.env.close() |