From 6ce8f458f51628df68a559784d42f4874b15e21c Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 01:48:25 +0530 Subject: Tests are now functional (only running without syntax errors) on Python 3 too. --- rsa/key.py | 10 +++++----- rsa/pkcs1.py | 6 +++--- rsa/prime.py | 6 +++--- tests/__init__.py | 0 tests/constants.py | 9 +++++++++ tests/py2kconstants.py | 3 +++ tests/py3kconstants.py | 3 +++ tests/test_bigfile.py | 5 ++++- tests/test_integers.py | 10 +++++----- tests/test_load_save_keys.py | 21 +++++++++++---------- tests/test_pkcs1.py | 11 +++++------ tests/test_strings.py | 12 ++++++++---- tests/test_varblock.py | 5 ++++- 13 files changed, 63 insertions(+), 38 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/constants.py create mode 100644 tests/py2kconstants.py create mode 100644 tests/py3kconstants.py diff --git a/rsa/key.py b/rsa/key.py index 479b8f0..17bf69e 100644 --- a/rsa/key.py +++ b/rsa/key.py @@ -116,7 +116,7 @@ class PublicKey(AbstractKey): return getattr(self, key) def __repr__(self): - return u'PublicKey(%i, %i)' % (self.n, self.e) + return 'PublicKey(%i, %i)' % (self.n, self.e) def __eq__(self, other): if other is None: @@ -272,7 +272,7 @@ class PrivateKey(AbstractKey): return getattr(self, key) def __repr__(self): - return u'PrivateKey(%(n)i, %(e)i, %(d)i, %(p)i, %(q)i)' % self + return 'PrivateKey(%(n)i, %(e)i, %(d)i, %(p)i, %(q)i)' % self def __eq__(self, other): if other is None: @@ -573,8 +573,8 @@ if __name__ == '__main__': break if (count and count % 10 == 0) or count == 1: - print '%i times' % count + print('%i times' % count) except KeyboardInterrupt: - print 'Aborted' + print('Aborted') else: - print 'Doctests done' + print('Doctests done') diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py index fbe9fe7..9686cf9 100644 --- a/rsa/pkcs1.py +++ b/rsa/pkcs1.py @@ -374,7 +374,7 @@ __all__ = ['encrypt', 'decrypt', 'sign', 'verify', 'DecryptionError', 'VerificationError', 'CryptoError'] if __name__ == '__main__': - print 'Running doctests 1000x or until failure' + print('Running doctests 1000x or until failure') import doctest for count in range(1000): @@ -383,6 +383,6 @@ if __name__ == '__main__': break if count and count % 100 == 0: - print '%i times' % count + print('%i times' % count) - print 'Doctests done' + print('Doctests done') diff --git a/rsa/prime.py b/rsa/prime.py index 340ab94..4dc190c 100644 --- a/rsa/prime.py +++ b/rsa/prime.py @@ -152,7 +152,7 @@ def are_relatively_prime(a, b): return (d == 1) if __name__ == '__main__': - print 'Running doctests 1000x or until failure' + print('Running doctests 1000x or until failure') import doctest for count in range(1000): @@ -161,6 +161,6 @@ if __name__ == '__main__': break if count and count % 100 == 0: - print '%i times' % count + print('%i times' % count) - print 'Doctests done' + print('Doctests done') diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/constants.py b/tests/constants.py new file mode 100644 index 0000000..6a0d081 --- /dev/null +++ b/tests/constants.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- + +from rsa._compat import have_python3 + +if have_python3: + from py3kconstants import * +else: + from py2kconstants import * + diff --git a/tests/py2kconstants.py b/tests/py2kconstants.py new file mode 100644 index 0000000..5f695dd --- /dev/null +++ b/tests/py2kconstants.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +unicode_string = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/py3kconstants.py b/tests/py3kconstants.py new file mode 100644 index 0000000..83b6712 --- /dev/null +++ b/tests/py3kconstants.py @@ -0,0 +1,3 @@ +# -*- coding: utf-8 -*- + +unicode_string = "Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 30156a1..39bd095 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,6 +1,9 @@ '''Tests block operations.''' -from StringIO import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO import unittest import rsa diff --git a/tests/test_integers.py b/tests/test_integers.py index 111f8ab..d4fa087 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -12,13 +12,13 @@ class IntegerTest(unittest.TestCase): def test_enc_dec(self): message = 42 - print "\tMessage: %d" % message + print("\tMessage: %d" % message) encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n) - print "\tEncrypted: %d" % encrypted + print("\tEncrypted: %d" % encrypted) decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n) - print "\tDecrypted: %d" % decrypted + print("\tDecrypted: %d" % decrypted) self.assertEqual(message, decrypted) @@ -27,10 +27,10 @@ class IntegerTest(unittest.TestCase): message = 42 signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n) - print "\tSigned: %d" % signed + print("\tSigned: %d" % signed) verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n) - print "\tVerified: %d" % verified + print("\tVerified: %d" % verified) self.assertEqual(message, verified) diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 466a3f5..56d45c4 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -2,16 +2,17 @@ import base64 import unittest +from rsa._compat import b import rsa.key -B64PRIV_DER = 'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt' +B64PRIV_DER = b('MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt') PRIVATE_DER = base64.decodestring(B64PRIV_DER) -B64PUB_DER = 'MAwCBQDeKYlRAgMBAAE=' +B64PUB_DER = b('MAwCBQDeKYlRAgMBAAE=') PUBLIC_DER = base64.decodestring(B64PUB_DER) -PRIVATE_PEM = ''' +PRIVATE_PEM = b(''' -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -23,15 +24,15 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PRIV_DER +''' % B64PRIV_DER) -CLEAN_PRIVATE_PEM = '''\ +CLEAN_PRIVATE_PEM = b('''\ -----BEGIN RSA PRIVATE KEY----- %s -----END RSA PRIVATE KEY----- -''' % B64PRIV_DER +''' % B64PRIV_DER) -PUBLIC_PEM = ''' +PUBLIC_PEM = b(''' -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -43,13 +44,13 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PUB_DER +''' % B64PUB_DER) -CLEAN_PUBLIC_PEM = '''\ +CLEAN_PUBLIC_PEM = b('''\ -----BEGIN RSA PUBLIC KEY----- %s -----END RSA PUBLIC KEY----- -''' % B64PUB_DER +''' % B64PUB_DER) class DerTest(unittest.TestCase): diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 3392ed7..c841485 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -14,13 +14,13 @@ class BinaryTest(unittest.TestCase): def test_enc_dec(self): message = struct.pack('>IIII', 0, 0, 0, 1) - print "\tMessage: %r" % message + print("\tMessage: %r" % message) encrypted = pkcs1.encrypt(message, self.pub) - print "\tEncrypted: %r" % encrypted + print("\tEncrypted: %r" % encrypted) decrypted = pkcs1.decrypt(encrypted, self.priv) - print "\tDecrypted: %r" % decrypted + print("\tDecrypted: %r" % decrypted) self.assertEqual(message, decrypted) @@ -55,10 +55,10 @@ class SignatureTest(unittest.TestCase): '''Test happy flow of sign and verify''' message = 'je moeder' - print "\tMessage: %r" % message + print("\tMessage: %r" % message) signature = pkcs1.sign(message, self.priv, 'SHA-256') - print "\tSignature: %r" % signature + print("\tSignature: %r" % signature) pkcs1.verify(message, signature, self.pub) @@ -88,4 +88,3 @@ class SignatureTest(unittest.TestCase): self.assertEqual(signature1, signature2) - \ No newline at end of file diff --git a/tests/test_strings.py b/tests/test_strings.py index 38fae06..58d3833 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,9 +1,13 @@ '''Tests string operations.''' +from __future__ import absolute_import + import unittest import rsa +from tests.constants import unicode_string + class StringTest(unittest.TestCase): def setUp(self): @@ -11,14 +15,14 @@ class StringTest(unittest.TestCase): def test_enc_dec(self): - message = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ".encode('utf-8') - print "\tMessage: %s" % message + message = unicode_string.encode('utf-8') + print("\tMessage: %s" % message) encrypted = rsa.encrypt(message, self.pub) - print "\tEncrypted: %s" % encrypted + print("\tEncrypted: %s" % encrypted) decrypted = rsa.decrypt(encrypted, self.priv) - print "\tDecrypted: %s" % decrypted + print("\tDecrypted: %s" % decrypted) self.assertEqual(message, decrypted) diff --git a/tests/test_varblock.py b/tests/test_varblock.py index d8addb4..6195258 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,6 +1,9 @@ '''Tests varblock operations.''' -from StringIO import StringIO +try: + from StringIO import StringIO +except ImportError: + from io import StringIO import unittest import rsa -- cgit v1.2.1