summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorSybren A. Stüvel <sybren@stuvel.eu>2016-01-22 11:36:06 +0100
committerSybren A. Stüvel <sybren@stuvel.eu>2016-01-22 11:36:06 +0100
commitd3d10345b47c2b17922bb91059cfceea82f82338 (patch)
tree6a336d74ee41a4ba98b6b3d97f123cd0c5f4e9b7 /tests
parent541ee468b6b33c7ae27818bbfea63df9622f9d8a (diff)
downloadrsa-git-d3d10345b47c2b17922bb91059cfceea82f82338.tar.gz
Big refactor to become more PEP8 compliant.
Mostly focused on docstrings (''' → """), indentation, empty lines, and superfluous parenthesis.
Diffstat (limited to 'tests')
-rw-r--r--tests/constants.py1
-rw-r--r--tests/test_bigfile.py13
-rw-r--r--tests/test_common.py8
-rw-r--r--tests/test_compat.py3
-rw-r--r--tests/test_integers.py12
-rw-r--r--tests/test_load_save_keys.py23
-rw-r--r--tests/test_pem.py2
-rw-r--r--tests/test_pkcs1.py41
-rw-r--r--tests/test_strings.py6
-rw-r--r--tests/test_varblock.py16
10 files changed, 53 insertions, 72 deletions
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)
-