[PATCH 4/4] test: self: add json parser test

Ahmad Fatoum a.fatoum at pengutronix.de
Tue Jan 10 00:49:30 PST 2023


Newly added JSON parsing code doesn't yet have a consumer in-tree.
Add a self-test to exercise the API for now.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 include/bselftest.h |   3 +-
 test/self/Kconfig   |   5 ++
 test/self/Makefile  |   1 +
 test/self/json.c    | 146 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 154 insertions(+), 1 deletion(-)
 create mode 100644 test/self/json.c

diff --git a/include/bselftest.h b/include/bselftest.h
index f03c803b6553..2bae6cd1393d 100644
--- a/include/bselftest.h
+++ b/include/bselftest.h
@@ -7,7 +7,8 @@
 #include <init.h>
 
 enum bselftest_group {
-	BSELFTEST_core
+	BSELFTEST_core,
+	BSELFTEST_parser,
 };
 
 struct selftest {
diff --git a/test/self/Kconfig b/test/self/Kconfig
index 5c6981959921..ce5048c70ec9 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -35,6 +35,7 @@ config SELFTEST_ENABLE_ALL
 	select SELFTEST_ENVIRONMENT_VARIABLES if ENVIRONMENT_VARIABLES
 	imply SELFTEST_FS_RAMFS
 	imply SELFTEST_TFTP
+	imply SELFTEST_JSON
 	help
 	  Selects all self-tests compatible with current configuration
 
@@ -64,4 +65,8 @@ config SELFTEST_FS_RAMFS
 	bool "ramfs selftest"
 	depends on FS_RAMFS
 
+config SELFTEST_JSON
+	bool "JSON selftest"
+	depends on JSMN
+
 endif
diff --git a/test/self/Makefile b/test/self/Makefile
index c7c729cba0e0..4d2c0374c9c3 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_SELFTEST_PROGRESS_NOTIFIER) += progress-notifier.o
 obj-$(CONFIG_SELFTEST_OF_MANIPULATION) += of_manipulation.o of_manipulation.dtb.o
 obj-$(CONFIG_SELFTEST_ENVIRONMENT_VARIABLES) += envvar.o
 obj-$(CONFIG_SELFTEST_FS_RAMFS) += ramfs.o
+obj-$(CONFIG_SELFTEST_JSON) += json.o
diff --git a/test/self/json.c b/test/self/json.c
new file mode 100644
index 000000000000..54323cf4350c
--- /dev/null
+++ b/test/self/json.c
@@ -0,0 +1,146 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <common.h>
+#include <bselftest.h>
+#include <jsmn.h>
+
+BSELFTEST_GLOBALS();
+
+static const jsmntok_t*
+__json_expect(const char *json, const jsmntok_t *token, const char **lookup,
+	      jsmntype_t expected_type, const char *expected_value)
+{
+	bool success = true;
+
+	total_tests++;
+
+	if (token->type != expected_type) {
+		failed_tests++;
+		printf("%pJP: type mismatch: got %d, but %d expected\n",
+		       lookup, token->type, expected_type);
+		success = false;
+	}
+
+	if (!expected_value)
+		goto out;
+
+	total_tests++;
+
+	if (!jsmn_eq(expected_value, json, token)) {
+		failed_tests++;
+		printf("%pJP: string mismatch: got %.*s, but %s expected\n",
+		       lookup, (int)(token->end - token->start),
+		       &json[token->start], expected_value);
+		success = false;
+	}
+
+out:
+	return success ? token : NULL;
+}
+
+static const jsmntok_t*
+json_expect(const char *json, const jsmntok_t *tokens, const char **lookup,
+	    jsmntype_t expected_type, const char *expected_value)
+{
+	const jsmntok_t *token;
+
+	total_tests++;
+
+	token = jsmn_locate(lookup, json, tokens);
+	if (!token) {
+		failed_tests++;
+		printf("%pJP: couldn't be located\n", lookup);
+		return NULL;
+	}
+
+	return __json_expect(json, token, lookup, expected_type, expected_value);
+}
+
+#define JP(...)	(const char *[]) { __VA_ARGS__, NULL }
+
+/* Wonky indentation is intended */
+static const char json[] =
+"{\n"
+"	\"null\" :null,\"number\" : 0x42,\n"
+"	\"object\": {\n"
+"		\"a\": \"b\",\n"
+"		\"C\": \"dE\","
+"		\"e\": \"\"\n"
+"	},"
+"	\"array\": [ \"1\",\"2\",\t\t\"3\"],\n"
+"	\"boolean\": true,\n"
+"\"string\": \"Hello World\n\"}\n";
+
+static void test_json(void)
+{
+	ssize_t token_count;
+	const jsmntok_t *token;
+	jsmntok_t *tokens;
+	jsmn_parser parser;
+	char *string;
+	int ret;
+
+	total_tests++;
+
+	jsmn_init(&parser);
+
+	/* Figure out how many tokens we need. */
+	ret = jsmn_parse(&parser, json, sizeof(json), NULL, 0);
+	if (ret < 0) {
+		printf("failed to determine number of tokens: %d\n", ret);
+		failed_tests++;
+		return;
+	}
+
+	token_count = ret;
+
+	/* `token_count` is strictly less than `length` which is strictly less
+	 * than CONFIG_SYS_EEPROM_SIZE (or 8K), so we should never overflow an
+	 * int here.
+	 */
+	tokens = kmalloc_array(token_count, sizeof(jsmntok_t), GFP_KERNEL);
+	if (WARN_ON(!tokens))
+		return;
+
+	total_tests++;
+
+	jsmn_init(&parser);
+	ret = jsmn_parse(&parser, json, sizeof(json), tokens, token_count);
+	if (ret < 0) {
+		printf("failed to parse JSON with tokens: %d\n", ret);
+		failed_tests++;
+		goto out;
+	}
+
+	json_expect(json, tokens, JP("null"), JSMN_PRIMITIVE, "null");
+	json_expect(json, tokens, JP("number"), JSMN_PRIMITIVE, "0x42");
+	json_expect(json, tokens, JP("object"), JSMN_OBJECT, NULL);
+	json_expect(json, tokens, JP("object", "a"), JSMN_STRING, "b");
+	json_expect(json, tokens, JP("object", "C"), JSMN_STRING, "dE");
+	json_expect(json, tokens, JP("object", "e"), JSMN_STRING, "");
+
+	token = json_expect(json, tokens, JP("array"), JSMN_ARRAY, NULL);
+
+	token = jsmn_skip_value(token);
+	__json_expect(json, token, JP("boolean"), JSMN_STRING, "boolean");
+
+	token = jsmn_skip_value(token);
+	__json_expect(json, token, JP("boolean"), JSMN_PRIMITIVE, "true");
+
+	string = jsmn_strcpy(JP("string"), json, tokens);
+	if (WARN_ON(!string))
+		return;
+
+	total_tests++;
+	if (strcmp(string, "Hello World\n")) {
+		failed_tests++;
+		printf("%pJP: string mismatch\n", JP("string"));
+	}
+
+	free(string);
+out:
+	free(tokens);
+}
+bselftest(parser, test_json);
-- 
2.30.2




More information about the barebox mailing list