summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAhmad Fatoum <a.fatoum@pengutronix.de>2023-01-10 09:49:30 +0100
committerSascha Hauer <s.hauer@pengutronix.de>2023-01-10 16:10:18 +0100
commit944ed14b53454a7bd63891d487af2272eddc5e2f (patch)
treeacd6a2f86ba62cdc64803154942c54a2ccfc8c37 /test
parentef242750c534f91c13bac1d49b8ebcf701814dda (diff)
downloadbarebox-944ed14b53454a7bd63891d487af2272eddc5e2f.tar.gz
test: self: add json parser test
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> Link: https://lore.barebox.org/20230110084930.3439001-5-a.fatoum@pengutronix.de Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'test')
-rw-r--r--test/self/Kconfig5
-rw-r--r--test/self/Makefile1
-rw-r--r--test/self/json.c146
3 files changed, 152 insertions, 0 deletions
diff --git a/test/self/Kconfig b/test/self/Kconfig
index 5c69819599..ce5048c70e 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 c7c729cba0..4d2c0374c9 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 0000000000..54323cf435
--- /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);