diff options
author | Jakub Grajciar <jgrajcia@cisco.com> | 2020-02-07 11:30:26 +0100 |
---|---|---|
committer | Ole Trøan <otroan@employees.org> | 2020-02-26 08:51:03 +0000 |
commit | 2dbee9361e74d03727a8b618ba80a5e28c006011 (patch) | |
tree | 443b6c39e99e0e46b62ef0dfd002e31bb1fa7665 /test/ext/vapi_c_test.c | |
parent | 8e755a16a71c55555f12381c8a12e22ae7138536 (diff) |
api: improve api string safety
- Remove vl_api_from_api_string
to prevent use of not nul-terminated strings.
- Rename vl_api_from_api_to_vec -> vl_api_from_api_to_new_vec
to imply a new vector is created. NOT nul terminated.
- Add vl_api_from_api_to_new_c_string. Returns
nul terminated string in a new vector.
- Add vl_api_c_string_to_api_string. Convert nul terminated
string to vl_api_string_t
- Add vl_api_vec_to_api_string. Convert NON nul terminated
vector to vl_api_string_t
Type: fix
Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Change-Id: Iadd59b612c0d960a34ad0dd07a9d17f56435c6ea
Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Diffstat (limited to 'test/ext/vapi_c_test.c')
-rw-r--r-- | test/ext/vapi_c_test.c | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/test/ext/vapi_c_test.c b/test/ext/vapi_c_test.c index a9572ae8716..51fc572746f 100644 --- a/test/ext/vapi_c_test.c +++ b/test/ext/vapi_c_test.c @@ -29,6 +29,9 @@ #include <vapi/l2.api.vapi.h> #include <fake.api.vapi.h> +#include <vppinfra/vec.h> +#include <vppinfra/mem.h> + DEFINE_VAPI_MSG_IDS_VPE_API_JSON; DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON; DEFINE_VAPI_MSG_IDS_L2_API_JSON; @@ -911,6 +914,66 @@ START_TEST (test_unsupported) END_TEST; +START_TEST (test_api_strings) +{ + printf ("--- Invalid api strings ---\n"); + + /* test string 'TEST' + * size = 5 + * length = 4 + */ + const char str[] = "TEST"; + u8 *vec_str = 0, *vstr = 0; + char *cstr; + + vapi_msg_sw_interface_dump *dump = + malloc (sizeof (vapi_msg_sw_interface_dump) + strlen (str)); + clib_mem_init (0, 1 << 20); + + vl_api_c_string_to_api_string (str, &dump->payload.name_filter); + /* Assert nul terminator NOT present */ + ck_assert_int_eq (vl_api_string_len (&dump->payload.name_filter), + strlen (str)); + + cstr = vl_api_from_api_to_new_c_string (&dump->payload.name_filter); + ck_assert_ptr_ne (cstr, NULL); + /* Assert nul terminator present */ + ck_assert_int_eq (vec_len (cstr), sizeof (str)); + ck_assert_int_eq (strlen (str), strlen (cstr)); + vec_free (cstr); + + vstr = vl_api_from_api_to_new_vec (&dump->payload.name_filter); + ck_assert_ptr_ne (vstr, NULL); + /* Assert nul terminator NOT present */ + ck_assert_int_eq (vec_len (vstr), strlen (str)); + vec_free (vstr); + + /* vector conaining NON nul terminated string 'TEST' */ + vec_add (vec_str, str, strlen (str)); + clib_memset (dump->payload.name_filter.buf, 0, strlen (str)); + dump->payload.name_filter.length = 0; + + vl_api_vec_to_api_string (vec_str, &dump->payload.name_filter); + /* Assert nul terminator NOT present */ + ck_assert_int_eq (vl_api_string_len (&dump->payload.name_filter), + vec_len (vec_str)); + + cstr = vl_api_from_api_to_new_c_string (&dump->payload.name_filter); + ck_assert_ptr_ne (cstr, NULL); + /* Assert nul terminator present */ + ck_assert_int_eq (vec_len (cstr), sizeof (str)); + ck_assert_int_eq (strlen (str), strlen (cstr)); + vec_free (cstr); + + vstr = vl_api_from_api_to_new_vec (&dump->payload.name_filter); + ck_assert_ptr_ne (vstr, NULL); + /* Assert nul terminator NOT present */ + ck_assert_int_eq (vec_len (vstr), strlen (str)); + vec_free (vstr); +} + +END_TEST; + Suite * test_suite (void) { @@ -957,6 +1020,10 @@ test_suite (void) tcase_add_test (tc_unsupported, test_unsupported); suite_add_tcase (s, tc_unsupported); + TCase *tc_dynamic = tcase_create ("Dynamic message size"); + tcase_add_test (tc_dynamic, test_api_strings); + suite_add_tcase (s, tc_dynamic); + return s; } |