aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOle Troan <ot@cisco.com>2017-10-31 14:50:13 +0100
committerNeale Ranns <nranns@cisco.com>2017-10-31 19:40:55 +0000
commitca3b6f1b4116010b1acbbb29a945767cb5443914 (patch)
tree93e4bdced73e8f97999688f348bcc4a92ac961cc
parent4f68e042b7bde7058a853afc9b97222cb448657a (diff)
VPP PAPI: Error in unserializer for non-array compound types.
A bug in the decoder of messages when there was a non-array compound type. The typical result was an error message from the struct library: "error:unpack_from requires a buffer of at least 4 bytes" Change-Id: Ie30fec6fc39b9f4177b54fa4adc4fc69674f0e12 Signed-off-by: Ole Troan <ot@cisco.com>
-rw-r--r--src/vpp-api/python/vpp_papi.py1
-rw-r--r--test/test_papi.py438
2 files changed, 439 insertions, 0 deletions
diff --git a/src/vpp-api/python/vpp_papi.py b/src/vpp-api/python/vpp_papi.py
index 7b66c0f4d37..67ac22a4d32 100644
--- a/src/vpp-api/python/vpp_papi.py
+++ b/src/vpp-api/python/vpp_papi.py
@@ -360,6 +360,7 @@ class VPP():
size += v[1].size
else:
if callable(v):
+ size = 0
(s, l) = v(self, False, buf, off, None)
res.append(l)
size += s
diff --git a/test/test_papi.py b/test/test_papi.py
index 1a5f6ae63e4..b5b5ac60a3c 100644
--- a/test/test_papi.py
+++ b/test/test_papi.py
@@ -1,5 +1,7 @@
import binascii
from framework import VppTestCase
+from vpp_papi import VPP
+import json
""" TestPAPI is a subclass of VPPTestCase classes.
@@ -29,3 +31,439 @@ class TestPAPI(VppTestCase):
node_name = 'X' * 100
self.assertRaises(ValueError, self.v.get_node_index,
node_name=node_name)
+
+
+class TestPAPIMessageParsing(VppTestCase):
+ """ PAPI Message parsing Test Case """
+
+ show_version_msg = '''["show_version",
+ ["u16", "_vl_msg_id"],
+ ["u32", "client_index"],
+ ["u32", "context"],
+ {"crc" : "0xf18f9480"}
+ ]'''
+
+ ip_address_details_msg = '''["ip_address_details",
+ ["u16", "_vl_msg_id"],
+ ["u32", "client_index"],
+ ["u32", "context"],
+ ["u8", "ip", 16],
+ ["u8", "prefix_length"],
+ {"crc" : "0x87d522a1"}
+ ]'''
+
+ cli_inband_msg = '''["cli_inband",
+ ["u16", "_vl_msg_id"],
+ ["u32", "client_index"],
+ ["u32", "context"],
+ ["u32", "length"],
+ ["u8", "cmd", 0, "length"],
+ {"crc" : "0x22345937"}
+ ]'''
+
+ def test_adding_new_message_object(self):
+ p = json.loads(TestPAPIMessageParsing.show_version_msg)
+ msglist = VPP(testmode=json)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ # Verify that message can be retrieved
+ self.assertTrue(msglist['show_version'])
+ self.assertFalse(msglist['foobar'])
+
+ # Test duplicate
+ self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
+
+ # Look at return tuple
+ self.assertTrue(msglist.ret_tup('show_version'))
+
+ def test_adding_new_message_object_with_array(self):
+ p = json.loads(TestPAPIMessageParsing.ip_address_details_msg)
+ msglist = VPP(testmode=True)
+ msglist.add_message(p[0], p[1:])
+
+ self.assertTrue(msglist['ip_address_details'])
+
+ def test_message_to_bytes(self):
+ msglist = VPP(testmode=True)
+ p = json.loads(TestPAPIMessageParsing.show_version_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ # Give me a byte string for given message and given arguments
+
+ b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123})
+ self.assertEqual(10, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(rv._0, 50)
+ self.assertEqual(rv.context, 123)
+
+ p = json.loads(TestPAPIMessageParsing.ip_address_details_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ # Give me a byte string for given message and given arguments
+ b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123,
+ 'ip': b'\xf0\xf1\xf2',
+ 'prefix_length': 12})
+ self.assertEqual(27, len(b))
+ rv = msglist.decode(msgdef, b)
+
+ self.assertEqual(rv.context, 123)
+ self.assertEqual(rv.ip, b'\xf0\xf1\xf2\x00\x00\x00' +
+ '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
+ self.assertEqual(rv.prefix_length, 12)
+
+ p = json.loads(TestPAPIMessageParsing.cli_inband_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ # Give me a byte string for given message and given arguments
+ b = msglist.encode(msgdef, {'_vl_msg_id': 50, 'context': 123,
+ 'length': 20,
+ 'cmd': 'show version verbose'})
+ self.assertEqual(34, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(rv._0, 50)
+ self.assertEqual(rv.context, 123)
+ self.assertEqual(rv.cmd.decode('ascii'), 'show version verbose')
+
+ variable_array_16_msg = '''["variable_array_16",
+ ["u32", "length"],
+ ["u16", "list", 0, "length"]
+ ]'''
+
+ p = json.loads(variable_array_16_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ # Give me a byte string for given message and given arguments
+ b = msglist.encode(msgdef, {'list': [1, 2], 'length': 2})
+ self.assertEqual(8, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+ self.assertEqual([1, 2], rv.list)
+
+ def test_add_new_types(self):
+ counter_type = '''["ip4_fib_counter",
+ ["u32", "address"],
+ ["u8", "address_length"],
+ ["u64", "packets"],
+ ["u64", "bytes"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ with_type_msg = '''["with_type_msg",
+ ["u32", "length"],
+ ["u16", "list", 0, "length"],
+ ["vl_api_ip4_fib_counter_t", "counter"]
+ ]'''
+
+ # Add new type
+ msglist = VPP(testmode=True)
+ p = json.loads(counter_type)
+ msglist.add_type(p[0], p[1:])
+ p = json.loads(with_type_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2],
+ 'counter': {'address': 4,
+ 'address_length': 12,
+ 'packets': 1235,
+ 'bytes': 5678}})
+ self.assertEqual(29, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+ self.assertEqual(5678, rv.counter.bytes)
+
+ def test_add_two_new_types(self):
+ mock_r1 = '''["mock_r1",
+ ["u32", "a1"],
+ {"crc" : "0xb2739495"}
+ ]'''
+ mock_r2 = '''["mock_r2",
+ ["u32", "a1"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ mock_msg = '''["mock_msg",
+ ["u32", "context"],
+ ["i32", "retval"],
+ ["vl_api_mock_r1_t", "r1"],
+ ["vl_api_mock_r2_t", "r2"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ # Add new type
+ msglist = VPP(testmode=True)
+ p = json.loads(mock_r1)
+ msglist.add_type(p[0], p[1:])
+ p = json.loads(mock_r2)
+ msglist.add_type(p[0], p[1:])
+ p = json.loads(mock_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'context': 2, 'retval': 0,
+ 'r1': {'a1': 4}, 'r2': {'a1': 12}})
+
+ self.assertEqual(16, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(4, rv.r1.a1)
+
+ def test_nested_array_type(self):
+ bier_type = '''["bier_table_id",
+ ["u8", "bt_set"],
+ ["u8", "bt_sub_domain"],
+ ["u8", "bt_hdr_len_id"],
+ {"crc" : "0xb2739495"}
+ ]'''
+ fib_path3 = '''["fib_path3",
+ ["u32", "sw_if_index"],
+ ["u8", "n_labels"],
+ ["u32", "label_stack", 0, "n_labels"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ bier_route_details = '''["bier_route_details",
+ ["u32", "client_index"],
+ ["vl_api_bier_table_id_t", "br_tbl_id"],
+ ["u32", "br_n_paths"],
+ ["vl_api_fib_path3_t", "br_paths", 0, "br_n_paths"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ # Add new type
+ msglist = VPP(testmode=True)
+
+ p = json.loads(bier_type)
+ msglist.add_type(p[0], p[1:])
+ p = json.loads(fib_path3)
+ msglist.add_type(p[0], p[1:])
+
+ p = json.loads(bier_route_details)
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ bt_tbl_id = {'bt_set': 1, 'bt_sub_domain': 2, 'bt_hdr_len_id': 3}
+ fib_path = {'sw_if_index': 1, 'n_labels': 2,
+ 'label_stack': [123, 456]}
+
+ b = msglist.encode(msgdef, {'client_index': 2,
+ 'br_tbl_id': bt_tbl_id,
+ 'br_n_paths': 2,
+ 'br_paths': [fib_path, fib_path]})
+ self.assertEqual(37, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual([123, 456], rv.br_paths[1].label_stack)
+ self.assertEqual(bt_tbl_id['bt_set'], rv.br_tbl_id.bt_set)
+
+ def test_add_new_compound_type_with_array(self):
+ counter_type = '''["ip4_fib_counter",
+ ["u32", "address"],
+ ["u8", "address_length"],
+ ["u64", "packets"],
+ ["u64", "bytes"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ with_type_msg = '''["with_type_msg",
+ ["u32", "length"],
+ ["u16", "list", 0, "length"],
+ ["vl_api_ip4_fib_counter_t", "counter", 2]
+
+ ]'''
+
+ # Add new type
+ msglist = VPP(testmode=True)
+ p = json.loads(counter_type)
+ msglist.add_type(p[0], p[1:])
+ p = json.loads(with_type_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2],
+ 'counter': [{'address': 4,
+ 'address_length': 12,
+ 'packets': 1235,
+ 'bytes': 5678},
+ {'address': 111,
+ 'address_length': 222,
+ 'packets': 333,
+ 'bytes': 444}]})
+ self.assertEqual(50, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual([1, 2], rv.list)
+ self.assertEqual(1235, rv.counter[0].packets)
+
+ with_type_variable_msg = '''["with_type_variable_msg",
+ ["u32", "length"],
+ ["vl_api_ip4_fib_counter_t", "counter", 0, "length"]
+
+ ]'''
+
+ p = json.loads(with_type_variable_msg)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2,
+ 'counter': [{'address': 4,
+ 'address_length': 12,
+ 'packets': 1235,
+ 'bytes': 5678},
+ {'address': 111,
+ 'address_length': 222,
+ 'packets': 333,
+ 'bytes': 444}]})
+ self.assertEqual(46, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+ self.assertEqual(1235, rv.counter[0].packets)
+ self.assertEqual(333, rv.counter[1].packets)
+
+ def test_simple_array(self):
+ msglist = VPP(testmode=True)
+
+ simple_byte_array = '''["simple_byte_array",
+ ["u32", "length"],
+ ["u8", "namecommand", 64]
+
+ ]'''
+ p = json.loads(simple_byte_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2, 'namecommand': 'foobar'})
+ self.assertEqual(68, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+
+ simple_array = '''["simple_array",
+ ["u32", "length"],
+ ["u32", "list", 2]
+
+ ]'''
+ p = json.loads(simple_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]})
+ self.assertEqual(12, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+ self.assertEqual([1, 2], rv.list)
+
+ simple_variable_array = '''["simple_variable_array",
+ ["u32", "length"],
+ ["u32", "list", 0, "length"]
+
+ ]'''
+ p = json.loads(simple_variable_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 2, 'list': [1, 2]})
+ self.assertEqual(12, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(2, rv.length)
+ self.assertEqual([1, 2], rv.list)
+
+ simple_variable_byte_array = '''["simple_variable_byte_array",
+ ["u32", "length"],
+ ["u8", "list", 0, "length"]
+ ]'''
+ p = json.loads(simple_variable_byte_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 6, 'list': 'foobar'})
+ self.assertEqual(10, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(6, rv.length)
+ self.assertEqual('foobar', rv.list)
+
+ def test_old_vla_array(self):
+ msglist = VPP(testmode=True)
+
+ # VLA
+ vla_byte_array = '''["vla_byte_array",
+ ["u32", "foobar"],
+ ["u32", "list", 2],
+ ["u32", "propercount"],
+ ["u8", "propermask", 0, "propercount"],
+ ["u8", "oldmask", 0],
+ {"crc" : "0xb2739495"}
+ ]'''
+ p = json.loads(vla_byte_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'list': [123, 456], 'oldmask': b'foobar',
+ 'propercount': 2,
+ 'propermask': [8, 9]})
+ self.assertEqual(24, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(b'foobar', rv.oldmask)
+
+ def test_old_vla_array_not_last_member(self):
+ msglist = VPP(testmode=True)
+
+ # VLA
+ vla_byte_array = '''["vla_byte_array",
+ ["u8", "oldmask", 0],
+ ["u32", "foobar"],
+ {"crc" : "0xb2739495"}
+ ]'''
+ p = json.loads(vla_byte_array)
+ self.assertRaises(ValueError, msglist.add_message, p[0], p[1:])
+
+ def test_old_vla_array_u32(self):
+ msglist = VPP(testmode=True)
+
+ # VLA
+ vla_byte_array = '''["vla_byte_array",
+ ["u32", "foobar"],
+ ["u32", "oldmask", 0],
+ {"crc" : "0xb2739495"}
+ ]'''
+ p = json.loads(vla_byte_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'foobar': 123,
+ 'oldmask': [123, 456, 789]})
+ self.assertEqual(16, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual([123, 456, 789], rv.oldmask)
+
+ def test_old_vla_array_compound(self):
+ msglist = VPP(testmode=True)
+
+ # VLA
+ counter_type = '''["ip4_fib_counter",
+ ["u32", "address"],
+ ["u8", "address_length"],
+ ["u64", "packets"],
+ ["u64", "bytes"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ vla_byte_array = '''["vla_byte_array",
+ ["vl_api_ip4_fib_counter_t", "counter", 0],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ p = json.loads(counter_type)
+ msglist.add_type(p[0], p[1:])
+
+ p = json.loads(vla_byte_array)
+ with self.assertRaises(NotImplementedError):
+ msgdef = msglist.add_message(p[0], p[1:])
+
+ def test_array_count_not_previous(self):
+ msglist = VPP(testmode=True)
+
+ # VLA
+ vla_byte_array = '''["vla_byte_array",
+ ["u32", "count"],
+ ["u32", "filler"],
+ ["u32", "lst", 0, "count"],
+ {"crc" : "0xb2739495"}
+ ]'''
+
+ p = json.loads(vla_byte_array)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'count': 3, 'lst': [1, 2, 3],
+ 'filler': 1})
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(rv.lst, [1, 2, 3])
+
+ def test_argument_name(self):
+ msglist = VPP(testmode=True)
+
+ simple_name = '''["simple_name",
+ ["u32", "length"],
+ ["u8", "name"]
+ ]'''
+ p = json.loads(simple_name)
+ msgdef = msglist.add_message(p[0], p[1:])
+ b = msglist.encode(msgdef, {'length': 6, 'name': 1})
+ self.assertEqual(5, len(b))
+ rv = msglist.decode(msgdef, b)
+ self.assertEqual(6, rv.length)
+ self.assertEqual(1, rv.name)