Renamed errors to utilities, added more utility functions.

This commit is contained in:
Nick Daly 2012-05-13 09:45:33 -05:00
parent 36420059df
commit edf035c222
4 changed files with 55 additions and 48 deletions

View File

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

View File

@ -8,10 +8,10 @@ isn't verifiable.
""" """
import ConfigParser as configparser
import gnupg import gnupg
import pgpprocessor import pgpprocessor
import unittest import unittest
import utilities
def remove_line(string, line, preserve_newlines = True): def remove_line(string, line, preserve_newlines = True):
"""Remove a line from a multi-line string.""" """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)) 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): class MessageWrapper(unittest.TestCase):
"""Basic setup for message-signing tests. """Basic setup for message-signing tests.
These tests would run much faster if I could use setUpClass (>30x faster: 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 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. 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.iterations = 3
self.gpg = gnupg.GPG(use_agent = True) 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): class UnwrapperTest(MessageWrapper):
"""Verify that we can unwrap multiply-signed PGP messages correctly.""" """Verify that we can unwrap multiply-signed PGP messages correctly."""

View File

@ -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: """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 os
import sys import sys
import unittest import unittest
import gnupg import gnupg
import logging import logging
from errors import InvalidSignatureError, UnwillingHostError
import simplesantiago as santiago import simplesantiago as santiago
import test_pgpprocessor import utilities
import pgpprocessor
# class SantiagoTest(unittest.TestCase): # class SantiagoTest(unittest.TestCase):

View File

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