From nobody Mon Feb 9 03:14:21 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.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1499165612620837.8535545665482; Tue, 4 Jul 2017 03:53:32 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BDE6865983; Tue, 4 Jul 2017 10:53:30 +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 6694A6FB6C; Tue, 4 Jul 2017 10:53:30 +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 A88611804753; Tue, 4 Jul 2017 10:53:29 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v64ArSWU013446 for ; Tue, 4 Jul 2017 06:53:28 -0400 Received: by smtp.corp.redhat.com (Postfix) id BD3026128A; Tue, 4 Jul 2017 10:53:28 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F5015D96A; Tue, 4 Jul 2017 10:53:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BDE6865983 Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=libvir-list-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com BDE6865983 From: Peter Krempa To: libvir-list@redhat.com Date: Tue, 4 Jul 2017 12:53:27 +0200 Message-Id: <23264bc62512b06aa2f4d4645103d1f0faa16b83.1499165422.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 4/5] tests: virjson: Test formatting along with parsing of JSON objects 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: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 04 Jul 2017 10:53:31 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Format the parsed string back and compare it to the original (or modified) string for back and forth comparison. --- tests/virjsontest.c | 58 ++++++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 44 insertions(+), 14 deletions(-) diff --git a/tests/virjsontest.c b/tests/virjsontest.c index 2a6b2f44e..451275a4c 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -21,6 +21,8 @@ testJSONFromString(const void *data) { const struct testInfo *info =3D data; virJSONValuePtr json; + const char *expectstr =3D info->expect ? info->expect : info->doc; + char *formatted =3D NULL; int ret =3D -1; json =3D virJSONValueFromString(info->doc); @@ -43,9 +45,20 @@ testJSONFromString(const void *data) VIR_TEST_DEBUG("Parsed %s\n", info->doc); + if (!(formatted =3D virJSONValueToString(json, false))) { + VIR_TEST_VERBOSE("Failed to format json data\n"); + goto cleanup; + } + + if (STRNEQ(expectstr, formatted)) { + virTestDifference(stderr, expectstr, formatted); + goto cleanup; + } + ret =3D 0; cleanup: + VIR_FREE(formatted); virJSONValueFree(json); return ret; } @@ -323,23 +336,39 @@ mymain(void) ret =3D -1; \ } while (0) -#define DO_TEST_PARSE(name, doc) \ - DO_TEST_FULL(name, FromString, doc, NULL, true) +/** + * DO_TEST_PARSE: + * @name: test name + * @doc: source JSON string + * @expect: expected output JSON formatted from parsed @doc + * + * Parses @doc and formats it back. If @expect is NULL the result has to be + * identical to @doc. + */ +#define DO_TEST_PARSE(name, doc, expect) \ + DO_TEST_FULL(name, FromString, doc, expect, true) #define DO_TEST_PARSE_FAIL(name, doc) \ DO_TEST_FULL(name, FromString, doc, NULL, false) - DO_TEST_PARSE("Simple", "{\"return\": {}, \"id\": \"libvirt-1\"}"); + 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\": = []}}"); - + "\"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\"}"); + "\"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= \"}," @@ -364,7 +393,7 @@ mymain(void) "\"query-mice\"},{\"name\":\"query-vnc\"},{\"name\":" "\"query-spice\"},{\"name\":\"query-name\"},{\"name\":" "\"query-uuid\"},{\"name\":\"query-migrate\"},{\"name\":" - "\"query-balloon\"}],\"id\":\"libvirt-2\"}"); + "\"query-balloon\"}],\"id\":\"libvirt-2\"}", NULL); DO_TEST_FULL("add and remove", AddRemove, "{\"name\": \"sample\", \"value\": true}", @@ -400,21 +429,22 @@ mymain(void) "\"query-balloon\"}], \"id\": \"libvirt-2\"}", NULL, true= ); - DO_TEST_PARSE("almost nothing", "[]"); + DO_TEST_PARSE("almost nothing", "[]", NULL); DO_TEST_PARSE_FAIL("nothing", ""); - DO_TEST_PARSE("number without garbage", "[ 234545 ]"); + DO_TEST_PARSE("number without garbage", "[ 234545 ]", "[234545]"); DO_TEST_PARSE_FAIL("number with garbage", "[ 2345b45 ]"); - DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]"); + DO_TEST_PARSE("float without garbage", "[ 0.0314159e+100 ]", "[0.03141= 59e+100]"); DO_TEST_PARSE_FAIL("float with garbage", "[ 0.0314159ee+100 ]"); - DO_TEST_PARSE("string", "[ \"The meaning of life\" ]"); + DO_TEST_PARSE("string", "[ \"The meaning of life\" ]", + "[\"The meaning of life\"]"); DO_TEST_PARSE_FAIL("unterminated string", "[ \"The meaning of lif ]"); - DO_TEST_PARSE("integer", "1"); - DO_TEST_PARSE("boolean", "true"); - DO_TEST_PARSE("null", "null"); + DO_TEST_PARSE("integer", "1", NULL); + DO_TEST_PARSE("boolean", "true", NULL); + DO_TEST_PARSE("null", "null", NULL); DO_TEST_PARSE_FAIL("incomplete keyword", "tr"); DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]"); DO_TEST_PARSE_FAIL("unknown keyword", "huh"); --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list