Exemplo n.º 1
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, (1,2,3): [4,5,6]}
        d = self.check_to_broker(p, '{T -> 42, 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))
Exemplo n.º 2
0
 def test_port(self):
     self.check_to_broker_and_back(broker.Port(65535, broker.Port.TCP),
                                   "65535/tcp", broker.Data.Type.Port)
     self.check_to_broker_and_back(broker.Port(53, broker.Port.UDP),
                                   "53/udp", broker.Data.Type.Port)
     self.check_to_broker_and_back(broker.Port(8, broker.Port.ICMP),
                                   "8/icmp", broker.Data.Type.Port)
     self.check_to_broker_and_back(broker.Port(0, broker.Port.Unknown),
                                   "0/?", broker.Data.Type.Port)
Exemplo n.º 3
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")