Add unit test to validate that when creating a string response in json, that characters such as \n or quotes are properly escaped.
Signed-off-by: Bruce Richardson <bruce.richard...@intel.com> --- app/test/test_telemetry_json.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c index 748b7cfe5a..955c2e5b1b 100644 --- a/app/test/test_telemetry_json.c +++ b/app/test/test_telemetry_json.c @@ -125,6 +125,22 @@ test_large_obj_element(void) return strncmp(expected, buf, sizeof(buf)); } +static int +test_string_char_escaping(void) +{ + static const char str[] = "A string across\ntwo lines and \"with quotes\"!"; + const char *expected = "\"A string across\\ntwo lines and \\\"with quotes\\\"!\""; + char buf[sizeof(str) + 10]; + int used = 0; + + used = rte_tel_json_str(buf, sizeof(buf), used, str); + printf("%s: buf = '%s', expected = '%s'\n", __func__, buf, expected); + if (used != (int)strlen(expected)) + return -1; + + return strncmp(expected, buf, sizeof(buf)); +} + typedef int (*test_fn)(void); static int @@ -138,6 +154,7 @@ test_telemetry_json(void) test_overflow_obj, test_large_array_element, test_large_obj_element, + test_string_char_escaping, }; for (i = 0; i < RTE_DIM(fns); i++) if (fns[i]() == 0) -- 2.34.1