2019-02-17 20:39:00 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys, os
|
|
|
|
sys.path.append(".")
|
2019-02-19 22:14:06 +00:00
|
|
|
import unittest, uuid
|
2019-02-17 20:39:00 +00:00
|
|
|
import json
|
|
|
|
TEST_DIR = 'testdata/%s-%s' % (uuid.uuid4(), os.path.basename(__file__)) + '/'
|
|
|
|
print("Test directory:", TEST_DIR)
|
|
|
|
os.environ["ONIONR_HOME"] = TEST_DIR
|
|
|
|
|
|
|
|
import onionrexceptions
|
|
|
|
from onionrusers import onionrusers
|
|
|
|
from onionrusers import contactmanager
|
2019-07-25 16:14:13 +00:00
|
|
|
import onionrcrypto as crypto
|
|
|
|
from coredb import keydb
|
|
|
|
from utils import identifyhome, createdirs
|
|
|
|
createdirs.create_dirs()
|
2019-02-17 20:39:00 +00:00
|
|
|
class OnionrUserTests(unittest.TestCase):
|
|
|
|
'''
|
|
|
|
Tests both the onionrusers class and the contactmanager (which inherits it)
|
|
|
|
'''
|
|
|
|
|
|
|
|
def test_users(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
keypair = crypto.generate()
|
|
|
|
onionrusers.OnionrUser(keypair[0])
|
2019-02-17 20:39:00 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
def test_contact_init_no_save(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contact = crypto.generate()[0]
|
|
|
|
contact = contactmanager.ContactManager(contact)
|
|
|
|
self.assertFalse(contact.publicKey in keydb.listkeys.list_peers())
|
2019-02-17 20:39:00 +00:00
|
|
|
|
|
|
|
def test_contact_create(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contact = crypto.generate()[0]
|
|
|
|
contact = contactmanager.ContactManager(contact, saveUser=True)
|
|
|
|
self.assertTrue(contact.publicKey in keydb.listkeys.list_peers())
|
2019-02-17 20:39:00 +00:00
|
|
|
|
|
|
|
def test_contact_set_info(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contact = crypto.generate()[0]
|
|
|
|
contact = contactmanager.ContactManager(contact, saveUser=True)
|
|
|
|
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
|
2019-02-17 20:39:00 +00:00
|
|
|
contact.set_info('alias', 'bob')
|
|
|
|
self.assertTrue(os.path.exists(fileLocation))
|
|
|
|
|
|
|
|
with open(fileLocation, 'r') as data:
|
|
|
|
data = data.read()
|
|
|
|
|
|
|
|
data = json.loads(data)
|
2019-02-20 23:12:11 +00:00
|
|
|
self.assertEqual(data['alias'], 'bob')
|
2019-02-17 20:39:00 +00:00
|
|
|
|
|
|
|
def test_contact_get_info(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contact = crypto.generate()[0]
|
|
|
|
contact = contactmanager.ContactManager(contact, saveUser=True)
|
|
|
|
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
|
2019-02-17 20:39:00 +00:00
|
|
|
|
|
|
|
with open(fileLocation, 'w') as contactFile:
|
|
|
|
contactFile.write('{"alias": "bob"}')
|
|
|
|
|
2019-02-20 23:12:11 +00:00
|
|
|
self.assertEqual(contact.get_info('alias', forceReload=True), 'bob')
|
|
|
|
self.assertEqual(contact.get_info('fail', forceReload=True), None)
|
|
|
|
self.assertEqual(contact.get_info('fail'), None)
|
|
|
|
|
|
|
|
def test_encrypt(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contactPair = crypto.generate()
|
|
|
|
contact = contactmanager.ContactManager(contactPair[0], saveUser=True)
|
2019-02-20 23:12:11 +00:00
|
|
|
encrypted = contact.encrypt('test')
|
2019-07-25 16:14:13 +00:00
|
|
|
decrypted = crypto.encryption.pub_key_decrypt(encrypted, privkey=contactPair[1], encodedData=True).decode()
|
2019-02-20 23:12:11 +00:00
|
|
|
self.assertEqual('test', decrypted)
|
2019-02-17 20:39:00 +00:00
|
|
|
|
|
|
|
def test_delete_contact(self):
|
2019-07-25 16:14:13 +00:00
|
|
|
contact = crypto.generate()[0]
|
|
|
|
contact = contactmanager.ContactManager(contact, saveUser=True)
|
|
|
|
fileLocation = '%s/contacts/%s.json' % (identifyhome.identify_home(), contact.publicKey)
|
2019-02-17 20:39:00 +00:00
|
|
|
self.assertFalse(os.path.exists(fileLocation))
|
|
|
|
with open(fileLocation, 'w') as contactFile:
|
|
|
|
contactFile.write('{"alias": "test"}')
|
|
|
|
self.assertTrue(os.path.exists(fileLocation))
|
|
|
|
contact.delete_contact()
|
|
|
|
self.assertFalse(os.path.exists(fileLocation))
|
|
|
|
try:
|
|
|
|
contact.get_info('alias')
|
|
|
|
except onionrexceptions.ContactDeleted:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
|
|
|
try:
|
|
|
|
contact.set_info('alias', 'test2')
|
|
|
|
except onionrexceptions.ContactDeleted:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
self.assertTrue(False)
|
|
|
|
|
|
|
|
unittest.main()
|