mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-09-19 04:59:01 +00:00
Move MessageWrapper's message wrapping into multi_sign.
This commit is contained in:
parent
e8f6df8d04
commit
ad89a8dd15
@ -13,8 +13,6 @@ import gnupg
|
|||||||
import pgpprocessor
|
import pgpprocessor
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
ITERATIONS = 3
|
|
||||||
|
|
||||||
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."""
|
||||||
|
|
||||||
@ -23,30 +21,40 @@ def remove_line(string, line, preserve_newlines = True):
|
|||||||
|
|
||||||
return str(string.splitlines(preserve_newlines).remove(line))
|
return str(string.splitlines(preserve_newlines).remove(line))
|
||||||
|
|
||||||
class MessageWrapper(unittest.TestCase):
|
|
||||||
"""The superclass for pgpprocessor tests, containing shared setup:
|
|
||||||
|
|
||||||
- Sign a message several times with a specified key.
|
def load_config():
|
||||||
|
"""Returns data from the test.cfg file."""
|
||||||
|
|
||||||
"""
|
config = configparser.ConfigParser(
|
||||||
MESSAGES = [ str({"host": "somebody"}), ]
|
|
||||||
GPG = gnupg.GPG(use_agent = True)
|
|
||||||
|
|
||||||
CONFIG = configparser.ConfigParser(
|
|
||||||
{"KEYID":
|
{"KEYID":
|
||||||
"D95C32042EE54FFDB25EC3489F2733F40928D23A"})
|
"D95C32042EE54FFDB25EC3489F2733F40928D23A"})
|
||||||
CONFIG.read(["test.cfg"])
|
config.read(["test.cfg"])
|
||||||
KEYID = CONFIG.get("pgpprocessor", "keyid")
|
return config
|
||||||
|
|
||||||
# sign the message a few times.
|
|
||||||
for i in range(ITERATIONS):
|
def multi_sign(message="hi", iterations=3, keyid=None, gpg=None):
|
||||||
MESSAGES.append(str(GPG.sign(MESSAGES[i], keyid = KEYID)))
|
"""Sign a message several times with a specified key."""
|
||||||
|
|
||||||
|
messages = list(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):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(MessageWrapper, self).setUp()
|
|
||||||
|
|
||||||
self.messages = list(MessageWrapper.MESSAGES)
|
self.iterations = 3
|
||||||
self.keyid = MessageWrapper.KEYID
|
self.gpg = gnupg.GPG(use_agent = True)
|
||||||
|
self.messages = 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."""
|
||||||
@ -55,18 +63,18 @@ class UnwrapperTest(MessageWrapper):
|
|||||||
super(UnwrapperTest, self).setUp()
|
super(UnwrapperTest, self).setUp()
|
||||||
|
|
||||||
self.unwrapper = pgpprocessor.Unwrapper(self.messages[-1],
|
self.unwrapper = pgpprocessor.Unwrapper(self.messages[-1],
|
||||||
MessageWrapper.GPG)
|
self.gpg)
|
||||||
|
|
||||||
def test_messages_wrapped(self):
|
def test_messages_wrapped(self):
|
||||||
"""Were the messages correctly wrapped in the first place?"""
|
"""Were the messages correctly wrapped in the first place?"""
|
||||||
|
|
||||||
self.assertEqual(ITERATIONS + 1, len(self.messages))
|
self.assertEqual(self.iterations + 1, len(self.messages))
|
||||||
|
|
||||||
def test_unwrap_all_messages(self):
|
def test_unwrap_all_messages(self):
|
||||||
"""Do we unwrap the right number of messages?"""
|
"""Do we unwrap the right number of messages?"""
|
||||||
|
|
||||||
# count each element in the iterator once, skipping the first.
|
# count each element in the iterator once, skipping the first.
|
||||||
self.assertEqual(ITERATIONS, sum([1 for e in self.unwrapper]))
|
self.assertEqual(self.iterations, sum([1 for e in self.unwrapper]))
|
||||||
|
|
||||||
def test_dont_uwrap(self):
|
def test_dont_uwrap(self):
|
||||||
"""Creating an unwrapper shouldn't unwrap the message.
|
"""Creating an unwrapper shouldn't unwrap the message.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user