diff options
author | Daniel Kahn Gillmor <dkg@fifthhorseman.net> | 2016-11-30 02:30:44 -0500 |
---|---|---|
committer | Dylan Baker <dylan@pnwbakers.com> | 2017-08-14 09:30:34 -0700 |
commit | b0e2f322aa571a5e1999c069779f589e282a566c (patch) | |
tree | d5b905ff596a045ef4c8e5c9540772218b026230 /tests | |
parent | c377ee5bd6e2b64be8bbdd5df72ac3ca50373134 (diff) |
convert from pygpgme to the python "gpg" module
This converts from the now abandoned pygpgme project for wrapping gpgme,
to the upstream gpgme python bindings (which are descended from the pyme
project, before they became official).
Largely this change should not be user visible, but there are a couple
cases where the new bindings provide slightly more detailed error
messages, and alot directly presents those messages to users.
This patch has been significantly revised and updated by Dylan Baker,
but was originally authored by Daniel Kahn Gillmor.
Fixes #1069
Diffstat (limited to 'tests')
-rw-r--r-- | tests/crypto_test.py | 88 | ||||
-rw-r--r-- | tests/utilities.py | 6 |
2 files changed, 61 insertions, 33 deletions
diff --git a/tests/crypto_test.py b/tests/crypto_test.py index e51adaeb..1ee5404f 100644 --- a/tests/crypto_test.py +++ b/tests/crypto_test.py @@ -10,7 +10,7 @@ import subprocess import tempfile import unittest -import gpgme +import gpg import mock from alot import crypto @@ -43,15 +43,13 @@ def setUpModule(): mock_home.start() MOD_CLEAN.add_cleanup(mock_home.stop) - ctx = gpgme.Context() - ctx.armor = True - - # Add the public and private keys. They have no password - search_dir = os.path.join(os.path.dirname(__file__), 'static/gpg-keys') - for each in os.listdir(search_dir): - if os.path.splitext(each)[1] == '.gpg': - with open(os.path.join(search_dir, each)) as f: - ctx.import_(f) + with gpg.core.Context(armor=True) as ctx: + # Add the public and private keys. They have no password + search_dir = os.path.join(os.path.dirname(__file__), 'static/gpg-keys') + for each in os.listdir(search_dir): + if os.path.splitext(each)[1] == '.gpg': + with open(os.path.join(search_dir, each)) as f: + ctx.op_import(f) @MOD_CLEAN.wrap_teardown @@ -66,29 +64,54 @@ def tearDownModule(): os.kill(int(pid), signal.SIGKILL) +def make_key(revoked=False, expired=False, invalid=False, can_encrypt=True, + can_sign=True): + # This is ugly + mock_key = mock.create_autospec(gpg._gpgme._gpgme_key) + mock_key.uids = [mock.Mock(uid=u'mocked')] + mock_key.revoked = revoked + mock_key.expired = expired + mock_key.invalid = invalid + mock_key.can_encrypt = can_encrypt + mock_key.can_sign = can_sign + + return mock_key + + +def make_uid(email, revoked=False, invalid=False, + validity=gpg.constants.validity.FULL): + uid = mock.Mock() + uid.email = email + uid.revoked = revoked + uid.invalid = invalid + uid.validity = validity + + return uid + + class TestHashAlgorithmHelper(unittest.TestCase): """Test cases for the helper function RFC3156_canonicalize.""" def test_returned_string_starts_with_pgp(self): - result = crypto.RFC3156_micalg_from_algo(gpgme.MD_MD5) + result = crypto.RFC3156_micalg_from_algo(gpg.constants.md.MD5) self.assertTrue(result.startswith('pgp-')) def test_returned_string_is_lower_case(self): - result = crypto.RFC3156_micalg_from_algo(gpgme.MD_MD5) + result = crypto.RFC3156_micalg_from_algo(gpg.constants.md.MD5) self.assertTrue(result.islower()) def test_raises_for_unknown_hash_name(self): with self.assertRaises(GPGProblem): - crypto.RFC3156_micalg_from_algo(gpgme.MD_NONE) + crypto.RFC3156_micalg_from_algo(gpg.constants.md.NONE) class TestDetachedSignatureFor(unittest.TestCase): def test_valid_signature_generated(self): - ctx = gpgme.Context() to_sign = "this is some text.\nit is more than nothing.\n" - _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) + with gpg.core.Context() as ctx: + _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) with tempfile.NamedTemporaryFile(delete=False) as f: f.write(detached) @@ -108,9 +131,9 @@ class TestDetachedSignatureFor(unittest.TestCase): class TestVerifyDetached(unittest.TestCase): def test_verify_signature_good(self): - ctx = gpgme.Context() to_sign = "this is some text.\nIt's something\n." - _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) + with gpg.core.Context() as ctx: + _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) try: crypto.verify_detached(to_sign, detached) @@ -118,10 +141,10 @@ class TestVerifyDetached(unittest.TestCase): raise AssertionError def test_verify_signature_bad(self): - ctx = gpgme.Context() to_sign = "this is some text.\nIt's something\n." similar = "this is some text.\r\n.It's something\r\n." - _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) + with gpg.core.Context() as ctx: + _, detached = crypto.detached_signature_for(to_sign, ctx.get_key(FPR)) with self.assertRaises(GPGProblem): crypto.verify_detached(similar, detached) @@ -218,7 +241,7 @@ class TestCheckUIDValidity(unittest.TestCase): key = utilities.make_key() key.uids[0] = utilities.make_uid( mock.sentinel.EMAIL, - validity=gpgme.VALIDITY_UNDEFINED) + validity=gpg.constants.validity.UNDEFINED) ret = crypto.check_uid_validity(key, mock.sentinel.EMAIL) self.assertFalse(ret) @@ -251,16 +274,16 @@ class TestGetKey(unittest.TestCase): def test_plain(self): # Test the uid of the only identity attached to the key we generated. - ctx = gpgme.Context() - expected = ctx.get_key(FPR).uids[0].uid + with gpg.core.Context() as ctx: + expected = ctx.get_key(FPR).uids[0].uid actual = crypto.get_key(FPR).uids[0].uid self.assertEqual(expected, actual) def test_validate(self): # Since we already test validation we're only going to test validate # once. - ctx = gpgme.Context() - expected = ctx.get_key(FPR).uids[0].uid + with gpg.core.Context() as ctx: + expected = ctx.get_key(FPR).uids[0].uid actual = crypto.get_key(FPR, validate=True, encrypt=True, sign=True).uids[0].uid self.assertEqual(expected, actual) @@ -289,10 +312,15 @@ class TestGetKey(unittest.TestCase): @staticmethod def _context_mock(): - error = gpgme.GpgmeError() - error.code = gpgme.ERR_AMBIGUOUS_NAME + class CustomError(gpg.errors.GPGMEError): + """A custom GPGMEError class that always has an errors code of + AMBIGUOUS_NAME. + """ + def getcode(self): + return gpg.errors.AMBIGUOUS_NAME + context_mock = mock.Mock() - context_mock.get_key = mock.Mock(side_effect=error) + context_mock.get_key = mock.Mock(side_effect=CustomError) return context_mock @@ -300,7 +328,7 @@ class TestGetKey(unittest.TestCase): invalid_key = utilities.make_key(invalid=True) valid_key = utilities.make_key() - with mock.patch('alot.crypto.gpgme.Context', + with mock.patch('alot.crypto.gpg.core.Context', mock.Mock(return_value=self._context_mock())), \ mock.patch('alot.crypto.list_keys', mock.Mock(return_value=[valid_key, invalid_key])): @@ -308,7 +336,7 @@ class TestGetKey(unittest.TestCase): self.assertIs(key, valid_key) def test_ambiguous_two_valid(self): - with mock.patch('alot.crypto.gpgme.Context', + with mock.patch('alot.crypto.gpg.core.Context', mock.Mock(return_value=self._context_mock())), \ mock.patch('alot.crypto.list_keys', mock.Mock(return_value=[utilities.make_key(), @@ -318,7 +346,7 @@ class TestGetKey(unittest.TestCase): self.assertEqual(cm.exception.code, GPGCode.AMBIGUOUS_NAME) def test_ambiguous_no_valid(self): - with mock.patch('alot.crypto.gpgme.Context', + with mock.patch('alot.crypto.gpg.core.Context', mock.Mock(return_value=self._context_mock())), \ mock.patch('alot.crypto.list_keys', mock.Mock(return_value=[ diff --git a/tests/utilities.py b/tests/utilities.py index 45433c99..402feb38 100644 --- a/tests/utilities.py +++ b/tests/utilities.py @@ -21,7 +21,7 @@ from __future__ import absolute_import import functools import unittest -import gpgme +import gpg import mock @@ -149,7 +149,7 @@ class ModuleCleanup(object): def make_uid(email, uid=u'mocked', revoked=False, invalid=False, - validity=gpgme.VALIDITY_FULL): + validity=gpg.constants.validity.FULL): uid_ = mock.Mock() uid_.email = email uid_.uid = uid @@ -162,7 +162,7 @@ def make_uid(email, uid=u'mocked', revoked=False, invalid=False, def make_key(revoked=False, expired=False, invalid=False, can_encrypt=True, can_sign=True): - mock_key = mock.create_autospec(gpgme.Key) + mock_key = mock.Mock() mock_key.uids = [make_uid(u'foo@example.com')] mock_key.revoked = revoked mock_key.expired = expired |