papi: fix vpp_format from change in vl_api_prefix_t
[vpp.git] / src / vpp-api / python / vpp_papi / tests / test_vpp_format.py
1 #  Copyright (c) 2019. Vinci Consulting Corp. All Rights Reserved.
2 #
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
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 import ipaddress
16 import socket
17 import unittest
18
19 try:
20     text_type = unicode
21 except NameError:
22     text_type = str
23
24 from vpp_papi import vpp_format
25
26 from parameterized import parameterized
27
28 ip4_addr = '1.2.3.4'
29 ip4_addrn = b'\x01\x02\x03\x04'
30 ip4_prefix_len = 32
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'},
34                                     'af': 0}
35 ip4_addr_format_vl_api_prefix_t = {'address':                                # noqa: E127,E501
36                                        {'un': {'ip4': b'\x01\x02\x03\x04'},
37                                         'af': 0},
38                                    'len': ip4_prefix_len}
39 ip4_addr_format_vl_api_prefix_packed_t = {'address': b'\x01\x02\x03\x04',
40                                           'len': ip4_prefix_len}
41
42 ip6_addr = 'dead::'
43 ip6_addrn = b'\xde\xad\x00\x00\x00\x00\x00\x00' \
44             b'\x00\x00\x00\x00\x00\x00\x00\x00'
45 ip6_prefix_len = 127
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 = {'un': {'ip6': b'\xde\xad\x00\x00'
49                                                   b'\x00\x00\x00\x00'
50                                                   b'\x00\x00\x00\x00'
51                                                   b'\x00\x00\x00\x00'},
52                                     'af': 1}
53 ip6_addr_format_vl_api_prefix_t = {'address':       # noqa: E127
54                                        {'af': 1,
55                                         'un': {
56                                             'ip6': b'\xde\xad\x00\x00'
57                                                    b'\x00\x00\x00\x00'
58                                                    b'\x00\x00\x00\x00'
59                                                    b'\x00\x00\x00\x00'}},
60                                    'len': ip6_prefix_len}
61 ip6_addr_format_vl_api_prefix_packed_t = {'address': b'\xde\xad\x00\x00'   # noqa: E127,E501
62                                                      b'\x00\x00\x00\x00'
63                                                      b'\x00\x00\x00\x00'
64                                                      b'\x00\x00\x00\x00',
65                                           'len': ip6_prefix_len}
66
67
68 class TestVppFormat(unittest.TestCase):
69
70     def test_format_vl_api_address_t(self):
71         res = vpp_format.format_vl_api_address_t(ip4_addr)
72         self.assertEqual(res, ip4_addr_format_vl_api_address_t)
73
74         # PY2: raises socket.error
75         # PY3: raises OSError
76         with self.assertRaises((TypeError,
77                                 socket.error,
78                                 OSError)):
79             res = vpp_format.format_vl_api_address_t(ip4_addrn)
80
81         res = vpp_format.format_vl_api_address_t(ip6_addr)
82         self.assertEqual(res, ip6_addr_format_vl_api_address_t)
83
84         with self.assertRaises(TypeError):
85             es = vpp_format.format_vl_api_address_t(ip6_addrn)
86
87     @parameterized.expand([('ip4 prefix',
88                             ip4_prefix,
89                             ip4_addr_format_vl_api_prefix_t),
90                            ('ip6 prefix',
91                             ip6_prefix,
92                             ip6_addr_format_vl_api_prefix_t),
93                            ('IPv4Network',
94                             ipv4_network,
95                             ip4_addr_format_vl_api_prefix_t),
96                            ('IPv6Network',
97                             ipv6_network,
98                             ip6_addr_format_vl_api_prefix_t),
99                            ])
100     def test_format_vl_api_prefix_t(self, _, arg, expected):
101         res = vpp_format.format_vl_api_prefix_t(arg)
102         self.assertEqual(res, expected)
103
104     def test_format_vl_api_ip6_prefix_t(self):
105         res = vpp_format.format_vl_api_ip6_prefix_t(ip6_prefix)
106         self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
107
108         res = vpp_format.format_vl_api_ip6_prefix_t(ipv6_network)
109         self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
110
111     def test_format_vl_api_ip4_prefix_t(self):
112         res = vpp_format.format_vl_api_ip4_prefix_t(ip4_prefix)
113         self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
114
115         res = vpp_format.format_vl_api_ip4_prefix_t(ipv4_network)
116         self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
117
118     def test_format_vl_api_ip6_prefix_t_raises(self):
119         # PY2: raises socket.error
120         # PY3: raises OSError
121         with self.assertRaises((socket.error, OSError)):
122             res = vpp_format.format_vl_api_ip6_prefix_t(ip4_prefix)
123
124     def test_format_vl_api_ip4_prefix_t_raises(self):
125         # PY2: raises socket.error
126         # PY3: raises OSError
127         with self.assertRaises((socket.error, OSError)):
128             res = vpp_format.format_vl_api_ip4_prefix_t(ip6_prefix)