Exemplo n.º 1
0
    def setUp(self):
        self.maxDiff = 10000

        heartbleed = SubRecord(
            {  # with explicit proto field indices
                "heartbeat_support":
                Boolean(pr_index=11),
                "heartbleed_vulnerable":
                Boolean(category="Vulnerabilities", pr_ignore=True),
                "timestamp":
                DateTime(pr_index=10)
            },
            pr_index=77)
        self.host = Record({
            "ipstr":
            IPv4Address(required=True, examples=["8.8.8.8"], pr_index=1),
            "ip":
            Unsigned32BitInteger(doc="The IP Address of the host", pr_index=2),
            Port(443):
            SubRecord({
                "tls": String(pr_index=1),
                "heartbleed": heartbleed
            },
                      category="heartbleed",
                      pr_index=3),
            "tags":
            ListOf(String(), pr_index=47)
        })
Exemplo n.º 2
0
    def setUp(self):
        self.maxDiff = 10000

        Child = SubRecordType(
            {
                "foo": Boolean(),
                "bar": Boolean(validation_policy="error"),
            },
            validation_policy="error")
        self.record = Record({
            "a": Child(validation_policy="error"),
            "b": Child(validation_policy="warn"),
            "c": Child(validation_policy="ignore"),
            "d": Child(validation_policy="inherit"),
        })
Exemplo n.º 3
0
    def test_multiple_subrecord_types(self):
        A = SubRecordType({
            "first": String(),
        }, type_name="A")
        B = SubRecordType({
            "second": Boolean(),
        }, type_name="B")

        a = A()
        self.assertIn("first", a.definition)
        b = B()
        self.assertIn("second", b.definition)
        a = A()
        self.assertIn("first", a.definition)
Exemplo n.º 4
0
    def test_subrecord_type(self):
        A = SubRecordType({
            "string": String(),
            "boolean": Boolean(),
        })

        first = A()
        second = A()

        # The class returned by SubRecordType() should be constructable into
        # unique objects
        self.assertIsNot(first, second)
        self.assertTrue(issubclass(A, SubRecord))
        self.assertIsInstance(first, A)
        self.assertIsInstance(second, A)

        # Check the properties aren't shared
        self.assertIsNone(first.definition['string'].doc)
        self.assertIsNone(second.definition['string'].doc)
        first.definition['string'].doc = "hello"
        self.assertIsNone(second.definition['string'].doc)
Exemplo n.º 5
0
    def test_subrecord_type_extends(self):
        S = SubRecordType({
            "provided": Boolean(),
        })

        extended_type = SubRecord(
            {
                "property": String(),
                "record": SubRecord({
                    "another": String(),
                }),
            },
            extends=S())

        base = S()
        extends = extended_type
        self.assertNotIsInstance(extends, S)
        self.assertFalse(base.definition['provided'].exclude)
        self.assertFalse(extended_type.definition['provided'].exclude)
        base.definition['provided'].exclude = ['bigquery']
        self.assertEqual(['bigquery'], base.definition['provided'].exclude)
        self.assertFalse(extended_type.definition['provided'].exclude)
Exemplo n.º 6
0
    def test_child_subtree_overrides_and_inherits(self):
        schema = Record(
            {
                Port(445):
                SubRecord({
                    "smb":
                    SubRecord({"banner": SubRecord({"smb_v1": Boolean()})},
                              validation_policy="error")
                })
            },
            validation_policy="warn")

        doc = {
            "445": {
                "smb": {
                    "banner": {
                        "smb_v1": True,
                        "metadata": {},
                    }
                }
            }
        }
        self.assertRaises(DataValidationException,
                          lambda: schema.validate(doc))
Exemplo n.º 7
0
from zschema.keys import Port
from zschema.compounds import ListOf, Record, SubRecord
from zschema.leaves import Boolean, DateTime, IPv4Address, String, Unsigned32BitInteger

heartbleed = SubRecord({
    "heartbeat_support": Boolean(),
    "heartbleed_vulnerable": Boolean(),
    "timestamp": DateTime()
})

host = Record({
    "ipstr": IPv4Address(required=True),
    "ip": Unsigned32BitInteger(),
    Port(443): SubRecord({
        "tls": String(),
        "heartbleed": heartbleed
    }),
    "tags": ListOf(String())
})