2020-08-05 18:14:25 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2020-09-20 10:15:48 +02:00
|
|
|
# standard imports
|
2020-08-05 18:14:25 +02:00
|
|
|
import unittest
|
|
|
|
import logging
|
|
|
|
import base64
|
2020-12-25 09:21:09 +01:00
|
|
|
import os
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2021-03-31 12:13:04 +02:00
|
|
|
# external imports
|
2020-08-05 18:14:25 +02:00
|
|
|
import psycopg2
|
|
|
|
from psycopg2 import sql
|
2020-08-05 19:54:19 +02:00
|
|
|
from cryptography.fernet import Fernet, InvalidToken
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-09-20 10:15:48 +02:00
|
|
|
# local imports
|
2021-10-10 09:55:15 +02:00
|
|
|
from funga.eth.keystore.sql import SQLKeystore
|
|
|
|
from funga.error import UnknownAccountError
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
|
|
|
class TestDatabase(unittest.TestCase):
|
|
|
|
|
|
|
|
conn = None
|
|
|
|
cur = None
|
|
|
|
symkey = None
|
2020-08-05 19:47:38 +02:00
|
|
|
address_hex = None
|
2020-08-05 18:14:25 +02:00
|
|
|
db = None
|
|
|
|
|
|
|
|
def setUp(self):
|
2020-09-20 10:15:48 +02:00
|
|
|
logg.debug('setup')
|
2020-08-05 19:47:38 +02:00
|
|
|
# arbitrary value
|
|
|
|
symkey_hex = 'E92431CAEE69313A7BE9E443C4ABEED9BF8157E9A13553B4D5D6E7D51B5021D9'
|
|
|
|
self.symkey = bytes.fromhex(symkey_hex)
|
|
|
|
self.address_hex = '9FA61f0E52A5C51b43f0d32404625BC436bb7041'
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
kw = {
|
|
|
|
'symmetric_key': self.symkey,
|
|
|
|
}
|
2021-10-10 09:55:15 +02:00
|
|
|
self.db = SQLKeystore('postgres+psycopg2://postgres@localhost:5432/signer_test', **kw)
|
2020-08-06 11:07:18 +02:00
|
|
|
self.address_hex = self.db.new('foo')
|
2021-03-31 12:13:04 +02:00
|
|
|
#self.address_hex = add_0x(address_hex)
|
2020-08-05 19:47:38 +02:00
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
def tearDown(self):
|
2020-10-20 08:37:20 +02:00
|
|
|
self.db.db_session.execute('DROP INDEX ethereum_address_idx;')
|
|
|
|
self.db.db_session.execute('DROP TABLE ethereum;')
|
|
|
|
self.db.db_session.commit()
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_get_key(self):
|
2020-08-06 11:07:18 +02:00
|
|
|
logg.debug('getting {}'.format(self.address_hex))
|
2020-08-05 19:54:19 +02:00
|
|
|
self.db.get(self.address_hex, 'foo')
|
|
|
|
with self.assertRaises(InvalidToken):
|
|
|
|
self.db.get(self.address_hex, 'bar')
|
2020-08-05 18:14:25 +02:00
|
|
|
|
2020-12-25 09:21:09 +01:00
|
|
|
bogus_account = '0x' + os.urandom(20).hex()
|
|
|
|
with self.assertRaises(UnknownAccountError):
|
|
|
|
self.db.get(bogus_account, 'bar')
|
|
|
|
|
2020-08-05 18:14:25 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|