api: enforce vla is last and fixed string type
[vpp.git] / src / vpp-api / python / vpp_papi / tests / test_vpp_serializer.py
index 4fbda2a..6ca8e6c 100755 (executable)
@@ -3,13 +3,78 @@
 import unittest
 from vpp_papi.vpp_serializer import VPPType, VPPEnumType
 from vpp_papi.vpp_serializer import VPPUnionType, VPPMessage
-from vpp_papi.vpp_serializer import VPPTypeAlias
+from vpp_papi.vpp_serializer import VPPTypeAlias, VPPSerializerValueError
 from socket import inet_pton, AF_INET, AF_INET6
 import logging
 import sys
 from ipaddress import *
 
 
+class TestLimits(unittest.TestCase):
+    def test_string(self):
+        fixed_string = VPPType('fixed_string',
+                               [['string', 'name', 16]])
+
+        b = fixed_string.pack({'name': 'foobar'})
+        self.assertEqual(len(b), 16)
+
+        # Ensure string is nul terminated
+        self.assertEqual(b.decode('ascii')[6], '\x00')
+
+        nt, size = fixed_string.unpack(b)
+        self.assertEqual(size, 16)
+        self.assertEqual(nt.name, 'foobar')
+
+        # Empty string
+        b = fixed_string.pack({'name': ''})
+        self.assertEqual(len(b), 16)
+        nt, size = fixed_string.unpack(b)
+        self.assertEqual(size, 16)
+        self.assertEqual(nt.name, '')
+
+        # String too long
+        with self.assertRaises(VPPSerializerValueError):
+            b = fixed_string.pack({'name': 'foobarfoobar1234'})
+
+        variable_string = VPPType('variable_string',
+                                  [['string', 'name', 0]])
+        b = variable_string.pack({'name': 'foobar'})
+        self.assertEqual(len(b), 4 + len('foobar'))
+
+        nt, size = variable_string.unpack(b)
+        self.assertEqual(size, 4 + len('foobar'))
+        self.assertEqual(nt.name, 'foobar')
+        self.assertEqual(len(nt.name), len('foobar'))
+
+
+    def test_limit(self):
+        limited_type = VPPType('limited_type_t',
+                               [['string', 'name', 0, {'limit': 16}]])
+        unlimited_type = VPPType('limited_type_t',
+                                 [['string', 'name', 0]])
+
+        b = limited_type.pack({'name': 'foobar'})
+        self.assertEqual(len(b), 10)
+        b = unlimited_type.pack({'name': 'foobar'})
+        self.assertEqual(len(b), 10)
+
+        with self.assertRaises(VPPSerializerValueError):
+            b = limited_type.pack({'name': 'foobar'*3})
+
+
+class TestDefaults(unittest.TestCase):
+    def test_defaults(self):
+        default_type = VPPType('default_type_t',
+                               [['u16', 'mtu', {'default': 1500, 'limit': 0}]])
+
+        b = default_type.pack({})
+        self.assertEqual(len(b), 2)
+
+        nt, size = default_type.unpack(b)
+        self.assertEqual(len(b), size)
+        self.assertEqual(nt.mtu, 1500)
+
+
 class TestAddType(unittest.TestCase):
 
     def test_union(self):
@@ -110,7 +175,7 @@ class TestAddType(unittest.TestCase):
 
         prefix = VPPType('vl_api_prefix_t',
                          [['vl_api_address_t', 'address'],
-                          ['u8', 'address_length']])
+                          ['u8', 'len']])
         message = VPPMessage('svs',
                              [['vl_api_prefix_t', 'prefix']])
         message_addr = VPPMessage('svs_address',