From 16308f736ed469643851c2c44ee9ffc571410318 Mon Sep 17 00:00:00 2001 From: nolash Date: Sun, 10 Oct 2021 11:33:30 +0200 Subject: [PATCH] Add keystore test --- cic/data/network_template_v0.json | 10 ++++++ cic/ext/eth/__init__.py | 13 ++++--- cic/keystore.py | 30 ++++++++++++++++ cic/meta.py | 5 ++- cic/network.py | 22 +++++++----- requirements.txt | 1 + tests/test_keyfile.py | 47 +++++++++++++++++++++++++ tests/testdata/keystore/bogus/nokey.txt | 1 + tests/testdata/keystore/ok/key.json | 1 + 9 files changed, 117 insertions(+), 13 deletions(-) create mode 100644 cic/data/network_template_v0.json create mode 100644 cic/keystore.py create mode 100644 tests/test_keyfile.py create mode 100644 tests/testdata/keystore/bogus/nokey.txt create mode 100644 tests/testdata/keystore/ok/key.json diff --git a/cic/data/network_template_v0.json b/cic/data/network_template_v0.json new file mode 100644 index 0000000..3d8c869 --- /dev/null +++ b/cic/data/network_template_v0.json @@ -0,0 +1,10 @@ +{ + "token": { + "reference": null, + "key_account": null + }, + "token_index": { + "reference": null, + "key_account": null + } +} diff --git a/cic/ext/eth/__init__.py b/cic/ext/eth/__init__.py index 2c0d1fb..940b6ab 100644 --- a/cic/ext/eth/__init__.py +++ b/cic/ext/eth/__init__.py @@ -9,10 +9,15 @@ logg = logging.getLogger(__name__) class CICEth: - def __init__(self, reference, proofs, signer_hint=None): - self.reference = reference + def __init__(self, resources, proofs, signer_hint=None): + self.resources = resources self.proofs = proofs + self.signer_hint = signer_hint -def new(reference, proofs, signer_hint=None): - return CICEth(reference, proofs, signer_hint=None) + def token_index(self): + pass + + +def new(resources, proofs, signer_hint=None): + return CICEth(resources, proofs, signer_hint=None) diff --git a/cic/keystore.py b/cic/keystore.py new file mode 100644 index 0000000..5ea9a70 --- /dev/null +++ b/cic/keystore.py @@ -0,0 +1,30 @@ +# standard imports +import os +import json +import logging + +# external imports +from funga.error import ( + DecryptError, + KeyfileError, + ) +from funga.keystore import Keystore + +logg = logging.getLogger(__name__) + +class KeystoreDirectory(Keystore): + + def process_dir(self, path, password_retriever=None, default_password=''): + for v in os.listdir(path): + fp = os.path.join(path, v) + try: + self.import_keystore_file(fp, password=default_password) + except IsADirectoryError: + pass + except KeyfileError as e: + logg.warning('file {} could not be parsed as keyfile: {}'.format(fp, e)) + except DecryptError as e: + if password_retriever == None: + raise e + password = password_retriever() + self.import_keystore_file(fp, password=password) diff --git a/cic/meta.py b/cic/meta.py index 24d5c16..becc79e 100644 --- a/cic/meta.py +++ b/cic/meta.py @@ -3,7 +3,10 @@ import os import json # local imports -from .base import * +from .base import ( + Data, + data_dir, + ) class Meta(Data): diff --git a/cic/network.py b/cic/network.py index 6ec22a1..16a905c 100644 --- a/cic/network.py +++ b/cic/network.py @@ -3,7 +3,10 @@ import os import json # local imports -from .base import * +from .base import ( + Data, + data_dir, + ) class Network(Data): @@ -31,15 +34,18 @@ class Network(Data): def start(self): super(Network, self).load() - f = open(self.network_path, 'w') - o = {'references': {}} + network_template_file_path = os.path.join(data_dir, 'network_template_v{}.json'.format(self.version())) + f = open(network_template_file_path) + o_part = json.load(f) + f.close() + + f = open(self.network_path, 'w') + o = {'resources': {}} + for v in self.targets: - o['references'][v] = { - 'token': None, - 'token_index': None, - } - json.dump(o, f) + o['resources'][v] = o_part + json.dump(o, f) f.close() diff --git a/requirements.txt b/requirements.txt index e69de29..963b4b3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -0,0 +1 @@ +funga>=0.5.0a1,<0.6.0 diff --git a/tests/test_keyfile.py b/tests/test_keyfile.py new file mode 100644 index 0000000..4cfd455 --- /dev/null +++ b/tests/test_keyfile.py @@ -0,0 +1,47 @@ +# standard imports +import os +import unittest +import logging + +# local imports +from cic.keystore import KeystoreDirectory +from funga.eth.keystore.dict import DictKeystore +from funga.error import DecryptError +from hexathon import uniform as hex_uniform + +logging = logging.getLogger() + +script_dir = os.path.dirname(os.path.realpath(__file__)) + + +def pass_getter(): + return 'test' + + +class EthKeystoreDirectory(DictKeystore, KeystoreDirectory): + pass + + +class TestKeyfile(unittest.TestCase): + + def setUp(self): + self.path = os.path.join(script_dir, 'testdata', 'keystore') + self.keystore = EthKeystoreDirectory() + + + def test_keystore_bogus(self): + bogus_path = os.path.join(self.path, 'bogus') + self.keystore.process_dir(bogus_path) + + + def test_keystore_ok(self): + ok_path = os.path.join(self.path, 'ok') + with self.assertRaises(DecryptError): + self.keystore.process_dir(ok_path) # wrong password + self.keystore.process_dir(ok_path, default_password='test') + self.keystore.process_dir(ok_path, password_retriever=pass_getter) + self.assertTrue(hex_uniform('cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C') in self.keystore.list()) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/testdata/keystore/bogus/nokey.txt b/tests/testdata/keystore/bogus/nokey.txt new file mode 100644 index 0000000..257cc56 --- /dev/null +++ b/tests/testdata/keystore/bogus/nokey.txt @@ -0,0 +1 @@ +foo diff --git a/tests/testdata/keystore/ok/key.json b/tests/testdata/keystore/ok/key.json new file mode 100644 index 0000000..9f563b9 --- /dev/null +++ b/tests/testdata/keystore/ok/key.json @@ -0,0 +1 @@ +{"address": "cc4f82F5DacDE395E1E0CFc4d62827C8B8B5688C", "version": 3, "crypto": {"cipher": "aes-128-ctr", "ciphertext": "755c0911b7b4b36fab03f7c0238f29984db4a4d705b9a8aaeb5cd4a0066b586d", "cipherparams": {"iv": "61caf4396b9923f52c6a4a605a79fed5"}, "kdf": "scrypt", "kdfparams": {"dklen": 32, "n": 262144, "p": 1, "r": 8, "salt": "4a84bb019dfef7fb6d4fcfaa0be89f95bdd588d6df6827553da607366fee74b1"}, "mac": "fe551628027caaffaefefe20dd6f9150baca97e473bb238e7e3385c9cc6dd80f"}, "id": "95f197c8-29ab-11ec-aae8-02428a6d9287"}