From c28f5464ed30ca3dc59fe32b8ffb96493dded74b Mon Sep 17 00:00:00 2001 From: Nick Daly Date: Sun, 6 May 2012 16:33:20 -0500 Subject: [PATCH] All pgpprocessor tests but one now pass. --- ugly_hacks/santiago/pgpprocessor.py | 39 ++++++++------- ugly_hacks/santiago/test_pgpprocessor.py | 64 ++++++++++++++++++++---- 2 files changed, 75 insertions(+), 28 deletions(-) diff --git a/ugly_hacks/santiago/pgpprocessor.py b/ugly_hacks/santiago/pgpprocessor.py index 678ada57a..2d4b34da4 100644 --- a/ugly_hacks/santiago/pgpprocessor.py +++ b/ugly_hacks/santiago/pgpprocessor.py @@ -25,13 +25,13 @@ class Unwrapper(object): TYPES = (SIG, CRYPT) = "sig", "crypt" SIG_LINES = (SIG_HEAD, SIG_BODY, SIG_FOOTER, SIG_END) = ( - "-----BEGIN PGP SIGNED MESSAGE-----", - "", - "-----BEGIN PGP SIGNATURE-----", - "-----END PGP SIGNATURE-----") + "-----BEGIN PGP SIGNED MESSAGE-----\n", + "\n", + "-----BEGIN PGP SIGNATURE-----\n", + "-----END PGP SIGNATURE-----\n") - CRYPT_LINES = (CRYPT_HEAD, CRYPT_END) = ("-----BEGIN PGP MESSAGE-----", - "-----END PGP MESSAGE-----") + CRYPT_LINES = (CRYPT_HEAD, CRYPT_END) = ("-----BEGIN PGP MESSAGE-----\n", + "-----END PGP MESSAGE-----\n") # ^- - - (-----SOMETHING-----|-----OTHERTHING-----)$ _TARGET = "^(- )+({0})$" @@ -121,15 +121,16 @@ class Unwrapper(object): continue getattr(self, point).append(line) - - self.handle_message(point, type_) - return "\n".join(self.body) + self.handle_end_conditions(point, type_) - def handle_message(self, point, type_): + return "".join(self.body) + + def handle_end_conditions(self, point, type_): """Handle end-conditions of message. - Do the right thing based on the state machine's results. + Do the right thing based on the state machine's results. If there is no + PGP data in the message, raise a StopIteration error. """ if point != Unwrapper.END or type_ not in (Unwrapper.CRYPT, @@ -148,9 +149,9 @@ class Unwrapper(object): self.body = Unwrapper.unwrap(self.body, self.type) # reset the state machine, now that we've unwrapped a layer. - self.message = "\n".join(self.body) + self.message = "".join(self.body) - if not self.gpg_data: + if not (self.gpg_data and self.gpg_data.valid): raise InvalidSignatureError() @classmethod @@ -179,20 +180,20 @@ class Unwrapper(object): def __str__(self): """Returns the GPG-part of the current message. - Non-PGP-message data are not returned. + Non-PGP-message data (before and after the message) are not returned. """ - return "\n".join([ - "\n".join(x) for x in (self.header, self.body, self.footer) ]) + return "".join([ + "".join(x) for x in (self.header, self.body, self.footer) ]) def original_message(self): """Returns the current wrapped message. - It's an iterator, so it discards previous iterations' data. + It's an iterator, so previous iterations' data isn't available. """ - return "\n".join([ - "\n".join(x) for x in (self.start, self.header, self.body, + return "".join([ + "".join(x) for x in (self.start, self.header, self.body, self.footer, self.end) ]) if __name__ == "__main__": diff --git a/ugly_hacks/santiago/test_pgpprocessor.py b/ugly_hacks/santiago/test_pgpprocessor.py index 4cac44e3a..20a15fb00 100644 --- a/ugly_hacks/santiago/test_pgpprocessor.py +++ b/ugly_hacks/santiago/test_pgpprocessor.py @@ -3,8 +3,8 @@ """Tests for the PGP Processing Tools. All aspects of each PGP processor should be fully tested: this verifies -identity, allowing trust to exist in the system. If this is mucked up, there -can be no trustworthy trust. +identity, allowing trust to exist in the system. If this is mucked up, trust +isn't verifiable. """ @@ -19,6 +19,14 @@ import unittest ITERATIONS = 3 +def remove_line(string, line, preserve_newlines = True): + """Remove a line from a multi-line string.""" + + if preserve_newlines and not line.endswith("\n"): + line =+ "\n" + + return str(string.splitlines(preserve_newlines).remove(line)) + class ProcessorCase(unittest.TestCase): """The superclass for pgpprocessor tests, containing shared setup: @@ -48,12 +56,13 @@ class UnwrapperTest(ProcessorCase): def test_messages_wrapped(self): """Were the messages correctly wrapped in the first place?""" - self.assertEqual(len(self.messages), ITERATIONS + 1) + self.assertEqual(ITERATIONS + 1, len(self.messages)) def test_unwrap_all_messages(self): - """Do we actually hit all the messages?""" + """Do we unwrap the right number of messages?""" - self.assertEqual(len([self.unwrapper]), ITERATIONS + 1) + # count each element in the iterator once, skipping the first. + self.assertEqual(ITERATIONS, sum([1 for e in self.unwrapper])) def test_creating_message_doesnt_unwrap(self): """Creating an unwrapper shouldn't unwrap the message. @@ -68,18 +77,55 @@ class UnwrapperTest(ProcessorCase): def test_iterator_unwraps_correctly(self): """The iterator should correctly unwrap each stage of the message.""" - for i, message in enumerate(self.messages): + unwrapped_messages = self.messages[:-1] + + for i, message in enumerate(reversed(unwrapped_messages)): unwrapped = self.unwrapper.next() - self.assertEqual(message, unwrapped) + self.assertEqual(message.strip(), unwrapped.strip()) def test_original_message(self): """Unwrapper.original_message actually returns the original message.""" - for i, message in enumerate(self.messages): + unwrapped_messages = self.messages[:-1] + + for i, message in enumerate(reversed(unwrapped_messages)): unwrapped = self.unwrapper.next() - self.assertEqual(self.unwrapper.original_message(), message) + self.assertEqual(self.unwrapper.original_message().strip(), + message.strip()) + + def test_no_header_invalid(self): + """Messages without heads are considered invalid.""" + + self.unwrapper.message = remove_line( + self.unwrapper.message, "-----BEGIN PGP SIGNED MESSAGE-----\n") + + self.assertRaises(StopIteration, self.unwrapper.next) + + def test_no_body_invalid(self): + """Messages without bodies are considered invalid.""" + + self.unwrapper.message = remove_line(self.unwrapper.message, "\n") + + self.assertRaises(StopIteration, self.unwrapper.next) + + def test_no_footer_invalid(self): + """Messages without feet are considered invalid.""" + + self.unwrapper.message = remove_line( + self.unwrapper.message, "-----BEGIN PGP SIGNATURE-----\n") + + self.assertRaises(StopIteration, self.unwrapper.next) + + def test_no_end_invalid(self): + """Messages without tails are considered invalid.""" + + self.unwrapper.message = remove_line( + self.unwrapper.message, "-----END PGP SIGNATURE-----\n") + + self.assertRaises(StopIteration, self.unwrapper.next) + if __name__ == "__main__": unittest.main()