commit b1a8aff620f1404b81e49ad5689cf77bbd063457 Author: Isis Lovecruft isis@torproject.org Date: Wed Apr 23 04:11:06 2014 +0000
Add GPGContextTests to test/test_crypto.py.
* ADD two new unittests: one for missing config settings pertaining to email signing, the other for when signing is disabled. --- lib/bridgedb/test/test_crypto.py | 80 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+)
diff --git a/lib/bridgedb/test/test_crypto.py b/lib/bridgedb/test/test_crypto.py index 935a784..e610d03 100644 --- a/lib/bridgedb/test/test_crypto.py +++ b/lib/bridgedb/test/test_crypto.py @@ -136,6 +136,86 @@ class LessCrypticGPGMEErrorTests(unittest.TestCase): self.assertSubstring(msg, lessCryptic.message)
+class GPGContextTests(unittest.TestCase): + """Tests for :func:`bridgedb.crypto.getGPGContext`.""" + + timeout = 15 + + @fileCheckDecorator + def doCopyFile(self, src, dst, description=None): + shutil.copy(src, dst) + + def removeRundir(self): + """Remove the rundir from the _trial_tmp directory.""" + if os.path.isdir(self.runDir): + shutil.rmtree(self.runDir) + + def makeBadKey(self): + """Make a bad keyfile and set its path in our config.""" + keyfile = os.path.join(self.runDir, 'badkey.asc') + with open(keyfile, 'w') as badkey: + badkey.write(str('NO PASARAN, DEATH CAKES!')) + badkey.flush() + self.setKey(keyfile) + + def enableSigning(self, enable=True): + """Enable or disable the config setting for email signing.""" + setattr(self.config, 'EMAIL_GPG_SIGNING_ENABLED', enable) + + def setKey(self, keyfile=''): + """Set the config keyfile path to **keyfile**.""" + setattr(self.config, 'EMAIL_GPG_SIGNING_KEY', keyfile) + + def setUp(self): + here = os.getcwd() + topDir = here.rstrip('_trial_temp') + self.runDir = os.path.join(here, 'rundir') + self.gpgFile = os.path.join(topDir, 'gnupghome', 'TESTING.subkeys.sec') + self.gpgMoved = os.path.join(here, 'TESTING.subkeys.sec') + + if not os.path.isdir(self.runDir): + os.makedirs(self.runDir) + + self.config = Conf() + self.enableSigning() + self.addCleanup(self.enableSigning) + self.addCleanup(self.removeRundir) + + def test_getGPGContext_good_keyfile(self): + """Test EmailServer.getGPGContext() with a good key filename.""" + self.setKey(self.gpgFile) + ctx = crypto.getGPGContext(self.config) + self.assertIsInstance(ctx, gpgme.Context) + + def test_getGPGContext_missing_keyfile(self): + """Test EmailServer.getGPGContext() with a missing key filename.""" + self.setKey('missing-keyfile.asc') + ctx = crypto.getGPGContext(self.config) + self.assertTrue(ctx is None) + + def test_getGPGContext_bad_keyfile(self): + """Test EmailServer.getGPGContext() with a missing key filename.""" + self.makeBadKey() + ctx = crypto.getGPGContext(self.config) + self.assertTrue(ctx is None) + + def test_getGPGContext_signing_disabled(self): + """getGPGContext() with signing disabled should return None.""" + self.setKey(self.gpgFile) + self.enableSigning(False) + ctx = crypto.getGPGContext(self.config) + self.assertIsNone(ctx) + + def test_getGPGContext_config_signing_missing(self): + """getGPGContext() with a missing/unset 'EMAIL_GPG_SIGNING_ENABLED' + config line should return None. + """ + self.setKey(self.gpgFile) + delattr(self.config, 'EMAIL_GPG_SIGNING_ENABLED') + ctx = crypto.getGPGContext(self.config) + self.assertIsNone(ctx) + + class SSLVerifyingContextFactoryTests(unittest.TestCase, txtagent.FakeReactorAndConnectMixin): """Tests for :class:`bridgedb.crypto.SSLVerifyingContextFactory`."""