Skip to content

Commit

Permalink
OpenPGP: added test key and real test environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernhardreiter committed Jun 1, 2016
1 parent ee9cadd commit 6fc9f35
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 19 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ An easy way to test the actual sending of emails, is to use Python's
`smtpd` module, running the `DebuggingServer`:

python3 -m smtpd -d -n -c DebuggingServer localhost:8025

Run Test-Suite
--------------
```
cd tests
python3 -m unittest
```
25 changes: 25 additions & 0 deletions tests/keys/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
First test key material created with

Package: gnupg2
Architecture: i386
Version: 2.0.25-99intevation2

libgcrypt 1.5.3

test1.intelmq@example.org
--------------------------

```
LANG=C GNUPGHOME=~/tmp/dot.gnupg gpg2 --full-gen-key
RSA (sign only)
Requested keysize is 4096 bits
Key does not expire at all
Real name: Test1 IntelMQ
Email address: test1.intelmq@example.org
Comment: no passphrase
```

```
LANG=C GNUPGHOME=~/tmp/dot.gnupg gpg2 --export-secret-key test1 >test1.gpg
LANG=C GNUPGHOME=~/tmp/dot.gnupg gpg2 --armor --export test1 >test1.pub
```
30 changes: 30 additions & 0 deletions tests/keys/test1.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v2

mQINBFdO3UsBEADLQUlMuWZbj64i54kO4/4I6JIjzd9ko4VTKYm/1BHeWA6+lxBk
sxzQ+9oYFJqqG5F22iJ1T/x0W5cgXqGz5ihyAXld6EfbDi6NbDVtbtYrYYZQcHCs
TAhk4RT9Eqsrn4rRDGailYw/j9exBFmg3Gb3QB17PEBFIwHH9UcMJYFuGVKsefMd
aKZbWWWLqMRLDoE1Y/JBF9q52aZjGtBwS0lqwct8zSbpBHCZWjaUIayAw40UOLIo
MVbyHponGkfJgJfbkEqy6POgV5tLDXp+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/m
MolwGaLqrXDEJTkCYf/I1C/Q3q0R68nighXl7hAbnJstWYc8Pik7uy2+xKfK5adS
GCR/TEvGT1BtVQVSdPlMf6bLRtnVp+361tOOR+Sw3HbTfdmxJ5qN7gsqhI4Va5LS
QZUfa8SQ3qtwqa2OhKQDsIbjpZGmcTn77gRB17PNR/VTecvQzOKPGcI9lYGbfl1a
IRFOLVjGJQ==
=/QOZ
-----END PGP PUBLIC KEY BLOCK-----
Binary file added tests/keys/test1.sec
Binary file not shown.
56 changes: 37 additions & 19 deletions tests/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,52 @@
First revision without test framework to be directly run.
Dependencies:
* pygpgme build for python3
* pygpgme build for python3 (tested with v0.3)
Authors:
* Bernhard E. Reiter <bernhard@intevation.de>
"""

import gpgme
import unittest
from io import BytesIO

def test_sign_nomime():
#TODO setup a separate GNUPGHOME
email_body = """Hello,
import gpgme
from util import GpgHomeTestCase

class SignTestCase(GpgHomeTestCase):
import_keys = ['test1.sec']

def test_sign_nomime(self):
email_body = """Hello,
this is my email body,
which shall be signed."""

ctx = gpgme.Context()
key = ctx.get_key('5F50 3EFA C8C8 9323 D54C 2525 91B8 CD7E 1592 5678')
ctx.signers = [key]

this is my email body,
which shall be signed."""
#plaintext = BytesIO(b"Hello World!")
plaintext = BytesIO(email_body.encode())
signature = BytesIO()

ctx = gpgme.Context()
sigs = ctx.sign(plaintext, signature, gpgme.SIG_MODE_CLEAR)
self.assertEqual(len(sigs), 1)

#TODO create or find test-key
key = ctx.get_key('2E17923D761D9154B2C1A1763C43F4C8EFF5D42A')
ctx.signers = [key]
sig = sigs[0]
self.assertEqual(sig.type, gpgme.SIG_MODE_CLEAR)
self.assertIsInstance(sig, gpgme.NewSignature)

#plaintext = BytesIO(b"Hello World!")
plaintext = BytesIO(email_body.encode())
signature = BytesIO()
sig = ctx.sign(plaintext, signature, gpgme.SIG_MODE_CLEAR)
## print out the unicode string of the signed email body
#signature.seek(0)
#print(signature.read().decode())

signature.seek(0)
print(signature.read().decode(), sig[0])
# let us verify the signature
signature.seek(0)
plaintext = BytesIO()
vsigs = ctx.verify(signature, None, plaintext)

if __name__ == "__main__":
test_sign_nomime()
plaintext.seek(0)
self.assertEqual(plaintext.read().decode(), email_body + '\n')
self.assertEqual(len(sigs), 1)
vsig = vsigs[0]
self.assertEqual(vsig.fpr, '5F503EFAC8C89323D54C252591B8CD7E15925678')
59 changes: 59 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Support test cases with pygpgme.
Initially imported from https://pypi.python.org/pypi/pygpgme/0.3
sha256sum pygpgme-0.3.tar.gz
5fd887c407015296a8fd3f4b867fe0fcca3179de97ccde90449853a3dfb802e1
# pygpgme - a Python wrapper for the gpgme library
# Copyright (C) 2006 James Henstridge
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
"""

import os
import shutil
import tempfile
import unittest

import gpgme

__all__ = ['GpgHomeTestCase']

keydir = os.path.join(os.path.dirname(__file__), 'keys')

class GpgHomeTestCase(unittest.TestCase):

gpg_conf_contents = ''
import_keys = []

def keyfile(self, key):
return open(os.path.join(keydir, key), 'rb')

def setUp(self):
self._gpghome = tempfile.mkdtemp(prefix='tmp.gpghome')
os.environ['GNUPGHOME'] = self._gpghome
fp = open(os.path.join(self._gpghome, 'gpg.conf'), 'wb')
fp.write(self.gpg_conf_contents.encode('UTF-8'))
fp.close()

# import requested keys into the keyring
ctx = gpgme.Context()
for key in self.import_keys:
with self.keyfile(key) as fp:
ctx.import_(fp)

def tearDown(self):
del os.environ['GNUPGHOME']
#shutil.rmtree(self._gpghome, ignore_errors=True)

0 comments on commit 6fc9f35

Please sign in to comment.