From 4827ef6def4cd371754f1ec4e33c9996a1720b4d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 15 Feb 2019 22:08:26 -0600 Subject: [PATCH] removed non-anonymous pubkey encryption, fixes, more tests --- onionr/tests/test_blocks.py | 19 ++++ onionr/tests/test_highlevelcrypto.py | 130 +++++++++++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 onionr/tests/test_blocks.py create mode 100644 onionr/tests/test_highlevelcrypto.py diff --git a/onionr/tests/test_blocks.py b/onionr/tests/test_blocks.py new file mode 100644 index 00000000..257238b1 --- /dev/null +++ b/onionr/tests/test_blocks.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +import sys, os +sys.path.append(".") +import unittest, uuid, hashlib +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 +import core, onionr + +c = core.Core() + +class OnionrBlockTests(unittest.TestCase): + def test_plaintext_insert(self): + message = 'hello world' + c.insertBlock(message) + +unittest.main() \ No newline at end of file diff --git a/onionr/tests/test_highlevelcrypto.py b/onionr/tests/test_highlevelcrypto.py new file mode 100644 index 00000000..67236b45 --- /dev/null +++ b/onionr/tests/test_highlevelcrypto.py @@ -0,0 +1,130 @@ +#!/usr/bin/env python3 +import sys, os +sys.path.append(".") +import unittest, uuid, hashlib +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 +import core, onionr + +c = core.Core() +crypto = c._crypto +class OnionrCryptoTests(unittest.TestCase): + + def test_blake2b(self): + self.assertTrue(crypto.blake2bHash('test') == crypto.blake2bHash(b'test')) + self.assertTrue(crypto.blake2bHash(b'test') == crypto.blake2bHash(b'test')) + + self.assertFalse(crypto.blake2bHash('') == crypto.blake2bHash(b'test')) + try: + crypto.blake2bHash(None) + except nacl.exceptions.TypeError: + pass + else: + self.assertTrue(False) + + self.assertTrue(nacl.hash.blake2b(b'test') == crypto.blake2bHash(b'test')) + + def test_sha3256(self): + hasher = hashlib.sha3_256() + self.assertTrue(crypto.sha3Hash('test') == crypto.sha3Hash(b'test')) + self.assertTrue(crypto.sha3Hash(b'test') == crypto.sha3Hash(b'test')) + + self.assertFalse(crypto.sha3Hash('') == crypto.sha3Hash(b'test')) + try: + crypto.sha3Hash(None) + except TypeError: + pass + else: + self.assertTrue(False) + + hasher.update(b'test') + normal = hasher.hexdigest() + self.assertTrue(crypto.sha3Hash(b'test') == normal) + + def valid_default_id(self): + self.assertTrue(c._utils.validatePubKey(crypto.pubKey)) + + def test_human_readable_length(self): + human = c._utils.getHumanReadableID() + self.assertTrue(len(human.split(' ')) == 32) + + def test_human_readable_rebuild(self): + return # Broken right now + # Test if we can get the human readable id, and convert it back to valid base32 key + human = c._utils.getHumanReadableID() + unHuman = c._utils.convertHumanReadableID(human) + nacl.signing.VerifyKey(c._utils.convertHumanReadableID(human), encoder=nacl.encoding.Base32Encoder) + + def test_safe_compare(self): + self.assertTrue(crypto.safeCompare('test', 'test')) + self.assertTrue(crypto.safeCompare('test', b'test')) + self.assertFalse(crypto.safeCompare('test', 'test2')) + try: + crypto.safeCompare('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 + + self.assertFalse(startList == list(crypto.randomShuffle(startList))) + self.assertTrue(len(startList) == len(startList)) + + def test_asymmetric(self): + keyPair = crypto.generatePubKey() + keyPair2 = crypto.generatePubKey() + message = "hello world" + + self.assertTrue(len(crypto.pubKeyEncrypt(message, keyPair2[0], encodedData=True)) > 0) + encrypted = crypto.pubKeyEncrypt(message, keyPair2[0], encodedData=False) + decrypted = crypto.pubKeyDecrypt(encrypted, privkey=keyPair2[1], encodedData=False) + + self.assertTrue(decrypted.decode() == message) + try: + crypto.pubKeyEncrypt(None, keyPair2[0]) + except TypeError: + pass + else: + self.assertTrue(False) + + blankMessage = crypto.pubKeyEncrypt('', keyPair2[0]) + self.assertTrue('' == crypto.pubKeyDecrypt(blankMessage, privkey=keyPair2[1], encodedData=False).decode()) + # Try to encrypt arbitrary bytes + crypto.pubKeyEncrypt(os.urandom(32), keyPair2[0]) + + def test_symmetric(self): + dataString = "this is a secret message" + dataBytes = dataString.encode() + key = b"tttttttttttttttttttttttttttttttt" + invalidKey = b'tttttttttttttttttttttttttttttttb' + encrypted = crypto.symmetricEncrypt(dataString, key, returnEncoded=True) + decrypted = crypto.symmetricDecrypt(encrypted, key, encodedMessage=True) + self.assertTrue(dataString == decrypted.decode()) + + try: + crypto.symmetricDecrypt(encrypted, invalidKey, encodedMessage=True) + except nacl.exceptions.CryptoError: + pass + else: + self.assertFalse(True) + try: + crypto.symmetricEncrypt(None, key, returnEncoded=True) + except AttributeError: + pass + else: + self.assertFalse(True) + crypto.symmetricEncrypt("string", key, returnEncoded=True) + try: + crypto.symmetricEncrypt("string", None, returnEncoded=True) + except nacl.exceptions.TypeError: + pass + else: + self.assertFalse(True) + +unittest.main() \ No newline at end of file