diff mbox series

[RFC,4/6] test/telemetry_json: add test for string character escaping

Message ID 20220623164245.561371-5-bruce.richardson@intel.com (mailing list archive)
State New
Delegated to: Thomas Monjalon
Headers show
Series add json string escaping to telemetry | expand

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Bruce Richardson June 23, 2022, 4:42 p.m. UTC
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.richardson@intel.com>
---
 app/test/test_telemetry_json.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
diff mbox series

Patch

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)