From 2cf60dec40d00de7c8b7b08a8abe421cb7c8af7d Mon Sep 17 00:00:00 2001 From: "Piotr F. Mieszkowski" Date: Sun, 9 Jan 2022 21:00:51 +0100 Subject: [PATCH] Add unit tests for GnuPG command-line generator Extract a function to calculate GPG commands to be executed and cover it with unit tests. --- GnuPG/__init__.py | 20 +++++++++++--------- Makefile | 13 +++++++++++++ test/test_gnupg.py | 16 ++++++++++++++++ 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 test/test_gnupg.py diff --git a/GnuPG/__init__.py b/GnuPG/__init__.py index 294f8c9..540622e 100644 --- a/GnuPG/__init__.py +++ b/GnuPG/__init__.py @@ -31,8 +31,12 @@ LINE_USER_ID = 'uid' POS_FINGERPRINT = 9 +def build_command(key_home, *args, **kwargs): + cmd = ["gpg", '--homedir', key_home] + list(args) + return cmd + def private_keys( keyhome ): - cmd = ['/usr/bin/gpg', '--homedir', keyhome, '--list-secret-keys', '--with-colons'] + cmd = build_command(keyhome, '--list-secret-keys', '--with-colons') p = subprocess.Popen( cmd, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p.wait() keys = dict() @@ -46,7 +50,7 @@ def private_keys( keyhome ): return keys def public_keys( keyhome ): - cmd = ['/usr/bin/gpg', '--homedir', keyhome, '--list-keys', '--with-colons'] + cmd = build_command(keyhome, '--list-keys', '--with-colons') p = subprocess.Popen( cmd, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p.wait() @@ -78,7 +82,7 @@ def confirm_key( content, email ): os.mkdir(tmpkeyhome) localized_env = os.environ.copy() localized_env["LANG"] = "C" - p = subprocess.Popen( ['/usr/bin/gpg', '--homedir', tmpkeyhome, '--import', '--batch'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=localized_env ) + p = subprocess.Popen( build_command(tmpkeyhome, '--import', '--batch'), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=localized_env ) result = p.communicate(input=content)[1] confirmed = False @@ -97,7 +101,7 @@ def confirm_key( content, email ): # adds a key and ensures it has the given email address def add_key( keyhome, content ): - p = subprocess.Popen( ['/usr/bin/gpg', '--homedir', keyhome, '--import', '--batch'], stdin=subprocess.PIPE, stdout=subprocess.PIPE,stderr=subprocess.PIPE ) + p = subprocess.Popen( build_command(keyhome, '--import', '--batch'), stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p.communicate(input=content) p.wait() @@ -107,7 +111,7 @@ def delete_key( keyhome, email ): if result[1]: # delete all keys matching this email address - p = subprocess.Popen( ['/usr/bin/gpg', '--homedir', keyhome, '--delete-key', '--batch', '--yes', result[1]], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) + p = subprocess.Popen( build_command(keyhome, '--delete-key', '--batch', '--yes', result[1]), stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) p.wait() return True @@ -131,7 +135,7 @@ class GPGEncryptor: return (encdata, p.returncode) def _command(self): - cmd = ["/usr/bin/gpg", "--trust-model", "always", "--homedir", self._keyhome, "--batch", "--yes", "--pgp7", "--no-secmem-warning", "-a", "-e"] + cmd = build_command(self._keyhome, "--trust-model", "always", "--batch", "--yes", "--pgp7", "--no-secmem-warning", "-a", "-e") # add recipients for recipient in self._recipients: @@ -159,6 +163,4 @@ class GPGDecryptor: return (decdata, p.returncode) def _command(self): - cmd = ["/usr/bin/gpg", "--trust-model", "always", "--homedir", self._keyhome, "--batch", "--yes", "--no-secmem-warning", "-a", "-d"] - - return cmd + return build_command(self._keyhome, "--trust-model", "always", "--batch", "--yes", "--no-secmem-warning", "-a", "-d") diff --git a/Makefile b/Makefile index 629a04b..a14332a 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,22 @@ PYTHON = python2.7 .PHONY: test pre-clean clean +# +# Run a set of end-to-end tests. +# +# Test scenarios are described and configured by the test/e2e.ini +# file. Basically this is just a script that feeds GPG Mailgate with +# known input and checks whether output meets expectations. +# test: test/tmp test/logs pre-clean $(PYTHON) test/e2e_test.py +# +# Run unit tests +# +unittest: + $(PYTHON) -m unittest discover -s test + pre-clean: rm -fv test/gpg-mailgate.conf rm -f test/logs/*.log diff --git a/test/test_gnupg.py b/test/test_gnupg.py new file mode 100644 index 0000000..5712acd --- /dev/null +++ b/test/test_gnupg.py @@ -0,0 +1,16 @@ +import GnuPG + +import unittest + +class GnuPGUtilitiesTest(unittest.TestCase): + def test_build_default_command(self): + cmd = GnuPG.build_command("test/keyhome") + self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome"]) + + def test_build_command_extended_with_args(self): + cmd = GnuPG.build_command("test/keyhome", "--foo", "--bar") + self.assertEqual(cmd, ["gpg", "--homedir", "test/keyhome", "--foo", "--bar"]) + + +if __name__ == '__main__': + unittest.main()