1 # Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
24 from vpp_papi import vpp_format
26 from parameterized import parameterized
29 ip4_addrn = b"\x01\x02\x03\x04"
31 ip4_prefix = "%s/%s" % (ip4_addr, ip4_prefix_len)
32 ipv4_network = ipaddress.IPv4Network(text_type(ip4_prefix))
33 ip4_addr_format_vl_api_address_t = {"un": {"ip4": b"\x01\x02\x03\x04"}, "af": 0}
34 ip4_addr_format_vl_api_prefix_t = {
35 "address": {"un": {"ip4": b"\x01\x02\x03\x04"}, "af": 0}, # noqa: E127,E501
36 "len": ip4_prefix_len,
38 ip4_addr_format_vl_api_prefix_packed_t = {
39 "address": b"\x01\x02\x03\x04",
40 "len": ip4_prefix_len,
44 ip6_addrn = b"\xde\xad\x00\x00\x00\x00\x00\x00" b"\x00\x00\x00\x00\x00\x00\x00\x00"
46 ip6_prefix = "%s/%s" % (ip6_addr, ip6_prefix_len)
47 ipv6_network = ipaddress.IPv6Network(text_type(ip6_prefix))
48 ip6_addr_format_vl_api_address_t = {
50 "ip6": b"\xde\xad\x00\x00"
57 ip6_addr_format_vl_api_prefix_t = {
58 "address": { # noqa: E127
61 "ip6": b"\xde\xad\x00\x00"
67 "len": ip6_prefix_len,
69 ip6_addr_format_vl_api_prefix_packed_t = {
70 "address": b"\xde\xad\x00\x00" # noqa: E127,E501
74 "len": ip6_prefix_len,
78 class TestVppFormat(unittest.TestCase):
79 def test_format_vl_api_address_t(self):
80 res = vpp_format.format_vl_api_address_t(ip4_addr)
81 self.assertEqual(res, ip4_addr_format_vl_api_address_t)
83 # PY2: raises socket.error
85 with self.assertRaises((TypeError, socket.error, OSError)):
86 res = vpp_format.format_vl_api_address_t(ip4_addrn)
88 res = vpp_format.format_vl_api_address_t(ip6_addr)
89 self.assertEqual(res, ip6_addr_format_vl_api_address_t)
91 with self.assertRaises(TypeError):
92 es = vpp_format.format_vl_api_address_t(ip6_addrn)
94 @parameterized.expand(
96 ("ip4 prefix", ip4_prefix, ip4_addr_format_vl_api_prefix_t),
97 ("ip6 prefix", ip6_prefix, ip6_addr_format_vl_api_prefix_t),
98 ("IPv4Network", ipv4_network, ip4_addr_format_vl_api_prefix_t),
99 ("IPv6Network", ipv6_network, ip6_addr_format_vl_api_prefix_t),
102 def test_format_vl_api_prefix_t(self, _, arg, expected):
103 res = vpp_format.format_vl_api_prefix_t(arg)
104 self.assertEqual(res, expected)
106 def test_format_vl_api_ip6_prefix_t(self):
107 res = vpp_format.format_vl_api_ip6_prefix_t(ip6_prefix)
108 self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
110 res = vpp_format.format_vl_api_ip6_prefix_t(ipv6_network)
111 self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
113 def test_format_vl_api_ip4_prefix_t(self):
114 res = vpp_format.format_vl_api_ip4_prefix_t(ip4_prefix)
115 self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
117 res = vpp_format.format_vl_api_ip4_prefix_t(ipv4_network)
118 self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
120 def test_format_vl_api_ip6_prefix_t_raises(self):
121 # PY2: raises socket.error
122 # PY3: raises OSError
123 with self.assertRaises((socket.error, OSError)):
124 res = vpp_format.format_vl_api_ip6_prefix_t(ip4_prefix)
126 def test_format_vl_api_ip4_prefix_t_raises(self):
127 # PY2: raises socket.error
128 # PY3: raises OSError
129 with self.assertRaises((socket.error, OSError)):
130 res = vpp_format.format_vl_api_ip4_prefix_t(ip6_prefix)