From 2eeea2ca19fdea9f354bbec685e168fee370d19f Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 02:38:13 +0530 Subject: Porting to Python 3 complete. All tests except pyasn1 stuff pass. --- tests/test_bigfile.py | 19 ++++++++++--------- tests/test_load_save_keys.py | 8 ++++---- tests/test_pkcs1.py | 20 +++++++++++++++----- 3 files changed, 29 insertions(+), 18 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 02e052e..974da8b 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,9 +1,10 @@ '''Tests block operations.''' +from rsa._compat import b try: - from StringIO import StringIO + from StringIO import StringIO as BytesIO except ImportError: - from io import StringIO + from io import BytesIO import unittest2 import rsa @@ -17,17 +18,17 @@ class BigfileTest(unittest2.TestCase): pub_key, priv_key = rsa.newkeys((6 + 11) * 8) # Encrypt the file - message = '123456Sybren' - infile = StringIO(message) - outfile = StringIO() + message = b('123456Sybren') + infile = BytesIO(message) + outfile = BytesIO() bigfile.encrypt_bigfile(infile, outfile, pub_key) # Test crypto = outfile.getvalue() - cryptfile = StringIO(crypto) - clearfile = StringIO() + cryptfile = BytesIO(crypto) + clearfile = BytesIO() bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key) self.assertEquals(clearfile.getvalue(), message) @@ -45,7 +46,7 @@ class BigfileTest(unittest2.TestCase): pub_key, priv_key = rsa.newkeys((34 + 11) * 8) # Sign the file - msgfile = StringIO('123456Sybren') + msgfile = BytesIO(b('123456Sybren')) signature = pkcs1.sign(msgfile, priv_key, 'MD5') # Check the signature @@ -53,7 +54,7 @@ class BigfileTest(unittest2.TestCase): pkcs1.verify(msgfile, signature, pub_key) # Alter the message, re-check - msgfile = StringIO('123456sybren') + msgfile = BytesIO(b('123456sybren')) self.assertRaises(pkcs1.VerificationError, pkcs1.verify, msgfile, signature, pub_key) diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index fabe92f..fc1a1aa 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -24,13 +24,13 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PRIV_DER) +''' % B64PRIV_DER.decode("utf-8")) CLEAN_PRIVATE_PEM = b('''\ -----BEGIN RSA PRIVATE KEY----- %s -----END RSA PRIVATE KEY----- -''' % B64PRIV_DER) +''' % B64PRIV_DER.decode("utf-8")) PUBLIC_PEM = b(''' -----BEGIN CONFUSING STUFF----- @@ -44,13 +44,13 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PUB_DER) +''' % B64PUB_DER.decode("utf-8")) CLEAN_PUBLIC_PEM = b('''\ -----BEGIN RSA PUBLIC KEY----- %s -----END RSA PUBLIC KEY----- -''' % B64PUB_DER) +''' % B64PUB_DER.decode("utf-8")) class DerTest(unittest2.TestCase): diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index d8fb1b4..82a3775 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -5,6 +5,7 @@ import unittest2 import rsa from rsa import pkcs1 +from rsa._compat import byte, is_integer, b, is_bytes class BinaryTest(unittest2.TestCase): @@ -29,8 +30,17 @@ class BinaryTest(unittest2.TestCase): message = struct.pack('>IIII', 0, 0, 0, 1) encrypted = pkcs1.encrypt(message, self.pub) + def _ord(a): + if is_integer(a): + return a + else: + return ord(a) + # Alter the encrypted stream - encrypted = encrypted[:5] + chr(ord(encrypted[5]) + 1) + encrypted[6:] + a = encrypted[5] + if is_bytes(a): + a = ord(a) + encrypted = encrypted[:5] + byte(a + 1) + encrypted[6:] self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted, self.priv) @@ -54,7 +64,7 @@ class SignatureTest(unittest2.TestCase): def test_sign_verify(self): '''Test happy flow of sign and verify''' - message = 'je moeder' + message = b('je moeder') print("\tMessage: %r" % message) signature = pkcs1.sign(message, self.priv, 'SHA-256') @@ -65,16 +75,16 @@ class SignatureTest(unittest2.TestCase): def test_alter_message(self): '''Altering the message should let the verification fail.''' - signature = pkcs1.sign('je moeder', self.priv, 'SHA-256') + signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, - 'mijn moeder', signature, self.pub) + b('mijn moeder'), signature, self.pub) def test_sign_different_key(self): '''Signing with another key should let the verification fail.''' (otherpub, _) = rsa.newkeys(512) - message = 'je moeder' + message = b('je moeder') signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, message, signature, otherpub) -- cgit v1.2.1