]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
Add tests for the json_parse cli tool.
authorEric Hawicz <erh+git@nimenees.com>
Sun, 21 Jun 2026 17:44:59 +0000 (13:44 -0400)
committerEric Hawicz <erh+git@nimenees.com>
Sun, 21 Jun 2026 17:44:59 +0000 (13:44 -0400)
apps/json_parse.c
tests/CMakeLists.txt
tests/test_json_parse_cli.expected [new file with mode: 0644]
tests/test_json_parse_cli.test [new file with mode: 0755]

index f797d273f639f9f14cf91fa760079b7f4b1b1171..ebb0f257d95e86bb4e772187b8cd15b5992f2172 100644 (file)
@@ -39,6 +39,7 @@
 
 static int formatted_output = JSON_C_TO_STRING_SPACED;
 static int show_output = 1;
+static int show_diag = 1;
 static int strict_mode = 0;
 static int validate_utf8 = 0;
 static int tokener_flags = 0;
@@ -56,6 +57,8 @@ static int showobj(struct json_object *new_obj);
 
 static void showmem(void)
 {
+       if (!show_diag)
+               return;
 #ifdef HAVE_GETRUSAGE
        struct rusage rusage;
        memset(&rusage, 0, sizeof(rusage));
@@ -171,10 +174,11 @@ static void usage(const char *argv0, int exitval, const char *errmsg)
                fp = stderr;
        if (errmsg != NULL)
                fprintf(fp, "ERROR: %s\n\n", errmsg);
-       fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [filename]\n", argv0);
+       fprintf(fp, "Usage: %s [-f|-F <arg>] [-n] [-s] [-u] [-N] [filename]\n", argv0);
        fprintf(fp, "  -f - Format the output to stdout with JSON_C_TO_STRING_PRETTY (default is JSON_C_TO_STRING_SPACED)\n");
        fprintf(fp, "  -F - Format the output to stdout with <arg>, e.g. 0 for JSON_C_TO_STRING_PLAIN\n");
        fprintf(fp, "  -n - No output\n");
+       fprintf(fp, "  -N - Omit diagnostic information, such as memory usage\n");
        fprintf(fp, "  -c - Set JSON_C_TO_STRING_COLOR to colorize the output\n");
        fprintf(fp, "  -P - Initialize tokener flags to the given value\n");
        fprintf(fp, "  -s - Parse in strict mode, add flags:\n");
@@ -191,7 +195,7 @@ int main(int argc, char **argv)
 {
        int opt;
 
-       while ((opt = getopt(argc, argv, "cfF:hnP:su")) != -1)
+       while ((opt = getopt(argc, argv, "cfF:hnNP:su")) != -1)
        {
                switch (opt)
                {
@@ -199,6 +203,7 @@ int main(int argc, char **argv)
                case 'f': formatted_output = JSON_C_TO_STRING_PRETTY; break;
                case 'F': formatted_output = atoi(optarg); break;
                case 'n': show_output = 0; break;
+               case 'N': show_diag = 0; break;
                case 'P': tokener_flags = atoi(optarg); break;
                case 's': strict_mode = 1; break;
                case 'u': validate_utf8 = 1; break;
index caff03f889bbb78efd42d50bb5903a5fbd0256bb..5d6217131d04bd3724e61e2e6aa78f9b83e78e78 100644 (file)
@@ -38,6 +38,10 @@ set(ALL_TEST_NAMES
     test_visit
     test_object_iterator)
 
+set(NOBUILD_TEST_NAMES
+    test_json_parse_cli
+)
+
 if (NOT DISABLE_JSON_POINTER)
     set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_json_pointer)
     set(ALL_TEST_NAMES ${ALL_TEST_NAMES} test_safe_json_pointer_set)
@@ -46,6 +50,10 @@ if (NOT DISABLE_JSON_POINTER)
     endif()
 endif()
 
+foreach(TESTNAME ${NOBUILD_TEST_NAMES})
+add_test(NAME ${TESTNAME} COMMAND ${PROJECT_SOURCE_DIR}/tests/${TESTNAME}.test)
+endforeach(TESTNAME)
+
 foreach(TESTNAME ${ALL_TEST_NAMES})
 
 add_executable(${TESTNAME} ${TESTNAME}.c)
diff --git a/tests/test_json_parse_cli.expected b/tests/test_json_parse_cli.expected
new file mode 100644 (file)
index 0000000..c1eeca4
--- /dev/null
@@ -0,0 +1,2 @@
+Successfully parsed object from stdin
+Failed at offset 71: invalid utf-8 string: char=0x00
diff --git a/tests/test_json_parse_cli.test b/tests/test_json_parse_cli.test
new file mode 100755 (executable)
index 0000000..0982afe
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+# Common definitions
+if test -z "$srcdir"; then
+    srcdir="${0%/*}"
+    test "$srcdir" = "$0" && srcdir=.
+    test -z "$srcdir" && srcdir=.
+fi
+. "$srcdir/test-defs.sh"
+
+set -x -v
+
+echo -n '"tenant=blue;note=CANARY_STACK_WINDOW_2026;status=ok"' > file1.dat
+(printf '"' ; printf 'A%.0s' $(seq 1 16) ; echo 'e2'  | xxd -r -p) > file2.dat
+
+TESTNAME="${0##*/}"
+TESTNAME="${TESTNAME%.test}"
+( cat file1.dat ; cat file2.dat ) | run_output_test -o "${TESTNAME}" --exit 1 ../apps/json_parse -u -N -n -
+_err=$?
+
+exit $_err