def test_add_two_new_types(self): """ Add new types 2 """ 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_add_new_types(self): """ Add new types """ 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_new_compound_type_with_array(self): """ New compound type with array """ 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_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_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:])