def testHisoryDataWithInvalidSignatures(): datum = gen.historyGen() vk, sk, did = gen.keyGen() datum[HISTORY]["signer"] = 1 datum[HISTORY]["signers"].append(vk) bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() # signed data has spaces that HistoryData object will not bad_bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(', ', ': ')).encode() signer = signing.signResource(bHistory, gen.key64uToKey(datum[SK1])) rotation = signing.signResource(bad_bHistory, gen.key64uToKey(datum[SK2])) data = { "history": datum[HISTORY], "signatures": { "signer": signer, "rotation": rotation } } history = resp.HistoryData(data) assert history.valid is False
def testHistoryDataWithInceptionData(): datum = gen.historyGen() bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() signature = signing.signResource(bHistory, gen.key64uToKey(datum[SK1])) data = {"history": datum[HISTORY], "signatures": {"signer": signature}} history = resp.HistoryData(data) assert history.data == data assert history.bdata == json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode() assert history.body == data["history"] assert history.bbody == json.dumps(data["history"], ensure_ascii=False, separators=(",", ":")).encode() assert history.did == data["history"]["id"] assert history.vk == data["history"]["signers"][0] assert history.signer_sig == signature assert history.rotation_sig is None assert history.signature == signature assert history.valid is True
def testHistoryGen(): history = gen.historyGen() assert history assert len(history) == 5 assert "id" in history[0] assert history[0]["id"] == "did:dad:{}".format(history[1]) assert "signer" in history[0] assert history[0]["signer"] == 0 assert "signers" in history[0] assert history[0]["signers"][0] == history[1] assert history[0]["signers"][1] == history[3]
def testResponseFactoryWtihHistoryData(): datum = gen.historyGen() bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() signature = signing.signResource(bHistory, gen.key64uToKey(datum[SK1])) data = {"history": datum[HISTORY], "signatures": {"signer": signature}} response = resp.responseFactory("", 200, data) assert type(response) == resp.DideryResponse assert type(response.response) == resp.HistoryData
def testHisoryDataWithInvalidSignature(): datum = gen.historyGen() bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(', ', ': ')).encode() # signed data has spaces that HistoryData object will not inv_signature = signing.signResource(bHistory, gen.key64uToKey(datum[SK1])) data = {"history": datum[HISTORY], "signatures": {"signer": inv_signature}} history = resp.HistoryData(data) assert history.valid is False
def historyInit(directory): didBoxInit = gen.DidBox() didBoxRot = gen.DidBox() history, vk, sk, pvk, psk = gen.historyGen(didBoxInit.seed, didBoxRot.seed) if directory is None: init_path = "./didery_keys_initial.json" rot_path = "./didery_keys_rotation.json" else: init_path = os.path.join(directory, "didery_keys_initial.json") rot_path = os.path.join(directory, "didery_keys_rotation.json") click.echo('Saving initial key pair to {}'.format(init_path)) click.echo('Saving pre-rotated key pair to {}'.format(rot_path)) didBoxInit.save64(init_path) didBoxRot.save64(rot_path) if directory is None: try: click.prompt( '\nKeys generated in: ' '' '\n\n./didery_keys_initial.json\n' './didery_keys_rotation.json\n\n' '' 'Make a copy and store them securely. \n' 'The file will be deleted after pressing any key+Enter') os.remove("./didery_keys_initial.json") os.remove("./didery_keys_rotation.json") click.echo('Key files deleted.') except KeyboardInterrupt as ex: if os.path.exists("./didery_keys_initial.json"): os.remove("./didery_keys_initial.json") if os.path.exists("./didery_keys_rotation.json"): os.remove("./didery_keys_rotation.json") click.echo('Key files deleted.') raise return history, sk
def testHistoryGenWithSeed(): seed1 = b'\x8dh\xf3\xa0!\xd1\xcd\xc0b\x8c^\x1d\xbcg>\xe1S\x12\xc7\xcb\xbc\x0eTOz@\xdb} \xba\x06\x04' seed2 = b'\x8dh\xf3\xa0!\xd1\xcd\xc0b\x8c^\x1d\xbcg>\xe1S\x11\xc7\xcb\xbc\x0eTOz@\xdb} \xba\x06\x04' history = gen.historyGen(seed1, seed2) exp_history = ( { 'id': 'did:dad:u8TWIE28yAdu-lGrPHH2ZJ73GxoMBZx-D3cf6RMEaTc=', 'signer': 0, 'signers': [ 'u8TWIE28yAdu-lGrPHH2ZJ73GxoMBZx-D3cf6RMEaTc=', 'Tni_GX-7IzHAcBwRfN0YBbKQDmpjEjOfB0Pmiki-hCg=' ] }, 'u8TWIE28yAdu-lGrPHH2ZJ73GxoMBZx-D3cf6RMEaTc=', 'jWjzoCHRzcBijF4dvGc-4VMSx8u8DlRPekDbfSC6BgS7xNYgTbzIB276Uas8cfZknvcbGgwFnH4Pdx_pEwRpNw==', 'Tni_GX-7IzHAcBwRfN0YBbKQDmpjEjOfB0Pmiki-hCg=', 'jWjzoCHRzcBijF4dvGc-4VMRx8u8DlRPekDbfSC6BgROeL8Zf7sjMcBwHBF83RgFspAOamMSM58HQ-aKSL6EKA==' ) assert history == exp_history
def testHistoryDataWithRotationData(): datum = gen.historyGen() vk, sk, did = gen.keyGen() datum[HISTORY]["signer"] = 1 datum[HISTORY]["signers"].append(vk) bHistory = json.dumps(datum[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() signer = signing.signResource(bHistory, gen.key64uToKey(datum[SK1])) rotation = signing.signResource(bHistory, gen.key64uToKey(datum[SK2])) data = { "history": datum[HISTORY], "signatures": { "signer": signer, "rotation": rotation } } history = resp.HistoryData(data) assert history.data == data assert history.bdata == json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode() assert history.body == data["history"] assert history.bbody == json.dumps(data["history"], ensure_ascii=False, separators=(",", ":")).encode() assert history.did == data["history"]["id"] assert history.vk == data["history"]["signers"][1] assert history.signer_sig == signer assert history.rotation_sig == rotation assert history.signature == rotation assert history.valid is True
import pytest try: import simplejson as json except ImportError: import json from ioflo.aio.http import Valet # import didery.routing from diderypy.help import helping as h from diderypy.lib import generating as gen from diderypy.lib import historying as hist history, vk1, sk1, vk2, sk2 = gen.historyGen() vk3, sk3, did3 = gen.keyGen() did = history['id'] url1, url2 = "http://localhost:8080/history", "http://localhost:8000/history" urls = ["http://localhost:8080", "http://localhost:8000"] def testPostHistory(): result = hist.postHistory(history, sk1, urls) assert result[url1].status == 201 assert result[url2].status == 201 def testPostHistoryNoUrls():
def testConsenseResults(): consense = consensing.Consense() datum1 = gen.historyGen() # (history, vk1, sk1, vk2, sk2) datum2 = gen.historyGen() # Test simple majority vk, sk, did = gen.keyGen() datum1[HISTORY]["signer"] = 1 datum1[HISTORY]["signers"].append(vk) bHistory1 = json.dumps(datum1[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() bHistory2 = json.dumps(datum2[HISTORY], ensure_ascii=False, separators=(',', ':')).encode() data = { "http://localhost:8000/history": resp.responseFactory( "http://localhost:8000/history", 200, { "history": datum2[HISTORY], "signatures": { "signer": signing.signResource(bHistory2, gen.key64uToKey( datum2[SK1])) } }), "http://localhost:8080/history": resp.responseFactory( "http://localhost:8080/history", 200, { "history": datum1[HISTORY], "signatures": { "signer": signing.signResource(bHistory1, gen.key64uToKey( datum1[SK1])), "rotation": signing.signResource(bHistory1, gen.key64uToKey( datum1[SK2])) } }), "http://localhost:8081/history": resp.responseFactory( "http://localhost:8081/history", 200, { "history": datum1[HISTORY], "signatures": { "signer": signing.signResource(bHistory1, gen.key64uToKey( datum1[SK1])), "rotation": signing.signResource(bHistory1, gen.key64uToKey( datum1[SK2])) } }) } results = consense.consense(data)[1] urls = [ "http://localhost:8000/history", "http://localhost:8080/history", "http://localhost:8081/history" ] assert len(results) == 3 for url in urls: assert url in results exp_results = { "http://localhost:8000/history": consenseModel.ConsensusResult.VALID, "http://localhost:8080/history": consenseModel.ConsensusResult.VALID, "http://localhost:8081/history": consenseModel.ConsensusResult.VALID } for url, status in exp_results.items(): assert results[url].validation_status == status # Test failed signature validation consense = consensing.Consense() data["http://localhost:8000/history"] = resp.responseFactory( "http://localhost:8000/history", 200, { "history": datum2[HISTORY], "signatures": { "signer": signing.signResource(bHistory2, gen.key64uToKey(datum2[SK2])) } }) results = consense.consense(data)[1] assert len(results) == 3 for url in urls: assert url in results exp_results[ "http://localhost:8000/history"] = consenseModel.ConsensusResult.FAILED for url, status in exp_results.items(): assert results[url].validation_status == status # Test failed request consense = consensing.Consense() data["http://localhost:8000/history"] = resp.responseFactory( "http://localhost:8000/history", 400, { "history": datum2[HISTORY], "signatures": { "signer": signing.signResource(bHistory2, gen.key64uToKey(datum2[SK1])) } }) results = consense.consense(data)[1] assert len(results) == 3 for url in urls: assert url in results exp_results[ "http://localhost:8000/history"] = consenseModel.ConsensusResult.ERROR for url, status in exp_results.items(): assert results[url].validation_status == status