Example #1
0
    def test_set(self):
        # Test an empty set
        self.check_to_broker_and_back(set(), '{}', broker.Data.Type.Set)

        # Test a simple set
        p = set([1, 2, 3])
        d = self.check_to_broker_and_back(p, '{1, 2, 3}', broker.Data.Type.Set)

        for (i, x) in enumerate(d.as_set()):
            self.check_to_broker(x, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(x, i + 1)

        # Test a set that contains various data types
        d = broker.Data(set(['foo', ipaddress.IPv6Address('::1'), None]))
        for (i, x) in enumerate(d.as_set()):
            if i == 1:
                self.check_to_broker(x, 'foo', broker.Data.Type.String)
                self.check_to_py(x, 'foo')
            elif i == 2:
                self.check_to_broker(x, '::1', broker.Data.Type.Address)
                self.check_to_py(x, ipaddress.IPv6Address('::1'))

        # Test some of our own methods on wrapped sets.
        d = broker.Data(set([1, 2, 3])).as_set()
        self.assertEqual(str(d), "Set{1, 2, 3}")
        d.remove(broker.Data(2))
        self.assertEqual(str(d), "Set{1, 3}")
        d.clear()
        self.assertEqual(str(d), "Set{}")
Example #2
0
    def test_table(self):
        # Test an empty table
        self.check_to_broker_and_back({}, '{}', broker.Data.Type.Table)

        # Test a simple table
        p = {"a": 1, "b": 2, "c": 3}
        d = self.check_to_broker_and_back(p, '{a -> 1, b -> 2, c -> 3}',
                                          broker.Data.Type.Table)

        for (i, (k, v)) in enumerate(d.as_table().items()):
            self.check_to_broker(k, ["a", "b", "c"][i],
                                 broker.Data.Type.String)
            self.check_to_py(k, ["a", "b", "c"][i])
            self.check_to_broker(v, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(v, i + 1)

        # Test a table that contains different data types
        p = {True: 42, broker.Port(22, broker.Port.TCP): False}
        d = self.check_to_broker(p, '{T -> 42, 22/tcp -> F}',
                                 broker.Data.Type.Table)

        t = d.as_table()
        self.check_to_broker(t[broker.Data(True)], "42",
                             broker.Data.Type.Integer)
        self.check_to_py(t[broker.Data(True)], 42)
        self.check_to_broker(t[broker.Data(broker.Port(22, broker.Port.TCP))],
                             "F", broker.Data.Type.Boolean)
        self.check_to_py(t[broker.Data(broker.Port(22, broker.Port.TCP))],
                         False)
Example #3
0
    def test_vector(self):
        # Test an empty vector
        self.check_to_broker_and_back((), '()', broker.Data.Type.Vector)

        # Test a simple vector
        d = self.check_to_broker((1, 2, 3), '(1, 2, 3)', broker.Data.Type.Vector)
        # Either a list or a tuple are mapped to a Broker vector.
        d = self.check_to_broker([1, 2, 3], '(1, 2, 3)', broker.Data.Type.Vector)

        for (i, x) in enumerate(d.as_vector()):
            self.check_to_broker(x, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(x, i + 1)

        # Test a vector that contains various data types
        d = broker.Data(['foo', [[True]], ipaddress.IPv6Address('::1'), None])
        v = d.as_vector()
        self.check_to_broker(v[0], 'foo', broker.Data.Type.String)
        self.check_to_py(v[0], 'foo')

        v1 = v[1].as_vector()
        self.check_to_broker(v1, '((T))', broker.Data.Type.Vector)
        self.check_to_py(v[1], ((True,),))
        self.check_to_broker(v1[0], '(T)', broker.Data.Type.Vector)
        self.check_to_py(v1[0], (True,))

        self.check_to_broker(v[2], '::1', broker.Data.Type.Address)
        self.check_to_py(v[2], ipaddress.IPv6Address("::1"))

        self.check_to_broker(v[3], 'nil', broker.Data.Type.Nil)
Example #4
0
    def test_vector(self):
        # Test an empty vector
        self.check_to_broker_and_back([], '[]', broker.Data.Type.Vector)

        # Test a simple vector
        d = self.check_to_broker([1, 2, 3], '[1, 2, 3]',
                                 broker.Data.Type.Vector)

        for (i, x) in enumerate(d.as_vector()):
            self.check_to_broker(x, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(x, i + 1)

        # Test a vector that contains various data types
        d = broker.Data(['foo', [[True]], ipaddress.IPv6Address('::1'), None])
        v = d.as_vector()
        self.check_to_broker(v[0], 'foo', broker.Data.Type.String)
        self.check_to_py(v[0], 'foo')

        v1 = v[1].as_vector()
        self.check_to_broker(v1, '[[T]]', broker.Data.Type.Vector)
        self.check_to_py(v[1], [[True]])
        self.check_to_broker(v1[0], '[T]', broker.Data.Type.Vector)
        self.check_to_py(v1[0], [True])

        self.check_to_broker(v[2], '::1', broker.Data.Type.Address)
        self.check_to_py(v[2], ipaddress.IPv6Address("::1"))

        self.check_to_broker(v[3], 'nil', broker.Data.Type.Nil)
Example #5
0
    def check_to_broker(self, p, s, t):
        assert not isinstance(p, broker.Data)
        b = broker.Data(p)
        #print("[to_broker] ({} / {}) -> ({} / {}) (expected: {} / {})".format(p, type(p), str(b), b.get_type(), s, t))

        if s is not None:
            self.assertEqual(str(b), s)

        self.assertEqual(b.get_type(), t)
        return b
Example #6
0
    def _test_table_impl(self, table_itype, table_otype=None):
        # Common table testing functionality for an input type into Broker and a
        # corresponding output type (table_itype/table_otype). When the output
        # type isn't provided, use the input type:
        table_otype = table_itype if table_otype is None else table_otype

        # Test an empty table
        self.check_to_broker_and_back(table_itype({}), '{}',
                                      broker.Data.Type.Table, table_otype())

        # Test a simple table
        d = {"a": 1, "b": 2, "c": 3}
        pi, po = table_itype(d), table_otype(d)
        d = self.check_to_broker_and_back(pi, '{a -> 1, b -> 2, c -> 3}',
                                          broker.Data.Type.Table, po)

        for (i, (k, v)) in enumerate(d.as_table().items()):
            self.check_to_broker(k, ["a", "b", "c"][i],
                                 broker.Data.Type.String)
            self.check_to_py(k, ["a", "b", "c"][i])
            self.check_to_broker(v, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(v, i + 1)

        # Test a table that contains different data types
        p = table_itype({
            True: 42,
            broker.Port(22, broker.Port.TCP): False,
            (1, 2, 3): [4, 5, 6],
            broker.Count(13): "test",
        })
        d = self.check_to_broker(
            p, '{T -> 42, 13 -> test, 22/tcp -> F, (1, 2, 3) -> (4, 5, 6)}',
            broker.Data.Type.Table)

        t = d.as_table()

        self.check_to_broker(t[broker.Data(True)], "42",
                             broker.Data.Type.Integer)
        self.check_to_py(t[broker.Data(True)], 42)

        self.check_to_broker(t[broker.Data(broker.Port(22, broker.Port.TCP))],
                             "F", broker.Data.Type.Boolean)
        self.check_to_py(t[broker.Data(broker.Port(22, broker.Port.TCP))],
                         False)

        self.check_to_broker(t[broker.Data((1, 2, 3))], "(4, 5, 6)",
                             broker.Data.Type.Vector)
        self.check_to_py(t[broker.Data([1, 2, 3])], (4, 5, 6))

        self.check_to_broker(t[broker.Data(broker.Count(13))], "test",
                             broker.Data.Type.String)
        self.check_to_py(t[broker.Data(broker.Count(13))], "test")
Example #7
0
    def _test_set_impl(self, set_itype, set_otype=None):
        # Common set testing functionality for an input type into Broker and a
        # corresponding output type (set_itype/set_otype). When the output type
        # isn't provided, use the input type:
        set_otype = set_itype if set_otype is None else set_otype

        # Test an empty set
        self.check_to_broker_and_back(set_itype(), '{}', broker.Data.Type.Set,
                                      set_otype())

        # Test a simple set
        pi, po = set_itype([1, 2, 3]), set_otype([1, 2, 3])
        d = self.check_to_broker_and_back(pi, '{1, 2, 3}',
                                          broker.Data.Type.Set, po)

        for (i, x) in enumerate(d.as_set()):
            self.check_to_broker(x, str(i + 1), broker.Data.Type.Integer)
            self.check_to_py(x, i + 1)

        # Test a set that contains various data types
        d = broker.Data(set_itype(['foo', ipaddress.IPv6Address('::1'), None]))
        for (i, x) in enumerate(d.as_set()):
            if i == 1:
                self.check_to_broker(x, 'foo', broker.Data.Type.String)
                self.check_to_py(x, 'foo')
            elif i == 2:
                self.check_to_broker(x, '::1', broker.Data.Type.Address)
                self.check_to_py(x, ipaddress.IPv6Address('::1'))

        # Test some of our own methods on wrapped sets.
        d = broker.Data(set_itype([1, 2, 3])).as_set()
        self.assertEqual(str(d), "Set{1, 2, 3}")
        d.remove(broker.Data(2))
        self.assertEqual(str(d), "Set{1, 3}")
        d.clear()
        self.assertEqual(str(d), "Set{}")