added the beginning of a testing suite to edward
authorAndrew Engelbrecht <sudoman@ninthfloor.org>
Mon, 20 Jul 2015 19:10:25 +0000 (15:10 -0400)
committerAndrew Engelbrecht <sudoman@ninthfloor.org>
Mon, 7 Dec 2015 18:16:28 +0000 (13:16 -0500)
automated testing is helpful.

.gitignore
edward
edward_config.py
tests/out1.txt [new file with mode: 0644]
tests/test1.eml [new file with mode: 0644]
tests/testgnupghome/pubring.gpg [new file with mode: 0644]
tests/testgnupghome/random_seed [new file with mode: 0644]
tests/testgnupghome/secring.gpg [new file with mode: 0644]
tests/testgnupghome/trustdb.gpg [new file with mode: 0644]

index 060a8e5bbd819084347c66bad9c8abca0a03a67b..5236e1e4621a7a79cad1c2b069faaa994d1bf73b 100644 (file)
@@ -1,3 +1,2 @@
 *~
-tests
 
diff --git a/edward b/edward
index 6b189b6df972e7c646787e692730813eb878e389..8fc26132f2c43a4f8d2b9d0693c5665284e15b52 100755 (executable)
--- a/edward
+++ b/edward
@@ -52,24 +52,41 @@ def main ():
 
     handle_args()
 
+    gpgme_ctx = get_gpg_context(edward_config.gnupghome,
+                              edward_config.sign_with_key)
+
     email_text = sys.stdin.read()
     email_from, email_subject = email_from_subject(email_text)
 
-    os.environ['GNUPGHOME'] = edward_config.gnupghome
-    gpgme_ctx = gpgme.Context()
-    gpgme_ctx.armor = True
-
     plaintext, keys = email_decode_flatten(email_text, gpgme_ctx, False)
     encrypt_to_key = choose_reply_encryption_key(keys)
 
     reply_message = generate_reply(plaintext, email_from, \
                                    email_subject, encrypt_to_key,
-                                   edward_config.sign_with_key,
                                    gpgme_ctx)
 
     print(reply_message)
 
 
+def get_gpg_context (gnupghome, sign_with_key_fp):
+
+    os.environ['GNUPGHOME'] = gnupghome
+
+    gpgme_ctx = gpgme.Context()
+    gpgme_ctx.armor = True
+
+    try:
+        sign_with_key = gpgme_ctx.get_key(sign_with_key_fp)
+    except:
+        error("unable to load signing key. is the gnupghome "
+                + "and signing key properly set in the edward_config.py?")
+        exit(1)
+
+    gpgme_ctx.signers = [sign_with_key]
+
+    return gpgme_ctx
+
+
 def email_decode_flatten (email_text, gpgme_ctx, from_decryption):
 
     body = ""
@@ -250,7 +267,7 @@ def choose_reply_encryption_key (keys):
 
 
 def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
-                    sign_with_fingerprint, gpgme_ctx):
+                    gpgme_ctx):
 
 
     reply  = "To: " + email_from + "\n"
@@ -272,7 +289,6 @@ def generate_reply (plaintext, email_from, email_subject, encrypt_to_key,
 
         encrypted_text = encrypt_sign_message(plaintext_mime.as_string(),
                                               encrypt_to_key,
-                                              sign_with_fingerprint,
                                               gpgme_ctx)
 
         control_mime = MIMEApplication("Version: 1",
@@ -310,10 +326,7 @@ def email_quote_text (text):
     return quoted_message
 
 
-def encrypt_sign_message (plaintext, encrypt_to_key, sign_with_fingerprint, gpgme_ctx):
-
-    sign_with_key = gpgme_ctx.get_key(sign_with_fingerprint)
-    gpgme_ctx.signers = [sign_with_key]
+def encrypt_sign_message (plaintext, encrypt_to_key, gpgme_ctx):
 
     plaintext_bytes = io.BytesIO(plaintext.encode('ascii'))
     encrypted_bytes = io.BytesIO()
@@ -325,10 +338,15 @@ def encrypt_sign_message (plaintext, encrypt_to_key, sign_with_fingerprint, gpgm
     return encrypted_txt
 
 
+def error (error_msg):
+
+    sys.stderr.write(progname + ": " + error_msg + "\n")
+
+
 def debug (debug_msg):
 
     if edward_config.debug == True:
-        sys.stderr.write(progname + ": " + debug_msg + "\n")
+        error(debug_msg)
 
 
 def handle_args ():
index 5523f3d68f392ef9a58a1e1c1f02a4c5016fe9c6..a1c1b47927643d485b303f0d389a3abf50137755 100644 (file)
@@ -25,8 +25,8 @@ Code sourced from these projects:
 
 """
 
-gnupghome = "/home/e/edward/.gnupg/"
-sign_with_key = "F357AA1A5B1FA42CFD9FE52A9FF2194CC09A61E8"
+gnupghome = "./tests/testgnupghome/"
+sign_with_key = "2470F96DB3CB20D2A39F89BA88611B2A4205FDDF"
 
 debug = False
 
diff --git a/tests/out1.txt b/tests/out1.txt
new file mode 100644 (file)
index 0000000..265c9d2
--- /dev/null
@@ -0,0 +1,23 @@
+MIME-Version: 1.0
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: quoted-printable
+
+thanks for the message!
+
+
+> thanks for the message!
+>=20
+> > =C3=9Cber Spa=C3=9F.
+> >=20
+> > qual =C3=A8 il suono di una mano sola?
+> >=20
+> > =E3=81=93=E3=82=93=E3=81=AB=E3=81=A1=E3=81=AF=E3=80=81=E3=81=8A=E5=85=
+=83=E6=B0=97=E3=81=A7=E3=81=99=E3=81=8B=EF=BC=9F
+> >=20
+>=20
+> test.
+>=20
+> -andrew
+>=20
+>=20
+>=20
diff --git a/tests/test1.eml b/tests/test1.eml
new file mode 100644 (file)
index 0000000..7dc9339
--- /dev/null
@@ -0,0 +1,43 @@
+From: No One <noone@localhost>
+MIME-Version: 1.0
+To: No One <noone@localhost>
+Subject: =?UTF-8?B?44GT44KT44Gr44Gh44Gv44CB44GK5YWD5rCX44Gn44GZ44GL77yf?=
+Content-Type: multipart/encrypted;
+ protocol="application/pgp-encrypted";
+ boundary="x2iAciceo0NrKsl4uFVFGTgJ6k0ocg8FW"
+
+This is an OpenPGP/MIME encrypted message (RFC 4880 and 3156)
+--x2iAciceo0NrKsl4uFVFGTgJ6k0ocg8FW
+Content-Type: application/pgp-encrypted
+Content-Description: PGP/MIME version identification
+
+Version: 1
+
+--x2iAciceo0NrKsl4uFVFGTgJ6k0ocg8FW
+Content-Type: application/octet-stream; name="encrypted.asc"
+Content-Description: OpenPGP encrypted message
+Content-Disposition: inline; filename="encrypted.asc"
+
+-----BEGIN PGP MESSAGE-----
+Version: GnuPG v1
+
+hIwDWouAYlp2iv8BA/sEjt5WzTB5XjR1ya3gb9q2bAhyn/KqxTX12K6/ZgiyVwO4
+1MW8MaVx1l9+RTeWj1mWYo7snTGsKYvUjnrFaL25LghxTcqiiIgrQbXS+94nbmmJ
+l5XUOND4Z9sLutWZ12edAZCPXONuvsRpjKbJ0COo2xT8dRG/nYPch5l+iQbW9tLp
+AYA+g74i8gOOvGubE9EAtcTCODfH/ZOeDfwBZx0DzyhJ8/WkDcgSTUX5O64o53z9
+ps4jcyw2usX74iNNiBvFsYLlFe3ouHnPgXidLCmuHGKm+NYSe2IHFaBNuMn9sjzm
+nkzuN5DxC5bnm4VsAY4hqh3JBlU9KhYse8z2JANNqe3ykF350mC5HZ7vldtiYzyI
+gAOJjkisIclt4W7S+zNXlAe+wtDjRadWAjLB1NO41ByBSqOTXo2HnLIE/wjZYAcO
++6AcCykXcnpgPbcpWxtmStVrUdCOhHJHyHxZaOtmym0+5ZxwEx3ZTNZI01LNUJ72
+6WGgEA7VnwYZpIONWpHiFE5R4nn/C/O465R60EM9V8rS7JyVNP/YBRVVVl1EyjDZ
+scOoTiV3dX1+42Mhhn7CrwrgADSID00gPrYWRiVjJZFfDIJoKShk4LxhoKCwiNr3
+lTFGV37BBQvAi5Ff38IhDF9maHlq1zNWYicQ1gt4IiWPToXCwEkAPvuBJngBoD7v
+y+BdGLSHDBM0aaM9XrUdIjp2JGDMkDu0omXluXDsy5arhdOPDz7DlKcRiNvdYc1P
+oR+Efnj6REgGXX7H/xwmD9+ZK2PBFMUMOOMV39o+SyP+10CV4OCD6B9vzYw0tCTK
+zzB1haHF1oHT2ywgtgM6eFeuc1XBbuEGTOToEC8VDuAug38k0ucFWdQHg++qzYnb
+K5sKzzl40UDdLDIU1bD3VIkqAufAr886CAvKDzGseA==
+=FfqK
+-----END PGP MESSAGE-----
+
+--x2iAciceo0NrKsl4uFVFGTgJ6k0ocg8FW--
+
diff --git a/tests/testgnupghome/pubring.gpg b/tests/testgnupghome/pubring.gpg
new file mode 100644 (file)
index 0000000..f455159
Binary files /dev/null and b/tests/testgnupghome/pubring.gpg differ
diff --git a/tests/testgnupghome/random_seed b/tests/testgnupghome/random_seed
new file mode 100644 (file)
index 0000000..3921a82
Binary files /dev/null and b/tests/testgnupghome/random_seed differ
diff --git a/tests/testgnupghome/secring.gpg b/tests/testgnupghome/secring.gpg
new file mode 100644 (file)
index 0000000..49e02da
Binary files /dev/null and b/tests/testgnupghome/secring.gpg differ
diff --git a/tests/testgnupghome/trustdb.gpg b/tests/testgnupghome/trustdb.gpg
new file mode 100644 (file)
index 0000000..3513713
Binary files /dev/null and b/tests/testgnupghome/trustdb.gpg differ