Split encryption tests for file-like objects into multiple tests.

This modifies the tests added in #89.
fix/89-python3
Isis Lovecruft 2015-03-10 01:06:20 +00:00
parent b3dd20a7e5
commit 782a81b46a
No known key found for this signature in database
GPG Key ID: 18C16EC5F9F1D673
1 changed files with 49 additions and 25 deletions

View File

@ -777,14 +777,12 @@ authentication."""
log.debug("Encrypted: %s" % encrypted)
self.assertNotEquals(message, encrypted)
def test_encryption_of_file_like_objects(self):
"""Test encryption of file-like objects"""
key = self.generate_key("Craig Gentry", "xorr.ox",
passphrase="craiggentry")
gentry_fpr = str(key.fingerprint)
def _encryption_test_setup(self):
passphrase = "craiggentry"
key = self.generate_key("Craig Gentry", "xorr.ox", passphrase=passphrase)
fpr = str(key.fingerprint)
gentry = self.gpg.export_keys(key.fingerprint)
self.gpg.import_keys(gentry)
message = """
In 2010 Riggio and Sicari presented a practical application of homomorphic
encryption to a hybrid wireless sensor/mesh network. The system enables
@ -792,32 +790,54 @@ transparent multi-hop wireless backhauls that are able to perform statistical
analysis of different kinds of data (temperature, humidity, etc.) coming from
a WSN while ensuring both end-to-end encryption and hop-by-hop
authentication."""
return (message, fpr, passphrase)
def _encryption_test_wrapper(stream_type, message):
stream = stream_type(message)
encrypted = str(self.gpg.encrypt(stream, gentry_fpr))
decrypted = str(self.gpg.decrypt(encrypted,
passphrase="craiggentry"))
self.assertEqual(message, decrypted)
def _encryption_test(self, stream_type, message, fingerprint, passphrase):
stream = stream_type(message)
encrypted = str(self.gpg.encrypt(stream, fingerprint))
decrypted = str(self.gpg.decrypt(encrypted, passphrase=passphrase))
self.assertEqual(message, decrypted)
def test_encryption_of_file_like_objects_io_StringIO(self):
"""Test encryption of file-like object io.StringIO."""
message, fpr, passphrase = self._encryption_test_setup()
# Test io.StringIO and io.BytesIO (Python 2.6+)
try:
from io import StringIO, BytesIO
_encryption_test_wrapper(StringIO, unicode(message))
_encryption_test_wrapper(BytesIO, message)
from io import StringIO
self._encryption_test(StringIO, message, fpr, passphrase)
except ImportError:
pass
# Test StringIO.StringIO
from StringIO import StringIO
_encryption_test_wrapper(StringIO, message)
def test_encryption_of_file_like_objects_io_BytesIO(self):
"""Test encryption of file-like object io.BytesIO."""
message, fpr, passphrase = self._encryption_test_setup()
# Test cStringIO.StringIO
from cStringIO import StringIO
_encryption_test_wrapper(StringIO, message)
try:
from io import BytesIO
if _util._py3k:
self._encryption_test(BytesIO, bytes(message, 'utf-8'), fpr, passphrase)
else:
self._encryption_test(BytesIO, str(message), fpr, passphrase)
except ImportError:
pass
def test_encryption_of_file_like_objects_StringIO_StringIO(self):
"""Test encryption of file-like object StringIO.StringIO (Python2 only)."""
message, fpr, passphrase = self._encryption_test_setup()
if not _util._py3k:
from StringIO import StringIO
self._encryption_test(StringIO, message, fpr, passphrase)
def test_encryption_of_file_like_objects_cStringIO_StringIO(self):
"""Test encryption of file-like object cStringIO.StringIO (Python2 only)."""
message, fpr, passphrase = self._encryption_test_setup()
if not _util._py3k:
from cStringIO import StringIO
self._encryption_test(StringIO, message, fpr, passphrase)
def test_encryption_alt_encoding(self):
"""Test encryption with latin-1 encoding"""
key = self.generate_key("Craig Gentry", "xorr.ox",
passphrase="craiggentry")
@ -998,7 +1018,8 @@ boolean circuit causes a considerable overhead."""
## We expect Alice's key to be hidden (returned as zero's) and Bob's
## key to be there.
expected_values = ["0000000000000000", "0000000000000000"]
self.assertEquals(expected_values, self.gpg.list_packets(encrypted).encrypted_to)
packets = self.gpg.list_packets(encrypted)
self.assertEquals(expected_values, packets.encrypted_to)
def test_encryption_decryption_multi_recipient(self):
"""Test decryption of an encrypted string for multiple users"""
@ -1187,7 +1208,10 @@ suites = { 'parsers': set(['test_parsers_fix_unsafe',
'test_signature_string_verification',
'test_signature_string_algorithm_encoding']),
'crypt': set(['test_encryption',
'test_encryption_of_file_like_objects',
'test_encryption_of_file_like_objects_io_StringIO',
'test_encryption_of_file_like_objects_io_BytesIO',
'test_encryption_of_file_like_objects_StringIO_StringIO',
'test_encryption_of_file_like_objects_cStringIO_StringIO',
'test_encryption_alt_encoding',
'test_encryption_multi_recipient',
'test_encryption_decryption_multi_recipient',