From 710fe104620683755de71e2bb5d97c438987df74 Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Tue, 2 Jun 2020 14:38:53 +0200 Subject: papi: allow unknown address family In unions all representations of the union are decoded. Which means trying to decode something that isn't an address might have invalid address family types. Type: fix Ticket: VPP-1884 Signed-off-by: Ole Troan Change-Id: Id3381ef8cc885952c1eb488ebc70e276eaceb366 --- .../python/vpp_papi/tests/test_vpp_serializer.py | 52 ++++++++++++++++++++++ src/vpp-api/python/vpp_papi/vpp_format.py | 29 +++++++----- 2 files changed, 71 insertions(+), 10 deletions(-) diff --git a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py index 5d6993f91a7..317fea7269a 100755 --- a/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py +++ b/src/vpp-api/python/vpp_papi/tests/test_vpp_serializer.py @@ -4,6 +4,7 @@ import unittest from vpp_papi.vpp_serializer import VPPType, VPPEnumType from vpp_papi.vpp_serializer import VPPUnionType, VPPMessage from vpp_papi.vpp_serializer import VPPTypeAlias, VPPSerializerValueError +from vpp_papi import MACAddress from socket import inet_pton, AF_INET, AF_INET6 import logging import sys @@ -557,5 +558,56 @@ class TestAddType(unittest.TestCase): self.assertEqual(len(b), 20) + 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) + + if __name__ == '__main__': unittest.main() diff --git a/src/vpp-api/python/vpp_papi/vpp_format.py b/src/vpp-api/python/vpp_papi/vpp_format.py index 4c26463bbb5..261683d5816 100644 --- a/src/vpp-api/python/vpp_papi/vpp_format.py +++ b/src/vpp-api/python/vpp_papi/vpp_format.py @@ -57,6 +57,7 @@ def format_vl_api_prefix_t(args): return {'address': format_vl_api_address_t(p), 'len': int(length)} + def format_vl_api_address_with_prefix_t(args): if isinstance(args, (ipaddress.IPv4Interface, ipaddress.IPv6Interface)): return {'address': format_vl_api_address_t( @@ -75,6 +76,7 @@ def format_vl_api_ip6_prefix_t(args): return {'address': inet_pton(AF_INET6, p), 'len': int(length)} + def format_vl_api_ip6_address_with_prefix_t(args): if isinstance(args, ipaddress.IPv6Interface): return {'address': args.network_address.packed, @@ -83,6 +85,7 @@ def format_vl_api_ip6_address_with_prefix_t(args): return {'address': inet_pton(AF_INET6, p), 'len': int(length)} + def format_vl_api_ip4_prefix_t(args): if isinstance(args, ipaddress.IPv4Network): return {'address': args.network_address.packed, @@ -91,6 +94,7 @@ def format_vl_api_ip4_prefix_t(args): return {'address': inet_pton(AF_INET, p), 'len': int(length)} + def format_vl_api_ip4_address_with_prefix_t(args): if isinstance(args, ipaddress.IPv4Interface): return {'address': args.network_address.packed, @@ -144,13 +148,13 @@ conversion_table = { 'vl_api_address_with_prefix_t': { 'IPv4Interface': lambda o: {'address': - {'af': ADDRESS_IP4, 'un': - {'ip4': o.packed}}, - 'len': o.network.prefixlen}, + {'af': ADDRESS_IP4, 'un': + {'ip4': o.packed}}, + 'len': o.network.prefixlen}, 'IPv6Interface': lambda o: {'address': - {'af': ADDRESS_IP6, 'un': - {'ip6': o.packed}}, - 'len': o.network.prefixlen}, + {'af': ADDRESS_IP6, 'un': + {'ip6': o.packed}}, + 'len': o.network.prefixlen}, 'str': lambda s: format_vl_api_address_with_prefix_t(s) }, 'vl_api_ip4_address_with_prefix_t': @@ -162,7 +166,7 @@ conversion_table = { 'vl_api_ip6_address_with_prefix_t': { 'IPv6Interface': lambda o: {'address': o.packed, - 'len': o.network.prefixlen}, + 'len': o.network.prefixlen}, 'str': lambda s: format_vl_api_ip6_address_with_prefix_t(s) }, 'vl_api_mac_address_t': @@ -183,14 +187,15 @@ def unformat_api_address_t(o): return ipaddress.IPv6Address(o.un.ip6) if o.af == 0: return ipaddress.IPv4Address(o.un.ip4) - raise ValueError('Unknown address family {}'.format(o)) + return None + def unformat_api_prefix_t(o): if o.address.af == 1: return ipaddress.IPv6Network((o.address.un.ip6, o.len), False) if o.address.af == 0: return ipaddress.IPv4Network((o.address.un.ip4, o.len), False) - raise ValueError('Unknown address family {}'.format(o)) + return None if isinstance(o.address, ipaddress.IPv4Address): return ipaddress.IPv4Network((o.address, o.len), False) @@ -198,19 +203,23 @@ def unformat_api_prefix_t(o): return ipaddress.IPv6Network((o.address, o.len), False) raise ValueError('Unknown instance {}', format(o)) + def unformat_api_address_with_prefix_t(o): if o.address.af == 1: return ipaddress.IPv6Interface((o.address.un.ip6, o.len)) if o.address.af == 0: return ipaddress.IPv4Interface((o.address.un.ip4, o.len)) - raise ValueError('Unknown address family {}'.format(o)) + return None + def unformat_api_ip4_address_with_prefix_t(o): return ipaddress.IPv4Interface((o.address, o.len)) + def unformat_api_ip6_address_with_prefix_t(o): return ipaddress.IPv6Interface((o.address, o.len)) + conversion_unpacker_table = { 'vl_api_ip6_address_t': lambda o: ipaddress.IPv6Address(o), 'vl_api_ip6_prefix_t': lambda o: ipaddress.IPv6Network((o.address, o.len)), -- cgit 1.2.3-korg