Пример #1
0
def test_bounty_settle_attempt(db):
    b = BountyComponent(Parent())
    b.polyswarm.settle_bounty = mock.Mock()
    b.polyswarm.bounty_assertions = mock.Mock()
    b.polyswarm.bounty_assertions.return_value = []

    guid404 = "c8700c42-5833-4eb9-9330-6128574cbab8"
    guid = "f85fb0ed-7cd3-4f82-8627-e7e25b832336"
    truth_value = "[true]"
    b_id, _ = _create_bounty(guid, truth_value)
    b_id404, _ = _create_bounty(guid404, truth_value)

    b.pending_bounties.add(guid)
    b.pending_bounties.add(guid404)

    try:
        b.settle_bounty_attempt(guid, truth_value)
        assert guid not in b.pending_bounties
        _bounty_check_state(b_id, True, True)
        b.polyswarm.settle_bounty.assert_called_with(guid, truth_value)

        b.polyswarm.settle_bounty.reset_mock()
        b.polyswarm.settle_bounty.side_effect = PolySwarmError(
            404, 'Does not exist', 'NOT FOUND')

        # TODO: should have a marking
        b.settle_bounty_attempt(guid404, truth_value)
        assert guid404 not in b.pending_bounties
        _bounty_check_state(b_id404, True, True)
        b.polyswarm.settle_bounty.assert_called_with(guid404, truth_value)

    finally:
        db_clear()
Пример #2
0
def test_expire_verdicts(dispatch_event, db):
    v = VerdictComponent(Parent())
    verdicts.analysis_backends = {
        u"cuckoo": AnalysisBackend(u"cuckoo", True, 1),
        u"zer0m0n": AnalysisBackend(u"zer0m0n", False, 1),
    }

    expire_at = datetime.datetime.utcnow() - datetime.timedelta(hours=1)

    mixed = artifact({
        u"cuckoo": {
            "status": JOB_STATUS_PENDING,
            "expires": expire_at
        },
        u"zer0m0n": {
            "status": JOB_STATUS_DONE,
            "expires": expire_at
        }
    })
    with mixed:
        v.expire_verdicts()
        assert dispatch_event.called

        s = DbSession()
        try:
            for av in s.query(DbArtifactVerdict):
                if av.backend == "cuckoo":
                    assert av.status == JOB_STATUS_FAILED
                else:
                    assert av.status == JOB_STATUS_DONE
        finally:
            s.close()
    db_clear()
Пример #3
0
def test_settle_manual(db):
    bad_guid = "0f0f0f0f-bbbb-cccc-dddd-000000000001"
    guid1 = "aaaaaaaa-bbbb-cccc-dddd-000000000001"
    guid2 = "aaaaaaaa-bbbb-cccc-dddd-000000000002"
    guid3 = "aaaaaaaa-bbbb-cccc-dddd-000000000003"
    truth_value = "[true]"
    _create_bounty(guid1, truth_value, True)
    _create_bounty(guid2, None, False, 1)
    _create_bounty(guid3, None, False, 2)
    try:
        with pytest.raises(KeyError):
            bounty_settle_manual(bad_guid, [True])
        with pytest.raises(ValueError):
            bounty_settle_manual(guid1, [True])
        # Should not raise
        bounty_settle_manual(guid2, [True])
        with pytest.raises(ValueError):
            bounty_settle_manual(guid3, [True])
    finally:
        db_clear()
Пример #4
0
def test_resubmit_pending_settle(dispatch_event, db):
    guid = "dda16db6-89eb-453b-8d0a-abababababab"
    truth_value = "[true]"
    _create_bounty(guid, truth_value, assertions=[])

    b = BountyComponent(Parent())

    try:
        b.resubmit_pending_settle()
        assert not dispatch_event.called

        b.cur_block = 1000
        b.resubmit_pending_settle()
        assert not dispatch_event.called

        b.cur_block = 1001
        b.resubmit_pending_settle()
        dispatch_event.assert_called_with("bounty_settle_attempt", guid,
                                          truth_value)
    finally:
        db_clear()
Пример #5
0
def test_bounty_with_manifest(dispatch_event, ipfs_download, ipfs_json):
    b = BountyComponent(Parent())
    bounty = {
        "resolved": True,
        "amount": "10000",
        "author": "0xdeadbeef",
        "expiration": 222,
        "guid": "0d6a0d07-8424-4972-82fc-550266ff4da5",
        "uri": "Q1111",
        "verdicts": "[False]"
    }
    bounties.analysis_backends = {
        "cuckoo": AnalysisBackend("cuckoo", True, 1),
        "zer0m0n": AnalysisBackend("zer0m0n", True, 1),
    }
    try:
        # Unresolved ignored
        ipfs_json.return_value = {"result": []}
        b.bounty_with_manifest(bounty)
        _no_such_bounty(bounty["guid"])

        # Empty manifest ignored
        bounty["resolved"] = False
        b.bounty_with_manifest(bounty)
        _no_such_bounty(bounty["guid"])

        ipfs_json.return_value = {
            "result": [{
                "hash": "Q12312312",
                "name": "malwr.exe"
            }]
        }
        b.bounty_with_manifest(bounty)
        _bounty_check_state(bounty["guid"], False, False)
    finally:
        db_clear()
Пример #6
0
def test_verdict_job_submit(dispatch_event, db):
    v = VerdictComponent(Parent())
    verdicts.analysis_backends = {
        u"cuckoo": AnalysisBackend(u"cuckoo", True, 1),
        u"zer0m0n": AnalysisBackend(u"zer0m0n", False, 1),
        u"cuckscan": AnalysisBackend(u"cuckscan", False, 1),
    }

    verdicts.analysis_backends[u"cuckoo"].submit_artifact = lambda a: None
    verdicts.analysis_backends[u"zer0m0n"].submit_artifact = lambda a: {}
    verdicts.analysis_backends[u"cuckscan"].submit_artifact = lambda a: 1

    pending = artifact({
        u"cuckoo": {
            "status": JOB_STATUS_SUBMITTING
        },
        u"zer0m0n": {
            "status": JOB_STATUS_SUBMITTING
        },
        u"cuckscan": {
            "status": JOB_STATUS_SUBMITTING
        }
    })
    with pending as x:
        v.verdict_job_submit(x["artifact_id"], x["jobs"])
        assert dispatch_event.called
        s = DbSession()
        try:
            for av in s.query(DbArtifactVerdict):
                if av.backend == "cuckscan":
                    assert av.status == JOB_STATUS_DONE
                else:
                    assert av.status == JOB_STATUS_PENDING
        finally:
            s.close()
    db_clear()