From 31555a3475a37195938378217a635b3451e449de Mon Sep 17 00:00:00 2001 From: Ole Troan Date: Mon, 22 Oct 2018 09:30:26 +0200 Subject: PAPI: Add support for format/unformat functions. With the introduction of new types, like vl_api_address_t it is now possible to call a message using one of those functions with a string representation. E.g. for an IP address ip_add_address(address="1.1.1.1/24") The language wrapper will automatically convert the string into the vl_api_address_t representation. Currently the caller must do the reverse conversion from the returned named tuple with the unformat function. rv = get_address_on_interface(sw_if_index=1) print(VPPFormat.unformat(rv.address)) Change-Id: Ic872b4560b2f4836255bd5260289bfa38c75bc5d Signed-off-by: Ole Troan --- .../python/vpp_papi/tests/test_vpp_serializer.py | 64 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) (limited to 'src/vpp-api/python/vpp_papi/tests') 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 4e8a417c6fd..4b47e1eca7d 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 @@ -3,6 +3,7 @@ import unittest from vpp_papi.vpp_serializer import VPPType, VPPEnumType from vpp_papi.vpp_serializer import VPPUnionType, VPPMessage +from vpp_papi.vpp_format import VPPFormat from socket import inet_pton, AF_INET, AF_INET6 import logging import sys @@ -89,6 +90,65 @@ class TestAddType(unittest.TestCase): nt, size = message_with_va_address_list.unpack(b) self.assertEqual(nt.is_cool, 100) + 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)) + + def test_zero_vla(self): + '''Default zero'ed out for VLAs''' + list = VPPType('vl_api_list_t', + [['u8', 'count', 10]]) + + # Define an embedded VLA type + valist = VPPType('vl_api_valist_t', + [['u8', 'count'], + ['u8', 'string', 0, 'count']]) + # Define a message + vamessage = VPPMessage('vamsg', + [['vl_api_valist_t', 'valist'], + ['u8', 'is_something']]) + + message = VPPMessage('msg', + [['vl_api_list_t', 'list'], + ['u8', 'is_something']]) + + # Pack message without VLA specified + b = message.pack({'is_something': 1}) + b = vamessage.pack({'is_something': 1}) + def test_arrays(self): # Test cases # 1. Fixed list @@ -133,7 +193,7 @@ class TestAddType(unittest.TestCase): inet_pton(AF_INET, '2.2.2.2')) string = 'foobar foobar' - b = s.pack({'length': len(string), 'string': string}) + b = s.pack({'length': len(string), 'string': string.encode()}) nt, size = s.unpack(b) self.assertEqual(len(b), size) @@ -142,7 +202,7 @@ class TestAddType(unittest.TestCase): ['u8', 'string', 0, 'length']]) string = '' - b = s.pack({'length': len(string), 'string': string}) + b = s.pack({'length': len(string), 'string': string.encode()}) nt, size = s.unpack(b) self.assertEqual(len(b), size) -- cgit 1.2.3-korg