Esempio n. 1
0
def test_environment_add_filters():
    env = stix2.Environment(factory=stix2.ObjectFactory())
    env.add_filters([INDICATOR_ID])
    env.add_filter(INDICATOR_ID)
Esempio n. 2
0
def test_environment_datastore_and_no_object_factory():
    # Uses a default object factory
    env = stix2.Environment(store=stix2.MemoryStore())
    ind = env.create(stix2.Indicator, id=INDICATOR_ID, **INDICATOR_KWARGS)
    assert ind.id == INDICATOR_ID
Esempio n. 3
0
def test_non_existent_config_for_object():
    r1 = stix2.v21.Report(id=REPORT_ID, **REPORT_KWARGS)
    r2 = stix2.v21.Report(id=REPORT_ID, **REPORT_KWARGS)
    assert stix2.Environment().object_similarity(r1, r2) == 0.0
Esempio n. 4
0
def test_environment_datastore_and_sink():
    with pytest.raises(ValueError) as excinfo:
        stix2.Environment(factory=stix2.ObjectFactory(),
                          store=stix2.MemoryStore(),
                          sink=stix2.MemorySink)
    assert 'Data store already provided' in str(excinfo.value)
Esempio n. 5
0
def test_related_to_by_source(ds):
    env = stix2.Environment(store=ds)
    resp = env.related_to(MALWARE_ID, source_only=True)

    assert len(resp) == 1
    assert resp[0]['id'] == IDENTITY_ID
def test_graph_equivalence_with_duplicate_graph(ds):
    prop_scores = {}
    env = stix2.Environment().graph_equivalence(ds, ds, prop_scores)
    assert env is True
    assert round(prop_scores["matching_score"]) == 800
    assert round(prop_scores["len_pairs"]) == 8
Esempio n. 7
0
def test_object_similarity_location_with_no_latlong():
    loc_kwargs = dict(country="US", administrative_area="US-DC")
    loc1 = stix2.v21.Location(id=LOCATION_ID, **LOCATION_KWARGS)
    loc2 = stix2.v21.Location(id=LOCATION_ID, **loc_kwargs)
    env = stix2.Environment().object_similarity(loc1, loc2)
    assert round(env) != 100
Esempio n. 8
0
def test_relationships_by_target_and_source(ds):
    env = stix2.Environment(store=ds)
    with pytest.raises(ValueError) as excinfo:
        env.relationships(MALWARE_ID, target_only=True, source_only=True)

    assert 'not both' in str(excinfo.value)
Esempio n. 9
0
def test_object_similarity_on_same_indicator():
    ind1 = stix2.v21.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
    ind2 = stix2.v21.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
    env = stix2.Environment().object_similarity(ind1, ind2)
    assert round(env) == 100
Esempio n. 10
0
def test_object_similarity_on_same_location1():
    location_kwargs = dict(latitude=45, longitude=179)
    loc1 = stix2.v21.Location(id=LOCATION_ID, **location_kwargs)
    loc2 = stix2.v21.Location(id=LOCATION_ID, **location_kwargs)
    env = stix2.Environment().object_similarity(loc1, loc2)
    assert round(env) == 100
Esempio n. 11
0
def test_object_similarity_on_same_identity1():
    iden1 = stix2.v21.Identity(id=IDENTITY_ID, **IDENTITY_KWARGS)
    iden2 = stix2.v21.Identity(id=IDENTITY_ID, **IDENTITY_KWARGS)
    env = stix2.Environment().object_similarity(iden1, iden2)
    assert round(env) == 100
Esempio n. 12
0
def test_object_similarity_on_same_campaign1():
    camp1 = stix2.v21.Campaign(id=CAMPAIGN_ID, **CAMPAIGN_KWARGS)
    camp2 = stix2.v21.Campaign(id=CAMPAIGN_ID, **CAMPAIGN_KWARGS)
    env = stix2.Environment().object_similarity(camp1, camp2)
    assert round(env) == 100
def test_no_datastore_fallsback_list_based_check_for_refs_check():
    r1 = stix2.v21.Report(id=REPORT_ID, **REPORT_KWARGS)
    r2 = stix2.v21.Report(id=REPORT_ID, **REPORT_KWARGS)
    prop_scores = {}
    assert stix2.Environment().object_similarity(r1, r2, prop_scores) == 100.0
    assert prop_scores["object_refs"]["check_type"] == "partial_list_based"
Esempio n. 14
0
def test_creator_of_no_created_by_ref():
    env = stix2.Environment(store=stix2.MemoryStore())
    ind = env.create(stix2.Indicator, **INDICATOR_KWARGS)
    creator = env.creator_of(ind)
    assert creator is None
Esempio n. 15
0
def test_object_similarity_on_same_malware():
    malw1 = stix2.v21.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
    malw2 = stix2.v21.Malware(id=MALWARE_ID, **MALWARE_KWARGS)
    env = stix2.Environment().object_similarity(malw1, malw2)
    assert round(env) == 100
Esempio n. 16
0
def test_relationships_by_source(ds):
    env = stix2.Environment(store=ds)
    resp = env.relationships(MALWARE_ID, source_only=True)

    assert len(resp) == 1
    assert resp[0]['id'] == RELATIONSHIP_IDS[1]
Esempio n. 17
0
def test_object_similarity_on_same_threat_actor1():
    ta1 = stix2.v21.ThreatActor(id=THREAT_ACTOR_ID, **THREAT_ACTOR_KWARGS)
    ta2 = stix2.v21.ThreatActor(id=THREAT_ACTOR_ID, **THREAT_ACTOR_KWARGS)
    env = stix2.Environment().object_similarity(ta1, ta2)
    assert round(env) == 100
Esempio n. 18
0
def test_related_to_no_id(ds):
    env = stix2.Environment(store=ds)
    mal = {"type": "malware", "name": "some variant"}
    with pytest.raises(ValueError) as excinfo:
        env.related_to(mal)
    assert "object has no 'id' property" in str(excinfo.value)
Esempio n. 19
0
def test_object_similarity_on_same_tool():
    tool1 = stix2.v21.Tool(id=TOOL_ID, **TOOL_KWARGS)
    tool2 = stix2.v21.Tool(id=TOOL_ID, **TOOL_KWARGS)
    env = stix2.Environment().object_similarity(tool1, tool2)
    assert round(env) == 100
def test_environment_source_and_sink():
    ind = stix2.v21.Indicator(id=INDICATOR_ID, **INDICATOR_KWARGS)
    env = stix2.Environment(source=stix2.MemorySource([ind]), sink=stix2.MemorySink([ind]))
    assert env.get(INDICATOR_ID).indicator_types[0] == 'malicious-activity'
def test_graph_similarity_with_duplicate_graph(ds):
    prop_scores = {}
    env = stix2.Environment().graph_similarity(ds, ds, prop_scores)
    assert round(env) == 100
    assert round(prop_scores["matching_score"]) == 800
    assert round(prop_scores["len_pairs"]) == 8