tests: replace pycodestyle with black
[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"}, "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,
37 }
38 ip4_addr_format_vl_api_prefix_packed_t = {
39     "address": b"\x01\x02\x03\x04",
40     "len": ip4_prefix_len,
41 }
42
43 ip6_addr = "dead::"
44 ip6_addrn = b"\xde\xad\x00\x00\x00\x00\x00\x00" 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 = {
49     "un": {
50         "ip6": b"\xde\xad\x00\x00"
51         b"\x00\x00\x00\x00"
52         b"\x00\x00\x00\x00"
53         b"\x00\x00\x00\x00"
54     },
55     "af": 1,
56 }
57 ip6_addr_format_vl_api_prefix_t = {
58     "address": {  # noqa: E127
59         "af": 1,
60         "un": {
61             "ip6": b"\xde\xad\x00\x00"
62             b"\x00\x00\x00\x00"
63             b"\x00\x00\x00\x00"
64             b"\x00\x00\x00\x00"
65         },
66     },
67     "len": ip6_prefix_len,
68 }
69 ip6_addr_format_vl_api_prefix_packed_t = {
70     "address": b"\xde\xad\x00\x00"  # noqa: E127,E501
71     b"\x00\x00\x00\x00"
72     b"\x00\x00\x00\x00"
73     b"\x00\x00\x00\x00",
74     "len": ip6_prefix_len,
75 }
76
77
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)
82
83         # PY2: raises socket.error
84         # PY3: raises OSError
85         with self.assertRaises((TypeError, socket.error, OSError)):
86             res = vpp_format.format_vl_api_address_t(ip4_addrn)
87
88         res = vpp_format.format_vl_api_address_t(ip6_addr)
89         self.assertEqual(res, ip6_addr_format_vl_api_address_t)
90
91         with self.assertRaises(TypeError):
92             es = vpp_format.format_vl_api_address_t(ip6_addrn)
93
94     @parameterized.expand(
95         [
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),
100         ]
101     )
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)
105
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)
109
110         res = vpp_format.format_vl_api_ip6_prefix_t(ipv6_network)
111         self.assertEqual(res, ip6_addr_format_vl_api_prefix_packed_t)
112
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)
116
117         res = vpp_format.format_vl_api_ip4_prefix_t(ipv4_network)
118         self.assertEqual(res, ip4_addr_format_vl_api_prefix_packed_t)
119
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)
125
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)