def test_composite_datastore_related_to_raises_error():
    with pytest.raises(AttributeError) as excinfo:
        CompositeDataSource().related_to(
            obj="indicator--00000000-0000-4000-8000-000000000001",
            target_only=True,
        )
    assert "CompositeDataSource has no data sources" == str(excinfo.value)
Beispiel #2
0
def test_add_remove_composite_datasource():
    cds = CompositeDataSource()
    ds1 = MemorySource()
    ds2 = MemorySource()
    ds3 = MemorySink()

    with pytest.raises(TypeError) as excinfo:
        cds.add_data_sources([ds1, ds2, ds1, ds3])
    assert str(excinfo.value) == ("DataSource (to be added) is not of type "
                                  "stix2.DataSource. DataSource type is '<class 'stix2.datastore.memory.MemorySink'>'")

    cds.add_data_sources([ds1, ds2, ds1])

    assert len(cds.get_all_data_sources()) == 2

    cds.remove_data_sources([ds1.id, ds2.id])

    assert len(cds.get_all_data_sources()) == 0
def test_composite_datasource_operations(stix_objs1, stix_objs2):
    BUNDLE1 = dict(
        id="bundle--%s" % make_id(),
        objects=stix_objs1,
        spec_version="2.0",
        type="bundle",
    )
    cds1 = CompositeDataSource()
    ds1_1 = MemorySource(stix_data=BUNDLE1)
    ds1_2 = MemorySource(stix_data=stix_objs2)

    cds2 = CompositeDataSource()
    ds2_1 = MemorySource(stix_data=BUNDLE1)
    ds2_2 = MemorySource(stix_data=stix_objs2)

    cds1.add_data_sources([ds1_1, ds1_2])
    cds2.add_data_sources([ds2_1, ds2_2])

    indicators = cds1.all_versions("indicator--00000000-0000-4000-8000-000000000001")

    # In STIX_OBJS2 changed the 'modified' property to a later time...
    assert len(indicators) == 3

    cds1.add_data_sources([cds2])

    indicator = cds1.get("indicator--00000000-0000-4000-8000-000000000001")

    assert indicator["id"] == "indicator--00000000-0000-4000-8000-000000000001"
    assert indicator["modified"] == parse_into_datetime("2017-01-31T13:49:53.935Z")
    assert indicator["type"] == "indicator"

    query1 = [
        Filter("type", "=", "indicator"),
    ]

    query2 = [
        Filter("valid_from", "=", "2017-01-27T13:49:53.935382Z"),
    ]

    cds1.filters.add(query2)

    results = cds1.query(query1)

    # STIX_OBJS2 has indicator with later time, one with different id, one with
    # original time in STIX_OBJS1
    assert len(results) == 4

    indicator = cds1.get("indicator--00000000-0000-4000-8000-000000000001")

    assert indicator["id"] == "indicator--00000000-0000-4000-8000-000000000001"
    assert indicator["modified"] == parse_into_datetime("2017-01-31T13:49:53.935Z")
    assert indicator["type"] == "indicator"

    results = cds1.all_versions("indicator--00000000-0000-4000-8000-000000000001")
    assert len(results) == 3

    # Since we have filters already associated with our CompositeSource providing
    # nothing returns the same as cds1.query(query1) (the associated query is query2)
    results = cds1.query([])
    assert len(results) == 4
Beispiel #4
0
def test_composite_datastore_add_data_source_raises_error():
    with pytest.raises(TypeError) as excinfo:
        ind = "indicator--00000000-0000-4000-8000-000000000001"
        CompositeDataSource().add_data_source(ind)
    assert "DataSource (to be added) is not of type stix2.DataSource. DataSource type is '{}'".format(type(ind)) == str(excinfo.value)
Beispiel #5
0
def test_composite_datastore_query_raises_error():
    with pytest.raises(AttributeError) as excinfo:
        CompositeDataSource().query([Filter("type", "=", "indicator")])
    assert "CompositeDataSource has no data sources" == str(excinfo.value)
Beispiel #6
0
def test_composite_datastore_all_versions_raises_error():
    with pytest.raises(AttributeError) as excinfo:
        CompositeDataSource().all_versions("indicator--00000000-0000-4000-8000-000000000001")
    assert "CompositeDataSource has no data sources" == str(excinfo.value)
Beispiel #7
0
def test_composite_datastore_no_datasource():
    cds = CompositeDataSource()
    with pytest.raises(AttributeError) as excinfo:
        cds.get("indicator--00000000-0000-4000-8000-000000000001")
    assert 'CompositeDataSource has no data source' in str(excinfo.value)
Beispiel #8
0
def test_composite_datastore_add_data_source_raises_error():
    with pytest.raises(TypeError) as excinfo:
        ind = "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f"
        CompositeDataSource().add_data_source(ind)
    assert "DataSource (to be added) is not of type stix2.DataSource. DataSource type is '{}'".format(type(ind)) == str(excinfo.value)
Beispiel #9
0
def test_composite_datastore_related_to_raises_error():
    with pytest.raises(AttributeError) as excinfo:
        CompositeDataSource().related_to(obj="indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f",
                                         target_only=True)
    assert "CompositeDataSource has no data sources" == str(excinfo.value)
Beispiel #10
0
def test_composite_datastore_all_versions_raises_error():
    with pytest.raises(AttributeError) as excinfo:
        CompositeDataSource().all_versions("indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")
    assert "CompositeDataSource has no data sources" == str(excinfo.value)
Beispiel #11
0
def test_composite_datastore_no_datasource():
    cds = CompositeDataSource()
    with pytest.raises(AttributeError) as excinfo:
        cds.get("indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")
    assert 'CompositeDataSource has no data source' in str(excinfo.value)
def test_composite_datasource_operations(stix_objs1, stix_objs2):
    BUNDLE1 = dict(id="bundle--%s" % make_id(),
                   objects=stix_objs1,
                   spec_version="2.0",
                   type="bundle")
    cds1 = CompositeDataSource()
    ds1_1 = MemorySource(stix_data=BUNDLE1)
    ds1_2 = MemorySource(stix_data=stix_objs2)

    cds2 = CompositeDataSource()
    ds2_1 = MemorySource(stix_data=BUNDLE1)
    ds2_2 = MemorySource(stix_data=stix_objs2)

    cds1.add_data_sources([ds1_1, ds1_2])
    cds2.add_data_sources([ds2_1, ds2_2])

    indicators = cds1.all_versions(
        "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")

    # In STIX_OBJS2 changed the 'modified' property to a later time...
    assert len(indicators) == 2

    cds1.add_data_sources([cds2])

    indicator = cds1.get("indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")

    assert indicator["id"] == "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f"
    assert indicator["modified"] == "2017-01-31T13:49:53.935Z"
    assert indicator["type"] == "indicator"

    query1 = [Filter("type", "=", "indicator")]

    query2 = [Filter("valid_from", "=", "2017-01-27T13:49:53.935382Z")]

    cds1.filters.add(query2)

    results = cds1.query(query1)

    # STIX_OBJS2 has indicator with later time, one with different id, one with
    # original time in STIX_OBJS1
    assert len(results) == 3

    indicator = cds1.get("indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")

    assert indicator["id"] == "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f"
    assert indicator["modified"] == "2017-01-31T13:49:53.935Z"
    assert indicator["type"] == "indicator"

    # There is only one indicator with different ID. Since we use the same data
    # when deduplicated, only two indicators (one with different modified).
    results = cds1.all_versions(
        "indicator--d81f86b9-975b-bc0b-775e-810c5ad45a4f")
    assert len(results) == 2

    # Since we have filters already associated with our CompositeSource providing
    # nothing returns the same as cds1.query(query1) (the associated query is query2)
    results = cds1.query([])
    assert len(results) == 3