Commits

Jason R. Coombs committed 1c3a460

Updated test to be runnable or discovered

Comments (0)

Files changed (1)

 #Use find_libary if dll in path
 #from ctypes.util import find_library
 #Location of Cryptographic DLL
-crypto_dll = os.path.join(r'C:\Python24', 'libeay32.dll')
+from ctypes.util import find_library
+crypto_dll = find_library('libeay32')
+crypto_dll = r'c:\program files\openssl\libeay32.dll'
 libcrypto = cdll.LoadLibrary(crypto_dll)
 libcrypto.OpenSSL_add_all_digests()
 libcrypto.OpenSSL_add_all_ciphers()
 from ctypescrypto import digest, cipher, rand
 import binascii
 
-class TestThread(Thread):
-    def __init__(self):
-        Thread.__init__(self)
+def test_basic_functionality():
+	digest_type = digest.DigestType(libcrypto, 'SHA512')
+	sha512 = digest.Digest(libcrypto, digest_type)
+	sha512.update("test")
+	assert binascii.hexlify(sha512.digest()) == "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f5"
+	for i in xrange(1, 1000):
+		c = cipher.CipherType(libcrypto, 'AES-256', 'CBC')
+		ce = cipher.Cipher(libcrypto, c, '11111111111111111111111111111111', '1111111111111111', encrypt=True)
+		ce.update("a" * i)
+		ce.update("b" * i)
+		e_t = ce.finish('c' * i)
+		c = cipher.CipherType(libcrypto, 'AES-256', 'CBC')
+		cd = cipher.Cipher(libcrypto, c, '11111111111111111111111111111111', '1111111111111111', encrypt=False)
+		assert cd.finish(e_t)==("a" * i) + ("b" * i) + ("c" * i)
+	ran = rand.bytes(libcrypto, 100)
+	assert len(ran) == 100
 
-    def run(self):
-        test_func()
+class ThreadedTester(Thread):
+	failed = False
+	def run(self):
+		try:
+			test_basic_functionality()
+		except Exception, e:
+			self.failed = True
+			self.exception = e
 
-def test_func():
-    digest_type = digest.DigestType(libcrypto, 'SHA512')
-    sha512 = digest.Digest(libcrypto, digest_type)
-    sha512.update("test")
-    assert binascii.hexlify(sha512.digest()) == "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f5"
-    for i in xrange(1, 1000):
-        c = cipher.CipherType(libcrypto, 'AES-256', 'CBC')
-        ce = cipher.Cipher(libcrypto, c, '11111111111111111111111111111111', '1111111111111111', encrypt=True)
-        ce.update("a" * i)
-        ce.update("b" * i)
-        e_t = ce.finish('c' * i)
-        c = cipher.CipherType(libcrypto, 'AES-256', 'CBC')
-        cd = cipher.Cipher(libcrypto, c, '11111111111111111111111111111111', '1111111111111111', encrypt=False)
-        assert cd.finish(e_t)==("a" * i) + ("b" * i) + ("c" * i)
-    ran = rand.bytes(libcrypto, 100)
-    assert len(ran) == 100
+def test_threaded_crypto():
+	threads = [ThreadedTester() for i in range(10)]
+	map(lambda t: t.start(), threads)
+	# wait for the threads to complete
+	map(lambda t: t.join(), threads)
+	assert all(not t.failed for t in threads), "Some threads failed"
 
-for i in xrange(1, 10):
-    th = TestThread()
-    th.start()
+if __name__ == '__main__':
+	test_basic_functionality()
+	test_threaded_crypto()