示例#1
0
def testValidateDataValidSigsConflictingData():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder()).withPort(8000)
    response2 = builder.DideryResponseBuilder(builder.SignedHistoryBuilder())
    response3 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation().withRotation()).withPort(
            8081)

    data = {
        "http://localhost:8000/history": response1.build(),
        "http://localhost:8080/history": response2.build(),
        "http://localhost:8081/history": response3.build()
    }

    consense.validateData(data)

    history1 = response1.historyBuilder
    history3 = response3.historyBuilder

    assert consense.valid_data == {
        history1.signerSig: history1.build().data,
        history3.rotationSig: history3.build().data
    }
    assert history1.signerSig in consense.valid_match_counts
    assert consense.valid_match_counts[history1.signerSig] == 2
    assert history3.rotationSig in consense.valid_match_counts
    assert consense.valid_match_counts[history3.rotationSig] == 1
示例#2
0
def testConsenseAllEqual():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation()).withPort(8000)

    data = {
        "http://localhost:8000/history": response1.withPort(8000).build(),
        "http://localhost:8080/history": response1.withPort(8080).build(),
        "http://localhost:8081/history": response1.withPort(8081).build()
    }

    signature = response1.historyBuilder.rotationSig

    assert consense.consense(data)[0] == response1.historyBuilder.build().data
    assert signature in consense.valid_data
    assert signature in consense.valid_match_counts
    assert consense.valid_match_counts[signature] == 3
示例#3
0
def testValidateDataWithTimeOut():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withEmptyData()).withPort(
            8000).withStatus(0)
    response2 = builder.DideryResponseBuilder(builder.SignedHistoryBuilder())

    data = {
        "http://localhost:8000/history": response1.build(),
        "http://localhost:8080/history": response2.build()
    }

    consense.validateData(data)

    history2 = response2.historyBuilder

    assert consense.valid_data == {history2.signerSig: history2.build().data}
    assert consense.valid_match_counts == {history2.signerSig: 1}
示例#4
0
def testConsenseIncompleteMajority():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withInvalidSignerSignature()).withPort(
            8000)
    response2 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation())
    response3 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation().withRotation()).withPort(
            8081)

    data = {
        "http://localhost:8000/history": response1.build(),
        "http://localhost:8080/history": response2.build(),
        "http://localhost:8081/history": response3.build()
    }

    assert consense.consense(data)[0] is None
示例#5
0
def testValidateDataInvalidSigsIncompleteMajority():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withInvalidSignerSignature()).withPort(
            8000)
    response2 = builder.DideryResponseBuilder(builder.SignedHistoryBuilder())

    data = {
        "http://localhost:8000/history": response1.build(),
        "http://localhost:8080/history": response2.build()
    }

    consense.validateData(data)

    history2 = response2.historyBuilder
    assert consense.valid_data == {
        history2.signerSig: builder.SignedHistoryBuilder().build().data
    }
    assert consense.valid_match_counts == {history2.signerSig: 1}
示例#6
0
def test50_50Split():
    consense = consensing.Consense()

    response1 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder()).withPort(8000)
    response2 = builder.DideryResponseBuilder(builder.SignedHistoryBuilder())
    response3 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation().withRotation()).withPort(
            8081)
    response4 = builder.DideryResponseBuilder(
        builder.SignedHistoryBuilder().withRotation().withRotation()).withPort(
            8001)

    data = {
        "http://localhost:8000/history": response1.build(),
        "http://localhost:8080/history": response2.build(),
        "http://localhost:8081/history": response3.build(),
        "http://localhost:8001/history": response4.build()
    }

    assert consense.consense(data)[0] is None
示例#7
0
def testValidateDataEmptyData():
    consense = consensing.Consense()
    consense.validateData({})

    assert consense.valid_data == {}
    assert consense.valid_match_counts == {}
示例#8
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