From ff3a1d04860d46c3ff48a6c9dcda88091e1f76cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 20 Jun 2011 00:13:53 +0200 Subject: Added unittests --- tests/test_integers.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_strings.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 tests/test_integers.py create mode 100644 tests/test_strings.py (limited to 'tests') diff --git a/tests/test_integers.py b/tests/test_integers.py new file mode 100644 index 0000000..1e318ab --- /dev/null +++ b/tests/test_integers.py @@ -0,0 +1,36 @@ +'''Tests integer operations.''' + +import unittest + +import rsa + +class IntegerTest(unittest.TestCase): + + def setUp(self): + (self.pub, self.priv) = rsa.newkeys(64) + + def test_enc_dec(self): + + message = 42 + print "\tMessage: %d" % message + + encrypted = rsa.encrypt_int(message, self.pub['e'], self.pub['n']) + print "\tEncrypted: %d" % encrypted + + decrypted = rsa.decrypt_int(encrypted, self.priv['d'], self.pub['n']) + print "\tDecrypted: %d" % decrypted + + self.assertEqual(message, decrypted) + + def test_sign_verify(self): + + message = 42 + + signed = rsa.encrypt_int(message,self.priv['d'], self.pub['n']) + print "\tSigned: %d" % signed + + verified = rsa.decrypt_int(signed, self.pub['e'],self.pub['n']) + print "\tVerified: %d" % verified + + self.assertEqual(message, verified) + diff --git a/tests/test_strings.py b/tests/test_strings.py new file mode 100644 index 0000000..c5803e4 --- /dev/null +++ b/tests/test_strings.py @@ -0,0 +1,38 @@ +'''Tests string operations.''' + +import unittest + +import rsa + +class StringTest(unittest.TestCase): + + def setUp(self): + (self.pub, self.priv) = rsa.newkeys(64) + + def test_enc_dec(self): + + # TODO: test with unicode strings and non-ascii chars + message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + print "\tMessage: %s" % message + + encrypted = rsa.encrypt(message, self.pub) + print "\tEncrypted: %s" % encrypted + + decrypted = rsa.decrypt(encrypted, self.priv) + print "\tDecrypted: %s" % decrypted + + self.assertEqual(message, decrypted) + + def test_sign_verify(self): + + # TODO: test with unicode strings and non-ascii chars + message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + signed = rsa.sign(message, self.priv) + print "\tSigned: %s" % signed + + verified = rsa.verify(signed, self.pub) + print "\tVerified: %s" % verified + + self.assertEqual(message, verified) + -- cgit v1.2.3 From 62abca7fce2b3c4e9b16b43209371bf3c225fb6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 20 Jun 2011 01:06:39 +0200 Subject: Added block padding to be able to work with leading zeroes, breaks all kind of stuff --- tests/test_binary.py | 37 +++++++++++++++++++++++++++++++++++++ tests/test_strings.py | 7 +++---- 2 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 tests/test_binary.py (limited to 'tests') diff --git a/tests/test_binary.py b/tests/test_binary.py new file mode 100644 index 0000000..f770b72 --- /dev/null +++ b/tests/test_binary.py @@ -0,0 +1,37 @@ +'''Tests string operations.''' + +import struct +import unittest + +import rsa + +class BinaryTest(unittest.TestCase): + + def setUp(self): + (self.pub, self.priv) = rsa.newkeys(64) + + def test_enc_dec(self): + + message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' + print "\tMessage: %r" % message + + encrypted = rsa.encrypt(message, self.pub) + print "\tEncrypted: %r" % encrypted + + decrypted = rsa.decrypt(encrypted, self.priv) + print "\tDecrypted: %r" % decrypted + + self.assertEqual(message, decrypted) + + def test_sign_verify(self): + + message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' + print "\tMessage: %r" % message + + signed = rsa.sign(message, self.priv) + print "\tSigned: %r" % signed + + verified = rsa.verify(signed, self.pub) + print "\tVerified: %r" % verified + + self.assertEqual(message, verified) diff --git a/tests/test_strings.py b/tests/test_strings.py index c5803e4..8baa63d 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -11,8 +11,7 @@ class StringTest(unittest.TestCase): def test_enc_dec(self): - # TODO: test with unicode strings and non-ascii chars - message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + message = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ".encode('utf-8') print "\tMessage: %s" % message encrypted = rsa.encrypt(message, self.pub) @@ -25,8 +24,8 @@ class StringTest(unittest.TestCase): def test_sign_verify(self): - # TODO: test with unicode strings and non-ascii chars - message = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + message = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ".encode('utf-8') + print "\tMessage: %s" % message signed = rsa.sign(message, self.priv) print "\tSigned: %s" % signed -- cgit v1.2.3 From 61ba818dbdf61c3175d247d4b47802fd3ddfae4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 10 Jul 2011 12:17:16 +0200 Subject: Replaced the binary test with a test of the PKCS1 module. --- tests/test_binary.py | 37 ------------------------------------- tests/test_pkcs1.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 tests/test_binary.py create mode 100644 tests/test_pkcs1.py (limited to 'tests') diff --git a/tests/test_binary.py b/tests/test_binary.py deleted file mode 100644 index f770b72..0000000 --- a/tests/test_binary.py +++ /dev/null @@ -1,37 +0,0 @@ -'''Tests string operations.''' - -import struct -import unittest - -import rsa - -class BinaryTest(unittest.TestCase): - - def setUp(self): - (self.pub, self.priv) = rsa.newkeys(64) - - def test_enc_dec(self): - - message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' - print "\tMessage: %r" % message - - encrypted = rsa.encrypt(message, self.pub) - print "\tEncrypted: %r" % encrypted - - decrypted = rsa.decrypt(encrypted, self.priv) - print "\tDecrypted: %r" % decrypted - - self.assertEqual(message, decrypted) - - def test_sign_verify(self): - - message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' - print "\tMessage: %r" % message - - signed = rsa.sign(message, self.priv) - print "\tSigned: %r" % signed - - verified = rsa.verify(signed, self.pub) - print "\tVerified: %r" % verified - - self.assertEqual(message, verified) diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py new file mode 100644 index 0000000..9d3d9ec --- /dev/null +++ b/tests/test_pkcs1.py @@ -0,0 +1,38 @@ +'''Tests string operations.''' + +import struct +import unittest + +import rsa +from rsa import pkcs1 + +class BinaryTest(unittest.TestCase): + + def setUp(self): + (self.pub, self.priv) = rsa.newkeys(256) + + def test_enc_dec(self): + + message = struct.pack('>IIII', 0, 0, 0, 1) + 5 * '\x00' + print "\tMessage: %r" % message + + encrypted = pkcs1.encrypt(message, self.pub) + print "\tEncrypted: %r" % encrypted + + decrypted = pkcs1.decrypt(encrypted, self.priv) + print "\tDecrypted: %r" % decrypted + + self.assertEqual(message, decrypted) + +# def test_sign_verify(self): +# +# message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' +# print "\tMessage: %r" % message +# +# signed = rsa.sign(message, self.priv) +# print "\tSigned: %r" % signed +# +# verified = rsa.verify(signed, self.pub) +# print "\tVerified: %r" % verified +# +# self.assertEqual(message, verified) -- cgit v1.2.3 From fdde99452cd975c51a178feb3483db5794990336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 10 Jul 2011 12:40:46 +0200 Subject: Added unittest for PKCS#1 decoding failures --- tests/test_pkcs1.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 9d3d9ec..230e7f7 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -13,7 +13,7 @@ class BinaryTest(unittest.TestCase): def test_enc_dec(self): - message = struct.pack('>IIII', 0, 0, 0, 1) + 5 * '\x00' + message = struct.pack('>IIII', 0, 0, 0, 1) print "\tMessage: %r" % message encrypted = pkcs1.encrypt(message, self.pub) @@ -24,6 +24,16 @@ class BinaryTest(unittest.TestCase): self.assertEqual(message, decrypted) + def test_decoding_failure(self): + + message = struct.pack('>IIII', 0, 0, 0, 1) + encrypted = pkcs1.encrypt(message, self.pub) + + # Alter the encrypted stream + encrypted = encrypted[:5] + chr(ord(encrypted[5]) + 1) + encrypted[6:] + + self.assertRaises(ValueError, pkcs1.decrypt, encrypted, self.priv) + # def test_sign_verify(self): # # message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' -- cgit v1.2.3 From 5f6d9e2705f2dbe9744e3332d5a803c0fcdf9390 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 10 Jul 2011 13:23:34 +0200 Subject: Added test for randomness --- tests/test_pkcs1.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'tests') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 230e7f7..0d55d7b 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -34,6 +34,17 @@ class BinaryTest(unittest.TestCase): self.assertRaises(ValueError, pkcs1.decrypt, encrypted, self.priv) + def test_randomness(self): + '''Encrypting the same message twice should result in different + cryptos. + ''' + + message = struct.pack('>IIII', 0, 0, 0, 1) + encrypted1 = pkcs1.encrypt(message, self.pub) + encrypted2 = pkcs1.encrypt(message, self.pub) + + self.assertNotEqual(encrypted1, encrypted2) + # def test_sign_verify(self): # # message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' -- cgit v1.2.3 From b57368946487f495eb9442d208acf65a2baa2ac5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 10 Jul 2011 14:16:38 +0200 Subject: Added PKCS#1 signatures and verification of signatures --- tests/test_pkcs1.py | 58 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 0d55d7b..3392ed7 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -32,7 +32,8 @@ class BinaryTest(unittest.TestCase): # Alter the encrypted stream encrypted = encrypted[:5] + chr(ord(encrypted[5]) + 1) + encrypted[6:] - self.assertRaises(ValueError, pkcs1.decrypt, encrypted, self.priv) + self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted, + self.priv) def test_randomness(self): '''Encrypting the same message twice should result in different @@ -45,15 +46,46 @@ class BinaryTest(unittest.TestCase): self.assertNotEqual(encrypted1, encrypted2) -# def test_sign_verify(self): -# -# message = struct.pack('>IIII', 0, 0, 0, 1) + 20 * '\x00' -# print "\tMessage: %r" % message -# -# signed = rsa.sign(message, self.priv) -# print "\tSigned: %r" % signed -# -# verified = rsa.verify(signed, self.pub) -# print "\tVerified: %r" % verified -# -# self.assertEqual(message, verified) +class SignatureTest(unittest.TestCase): + + def setUp(self): + (self.pub, self.priv) = rsa.newkeys(512) + + def test_sign_verify(self): + '''Test happy flow of sign and verify''' + + message = 'je moeder' + print "\tMessage: %r" % message + + signature = pkcs1.sign(message, self.priv, 'SHA-256') + print "\tSignature: %r" % signature + + pkcs1.verify(message, signature, self.pub) + + def test_alter_message(self): + '''Altering the message should let the verification fail.''' + + signature = pkcs1.sign('je moeder', self.priv, 'SHA-256') + self.assertRaises(pkcs1.VerificationError, pkcs1.verify, + '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' + signature = pkcs1.sign(message, self.priv, 'SHA-256') + self.assertRaises(pkcs1.VerificationError, pkcs1.verify, + message, signature, otherpub) + + def test_multiple_signings(self): + '''Signing the same message twice should return the same signatures.''' + + message = struct.pack('>IIII', 0, 0, 0, 1) + signature1 = pkcs1.sign(message, self.priv, 'SHA-1') + signature2 = pkcs1.sign(message, self.priv, 'SHA-1') + + self.assertEqual(signature1, signature2) + + \ No newline at end of file -- cgit v1.2.3 From fa34679a8da7ef536772494be860e9ebeff2641e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Tue, 19 Jul 2011 23:53:59 +0200 Subject: Added loading of DER and PEM encoded private keys --- tests/test_load_save_keys.py | 47 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/test_load_save_keys.py (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py new file mode 100644 index 0000000..695de72 --- /dev/null +++ b/tests/test_load_save_keys.py @@ -0,0 +1,47 @@ +'''Unittest for saving and loading keys.''' + +import base64 +import unittest + +import rsa.key + +B64PRIV_DER = 'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt' +PRIVATE_DER = base64.decodestring(B64PRIV_DER) + +PRIVATE_PEM = ''' +-----BEGIN CONFUSING STUFF----- +Cruft before the key + +-----BEGIN RSA PRIVATE KEY----- +%s +-----END RSA PRIVATE KEY----- + +Stuff after the key +-----END CONFUSING STUFF----- +''' % B64PRIV_DER + + +class DerTest(unittest.TestCase): + '''Test saving and loading DER keys.''' + + def test_load_private_key(self): + '''Test loading private DER keys.''' + + key = rsa.key.load_private_key_der(PRIVATE_DER) + expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + + self.assertEqual(expected, key) + + +class PemTest(unittest.TestCase): + '''Test saving and loading PEM keys.''' + + + def test_load_private_key(self): + '''Test loading private PEM files.''' + + key = rsa.key.load_private_key_pem(PRIVATE_PEM) + expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + + self.assertEqual(expected, key) + -- cgit v1.2.3 From f6a107324a774aa933eb8e1a6c80b37bb64372b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 20 Jul 2011 01:11:34 +0200 Subject: Added support for saving private keys in DER and PEM format --- tests/test_load_save_keys.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 695de72..c13d059 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -20,6 +20,12 @@ Stuff after the key -----END CONFUSING STUFF----- ''' % B64PRIV_DER +CLEAN_PRIVATE_PEM = '''\ +-----BEGIN RSA PRIVATE KEY----- +%s +-----END RSA PRIVATE KEY----- +''' % B64PRIV_DER + class DerTest(unittest.TestCase): '''Test saving and loading DER keys.''' @@ -32,6 +38,13 @@ class DerTest(unittest.TestCase): self.assertEqual(expected, key) + def test_save_private_key(self): + '''Test saving private DER keys.''' + + key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + der = rsa.key.save_private_key_der(key) + + self.assertEqual(PRIVATE_DER, der) class PemTest(unittest.TestCase): '''Test saving and loading PEM keys.''' @@ -45,3 +58,11 @@ class PemTest(unittest.TestCase): self.assertEqual(expected, key) + def test_save_private_key(self): + '''Test saving private PEM files.''' + + key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + pem = rsa.key.save_private_key_pem(key) + + self.assertEqual(CLEAN_PRIVATE_PEM, pem) + -- cgit v1.2.3 From 049cc4acc00fd75bcbdce13760b72fcab9c4b009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 21 Jul 2011 22:16:56 +0200 Subject: Moved key load/save to PrivateKey class --- tests/test_load_save_keys.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index c13d059..5026afd 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -33,7 +33,7 @@ class DerTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private DER keys.''' - key = rsa.key.load_private_key_der(PRIVATE_DER) + key = rsa.key.PrivateKey.load_pkcs1_der(PRIVATE_DER) expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -42,7 +42,7 @@ class DerTest(unittest.TestCase): '''Test saving private DER keys.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - der = rsa.key.save_private_key_der(key) + der = key.save_pkcs1_der() self.assertEqual(PRIVATE_DER, der) @@ -53,7 +53,7 @@ class PemTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private PEM files.''' - key = rsa.key.load_private_key_pem(PRIVATE_PEM) + key = rsa.key.PrivateKey.load_pkcs1_pem(PRIVATE_PEM) expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -62,7 +62,7 @@ class PemTest(unittest.TestCase): '''Test saving private PEM files.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - pem = rsa.key.save_private_key_pem(key) + pem = key.save_pkcs1_pem() self.assertEqual(CLEAN_PRIVATE_PEM, pem) -- cgit v1.2.3 From 74023dc2979d27f24107fed64b0dedcd04974cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 24 Jul 2011 18:29:17 +0200 Subject: Added saving and loading public keys in PKCS#1 format (PEM+DER) --- tests/test_load_save_keys.py | 54 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 5026afd..fca4241 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -8,6 +8,9 @@ import rsa.key B64PRIV_DER = 'MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt' PRIVATE_DER = base64.decodestring(B64PRIV_DER) +B64PUB_DER = 'MAwCBQDeKYlRAgMBAAE=' +PUBLIC_DER = base64.decodestring(B64PUB_DER) + PRIVATE_PEM = ''' -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -26,6 +29,24 @@ CLEAN_PRIVATE_PEM = '''\ -----END RSA PRIVATE KEY----- ''' % B64PRIV_DER +PUBLIC_PEM = ''' +-----BEGIN CONFUSING STUFF----- +Cruft before the key + +-----BEGIN RSA PUBLIC KEY----- +%s +-----END RSA PUBLIC KEY----- + +Stuff after the key +-----END CONFUSING STUFF----- +''' % B64PUB_DER + +CLEAN_PUBLIC_PEM = '''\ +-----BEGIN RSA PUBLIC KEY----- +%s +-----END RSA PUBLIC KEY----- +''' % B64PUB_DER + class DerTest(unittest.TestCase): '''Test saving and loading DER keys.''' @@ -46,6 +67,22 @@ class DerTest(unittest.TestCase): self.assertEqual(PRIVATE_DER, der) + def test_load_public_key(self): + '''Test loading public DER keys.''' + + key = rsa.key.PublicKey.load_pkcs1_der(PUBLIC_DER) + expected = rsa.key.PublicKey(3727264081, 65537) + + self.assertEqual(expected, key) + + def test_save_public_key(self): + '''Test saving public DER keys.''' + + key = rsa.key.PublicKey(3727264081, 65537) + der = key.save_pkcs1_der() + + self.assertEqual(PUBLIC_DER, der) + class PemTest(unittest.TestCase): '''Test saving and loading PEM keys.''' @@ -66,3 +103,20 @@ class PemTest(unittest.TestCase): self.assertEqual(CLEAN_PRIVATE_PEM, pem) + def test_load_public_key(self): + '''Test loading public PEM files.''' + + key = rsa.key.PublicKey.load_pkcs1_pem(PUBLIC_PEM) + expected = rsa.key.PublicKey(3727264081, 65537) + + self.assertEqual(expected, key) + + def test_save_public_key(self): + '''Test saving public PEM files.''' + + key = rsa.key.PublicKey(3727264081, 65537) + pem = key.save_pkcs1_pem() + + self.assertEqual(CLEAN_PUBLIC_PEM, pem) + + -- cgit v1.2.3 From fd96fe5e00aa905a3402968954852396f0d12ed2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 24 Jul 2011 19:00:56 +0200 Subject: Added simpler save/load functions --- tests/test_load_save_keys.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index fca4241..abcfb18 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -54,7 +54,7 @@ class DerTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private DER keys.''' - key = rsa.key.PrivateKey.load_pkcs1_der(PRIVATE_DER) + key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'der') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -63,14 +63,14 @@ class DerTest(unittest.TestCase): '''Test saving private DER keys.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - der = key.save_pkcs1_der() + der = key.save_pkcs1('der') self.assertEqual(PRIVATE_DER, der) def test_load_public_key(self): '''Test loading public DER keys.''' - key = rsa.key.PublicKey.load_pkcs1_der(PUBLIC_DER) + key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'der') expected = rsa.key.PublicKey(3727264081, 65537) self.assertEqual(expected, key) @@ -79,7 +79,7 @@ class DerTest(unittest.TestCase): '''Test saving public DER keys.''' key = rsa.key.PublicKey(3727264081, 65537) - der = key.save_pkcs1_der() + der = key.save_pkcs1('der') self.assertEqual(PUBLIC_DER, der) @@ -90,7 +90,7 @@ class PemTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private PEM files.''' - key = rsa.key.PrivateKey.load_pkcs1_pem(PRIVATE_PEM) + key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'pem') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -99,14 +99,14 @@ class PemTest(unittest.TestCase): '''Test saving private PEM files.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - pem = key.save_pkcs1_pem() + pem = key.save_pkcs1('pem') self.assertEqual(CLEAN_PRIVATE_PEM, pem) def test_load_public_key(self): '''Test loading public PEM files.''' - key = rsa.key.PublicKey.load_pkcs1_pem(PUBLIC_PEM) + key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'pem') expected = rsa.key.PublicKey(3727264081, 65537) self.assertEqual(expected, key) @@ -115,7 +115,7 @@ class PemTest(unittest.TestCase): '''Test saving public PEM files.''' key = rsa.key.PublicKey(3727264081, 65537) - pem = key.save_pkcs1_pem() + pem = key.save_pkcs1('pem') self.assertEqual(CLEAN_PUBLIC_PEM, pem) -- cgit v1.2.3 From aa5a7fd9744746721ebb7c324867c829dea9f78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 24 Jul 2011 19:06:32 +0200 Subject: Specify format (PEM/DER) in capitals --- tests/test_load_save_keys.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index abcfb18..facb826 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -54,7 +54,7 @@ class DerTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private DER keys.''' - key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'der') + key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -63,14 +63,14 @@ class DerTest(unittest.TestCase): '''Test saving private DER keys.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - der = key.save_pkcs1('der') + der = key.save_pkcs1('DER') self.assertEqual(PRIVATE_DER, der) def test_load_public_key(self): '''Test loading public DER keys.''' - key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'der') + key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'DER') expected = rsa.key.PublicKey(3727264081, 65537) self.assertEqual(expected, key) @@ -79,7 +79,7 @@ class DerTest(unittest.TestCase): '''Test saving public DER keys.''' key = rsa.key.PublicKey(3727264081, 65537) - der = key.save_pkcs1('der') + der = key.save_pkcs1('DER') self.assertEqual(PUBLIC_DER, der) @@ -90,7 +90,7 @@ class PemTest(unittest.TestCase): def test_load_private_key(self): '''Test loading private PEM files.''' - key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'pem') + key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'PEM') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) self.assertEqual(expected, key) @@ -99,14 +99,14 @@ class PemTest(unittest.TestCase): '''Test saving private PEM files.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) - pem = key.save_pkcs1('pem') + pem = key.save_pkcs1('PEM') self.assertEqual(CLEAN_PRIVATE_PEM, pem) def test_load_public_key(self): '''Test loading public PEM files.''' - key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'pem') + key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'PEM') expected = rsa.key.PublicKey(3727264081, 65537) self.assertEqual(expected, key) @@ -115,7 +115,7 @@ class PemTest(unittest.TestCase): '''Test saving public PEM files.''' key = rsa.key.PublicKey(3727264081, 65537) - pem = key.save_pkcs1('pem') + pem = key.save_pkcs1('PEM') self.assertEqual(CLEAN_PUBLIC_PEM, pem) -- cgit v1.2.3 From 70bc1a5611a45388591f7c7f1eefdae080a29be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sat, 30 Jul 2011 20:13:09 +0200 Subject: Ignore fields in PEM-encoded files --- tests/test_load_save_keys.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index facb826..466a3f5 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -16,6 +16,8 @@ PRIVATE_PEM = ''' Cruft before the key -----BEGIN RSA PRIVATE KEY----- +Comment: something blah + %s -----END RSA PRIVATE KEY----- @@ -34,6 +36,8 @@ PUBLIC_PEM = ''' Cruft before the key -----BEGIN RSA PUBLIC KEY----- +Comment: something blah + %s -----END RSA PUBLIC KEY----- -- cgit v1.2.3 From 8c857f49f42e6ad169d79c2e889e7470a9fc90b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sat, 30 Jul 2011 20:39:02 +0200 Subject: Removed insecure code from rsa/__init__.py --- tests/test_integers.py | 10 +++++----- tests/test_strings.py | 15 +-------------- 2 files changed, 6 insertions(+), 19 deletions(-) (limited to 'tests') diff --git a/tests/test_integers.py b/tests/test_integers.py index 1e318ab..111f8ab 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -2,7 +2,7 @@ import unittest -import rsa +import rsa.core class IntegerTest(unittest.TestCase): @@ -14,10 +14,10 @@ class IntegerTest(unittest.TestCase): message = 42 print "\tMessage: %d" % message - encrypted = rsa.encrypt_int(message, self.pub['e'], self.pub['n']) + encrypted = rsa.core.encrypt_int(message, self.pub.e, self.pub.n) print "\tEncrypted: %d" % encrypted - decrypted = rsa.decrypt_int(encrypted, self.priv['d'], self.pub['n']) + decrypted = rsa.core.decrypt_int(encrypted, self.priv.d, self.pub.n) print "\tDecrypted: %d" % decrypted self.assertEqual(message, decrypted) @@ -26,10 +26,10 @@ class IntegerTest(unittest.TestCase): message = 42 - signed = rsa.encrypt_int(message,self.priv['d'], self.pub['n']) + signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n) print "\tSigned: %d" % signed - verified = rsa.decrypt_int(signed, self.pub['e'],self.pub['n']) + verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n) print "\tVerified: %d" % verified self.assertEqual(message, verified) diff --git a/tests/test_strings.py b/tests/test_strings.py index 8baa63d..38fae06 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -7,7 +7,7 @@ import rsa class StringTest(unittest.TestCase): def setUp(self): - (self.pub, self.priv) = rsa.newkeys(64) + (self.pub, self.priv) = rsa.newkeys(384) def test_enc_dec(self): @@ -22,16 +22,3 @@ class StringTest(unittest.TestCase): self.assertEqual(message, decrypted) - def test_sign_verify(self): - - message = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ".encode('utf-8') - print "\tMessage: %s" % message - - signed = rsa.sign(message, self.priv) - print "\tSigned: %s" % signed - - verified = rsa.verify(signed, self.pub) - print "\tVerified: %s" % verified - - self.assertEqual(message, verified) - -- cgit v1.2.3 From 006cbf132323c271d2ec3490c4001dcf2aa13721 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sat, 30 Jul 2011 21:39:10 +0200 Subject: Added start of blocks module (varint impl) --- tests/test_blocks.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tests/test_blocks.py (limited to 'tests') diff --git a/tests/test_blocks.py b/tests/test_blocks.py new file mode 100644 index 0000000..be7b640 --- /dev/null +++ b/tests/test_blocks.py @@ -0,0 +1,58 @@ +'''Tests block operations.''' + +from StringIO import StringIO +import unittest + +from rsa import blocks + +class VarintTest(unittest.TestCase): + + def test_read_varint(self): + + encoded = '\xac\x02crummy' + infile = StringIO(encoded) + + (decoded, read) = blocks.read_varint(infile) + + # Test the returned values + self.assertEqual(300, decoded) + self.assertEqual(2, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_read_zero(self): + + encoded = '\x00crummy' + infile = StringIO(encoded) + + (decoded, read) = blocks.read_varint(infile) + + # Test the returned values + self.assertEqual(0, decoded) + self.assertEqual(1, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_write_varint(self): + + expected = '\xac\x02' + outfile = StringIO() + + written = blocks.write_varint(outfile, 300) + + # Test the returned values + self.assertEqual(expected, outfile.getvalue()) + self.assertEqual(2, written) + + + def test_write_zero(self): + + outfile = StringIO() + written = blocks.write_varint(outfile, 0) + + # Test the returned values + self.assertEqual('\x00', outfile.getvalue()) + self.assertEqual(1, written) + -- cgit v1.2.3 From 5524a39f4114430ac4dee2f81fc436ac3b3a815b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sat, 30 Jul 2011 21:58:13 +0200 Subject: Added reading of varblocks --- tests/test_blocks.py | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/test_blocks.py b/tests/test_blocks.py index be7b640..8481c3c 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -56,3 +56,13 @@ class VarintTest(unittest.TestCase): self.assertEqual('\x00', outfile.getvalue()) self.assertEqual(1, written) + +class VarblockTest(unittest.TestCase): + + def test_yield_varblock(self): + infile = StringIO('\x01\x0512345\x06Sybren') + + varblocks = list(blocks.yield_varblocks(infile)) + self.assertEqual(['12345', 'Sybren'], varblocks) + + -- cgit v1.2.3 From e8988522041ce8b02f6c6cf99e5dae4bca194957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 31 Jul 2011 19:54:53 +0200 Subject: Added yield_fixedblock --- tests/test_blocks.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests') diff --git a/tests/test_blocks.py b/tests/test_blocks.py index 8481c3c..ce5f03a 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -65,4 +65,11 @@ class VarblockTest(unittest.TestCase): varblocks = list(blocks.yield_varblocks(infile)) self.assertEqual(['12345', 'Sybren'], varblocks) +class FixedblockTest(unittest.TestCase): + def test_yield_fixedblock(self): + + infile = StringIO('123456Sybren') + + fixedblocks = list(blocks.yield_fixedblocks(infile, 6)) + self.assertEqual(['123456', 'Sybren'], fixedblocks) -- cgit v1.2.3 From fa345b65cf1951d8177fa9f4f90c06e1249e5f6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 31 Jul 2011 20:25:40 +0200 Subject: Added encrypting and decrypting of large files --- tests/test_blocks.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'tests') diff --git a/tests/test_blocks.py b/tests/test_blocks.py index ce5f03a..22d6500 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -3,6 +3,7 @@ from StringIO import StringIO import unittest +import rsa from rsa import blocks class VarintTest(unittest.TestCase): @@ -73,3 +74,33 @@ class FixedblockTest(unittest.TestCase): fixedblocks = list(blocks.yield_fixedblocks(infile, 6)) self.assertEqual(['123456', 'Sybren'], fixedblocks) + +class BigfileTest(unittest.TestCase): + + def test_encrypt_decrypt_bigfile(self): + + # Expected block size + 11 bytes padding + pub_key, priv_key = rsa.newkeys((6 + 11) * 8) + + # Encrypt the file + message = '123456Sybren' + infile = StringIO(message) + outfile = StringIO() + + blocks.encrypt_bigfile(infile, outfile, pub_key) + + # Test + crypto = outfile.getvalue() + + cryptfile = StringIO(crypto) + clearfile = StringIO() + + blocks.decrypt_bigfile(cryptfile, clearfile, priv_key) + self.assertEquals(clearfile.getvalue(), message) + + # We have 2x6 bytes in the message, so that should result in two + # blocks. + cryptfile.seek(0) + varblocks = list(blocks.yield_varblocks(cryptfile)) + self.assertEqual(2, len(varblocks)) + -- cgit v1.2.3 From 3200f895745919a6074505dd40c8d0f88b01836a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 31 Jul 2011 20:47:49 +0200 Subject: Made hashing efficient for large files --- tests/test_bigfile.py | 37 +++++++++++++++++ tests/test_blocks.py | 106 ------------------------------------------------- tests/test_varblock.py | 77 +++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 106 deletions(-) create mode 100644 tests/test_bigfile.py delete mode 100644 tests/test_blocks.py create mode 100644 tests/test_varblock.py (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py new file mode 100644 index 0000000..ffca5b0 --- /dev/null +++ b/tests/test_bigfile.py @@ -0,0 +1,37 @@ +'''Tests block operations.''' + +from StringIO import StringIO +import unittest + +import rsa +from rsa import bigfile, varblock + +class BigfileTest(unittest.TestCase): + + def test_encrypt_decrypt_bigfile(self): + + # Expected block size + 11 bytes padding + pub_key, priv_key = rsa.newkeys((6 + 11) * 8) + + # Encrypt the file + message = '123456Sybren' + infile = StringIO(message) + outfile = StringIO() + + bigfile.encrypt_bigfile(infile, outfile, pub_key) + + # Test + crypto = outfile.getvalue() + + cryptfile = StringIO(crypto) + clearfile = StringIO() + + bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key) + self.assertEquals(clearfile.getvalue(), message) + + # We have 2x6 bytes in the message, so that should result in two + # bigfile. + cryptfile.seek(0) + varblocks = list(varblock.yield_varblocks(cryptfile)) + self.assertEqual(2, len(varblocks)) + diff --git a/tests/test_blocks.py b/tests/test_blocks.py deleted file mode 100644 index 22d6500..0000000 --- a/tests/test_blocks.py +++ /dev/null @@ -1,106 +0,0 @@ -'''Tests block operations.''' - -from StringIO import StringIO -import unittest - -import rsa -from rsa import blocks - -class VarintTest(unittest.TestCase): - - def test_read_varint(self): - - encoded = '\xac\x02crummy' - infile = StringIO(encoded) - - (decoded, read) = blocks.read_varint(infile) - - # Test the returned values - self.assertEqual(300, decoded) - self.assertEqual(2, read) - - # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) - - def test_read_zero(self): - - encoded = '\x00crummy' - infile = StringIO(encoded) - - (decoded, read) = blocks.read_varint(infile) - - # Test the returned values - self.assertEqual(0, decoded) - self.assertEqual(1, read) - - # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) - - def test_write_varint(self): - - expected = '\xac\x02' - outfile = StringIO() - - written = blocks.write_varint(outfile, 300) - - # Test the returned values - self.assertEqual(expected, outfile.getvalue()) - self.assertEqual(2, written) - - - def test_write_zero(self): - - outfile = StringIO() - written = blocks.write_varint(outfile, 0) - - # Test the returned values - self.assertEqual('\x00', outfile.getvalue()) - self.assertEqual(1, written) - - -class VarblockTest(unittest.TestCase): - - def test_yield_varblock(self): - infile = StringIO('\x01\x0512345\x06Sybren') - - varblocks = list(blocks.yield_varblocks(infile)) - self.assertEqual(['12345', 'Sybren'], varblocks) - -class FixedblockTest(unittest.TestCase): - - def test_yield_fixedblock(self): - - infile = StringIO('123456Sybren') - - fixedblocks = list(blocks.yield_fixedblocks(infile, 6)) - self.assertEqual(['123456', 'Sybren'], fixedblocks) - -class BigfileTest(unittest.TestCase): - - def test_encrypt_decrypt_bigfile(self): - - # Expected block size + 11 bytes padding - pub_key, priv_key = rsa.newkeys((6 + 11) * 8) - - # Encrypt the file - message = '123456Sybren' - infile = StringIO(message) - outfile = StringIO() - - blocks.encrypt_bigfile(infile, outfile, pub_key) - - # Test - crypto = outfile.getvalue() - - cryptfile = StringIO(crypto) - clearfile = StringIO() - - blocks.decrypt_bigfile(cryptfile, clearfile, priv_key) - self.assertEquals(clearfile.getvalue(), message) - - # We have 2x6 bytes in the message, so that should result in two - # blocks. - cryptfile.seek(0) - varblocks = list(blocks.yield_varblocks(cryptfile)) - self.assertEqual(2, len(varblocks)) - diff --git a/tests/test_varblock.py b/tests/test_varblock.py new file mode 100644 index 0000000..d8addb4 --- /dev/null +++ b/tests/test_varblock.py @@ -0,0 +1,77 @@ +'''Tests varblock operations.''' + +from StringIO import StringIO +import unittest + +import rsa +from rsa import varblock + +class VarintTest(unittest.TestCase): + + def test_read_varint(self): + + encoded = '\xac\x02crummy' + infile = StringIO(encoded) + + (decoded, read) = varblock.read_varint(infile) + + # Test the returned values + self.assertEqual(300, decoded) + self.assertEqual(2, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_read_zero(self): + + encoded = '\x00crummy' + infile = StringIO(encoded) + + (decoded, read) = varblock.read_varint(infile) + + # Test the returned values + self.assertEqual(0, decoded) + self.assertEqual(1, read) + + # The rest of the file should be untouched + self.assertEqual('crummy', infile.read()) + + def test_write_varint(self): + + expected = '\xac\x02' + outfile = StringIO() + + written = varblock.write_varint(outfile, 300) + + # Test the returned values + self.assertEqual(expected, outfile.getvalue()) + self.assertEqual(2, written) + + + def test_write_zero(self): + + outfile = StringIO() + written = varblock.write_varint(outfile, 0) + + # Test the returned values + self.assertEqual('\x00', outfile.getvalue()) + self.assertEqual(1, written) + + +class VarblockTest(unittest.TestCase): + + def test_yield_varblock(self): + infile = StringIO('\x01\x0512345\x06Sybren') + + varblocks = list(varblock.yield_varblocks(infile)) + self.assertEqual(['12345', 'Sybren'], varblocks) + +class FixedblockTest(unittest.TestCase): + + def test_yield_fixedblock(self): + + infile = StringIO('123456Sybren') + + fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) + self.assertEqual(['123456', 'Sybren'], fixedblocks) + -- cgit v1.2.3 From 61becf6573aee105baf866b2541cf10dffc3c820 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 31 Jul 2011 20:56:13 +0200 Subject: Added unittest for large file signing/verifying --- tests/test_bigfile.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index ffca5b0..30156a1 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -4,7 +4,7 @@ from StringIO import StringIO import unittest import rsa -from rsa import bigfile, varblock +from rsa import bigfile, varblock, pkcs1 class BigfileTest(unittest.TestCase): @@ -35,3 +35,22 @@ class BigfileTest(unittest.TestCase): varblocks = list(varblock.yield_varblocks(cryptfile)) self.assertEqual(2, len(varblocks)) + + def test_sign_verify_bigfile(self): + + # Large enough to store MD5-sum and ASN.1 code for MD5 + pub_key, priv_key = rsa.newkeys((34 + 11) * 8) + + # Sign the file + msgfile = StringIO('123456Sybren') + signature = pkcs1.sign(msgfile, priv_key, 'MD5') + + # Check the signature + msgfile.seek(0) + pkcs1.verify(msgfile, signature, pub_key) + + # Alter the message, re-check + msgfile = StringIO('123456sybren') + self.assertRaises(pkcs1.VerificationError, + pkcs1.verify, msgfile, signature, pub_key) + -- cgit v1.2.3 From fd1ec36f68eb15f86b3afcad8371ff77b653363a Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 01:47:46 +0530 Subject: Compatibility wrapper module to make rsa work with various versions of Python. --- tests/test_compat.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/test_compat.py (limited to 'tests') diff --git a/tests/test_compat.py b/tests/test_compat.py new file mode 100644 index 0000000..3652c82 --- /dev/null +++ b/tests/test_compat.py @@ -0,0 +1,17 @@ +# -*- coding: utf-8 -*- + +import unittest2 +import struct + +from rsa._compat import is_bytes, byte + +class Test_byte(unittest2.TestCase): + def test_byte(self): + for i in range(256): + byt = byte(i) + self.assertTrue(is_bytes(byt)) + self.assertEqual(ord(byt), i) + + def test_raises_StructError_on_overflow(self): + self.assertRaises(struct.error, byte, 256) + self.assertRaises(struct.error, byte, -1) -- cgit v1.2.3 From 5802431960ed2904db5f8cbcb2e318e6f2d4c792 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. --- 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 ++++- 10 files changed, 52 insertions(+), 27 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 (limited to 'tests') 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.3 From aed6e8657bc316ccd6964a33c04bd650e9951ae2 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 01:59:50 +0530 Subject: Update tests to use unittest2. --- tests/test_bigfile.py | 4 ++-- tests/test_integers.py | 4 ++-- tests/test_load_save_keys.py | 6 +++--- tests/test_pkcs1.py | 6 +++--- tests/test_strings.py | 4 ++-- tests/test_varblock.py | 34 ++++++++++++++++++---------------- 6 files changed, 30 insertions(+), 28 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 39bd095..02e052e 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -4,12 +4,12 @@ try: from StringIO import StringIO except ImportError: from io import StringIO -import unittest +import unittest2 import rsa from rsa import bigfile, varblock, pkcs1 -class BigfileTest(unittest.TestCase): +class BigfileTest(unittest2.TestCase): def test_encrypt_decrypt_bigfile(self): diff --git a/tests/test_integers.py b/tests/test_integers.py index d4fa087..0a712aa 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -1,10 +1,10 @@ '''Tests integer operations.''' -import unittest +import unittest2 import rsa.core -class IntegerTest(unittest.TestCase): +class IntegerTest(unittest2.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(64) diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 56d45c4..fabe92f 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -1,7 +1,7 @@ '''Unittest for saving and loading keys.''' import base64 -import unittest +import unittest2 from rsa._compat import b import rsa.key @@ -53,7 +53,7 @@ CLEAN_PUBLIC_PEM = b('''\ ''' % B64PUB_DER) -class DerTest(unittest.TestCase): +class DerTest(unittest2.TestCase): '''Test saving and loading DER keys.''' def test_load_private_key(self): @@ -88,7 +88,7 @@ class DerTest(unittest.TestCase): self.assertEqual(PUBLIC_DER, der) -class PemTest(unittest.TestCase): +class PemTest(unittest2.TestCase): '''Test saving and loading PEM keys.''' diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index c841485..d8fb1b4 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -1,12 +1,12 @@ '''Tests string operations.''' import struct -import unittest +import unittest2 import rsa from rsa import pkcs1 -class BinaryTest(unittest.TestCase): +class BinaryTest(unittest2.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(256) @@ -46,7 +46,7 @@ class BinaryTest(unittest.TestCase): self.assertNotEqual(encrypted1, encrypted2) -class SignatureTest(unittest.TestCase): +class SignatureTest(unittest2.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(512) diff --git a/tests/test_strings.py b/tests/test_strings.py index 58d3833..001456d 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -2,13 +2,13 @@ from __future__ import absolute_import -import unittest +import unittest2 import rsa from tests.constants import unicode_string -class StringTest(unittest.TestCase): +class StringTest(unittest2.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(384) diff --git a/tests/test_varblock.py b/tests/test_varblock.py index 6195258..24ea50f 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,20 +1,22 @@ '''Tests varblock operations.''' + try: - from StringIO import StringIO + from StringIO import StringIO as BytesIO except ImportError: - from io import StringIO + from io import BytesIO import unittest import rsa +from rsa._compat import b from rsa import varblock class VarintTest(unittest.TestCase): def test_read_varint(self): - encoded = '\xac\x02crummy' - infile = StringIO(encoded) + encoded = b('\xac\x02crummy') + infile = BytesIO(encoded) (decoded, read) = varblock.read_varint(infile) @@ -23,12 +25,12 @@ class VarintTest(unittest.TestCase): self.assertEqual(2, read) # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) + self.assertEqual(b('crummy'), infile.read()) def test_read_zero(self): - encoded = '\x00crummy' - infile = StringIO(encoded) + encoded = b('\x00crummy') + infile = BytesIO(encoded) (decoded, read) = varblock.read_varint(infile) @@ -37,12 +39,12 @@ class VarintTest(unittest.TestCase): self.assertEqual(1, read) # The rest of the file should be untouched - self.assertEqual('crummy', infile.read()) + self.assertEqual(b('crummy'), infile.read()) def test_write_varint(self): - expected = '\xac\x02' - outfile = StringIO() + expected = b('\xac\x02') + outfile = BytesIO() written = varblock.write_varint(outfile, 300) @@ -53,28 +55,28 @@ class VarintTest(unittest.TestCase): def test_write_zero(self): - outfile = StringIO() + outfile = BytesIO() written = varblock.write_varint(outfile, 0) # Test the returned values - self.assertEqual('\x00', outfile.getvalue()) + self.assertEqual(b('\x00'), outfile.getvalue()) self.assertEqual(1, written) class VarblockTest(unittest.TestCase): def test_yield_varblock(self): - infile = StringIO('\x01\x0512345\x06Sybren') + infile = BytesIO(b('\x01\x0512345\x06Sybren')) varblocks = list(varblock.yield_varblocks(infile)) - self.assertEqual(['12345', 'Sybren'], varblocks) + self.assertEqual([b('12345'), b('Sybren')], varblocks) class FixedblockTest(unittest.TestCase): def test_yield_fixedblock(self): - infile = StringIO('123456Sybren') + infile = BytesIO(b('123456Sybren')) fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) - self.assertEqual(['123456', 'Sybren'], fixedblocks) + self.assertEqual([b('123456'), b('Sybren')], fixedblocks) -- cgit v1.2.3 From e97d0d730367ad3b49efc712c01509500939c904 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.3 From ce06c91a7be08d6effed185c54bd831f3562e008 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 02:49:34 +0530 Subject: Removes unnecessary code that I had added. --- tests/test_pkcs1.py | 6 ------ 1 file changed, 6 deletions(-) (limited to 'tests') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 82a3775..55098e2 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -30,12 +30,6 @@ 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 a = encrypted[5] if is_bytes(a): -- cgit v1.2.3 From c19a21bac0fef5691143959d799e5490537fe312 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 04:48:11 +0530 Subject: Adds tests for int2bytes. --- tests/test_transform.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/test_transform.py (limited to 'tests') diff --git a/tests/test_transform.py b/tests/test_transform.py new file mode 100644 index 0000000..ecc1a30 --- /dev/null +++ b/tests/test_transform.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- + + +import unittest2 +from rsa._compat import b +from rsa.transform import int2bytes + + +class Test_integer_to_bytes(unittest2.TestCase): + def test_chunk_size(self): + self.assertEqual(int2bytes(123456789, 6), + b('\x00\x00\x07[\xcd\x15')) + self.assertEqual(int2bytes(123456789, 7), + b('\x00\x00\x00\x07[\xcd\x15')) + + def test_raises_OverflowError_when_chunk_size_is_insufficient(self): + self.assertRaises(OverflowError, int2bytes, 123456789, 3) + self.assertRaises(OverflowError, int2bytes, 299999999999, 4) + + def test_raises_ValueError_when_negative_integer(self): + self.assertRaises(ValueError, int2bytes, -1) + + def test_raises_TypeError_when_not_integer(self): + self.assertRaises(TypeError, int2bytes, None) -- cgit v1.2.3 From a4453c6f8b1f919794e7f3213292d03416eb6d91 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Thu, 11 Aug 2011 12:03:37 +0530 Subject: Adds speed tests for int2bytes and old_int2bytes. * In the following tests, the first speed test for each version of Python checked is the new implementation and the second is the old implementation. $ ./speed.sh int2bytes speed test python2.5 1000 loops, best of 3: 315 usec per loop 100 loops, best of 3: 4.87 msec per loop python2.6 10000 loops, best of 3: 170 usec per loop 100 loops, best of 3: 3.34 msec per loop python2.7 10000 loops, best of 3: 169 usec per loop 100 loops, best of 3: 2.8 msec per loop python3.2 10000 loops, best of 3: 169 usec per loop 100 loops, best of 3: 3.16 msec per loop --- tests/test_transform.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_transform.py b/tests/test_transform.py index ecc1a30..9bd3c6d 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -3,7 +3,7 @@ import unittest2 from rsa._compat import b -from rsa.transform import int2bytes +from rsa.transform import int2bytes, old_int2bytes class Test_integer_to_bytes(unittest2.TestCase): @@ -12,13 +12,21 @@ class Test_integer_to_bytes(unittest2.TestCase): b('\x00\x00\x07[\xcd\x15')) self.assertEqual(int2bytes(123456789, 7), b('\x00\x00\x00\x07[\xcd\x15')) + self.assertEqual(old_int2bytes(123456789, 6), + b('\x00\x00\x07[\xcd\x15')) + self.assertEqual(old_int2bytes(123456789, 7), + b('\x00\x00\x00\x07[\xcd\x15')) def test_raises_OverflowError_when_chunk_size_is_insufficient(self): self.assertRaises(OverflowError, int2bytes, 123456789, 3) self.assertRaises(OverflowError, int2bytes, 299999999999, 4) + self.assertRaises(OverflowError, old_int2bytes, 123456789, 3) + self.assertRaises(OverflowError, old_int2bytes, 299999999999, 4) def test_raises_ValueError_when_negative_integer(self): self.assertRaises(ValueError, int2bytes, -1) + self.assertRaises(ValueError, old_int2bytes, -1) def test_raises_TypeError_when_not_integer(self): self.assertRaises(TypeError, int2bytes, None) + self.assertRaises(TypeError, old_int2bytes, None) -- cgit v1.2.3 From 81325245001d80d99cb59275dfe91833f4217320 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Fri, 12 Aug 2011 13:06:51 +0530 Subject: Adds verification tests for int2bytes and bytes2int * There is a bug in the older int2bytes implementation. I've raised an issue on bitbucket for that already. #11 The pkcs1 file verification test fails if the behavior for int2bytes is corrected. --- tests/test_transform.py | 55 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/test_transform.py b/tests/test_transform.py index 9bd3c6d..ffd9ec8 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -3,30 +3,65 @@ import unittest2 from rsa._compat import b -from rsa.transform import int2bytes, old_int2bytes +from rsa.transform import int2bytes, bytes2int, _int2bytes -class Test_integer_to_bytes(unittest2.TestCase): +class Test_int2bytes(unittest2.TestCase): + def test_accuracy(self): + self.assertEqual(int2bytes(123456789), b('\x07[\xcd\x15')) + self.assertEqual(_int2bytes(123456789), b('\x07[\xcd\x15')) + + def test_codec_identity(self): + self.assertEqual(bytes2int(int2bytes(123456789, 128)), 123456789) + self.assertEqual(bytes2int(_int2bytes(123456789, 128)), 123456789) + def test_chunk_size(self): - self.assertEqual(int2bytes(123456789, 6), - b('\x00\x00\x07[\xcd\x15')) + self.assertEqual(int2bytes(123456789, 6), b('\x00\x00\x07[\xcd\x15')) self.assertEqual(int2bytes(123456789, 7), b('\x00\x00\x00\x07[\xcd\x15')) - self.assertEqual(old_int2bytes(123456789, 6), + + self.assertEqual(_int2bytes(123456789, 6), b('\x00\x00\x07[\xcd\x15')) - self.assertEqual(old_int2bytes(123456789, 7), + self.assertEqual(_int2bytes(123456789, 7), b('\x00\x00\x00\x07[\xcd\x15')) + def test_zero(self): + self.assertEqual(int2bytes(0, 4), b('\x00') * 4) + self.assertEqual(int2bytes(0, 7), b('\x00') * 7) + self.assertEqual(int2bytes(0), b('\x00')) + + self.assertEqual(_int2bytes(0, 4), b('\x00') * 4) + self.assertEqual(_int2bytes(0, 7), b('\x00') * 7) + self.assertEqual(_int2bytes(0), b('\x00')) + + def test_correctness_against_base_implementation(self): + # Slow test. + values = [ + 1 << 512, + 1 << 8192, + 1 << 77, + ] + for value in values: + self.assertEqual(int2bytes(value), _int2bytes(value), + "Boom %d" % value) + self.assertEqual(bytes2int(int2bytes(value)), + value, + "Boom %d" % value) + self.assertEqual(bytes2int(_int2bytes(value)), + value, + "Boom %d" % value) + def test_raises_OverflowError_when_chunk_size_is_insufficient(self): self.assertRaises(OverflowError, int2bytes, 123456789, 3) self.assertRaises(OverflowError, int2bytes, 299999999999, 4) - self.assertRaises(OverflowError, old_int2bytes, 123456789, 3) - self.assertRaises(OverflowError, old_int2bytes, 299999999999, 4) + + self.assertRaises(OverflowError, _int2bytes, 123456789, 3) + self.assertRaises(OverflowError, _int2bytes, 299999999999, 4) def test_raises_ValueError_when_negative_integer(self): self.assertRaises(ValueError, int2bytes, -1) - self.assertRaises(ValueError, old_int2bytes, -1) + self.assertRaises(ValueError, _int2bytes, -1) def test_raises_TypeError_when_not_integer(self): self.assertRaises(TypeError, int2bytes, None) - self.assertRaises(TypeError, old_int2bytes, None) + self.assertRaises(TypeError, _int2bytes, None) -- cgit v1.2.3 From 03c51e75de8f9969f3fd5f3885a33ef04ce7348a Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Tue, 16 Aug 2011 14:30:48 +0530 Subject: Parellelized testing. Caught a lot of bugs. --- tests/test_bigfile.py | 2 +- tests/test_common.py | 50 ++++++++++++++++++++++++++++++++++++++++++++ tests/test_integers.py | 2 +- tests/test_load_save_keys.py | 38 ++++++++++++++++----------------- tests/test_pkcs1.py | 14 ++++++------- tests/test_strings.py | 2 +- tests/test_varblock.py | 2 +- 7 files changed, 80 insertions(+), 30 deletions(-) create mode 100644 tests/test_common.py (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 974da8b..9e3a864 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,4 +1,4 @@ -'''Tests block operations.''' +"""Tests block operations.""" from rsa._compat import b try: diff --git a/tests/test_common.py b/tests/test_common.py new file mode 100644 index 0000000..bad24f8 --- /dev/null +++ b/tests/test_common.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest2 +import struct +from rsa._compat import byte, b +from rsa.common import byte_size, bit_size, _bit_size + + +class Test_byte(unittest2.TestCase): + def test_values(self): + self.assertEqual(byte(0), b('\x00')) + self.assertEqual(byte(255), b('\xff')) + + def test_struct_error_when_out_of_bounds(self): + self.assertRaises(struct.error, byte, 256) + self.assertRaises(struct.error, byte, -1) + + +class Test_byte_size(unittest2.TestCase): + def test_values(self): + self.assertEqual(byte_size(1 << 1023), 128) + self.assertEqual(byte_size((1 << 1024) - 1), 128) + self.assertEqual(byte_size(1 << 1024), 129) + + def test_zero(self): + self.assertEqual(byte_size(0), 0) + + def test_bad_type(self): + self.assertRaises(TypeError, byte_size, []) + self.assertRaises(TypeError, byte_size, ()) + self.assertRaises(TypeError, byte_size, dict()) + self.assertRaises(TypeError, byte_size, "") + self.assertRaises(TypeError, byte_size, None) + +class Test_bit_size(unittest2.TestCase): + def test_values(self): + self.assertEqual(bit_size(1023), 10) + self.assertEqual(bit_size(1024), 11) + self.assertEqual(bit_size(1025), 11) + self.assertEqual(bit_size(1 << 1024), 1025) + self.assertEqual(bit_size((1 << 1024) + 1), 1025) + self.assertEqual(bit_size((1 << 1024) - 1), 1024) + + self.assertEqual(_bit_size(1023), 10) + self.assertEqual(_bit_size(1024), 11) + self.assertEqual(_bit_size(1025), 11) + self.assertEqual(_bit_size(1 << 1024), 1025) + self.assertEqual(_bit_size((1 << 1024) + 1), 1025) + self.assertEqual(_bit_size((1 << 1024) - 1), 1024) diff --git a/tests/test_integers.py b/tests/test_integers.py index 0a712aa..c73be2c 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -1,4 +1,4 @@ -'''Tests integer operations.''' +"""Tests integer operations.""" import unittest2 diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index fc1a1aa..30bbb21 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -1,4 +1,4 @@ -'''Unittest for saving and loading keys.''' +"""Unittest for saving and loading keys.""" import base64 import unittest2 @@ -12,7 +12,7 @@ PRIVATE_DER = base64.decodestring(B64PRIV_DER) B64PUB_DER = b('MAwCBQDeKYlRAgMBAAE=') PUBLIC_DER = base64.decodestring(B64PUB_DER) -PRIVATE_PEM = b(''' +PRIVATE_PEM = b(""" -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -24,15 +24,15 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PRIV_DER.decode("utf-8")) +""" % B64PRIV_DER.decode("utf-8")) -CLEAN_PRIVATE_PEM = b('''\ +CLEAN_PRIVATE_PEM = b("""\ -----BEGIN RSA PRIVATE KEY----- %s -----END RSA PRIVATE KEY----- -''' % B64PRIV_DER.decode("utf-8")) +""" % B64PRIV_DER.decode("utf-8")) -PUBLIC_PEM = b(''' +PUBLIC_PEM = b(""" -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -44,20 +44,20 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -''' % B64PUB_DER.decode("utf-8")) +""" % B64PUB_DER.decode("utf-8")) -CLEAN_PUBLIC_PEM = b('''\ +CLEAN_PUBLIC_PEM = b("""\ -----BEGIN RSA PUBLIC KEY----- %s -----END RSA PUBLIC KEY----- -''' % B64PUB_DER.decode("utf-8")) +""" % B64PUB_DER.decode("utf-8")) class DerTest(unittest2.TestCase): - '''Test saving and loading DER keys.''' + """Test saving and loading DER keys.""" def test_load_private_key(self): - '''Test loading private DER keys.''' + """Test loading private DER keys.""" key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -65,7 +65,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - '''Test saving private DER keys.''' + """Test saving private DER keys.""" key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) der = key.save_pkcs1('DER') @@ -73,7 +73,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(PRIVATE_DER, der) def test_load_public_key(self): - '''Test loading public DER keys.''' + """Test loading public DER keys.""" key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'DER') expected = rsa.key.PublicKey(3727264081, 65537) @@ -81,7 +81,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - '''Test saving public DER keys.''' + """Test saving public DER keys.""" key = rsa.key.PublicKey(3727264081, 65537) der = key.save_pkcs1('DER') @@ -89,11 +89,11 @@ class DerTest(unittest2.TestCase): self.assertEqual(PUBLIC_DER, der) class PemTest(unittest2.TestCase): - '''Test saving and loading PEM keys.''' + """Test saving and loading PEM keys.""" def test_load_private_key(self): - '''Test loading private PEM files.''' + """Test loading private PEM files.""" key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'PEM') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -101,7 +101,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - '''Test saving private PEM files.''' + """Test saving private PEM files.""" key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) pem = key.save_pkcs1('PEM') @@ -109,7 +109,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(CLEAN_PRIVATE_PEM, pem) def test_load_public_key(self): - '''Test loading public PEM files.''' + """Test loading public PEM files.""" key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'PEM') expected = rsa.key.PublicKey(3727264081, 65537) @@ -117,7 +117,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - '''Test saving public PEM files.''' + """Test saving public PEM files.""" key = rsa.key.PublicKey(3727264081, 65537) pem = key.save_pkcs1('PEM') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 55098e2..5d2fbad 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -1,4 +1,4 @@ -'''Tests string operations.''' +"""Tests string operations.""" import struct import unittest2 @@ -40,9 +40,9 @@ class BinaryTest(unittest2.TestCase): self.priv) def test_randomness(self): - '''Encrypting the same message twice should result in different + """Encrypting the same message twice should result in different cryptos. - ''' + """ message = struct.pack('>IIII', 0, 0, 0, 1) encrypted1 = pkcs1.encrypt(message, self.pub) @@ -56,7 +56,7 @@ class SignatureTest(unittest2.TestCase): (self.pub, self.priv) = rsa.newkeys(512) def test_sign_verify(self): - '''Test happy flow of sign and verify''' + """Test happy flow of sign and verify""" message = b('je moeder') print("\tMessage: %r" % message) @@ -67,14 +67,14 @@ class SignatureTest(unittest2.TestCase): pkcs1.verify(message, signature, self.pub) def test_alter_message(self): - '''Altering the message should let the verification fail.''' + """Altering the message should let the verification fail.""" signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, b('mijn moeder'), signature, self.pub) def test_sign_different_key(self): - '''Signing with another key should let the verification fail.''' + """Signing with another key should let the verification fail.""" (otherpub, _) = rsa.newkeys(512) @@ -84,7 +84,7 @@ class SignatureTest(unittest2.TestCase): message, signature, otherpub) def test_multiple_signings(self): - '''Signing the same message twice should return the same signatures.''' + """Signing the same message twice should return the same signatures.""" message = struct.pack('>IIII', 0, 0, 0, 1) signature1 = pkcs1.sign(message, self.priv, 'SHA-1') diff --git a/tests/test_strings.py b/tests/test_strings.py index 001456d..a2f9483 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,4 +1,4 @@ -'''Tests string operations.''' +"""Tests string operations.""" from __future__ import absolute_import diff --git a/tests/test_varblock.py b/tests/test_varblock.py index 24ea50f..d6465ed 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,4 +1,4 @@ -'''Tests varblock operations.''' +"""Tests varblock operations.""" try: -- cgit v1.2.3 From d1060d36697efcd1179ffdbc7b16d3a68643b8c5 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Tue, 16 Aug 2011 14:45:38 +0530 Subject: Adds common includes and tests. --- tests/test_common.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'tests') diff --git a/tests/test_common.py b/tests/test_common.py index bad24f8..9980b15 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -34,6 +34,9 @@ class Test_byte_size(unittest2.TestCase): self.assertRaises(TypeError, byte_size, None) class Test_bit_size(unittest2.TestCase): + def test_zero(self): + self.assertEqual(bit_size(0), 0) + def test_values(self): self.assertEqual(bit_size(1023), 10) self.assertEqual(bit_size(1024), 11) -- cgit v1.2.3 From 5bfe5ffea25ee7aacaa48db55ed429b31714afb0 Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Tue, 16 Aug 2011 14:58:32 +0530 Subject: Moves incompatible doctests to unit test modules. --- tests/test__version200.py | 12 ++++++++++++ tests/test_pem.py | 14 ++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/test__version200.py create mode 100644 tests/test_pem.py (limited to 'tests') diff --git a/tests/test__version200.py b/tests/test__version200.py new file mode 100644 index 0000000..2a179a8 --- /dev/null +++ b/tests/test__version200.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest2 +from rsa._compat import b + +from rsa._version200 import int2bytes, bytes2int + +class Test_int2bytes(unittest2.TestCase): + def test_values(self): + self.assertEqual(int2bytes(123456789), b('\x07[\xcd\x15')) + self.assertEqual(bytes2int(int2bytes(123456789)), 123456789) diff --git a/tests/test_pem.py b/tests/test_pem.py new file mode 100644 index 0000000..867f678 --- /dev/null +++ b/tests/test_pem.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +import unittest2 +from rsa._compat import b +from rsa.pem import _markers + + +class Test__markers(unittest2.TestCase): + def test_values(self): + self.assertEqual(_markers('RSA PRIVATE KEY'), + (b('-----BEGIN RSA PRIVATE KEY-----'), + b('-----END RSA PRIVATE KEY-----'))) -- cgit v1.2.3 From a5a43a5545c80bbc9d3df83011d0da41316d4afc Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Wed, 17 Aug 2011 00:19:39 +0530 Subject: Fixes a silly error. --- tests/test_common.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_common.py b/tests/test_common.py index 9980b15..9b69193 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -16,7 +16,6 @@ class Test_byte(unittest2.TestCase): self.assertRaises(struct.error, byte, 256) self.assertRaises(struct.error, byte, -1) - class Test_byte_size(unittest2.TestCase): def test_values(self): self.assertEqual(byte_size(1 << 1023), 128) @@ -24,7 +23,17 @@ class Test_byte_size(unittest2.TestCase): self.assertEqual(byte_size(1 << 1024), 129) def test_zero(self): - self.assertEqual(byte_size(0), 0) + self.assertEqual(byte_size(0), 1) + self.assertEqual(byte_size(255), 1) + self.assertEqual(byte_size(256), 2) + self.assertEqual(byte_size(0xffff), 2) + self.assertEqual(byte_size(0xffffff), 3) + self.assertEqual(byte_size(0xffffffff), 4) + self.assertEqual(byte_size(0xffffffffff), 5) + self.assertEqual(byte_size(0xffffffffffff), 6) + self.assertEqual(byte_size(0xffffffffffffff), 7) + self.assertEqual(byte_size(0xffffffffffffffff), 8) + def test_bad_type(self): self.assertRaises(TypeError, byte_size, []) -- cgit v1.2.3 From 5887158dcd73bfa24bef44c9d6267e4613b0c88c Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Wed, 17 Aug 2011 00:35:36 +0530 Subject: Reorganizes tests. --- tests/test_common.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/test_common.py b/tests/test_common.py index 9b69193..d105dc0 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -21,9 +21,6 @@ class Test_byte_size(unittest2.TestCase): self.assertEqual(byte_size(1 << 1023), 128) self.assertEqual(byte_size((1 << 1024) - 1), 128) self.assertEqual(byte_size(1 << 1024), 129) - - def test_zero(self): - self.assertEqual(byte_size(0), 1) self.assertEqual(byte_size(255), 1) self.assertEqual(byte_size(256), 2) self.assertEqual(byte_size(0xffff), 2) @@ -33,7 +30,9 @@ class Test_byte_size(unittest2.TestCase): self.assertEqual(byte_size(0xffffffffffff), 6) self.assertEqual(byte_size(0xffffffffffffff), 7) self.assertEqual(byte_size(0xffffffffffffffff), 8) - + + def test_zero(self): + self.assertEqual(byte_size(0), 1) def test_bad_type(self): self.assertRaises(TypeError, byte_size, []) -- cgit v1.2.3 From b5bab2221ad88ad49962c402eecae418db6a9eba Mon Sep 17 00:00:00 2001 From: Yesudeep Mangalapilly Date: Wed, 24 Aug 2011 16:53:39 +0530 Subject: Reverts docstring quoting syntax. --- tests/test_bigfile.py | 2 +- tests/test_integers.py | 2 +- tests/test_load_save_keys.py | 38 +++++++++++++++++++------------------- tests/test_pkcs1.py | 14 +++++++------- tests/test_strings.py | 2 +- tests/test_varblock.py | 2 +- 6 files changed, 30 insertions(+), 30 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 9e3a864..974da8b 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,4 +1,4 @@ -"""Tests block operations.""" +'''Tests block operations.''' from rsa._compat import b try: diff --git a/tests/test_integers.py b/tests/test_integers.py index c73be2c..0a712aa 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -1,4 +1,4 @@ -"""Tests integer operations.""" +'''Tests integer operations.''' import unittest2 diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 30bbb21..fc1a1aa 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -1,4 +1,4 @@ -"""Unittest for saving and loading keys.""" +'''Unittest for saving and loading keys.''' import base64 import unittest2 @@ -12,7 +12,7 @@ PRIVATE_DER = base64.decodestring(B64PRIV_DER) B64PUB_DER = b('MAwCBQDeKYlRAgMBAAE=') PUBLIC_DER = base64.decodestring(B64PUB_DER) -PRIVATE_PEM = b(""" +PRIVATE_PEM = b(''' -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -24,15 +24,15 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -""" % B64PRIV_DER.decode("utf-8")) +''' % B64PRIV_DER.decode("utf-8")) -CLEAN_PRIVATE_PEM = b("""\ +CLEAN_PRIVATE_PEM = b('''\ -----BEGIN RSA PRIVATE KEY----- %s -----END RSA PRIVATE KEY----- -""" % B64PRIV_DER.decode("utf-8")) +''' % B64PRIV_DER.decode("utf-8")) -PUBLIC_PEM = b(""" +PUBLIC_PEM = b(''' -----BEGIN CONFUSING STUFF----- Cruft before the key @@ -44,20 +44,20 @@ Comment: something blah Stuff after the key -----END CONFUSING STUFF----- -""" % B64PUB_DER.decode("utf-8")) +''' % B64PUB_DER.decode("utf-8")) -CLEAN_PUBLIC_PEM = b("""\ +CLEAN_PUBLIC_PEM = b('''\ -----BEGIN RSA PUBLIC KEY----- %s -----END RSA PUBLIC KEY----- -""" % B64PUB_DER.decode("utf-8")) +''' % B64PUB_DER.decode("utf-8")) class DerTest(unittest2.TestCase): - """Test saving and loading DER keys.""" + '''Test saving and loading DER keys.''' def test_load_private_key(self): - """Test loading private DER keys.""" + '''Test loading private DER keys.''' key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -65,7 +65,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - """Test saving private DER keys.""" + '''Test saving private DER keys.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) der = key.save_pkcs1('DER') @@ -73,7 +73,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(PRIVATE_DER, der) def test_load_public_key(self): - """Test loading public DER keys.""" + '''Test loading public DER keys.''' key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'DER') expected = rsa.key.PublicKey(3727264081, 65537) @@ -81,7 +81,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - """Test saving public DER keys.""" + '''Test saving public DER keys.''' key = rsa.key.PublicKey(3727264081, 65537) der = key.save_pkcs1('DER') @@ -89,11 +89,11 @@ class DerTest(unittest2.TestCase): self.assertEqual(PUBLIC_DER, der) class PemTest(unittest2.TestCase): - """Test saving and loading PEM keys.""" + '''Test saving and loading PEM keys.''' def test_load_private_key(self): - """Test loading private PEM files.""" + '''Test loading private PEM files.''' key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'PEM') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -101,7 +101,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - """Test saving private PEM files.""" + '''Test saving private PEM files.''' key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) pem = key.save_pkcs1('PEM') @@ -109,7 +109,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(CLEAN_PRIVATE_PEM, pem) def test_load_public_key(self): - """Test loading public PEM files.""" + '''Test loading public PEM files.''' key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'PEM') expected = rsa.key.PublicKey(3727264081, 65537) @@ -117,7 +117,7 @@ class PemTest(unittest2.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - """Test saving public PEM files.""" + '''Test saving public PEM files.''' key = rsa.key.PublicKey(3727264081, 65537) pem = key.save_pkcs1('PEM') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 5d2fbad..55098e2 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -1,4 +1,4 @@ -"""Tests string operations.""" +'''Tests string operations.''' import struct import unittest2 @@ -40,9 +40,9 @@ class BinaryTest(unittest2.TestCase): self.priv) def test_randomness(self): - """Encrypting the same message twice should result in different + '''Encrypting the same message twice should result in different cryptos. - """ + ''' message = struct.pack('>IIII', 0, 0, 0, 1) encrypted1 = pkcs1.encrypt(message, self.pub) @@ -56,7 +56,7 @@ class SignatureTest(unittest2.TestCase): (self.pub, self.priv) = rsa.newkeys(512) def test_sign_verify(self): - """Test happy flow of sign and verify""" + '''Test happy flow of sign and verify''' message = b('je moeder') print("\tMessage: %r" % message) @@ -67,14 +67,14 @@ class SignatureTest(unittest2.TestCase): pkcs1.verify(message, signature, self.pub) def test_alter_message(self): - """Altering the message should let the verification fail.""" + '''Altering the message should let the verification fail.''' signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, b('mijn moeder'), signature, self.pub) def test_sign_different_key(self): - """Signing with another key should let the verification fail.""" + '''Signing with another key should let the verification fail.''' (otherpub, _) = rsa.newkeys(512) @@ -84,7 +84,7 @@ class SignatureTest(unittest2.TestCase): message, signature, otherpub) def test_multiple_signings(self): - """Signing the same message twice should return the same signatures.""" + '''Signing the same message twice should return the same signatures.''' message = struct.pack('>IIII', 0, 0, 0, 1) signature1 = pkcs1.sign(message, self.priv, 'SHA-1') diff --git a/tests/test_strings.py b/tests/test_strings.py index a2f9483..001456d 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,4 +1,4 @@ -"""Tests string operations.""" +'''Tests string operations.''' from __future__ import absolute_import diff --git a/tests/test_varblock.py b/tests/test_varblock.py index d6465ed..24ea50f 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,4 +1,4 @@ -"""Tests varblock operations.""" +'''Tests varblock operations.''' try: -- cgit v1.2.3 From 15e257a8a147bfa7e8edac30089acd37984f8567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Sun, 6 Nov 2011 10:11:51 +0100 Subject: Fixed unittesting with tox --- tests/test_strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_strings.py b/tests/test_strings.py index 001456d..4af0629 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -6,7 +6,7 @@ import unittest2 import rsa -from tests.constants import unicode_string +from constants import unicode_string class StringTest(unittest2.TestCase): -- cgit v1.2.3 From c167ba3b35e7e067cfce83e4e31cde27de425991 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 18 Jun 2012 16:03:32 +0200 Subject: Tweaked unittests for Python 3.x --- tests/test__version200.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 tests/test__version200.py (limited to 'tests') diff --git a/tests/test__version200.py b/tests/test__version200.py deleted file mode 100644 index 2a179a8..0000000 --- a/tests/test__version200.py +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - -import unittest2 -from rsa._compat import b - -from rsa._version200 import int2bytes, bytes2int - -class Test_int2bytes(unittest2.TestCase): - def test_values(self): - self.assertEqual(int2bytes(123456789), b('\x07[\xcd\x15')) - self.assertEqual(bytes2int(int2bytes(123456789)), 123456789) -- cgit v1.2.3 From 7446f0a8d0eadac1581d86891fac8b5c554cc13b Mon Sep 17 00:00:00 2001 From: Tim Heckman Date: Wed, 17 Oct 2012 21:09:43 -0400 Subject: rsa.pkcs1.verify() should return True when successful - when verification passes verify() will return True, instead of None. If verification fails the function will still raise a rsa.pkcs1.VerificationError for legacy purposes. - update the docs to note that the verify() function returns True when successful - write unit tests to verify this new behavior This commit passes all build tests: Ran 44 tests in 1.217s OK --- tests/test_bigfile.py | 2 +- tests/test_pkcs1.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 974da8b..86bcbba 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -51,7 +51,7 @@ class BigfileTest(unittest2.TestCase): # Check the signature msgfile.seek(0) - pkcs1.verify(msgfile, signature, pub_key) + self.assertTrue(pkcs1.verify(msgfile, signature, pub_key)) # Alter the message, re-check msgfile = BytesIO(b('123456sybren')) diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 55098e2..d5882df 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -64,7 +64,7 @@ class SignatureTest(unittest2.TestCase): signature = pkcs1.sign(message, self.priv, 'SHA-256') print("\tSignature: %r" % signature) - pkcs1.verify(message, signature, self.pub) + self.assertTrue(pkcs1.verify(message, signature, self.pub)) def test_alter_message(self): '''Altering the message should let the verification fail.''' -- cgit v1.2.3 From 0659aacebbb17df7b072cc7bde6cd6ca91d3e000 Mon Sep 17 00:00:00 2001 From: Roy Kokkelkoren Date: Sun, 25 Oct 2015 16:12:11 +0100 Subject: Added per-file licenses --- tests/constants.py | 14 ++++++++++++++ tests/py2kconstants.py | 14 ++++++++++++++ tests/py3kconstants.py | 14 ++++++++++++++ tests/test_bigfile.py | 15 +++++++++++++++ tests/test_common.py | 14 ++++++++++++++ tests/test_compat.py | 14 ++++++++++++++ tests/test_integers.py | 15 +++++++++++++++ tests/test_load_save_keys.py | 15 +++++++++++++++ tests/test_pem.py | 15 ++++++++++++++- tests/test_pkcs1.py | 15 +++++++++++++++ tests/test_strings.py | 15 +++++++++++++++ tests/test_transform.py | 15 ++++++++++++++- tests/test_varblock.py | 15 +++++++++++++++ 13 files changed, 188 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/constants.py b/tests/constants.py index 6a0d081..5eab9f2 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -1,4 +1,18 @@ # -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. from rsa._compat import have_python3 diff --git a/tests/py2kconstants.py b/tests/py2kconstants.py index 5f695dd..0b53a78 100644 --- a/tests/py2kconstants.py +++ b/tests/py2kconstants.py @@ -1,3 +1,17 @@ # -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. unicode_string = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/py3kconstants.py b/tests/py3kconstants.py index 83b6712..c12a39b 100644 --- a/tests/py3kconstants.py +++ b/tests/py3kconstants.py @@ -1,3 +1,17 @@ # -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. unicode_string = "Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 86bcbba..03863c1 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Tests block operations.''' from rsa._compat import b diff --git a/tests/test_common.py b/tests/test_common.py index d105dc0..eba5d27 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,5 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. import unittest2 import struct diff --git a/tests/test_compat.py b/tests/test_compat.py index 3652c82..1788ff0 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -1,4 +1,18 @@ # -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. import unittest2 import struct diff --git a/tests/test_integers.py b/tests/test_integers.py index 0a712aa..58af48c 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Tests integer operations.''' import unittest2 diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index fc1a1aa..0d132b9 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Unittest for saving and loading keys.''' import base64 diff --git a/tests/test_pem.py b/tests/test_pem.py index 867f678..d1dcf3a 100644 --- a/tests/test_pem.py +++ b/tests/test_pem.py @@ -1,6 +1,19 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. import unittest2 from rsa._compat import b diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index d5882df..4be2dd8 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Tests string operations.''' import struct diff --git a/tests/test_strings.py b/tests/test_strings.py index 4af0629..e307efb 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Tests string operations.''' from __future__ import absolute_import diff --git a/tests/test_transform.py b/tests/test_transform.py index ffd9ec8..ed3e38b 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -1,5 +1,18 @@ # -*- coding: utf-8 -*- - +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. import unittest2 from rsa._compat import b diff --git a/tests/test_varblock.py b/tests/test_varblock.py index 24ea50f..6dad5ab 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,3 +1,18 @@ +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + '''Tests varblock operations.''' -- cgit v1.2.3 From 9d2cd9a213832b8fa000f3abb5188344f7b81bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 5 Nov 2015 19:53:38 +0000 Subject: Added character encoding markers for Python 2.x --- tests/test_bigfile.py | 1 + tests/test_integers.py | 1 + tests/test_load_save_keys.py | 1 + tests/test_pkcs1.py | 1 + tests/test_strings.py | 1 + tests/test_varblock.py | 1 + 6 files changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 03863c1..6c3de00 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # diff --git a/tests/test_integers.py b/tests/test_integers.py index 58af48c..f504e45 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 0d132b9..1a4ee23 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 4be2dd8..6150215 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # diff --git a/tests/test_strings.py b/tests/test_strings.py index e307efb..fe60055 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # diff --git a/tests/test_varblock.py b/tests/test_varblock.py index 6dad5ab..c6f2485 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- # # Copyright 2011 Sybren A. Stüvel # -- cgit v1.2.3 From ed1c81d6b1633b72e1c1e97cb7bc56b413481ddf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 14 Jan 2016 12:23:32 +0100 Subject: Removed dependency on unittest2, also merged requirements.txt for py 2.x and 3.x --- tests/test_bigfile.py | 4 ++-- tests/test_common.py | 8 ++++---- tests/test_compat.py | 4 ++-- tests/test_integers.py | 4 ++-- tests/test_load_save_keys.py | 6 +++--- tests/test_pem.py | 4 ++-- tests/test_pkcs1.py | 6 +++--- tests/test_strings.py | 4 ++-- tests/test_transform.py | 4 ++-- 9 files changed, 22 insertions(+), 22 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 6c3de00..b45b52f 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -21,12 +21,12 @@ try: from StringIO import StringIO as BytesIO except ImportError: from io import BytesIO -import unittest2 +import unittest import rsa from rsa import bigfile, varblock, pkcs1 -class BigfileTest(unittest2.TestCase): +class BigfileTest(unittest.TestCase): def test_encrypt_decrypt_bigfile(self): diff --git a/tests/test_common.py b/tests/test_common.py index eba5d27..a563d21 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -15,13 +15,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest2 +import unittest import struct from rsa._compat import byte, b from rsa.common import byte_size, bit_size, _bit_size -class Test_byte(unittest2.TestCase): +class Test_byte(unittest.TestCase): def test_values(self): self.assertEqual(byte(0), b('\x00')) self.assertEqual(byte(255), b('\xff')) @@ -30,7 +30,7 @@ class Test_byte(unittest2.TestCase): self.assertRaises(struct.error, byte, 256) self.assertRaises(struct.error, byte, -1) -class Test_byte_size(unittest2.TestCase): +class Test_byte_size(unittest.TestCase): def test_values(self): self.assertEqual(byte_size(1 << 1023), 128) self.assertEqual(byte_size((1 << 1024) - 1), 128) @@ -55,7 +55,7 @@ class Test_byte_size(unittest2.TestCase): self.assertRaises(TypeError, byte_size, "") self.assertRaises(TypeError, byte_size, None) -class Test_bit_size(unittest2.TestCase): +class Test_bit_size(unittest.TestCase): def test_zero(self): self.assertEqual(bit_size(0), 0) diff --git a/tests/test_compat.py b/tests/test_compat.py index 1788ff0..2ab7fd2 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -14,12 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest2 +import unittest import struct from rsa._compat import is_bytes, byte -class Test_byte(unittest2.TestCase): +class Test_byte(unittest.TestCase): def test_byte(self): for i in range(256): byt = byte(i) diff --git a/tests/test_integers.py b/tests/test_integers.py index f504e45..118204a 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -16,11 +16,11 @@ '''Tests integer operations.''' -import unittest2 +import unittest import rsa.core -class IntegerTest(unittest2.TestCase): +class IntegerTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(64) diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 1a4ee23..b2b4071 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -17,7 +17,7 @@ '''Unittest for saving and loading keys.''' import base64 -import unittest2 +import unittest from rsa._compat import b import rsa.key @@ -69,7 +69,7 @@ CLEAN_PUBLIC_PEM = b('''\ ''' % B64PUB_DER.decode("utf-8")) -class DerTest(unittest2.TestCase): +class DerTest(unittest.TestCase): '''Test saving and loading DER keys.''' def test_load_private_key(self): @@ -104,7 +104,7 @@ class DerTest(unittest2.TestCase): self.assertEqual(PUBLIC_DER, der) -class PemTest(unittest2.TestCase): +class PemTest(unittest.TestCase): '''Test saving and loading PEM keys.''' diff --git a/tests/test_pem.py b/tests/test_pem.py index d1dcf3a..de1b8a6 100644 --- a/tests/test_pem.py +++ b/tests/test_pem.py @@ -15,12 +15,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest2 +import unittest from rsa._compat import b from rsa.pem import _markers -class Test__markers(unittest2.TestCase): +class Test__markers(unittest.TestCase): def test_values(self): self.assertEqual(_markers('RSA PRIVATE KEY'), (b('-----BEGIN RSA PRIVATE KEY-----'), diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 6150215..7b92197 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -17,13 +17,13 @@ '''Tests string operations.''' import struct -import unittest2 +import unittest import rsa from rsa import pkcs1 from rsa._compat import byte, is_integer, b, is_bytes -class BinaryTest(unittest2.TestCase): +class BinaryTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(256) @@ -66,7 +66,7 @@ class BinaryTest(unittest2.TestCase): self.assertNotEqual(encrypted1, encrypted2) -class SignatureTest(unittest2.TestCase): +class SignatureTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(512) diff --git a/tests/test_strings.py b/tests/test_strings.py index fe60055..c4ee4c4 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -18,13 +18,13 @@ from __future__ import absolute_import -import unittest2 +import unittest import rsa from constants import unicode_string -class StringTest(unittest2.TestCase): +class StringTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(384) diff --git a/tests/test_transform.py b/tests/test_transform.py index ed3e38b..f919b1b 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -14,12 +14,12 @@ # See the License for the specific language governing permissions and # limitations under the License. -import unittest2 +import unittest from rsa._compat import b from rsa.transform import int2bytes, bytes2int, _int2bytes -class Test_int2bytes(unittest2.TestCase): +class Test_int2bytes(unittest.TestCase): def test_accuracy(self): self.assertEqual(int2bytes(123456789), b('\x07[\xcd\x15')) self.assertEqual(_int2bytes(123456789), b('\x07[\xcd\x15')) -- cgit v1.2.3 From e798a6e726eb6497b45e91bfdc498613528b16b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 18 Jan 2016 15:39:00 +0100 Subject: No longer using obsolete base64 interface, fixes bug #30 --- tests/test_load_save_keys.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index b2b4071..64f75de 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -23,10 +23,10 @@ from rsa._compat import b import rsa.key B64PRIV_DER = b('MC4CAQACBQDeKYlRAgMBAAECBQDHn4npAgMA/icCAwDfxwIDANcXAgInbwIDAMZt') -PRIVATE_DER = base64.decodestring(B64PRIV_DER) +PRIVATE_DER = base64.standard_b64decode(B64PRIV_DER) B64PUB_DER = b('MAwCBQDeKYlRAgMBAAE=') -PUBLIC_DER = base64.decodestring(B64PUB_DER) +PUBLIC_DER = base64.standard_b64decode(B64PUB_DER) PRIVATE_PEM = b(''' -----BEGIN CONFUSING STUFF----- -- cgit v1.2.3 From f68c52a018721dfb536cc735a6bfee4a9af1c419 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Mon, 18 Jan 2016 15:39:50 +0100 Subject: Updated key loading example and included it as unit test, fixes bug #31 --- tests/private.pem | 5 +++++ tests/test_load_save_keys.py | 12 ++++++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/private.pem (limited to 'tests') diff --git a/tests/private.pem b/tests/private.pem new file mode 100644 index 0000000..1a17279 --- /dev/null +++ b/tests/private.pem @@ -0,0 +1,5 @@ +-----BEGIN RSA PRIVATE KEY----- +MGECAQACEQCvWovlXBvfEeOMZPEleO9NAgMBAAECEA20Y+6fDkaWvC24horBzQEC +CQDdS2PAL/tK4QIJAMratZuNnT3tAghs7iNYA0ZrgQIIQQ5nU93U4fkCCHR55el6 +/K+2 +-----END RSA PRIVATE KEY----- diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 64f75de..6d87cf9 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -18,6 +18,8 @@ import base64 import unittest +import os.path + from rsa._compat import b import rsa.key @@ -141,3 +143,13 @@ class PemTest(unittest.TestCase): self.assertEqual(CLEAN_PUBLIC_PEM, pem) + def test_load_from_disk(self): + """Test loading a PEM file from disk.""" + + fname = os.path.join(os.path.dirname(__file__), 'private.pem') + with open(fname, mode='rb') as privatefile: + keydata = privatefile.read() + privkey = rsa.key.PrivateKey.load_pkcs1(keydata) + + self.assertEqual(15945948582725241569, privkey.p) + self.assertEqual(14617195220284816877, privkey.q) -- cgit v1.2.3 From a26f518df5eb42d004c0fc19dfdb630aecb182f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 22 Jan 2016 11:01:39 +0100 Subject: Fix #27: Close Pipes at parallel version of genprime. Now closing pipes after reading result, and added a unittest. --- tests/test_parallel.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 tests/test_parallel.py (limited to 'tests') diff --git a/tests/test_parallel.py b/tests/test_parallel.py new file mode 100644 index 0000000..1a69e9e --- /dev/null +++ b/tests/test_parallel.py @@ -0,0 +1,20 @@ +"""Test for multiprocess prime generation.""" + +import unittest + +import rsa.prime +import rsa.parallel +import rsa.common + + +class ParallelTest(unittest.TestCase): + """Tests for multiprocess prime generation.""" + + def test_parallel_primegen(self): + p = rsa.parallel.getprime(1024, 3) + + self.assertFalse(rsa.prime.is_prime(p - 1)) + self.assertTrue(rsa.prime.is_prime(p)) + self.assertFalse(rsa.prime.is_prime(p + 1)) + + self.assertEqual(1024, rsa.common.bit_size(p)) -- cgit v1.2.3 From d3d10345b47c2b17922bb91059cfceea82f82338 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 22 Jan 2016 11:36:06 +0100 Subject: =?UTF-8?q?Big=20refactor=20to=20become=20more=20PEP8=20compliant.?= =?UTF-8?q?=20Mostly=20focused=20on=20docstrings=20('''=20=E2=86=92=20""")?= =?UTF-8?q?,=20indentation,=20empty=20lines,=20and=20superfluous=20parenth?= =?UTF-8?q?esis.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/constants.py | 1 - tests/test_bigfile.py | 13 +++++-------- tests/test_common.py | 8 +++++--- tests/test_compat.py | 3 ++- tests/test_integers.py | 12 +++++------- tests/test_load_save_keys.py | 23 +++++++++++------------ tests/test_pem.py | 2 +- tests/test_pkcs1.py | 41 +++++++++++++++++++---------------------- tests/test_strings.py | 6 ++---- tests/test_varblock.py | 16 +++------------- 10 files changed, 53 insertions(+), 72 deletions(-) (limited to 'tests') diff --git a/tests/constants.py b/tests/constants.py index 5eab9f2..9a96774 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -20,4 +20,3 @@ if have_python3: from py3kconstants import * else: from py2kconstants import * - diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index b45b52f..87d76f6 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -14,7 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -'''Tests block operations.''' +"""Tests block operations.""" + from rsa._compat import b try: @@ -26,10 +27,9 @@ import unittest import rsa from rsa import bigfile, varblock, pkcs1 -class BigfileTest(unittest.TestCase): +class BigfileTest(unittest.TestCase): def test_encrypt_decrypt_bigfile(self): - # Expected block size + 11 bytes padding pub_key, priv_key = rsa.newkeys((6 + 11) * 8) @@ -48,16 +48,14 @@ class BigfileTest(unittest.TestCase): bigfile.decrypt_bigfile(cryptfile, clearfile, priv_key) self.assertEquals(clearfile.getvalue(), message) - + # We have 2x6 bytes in the message, so that should result in two # bigfile. cryptfile.seek(0) varblocks = list(varblock.yield_varblocks(cryptfile)) self.assertEqual(2, len(varblocks)) - def test_sign_verify_bigfile(self): - # Large enough to store MD5-sum and ASN.1 code for MD5 pub_key, priv_key = rsa.newkeys((34 + 11) * 8) @@ -72,5 +70,4 @@ class BigfileTest(unittest.TestCase): # Alter the message, re-check msgfile = BytesIO(b('123456sybren')) self.assertRaises(pkcs1.VerificationError, - pkcs1.verify, msgfile, signature, pub_key) - + pkcs1.verify, msgfile, signature, pub_key) diff --git a/tests/test_common.py b/tests/test_common.py index a563d21..131d116 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -21,7 +21,7 @@ from rsa._compat import byte, b from rsa.common import byte_size, bit_size, _bit_size -class Test_byte(unittest.TestCase): +class TestByte(unittest.TestCase): def test_values(self): self.assertEqual(byte(0), b('\x00')) self.assertEqual(byte(255), b('\xff')) @@ -30,7 +30,8 @@ class Test_byte(unittest.TestCase): self.assertRaises(struct.error, byte, 256) self.assertRaises(struct.error, byte, -1) -class Test_byte_size(unittest.TestCase): + +class TestByteSize(unittest.TestCase): def test_values(self): self.assertEqual(byte_size(1 << 1023), 128) self.assertEqual(byte_size((1 << 1024) - 1), 128) @@ -55,7 +56,8 @@ class Test_byte_size(unittest.TestCase): self.assertRaises(TypeError, byte_size, "") self.assertRaises(TypeError, byte_size, None) -class Test_bit_size(unittest.TestCase): + +class TestBitSize(unittest.TestCase): def test_zero(self): self.assertEqual(bit_size(0), 0) diff --git a/tests/test_compat.py b/tests/test_compat.py index 2ab7fd2..fa5e918 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -19,7 +19,8 @@ import struct from rsa._compat import is_bytes, byte -class Test_byte(unittest.TestCase): + +class TestByte(unittest.TestCase): def test_byte(self): for i in range(256): byt = byte(i) diff --git a/tests/test_integers.py b/tests/test_integers.py index 118204a..ad55eed 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -14,19 +14,19 @@ # See the License for the specific language governing permissions and # limitations under the License. -'''Tests integer operations.''' +"""Tests integer operations.""" import unittest +import rsa import rsa.core -class IntegerTest(unittest.TestCase): +class IntegerTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(64) def test_enc_dec(self): - message = 42 print("\tMessage: %d" % message) @@ -39,14 +39,12 @@ class IntegerTest(unittest.TestCase): self.assertEqual(message, decrypted) def test_sign_verify(self): - message = 42 - signed = rsa.core.encrypt_int(message,self.priv.d, self.pub.n) + signed = rsa.core.encrypt_int(message, self.priv.d, self.pub.n) print("\tSigned: %d" % signed) - verified = rsa.core.decrypt_int(signed, self.pub.e,self.pub.n) + verified = rsa.core.decrypt_int(signed, self.pub.e, self.pub.n) 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 6d87cf9..8fc3b22 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -72,10 +72,10 @@ CLEAN_PUBLIC_PEM = b('''\ class DerTest(unittest.TestCase): - '''Test saving and loading DER keys.''' + """Test saving and loading DER keys.""" def test_load_private_key(self): - '''Test loading private DER keys.''' + """Test loading private DER keys.""" key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_DER, 'DER') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -83,7 +83,7 @@ class DerTest(unittest.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - '''Test saving private DER keys.''' + """Test saving private DER keys.""" key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) der = key.save_pkcs1('DER') @@ -91,7 +91,7 @@ class DerTest(unittest.TestCase): self.assertEqual(PRIVATE_DER, der) def test_load_public_key(self): - '''Test loading public DER keys.''' + """Test loading public DER keys.""" key = rsa.key.PublicKey.load_pkcs1(PUBLIC_DER, 'DER') expected = rsa.key.PublicKey(3727264081, 65537) @@ -99,19 +99,19 @@ class DerTest(unittest.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - '''Test saving public DER keys.''' + """Test saving public DER keys.""" key = rsa.key.PublicKey(3727264081, 65537) der = key.save_pkcs1('DER') self.assertEqual(PUBLIC_DER, der) -class PemTest(unittest.TestCase): - '''Test saving and loading PEM keys.''' +class PemTest(unittest.TestCase): + """Test saving and loading PEM keys.""" def test_load_private_key(self): - '''Test loading private PEM files.''' + """Test loading private PEM files.""" key = rsa.key.PrivateKey.load_pkcs1(PRIVATE_PEM, 'PEM') expected = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) @@ -119,7 +119,7 @@ class PemTest(unittest.TestCase): self.assertEqual(expected, key) def test_save_private_key(self): - '''Test saving private PEM files.''' + """Test saving private PEM files.""" key = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) pem = key.save_pkcs1('PEM') @@ -127,7 +127,7 @@ class PemTest(unittest.TestCase): self.assertEqual(CLEAN_PRIVATE_PEM, pem) def test_load_public_key(self): - '''Test loading public PEM files.''' + """Test loading public PEM files.""" key = rsa.key.PublicKey.load_pkcs1(PUBLIC_PEM, 'PEM') expected = rsa.key.PublicKey(3727264081, 65537) @@ -135,14 +135,13 @@ class PemTest(unittest.TestCase): self.assertEqual(expected, key) def test_save_public_key(self): - '''Test saving public PEM files.''' + """Test saving public PEM files.""" key = rsa.key.PublicKey(3727264081, 65537) pem = key.save_pkcs1('PEM') self.assertEqual(CLEAN_PUBLIC_PEM, pem) - def test_load_from_disk(self): """Test loading a PEM file from disk.""" diff --git a/tests/test_pem.py b/tests/test_pem.py index de1b8a6..ca4278e 100644 --- a/tests/test_pem.py +++ b/tests/test_pem.py @@ -20,7 +20,7 @@ from rsa._compat import b from rsa.pem import _markers -class Test__markers(unittest.TestCase): +class TestMarkers(unittest.TestCase): def test_values(self): self.assertEqual(_markers('RSA PRIVATE KEY'), (b('-----BEGIN RSA PRIVATE KEY-----'), diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 7b92197..1bff0fb 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -14,22 +14,21 @@ # See the License for the specific language governing permissions and # limitations under the License. -'''Tests string operations.''' +"""Tests string operations.""" import struct import unittest import rsa from rsa import pkcs1 -from rsa._compat import byte, is_integer, b, is_bytes +from rsa._compat import byte, b, is_bytes -class BinaryTest(unittest.TestCase): +class BinaryTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(256) def test_enc_dec(self): - message = struct.pack('>IIII', 0, 0, 0, 1) print("\tMessage: %r" % message) @@ -42,7 +41,6 @@ class BinaryTest(unittest.TestCase): self.assertEqual(message, decrypted) def test_decoding_failure(self): - message = struct.pack('>IIII', 0, 0, 0, 1) encrypted = pkcs1.encrypt(message, self.pub) @@ -51,29 +49,29 @@ class BinaryTest(unittest.TestCase): if is_bytes(a): a = ord(a) encrypted = encrypted[:5] + byte(a + 1) + encrypted[6:] - + self.assertRaises(pkcs1.DecryptionError, pkcs1.decrypt, encrypted, self.priv) def test_randomness(self): - '''Encrypting the same message twice should result in different + """Encrypting the same message twice should result in different cryptos. - ''' - + """ + message = struct.pack('>IIII', 0, 0, 0, 1) encrypted1 = pkcs1.encrypt(message, self.pub) encrypted2 = pkcs1.encrypt(message, self.pub) - + self.assertNotEqual(encrypted1, encrypted2) -class SignatureTest(unittest.TestCase): +class SignatureTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(512) def test_sign_verify(self): - '''Test happy flow of sign and verify''' - + """Test happy flow of sign and verify""" + message = b('je moeder') print("\tMessage: %r" % message) @@ -83,28 +81,27 @@ class SignatureTest(unittest.TestCase): self.assertTrue(pkcs1.verify(message, signature, self.pub)) def test_alter_message(self): - '''Altering the message should let the verification fail.''' - + """Altering the message should let the verification fail.""" + signature = pkcs1.sign(b('je moeder'), self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, b('mijn moeder'), signature, self.pub) def test_sign_different_key(self): - '''Signing with another key should let the verification fail.''' - + """Signing with another key should let the verification fail.""" + (otherpub, _) = rsa.newkeys(512) - + message = b('je moeder') signature = pkcs1.sign(message, self.priv, 'SHA-256') self.assertRaises(pkcs1.VerificationError, pkcs1.verify, message, signature, otherpub) def test_multiple_signings(self): - '''Signing the same message twice should return the same signatures.''' - + """Signing the same message twice should return the same signatures.""" + message = struct.pack('>IIII', 0, 0, 0, 1) signature1 = pkcs1.sign(message, self.priv, 'SHA-1') signature2 = pkcs1.sign(message, self.priv, 'SHA-1') - - self.assertEqual(signature1, signature2) + self.assertEqual(signature1, signature2) diff --git a/tests/test_strings.py b/tests/test_strings.py index c4ee4c4..541b317 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -'''Tests string operations.''' +"""Tests string operations.""" from __future__ import absolute_import @@ -24,13 +24,12 @@ import rsa from constants import unicode_string -class StringTest(unittest.TestCase): +class StringTest(unittest.TestCase): def setUp(self): (self.pub, self.priv) = rsa.newkeys(384) def test_enc_dec(self): - message = unicode_string.encode('utf-8') print("\tMessage: %s" % message) @@ -41,4 +40,3 @@ class StringTest(unittest.TestCase): print("\tDecrypted: %s" % decrypted) self.assertEqual(message, decrypted) - diff --git a/tests/test_varblock.py b/tests/test_varblock.py index c6f2485..ac482f6 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -14,8 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -'''Tests varblock operations.''' - +"""Tests varblock operations.""" try: from StringIO import StringIO as BytesIO @@ -23,14 +22,12 @@ except ImportError: from io import BytesIO import unittest -import rsa from rsa._compat import b from rsa import varblock -class VarintTest(unittest.TestCase): +class VarintTest(unittest.TestCase): def test_read_varint(self): - encoded = b('\xac\x02crummy') infile = BytesIO(encoded) @@ -44,7 +41,6 @@ class VarintTest(unittest.TestCase): self.assertEqual(b('crummy'), infile.read()) def test_read_zero(self): - encoded = b('\x00crummy') infile = BytesIO(encoded) @@ -58,7 +54,6 @@ class VarintTest(unittest.TestCase): self.assertEqual(b('crummy'), infile.read()) def test_write_varint(self): - expected = b('\xac\x02') outfile = BytesIO() @@ -68,9 +63,7 @@ class VarintTest(unittest.TestCase): self.assertEqual(expected, outfile.getvalue()) self.assertEqual(2, written) - def test_write_zero(self): - outfile = BytesIO() written = varblock.write_varint(outfile, 0) @@ -80,19 +73,16 @@ class VarintTest(unittest.TestCase): class VarblockTest(unittest.TestCase): - def test_yield_varblock(self): infile = BytesIO(b('\x01\x0512345\x06Sybren')) varblocks = list(varblock.yield_varblocks(infile)) self.assertEqual([b('12345'), b('Sybren')], varblocks) -class FixedblockTest(unittest.TestCase): +class FixedblockTest(unittest.TestCase): def test_yield_fixedblock(self): - infile = BytesIO(b('123456Sybren')) fixedblocks = list(varblock.yield_fixedblocks(infile, 6)) self.assertEqual([b('123456'), b('Sybren')], fixedblocks) - -- cgit v1.2.3 From 2310b34bdb530e0bad793d42f589c9f848ff181b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 22 Jan 2016 13:11:22 +0100 Subject: Fix #19: Implemented blinding when decrypting. This prevents side-channel (such as timing) attacks, see: https://en.wikipedia.org/wiki/Blinding_%28cryptography%29 --- tests/test_key.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 tests/test_key.py (limited to 'tests') diff --git a/tests/test_key.py b/tests/test_key.py new file mode 100644 index 0000000..df35335 --- /dev/null +++ b/tests/test_key.py @@ -0,0 +1,30 @@ +""" +Some tests for the rsa/key.py file. +""" + + +import unittest + +import rsa.key +import rsa.core + + +class BlindingTest(unittest.TestCase): + + def test_blinding(self): + """Test blinding and unblinding. + + This is basically the doctest of the PrivateKey.blind method, but then + implemented as unittest to allow running on different Python versions. + """ + + pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + + message = 12345 + encrypted = rsa.core.encrypt_int(message, pk.e, pk.n) + + blinded = pk.blind(encrypted, 4134431) # blind before decrypting + decrypted = rsa.core.decrypt_int(blinded, pk.d, pk.n) + unblinded = pk.unblind(decrypted, 4134431) + + self.assertEqual(unblinded, message) -- cgit v1.2.3 From 4bc9733b78cd115a742b9486ab11ccbdcb9ca001 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 22 Jan 2016 15:41:40 +0100 Subject: Fix #12 Allow pickling of keys. Pickling is now possible, with the added note that one should never unpickle from an untrusted or unauthenticated source. --- tests/test_load_save_keys.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'tests') diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 8fc3b22..5ae1596 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -19,6 +19,7 @@ import base64 import unittest import os.path +import pickle from rsa._compat import b @@ -152,3 +153,22 @@ class PemTest(unittest.TestCase): self.assertEqual(15945948582725241569, privkey.p) self.assertEqual(14617195220284816877, privkey.q) + + +class PickleTest(unittest.TestCase): + """Test saving and loading keys by pickling.""" + + def test_private_key(self): + pk = rsa.key.PrivateKey(3727264081, 65537, 3349121513, 65063, 57287) + + pickled = pickle.dumps(pk) + unpickled = pickle.loads(pickled) + self.assertEqual(pk, unpickled) + + def test_public_key(self): + pk = rsa.key.PublicKey(3727264081, 65537) + + pickled = pickle.dumps(pk) + unpickled = pickle.loads(pickled) + + self.assertEqual(pk, unpickled) -- cgit v1.2.3 From dd5e979d43e1c6938a344e013e04eb4477c9f165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 27 Jan 2016 15:54:47 +0100 Subject: Simplified test structure a bit, so we no longer need to set PYTHONPATH The u'' string prefix was reintroduced in Python 3.3, and since we've dropped py3.2 support we can avoid the whole py{2k,3k}constants.py mess. --- tests/constants.py | 22 ---------------------- tests/py2kconstants.py | 17 ----------------- tests/py3kconstants.py | 17 ----------------- tests/test_strings.py | 2 +- 4 files changed, 1 insertion(+), 57 deletions(-) delete mode 100644 tests/constants.py delete mode 100644 tests/py2kconstants.py delete mode 100644 tests/py3kconstants.py (limited to 'tests') diff --git a/tests/constants.py b/tests/constants.py deleted file mode 100644 index 9a96774..0000000 --- a/tests/constants.py +++ /dev/null @@ -1,22 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2011 Sybren A. Stüvel -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -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 deleted file mode 100644 index 0b53a78..0000000 --- a/tests/py2kconstants.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2011 Sybren A. Stüvel -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -unicode_string = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/py3kconstants.py b/tests/py3kconstants.py deleted file mode 100644 index c12a39b..0000000 --- a/tests/py3kconstants.py +++ /dev/null @@ -1,17 +0,0 @@ -# -*- coding: utf-8 -*- -# -# Copyright 2011 Sybren A. Stüvel -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -unicode_string = "Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" diff --git a/tests/test_strings.py b/tests/test_strings.py index 541b317..2b9a1d1 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -22,7 +22,7 @@ import unittest import rsa -from constants import unicode_string +unicode_string = u"Euro=\u20ac ABCDEFGHIJKLMNOPQRSTUVWXYZ" class StringTest(unittest.TestCase): -- cgit v1.2.3 From 29feb7900fe03ca9c5771be753e4febb36e2af67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Wed, 27 Jan 2016 18:12:13 +0100 Subject: Fix #18: Add an 'exponent' argument to key.newkeys() Adds the possibility to create a new key using a custom exponent. Mostly for compatibility. Also removed the unused parameter nbits from calculate_keys(). I added a new function calculate_keys_custom_exponent() so that people still passing a value to nbits don't accidentally use it as the exponent. --- tests/test_key.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_key.py b/tests/test_key.py index df35335..0e62f55 100644 --- a/tests/test_key.py +++ b/tests/test_key.py @@ -2,7 +2,6 @@ Some tests for the rsa/key.py file. """ - import unittest import rsa.key @@ -10,7 +9,6 @@ import rsa.core class BlindingTest(unittest.TestCase): - def test_blinding(self): """Test blinding and unblinding. @@ -28,3 +26,17 @@ class BlindingTest(unittest.TestCase): unblinded = pk.unblind(decrypted, 4134431) self.assertEqual(unblinded, message) + + +class KeyGenTest(unittest.TestCase): + def test_custom_exponent(self): + priv, pub = rsa.key.newkeys(16, exponent=3) + + self.assertEqual(3, priv.e) + self.assertEqual(3, pub.e) + + def test_default_exponent(self): + priv, pub = rsa.key.newkeys(16) + + self.assertEqual(0x10001, priv.e) + self.assertEqual(0x10001, pub.e) -- cgit v1.2.3 From 3934ab4f16becf439033610b6986a796ab327f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 5 Feb 2016 16:01:20 +0100 Subject: Updated documentation, mostly http -> https changes Also: - changed http to https in the code - changed header underlines in the documentation to match the header length --- tests/test_bigfile.py | 2 +- tests/test_common.py | 2 +- tests/test_compat.py | 2 +- tests/test_integers.py | 2 +- tests/test_load_save_keys.py | 2 +- tests/test_pem.py | 2 +- tests/test_pkcs1.py | 2 +- tests/test_strings.py | 2 +- tests/test_transform.py | 2 +- tests/test_varblock.py | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/test_bigfile.py b/tests/test_bigfile.py index 87d76f6..70278dc 100644 --- a/tests/test_bigfile.py +++ b/tests/test_bigfile.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_common.py b/tests/test_common.py index 131d116..453dcc8 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -7,7 +7,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_compat.py b/tests/test_compat.py index fa5e918..8cbf101 100644 --- a/tests/test_compat.py +++ b/tests/test_compat.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_integers.py b/tests/test_integers.py index ad55eed..fb29ba4 100644 --- a/tests/test_integers.py +++ b/tests/test_integers.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_load_save_keys.py b/tests/test_load_save_keys.py index 5ae1596..6f374cf 100644 --- a/tests/test_load_save_keys.py +++ b/tests/test_load_save_keys.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_pem.py b/tests/test_pem.py index ca4278e..05bbb9b 100644 --- a/tests/test_pem.py +++ b/tests/test_pem.py @@ -7,7 +7,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index 1bff0fb..39555f6 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_strings.py b/tests/test_strings.py index 2b9a1d1..28fa091 100644 --- a/tests/test_strings.py +++ b/tests/test_strings.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_transform.py b/tests/test_transform.py index f919b1b..7fe121b 100644 --- a/tests/test_transform.py +++ b/tests/test_transform.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, diff --git a/tests/test_varblock.py b/tests/test_varblock.py index ac482f6..d1c3730 100644 --- a/tests/test_varblock.py +++ b/tests/test_varblock.py @@ -6,7 +6,7 @@ # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, -- cgit v1.2.3 From d0f1925bcd9c58d5ac7a3bc2d9686c602254eaeb Mon Sep 17 00:00:00 2001 From: adamantike Date: Thu, 28 Jan 2016 14:06:13 -0300 Subject: Add Jacobi test for table of values --- tests/test_prime.py | 214 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 214 insertions(+) create mode 100644 tests/test_prime.py (limited to 'tests') diff --git a/tests/test_prime.py b/tests/test_prime.py new file mode 100644 index 0000000..ef448e6 --- /dev/null +++ b/tests/test_prime.py @@ -0,0 +1,214 @@ +# -*- coding: utf-8 -*- +# +# Copyright 2011 Sybren A. Stüvel +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Tests prime functions.""" + +import unittest + +import rsa.prime + + +class JacobiTest(unittest.TestCase): + def test_table_values(self): + """ Tests table of values for 1<=a<=30, 1<=b<=59, where b is odd""" + + # Construct table of values, where case format is (a, b, jacobi(a, b)) + table_values = [ + (1, 1, 1), (1, 3, 1), (1, 5, 1), (1, 7, 1), (1, 9, 1), + (1, 11, 1), (1, 13, 1), (1, 15, 1), (1, 17, 1), (1, 19, 1), + (1, 21, 1), (1, 23, 1), (1, 25, 1), (1, 27, 1), (1, 29, 1), + (1, 31, 1), (1, 33, 1), (1, 35, 1), (1, 37, 1), (1, 39, 1), + (1, 41, 1), (1, 43, 1), (1, 45, 1), (1, 47, 1), (1, 49, 1), + (1, 51, 1), (1, 53, 1), (1, 55, 1), (1, 57, 1), (1, 59, 1), + (2, 1, 1), (2, 3, -1), (2, 5, -1), (2, 7, 1), (2, 9, 1), + (2, 11, -1), (2, 13, -1), (2, 15, 1), (2, 17, 1), (2, 19, -1), + (2, 21, -1), (2, 23, 1), (2, 25, 1), (2, 27, -1), (2, 29, -1), + (2, 31, 1), (2, 33, 1), (2, 35, -1), (2, 37, -1), (2, 39, 1), + (2, 41, 1), (2, 43, -1), (2, 45, -1), (2, 47, 1), (2, 49, 1), + (2, 51, -1), (2, 53, -1), (2, 55, 1), (2, 57, 1), (2, 59, -1), + (3, 1, 1), (3, 3, 0), (3, 5, -1), (3, 7, -1), (3, 9, 0), + (3, 11, 1), (3, 13, 1), (3, 15, 0), (3, 17, -1), (3, 19, -1), + (3, 21, 0), (3, 23, 1), (3, 25, 1), (3, 27, 0), (3, 29, -1), + (3, 31, -1), (3, 33, 0), (3, 35, 1), (3, 37, 1), (3, 39, 0), + (3, 41, -1), (3, 43, -1), (3, 45, 0), (3, 47, 1), (3, 49, 1), + (3, 51, 0), (3, 53, -1), (3, 55, -1), (3, 57, 0), (3, 59, 1), + (4, 1, 1), (4, 3, 1), (4, 5, 1), (4, 7, 1), (4, 9, 1), + (4, 11, 1), (4, 13, 1), (4, 15, 1), (4, 17, 1), (4, 19, 1), + (4, 21, 1), (4, 23, 1), (4, 25, 1), (4, 27, 1), (4, 29, 1), + (4, 31, 1), (4, 33, 1), (4, 35, 1), (4, 37, 1), (4, 39, 1), + (4, 41, 1), (4, 43, 1), (4, 45, 1), (4, 47, 1), (4, 49, 1), + (4, 51, 1), (4, 53, 1), (4, 55, 1), (4, 57, 1), (4, 59, 1), + (5, 1, 1), (5, 3, -1), (5, 5, 0), (5, 7, -1), (5, 9, 1), + (5, 11, 1), (5, 13, -1), (5, 15, 0), (5, 17, -1), (5, 19, 1), + (5, 21, 1), (5, 23, -1), (5, 25, 0), (5, 27, -1), (5, 29, 1), + (5, 31, 1), (5, 33, -1), (5, 35, 0), (5, 37, -1), (5, 39, 1), + (5, 41, 1), (5, 43, -1), (5, 45, 0), (5, 47, -1), (5, 49, 1), + (5, 51, 1), (5, 53, -1), (5, 55, 0), (5, 57, -1), (5, 59, 1), + (6, 1, 1), (6, 3, 0), (6, 5, 1), (6, 7, -1), (6, 9, 0), + (6, 11, -1), (6, 13, -1), (6, 15, 0), (6, 17, -1), (6, 19, 1), + (6, 21, 0), (6, 23, 1), (6, 25, 1), (6, 27, 0), (6, 29, 1), + (6, 31, -1), (6, 33, 0), (6, 35, -1), (6, 37, -1), (6, 39, 0), + (6, 41, -1), (6, 43, 1), (6, 45, 0), (6, 47, 1), (6, 49, 1), + (6, 51, 0), (6, 53, 1), (6, 55, -1), (6, 57, 0), (6, 59, -1), + (7, 1, 1), (7, 3, 1), (7, 5, -1), (7, 7, 0), (7, 9, 1), + (7, 11, -1), (7, 13, -1), (7, 15, -1), (7, 17, -1), (7, 19, 1), + (7, 21, 0), (7, 23, -1), (7, 25, 1), (7, 27, 1), (7, 29, 1), + (7, 31, 1), (7, 33, -1), (7, 35, 0), (7, 37, 1), (7, 39, -1), + (7, 41, -1), (7, 43, -1), (7, 45, -1), (7, 47, 1), (7, 49, 0), + (7, 51, -1), (7, 53, 1), (7, 55, 1), (7, 57, 1), (7, 59, 1), + (8, 1, 1), (8, 3, -1), (8, 5, -1), (8, 7, 1), (8, 9, 1), + (8, 11, -1), (8, 13, -1), (8, 15, 1), (8, 17, 1), (8, 19, -1), + (8, 21, -1), (8, 23, 1), (8, 25, 1), (8, 27, -1), (8, 29, -1), + (8, 31, 1), (8, 33, 1), (8, 35, -1), (8, 37, -1), (8, 39, 1), + (8, 41, 1), (8, 43, -1), (8, 45, -1), (8, 47, 1), (8, 49, 1), + (8, 51, -1), (8, 53, -1), (8, 55, 1), (8, 57, 1), (8, 59, -1), + (9, 1, 1), (9, 3, 0), (9, 5, 1), (9, 7, 1), (9, 9, 0), + (9, 11, 1), (9, 13, 1), (9, 15, 0), (9, 17, 1), (9, 19, 1), + (9, 21, 0), (9, 23, 1), (9, 25, 1), (9, 27, 0), (9, 29, 1), + (9, 31, 1), (9, 33, 0), (9, 35, 1), (9, 37, 1), (9, 39, 0), + (9, 41, 1), (9, 43, 1), (9, 45, 0), (9, 47, 1), (9, 49, 1), + (9, 51, 0), (9, 53, 1), (9, 55, 1), (9, 57, 0), (9, 59, 1), + (10, 1, 1), (10, 3, 1), (10, 5, 0), (10, 7, -1), (10, 9, 1), + (10, 11, -1), (10, 13, 1), (10, 15, 0), (10, 17, -1), (10, 19, -1), + (10, 21, -1), (10, 23, -1), (10, 25, 0), (10, 27, 1), (10, 29, -1), + (10, 31, 1), (10, 33, -1), (10, 35, 0), (10, 37, 1), (10, 39, 1), + (10, 41, 1), (10, 43, 1), (10, 45, 0), (10, 47, -1), (10, 49, 1), + (10, 51, -1), (10, 53, 1), (10, 55, 0), (10, 57, -1), (10, 59, -1), + (11, 1, 1), (11, 3, -1), (11, 5, 1), (11, 7, 1), (11, 9, 1), + (11, 11, 0), (11, 13, -1), (11, 15, -1), (11, 17, -1), (11, 19, 1), + (11, 21, -1), (11, 23, -1), (11, 25, 1), (11, 27, -1), (11, 29, -1), + (11, 31, -1), (11, 33, 0), (11, 35, 1), (11, 37, 1), (11, 39, 1), + (11, 41, -1), (11, 43, 1), (11, 45, 1), (11, 47, -1), (11, 49, 1), + (11, 51, 1), (11, 53, 1), (11, 55, 0), (11, 57, -1), (11, 59, -1), + (12, 1, 1), (12, 3, 0), (12, 5, -1), (12, 7, -1), (12, 9, 0), + (12, 11, 1), (12, 13, 1), (12, 15, 0), (12, 17, -1), (12, 19, -1), + (12, 21, 0), (12, 23, 1), (12, 25, 1), (12, 27, 0), (12, 29, -1), + (12, 31, -1), (12, 33, 0), (12, 35, 1), (12, 37, 1), (12, 39, 0), + (12, 41, -1), (12, 43, -1), (12, 45, 0), (12, 47, 1), (12, 49, 1), + (12, 51, 0), (12, 53, -1), (12, 55, -1), (12, 57, 0), (12, 59, 1), + (13, 1, 1), (13, 3, 1), (13, 5, -1), (13, 7, -1), (13, 9, 1), + (13, 11, -1), (13, 13, 0), (13, 15, -1), (13, 17, 1), (13, 19, -1), + (13, 21, -1), (13, 23, 1), (13, 25, 1), (13, 27, 1), (13, 29, 1), + (13, 31, -1), (13, 33, -1), (13, 35, 1), (13, 37, -1), (13, 39, 0), + (13, 41, -1), (13, 43, 1), (13, 45, -1), (13, 47, -1), (13, 49, 1), + (13, 51, 1), (13, 53, 1), (13, 55, 1), (13, 57, -1), (13, 59, -1), + (14, 1, 1), (14, 3, -1), (14, 5, 1), (14, 7, 0), (14, 9, 1), + (14, 11, 1), (14, 13, 1), (14, 15, -1), (14, 17, -1), (14, 19, -1), + (14, 21, 0), (14, 23, -1), (14, 25, 1), (14, 27, -1), (14, 29, -1), + (14, 31, 1), (14, 33, -1), (14, 35, 0), (14, 37, -1), (14, 39, -1), + (14, 41, -1), (14, 43, 1), (14, 45, 1), (14, 47, 1), (14, 49, 0), + (14, 51, 1), (14, 53, -1), (14, 55, 1), (14, 57, 1), (14, 59, -1), + (15, 1, 1), (15, 3, 0), (15, 5, 0), (15, 7, 1), (15, 9, 0), + (15, 11, 1), (15, 13, -1), (15, 15, 0), (15, 17, 1), (15, 19, -1), + (15, 21, 0), (15, 23, -1), (15, 25, 0), (15, 27, 0), (15, 29, -1), + (15, 31, -1), (15, 33, 0), (15, 35, 0), (15, 37, -1), (15, 39, 0), + (15, 41, -1), (15, 43, 1), (15, 45, 0), (15, 47, -1), (15, 49, 1), + (15, 51, 0), (15, 53, 1), (15, 55, 0), (15, 57, 0), (15, 59, 1), + (16, 1, 1), (16, 3, 1), (16, 5, 1), (16, 7, 1), (16, 9, 1), + (16, 11, 1), (16, 13, 1), (16, 15, 1), (16, 17, 1), (16, 19, 1), + (16, 21, 1), (16, 23, 1), (16, 25, 1), (16, 27, 1), (16, 29, 1), + (16, 31, 1), (16, 33, 1), (16, 35, 1), (16, 37, 1), (16, 39, 1), + (16, 41, 1), (16, 43, 1), (16, 45, 1), (16, 47, 1), (16, 49, 1), + (16, 51, 1), (16, 53, 1), (16, 55, 1), (16, 57, 1), (16, 59, 1), + (17, 1, 1), (17, 3, -1), (17, 5, -1), (17, 7, -1), (17, 9, 1), + (17, 11, -1), (17, 13, 1), (17, 15, 1), (17, 17, 0), (17, 19, 1), + (17, 21, 1), (17, 23, -1), (17, 25, 1), (17, 27, -1), (17, 29, -1), + (17, 31, -1), (17, 33, 1), (17, 35, 1), (17, 37, -1), (17, 39, -1), + (17, 41, -1), (17, 43, 1), (17, 45, -1), (17, 47, 1), (17, 49, 1), + (17, 51, 0), (17, 53, 1), (17, 55, 1), (17, 57, -1), (17, 59, 1), + (18, 1, 1), (18, 3, 0), (18, 5, -1), (18, 7, 1), (18, 9, 0), + (18, 11, -1), (18, 13, -1), (18, 15, 0), (18, 17, 1), (18, 19, -1), + (18, 21, 0), (18, 23, 1), (18, 25, 1), (18, 27, 0), (18, 29, -1), + (18, 31, 1), (18, 33, 0), (18, 35, -1), (18, 37, -1), (18, 39, 0), + (18, 41, 1), (18, 43, -1), (18, 45, 0), (18, 47, 1), (18, 49, 1), + (18, 51, 0), (18, 53, -1), (18, 55, 1), (18, 57, 0), (18, 59, -1), + (19, 1, 1), (19, 3, 1), (19, 5, 1), (19, 7, -1), (19, 9, 1), + (19, 11, -1), (19, 13, -1), (19, 15, 1), (19, 17, 1), (19, 19, 0), + (19, 21, -1), (19, 23, -1), (19, 25, 1), (19, 27, 1), (19, 29, -1), + (19, 31, 1), (19, 33, -1), (19, 35, -1), (19, 37, -1), (19, 39, -1), + (19, 41, -1), (19, 43, -1), (19, 45, 1), (19, 47, -1), (19, 49, 1), + (19, 51, 1), (19, 53, -1), (19, 55, -1), (19, 57, 0), (19, 59, 1), + (20, 1, 1), (20, 3, -1), (20, 5, 0), (20, 7, -1), (20, 9, 1), + (20, 11, 1), (20, 13, -1), (20, 15, 0), (20, 17, -1), (20, 19, 1), + (20, 21, 1), (20, 23, -1), (20, 25, 0), (20, 27, -1), (20, 29, 1), + (20, 31, 1), (20, 33, -1), (20, 35, 0), (20, 37, -1), (20, 39, 1), + (20, 41, 1), (20, 43, -1), (20, 45, 0), (20, 47, -1), (20, 49, 1), + (20, 51, 1), (20, 53, -1), (20, 55, 0), (20, 57, -1), (20, 59, 1), + (21, 1, 1), (21, 3, 0), (21, 5, 1), (21, 7, 0), (21, 9, 0), + (21, 11, -1), (21, 13, -1), (21, 15, 0), (21, 17, 1), (21, 19, -1), + (21, 21, 0), (21, 23, -1), (21, 25, 1), (21, 27, 0), (21, 29, -1), + (21, 31, -1), (21, 33, 0), (21, 35, 0), (21, 37, 1), (21, 39, 0), + (21, 41, 1), (21, 43, 1), (21, 45, 0), (21, 47, 1), (21, 49, 0), + (21, 51, 0), (21, 53, -1), (21, 55, -1), (21, 57, 0), (21, 59, 1), + (22, 1, 1), (22, 3, 1), (22, 5, -1), (22, 7, 1), (22, 9, 1), + (22, 11, 0), (22, 13, 1), (22, 15, -1), (22, 17, -1), (22, 19, -1), + (22, 21, 1), (22, 23, -1), (22, 25, 1), (22, 27, 1), (22, 29, 1), + (22, 31, -1), (22, 33, 0), (22, 35, -1), (22, 37, -1), (22, 39, 1), + (22, 41, -1), (22, 43, -1), (22, 45, -1), (22, 47, -1), (22, 49, 1), + (22, 51, -1), (22, 53, -1), (22, 55, 0), (22, 57, -1), (22, 59, 1), + (23, 1, 1), (23, 3, -1), (23, 5, -1), (23, 7, 1), (23, 9, 1), + (23, 11, 1), (23, 13, 1), (23, 15, 1), (23, 17, -1), (23, 19, 1), + (23, 21, -1), (23, 23, 0), (23, 25, 1), (23, 27, -1), (23, 29, 1), + (23, 31, -1), (23, 33, -1), (23, 35, -1), (23, 37, -1), (23, 39, -1), + (23, 41, 1), (23, 43, 1), (23, 45, -1), (23, 47, -1), (23, 49, 1), + (23, 51, 1), (23, 53, -1), (23, 55, -1), (23, 57, -1), (23, 59, -1), + (24, 1, 1), (24, 3, 0), (24, 5, 1), (24, 7, -1), (24, 9, 0), + (24, 11, -1), (24, 13, -1), (24, 15, 0), (24, 17, -1), (24, 19, 1), + (24, 21, 0), (24, 23, 1), (24, 25, 1), (24, 27, 0), (24, 29, 1), + (24, 31, -1), (24, 33, 0), (24, 35, -1), (24, 37, -1), (24, 39, 0), + (24, 41, -1), (24, 43, 1), (24, 45, 0), (24, 47, 1), (24, 49, 1), + (24, 51, 0), (24, 53, 1), (24, 55, -1), (24, 57, 0), (24, 59, -1), + (25, 1, 1), (25, 3, 1), (25, 5, 0), (25, 7, 1), (25, 9, 1), + (25, 11, 1), (25, 13, 1), (25, 15, 0), (25, 17, 1), (25, 19, 1), + (25, 21, 1), (25, 23, 1), (25, 25, 0), (25, 27, 1), (25, 29, 1), + (25, 31, 1), (25, 33, 1), (25, 35, 0), (25, 37, 1), (25, 39, 1), + (25, 41, 1), (25, 43, 1), (25, 45, 0), (25, 47, 1), (25, 49, 1), + (25, 51, 1), (25, 53, 1), (25, 55, 0), (25, 57, 1), (25, 59, 1), + (26, 1, 1), (26, 3, -1), (26, 5, 1), (26, 7, -1), (26, 9, 1), + (26, 11, 1), (26, 13, 0), (26, 15, -1), (26, 17, 1), (26, 19, 1), + (26, 21, 1), (26, 23, 1), (26, 25, 1), (26, 27, -1), (26, 29, -1), + (26, 31, -1), (26, 33, -1), (26, 35, -1), (26, 37, 1), (26, 39, 0), + (26, 41, -1), (26, 43, -1), (26, 45, 1), (26, 47, -1), (26, 49, 1), + (26, 51, -1), (26, 53, -1), (26, 55, 1), (26, 57, -1), (26, 59, 1), + (27, 1, 1), (27, 3, 0), (27, 5, -1), (27, 7, -1), (27, 9, 0), + (27, 11, 1), (27, 13, 1), (27, 15, 0), (27, 17, -1), (27, 19, -1), + (27, 21, 0), (27, 23, 1), (27, 25, 1), (27, 27, 0), (27, 29, -1), + (27, 31, -1), (27, 33, 0), (27, 35, 1), (27, 37, 1), (27, 39, 0), + (27, 41, -1), (27, 43, -1), (27, 45, 0), (27, 47, 1), (27, 49, 1), + (27, 51, 0), (27, 53, -1), (27, 55, -1), (27, 57, 0), (27, 59, 1), + (28, 1, 1), (28, 3, 1), (28, 5, -1), (28, 7, 0), (28, 9, 1), + (28, 11, -1), (28, 13, -1), (28, 15, -1), (28, 17, -1), (28, 19, 1), + (28, 21, 0), (28, 23, -1), (28, 25, 1), (28, 27, 1), (28, 29, 1), + (28, 31, 1), (28, 33, -1), (28, 35, 0), (28, 37, 1), (28, 39, -1), + (28, 41, -1), (28, 43, -1), (28, 45, -1), (28, 47, 1), (28, 49, 0), + (28, 51, -1), (28, 53, 1), (28, 55, 1), (28, 57, 1), (28, 59, 1), + (29, 1, 1), (29, 3, -1), (29, 5, 1), (29, 7, 1), (29, 9, 1), + (29, 11, -1), (29, 13, 1), (29, 15, -1), (29, 17, -1), (29, 19, -1), + (29, 21, -1), (29, 23, 1), (29, 25, 1), (29, 27, -1), (29, 29, 0), + (29, 31, -1), (29, 33, 1), (29, 35, 1), (29, 37, -1), (29, 39, -1), + (29, 41, -1), (29, 43, -1), (29, 45, 1), (29, 47, -1), (29, 49, 1), + (29, 51, 1), (29, 53, 1), (29, 55, -1), (29, 57, 1), (29, 59, 1), + (30, 1, 1), (30, 3, 0), (30, 5, 0), (30, 7, 1), (30, 9, 0), + (30, 11, -1), (30, 13, 1), (30, 15, 0), (30, 17, 1), (30, 19, 1), + (30, 21, 0), (30, 23, -1), (30, 25, 0), (30, 27, 0), (30, 29, 1), + (30, 31, -1), (30, 33, 0), (30, 35, 0), (30, 37, 1), (30, 39, 0), + (30, 41, -1), (30, 43, -1), (30, 45, 0), (30, 47, -1), (30, 49, 1), + (30, 51, 0), (30, 53, -1), (30, 55, 0), (30, 57, 0), (30, 59, -1), + ] + + for case in table_values: + a, b, result = case + self.assertEqual(rsa.prime.jacobi(a, b), result) -- cgit v1.2.3 From bb74fc918d133f170a40f4f90a3db3bf484214e7 Mon Sep 17 00:00:00 2001 From: adamantike Date: Wed, 10 Feb 2016 11:03:58 -0300 Subject: Remove Solovay-Strassen implementation --- tests/test_prime.py | 193 ---------------------------------------------------- 1 file changed, 193 deletions(-) (limited to 'tests') diff --git a/tests/test_prime.py b/tests/test_prime.py index ef448e6..55ec31a 100644 --- a/tests/test_prime.py +++ b/tests/test_prime.py @@ -19,196 +19,3 @@ import unittest import rsa.prime - - -class JacobiTest(unittest.TestCase): - def test_table_values(self): - """ Tests table of values for 1<=a<=30, 1<=b<=59, where b is odd""" - - # Construct table of values, where case format is (a, b, jacobi(a, b)) - table_values = [ - (1, 1, 1), (1, 3, 1), (1, 5, 1), (1, 7, 1), (1, 9, 1), - (1, 11, 1), (1, 13, 1), (1, 15, 1), (1, 17, 1), (1, 19, 1), - (1, 21, 1), (1, 23, 1), (1, 25, 1), (1, 27, 1), (1, 29, 1), - (1, 31, 1), (1, 33, 1), (1, 35, 1), (1, 37, 1), (1, 39, 1), - (1, 41, 1), (1, 43, 1), (1, 45, 1), (1, 47, 1), (1, 49, 1), - (1, 51, 1), (1, 53, 1), (1, 55, 1), (1, 57, 1), (1, 59, 1), - (2, 1, 1), (2, 3, -1), (2, 5, -1), (2, 7, 1), (2, 9, 1), - (2, 11, -1), (2, 13, -1), (2, 15, 1), (2, 17, 1), (2, 19, -1), - (2, 21, -1), (2, 23, 1), (2, 25, 1), (2, 27, -1), (2, 29, -1), - (2, 31, 1), (2, 33, 1), (2, 35, -1), (2, 37, -1), (2, 39, 1), - (2, 41, 1), (2, 43, -1), (2, 45, -1), (2, 47, 1), (2, 49, 1), - (2, 51, -1), (2, 53, -1), (2, 55, 1), (2, 57, 1), (2, 59, -1), - (3, 1, 1), (3, 3, 0), (3, 5, -1), (3, 7, -1), (3, 9, 0), - (3, 11, 1), (3, 13, 1), (3, 15, 0), (3, 17, -1), (3, 19, -1), - (3, 21, 0), (3, 23, 1), (3, 25, 1), (3, 27, 0), (3, 29, -1), - (3, 31, -1), (3, 33, 0), (3, 35, 1), (3, 37, 1), (3, 39, 0), - (3, 41, -1), (3, 43, -1), (3, 45, 0), (3, 47, 1), (3, 49, 1), - (3, 51, 0), (3, 53, -1), (3, 55, -1), (3, 57, 0), (3, 59, 1), - (4, 1, 1), (4, 3, 1), (4, 5, 1), (4, 7, 1), (4, 9, 1), - (4, 11, 1), (4, 13, 1), (4, 15, 1), (4, 17, 1), (4, 19, 1), - (4, 21, 1), (4, 23, 1), (4, 25, 1), (4, 27, 1), (4, 29, 1), - (4, 31, 1), (4, 33, 1), (4, 35, 1), (4, 37, 1), (4, 39, 1), - (4, 41, 1), (4, 43, 1), (4, 45, 1), (4, 47, 1), (4, 49, 1), - (4, 51, 1), (4, 53, 1), (4, 55, 1), (4, 57, 1), (4, 59, 1), - (5, 1, 1), (5, 3, -1), (5, 5, 0), (5, 7, -1), (5, 9, 1), - (5, 11, 1), (5, 13, -1), (5, 15, 0), (5, 17, -1), (5, 19, 1), - (5, 21, 1), (5, 23, -1), (5, 25, 0), (5, 27, -1), (5, 29, 1), - (5, 31, 1), (5, 33, -1), (5, 35, 0), (5, 37, -1), (5, 39, 1), - (5, 41, 1), (5, 43, -1), (5, 45, 0), (5, 47, -1), (5, 49, 1), - (5, 51, 1), (5, 53, -1), (5, 55, 0), (5, 57, -1), (5, 59, 1), - (6, 1, 1), (6, 3, 0), (6, 5, 1), (6, 7, -1), (6, 9, 0), - (6, 11, -1), (6, 13, -1), (6, 15, 0), (6, 17, -1), (6, 19, 1), - (6, 21, 0), (6, 23, 1), (6, 25, 1), (6, 27, 0), (6, 29, 1), - (6, 31, -1), (6, 33, 0), (6, 35, -1), (6, 37, -1), (6, 39, 0), - (6, 41, -1), (6, 43, 1), (6, 45, 0), (6, 47, 1), (6, 49, 1), - (6, 51, 0), (6, 53, 1), (6, 55, -1), (6, 57, 0), (6, 59, -1), - (7, 1, 1), (7, 3, 1), (7, 5, -1), (7, 7, 0), (7, 9, 1), - (7, 11, -1), (7, 13, -1), (7, 15, -1), (7, 17, -1), (7, 19, 1), - (7, 21, 0), (7, 23, -1), (7, 25, 1), (7, 27, 1), (7, 29, 1), - (7, 31, 1), (7, 33, -1), (7, 35, 0), (7, 37, 1), (7, 39, -1), - (7, 41, -1), (7, 43, -1), (7, 45, -1), (7, 47, 1), (7, 49, 0), - (7, 51, -1), (7, 53, 1), (7, 55, 1), (7, 57, 1), (7, 59, 1), - (8, 1, 1), (8, 3, -1), (8, 5, -1), (8, 7, 1), (8, 9, 1), - (8, 11, -1), (8, 13, -1), (8, 15, 1), (8, 17, 1), (8, 19, -1), - (8, 21, -1), (8, 23, 1), (8, 25, 1), (8, 27, -1), (8, 29, -1), - (8, 31, 1), (8, 33, 1), (8, 35, -1), (8, 37, -1), (8, 39, 1), - (8, 41, 1), (8, 43, -1), (8, 45, -1), (8, 47, 1), (8, 49, 1), - (8, 51, -1), (8, 53, -1), (8, 55, 1), (8, 57, 1), (8, 59, -1), - (9, 1, 1), (9, 3, 0), (9, 5, 1), (9, 7, 1), (9, 9, 0), - (9, 11, 1), (9, 13, 1), (9, 15, 0), (9, 17, 1), (9, 19, 1), - (9, 21, 0), (9, 23, 1), (9, 25, 1), (9, 27, 0), (9, 29, 1), - (9, 31, 1), (9, 33, 0), (9, 35, 1), (9, 37, 1), (9, 39, 0), - (9, 41, 1), (9, 43, 1), (9, 45, 0), (9, 47, 1), (9, 49, 1), - (9, 51, 0), (9, 53, 1), (9, 55, 1), (9, 57, 0), (9, 59, 1), - (10, 1, 1), (10, 3, 1), (10, 5, 0), (10, 7, -1), (10, 9, 1), - (10, 11, -1), (10, 13, 1), (10, 15, 0), (10, 17, -1), (10, 19, -1), - (10, 21, -1), (10, 23, -1), (10, 25, 0), (10, 27, 1), (10, 29, -1), - (10, 31, 1), (10, 33, -1), (10, 35, 0), (10, 37, 1), (10, 39, 1), - (10, 41, 1), (10, 43, 1), (10, 45, 0), (10, 47, -1), (10, 49, 1), - (10, 51, -1), (10, 53, 1), (10, 55, 0), (10, 57, -1), (10, 59, -1), - (11, 1, 1), (11, 3, -1), (11, 5, 1), (11, 7, 1), (11, 9, 1), - (11, 11, 0), (11, 13, -1), (11, 15, -1), (11, 17, -1), (11, 19, 1), - (11, 21, -1), (11, 23, -1), (11, 25, 1), (11, 27, -1), (11, 29, -1), - (11, 31, -1), (11, 33, 0), (11, 35, 1), (11, 37, 1), (11, 39, 1), - (11, 41, -1), (11, 43, 1), (11, 45, 1), (11, 47, -1), (11, 49, 1), - (11, 51, 1), (11, 53, 1), (11, 55, 0), (11, 57, -1), (11, 59, -1), - (12, 1, 1), (12, 3, 0), (12, 5, -1), (12, 7, -1), (12, 9, 0), - (12, 11, 1), (12, 13, 1), (12, 15, 0), (12, 17, -1), (12, 19, -1), - (12, 21, 0), (12, 23, 1), (12, 25, 1), (12, 27, 0), (12, 29, -1), - (12, 31, -1), (12, 33, 0), (12, 35, 1), (12, 37, 1), (12, 39, 0), - (12, 41, -1), (12, 43, -1), (12, 45, 0), (12, 47, 1), (12, 49, 1), - (12, 51, 0), (12, 53, -1), (12, 55, -1), (12, 57, 0), (12, 59, 1), - (13, 1, 1), (13, 3, 1), (13, 5, -1), (13, 7, -1), (13, 9, 1), - (13, 11, -1), (13, 13, 0), (13, 15, -1), (13, 17, 1), (13, 19, -1), - (13, 21, -1), (13, 23, 1), (13, 25, 1), (13, 27, 1), (13, 29, 1), - (13, 31, -1), (13, 33, -1), (13, 35, 1), (13, 37, -1), (13, 39, 0), - (13, 41, -1), (13, 43, 1), (13, 45, -1), (13, 47, -1), (13, 49, 1), - (13, 51, 1), (13, 53, 1), (13, 55, 1), (13, 57, -1), (13, 59, -1), - (14, 1, 1), (14, 3, -1), (14, 5, 1), (14, 7, 0), (14, 9, 1), - (14, 11, 1), (14, 13, 1), (14, 15, -1), (14, 17, -1), (14, 19, -1), - (14, 21, 0), (14, 23, -1), (14, 25, 1), (14, 27, -1), (14, 29, -1), - (14, 31, 1), (14, 33, -1), (14, 35, 0), (14, 37, -1), (14, 39, -1), - (14, 41, -1), (14, 43, 1), (14, 45, 1), (14, 47, 1), (14, 49, 0), - (14, 51, 1), (14, 53, -1), (14, 55, 1), (14, 57, 1), (14, 59, -1), - (15, 1, 1), (15, 3, 0), (15, 5, 0), (15, 7, 1), (15, 9, 0), - (15, 11, 1), (15, 13, -1), (15, 15, 0), (15, 17, 1), (15, 19, -1), - (15, 21, 0), (15, 23, -1), (15, 25, 0), (15, 27, 0), (15, 29, -1), - (15, 31, -1), (15, 33, 0), (15, 35, 0), (15, 37, -1), (15, 39, 0), - (15, 41, -1), (15, 43, 1), (15, 45, 0), (15, 47, -1), (15, 49, 1), - (15, 51, 0), (15, 53, 1), (15, 55, 0), (15, 57, 0), (15, 59, 1), - (16, 1, 1), (16, 3, 1), (16, 5, 1), (16, 7, 1), (16, 9, 1), - (16, 11, 1), (16, 13, 1), (16, 15, 1), (16, 17, 1), (16, 19, 1), - (16, 21, 1), (16, 23, 1), (16, 25, 1), (16, 27, 1), (16, 29, 1), - (16, 31, 1), (16, 33, 1), (16, 35, 1), (16, 37, 1), (16, 39, 1), - (16, 41, 1), (16, 43, 1), (16, 45, 1), (16, 47, 1), (16, 49, 1), - (16, 51, 1), (16, 53, 1), (16, 55, 1), (16, 57, 1), (16, 59, 1), - (17, 1, 1), (17, 3, -1), (17, 5, -1), (17, 7, -1), (17, 9, 1), - (17, 11, -1), (17, 13, 1), (17, 15, 1), (17, 17, 0), (17, 19, 1), - (17, 21, 1), (17, 23, -1), (17, 25, 1), (17, 27, -1), (17, 29, -1), - (17, 31, -1), (17, 33, 1), (17, 35, 1), (17, 37, -1), (17, 39, -1), - (17, 41, -1), (17, 43, 1), (17, 45, -1), (17, 47, 1), (17, 49, 1), - (17, 51, 0), (17, 53, 1), (17, 55, 1), (17, 57, -1), (17, 59, 1), - (18, 1, 1), (18, 3, 0), (18, 5, -1), (18, 7, 1), (18, 9, 0), - (18, 11, -1), (18, 13, -1), (18, 15, 0), (18, 17, 1), (18, 19, -1), - (18, 21, 0), (18, 23, 1), (18, 25, 1), (18, 27, 0), (18, 29, -1), - (18, 31, 1), (18, 33, 0), (18, 35, -1), (18, 37, -1), (18, 39, 0), - (18, 41, 1), (18, 43, -1), (18, 45, 0), (18, 47, 1), (18, 49, 1), - (18, 51, 0), (18, 53, -1), (18, 55, 1), (18, 57, 0), (18, 59, -1), - (19, 1, 1), (19, 3, 1), (19, 5, 1), (19, 7, -1), (19, 9, 1), - (19, 11, -1), (19, 13, -1), (19, 15, 1), (19, 17, 1), (19, 19, 0), - (19, 21, -1), (19, 23, -1), (19, 25, 1), (19, 27, 1), (19, 29, -1), - (19, 31, 1), (19, 33, -1), (19, 35, -1), (19, 37, -1), (19, 39, -1), - (19, 41, -1), (19, 43, -1), (19, 45, 1), (19, 47, -1), (19, 49, 1), - (19, 51, 1), (19, 53, -1), (19, 55, -1), (19, 57, 0), (19, 59, 1), - (20, 1, 1), (20, 3, -1), (20, 5, 0), (20, 7, -1), (20, 9, 1), - (20, 11, 1), (20, 13, -1), (20, 15, 0), (20, 17, -1), (20, 19, 1), - (20, 21, 1), (20, 23, -1), (20, 25, 0), (20, 27, -1), (20, 29, 1), - (20, 31, 1), (20, 33, -1), (20, 35, 0), (20, 37, -1), (20, 39, 1), - (20, 41, 1), (20, 43, -1), (20, 45, 0), (20, 47, -1), (20, 49, 1), - (20, 51, 1), (20, 53, -1), (20, 55, 0), (20, 57, -1), (20, 59, 1), - (21, 1, 1), (21, 3, 0), (21, 5, 1), (21, 7, 0), (21, 9, 0), - (21, 11, -1), (21, 13, -1), (21, 15, 0), (21, 17, 1), (21, 19, -1), - (21, 21, 0), (21, 23, -1), (21, 25, 1), (21, 27, 0), (21, 29, -1), - (21, 31, -1), (21, 33, 0), (21, 35, 0), (21, 37, 1), (21, 39, 0), - (21, 41, 1), (21, 43, 1), (21, 45, 0), (21, 47, 1), (21, 49, 0), - (21, 51, 0), (21, 53, -1), (21, 55, -1), (21, 57, 0), (21, 59, 1), - (22, 1, 1), (22, 3, 1), (22, 5, -1), (22, 7, 1), (22, 9, 1), - (22, 11, 0), (22, 13, 1), (22, 15, -1), (22, 17, -1), (22, 19, -1), - (22, 21, 1), (22, 23, -1), (22, 25, 1), (22, 27, 1), (22, 29, 1), - (22, 31, -1), (22, 33, 0), (22, 35, -1), (22, 37, -1), (22, 39, 1), - (22, 41, -1), (22, 43, -1), (22, 45, -1), (22, 47, -1), (22, 49, 1), - (22, 51, -1), (22, 53, -1), (22, 55, 0), (22, 57, -1), (22, 59, 1), - (23, 1, 1), (23, 3, -1), (23, 5, -1), (23, 7, 1), (23, 9, 1), - (23, 11, 1), (23, 13, 1), (23, 15, 1), (23, 17, -1), (23, 19, 1), - (23, 21, -1), (23, 23, 0), (23, 25, 1), (23, 27, -1), (23, 29, 1), - (23, 31, -1), (23, 33, -1), (23, 35, -1), (23, 37, -1), (23, 39, -1), - (23, 41, 1), (23, 43, 1), (23, 45, -1), (23, 47, -1), (23, 49, 1), - (23, 51, 1), (23, 53, -1), (23, 55, -1), (23, 57, -1), (23, 59, -1), - (24, 1, 1), (24, 3, 0), (24, 5, 1), (24, 7, -1), (24, 9, 0), - (24, 11, -1), (24, 13, -1), (24, 15, 0), (24, 17, -1), (24, 19, 1), - (24, 21, 0), (24, 23, 1), (24, 25, 1), (24, 27, 0), (24, 29, 1), - (24, 31, -1), (24, 33, 0), (24, 35, -1), (24, 37, -1), (24, 39, 0), - (24, 41, -1), (24, 43, 1), (24, 45, 0), (24, 47, 1), (24, 49, 1), - (24, 51, 0), (24, 53, 1), (24, 55, -1), (24, 57, 0), (24, 59, -1), - (25, 1, 1), (25, 3, 1), (25, 5, 0), (25, 7, 1), (25, 9, 1), - (25, 11, 1), (25, 13, 1), (25, 15, 0), (25, 17, 1), (25, 19, 1), - (25, 21, 1), (25, 23, 1), (25, 25, 0), (25, 27, 1), (25, 29, 1), - (25, 31, 1), (25, 33, 1), (25, 35, 0), (25, 37, 1), (25, 39, 1), - (25, 41, 1), (25, 43, 1), (25, 45, 0), (25, 47, 1), (25, 49, 1), - (25, 51, 1), (25, 53, 1), (25, 55, 0), (25, 57, 1), (25, 59, 1), - (26, 1, 1), (26, 3, -1), (26, 5, 1), (26, 7, -1), (26, 9, 1), - (26, 11, 1), (26, 13, 0), (26, 15, -1), (26, 17, 1), (26, 19, 1), - (26, 21, 1), (26, 23, 1), (26, 25, 1), (26, 27, -1), (26, 29, -1), - (26, 31, -1), (26, 33, -1), (26, 35, -1), (26, 37, 1), (26, 39, 0), - (26, 41, -1), (26, 43, -1), (26, 45, 1), (26, 47, -1), (26, 49, 1), - (26, 51, -1), (26, 53, -1), (26, 55, 1), (26, 57, -1), (26, 59, 1), - (27, 1, 1), (27, 3, 0), (27, 5, -1), (27, 7, -1), (27, 9, 0), - (27, 11, 1), (27, 13, 1), (27, 15, 0), (27, 17, -1), (27, 19, -1), - (27, 21, 0), (27, 23, 1), (27, 25, 1), (27, 27, 0), (27, 29, -1), - (27, 31, -1), (27, 33, 0), (27, 35, 1), (27, 37, 1), (27, 39, 0), - (27, 41, -1), (27, 43, -1), (27, 45, 0), (27, 47, 1), (27, 49, 1), - (27, 51, 0), (27, 53, -1), (27, 55, -1), (27, 57, 0), (27, 59, 1), - (28, 1, 1), (28, 3, 1), (28, 5, -1), (28, 7, 0), (28, 9, 1), - (28, 11, -1), (28, 13, -1), (28, 15, -1), (28, 17, -1), (28, 19, 1), - (28, 21, 0), (28, 23, -1), (28, 25, 1), (28, 27, 1), (28, 29, 1), - (28, 31, 1), (28, 33, -1), (28, 35, 0), (28, 37, 1), (28, 39, -1), - (28, 41, -1), (28, 43, -1), (28, 45, -1), (28, 47, 1), (28, 49, 0), - (28, 51, -1), (28, 53, 1), (28, 55, 1), (28, 57, 1), (28, 59, 1), - (29, 1, 1), (29, 3, -1), (29, 5, 1), (29, 7, 1), (29, 9, 1), - (29, 11, -1), (29, 13, 1), (29, 15, -1), (29, 17, -1), (29, 19, -1), - (29, 21, -1), (29, 23, 1), (29, 25, 1), (29, 27, -1), (29, 29, 0), - (29, 31, -1), (29, 33, 1), (29, 35, 1), (29, 37, -1), (29, 39, -1), - (29, 41, -1), (29, 43, -1), (29, 45, 1), (29, 47, -1), (29, 49, 1), - (29, 51, 1), (29, 53, 1), (29, 55, -1), (29, 57, 1), (29, 59, 1), - (30, 1, 1), (30, 3, 0), (30, 5, 0), (30, 7, 1), (30, 9, 0), - (30, 11, -1), (30, 13, 1), (30, 15, 0), (30, 17, 1), (30, 19, 1), - (30, 21, 0), (30, 23, -1), (30, 25, 0), (30, 27, 0), (30, 29, 1), - (30, 31, -1), (30, 33, 0), (30, 35, 0), (30, 37, 1), (30, 39, 0), - (30, 41, -1), (30, 43, -1), (30, 45, 0), (30, 47, -1), (30, 49, 1), - (30, 51, 0), (30, 53, -1), (30, 55, 0), (30, 57, 0), (30, 59, -1), - ] - - for case in table_values: - a, b, result = case - self.assertEqual(rsa.prime.jacobi(a, b), result) -- cgit v1.2.3 From cfff5bf567ab974cea932c461c8e75ab088c3d73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 17 Mar 2016 12:01:23 +0100 Subject: Reintroduced test for rsa.prime.is_prime --- tests/test_prime.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests') diff --git a/tests/test_prime.py b/tests/test_prime.py index 55ec31a..a47c3f2 100644 --- a/tests/test_prime.py +++ b/tests/test_prime.py @@ -19,3 +19,26 @@ import unittest import rsa.prime + + +class PrimeTest(unittest.TestCase): + def test_is_prime(self): + """Test some common primes.""" + + # Test some trivial numbers + self.assertFalse(rsa.prime.is_prime(-1)) + self.assertFalse(rsa.prime.is_prime(0)) + self.assertFalse(rsa.prime.is_prime(1)) + self.assertTrue(rsa.prime.is_prime(2)) + self.assertFalse(rsa.prime.is_prime(42)) + self.assertTrue(rsa.prime.is_prime(41)) + + # Test some slightly larger numbers + self.assertEqual( + [907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997], + [x for x in range(901, 1000) if rsa.prime.is_prime(x)] + ) + + # Test around the 50th millionth known prime. + self.assertTrue(rsa.prime.is_prime(982451653)) + self.assertFalse(rsa.prime.is_prime(982451653 * 961748941)) -- cgit v1.2.3 From bd4e213f07c45a8e932bf662ceb07926861775b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 17 Mar 2016 12:35:48 +0100 Subject: Allow loading PEM from strings (not just bytes), closes issue #49 --- tests/test_pem.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/test_pem.py b/tests/test_pem.py index 05bbb9b..952ec79 100644 --- a/tests/test_pem.py +++ b/tests/test_pem.py @@ -16,12 +16,59 @@ # limitations under the License. import unittest + from rsa._compat import b from rsa.pem import _markers +import rsa.key + +# 512-bit key. Too small for practical purposes, but good enough for testing with. +public_key_pem = ''' +-----BEGIN PUBLIC KEY----- +MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M +0c+h4sKMXwjhjbQAZdtWIw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQ== +-----END PUBLIC KEY----- +''' + +private_key_pem = ''' +-----BEGIN RSA PRIVATE KEY----- +MIIBOwIBAAJBAKH0aYP9ZFuctlPnXhEyHjgc8ltKKx9M0c+h4sKMXwjhjbQAZdtW +Iw8RRghpUJnKj+6bN2XzZDazyULxgPhtax0CAwEAAQJADwR36EpNzQTqDzusCFIq +ZS+h9X8aIovgBK3RNhMIGO2ThpsnhiDTcqIvgQ56knbl6B2W4iOl54tJ6CNtf6l6 +zQIhANTaNLFGsJfOvZHcI0WL1r89+1A4JVxR+lpslJJwAvgDAiEAwsjqqZ2wY2F0 +F8p1J98BEbtjU2mEZIVCMn6vQuhWdl8CIDRL4IJl4eGKlB0QP0JJF1wpeGO/R76l +DaPF5cMM7k3NAiEAss28m/ck9BWBfFVdNjx/vsdFZkx2O9AX9EJWoBSnSgECIQCa ++sVQMUVJFGsdE/31C7wCIbE3IpB7ziABZ7mN+V3Dhg== +-----END RSA PRIVATE KEY----- +''' + +# Private key components +prime1 = 96275860229939261876671084930484419185939191875438854026071315955024109172739 +prime2 = 88103681619592083641803383393198542599284510949756076218404908654323473741407 class TestMarkers(unittest.TestCase): def test_values(self): self.assertEqual(_markers('RSA PRIVATE KEY'), - (b('-----BEGIN RSA PRIVATE KEY-----'), - b('-----END RSA PRIVATE KEY-----'))) + (b('-----BEGIN RSA PRIVATE KEY-----'), + b('-----END RSA PRIVATE KEY-----'))) + + +class TestBytesAndStrings(unittest.TestCase): + """Test that we can use PEM in both Unicode strings and bytes.""" + + def test_unicode_public(self): + key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem) + self.assertEqual(prime1 * prime2, key.n) + + def test_bytes_public(self): + key = rsa.key.PublicKey.load_pkcs1_openssl_pem(public_key_pem.encode('ascii')) + self.assertEqual(prime1 * prime2, key.n) + + def test_unicode_private(self): + key = rsa.key.PrivateKey.load_pkcs1(private_key_pem) + self.assertEqual(prime1 * prime2, key.n) + + def test_bytes_private(self): + key = rsa.key.PrivateKey.load_pkcs1(private_key_pem.encode('ascii')) + self.assertEqual(prime1, key.p) + self.assertEqual(prime2, key.q) -- cgit v1.2.3