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 testOtpData(): vk, sk, did = gen.keyGen() otp_data = { "id": did, "blob": "AeYbsHot0pmdWAcgTo5sD8iAuSQAfnH5U6wiIGpVNJQQoYKBYrPPxAoIc1i5SHCIDS8KFFgf8i0tDq8XGizaCgo9yju" "KHHNJZFi0QD9K6Vpt6fP0XgXlj8z_4D-7s3CcYmuoWAh6NVtYaf_GWw_2sCrHBAA2mAEsml3thLmu50Dw" } botp_data = json.dumps(otp_data, ensure_ascii=False, separators=(",", ":")).encode() signer = signing.signResource(botp_data, gen.key64uToKey(sk)) data = {"otp_data": otp_data, "signatures": {"signer": signer}} otp = resp.OtpData(data) assert otp.data == data assert otp.bdata == json.dumps(data, ensure_ascii=False, separators=(",", ":")).encode() assert otp.body == data["otp_data"] assert otp.bbody == json.dumps(data["otp_data"], ensure_ascii=False, separators=(",", ":")).encode() assert otp.did == did assert otp.vk == vk assert otp.signature == signer assert otp.valid is True
def testKeyGenWithSeed(): seed = b'\x8dh\xf3\xa0!\xd1\xcd\xc0b\x8c^\x1d\xbcg>\xe1S\x12\xc7\xcb\xbc\x0eTOz@\xdb} \xba\x06\x04' vk, sk, did = gen.keyGen(seed) exp_vk = "u8TWIE28yAdu-lGrPHH2ZJ73GxoMBZx-D3cf6RMEaTc=" exp_sk = "jWjzoCHRzcBijF4dvGc-4VMSx8u8DlRPekDbfSC6BgS7xNYgTbzIB276Uas8cfZknvcbGgwFnH4Pdx_pEwRpNw==" assert vk == exp_vk assert sk == exp_sk
def testResponseFactoryWtihOtpData(): vk, sk, did = gen.keyGen() otp_data = { "id": did, "blob": "AeYbsHot0pmdWAcgTo5sD8iAuSQAfnH5U6wiIGpVNJQQoYKBYrPPxAoIc1i5SHCIDS8KFFgf8i0tDq8XGizaCgo9yju" "KHHNJZFi0QD9K6Vpt6fP0XgXlj8z_4D-7s3CcYmuoWAh6NVtYaf_GWw_2sCrHBAA2mAEsml3thLmu50Dw" } botp_data = json.dumps(otp_data, ensure_ascii=False, separators=(",", ":")).encode() signer = signing.signResource(botp_data, gen.key64uToKey(sk)) data = {"otp_data": otp_data, "signatures": {"signer": signer}} response = resp.responseFactory("", 200, data) assert type(response) == resp.DideryResponse assert type(response.response) == resp.OtpData
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.lib import generating as gen from diderypy.lib import otping as otp vk, sk, did = gen.keyGen() otpData = { "id": did, "blob": "AeYbsHot0pmdWAcgTo5sD8iAuSQAfnH5U6wiIGpVNJQQoYKBYrPPxAoIc1i5SHCIDS8KFFgf8i0tDq8XGizaCgo9yjuKHHNJZFi0QD9K" "6Vpt6fP0XgXlj8z_4D-7s3CcYmuoWAh6NVtYaf_GWw_2sCrHBAA2mAEsml3thLmu50Dw" } url1, url2 = "http://localhost:8080/blob", "http://localhost:8000/blob" urls = ["http://localhost:8080", "http://localhost:8000"] def testPostOtpBlob(): result = otp.postOtpBlob(otpData, sk, urls) assert result[url1].status == 201
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(): with pytest.raises(ValueError) as ex:
def testKeyGen(): vk, sk, did = gen.keyGen() assert vk assert sk
def testDidGen64(): vk, sk, did = gen.keyGen() did = didering.didGen64(vk) assert did == "did:dad:{}".format(vk)
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