From nobody Mon Feb 9 04:46:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1554219809916182.87141252653032; Tue, 2 Apr 2019 08:43:29 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5C71F88302; Tue, 2 Apr 2019 15:43:28 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3768D18E22; Tue, 2 Apr 2019 15:43:28 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DD92818033A2; Tue, 2 Apr 2019 15:43:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x32FgCrK011887 for ; Tue, 2 Apr 2019 11:42:12 -0400 Received: by smtp.corp.redhat.com (Postfix) id CEF185C21A; Tue, 2 Apr 2019 15:42:12 +0000 (UTC) Received: from icr.brq.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 56A345C6B3 for ; Tue, 2 Apr 2019 15:42:12 +0000 (UTC) From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Date: Tue, 2 Apr 2019 17:41:46 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 07/14] virjsontest: introduce DO_TEST_PARSE_FILE X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 02 Apr 2019 15:43:28 +0000 (UTC) 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=C3=A1n Tomko --- 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/par= se-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/virjsondat= a/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/par= se-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/p= arse-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 { }; =20 =20 +static int +testJSONFromFile(const void *data) +{ + const struct testInfo *info =3D data; + VIR_AUTOPTR(virJSONValue) injson =3D NULL; + VIR_AUTOFREE(char *) infile =3D NULL; + VIR_AUTOFREE(char *) indata =3D NULL; + VIR_AUTOFREE(char *) actual =3D 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 =3D 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 =3D 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) =20 - - 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,ser= ver\"," - "\"label\": \"charmonitor\"}, {\"filename\": \"pty:/dev/= pts/158\"," - "\"label\": \"charserial0\"}], \"id\": \"libvirt-3\"}", - "{\"return\":[{\"filename\":" - "\"unix:/home/berrange/.libvirt/qemu/lib/tck.monitor,ser= ver\"," - "\"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_pas= swd\"}," - "{\"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\"},{\"n= ame\":" - "\"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.monito= r,server\"," + "\"label\":\"charmonitor\"},{\"filename\":\"pty:/de= v/pts/158\"," + "\"label\":\"charserial0\"}],\"id\":\"libvirt-3\"}"= ); + + DO_TEST_PARSE_FILE("VeryHard", "{\"return\":[{\"name\":\"quit\"},{\"na= me\":" + "\"eject\"},{\"name\":\"change\"},{\"name\":\"scree= ndump\"}," + "{\"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\":\"bloc= k_passwd\"}," + "{\"name\":\"set_password\"},{\"name\":\"expire_pas= sword\"}," + "{\"name\":\"qmp_capabilities\"},{\"name\":" + "\"human-monitor-command\"},{\"name\":\"query-versi= on\"}," + "{\"name\":\"query-commands\"},{\"name\":\"query-ch= ardev\"}," + "{\"name\":\"query-block\"},{\"name\":\"query-block= stats\"}," + "{\"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\"},{\"na= me\":" + "\"query-balloon\"}],\"id\":\"libvirt-2\"}"); =20 DO_TEST_FULL("success", AddRemove, NULL, NULL, true); DO_TEST_FULL("failure", AddRemove, NULL, NULL, false); --=20 2.19.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list