summaryrefslogtreecommitdiff
path: root/tests/crypto_test.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/crypto_test.py')
-rw-r--r--tests/crypto_test.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/crypto_test.py b/tests/crypto_test.py
index 156d4ad6..51cb48ba 100644
--- a/tests/crypto_test.py
+++ b/tests/crypto_test.py
@@ -3,9 +3,16 @@
# For further details see the COPYING file
from __future__ import absolute_import
+import os
+import shutil
+import subprocess
+import tempfile
import unittest
import gpgme
+import mock
+
+from . import utilities
from alot import crypto
from alot.errors import GPGProblem
@@ -26,3 +33,65 @@ class TestHashAlgorithmHelper(unittest.TestCase):
def test_raises_for_unknown_hash_name(self):
with self.assertRaises(GPGProblem):
crypto.RFC3156_micalg_from_algo(gpgme.MD_NONE)
+
+
+class TestSignature(utilities.TestCaseClassCleanup):
+
+ FPR = "F74091D4133F87D56B5D343C1974EC55FBC2D660"
+
+ @classmethod
+ def setUpClass(cls):
+ # Create a temporary directory to use as gnupg's home directory. This
+ # allows us to import keys without dirtying the user's keyring
+ home = tempfile.mkdtemp()
+ cls.addClassCleanup(shutil.rmtree, home)
+ mock_home = mock.patch.dict(os.environ, {'GNUPGHOME': home})
+ mock_home.start()
+ cls.addClassCleanup(mock_home.stop)
+
+ # create a single context to use class wide.
+ cls.ctx = gpgme.Context()
+ cls.ctx.armor = True
+
+ # Add the public and private keys. They have no password
+ here = os.path.dirname(__file__)
+ with open(os.path.join(here, 'static/pub.gpg')) as f:
+ cls.ctx.import_(f)
+ with open(os.path.join(here, 'static/sec.gpg')) as f:
+ cls.ctx.import_(f)
+
+ cls.key = cls.ctx.get_key(cls.FPR)
+
+ def test_detached_signature_for(self):
+ to_sign = "this is some text.\nit is more than nothing.\n"
+ _, detached = crypto.detached_signature_for(to_sign, self.key)
+
+ with tempfile.NamedTemporaryFile(delete=False) as f:
+ f.write(detached)
+ sig = f.name
+ self.addCleanup(os.unlink, f.name)
+
+ with tempfile.NamedTemporaryFile(delete=False) as f:
+ f.write(to_sign)
+ text = f.name
+ self.addCleanup(os.unlink, f.name)
+
+ res = subprocess.check_call(['gpg', '--verify', sig, text])
+ self.assertEqual(res, 0)
+
+ def test_verify_signature_good(self):
+ to_sign = "this is some text.\nIt's something\n."
+ _, detached = crypto.detached_signature_for(to_sign, self.key)
+
+ try:
+ crypto.verify_detached(to_sign, detached)
+ except GPGProblem:
+ raise AssertionError
+
+ def test_verify_signature_bad(self):
+ 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, self.key)
+
+ with self.assertRaises(GPGProblem):
+ crypto.verify_detached(similar, detached)