Add unit tests for GnuPG command-line generator

Extract a function to calculate GPG commands to be executed and cover it with
unit tests.
This commit is contained in:
Piotr F. Mieszkowski 2022-01-09 21:00:51 +01:00
parent fc2779ef7d
commit 2cf60dec40
3 changed files with 40 additions and 9 deletions

View File

@ -31,8 +31,12 @@ LINE_USER_ID = 'uid'
POS_FINGERPRINT = 9 POS_FINGERPRINT = 9
def build_command(key_home, *args, **kwargs):
cmd = ["gpg", '--homedir', key_home] + list(args)
return cmd
def private_keys( keyhome ): 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 = subprocess.Popen( cmd, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
p.wait() p.wait()
keys = dict() keys = dict()
@ -46,7 +50,7 @@ def private_keys( keyhome ):
return keys return keys
def public_keys( keyhome ): 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 = subprocess.Popen( cmd, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
p.wait() p.wait()
@ -78,7 +82,7 @@ def confirm_key( content, email ):
os.mkdir(tmpkeyhome) os.mkdir(tmpkeyhome)
localized_env = os.environ.copy() localized_env = os.environ.copy()
localized_env["LANG"] = "C" 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] result = p.communicate(input=content)[1]
confirmed = False confirmed = False
@ -97,7 +101,7 @@ def confirm_key( content, email ):
# adds a key and ensures it has the given email address # adds a key and ensures it has the given email address
def add_key( keyhome, content ): 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.communicate(input=content)
p.wait() p.wait()
@ -107,7 +111,7 @@ def delete_key( keyhome, email ):
if result[1]: if result[1]:
# delete all keys matching this email address # 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() p.wait()
return True return True
@ -131,7 +135,7 @@ class GPGEncryptor:
return (encdata, p.returncode) return (encdata, p.returncode)
def _command(self): 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 # add recipients
for recipient in self._recipients: for recipient in self._recipients:
@ -159,6 +163,4 @@ class GPGDecryptor:
return (decdata, p.returncode) return (decdata, p.returncode)
def _command(self): def _command(self):
cmd = ["/usr/bin/gpg", "--trust-model", "always", "--homedir", self._keyhome, "--batch", "--yes", "--no-secmem-warning", "-a", "-d"] return build_command(self._keyhome, "--trust-model", "always", "--batch", "--yes", "--no-secmem-warning", "-a", "-d")
return cmd

View File

@ -2,9 +2,22 @@ PYTHON = python2.7
.PHONY: test pre-clean clean .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 test: test/tmp test/logs pre-clean
$(PYTHON) test/e2e_test.py $(PYTHON) test/e2e_test.py
#
# Run unit tests
#
unittest:
$(PYTHON) -m unittest discover -s test
pre-clean: pre-clean:
rm -fv test/gpg-mailgate.conf rm -fv test/gpg-mailgate.conf
rm -f test/logs/*.log rm -f test/logs/*.log

16
test/test_gnupg.py Normal file
View File

@ -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()