Example #1
0
    def test_logger(self):
        # test logger name 'vpp_papi.serializer'
        with self.assertRaises(VPPSerializerValueError) as ctx:
            with self.assertLogs("vpp_papi.serializer", level="DEBUG") as cm:
                u = VPPUnionType(
                    "vl_api_eid_address_t",
                    [
                        ["vl_api_prefix_t", "prefix"],
                        ["vl_api_mac_address_t", "mac"],
                        ["vl_api_nsh_t", "nsh"],
                    ],
                )
        self.assertEqual(
            cm.output,
            ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"])

        # test parent logger name 'vpp_papi'
        with self.assertRaises(VPPSerializerValueError) as ctx:
            with self.assertLogs("vpp_papi", level="DEBUG") as cm:
                u = VPPUnionType(
                    "vl_api_eid_address_t",
                    [
                        ["vl_api_prefix_t", "prefix"],
                        ["vl_api_mac_address_t", "mac"],
                        ["vl_api_nsh_t", "nsh"],
                    ],
                )
        self.assertEqual(
            cm.output,
            ["DEBUG:vpp_papi.serializer:Unknown union type vl_api_prefix_t"])
Example #2
0
    def test_union(self):
        un = VPPUnionType('test_union', [['u8', 'is_bool'], ['u32', 'is_int']])

        b = un.pack({'is_int': 0x12345678})
        nt, size = un.unpack(b)
        self.assertEqual(len(b), size)
        self.assertEqual(nt.is_bool, 0x12)
        self.assertEqual(nt.is_int, 0x12345678)
Example #3
0
    def test_union(self):
        un = VPPUnionType("test_union", [["u8", "is_bool"], ["u32", "is_int"]])

        b = un.pack({"is_int": 0x12345678})
        nt, size = un.unpack(b)
        self.assertEqual(len(b), size)
        self.assertEqual(nt.is_bool, 0x12)
        self.assertEqual(nt.is_int, 0x12345678)
Example #4
0
    def test_union(self):
        un = VPPUnionType('test_union',
                          [['u8', 'is_bool'],
                           ['u32', 'is_int']])

        b = un.pack({'is_int': 0x12345678})
        nt, size = un.unpack(b)
        self.assertEqual(len(b), size)
        self.assertEqual(nt.is_bool, 0x12)
        self.assertEqual(nt.is_int, 0x12345678)
Example #5
0
    def test_lisp(self):
        VPPEnumType('vl_api_eid_type_t',
                    [["EID_TYPE_API_PREFIX", 0],
                     ["EID_TYPE_API_MAC", 1],
                     ["EID_TYPE_API_NSH", 2],
                     {"enumtype": "u32"}])

        VPPTypeAlias('vl_api_mac_address_t', {'type': 'u8',
                                              'length': 6})

        VPPType('vl_api_nsh_t',
                [["u32", "spi"],
                 ["u8", "si"]])

        VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0],
                                                ["ADDRESS_IP6", 1],
                                                {"enumtype": "u32"}])
        VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8',
                                              'length': 4})
        VPPTypeAlias('vl_api_ip6_address_t', {'type': 'u8',
                                              'length': 16})
        VPPUnionType('vl_api_address_union_t',
                     [["vl_api_ip4_address_t", "ip4"],
                      ["vl_api_ip6_address_t", "ip6"]])

        VPPType('vl_api_address_t',
                [['vl_api_address_family_t', 'af'],
                 ['vl_api_address_union_t', 'un']])

        VPPType('vl_api_prefix_t',
                [['vl_api_address_t', 'address'],
                 ['u8', 'len']])

        VPPUnionType('vl_api_eid_address_t',
                     [["vl_api_prefix_t", "prefix"],
                      ["vl_api_mac_address_t", "mac"],
                      ["vl_api_nsh_t", "nsh"]])

        eid = VPPType('vl_api_eid_t',
                      [["vl_api_eid_type_t", "type"],
                       ["vl_api_eid_address_t", "address"]])

        b = eid.pack({'type':1,
                      'address': {
                          'mac': MACAddress('aa:bb:cc:dd:ee:ff')}})
        self.assertEqual(len(b), 25)
        nt, size = eid.unpack(b)
        self.assertEqual(str(nt.address.mac), 'aa:bb:cc:dd:ee:ff')
        self.assertIsNone(nt.address.prefix)
Example #6
0
    def test_recursive_address(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPType('vl_api_ip4_address_t', [['u8', 'address', 4]])
        ip6 = VPPType('vl_api_ip6_address_t', [['u8', 'address', 16]])
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('vl_api_address_t',
                          [['vl_api_address_family_t', 'af'],
                           ['vl_api_address_union_t', 'un']])

        prefix = VPPType(
            'vl_api_prefix_t',
            [['vl_api_address_t', 'address'], ['u8', 'address_length']])
        message = VPPMessage('svs', [['vl_api_prefix_t', 'prefix']])
        message_addr = VPPMessage('svs_address',
                                  [['vl_api_address_t', 'address']])

        b = message_addr.pack({'address': "1::1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1::1", VPPFormat.unformat(nt.address))
        b = message_addr.pack({'address': "1.1.1.1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1.1.1.1", VPPFormat.unformat(nt.address))

        b = message.pack({'prefix': "1.1.1.1/24"})
        self.assertEqual(len(b), 21)
        nt, size = message.unpack(b)
        self.assertEqual("1.1.1.1/24", VPPFormat.unformat(nt.prefix))
Example #7
0
    def test_address(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPType('vl_api_ip4_address_t', [['u8', 'address', 4]])
        ip6 = VPPType('vl_api_ip6_address_t', [['u8', 'address', 16]])
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('address', [['vl_api_address_family_t', 'af'],
                                      ['vl_api_address_union_t', 'un']])

        b = ip4.pack({'address': inet_pton(AF_INET, '1.1.1.1')})
        self.assertEqual(len(b), 4)
        nt = ip4.unpack(b)
        self.assertEqual(nt.address, inet_pton(AF_INET, '1.1.1.1'))

        b = ip6.pack({'address': inet_pton(AF_INET6, '1::1')})
        self.assertEqual(len(b), 16)

        b = address.pack({
            'af': af.ADDRESS_IP4,
            'un': {
                'ip4': {
                    'address': inet_pton(AF_INET, '2.2.2.2')
                }
            }
        })
        self.assertEqual(len(b), 20)

        nt = address.unpack(b)
        self.assertEqual(nt.af, af.ADDRESS_IP4)
        self.assertEqual(nt.un.ip4.address, inet_pton(AF_INET, '2.2.2.2'))
        self.assertEqual(nt.un.ip6.address, inet_pton(AF_INET6, '0202:0202::'))
Example #8
0
    def test_address(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPType('vl_api_ip4_address_t', [['u8', 'address', 4]])
        ip6 = VPPType('vl_api_ip6_address_t', [['u8', 'address', 16]])
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('vl_api_address_t',
                          [['vl_api_address_family_t', 'af'],
                           ['vl_api_address_union_t', 'un']])

        va_address_list = VPPType(
            'list_addresses',
            [['u8', 'count'], ['vl_api_address_t', 'addresses', 0, 'count']])

        message_with_va_address_list = VPPType(
            'msg_with_vla',
            [['list_addresses', 'vla_address'], ['u8', 'is_cool']])

        b = ip4.pack({'address': inet_pton(AF_INET, '1.1.1.1')})
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)
        self.assertEqual(nt.address, inet_pton(AF_INET, '1.1.1.1'))

        b = ip6.pack({'address': inet_pton(AF_INET6, '1::1')})
        self.assertEqual(len(b), 16)

        b = address.pack({
            'af': af.ADDRESS_IP4,
            'un': {
                'ip4': {
                    'address': inet_pton(AF_INET, '2.2.2.2')
                }
            }
        })
        self.assertEqual(len(b), 20)

        nt, size = address.unpack(b)
        self.assertEqual(nt.af, af.ADDRESS_IP4)
        self.assertEqual(nt.un.ip4.address, inet_pton(AF_INET, '2.2.2.2'))
        self.assertEqual(nt.un.ip6.address, inet_pton(AF_INET6, '0202:0202::'))

        # List of addresses
        address_list = []
        for i in range(4):
            address_list.append({
                'af': af.ADDRESS_IP4,
                'un': {
                    'ip4': {
                        'address': inet_pton(AF_INET, '2.2.2.2')
                    }
                }
            })
        b = va_address_list.pack({
            'count': len(address_list),
            'addresses': address_list
        })
        self.assertEqual(len(b), 81)

        nt, size = va_address_list.unpack(b)
        self.assertEqual(nt.addresses[0].un.ip4.address,
                         inet_pton(AF_INET, '2.2.2.2'))

        b = message_with_va_address_list.pack({
            'vla_address': {
                'count': len(address_list),
                'addresses': address_list
            },
            'is_cool': 100
        })
        self.assertEqual(len(b), 82)
        nt, size = message_with_va_address_list.unpack(b)
        self.assertEqual(nt.is_cool, 100)
Example #9
0
    def test_recursive_address(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', 'length': 4})
        b = ip4.pack('1.1.1.1')
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)

        self.assertEqual(str(nt), '1.1.1.1')

        ip6 = VPPTypeAlias('vl_api_ip6_address_t', {
            'type': 'u8',
            'length': 16
        })
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('vl_api_address_t',
                          [['vl_api_address_family_t', 'af'],
                           ['vl_api_address_union_t', 'un']])

        prefix = VPPType('vl_api_prefix_t',
                         [['vl_api_address_t', 'address'], ['u8', 'len']])
        message = VPPMessage('svs', [['vl_api_prefix_t', 'prefix']])
        message_addr = VPPMessage('svs_address',
                                  [['vl_api_address_t', 'address']])

        b = message_addr.pack({'address': "1::1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1::1", str(nt.address))
        b = message_addr.pack({'address': "1.1.1.1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1.1.1.1", str(nt.address))

        b = message.pack({'prefix': "1.1.1.0/24"})
        self.assertEqual(len(b), 21)
        nt, size = message.unpack(b)
        self.assertEqual("1.1.1.0/24", str(nt.prefix))

        message_array = VPPMessage('address_array',
                                   [['vl_api_ip6_address_t', 'addresses', 2]])
        b = message_array.pack({'addresses': [IPv6Address(u"1::1"), "2::2"]})
        self.assertEqual(len(b), 32)
        message_array_vla = VPPMessage(
            'address_array_vla',
            [['u32', 'num'], ['vl_api_ip6_address_t', 'addresses', 0, 'num']])
        b = message_array_vla.pack({'addresses': ["1::1", "2::2"], 'num': 2})
        self.assertEqual(len(b), 36)

        message_array4 = VPPMessage('address_array4',
                                    [['vl_api_ip4_address_t', 'addresses', 2]])
        b = message_array4.pack({'addresses': ["1.1.1.1", "2.2.2.2"]})
        self.assertEqual(len(b), 8)
        b = message_array4.pack(
            {'addresses': [IPv4Address(u"1.1.1.1"), "2.2.2.2"]})
        self.assertEqual(len(b), 8)

        message = VPPMessage('address', [['vl_api_address_t', 'address']])
        b = message.pack({'address': '1::1'})
        self.assertEqual(len(b), 20)
        b = message.pack({'address': '1.1.1.1'})
        self.assertEqual(len(b), 20)
        message = VPPMessage('prefix', [['vl_api_prefix_t', 'prefix']])
        b = message.pack({'prefix': '1::1/130'})
        self.assertEqual(len(b), 21)
        b = message.pack({'prefix': IPv6Network(u'1::/119')})
        self.assertEqual(len(b), 21)
        b = message.pack({'prefix': IPv4Network(u'1.1.0.0/16')})
        self.assertEqual(len(b), 21)
Example #10
0
    def test_address_with_prefix(self):
        af = VPPEnumType('vl_api_address_family_t',
                         [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                             "enumtype": "u32"
                         }])
        ip4 = VPPTypeAlias('vl_api_ip4_address_t', {'type': 'u8', 'length': 4})
        ip6 = VPPTypeAlias('vl_api_ip6_address_t', {
            'type': 'u8',
            'length': 16
        })
        VPPUnionType(
            'vl_api_address_union_t',
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]])

        address = VPPType('vl_api_address_t',
                          [['vl_api_address_family_t', 'af'],
                           ['vl_api_address_union_t', 'un']])

        prefix = VPPType('vl_api_prefix_t',
                         [['vl_api_address_t', 'address'], ['u8', 'len']])
        prefix4 = VPPType('vl_api_ip4_prefix_t',
                          [['vl_api_ip4_address_t', 'address'], ['u8', 'len']])
        prefix6 = VPPType('vl_api_ip6_prefix_t',
                          [['vl_api_ip6_address_t', 'address'], ['u8', 'len']])

        address_with_prefix = VPPTypeAlias('vl_api_address_with_prefix_t',
                                           {'type': 'vl_api_prefix_t'})
        address4_with_prefix = VPPTypeAlias('vl_api_ip4_address_with_prefix_t',
                                            {'type': 'vl_api_ip4_prefix_t'})
        address6_with_prefix = VPPTypeAlias('vl_api_ip6_address_with_prefix_t',
                                            {'type': 'vl_api_ip6_prefix_t'})

        awp_type = VPPType('foobar_t',
                           [['vl_api_address_with_prefix_t', 'address']])

        # address with prefix
        b = address_with_prefix.pack(IPv4Interface('2.2.2.2/24'))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), '2.2.2.2/24')

        b = address_with_prefix.pack(IPv6Interface('2::2/64'))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv6Interface))
        self.assertEqual(str(nt), '2::2/64')

        b = address_with_prefix.pack(IPv4Network('2.2.2.2/24', strict=False))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), '2.2.2.0/24')

        b = address4_with_prefix.pack('2.2.2.2/24')
        self.assertEqual(len(b), 5)
        nt, size = address4_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), '2.2.2.2/24')
        b = address4_with_prefix.pack(IPv4Interface('2.2.2.2/24'))
        self.assertEqual(len(b), 5)

        b = address6_with_prefix.pack('2::2/64')
        self.assertEqual(len(b), 17)
        nt, size = address6_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv6Interface))
        self.assertEqual(str(nt), '2::2/64')
        b = address6_with_prefix.pack(IPv6Interface('2::2/64'))
        self.assertEqual(len(b), 17)

        b = prefix.pack('192.168.10.0/24')
        self.assertEqual(len(b), 21)
        nt, size = prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Network))
        self.assertEqual(str(nt), '192.168.10.0/24')

        b = awp_type.pack({'address': '1.2.3.4/24'})
        self.assertEqual(len(b), 21)
        nt, size = awp_type.unpack(b)
        self.assertTrue(isinstance(nt.address, IPv4Interface))
        self.assertEqual(str(nt.address), '1.2.3.4/24')

        b = awp_type.pack({'address': IPv4Interface('1.2.3.4/24')})
        self.assertEqual(len(b), 21)
        nt, size = awp_type.unpack(b)
        self.assertTrue(isinstance(nt.address, IPv4Interface))
        self.assertEqual(str(nt.address), '1.2.3.4/24')
Example #11
0
    def test_lisp(self):
        VPPEnumType(
            "vl_api_eid_type_t",
            [
                ["EID_TYPE_API_PREFIX", 0],
                ["EID_TYPE_API_MAC", 1],
                ["EID_TYPE_API_NSH", 2],
                {
                    "enumtype": "u32"
                },
            ],
        )

        VPPTypeAlias("vl_api_mac_address_t", {"type": "u8", "length": 6})

        VPPType("vl_api_nsh_t", [["u32", "spi"], ["u8", "si"]])

        VPPEnumType(
            "vl_api_address_family_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4})
        VPPTypeAlias("vl_api_ip6_address_t", {"type": "u8", "length": 16})
        VPPUnionType(
            "vl_api_address_union_t",
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]],
        )

        VPPType(
            "vl_api_address_t",
            [["vl_api_address_family_t", "af"],
             ["vl_api_address_union_t", "un"]],
        )

        VPPType("vl_api_prefix_t",
                [["vl_api_address_t", "address"], ["u8", "len"]])

        VPPUnionType(
            "vl_api_eid_address_t",
            [
                ["vl_api_prefix_t", "prefix"],
                ["vl_api_mac_address_t", "mac"],
                ["vl_api_nsh_t", "nsh"],
            ],
        )

        eid = VPPType(
            "vl_api_eid_t",
            [["vl_api_eid_type_t", "type"],
             ["vl_api_eid_address_t", "address"]],
        )

        b = eid.pack({
            "type": 1,
            "address": {
                "mac": MACAddress("aa:bb:cc:dd:ee:ff")
            }
        })
        self.assertEqual(len(b), 25)
        nt, size = eid.unpack(b)
        self.assertEqual(str(nt.address.mac), "aa:bb:cc:dd:ee:ff")
        self.assertIsNone(nt.address.prefix)
Example #12
0
    def test_recursive_address(self):
        af = VPPEnumType(
            "vl_api_address_family_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        ip4 = VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4})
        b = ip4.pack("1.1.1.1")
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)

        self.assertEqual(str(nt), "1.1.1.1")

        ip6 = VPPTypeAlias("vl_api_ip6_address_t", {
            "type": "u8",
            "length": 16
        })
        VPPUnionType(
            "vl_api_address_union_t",
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]],
        )

        address = VPPType(
            "vl_api_address_t",
            [["vl_api_address_family_t", "af"],
             ["vl_api_address_union_t", "un"]],
        )

        prefix = VPPType("vl_api_prefix_t",
                         [["vl_api_address_t", "address"], ["u8", "len"]])
        message = VPPMessage("svs", [["vl_api_prefix_t", "prefix"]])
        message_addr = VPPMessage("svs_address",
                                  [["vl_api_address_t", "address"]])

        b = message_addr.pack({"address": "1::1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1::1", str(nt.address))
        b = message_addr.pack({"address": "1.1.1.1"})
        self.assertEqual(len(b), 20)
        nt, size = message_addr.unpack(b)
        self.assertEqual("1.1.1.1", str(nt.address))

        b = message.pack({"prefix": "1.1.1.0/24"})
        self.assertEqual(len(b), 21)
        nt, size = message.unpack(b)
        self.assertEqual("1.1.1.0/24", str(nt.prefix))

        message_array = VPPMessage("address_array",
                                   [["vl_api_ip6_address_t", "addresses", 2]])
        b = message_array.pack({"addresses": [IPv6Address("1::1"), "2::2"]})
        self.assertEqual(len(b), 32)
        message_array_vla = VPPMessage(
            "address_array_vla",
            [["u32", "num"], ["vl_api_ip6_address_t", "addresses", 0, "num"]],
        )
        b = message_array_vla.pack({"addresses": ["1::1", "2::2"], "num": 2})
        self.assertEqual(len(b), 36)

        message_array4 = VPPMessage("address_array4",
                                    [["vl_api_ip4_address_t", "addresses", 2]])
        b = message_array4.pack({"addresses": ["1.1.1.1", "2.2.2.2"]})
        self.assertEqual(len(b), 8)
        b = message_array4.pack(
            {"addresses": [IPv4Address("1.1.1.1"), "2.2.2.2"]})
        self.assertEqual(len(b), 8)

        message = VPPMessage("address", [["vl_api_address_t", "address"]])
        b = message.pack({"address": "1::1"})
        self.assertEqual(len(b), 20)
        b = message.pack({"address": "1.1.1.1"})
        self.assertEqual(len(b), 20)
        message = VPPMessage("prefix", [["vl_api_prefix_t", "prefix"]])
        b = message.pack({"prefix": "1::1/130"})
        self.assertEqual(len(b), 21)
        b = message.pack({"prefix": IPv6Network("1::/119")})
        self.assertEqual(len(b), 21)
        b = message.pack({"prefix": IPv4Network("1.1.0.0/16")})
        self.assertEqual(len(b), 21)
Example #13
0
    def test_address_with_prefix(self):
        af = VPPEnumType(
            "vl_api_address_family_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        ip4 = VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4})
        ip6 = VPPTypeAlias("vl_api_ip6_address_t", {
            "type": "u8",
            "length": 16
        })
        VPPUnionType(
            "vl_api_address_union_t",
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]],
        )

        address = VPPType(
            "vl_api_address_t",
            [["vl_api_address_family_t", "af"],
             ["vl_api_address_union_t", "un"]],
        )

        prefix = VPPType("vl_api_prefix_t",
                         [["vl_api_address_t", "address"], ["u8", "len"]])
        prefix4 = VPPType("vl_api_ip4_prefix_t",
                          [["vl_api_ip4_address_t", "address"], ["u8", "len"]])
        prefix6 = VPPType("vl_api_ip6_prefix_t",
                          [["vl_api_ip6_address_t", "address"], ["u8", "len"]])

        address_with_prefix = VPPTypeAlias("vl_api_address_with_prefix_t",
                                           {"type": "vl_api_prefix_t"})
        address4_with_prefix = VPPTypeAlias("vl_api_ip4_address_with_prefix_t",
                                            {"type": "vl_api_ip4_prefix_t"})
        address6_with_prefix = VPPTypeAlias("vl_api_ip6_address_with_prefix_t",
                                            {"type": "vl_api_ip6_prefix_t"})

        awp_type = VPPType("foobar_t",
                           [["vl_api_address_with_prefix_t", "address"]])

        # address with prefix
        b = address_with_prefix.pack(IPv4Interface("2.2.2.2/24"))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), "2.2.2.2/24")

        b = address_with_prefix.pack(IPv6Interface("2::2/64"))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv6Interface))
        self.assertEqual(str(nt), "2::2/64")

        b = address_with_prefix.pack(IPv4Network("2.2.2.2/24", strict=False))
        self.assertEqual(len(b), 21)
        nt, size = address_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), "2.2.2.0/24")

        b = address4_with_prefix.pack("2.2.2.2/24")
        self.assertEqual(len(b), 5)
        nt, size = address4_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Interface))
        self.assertEqual(str(nt), "2.2.2.2/24")
        b = address4_with_prefix.pack(IPv4Interface("2.2.2.2/24"))
        self.assertEqual(len(b), 5)

        b = address6_with_prefix.pack("2::2/64")
        self.assertEqual(len(b), 17)
        nt, size = address6_with_prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv6Interface))
        self.assertEqual(str(nt), "2::2/64")
        b = address6_with_prefix.pack(IPv6Interface("2::2/64"))
        self.assertEqual(len(b), 17)

        b = prefix.pack("192.168.10.0/24")
        self.assertEqual(len(b), 21)
        nt, size = prefix.unpack(b)
        self.assertTrue(isinstance(nt, IPv4Network))
        self.assertEqual(str(nt), "192.168.10.0/24")

        b = awp_type.pack({"address": "1.2.3.4/24"})
        self.assertEqual(len(b), 21)
        nt, size = awp_type.unpack(b)
        self.assertTrue(isinstance(nt.address, IPv4Interface))
        self.assertEqual(str(nt.address), "1.2.3.4/24")

        b = awp_type.pack({"address": IPv4Interface("1.2.3.4/24")})
        self.assertEqual(len(b), 21)
        nt, size = awp_type.unpack(b)
        self.assertTrue(isinstance(nt.address, IPv4Interface))
        self.assertEqual(str(nt.address), "1.2.3.4/24")
Example #14
0
    def test_address(self):
        af = VPPEnumType(
            "vl_api_address_family_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        aff = VPPEnumFlagType(
            "vl_api_address_family_flag_t",
            [["ADDRESS_IP4", 0], ["ADDRESS_IP6", 1], {
                "enumtype": "u32"
            }],
        )
        ip4 = VPPTypeAlias("vl_api_ip4_address_t", {"type": "u8", "length": 4})
        ip6 = VPPTypeAlias("vl_api_ip6_address_t", {
            "type": "u8",
            "length": 16
        })
        VPPUnionType(
            "vl_api_address_union_t",
            [["vl_api_ip4_address_t", "ip4"], ["vl_api_ip6_address_t", "ip6"]],
        )

        address = VPPType(
            "vl_api_address_t",
            [["vl_api_address_family_t", "af"],
             ["vl_api_address_union_t", "un"]],
        )

        prefix = VPPType("vl_api_prefix_t",
                         [["vl_api_address_t", "address"], ["u8", "len"]])

        va_address_list = VPPType(
            "list_addresses",
            [["u8", "count"], ["vl_api_address_t", "addresses", 0, "count"]],
        )

        message_with_va_address_list = VPPType(
            "msg_with_vla",
            [["list_addresses", "vla_address"], ["u8", "is_cool"]])

        b = ip4.pack(inet_pton(AF_INET, "1.1.1.1"))
        self.assertEqual(len(b), 4)
        nt, size = ip4.unpack(b)
        self.assertEqual(str(nt), "1.1.1.1")

        b = ip6.pack(inet_pton(AF_INET6, "1::1"))
        self.assertEqual(len(b), 16)

        b = address.pack({
            "af": af.ADDRESS_IP4,
            "un": {
                "ip4": inet_pton(AF_INET, "2.2.2.2")
            }
        })
        self.assertEqual(len(b), 20)

        nt, size = address.unpack(b)
        self.assertEqual(str(nt), "2.2.2.2")

        # List of addresses
        address_list = []
        for i in range(4):
            address_list.append({
                "af": af.ADDRESS_IP4,
                "un": {
                    "ip4": inet_pton(AF_INET, "2.2.2.2")
                }
            })
        b = va_address_list.pack({
            "count": len(address_list),
            "addresses": address_list
        })
        self.assertEqual(len(b), 81)

        nt, size = va_address_list.unpack(b)
        self.assertEqual(str(nt.addresses[0]), "2.2.2.2")

        b = message_with_va_address_list.pack({
            "vla_address": {
                "count": len(address_list),
                "addresses": address_list
            },
            "is_cool": 100,
        })
        self.assertEqual(len(b), 82)
        nt, size = message_with_va_address_list.unpack(b)
        self.assertEqual(nt.is_cool, 100)