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)
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)
def test_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}) 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']]) 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)
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")
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)
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')
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', '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", 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)
def test_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}) 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']]) 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)