onionr/tests/test_highlevelcrypto.py

123 lines
4.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import sys, os
sys.path.append(".")
2019-09-13 02:22:25 +00:00
sys.path.append("onionr/")
2019-02-19 22:14:06 +00:00
import unittest, uuid, hashlib, base64
import nacl.exceptions
import nacl.signing, nacl.hash, nacl.encoding
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
print("Test directory:", TEST_DIR)
os.environ["ONIONR_HOME"] = TEST_DIR
2019-08-05 23:09:04 +00:00
from utils import createdirs
createdirs.create_dirs()
from onionrutils import stringvalidators, mnemonickeys
2019-07-25 16:14:13 +00:00
import onionrcrypto as crypto, onionrexceptions
class OnionrCryptoTests(unittest.TestCase):
def test_blake2b(self):
2019-07-25 16:14:13 +00:00
self.assertEqual(crypto.hashers.blake2b_hash('test'), crypto.hashers.blake2b_hash(b'test'))
self.assertEqual(crypto.hashers.blake2b_hash(b'test'), crypto.hashers.blake2b_hash(b'test'))
2019-07-25 16:14:13 +00:00
self.assertNotEqual(crypto.hashers.blake2b_hash(''), crypto.hashers.blake2b_hash(b'test'))
try:
2019-07-25 16:14:13 +00:00
crypto.hashers.blake2b_hash(None)
except nacl.exceptions.TypeError:
pass
else:
self.assertTrue(False)
2019-07-25 16:14:13 +00:00
self.assertEqual(nacl.hash.blake2b(b'test'), crypto.hashers.blake2b_hash(b'test'))
def test_sha3256(self):
hasher = hashlib.sha3_256()
2019-07-25 16:14:13 +00:00
self.assertEqual(crypto.hashers.sha3_hash('test'), crypto.hashers.sha3_hash(b'test'))
self.assertEqual(crypto.hashers.sha3_hash(b'test'), crypto.hashers.sha3_hash(b'test'))
2019-07-25 16:14:13 +00:00
self.assertNotEqual(crypto.hashers.sha3_hash(''), crypto.hashers.sha3_hash(b'test'))
try:
2019-07-25 16:14:13 +00:00
crypto.hashers.sha3_hash(None)
except TypeError:
pass
else:
self.assertTrue(False)
hasher.update(b'test')
normal = hasher.hexdigest()
2019-07-25 16:14:13 +00:00
self.assertEqual(crypto.hashers.sha3_hash(b'test'), normal)
def valid_default_id(self):
2019-07-25 16:14:13 +00:00
self.assertTrue(stringvalidators.validate_pub_key(crypto.pub_key))
def test_human_readable_length(self):
2019-07-25 16:14:13 +00:00
human = mnemonickeys.get_human_readable_ID()
2019-09-30 01:55:59 +00:00
self.assertTrue(len(human.split('-')) == 16)
def test_safe_compare(self):
2019-07-25 16:14:13 +00:00
self.assertTrue(crypto.cryptoutils.safe_compare('test', 'test'))
self.assertTrue(crypto.cryptoutils.safe_compare('test', b'test'))
self.assertFalse(crypto.cryptoutils.safe_compare('test', 'test2'))
try:
2019-07-25 16:14:13 +00:00
crypto.cryptoutils.safe_compare('test', None)
except TypeError:
pass
else:
self.assertTrue(False)
def test_random_shuffle(self):
# Small chance that the randomized list will be same. Rerun test a couple times if it fails
startList = ['cat', 'dog', 'moose', 'rabbit', 'monkey', 'crab', 'human', 'dolphin', 'whale', 'etc'] * 10
2019-07-25 16:14:13 +00:00
self.assertNotEqual(startList, list(crypto.cryptoutils.random_shuffle(startList)))
self.assertTrue(len(list(crypto.cryptoutils.random_shuffle(startList))) == len(startList))
def test_asymmetric(self):
2019-07-25 16:14:13 +00:00
keyPair = crypto.generate()
keyPair2 = crypto.generate()
message = "hello world"
2019-07-25 16:14:13 +00:00
self.assertTrue(len(crypto.encryption.pub_key_encrypt(message, keyPair2[0], encodedData=True)) > 0)
encrypted = crypto.encryption.pub_key_encrypt(message, keyPair2[0], encodedData=False)
decrypted = crypto.encryption.pub_key_decrypt(encrypted, privkey=keyPair2[1], encodedData=False)
self.assertTrue(decrypted.decode() == message)
try:
2019-07-25 16:14:13 +00:00
crypto.encryption.pub_key_encrypt(None, keyPair2[0])
except TypeError:
pass
else:
self.assertTrue(False)
2019-07-25 16:14:13 +00:00
blankMessage = crypto.encryption.pub_key_encrypt('', keyPair2[0])
self.assertTrue('' == crypto.encryption.pub_key_decrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode())
# Try to encrypt arbitrary bytes
2019-07-25 16:14:13 +00:00
crypto.encryption.pub_key_encrypt(os.urandom(32), keyPair2[0])
2019-02-19 22:14:06 +00:00
def test_deterministic(self):
password = os.urandom(32)
2019-07-25 16:14:13 +00:00
gen = crypto.generate_deterministic(password)
self.assertTrue(stringvalidators.validate_pub_key(gen[0]))
2019-02-19 22:14:06 +00:00
try:
2019-07-25 16:14:13 +00:00
crypto.generate_deterministic('weakpassword')
2019-02-19 22:14:06 +00:00
except onionrexceptions.PasswordStrengthError:
pass
else:
self.assertFalse(True)
try:
2019-07-25 16:14:13 +00:00
crypto.generate_deterministic(None)
2019-02-19 22:14:06 +00:00
except TypeError:
pass
else:
self.assertFalse(True)
2019-07-25 16:14:13 +00:00
gen = crypto.generate_deterministic('weakpassword', bypassCheck=True)
2019-02-19 22:14:06 +00:00
password = base64.b64encode(os.urandom(32))
2019-07-25 16:14:13 +00:00
gen1 = crypto.generate_deterministic(password)
gen2 = crypto.generate_deterministic(password)
2019-02-19 22:14:06 +00:00
self.assertFalse(gen == gen1)
self.assertTrue(gen1 == gen2)
self.assertTrue(stringvalidators.validate_pub_key(gen1[0]))
2019-09-13 02:22:25 +00:00
unittest.main()