Added more tests, made tests more reliable.

This commit is contained in:
Nick Daly 2012-05-01 07:46:43 -05:00
parent 832185338b
commit d499ea794c
2 changed files with 33 additions and 13 deletions

View File

@ -25,10 +25,10 @@ class Unwrapper(object):
TYPES = (SIG, CRYPT) = "sig", "crypt" TYPES = (SIG, CRYPT) = "sig", "crypt"
SIG_LINES = (SIG_HEAD, SIG_BODY, SIG_FOOTER, SIG_END) = ( SIG_LINES = (SIG_HEAD, SIG_BODY, SIG_FOOTER, SIG_END) = (
"-----BEGIN PGP SIGNED MESSAGE-----", "-----BEGIN PGP SIGNED MESSAGE-----",
"", "",
"-----BEGIN PGP SIGNATURE-----", "-----BEGIN PGP SIGNATURE-----",
"-----END PGP SIGNATURE-----") "-----END PGP SIGNATURE-----")
CRYPT_LINES = (CRYPT_HEAD, CRYPT_END) = ("-----BEGIN PGP MESSAGE-----", CRYPT_LINES = (CRYPT_HEAD, CRYPT_END) = ("-----BEGIN PGP MESSAGE-----",
"-----END PGP MESSAGE-----") "-----END PGP MESSAGE-----")
@ -133,7 +133,7 @@ class Unwrapper(object):
""" """
if point != Unwrapper.END or type_ not in (Unwrapper.CRYPT, if point != Unwrapper.END or type_ not in (Unwrapper.CRYPT,
Unwrapper.SIG): Unwrapper.SIG):
raise StopIteration("No valid PGP data.") raise StopIteration("No valid PGP data.")
args = (self.gnupg_verify if type_ == Unwrapper.SIG args = (self.gnupg_verify if type_ == Unwrapper.SIG
@ -142,7 +142,7 @@ class Unwrapper(object):
self.gpg_data = { self.gpg_data = {
Unwrapper.SIG: self.gpg.verify, Unwrapper.SIG: self.gpg.verify,
Unwrapper.CRYPT: self.gpg.decrypt Unwrapper.CRYPT: self.gpg.decrypt
}[type_](str(self), **args) }[type_](str(self), **args)
self.type = type_ self.type = type_
self.body = Unwrapper.unwrap(self.body, self.type) self.body = Unwrapper.unwrap(self.body, self.type)
@ -194,3 +194,6 @@ class Unwrapper(object):
return "\n".join([ return "\n".join([
"\n".join(x) for x in (self.start, self.header, self.body, "\n".join(x) for x in (self.start, self.header, self.body,
self.footer, self.end) ]) self.footer, self.end) ])
if __name__ == "__main__":
unittest.main()

View File

@ -8,13 +8,17 @@ can be no trustworthy trust.
""" """
from pprint import pprint import sys
sys.path.extend(["../..", ".", "/home/nick/programs/python-gnupg/python-gnupg-0.2.9"])
from pprint import pprint
import ConfigParser as configparser import ConfigParser as configparser
import gnupg import gnupg
import pgpprocessor import pgpprocessor
import unittest import unittest
ITERATIONS = 3
class ProcessorCase(unittest.TestCase): class ProcessorCase(unittest.TestCase):
"""The superclass for pgpprocessor tests, containing shared setup: """The superclass for pgpprocessor tests, containing shared setup:
@ -27,9 +31,9 @@ class ProcessorCase(unittest.TestCase):
CONFIG = configparser.ConfigParser({"KEYID": "0928D23A"}) CONFIG = configparser.ConfigParser({"KEYID": "0928D23A"})
CONFIG.read(["test.cfg"]) CONFIG.read(["test.cfg"])
KEYID = CONFIG.get("pgpprocessor", "keyid") KEYID = CONFIG.get("pgpprocessor", "keyid")
# sign the message a few times. # sign the message a few times.
for i in range(0, 3): for i in range(ITERATIONS):
MESSAGES.append(str(GPG.sign(MESSAGES[i], keyid = KEYID))) MESSAGES.append(str(GPG.sign(MESSAGES[i], keyid = KEYID)))
class UnwrapperTest(ProcessorCase): class UnwrapperTest(ProcessorCase):
@ -41,6 +45,16 @@ class UnwrapperTest(ProcessorCase):
self.unwrapper = pgpprocessor.Unwrapper(self.messages[-1], self.unwrapper = pgpprocessor.Unwrapper(self.messages[-1],
ProcessorCase.GPG) ProcessorCase.GPG)
def test_messages_wrapped(self):
"""Were the messages correctly wrapped in the first place?"""
self.assertEqual(len(self.messages), ITERATIONS + 1)
def test_unwrap_all_messages(self):
"""Do we actually hit all the messages?"""
self.assertEqual(len([self.unwrapper]), ITERATIONS + 1)
def test_creating_message_doesnt_unwrap(self): def test_creating_message_doesnt_unwrap(self):
"""Creating an unwrapper shouldn't unwrap the message. """Creating an unwrapper shouldn't unwrap the message.
@ -54,15 +68,18 @@ class UnwrapperTest(ProcessorCase):
def test_iterator_unwraps_correctly(self): def test_iterator_unwraps_correctly(self):
"""The iterator should correctly unwrap each stage of the message.""" """The iterator should correctly unwrap each stage of the message."""
for i, message in enumerate(self.unwrapper): for i, message in enumerate(self.messages):
self.assertEqual(message, self.messages[-i-2]) unwrapped = self.unwrapper.next()
self.assertEqual(message, unwrapped)
def test_original_message(self): def test_original_message(self):
"""Unwrapper.original_message actually returns the original message.""" """Unwrapper.original_message actually returns the original message."""
print (self.unwrapper.message) for i, message in enumerate(self.messages):
self.assertEqual(self.unwrapper.next(), self.messages[-1]) unwrapped = self.unwrapper.next()
self.assertEqual(self.unwrapper.original_message(), message)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()