Introduce a new macro DO_TEST_PARSE_FILE which takes the input JSON
from a file instead of a C string.
This lets us get rid of quote escaping and makes the JSON easier to
edit.
The output JSON is still taken from a string and will be moved
separately.
Signed-off-by: Ján Tomko <jtomko(a)redhat.com>
---
tests/virjsondata/parse-Harder-in.json | 4 +
tests/virjsondata/parse-NotSoSimple-in.json | 3 +
tests/virjsondata/parse-Simple-in.json | 1 +
tests/virjsondata/parse-VeryHard-in.json | 24 ++++
tests/virjsontest.c | 129 +++++++++++++-------
5 files changed, 118 insertions(+), 43 deletions(-)
create mode 100644 tests/virjsondata/parse-Harder-in.json
create mode 100644 tests/virjsondata/parse-NotSoSimple-in.json
create mode 100644 tests/virjsondata/parse-Simple-in.json
create mode 100644 tests/virjsondata/parse-VeryHard-in.json
diff --git a/tests/virjsondata/parse-Harder-in.json
b/tests/virjsondata/parse-Harder-in.json
new file mode 100644
index 0000000000..739d780fb9
--- /dev/null
+++ b/tests/virjsondata/parse-Harder-in.json
@@ -0,0 +1,4 @@
+{"return": [{"filename": \
+"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server",\
+"label": "charmonitor"}, {"filename":
"pty:/dev/pts/158",\
+"label": "charserial0"}], "id": "libvirt-3"}
diff --git a/tests/virjsondata/parse-NotSoSimple-in.json
b/tests/virjsondata/parse-NotSoSimple-in.json
new file mode 100644
index 0000000000..bda5fd1c3f
--- /dev/null
+++ b/tests/virjsondata/parse-NotSoSimple-in.json
@@ -0,0 +1,3 @@
+{"QMP": {"version": {"qemu":\
+{"micro": 91, "minor": 13, "major": 0},\
+"package": " (qemu-kvm-devel)"}, "capabilities": []}}
diff --git a/tests/virjsondata/parse-Simple-in.json
b/tests/virjsondata/parse-Simple-in.json
new file mode 100644
index 0000000000..a40724b322
--- /dev/null
+++ b/tests/virjsondata/parse-Simple-in.json
@@ -0,0 +1 @@
+{"return": {}, "id": "libvirt-1"}
diff --git a/tests/virjsondata/parse-VeryHard-in.json
b/tests/virjsondata/parse-VeryHard-in.json
new file mode 100644
index 0000000000..e10d605950
--- /dev/null
+++ b/tests/virjsondata/parse-VeryHard-in.json
@@ -0,0 +1,24 @@
+{"return":[{"name":"quit"},{"name":\
+"eject"},{"name":"change"},{"name":"screendump"},\
+{"name":"stop"},{"name":"cont"},{"name":\
+"system_reset"},{"name":"system_powerdown"},\
+{"name":"device_add"},{"name":"device_del"},\
+{"name":"cpu"},{"name":"memsave"},{"name":\
+"pmemsave"},{"name":"migrate"},{"name":\
+"migrate_cancel"},{"name":"migrate_set_speed"},\
+{"name":"client_migrate_info"},{"name":\
+"migrate_set_downtime"},{"name":"netdev_add"},\
+{"name":"netdev_del"},{"name":"block_resize"},\
+{"name":"balloon"},{"name":"set_link"},{"name":\
+"getfd"},{"name":"closefd"},{"name":"block_passwd"},\
+{"name":"set_password"},{"name":"expire_password"},\
+{"name":"qmp_capabilities"},{"name":\
+"human-monitor-command"},{"name":"query-version"},\
+{"name":"query-commands"},{"name":"query-chardev"},\
+{"name":"query-block"},{"name":"query-blockstats"},\
+{"name":"query-cpus"},{"name":"query-pci"},{"name":\
+"query-kvm"},{"name":"query-status"},{"name":\
+"query-mice"},{"name":"query-vnc"},{"name":\
+"query-spice"},{"name":"query-name"},{"name":\
+"query-uuid"},{"name":"query-migrate"},{"name":\
+"query-balloon"}],"id":"libvirt-2"}
diff --git a/tests/virjsontest.c b/tests/virjsontest.c
index 0825a87188..262fcebca6 100644
--- a/tests/virjsontest.c
+++ b/tests/virjsontest.c
@@ -16,6 +16,51 @@ struct testInfo {
};
+static int
+testJSONFromFile(const void *data)
+{
+ const struct testInfo *info = data;
+ VIR_AUTOPTR(virJSONValue) injson = NULL;
+ VIR_AUTOFREE(char *) infile = NULL;
+ VIR_AUTOFREE(char *) indata = NULL;
+ VIR_AUTOFREE(char *) actual = NULL;
+
+ if (virAsprintf(&infile, "%s/virjsondata/parse-%s-in.json",
+ abs_srcdir, info->name) < 0)
+ return -1;
+
+ if (virTestLoadFile(infile, &indata) < 0)
+ return -1;
+
+ injson = virJSONValueFromString(indata);
+
+ if (!injson) {
+ if (info->pass) {
+ VIR_TEST_VERBOSE("Failed to parse %s\n", info->doc);
+ return -1;
+ } else {
+ VIR_TEST_DEBUG("As expected, failed to parse %s\n", info->doc);
+ return 0;
+ }
+ } else {
+ if (!info->pass) {
+ VIR_TEST_VERBOSE("Unexpected success while parsing %s\n",
info->doc);
+ return -1;
+ }
+ }
+
+ if (!(actual = virJSONValueToString(injson, false)))
+ return -1;
+
+ if (STRNEQ(info->expect, actual)) {
+ virTestDifference(stderr, info->expect, actual);
+ return -1;
+ }
+
+ return 0;
+}
+
+
static int
testJSONFromString(const void *data)
{
@@ -454,49 +499,47 @@ mymain(void)
#define DO_TEST_PARSE_FAIL(name, doc) \
DO_TEST_FULL(name, FromString, doc, NULL, false)
-
- DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\":
\"libvirt-1\"}",
-
"{\"return\":{},\"id\":\"libvirt-1\"}");
- DO_TEST_PARSE("NotSoSimple", "{\"QMP\":
{\"version\": {\"qemu\":"
- "{\"micro\": 91, \"minor\": 13,
\"major\": 0},"
- "\"package\": \" (qemu-kvm-devel)\"},
\"capabilities\": []}}",
-
"{\"QMP\":{\"version\":{\"qemu\":"
-
"{\"micro\":91,\"minor\":13,\"major\":0},"
- "\"package\":\"
(qemu-kvm-devel)\"},\"capabilities\":[]}}");
-
- DO_TEST_PARSE("Harder", "{\"return\":
[{\"filename\": "
-
"\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
- "\"label\": \"charmonitor\"},
{\"filename\": \"pty:/dev/pts/158\","
- "\"label\": \"charserial0\"}],
\"id\": \"libvirt-3\"}",
- "{\"return\":[{\"filename\":"
-
"\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
-
"\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\","
-
"\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}");
-
- DO_TEST_PARSE("VeryHard",
"{\"return\":[{\"name\":\"quit\"},{\"name\":"
-
"\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"},"
-
"{\"name\":\"stop\"},{\"name\":\"cont\"},{\"name\":"
-
"\"system_reset\"},{\"name\":\"system_powerdown\"},"
-
"{\"name\":\"device_add\"},{\"name\":\"device_del\"},"
-
"{\"name\":\"cpu\"},{\"name\":\"memsave\"},{\"name\":"
-
"\"pmemsave\"},{\"name\":\"migrate\"},{\"name\":"
-
"\"migrate_cancel\"},{\"name\":\"migrate_set_speed\"},"
-
"{\"name\":\"client_migrate_info\"},{\"name\":"
-
"\"migrate_set_downtime\"},{\"name\":\"netdev_add\"},"
-
"{\"name\":\"netdev_del\"},{\"name\":\"block_resize\"},"
-
"{\"name\":\"balloon\"},{\"name\":\"set_link\"},{\"name\":"
-
"\"getfd\"},{\"name\":\"closefd\"},{\"name\":\"block_passwd\"},"
-
"{\"name\":\"set_password\"},{\"name\":\"expire_password\"},"
-
"{\"name\":\"qmp_capabilities\"},{\"name\":"
-
"\"human-monitor-command\"},{\"name\":\"query-version\"},"
-
"{\"name\":\"query-commands\"},{\"name\":\"query-chardev\"},"
-
"{\"name\":\"query-block\"},{\"name\":\"query-blockstats\"},"
-
"{\"name\":\"query-cpus\"},{\"name\":\"query-pci\"},{\"name\":"
-
"\"query-kvm\"},{\"name\":\"query-status\"},{\"name\":"
-
"\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":"
-
"\"query-spice\"},{\"name\":\"query-name\"},{\"name\":"
-
"\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":"
-
"\"query-balloon\"}],\"id\":\"libvirt-2\"}",
NULL);
+#define DO_TEST_PARSE_FILE(name, expect) \
+ DO_TEST_FULL(name, FromFile, NULL, expect, true)
+
+
+ DO_TEST_PARSE_FILE("Simple",
+
"{\"return\":{},\"id\":\"libvirt-1\"}");
+ DO_TEST_PARSE_FILE("NotSoSimple",
+
"{\"QMP\":{\"version\":{\"qemu\":"
+
"{\"micro\":91,\"minor\":13,\"major\":0},"
+ "\"package\":\"
(qemu-kvm-devel)\"},\"capabilities\":[]}}");
+
+ DO_TEST_PARSE_FILE("Harder",
+ "{\"return\":[{\"filename\":"
+
"\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,server\","
+
"\"label\":\"charmonitor\"},{\"filename\":\"pty:/dev/pts/158\","
+
"\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}");
+
+ DO_TEST_PARSE_FILE("VeryHard",
"{\"return\":[{\"name\":\"quit\"},{\"name\":"
+
"\"eject\"},{\"name\":\"change\"},{\"name\":\"screendump\"},"
+
"{\"name\":\"stop\"},{\"name\":\"cont\"},{\"name\":"
+
"\"system_reset\"},{\"name\":\"system_powerdown\"},"
+
"{\"name\":\"device_add\"},{\"name\":\"device_del\"},"
+
"{\"name\":\"cpu\"},{\"name\":\"memsave\"},{\"name\":"
+
"\"pmemsave\"},{\"name\":\"migrate\"},{\"name\":"
+
"\"migrate_cancel\"},{\"name\":\"migrate_set_speed\"},"
+
"{\"name\":\"client_migrate_info\"},{\"name\":"
+
"\"migrate_set_downtime\"},{\"name\":\"netdev_add\"},"
+
"{\"name\":\"netdev_del\"},{\"name\":\"block_resize\"},"
+
"{\"name\":\"balloon\"},{\"name\":\"set_link\"},{\"name\":"
+
"\"getfd\"},{\"name\":\"closefd\"},{\"name\":\"block_passwd\"},"
+
"{\"name\":\"set_password\"},{\"name\":\"expire_password\"},"
+
"{\"name\":\"qmp_capabilities\"},{\"name\":"
+
"\"human-monitor-command\"},{\"name\":\"query-version\"},"
+
"{\"name\":\"query-commands\"},{\"name\":\"query-chardev\"},"
+
"{\"name\":\"query-block\"},{\"name\":\"query-blockstats\"},"
+
"{\"name\":\"query-cpus\"},{\"name\":\"query-pci\"},{\"name\":"
+
"\"query-kvm\"},{\"name\":\"query-status\"},{\"name\":"
+
"\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":"
+
"\"query-spice\"},{\"name\":\"query-name\"},{\"name\":"
+
"\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":"
+
"\"query-balloon\"}],\"id\":\"libvirt-2\"}");
DO_TEST_FULL("success", AddRemove, NULL, NULL, true);
DO_TEST_FULL("failure", AddRemove, NULL, NULL, false);
--
2.19.2