Example #1
0
def test_const_object_spec_all_types(cleanup_stix2):
    spec = {
        "const": {
            "type": "my-type",
            "int": 1,
            "number": 0.5,
            "boolean": False,
            "string": "hello, world",
            "array": [1, 2, 3],
            "object": {
                "subprop1": 1,
                "subprop2": "two"
            }
        }
    }

    stix2_register_custom(spec, "my-type", "2.1")

    # now it should be able to parse our object
    obj = stix2.parse(spec["const"], version="2.1")

    assert obj["type"] == "my-type"
    assert obj["int"] == 1
    assert obj["number"] == 0.5
    assert obj["boolean"] is False
    assert obj["string"] == "hello, world"
    assert obj["array"] == [1, 2, 3]
    assert obj["object"] == {"subprop1": 1, "subprop2": "two"}
Example #2
0
def test_nonconst_object_spec_all_types(cleanup_stix2):
    spec = {
        "type": "object",
        "properties": {
            "type": "my-type",
            "int": {"type": "integer"},
            "number": {"type": "number"},
            "boolean": {"type": "boolean"},
            "string": {"type": "string"},
            "array": {"type": "array", "items": {"type": "integer"}},
            "object": {
                "type": "object",
                "properties": {
                    "subprop1": {"type": "integer"},
                    "subprop2": {"type": "string"}
                }
            }
        }
    }

    stix2_register_custom(spec, "my-type", "2.1")

    gen = stix2generator.create_object_generator()
    random_dict = gen.generate_from_spec(spec)
    stix_obj = stix2.parse(random_dict, version="2.1")

    # Make sure all props from the dict came through in the stix2 object
    for prop, value in random_dict.items():
        assert stix_obj[prop] == value
Example #3
0
def test_stix2_object_result(cleanup_stix2):
    spec = {
        "Foobar": {
            "type": "object",
            "import": "common-properties",
            "required": ["id", "some-property", "type"],
            "properties": {
                "type": "x-foobar",
                "id": {
                    "type": "string",
                    "semantics": "stix-id",
                    "stix-type": "x-foobar"
                },
                "some-property": {
                    "type": "string",
                    "semantics": "word"
                }
            }
        }
    }

    stix2_register_custom(spec['Foobar'], "x-foobar", "2.1")
    processor = stix2generator.create_default_language_processor(
        extra_specs=spec, stix_version="2.1"
    )
    obj = processor.build_graph("Foobar.")[0]

    assert isinstance(obj, stix2.base._STIXBase)
Example #4
0
def test_non_object_spec_error():
    spec = [1, 2, 3]

    # We're registering a custom STIX object, so the spec needs to be for an
    # object!
    with pytest.raises(stix2generator.exceptions.IllegalSTIXObjectSpecType):
        stix2_register_custom(spec, "foo", "2.1")
Example #5
0
def test_prop_type_error():
    spec = {
        "const": {
            "nullprop": None
        }
    }

    with pytest.raises(stix2generator.exceptions.IllegalSTIXObjectPropertyType):
        stix2_register_custom(spec, "null", "2.1")
Example #6
0
def test_heterogenous_list_error():
    spec = {
        "type": "object",
        "properties": {
            "heterolist": [1, "foo", True]
        }
    }

    # can't infer list element type from a heterogenous list
    with pytest.raises(stix2generator.exceptions.HeterogenousListError):
        stix2_register_custom(spec, "foo", "2.1")
Example #7
0
def test_empty_list_error():
    spec = {
        "type": "object",
        "properties": {
            "emptylist": []
        }
    }

    # can't infer list element type from an empty list
    with pytest.raises(stix2generator.exceptions.EmptyListError):
        stix2_register_custom(spec, "foo", "2.1")