From edf035c2229d2b02ea2ea53eb59695e7824fb52c Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Sun, 13 May 2012 09:45:33 -0500 Subject: [PATCH] Renamed errors to utilities, added more utility functions. --- ugly_hacks/santiago/errors.py | 14 ------- ugly_hacks/santiago/test_pgpprocessor.py | 34 +++-------------- ugly_hacks/santiago/test_santiago.py | 7 +--- ugly_hacks/santiago/utilities.py | 48 ++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 48 deletions(-) delete mode 100644 ugly_hacks/santiago/errors.py create mode 100644 ugly_hacks/santiago/utilities.py diff --git a/ugly_hacks/santiago/errors.py b/ugly_hacks/santiago/errors.py deleted file mode 100644 index ac6a1b820..000000000 --- a/ugly_hacks/santiago/errors.py +++ /dev/null @@ -1,14 +0,0 @@ -class SignatureError(Exception): - """Base class for signature-related errors.""" - - pass - -class InvalidSignatureError(SignatureError): - """The signature in this message is cryptographically invalid.""" - - pass - -class UnwillingHostError(SignatureError): - """The current process isn't willing to host a service for the client.""" - - pass diff --git a/ugly_hacks/santiago/test_pgpprocessor.py b/ugly_hacks/santiago/test_pgpprocessor.py index e63f7eae9..422b9a63e 100644 --- a/ugly_hacks/santiago/test_pgpprocessor.py +++ b/ugly_hacks/santiago/test_pgpprocessor.py @@ -8,10 +8,10 @@ isn't verifiable. """ -import ConfigParser as configparser import gnupg import pgpprocessor import unittest +import utilities def remove_line(string, line, preserve_newlines = True): """Remove a line from a multi-line string.""" @@ -22,37 +22,11 @@ def remove_line(string, line, preserve_newlines = True): return str(string.splitlines(preserve_newlines).remove(line)) -def load_config(): - """Returns data from the test.cfg file.""" - - config = configparser.ConfigParser( - {"KEYID": - "D95C32042EE54FFDB25EC3489F2733F40928D23A"}) - config.read(["test.cfg"]) - return config - - -def multi_sign(message="hi", iterations=3, keyid=None, gpg=None): - """Sign a message several times with a specified key.""" - - messages = [message] - - if not gpg: - gpg = gnupg.GPG(use_agent = True) - if not keyid: - keyid = load_config().get("pgpprocessor", "keyid") - - for i in range(iterations): - messages.append(str(gpg.sign(messages[i], keyid=keyid))) - - return messages - - class MessageWrapper(unittest.TestCase): """Basic setup for message-signing tests. These tests would run much faster if I could use setUpClass (>30x faster: - signing four messages for each test consumes lots of entropy that needs to + signing three messages for each test consumes lots of entropy that needs to be rebuilt?), but that's a Python 2.7 feature. I'll rewrite this when Debian Stable includes Python 2.7 or Python 3.X. It's much prettier. @@ -61,7 +35,9 @@ class MessageWrapper(unittest.TestCase): self.iterations = 3 self.gpg = gnupg.GPG(use_agent = True) - self.messages = multi_sign(gpg = self.gpg, iterations = self.iterations) + self.messages = utilities.multi_sign( + gpg = self.gpg, + iterations = self.iterations) class UnwrapperTest(MessageWrapper): """Verify that we can unwrap multiply-signed PGP messages correctly.""" diff --git a/ugly_hacks/santiago/test_santiago.py b/ugly_hacks/santiago/test_santiago.py index ae26df802..3c391638d 100644 --- a/ugly_hacks/santiago/test_santiago.py +++ b/ugly_hacks/santiago/test_santiago.py @@ -1,4 +1,4 @@ -#! /usr/bin/python -*- mode: autofill; fill-column: 80 -*- +#! /usr/bin/python -*- mode: auto-fill; fill-column: 80 -*- """Making Santiago dance, in 4 parts: @@ -40,17 +40,14 @@ If I produce a listener that just echoes the parameters, I can validate the resp """ -import ConfigParser as configparser import os import sys import unittest import gnupg import logging -from errors import InvalidSignatureError, UnwillingHostError import simplesantiago as santiago -import test_pgpprocessor -import pgpprocessor +import utilities # class SantiagoTest(unittest.TestCase): diff --git a/ugly_hacks/santiago/utilities.py b/ugly_hacks/santiago/utilities.py new file mode 100644 index 000000000..01a624ee1 --- /dev/null +++ b/ugly_hacks/santiago/utilities.py @@ -0,0 +1,48 @@ +"""Shared utilities. + +Currently contains a bunch of errors and config-file shortcuts. + +""" + +import ConfigParser as configparser + + +def load_config(configfile="test.cfg"): + """Returns data from the named config file.""" + + config = configparser.ConfigParser( + {"KEYID": + "D95C32042EE54FFDB25EC3489F2733F40928D23A"}) + config.read([configfile]) + return config + +def multi_sign(message="hi", iterations=3, keyid=None, gpg=None): + """Sign a message several times with a specified key.""" + + messages = [message] + + if not gpg: + gpg = gnupg.GPG(use_agent = True) + if not keyid: + keyid = load_config().get("pgpprocessor", "keyid") + + for i in range(iterations): + messages.append(str(gpg.sign(messages[i], keyid=keyid))) + + return messages + + +class SignatureError(Exception): + """Base class for signature-related errors.""" + + pass + +class InvalidSignatureError(SignatureError): + """The signature in this message is cryptographically invalid.""" + + pass + +class UnwillingHostError(SignatureError): + """The current process isn't willing to host a service for the client.""" + + pass