summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@cepl.eu>2018-03-18 23:11:38 +0100
committerMatěj Cepl <mcepl@cepl.eu>2018-03-20 18:47:23 +0100
commit17f7ca77afa75cedaa60bf3db767119adba4a2ec (patch)
tree0c4ff5f475bce84a3e7e88ab83b32dd38892066c /tests
parentb977909fc93caa599e52943be25b7f6042a4c70b (diff)
downloadm2crypto-17f7ca77afa75cedaa60bf3db767119adba4a2ec.tar.gz
PEP8isation
Specially replace complicated construct with unittest2 to one import to M2Crypto top module. Also, Rand.load_file should have first parameter as bytes(), not str().
Diffstat (limited to 'tests')
-rw-r--r--tests/__init__.py6
-rw-r--r--tests/test_aes.py12
-rw-r--r--tests/test_asn1.py5
-rwxr-xr-xtests/test_authcookie.py5
-rw-r--r--tests/test_bio.py23
-rw-r--r--tests/test_bio_file.py9
-rw-r--r--tests/test_bio_iobuf.py6
-rw-r--r--tests/test_bio_membuf.py5
-rw-r--r--tests/test_bio_ssl.py25
-rw-r--r--tests/test_bn.py5
-rw-r--r--tests/test_dh.py6
-rw-r--r--tests/test_dsa.py11
-rw-r--r--tests/test_ec_curves.py6
-rw-r--r--tests/test_ecdh.py5
-rw-r--r--tests/test_ecdsa.py5
-rw-r--r--tests/test_engine.py6
-rw-r--r--tests/test_evp.py8
-rw-r--r--tests/test_obj.py13
-rw-r--r--tests/test_rand.py5
-rw-r--r--tests/test_rc4.py5
-rw-r--r--tests/test_rsa.py6
-rw-r--r--tests/test_smime.py5
-rw-r--r--tests/test_ssl.py5
-rw-r--r--tests/test_ssl_offline.py16
-rw-r--r--tests/test_ssl_win.py9
-rw-r--r--tests/test_threading.py6
-rw-r--r--tests/test_util.py10
-rw-r--r--tests/test_x509.py5
28 files changed, 77 insertions, 156 deletions
diff --git a/tests/__init__.py b/tests/__init__.py
index 1d4b26a..688264f 100644
--- a/tests/__init__.py
+++ b/tests/__init__.py
@@ -6,5 +6,11 @@ if sys.version_info[:2] <= (2, 6):
sys.path.insert(0, os.path.join(os.path.abspath(os.path.dirname(__file__)),
'vendor'))
+try:
+ import unittest2 as unittest
+except ImportError:
+ import unittest
+
+
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s',
level=logging.DEBUG)
diff --git a/tests/test_aes.py b/tests/test_aes.py
index e525f9d..1da122d 100644
--- a/tests/test_aes.py
+++ b/tests/test_aes.py
@@ -5,17 +5,15 @@ from __future__ import absolute_import, print_function
Copyright (c) 2018 Matej Cepl. All rights reserved.
"""
import logging
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import m2
+from tests import unittest
log = logging.getLogger('test_AES')
KEY_TEXT = (b'blabulka' * 3)[:16] # 128/8 == 16
+
class AESTestCase(unittest.TestCase):
"""
Functions in AES module are very low level, and you are HIGHLY
@@ -73,9 +71,9 @@ class AESTestCase(unittest.TestCase):
def suite():
- s = unittest.TestSuite()
- s.addTest(unittest.makeSuite(AESTestCase))
- return s
+ t_suite = unittest.TestSuite()
+ t_suite.addTest(unittest.makeSuite(AESTestCase))
+ return t_suite
if __name__ == '__main__':
diff --git a/tests/test_asn1.py b/tests/test_asn1.py
index 9f12b77..0f5274f 100644
--- a/tests/test_asn1.py
+++ b/tests/test_asn1.py
@@ -6,12 +6,9 @@ Copyright (c) 2005 Open Source Applications Foundation. All rights reserved."""
import datetime
import time
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import ASN1, m2
+from tests import unittest
class ASN1TestCase(unittest.TestCase):
diff --git a/tests/test_authcookie.py b/tests/test_authcookie.py
index a2dae16..006571f 100755
--- a/tests/test_authcookie.py
+++ b/tests/test_authcookie.py
@@ -6,14 +6,11 @@ Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
import logging
import time
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import EVP, Rand, six, util
from M2Crypto.AuthCookie import AuthCookie, AuthCookieJar, mix, unmix, unmix3
from M2Crypto.six.moves.http_cookies import SimpleCookie # pylint: disable=no-name-in-module,import-error
+from tests import unittest
log = logging.getLogger(__name__)
diff --git a/tests/test_bio.py b/tests/test_bio.py
index 447df67..d2b4fa4 100644
--- a/tests/test_bio.py
+++ b/tests/test_bio.py
@@ -10,13 +10,9 @@ Copyright (c) 2006 Open Source Applications Foundation
Author: Heikki Toivonen
"""
import logging
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import BIO, Rand
-
+from tests import unittest
from tests.fips import fips_mode
log = logging.getLogger('test_bio')
@@ -64,11 +60,11 @@ class CipherStreamTestCase(unittest.TestCase):
'aes_192_ecb', 'aes_192_cbc', 'aes_192_cfb', 'aes_192_ofb',
'aes_256_ecb', 'aes_256_cbc', 'aes_256_cfb', 'aes_256_ofb']
nonfips_ciphers = ['bf_ecb', 'bf_cbc', 'bf_cfb', 'bf_ofb',
- #'idea_ecb', 'idea_cbc', 'idea_cfb', 'idea_ofb',
- 'cast5_ecb', 'cast5_cbc', 'cast5_cfb', 'cast5_ofb',
- #'rc5_ecb', 'rc5_cbc', 'rc5_cfb', 'rc5_ofb',
- 'des_ecb', 'des_cbc', 'des_cfb', 'des_ofb',
- 'rc4', 'rc2_40_cbc']
+ # 'idea_ecb', 'idea_cbc', 'idea_cfb', 'idea_ofb',
+ 'cast5_ecb', 'cast5_cbc', 'cast5_cfb', 'cast5_ofb',
+ # 'rc5_ecb', 'rc5_cbc', 'rc5_cfb', 'rc5_ofb',
+ 'des_ecb', 'des_cbc', 'des_cfb', 'des_ofb',
+ 'rc4', 'rc2_40_cbc']
if not fips_mode: # Forbidden ciphers
ciphers += nonfips_ciphers
@@ -86,10 +82,11 @@ class CipherStreamTestCase(unittest.TestCase):
with self.assertRaises(ValueError):
self.try_algo('nosuchalgo4567')
+
def suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(CipherStreamTestCase))
- return suite
+ t_suite = unittest.TestSuite()
+ t_suite.addTest(unittest.makeSuite(CipherStreamTestCase))
+ return t_suite
if __name__ == '__main__':
Rand.load_file('randpool.dat', -1)
diff --git a/tests/test_bio_file.py b/tests/test_bio_file.py
index 8256f43..fe0a8a7 100644
--- a/tests/test_bio_file.py
+++ b/tests/test_bio_file.py
@@ -6,21 +6,18 @@ Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
import logging
import os
-import sys
import platform
import tempfile
import ctypes
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
if platform.system() == 'Windows':
import ctypes.wintypes
from M2Crypto.BIO import File, openfile
+from tests import unittest
log = logging.getLogger(__name__)
+
def getCountProcHandles():
PROCESS_QUERY_INFORMATION = 0x400
handle = ctypes.windll.kernel32.OpenProcess(
@@ -32,6 +29,7 @@ def getCountProcHandles():
ctypes.windll.kernel32.CloseHandle(handle)
return sys_value + 1
+
class FileTestCase(unittest.TestCase):
def setUp(self):
@@ -105,7 +103,6 @@ class FileTestCase(unittest.TestCase):
self.assertEqual(in_data, self.data)
def test_readline_bin(self):
- sep = os.linesep.encode()
with open(self.fname, 'wb') as f:
f.write(b'hello\nworld\n')
with openfile(self.fname, 'rb') as f:
diff --git a/tests/test_bio_iobuf.py b/tests/test_bio_iobuf.py
index d206c7f..372fd04 100644
--- a/tests/test_bio_iobuf.py
+++ b/tests/test_bio_iobuf.py
@@ -5,12 +5,10 @@
Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
from io import BytesIO
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto.BIO import IOBuffer, MemoryBuffer
+from tests import unittest
+
class IOBufferTestCase(unittest.TestCase):
diff --git a/tests/test_bio_membuf.py b/tests/test_bio_membuf.py
index f9562db..49a0e24 100644
--- a/tests/test_bio_membuf.py
+++ b/tests/test_bio_membuf.py
@@ -6,12 +6,9 @@ Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
import os
import multiprocessing
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto.BIO import MemoryBuffer
+from tests import unittest
class TimeLimitExpired(Exception):
diff --git a/tests/test_bio_ssl.py b/tests/test_bio_ssl.py
index 7e204a5..3d087b0 100644
--- a/tests/test_bio_ssl.py
+++ b/tests/test_bio_ssl.py
@@ -5,11 +5,9 @@ from __future__ import absolute_import, print_function
Copyright (c) 1999-2002 Ng Pheng Siong. All rights reserved."""
-import threading, sys, socket
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
+import socket
+import sys
+import threading
from M2Crypto import BIO
from M2Crypto import SSL
@@ -17,8 +15,10 @@ from M2Crypto import Err
from M2Crypto import Rand
from M2Crypto import threading as m2threading
+from tests import unittest
from tests.test_ssl import srv_host, allocate_srv_port
+
class HandshakeClient(threading.Thread):
def __init__(self, host, port):
@@ -49,12 +49,12 @@ class HandshakeClient(threading.Thread):
print(err_string)
sys.exit("unrecoverable error in handshake - client")
else:
- output_token = writebio.read()
- if output_token is not None:
- sock.sendall(output_token)
- else:
- input_token = sock.recv(1024)
- readbio.write(input_token)
+ output_token = writebio.read()
+ if output_token is not None:
+ sock.sendall(output_token)
+ else:
+ input_token = sock.recv(1024)
+ readbio.write(input_token)
else:
handshake_complete = True
@@ -125,7 +125,7 @@ class SSLTestCase(unittest.TestCase):
sock.listen(5)
handshake_client = HandshakeClient(srv_host, srv_port)
handshake_client.start()
- new_sock, addr = sock.accept()
+ new_sock, _ = sock.accept()
while not handshake_complete:
input_token = new_sock.recv(1024)
readbio.write(input_token)
@@ -145,6 +145,7 @@ class SSLTestCase(unittest.TestCase):
sock.close()
new_sock.close()
+
def suite():
return unittest.makeSuite(SSLTestCase)
diff --git a/tests/test_bn.py b/tests/test_bn.py
index 0f59481..410969a 100644
--- a/tests/test_bn.py
+++ b/tests/test_bn.py
@@ -8,12 +8,9 @@ Copyright (c) 2005 Open Source Applications Foundation. All rights reserved.
import re
import warnings
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import BN, Rand
+from tests import unittest
loops = 16
diff --git a/tests/test_dh.py b/tests/test_dh.py
index 20b763a..074f154 100644
--- a/tests/test_dh.py
+++ b/tests/test_dh.py
@@ -4,12 +4,8 @@
Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
from M2Crypto import DH, BIO, Rand
+from tests import unittest
class DHTestCase(unittest.TestCase):
diff --git a/tests/test_dsa.py b/tests/test_dsa.py
index e90fb2c..a9a940a 100644
--- a/tests/test_dsa.py
+++ b/tests/test_dsa.py
@@ -5,12 +5,10 @@
Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
import hashlib
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import DSA, Rand
+from tests import unittest
+
class DSATestCase(unittest.TestCase):
@@ -107,11 +105,12 @@ class DSATestCase(unittest.TestCase):
q = dsa.q
g = dsa.g
pub = dsa.pub
- dsa2 = DSA.pub_key_from_params(p,q,g,pub)
+ dsa2 = DSA.pub_key_from_params(p, q, g, pub)
assert dsa2.check_key()
- r,s = dsa.sign(self.data)
+ r, s = dsa.sign(self.data)
assert dsa2.verify(self.data, r, s)
+
def suite():
return unittest.makeSuite(DSATestCase)
diff --git a/tests/test_ec_curves.py b/tests/test_ec_curves.py
index f208ad5..c15dcec 100644
--- a/tests/test_ec_curves.py
+++ b/tests/test_ec_curves.py
@@ -21,12 +21,8 @@ from __future__ import absolute_import
"""
import logging
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
from M2Crypto import EC, Rand, m2 # noqa
+from tests import unittest
log = logging.getLogger(__name__)
diff --git a/tests/test_ecdh.py b/tests/test_ecdh.py
index fbe5609..0dc4600 100644
--- a/tests/test_ecdh.py
+++ b/tests/test_ecdh.py
@@ -6,13 +6,10 @@ Copyright (c) 2000 Ng Pheng Siong. All rights reserved.
Portions copyright (c) 2005-2006 Vrije Universiteit Amsterdam. All
rights reserved.
"""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import EC, Rand
+from tests import unittest
from tests.test_ec_curves import tested_curve
diff --git a/tests/test_ecdsa.py b/tests/test_ecdsa.py
index 4cd2f0a..dea1209 100644
--- a/tests/test_ecdsa.py
+++ b/tests/test_ecdsa.py
@@ -8,13 +8,10 @@ rights reserved.
"""
import hashlib
import logging
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import EC, Rand
+from tests import unittest
from tests.test_ec_curves import tested_curve
log = logging.getLogger(__name__)
diff --git a/tests/test_engine.py b/tests/test_engine.py
index 79eaec0..c2c6047 100644
--- a/tests/test_engine.py
+++ b/tests/test_engine.py
@@ -2,12 +2,8 @@
"""Unit tests for M2Crypto.Engine."""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
from M2Crypto import Engine
+from tests import unittest
class EngineTestCase(unittest.TestCase):
diff --git a/tests/test_evp.py b/tests/test_evp.py
index e1af5fc..10654a8 100644
--- a/tests/test_evp.py
+++ b/tests/test_evp.py
@@ -12,14 +12,10 @@ import hashlib
import io
import logging
-from M2Crypto import BIO, EVP, RSA, Rand, m2, six, util
-
from binascii import a2b_hex, hexlify, unhexlify
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
+from M2Crypto import BIO, EVP, RSA, Rand, m2, util
+from tests import unittest
from tests.fips import fips_mode
log = logging.getLogger('test_EVP')
diff --git a/tests/test_obj.py b/tests/test_obj.py
index 7cdada0..737511a 100644
--- a/tests/test_obj.py
+++ b/tests/test_obj.py
@@ -2,13 +2,8 @@
"""Unit tests for M2Crypto.m2 obj_* functions.
"""
-
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
from M2Crypto import ASN1, BIO, Rand, X509, m2, six
+from tests import unittest
"""
These functions must be cleaned up and moved to some python module
@@ -112,9 +107,9 @@ class ObjectsTestCase(unittest.TestCase):
def suite():
- s = unittest.TestSuite()
- s.addTest(unittest.makeSuite(ObjectsTestCase))
- return s
+ t_suite = unittest.TestSuite()
+ t_suite.addTest(unittest.makeSuite(ObjectsTestCase))
+ return t_suite
if __name__ == '__main__':
diff --git a/tests/test_rand.py b/tests/test_rand.py
index f669209..cd27a25 100644
--- a/tests/test_rand.py
+++ b/tests/test_rand.py
@@ -9,12 +9,9 @@ All Rights Reserved.
import os
import ctypes
import warnings
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import Rand, m2
+from tests import unittest
class RandTestCase(unittest.TestCase):
diff --git a/tests/test_rc4.py b/tests/test_rc4.py
index ab30693..b06406c 100644
--- a/tests/test_rc4.py
+++ b/tests/test_rc4.py
@@ -5,13 +5,10 @@ from __future__ import absolute_import
Copyright (c) 2009 Heikki Toivonen. All rights reserved."""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import RC4, Rand
from binascii import hexlify
+from tests import unittest
from tests.fips import fips_mode
diff --git a/tests/test_rsa.py b/tests/test_rsa.py
index 85c0e49..308b1b1 100644
--- a/tests/test_rsa.py
+++ b/tests/test_rsa.py
@@ -8,13 +8,9 @@ Copyright (c) 2000 Ng Pheng Siong. All rights reserved."""
import hashlib
import logging
import os
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import BIO, RSA, Rand, X509, m2, six
-
+from tests import unittest
from tests.fips import fips_mode
log = logging.getLogger('test_RSA')
diff --git a/tests/test_smime.py b/tests/test_smime.py
index 678204b..a0edca8 100644
--- a/tests/test_smime.py
+++ b/tests/test_smime.py
@@ -6,12 +6,9 @@ Copyright (C) 2006 Open Source Applications Foundation. All Rights Reserved.
"""
import os.path
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import BIO, EVP, Rand, SMIME, X509
+from tests import unittest
# Various callbacks to set by X509_Store.set_verify_cb() for
diff --git a/tests/test_ssl.py b/tests/test_ssl.py
index 7c683dc..55ba0d1 100644
--- a/tests/test_ssl.py
+++ b/tests/test_ssl.py
@@ -30,13 +30,10 @@ import sys
import tempfile
import time
import warnings
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import (Err, Rand, SSL, X509, ftpslib, httpslib, m2, m2urllib,
m2urllib2, m2xmlrpclib, py27plus, six)
+from tests import unittest
from tests.fips import fips_mode
log = logging.getLogger('test_SSL')
diff --git a/tests/test_ssl_offline.py b/tests/test_ssl_offline.py
index 7cf803f..b9e2a1a 100644
--- a/tests/test_ssl_offline.py
+++ b/tests/test_ssl_offline.py
@@ -8,12 +8,9 @@ Copyright (C) 2009-2010 Heikki Toivonen. All Rights Reserved.
"""
import doctest
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
from M2Crypto import Rand, SSL, X509
+from tests import unittest
from tests.test_ssl import srv_host
@@ -40,8 +37,7 @@ class ContextTestCase(unittest.TestCase):
def test_ctx_set_default_verify_paths(self):
ctx = SSL.Context()
ctx.set_default_verify_paths()
- self.assertTrue(True) # test will get here only if the
- # previous won't fail
+ # test will get here only if the previous won't fail
def test_map(self):
from M2Crypto.SSL.Context import ctxmap, _ctxmap
@@ -62,10 +58,10 @@ class ContextTestCase(unittest.TestCase):
def suite():
- suite = unittest.TestSuite()
- suite.addTest(unittest.makeSuite(CheckerTestCase))
- suite.addTest(unittest.makeSuite(ContextTestCase))
- return suite
+ t_suite = unittest.TestSuite()
+ t_suite.addTest(unittest.makeSuite(CheckerTestCase))
+ t_suite.addTest(unittest.makeSuite(ContextTestCase))
+ return t_suite
if __name__ == '__main__':
diff --git a/tests/test_ssl_win.py b/tests/test_ssl_win.py
index 23aa0db..027c457 100644
--- a/tests/test_ssl_win.py
+++ b/tests/test_ssl_win.py
@@ -11,19 +11,16 @@ Copyright (c) 2000-2001 Ng Pheng Siong. All rights reserved."""
import os
import os.path
import time
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
try:
import win32process
except ImportError:
win32process = None
+from tests import test_ssl, unittest
+
if win32process:
from M2Crypto import Rand
- from tests import test_ssl
def find_openssl():
plist = os.environ['PATH'].split(';')
@@ -32,7 +29,7 @@ if win32process:
path_dir = os.listdir(p)
if 'openssl.exe' in path_dir:
return os.path.join(p, 'openssl.exe')
- except WindowsError:
+ except win32process.WindowsError:
pass
return None
diff --git a/tests/test_threading.py b/tests/test_threading.py
index 387aa11..ad1d111 100644
--- a/tests/test_threading.py
+++ b/tests/test_threading.py
@@ -4,12 +4,8 @@
Copyright (C) 2007 Open Source Applications Foundation. All Rights Reserved.
"""
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
from M2Crypto import threading as m2threading, Rand
+from tests import unittest
class ThreadingTestCase(unittest.TestCase):
diff --git a/tests/test_util.py b/tests/test_util.py
index c4b3c0e..0aa4c5e 100644
--- a/tests/test_util.py
+++ b/tests/test_util.py
@@ -6,14 +6,8 @@ Copyright (C) 2006 Open Source Applications Foundation (OSAF).
All Rights Reserved.
"""
-import os
-import warnings
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
-
-from M2Crypto import util, six
+from M2Crypto import six
+from tests import unittest
class UtilTestCase(unittest.TestCase):
diff --git a/tests/test_x509.py b/tests/test_x509.py
index aee7046..b582e52 100644
--- a/tests/test_x509.py
+++ b/tests/test_x509.py
@@ -13,13 +13,10 @@ import base64
import logging
import os
import time
-try:
- import unittest2 as unittest
-except ImportError:
- import unittest
import warnings
from M2Crypto import ASN1, BIO, EVP, RSA, Rand, X509, m2 # noqa
+from tests import unittest
log = logging.getLogger(__name__)