示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
def testKey64uToKey():
    vk64 = "jTWovL2a9buw0IhMr8qW5gNEAkBOj6k3U5zpO0nYykE="
    vk = gen.key64uToKey(vk64)

    exp_vk = b'\x8d5\xa8\xbc\xbd\x9a\xf5\xbb\xb0\xd0\x88L\xaf\xca\x96\xe6\x03D\x02@N\x8f\xa97S\x9c\xe9;I\xd8\xcaA'

    assert vk == exp_vk
示例#5
0
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
示例#6
0
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
示例#7
0
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
示例#8
0
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
示例#9
0
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