From nobody Mon Dec 15 23:29:09 2025 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 1499165629637363.05018964933413; Tue, 4 Jul 2017 03:53:49 -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 119AE624D7; Tue, 4 Jul 2017 10:53:47 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DB5D16FB71; Tue, 4 Jul 2017 10:53:46 +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 9237A261; Tue, 4 Jul 2017 10:53:46 +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 v64ArV6k013462 for ; Tue, 4 Jul 2017 06:53:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 436F35D96A; Tue, 4 Jul 2017 10:53:31 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.201]) by smtp.corp.redhat.com (Postfix) with ESMTP id 15F596128A; Tue, 4 Jul 2017 10:53:28 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 119AE624D7 Authentication-Results: ext-mx10.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx10.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 119AE624D7 From: Peter Krempa To: libvir-list@redhat.com Date: Tue, 4 Jul 2017 12:53:28 +0200 Message-Id: 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 5/5] tests: virjson: Test string escaping 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.39]); Tue, 04 Jul 2017 10:53:47 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Make sure that JSON strings can contain characters which need to be escaped (double quotes, backslashes, tabs, etc.) and that JSON objects formatted into strings can be nested into strings. --- tests/virjsontest.c | 67 +++++++++++++++++++++++++++++++++++++++++++++++++= ++++ 1 file changed, 67 insertions(+) diff --git a/tests/virjsontest.c b/tests/virjsontest.c index 451275a4c..e4176af0e 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -325,6 +325,67 @@ testJSONCopy(const void *data) static int +testJSONEscapeObj(const void *data ATTRIBUTE_UNUSED) +{ + virJSONValuePtr json =3D NULL; + virJSONValuePtr nestjson =3D NULL; + virJSONValuePtr parsejson =3D NULL; + char *neststr =3D NULL; + char *result =3D NULL; + const char *parsednestedstr; + int ret =3D -1; + + if (virJSONValueObjectCreate(&nestjson, + "s:stringkey", "stringvalue", + "i:numberkey", 1234, + "b:booleankey", false, NULL) < 0) { + VIR_TEST_VERBOSE("failed to create nested json object"); + goto cleanup; + } + + if (!(neststr =3D virJSONValueToString(nestjson, false))) { + VIR_TEST_VERBOSE("failed to format nested json object"); + goto cleanup; + } + + if (virJSONValueObjectCreate(&json, "s:test", neststr, NULL) < 0) { + VIR_TEST_VERBOSE("Failed to create json object"); + goto cleanup; + } + + if (!(result =3D virJSONValueToString(json, false))) { + VIR_TEST_VERBOSE("Failed to format json object"); + goto cleanup; + } + + if (!(parsejson =3D virJSONValueFromString(result))) { + VIR_TEST_VERBOSE("Failed to parse JSON with nested JSON in string"= ); + goto cleanup; + } + + if (!(parsednestedstr =3D virJSONValueObjectGetString(parsejson, "test= "))) { + VIR_TEST_VERBOSE("Failed to retrieve string containing nested json= "); + goto cleanup; + } + + if (STRNEQ(parsednestedstr, neststr)) { + virTestDifference(stderr, neststr, parsednestedstr); + goto cleanup; + } + + ret =3D 0; + + cleanup: + VIR_FREE(neststr); + VIR_FREE(result); + virJSONValueFree(json); + virJSONValueFree(nestjson); + virJSONValueFree(parsejson); + return ret; +} + + +static int mymain(void) { int ret =3D 0; @@ -445,6 +506,10 @@ mymain(void) DO_TEST_PARSE("integer", "1", NULL); DO_TEST_PARSE("boolean", "true", NULL); DO_TEST_PARSE("null", "null", NULL); + + DO_TEST_PARSE("escaping symbols", "[\"\\\"\\t\\n\\\\\"]", NULL); + DO_TEST_PARSE("escaped strings", "[\"{\\\"blurb\\\":\\\"test\\\"}\"]",= NULL); + DO_TEST_PARSE_FAIL("incomplete keyword", "tr"); DO_TEST_PARSE_FAIL("overdone keyword", "[ truest ]"); DO_TEST_PARSE_FAIL("unknown keyword", "huh"); @@ -477,6 +542,8 @@ mymain(void) DO_TEST_FULL("lookup with correct type", Lookup, "{ \"a\": {}, \"b\": 1, \"c\": \"str\", \"d\": [] }", NULL, true); + DO_TEST_FULL("create object with nested json in attribute", EscapeObj, + NULL, NULL, true); return (ret =3D=3D 0) ? EXIT_SUCCESS : EXIT_FAILURE; } --=20 2.12.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list