aboutsummaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2022-04-21 02:37:15 +0000
committerDouglas Gilbert <dgilbert@interlog.com>2022-04-21 02:37:15 +0000
commit8cd2200ba1758cd127a1b95ab1808cde4f56713c (patch)
tree81f7e8e9c995548351865b77c13092ebf40d7174 /testing
parentf26bb872bf4f043a1b817c808a6947fcb03ecda4 (diff)
downloadsg3_utils-8cd2200ba1758cd127a1b95ab1808cde4f56713c.tar.gz
sg_opcodes: add --inhex=FN to process earlier -HHH, add experimental --json[=JO] option; sg_turs: change nanosleep() to Sleep() in MinGW
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@945 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'testing')
-rw-r--r--testing/Makefile6
-rw-r--r--testing/sg_json_builder_test.c73
2 files changed, 77 insertions, 2 deletions
diff --git a/testing/Makefile b/testing/Makefile
index 5d05ad5a..aec375f6 100644
--- a/testing/Makefile
+++ b/testing/Makefile
@@ -45,12 +45,14 @@ CFLAGS = -g -O2 -W -Wall
LDFLAGS =
-LIBFILESOLD = ../lib/sg_lib.o ../lib/sg_lib_data.o ../lib/sg_io_linux.o
+LIBFILESOLD = ../lib/sg_lib.o ../lib/sg_lib_data.o ../lib/sg_io_linux.o \
+ ../lib/sg_json_builder.o ../lib/sg_pr2serr.o
+
LIBFILESNEW = ../lib/sg_pt_linux_nvme.o ../lib/sg_lib.o ../lib/sg_lib_data.o \
../lib/sg_pt_linux.o ../lib/sg_io_linux.o \
../lib/sg_pt_common.o ../lib/sg_cmds_basic.o \
../lib/sg_cmds_basic2.o ../lib/sg_lib_names.o \
- ../lib/sg_json_builder.o
+ ../lib/sg_json_builder.o ../lib/sg_pr2serr.o
all: $(EXECS)
diff --git a/testing/sg_json_builder_test.c b/testing/sg_json_builder_test.c
new file mode 100644
index 00000000..a43b9f78
--- /dev/null
+++ b/testing/sg_json_builder_test.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
+/*
+ * Simple streaming JSON writer
+ *
+ * This takes care of the annoying bits of JSON syntax like the commas
+ * after elements
+ *
+ * Authors: Stephen Hemminger <stephen@networkplumber.org>
+ *
+ * Borrowed from Linux kernel [5.17.0]: tools/bpf/bpftool/json_writer.[hc]
+ */
+
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdarg.h>
+#include <assert.h>
+#include <malloc.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+#include "../lib/sg_json_builder.h"
+
+int main(int argc, char **argv)
+{
+ json_writer_t *wr = jsonw_new(stdout);
+
+ jsonw_start_object(wr);
+ jsonw_pretty(wr, true);
+ jsonw_name(wr, "Vyatta");
+ jsonw_start_object(wr);
+ jsonw_string_field(wr, "url", "http://vyatta.com");
+ jsonw_uint_field(wr, "downloads", 2000000ul);
+ jsonw_float_field(wr, "stock", 8.16);
+
+ jsonw_name(wr, "ARGV");
+ jsonw_start_array(wr);
+ while (--argc)
+ jsonw_string(wr, *++argv);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "empty");
+ jsonw_start_array(wr);
+ jsonw_end_array(wr);
+
+ jsonw_name(wr, "NIL");
+ jsonw_start_object(wr);
+ jsonw_end_object(wr);
+
+ jsonw_null_field(wr, "my_null");
+
+ jsonw_name(wr, "special chars");
+ jsonw_start_array(wr);
+ jsonw_string_field(wr, "slash", "/");
+ jsonw_string_field(wr, "newline", "\n");
+ jsonw_string_field(wr, "tab", "\t");
+ jsonw_string_field(wr, "ff", "\f");
+ jsonw_string_field(wr, "quote", "\"");
+ jsonw_string_field(wr, "tick", "\'");
+ jsonw_string_field(wr, "backslash", "\\");
+ jsonw_end_array(wr);
+
+jsonw_name(wr, "ARGV");
+jsonw_start_array(wr);
+jsonw_string(wr, "boo: appended or new entry?");
+jsonw_end_array(wr);
+
+ jsonw_end_object(wr);
+
+ jsonw_end_object(wr);
+ jsonw_destroy(&wr);
+ return 0;
+}
+