6b867f9e6feaa4a1e68ef617e3dcc23813130c51
[vpp.git] / src / vpp-api / python / tests / test_vpp_serializer.py
1 #!/usr/bin/env python3
2
3 import unittest
4 from vpp_serializer import VPPType, VPPEnumType, VPPUnionType
5 from socket import inet_pton, AF_INET, AF_INET6
6 import logging
7
8 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
9 es_logger = logging.getLogger('vpp_serializer')
10 es_logger.setLevel(logging.DEBUG)
11
12 class VPPMessage(VPPType):
13     pass
14
15
16 class TestAddType(unittest.TestCase):
17
18     def test_union(self):
19         un = VPPUnionType('test_union',
20                           [['u8', 'is_bool'],
21                            ['u32', 'is_int']])
22
23         b = un.pack({'is_int': 0x1234})
24         self.assertEqual(len(b), 4)
25         nt = un.unpack(b)
26         self.assertEqual(nt.is_bool, 52)
27         self.assertEqual(nt.is_int, 0x1234)
28
29     def test_address(self):
30         af = VPPEnumType('vl_api_address_family_t', [["ADDRESS_IP4", 0],
31                                                      ["ADDRESS_IP6", 1],
32                                                      {"enumtype": "u32"}])
33         ip4 = VPPType('vl_api_ip4_address_t', [['u8', 'address', 4]])
34         ip6 = VPPType('vl_api_ip6_address_t', [['u8', 'address', 16]])
35         VPPUnionType('vl_api_address_union_t',
36                      [["vl_api_ip4_address_t", "ip4"],
37                       ["vl_api_ip6_address_t", "ip6"]])
38
39         address = VPPType('address', [['vl_api_address_family_t', 'af'],
40                                       ['vl_api_address_union_t', 'un']])
41
42         b = ip4.pack({'address': inet_pton(AF_INET, '1.1.1.1')})
43         self.assertEqual(len(b), 4)
44         nt = ip4.unpack(b)
45         self.assertEqual(nt.address, inet_pton(AF_INET, '1.1.1.1'))
46
47         b = ip6.pack({'address': inet_pton(AF_INET6, '1::1')})
48         self.assertEqual(len(b), 16)
49
50         b = address.pack({'af': af.ADDRESS_IP4,
51                           'un':
52                           {'ip4':
53                            {'address': inet_pton(AF_INET, '2.2.2.2')}}})
54         self.assertEqual(len(b), 20)
55
56         nt = address.unpack(b)
57         print('NT union', nt)
58         self.assertEqual(nt.af, af.ADDRESS_IP4)
59         self.assertEqual(nt.un.ip4.address,
60                          inet_pton(AF_INET, '2.2.2.2'))
61         self.assertEqual(nt.un.ip6.address,
62                          inet_pton(AF_INET6, '::0202:0202'))
63
64     def test_arrays(self):
65         # Test cases
66         # 1. Fixed list
67         # 2. Fixed list of variable length sub type
68         # 3. Variable length type
69         #
70         ip4 = VPPType('ip4_address', [['u8', 'address', 4]])
71         listip4 = VPPType('list_ip4_t', [['ip4_address', 'addresses', 4]])
72         valistip4 = VPPType('list_ip4_t',
73                             [['u8', 'count'],
74                              ['ip4_address', 'addresses', 0, 'count']])
75
76         valistip4_legacy = VPPType('list_ip4_t',
77                                    [['u8', 'foo'],
78                                     ['ip4_address', 'addresses', 0]])
79         
80         addresses = []
81         for i in range(4):
82             addresses.append({'address': inet_pton(AF_INET, '2.2.2.2')})
83         b = listip4.pack({'addresses': addresses})
84         self.assertEqual(len(b), 16)
85         nt = listip4.unpack(b)
86
87         self.assertEqual(nt.addresses[0].address,
88                          inet_pton(AF_INET, '2.2.2.2'))
89
90         b = valistip4.pack({'count': len(addresses), 'addresses': addresses})
91         self.assertEqual(len(b), 17)
92
93         nt = valistip4.unpack(b)
94         print('NT', nt)
95
96         b = valistip4_legacy.pack({'foo': 1, 'addresses': addresses})
97         self.assertEqual(len(b), 17)
98         nt = valistip4_legacy.unpack(b)
99         print('NT', nt)
100
101         
102     def test_message(self):
103         foo = VPPMessage('foo', [['u16', '_vl_msg_id'],
104                                  ['u8', 'client_index'],
105                                  ['u8', 'something'],
106                                  {"crc": "0x559b9f3c"}])
107         b = foo.pack({'_vl_msg_id': 1, 'client_index': 5,
108                       'something': 200})
109         self.assertEqual(len(b), 4)
110         nt = foo.unpack(b)
111         print('NT', nt)
112
113 if __name__ == '__main__':
114     unittest.main()