[RFC,6/6] test/telemetry-json: add test case for escaping strings in arrays

Message ID 20220623164245.561371-7-bruce.richardson@intel.com (mailing list archive)
State Superseded, archived
Delegated to: Thomas Monjalon
Headers
Series add json string escaping to telemetry |

Checks

Context Check Description
ci/checkpatch warning coding style issues
ci/Intel-compilation success Compilation OK
ci/intel-Testing success Testing PASS

Commit Message

Bruce Richardson June 23, 2022, 4:42 p.m. UTC
  Add test-case to validate that when adding strings to arrays, the
strings are properly escaped to remove any invalid characters.

Signed-off-by: Bruce Richardson <bruce.richardson@intel.com>
---
 app/test/test_telemetry_json.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)
  

Patch

diff --git a/app/test/test_telemetry_json.c b/app/test/test_telemetry_json.c
index 955c2e5b1b..642ae9d6e1 100644
--- a/app/test/test_telemetry_json.c
+++ b/app/test/test_telemetry_json.c
@@ -141,6 +141,29 @@  test_string_char_escaping(void)
 	return strncmp(expected, buf, sizeof(buf));
 }
 
+static int
+test_array_char_escaping(void)
+{
+	/* "meaning of life", with tab between first two words, \n at end, and "life" in quotes,
+	 * followed by "all the fish" in quotes */
+	const char *expected = "[\"meaning\\tof \\\"life\\\"\\n\",\"\\\"all the fish\\\"\"]";
+	char buf[1024];
+	int used = 0;
+
+	printf("%s: ", __func__);
+	used = rte_tel_json_empty_array(buf, sizeof(buf), used);
+	if (used != 2 || strcmp(buf, "[]"))
+		return -1;
+
+	used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "meaning\tof \"life\"\n");
+	used = rte_tel_json_add_array_string(buf, sizeof(buf), used, "\"all the fish\"");
+
+	printf("buf = '%s', expected = '%s'\n", buf, expected);
+	if (used != (int)strlen(expected))
+		return -1;
+	return strncmp(expected, buf, sizeof(buf));
+}
+
 typedef int (*test_fn)(void);
 
 static int
@@ -155,6 +178,7 @@  test_telemetry_json(void)
 			test_large_array_element,
 			test_large_obj_element,
 			test_string_char_escaping,
+			test_array_char_escaping,
 	};
 	for (i = 0; i < RTE_DIM(fns); i++)
 		if (fns[i]() == 0)