2019-02-20 06:09:18 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys, os, random
|
|
|
|
sys.path.append(".")
|
2019-11-21 09:26:23 +00:00
|
|
|
sys.path.append("src/")
|
2019-02-20 06:09:18 +00:00
|
|
|
import unittest, uuid
|
|
|
|
TEST_DIR_1 = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
|
|
|
TEST_DIR_2 = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
2019-08-05 23:09:04 +00:00
|
|
|
import time
|
|
|
|
|
|
|
|
os.environ["ONIONR_HOME"] = TEST_DIR_1
|
|
|
|
from utils import createdirs
|
|
|
|
createdirs.create_dirs()
|
2019-02-20 06:09:18 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
import onionrexceptions, onionrcrypto as crypto
|
2019-02-20 06:09:18 +00:00
|
|
|
from onionrusers import onionrusers
|
|
|
|
from onionrusers import contactmanager
|
2019-08-05 23:09:04 +00:00
|
|
|
|
2019-02-20 06:09:18 +00:00
|
|
|
|
|
|
|
class OnionrForwardSecrecyTests(unittest.TestCase):
|
|
|
|
'''
|
|
|
|
Tests both the onionrusers class and the contactmanager (which inherits it)
|
|
|
|
'''
|
2020-03-26 08:47:49 +00:00
|
|
|
def test_forward_encrypt_bin(self):
|
|
|
|
|
|
|
|
friend = crypto.generate()
|
|
|
|
|
|
|
|
friendUser = onionrusers.OnionrUser(friend[0], saveUser=True)
|
|
|
|
|
|
|
|
for x in range(5):
|
|
|
|
message = os.urandom(32)
|
|
|
|
forwardKey = friendUser.generateForwardKey()
|
|
|
|
|
|
|
|
fakeForwardPair = crypto.generate()
|
|
|
|
|
|
|
|
self.assertTrue(friendUser.addForwardKey(fakeForwardPair[0]))
|
|
|
|
|
|
|
|
encrypted = friendUser.forwardEncrypt(message)
|
|
|
|
|
|
|
|
decrypted = crypto.encryption.pub_key_decrypt(encrypted[0], privkey=fakeForwardPair[1], encodedData=True)
|
|
|
|
self.assertEqual(decrypted, message.encode())
|
2019-02-20 06:09:18 +00:00
|
|
|
|
2019-02-20 23:12:11 +00:00
|
|
|
def test_forward_encrypt(self):
|
2020-03-26 08:47:49 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
friend = crypto.generate()
|
2019-02-20 06:09:18 +00:00
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
friendUser = onionrusers.OnionrUser(friend[0], saveUser=True)
|
2019-02-20 06:09:18 +00:00
|
|
|
|
2019-02-20 23:12:11 +00:00
|
|
|
for x in range(5):
|
2019-02-20 06:09:18 +00:00
|
|
|
message = 'hello world %s' % (random.randint(1, 1000))
|
|
|
|
forwardKey = friendUser.generateForwardKey()
|
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
fakeForwardPair = crypto.generate()
|
2019-02-20 06:09:18 +00:00
|
|
|
|
|
|
|
self.assertTrue(friendUser.addForwardKey(fakeForwardPair[0]))
|
|
|
|
|
|
|
|
encrypted = friendUser.forwardEncrypt(message)
|
|
|
|
|
2019-07-25 16:14:13 +00:00
|
|
|
decrypted = crypto.encryption.pub_key_decrypt(encrypted[0], privkey=fakeForwardPair[1], encodedData=True)
|
2019-02-20 23:12:11 +00:00
|
|
|
self.assertEqual(decrypted, message.encode())
|
2019-02-20 06:09:18 +00:00
|
|
|
return
|
|
|
|
|
2019-09-13 02:22:25 +00:00
|
|
|
unittest.main()
|