mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/4] test: self: add json parser test
Date: Tue, 10 Jan 2023 09:49:30 +0100	[thread overview]
Message-ID: <20230110084930.3439001-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230110084930.3439001-1-a.fatoum@pengutronix.de>

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@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




  parent reply	other threads:[~2023-01-10  8:51 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-10  8:49 [PATCH 0/4] lib: add jsmn JSON parser Ahmad Fatoum
2023-01-10  8:49 ` [PATCH 1/4] lib: add jsmn JSON parser support Ahmad Fatoum
2023-01-10  8:49 ` [PATCH 2/4] lib: extend jsmn with simple JSONPath lookup helpers Ahmad Fatoum
2023-01-10  8:49 ` [PATCH 3/4] vsprintf: implement %pJP for printing JSONPaths Ahmad Fatoum
2023-01-10  8:49 ` Ahmad Fatoum [this message]
2023-01-10 15:10 ` [PATCH 0/4] lib: add jsmn JSON parser Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230110084930.3439001-5-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox