From nobody Mon Feb 9 03:48:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1633622737; cv=none; d=zohomail.com; s=zohoarc; b=ZsY87J9Qe09on34oCvqS8oCB1bKkkKoNzzNW+cMtTEMddo79LXjAKdsP6qbzXoWymBuxytESzE80NrFHQgNvaAgmOnxsDrnYF6+Kez1dmfXLehRpP/YZZBGi5AczqlUA+WnRBl3kS1DfjuVuXFs3scZcoQPoQiq9Py2gWb/wALE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1633622737; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Serzhg3Hs2Yjw67YbeIdsXnMLADJWUWPcb2S1eZSl2w=; b=LC8yUV2uPrFfndIsEu8g5yl+XZRpsJWkMgJpPChuCzLjK/G363T3RPNAIEdHs1mjD+iHaptkoHk6Bk6DOewH0Y8YNtWcIQr8KuyeIUtM7iHp1Q5xqjixqKDY2pHlUc0AJXHM5bZRjr1Bq+nwMhfT29smxgi96mq4mbJCd5cJJmc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1633622737510940.8403371787947; Thu, 7 Oct 2021 09:05:37 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-572-r4PSd1quMRay4So2dKLOQA-1; Thu, 07 Oct 2021 12:05:33 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 42A644D5D24; Thu, 7 Oct 2021 15:48:06 +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 1A76A4AC87D; Thu, 7 Oct 2021 15:48:06 +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 D90661806D03; Thu, 7 Oct 2021 15:48:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 197FInAI023635 for ; Thu, 7 Oct 2021 11:18:49 -0400 Received: by smtp.corp.redhat.com (Postfix) id 58ADA5F4EE; Thu, 7 Oct 2021 15:18:49 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.5]) by smtp.corp.redhat.com (Postfix) with ESMTP id BD3FD5F4FE for ; Thu, 7 Oct 2021 15:18:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633622735; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Serzhg3Hs2Yjw67YbeIdsXnMLADJWUWPcb2S1eZSl2w=; b=gWmM3zV6QZ3Ue37A+26uNU2lGA5PHzAlZplEkhzVT3lGX+BfzIvm+PWKAeHXOlj2cowUKn 8cDG48jKQfvrMI0ubUmXpkEeuzAkpQr+m/iBktWs2JbYYuN/PjuOpCtp8YUaZ0a6K38KW0 WKkBxpJQEkzi44aECSGg4BhCXAIkuOE= X-MC-Unique: r4PSd1quMRay4So2dKLOQA-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 015/103] qemuxml2argvtest: Refactor QAPI schema validation code Date: Thu, 7 Oct 2021 17:17:03 +0200 Message-Id: <967d1b5571fe30c7d2c319ec3cfb29d7f512c48d.1633619630.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1633622739247100003 Content-Type: text/plain; charset="utf-8" Prevent duplication of code when extending the validator for new commands. Add a struct describing a command to validate and make the validation loop a bit more robust to corner cases. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- tests/qemuxml2argvtest.c | 120 +++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 7c6e3d3dd7..b5fab1178c 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -487,6 +487,70 @@ testCompareXMLToArgvCreateArgs(virQEMUDriver *drv, } +struct testValidateSchemaCommandData { + const char *name; + const char *schema; +}; + + +static const struct testValidateSchemaCommandData commands[] =3D { + { "-blockdev", "blockdev-add" }, + { "-netdev", "netdev_add" }, + { "-object", "object-add" }, +}; + +static int +testCompareXMLToArgvValidateSchemaCommand(GStrv args, + GHashTable *schema) +{ + GStrv arg; + + for (arg =3D args; *arg; arg++) { + const char *curcommand =3D *arg; + const char *curargs =3D *(arg + 1); + size_t i; + + for (i =3D 0; i < G_N_ELEMENTS(commands); i++) { + const struct testValidateSchemaCommandData *command =3D comman= ds + i; + g_auto(virBuffer) debug =3D VIR_BUFFER_INITIALIZER; + g_autoptr(virJSONValue) jsonargs =3D NULL; + + if (STRNEQ(curcommand, command->name)) + continue; + + if (!curargs) { + VIR_TEST_VERBOSE("expected arguments for command '%s'", + command->name); + return -1; + } + + if (*curargs !=3D '{') { + /* skip the current validation */ + VIR_TEST_DEBUG("skipping validation of '%s': argument is n= ot JSON", + command->name); + arg++; + break; + } + + if (!(jsonargs =3D virJSONValueFromString(curargs))) + return -1; + + if (testQEMUSchemaValidateCommand(command->schema, jsonargs, + schema, false, false, &debug= ) < 0) { + VIR_TEST_VERBOSE("failed to validate '%s %s' against QAPI = schema: %s", + command->name, curargs, virBufferCurrentC= ontent(&debug)); + return -1; + } + + /* don't check the argument twice */ + arg++; + } + } + + return 0; +} + + static int testCompareXMLToArgvValidateSchema(virQEMUDriver *drv, const char *migrateURI, @@ -497,7 +561,6 @@ testCompareXMLToArgvValidateSchema(virQEMUDriver *drv, g_autoptr(virDomainObj) vm =3D NULL; qemuDomainObjPrivate *priv =3D NULL; size_t nargs =3D 0; - size_t i; GHashTable *schema =3D NULL; g_autoptr(virCommand) cmd =3D NULL; unsigned int parseFlags =3D info->parseFlags; @@ -542,59 +605,8 @@ testCompareXMLToArgvValidateSchema(virQEMUDriver *drv, if (virCommandGetArgList(cmd, &args, &nargs) < 0) return -1; - for (i =3D 0; i < nargs; i++) { - g_auto(virBuffer) debug =3D VIR_BUFFER_INITIALIZER; - g_autoptr(virJSONValue) jsonargs =3D NULL; - - if (STREQ(args[i], "-blockdev")) { - if (!(jsonargs =3D virJSONValueFromString(args[i + 1]))) - return -1; - - if (testQEMUSchemaValidateCommand("blockdev-add", jsonargs, - schema, false, false, &debug= ) < 0) { - VIR_TEST_VERBOSE("failed to validate -blockdev '%s' agains= t QAPI schema: %s", - args[i + 1], virBufferCurrentContent(&deb= ug)); - return -1; - } - - i++; - } else if (STREQ(args[i], "-netdev")) { - if (*args[i + 1] !=3D '{') { - i++; - continue; - } - - if (!(jsonargs =3D virJSONValueFromString(args[i + 1]))) - return -1; - - if (testQEMUSchemaValidateCommand("netdev_add", jsonargs, - schema, false, false, &debug= ) < 0) { - VIR_TEST_VERBOSE("failed to validate -netdev '%s' against = QAPI schema: %s", - args[i + 1], virBufferCurrentContent(&deb= ug)); - return -1; - } - - i++; - } else if (STREQ(args[i], "-object")) { - - if (*args[i + 1] !=3D '{') { - i++; - continue; - } - - if (!(jsonargs =3D virJSONValueFromString(args[i + 1]))) - return -1; - - if (testQEMUSchemaValidateCommand("object-add", jsonargs, - schema, false, false, &debug= ) < 0) { - VIR_TEST_VERBOSE("failed to validate -object '%s' against = QAPI schema: %s", - args[i + 1], virBufferCurrentContent(&deb= ug)); - return -1; - } - - i++; - } - } + if (testCompareXMLToArgvValidateSchemaCommand(args, schema) < 0) + return -1; return 0; } --=20 2.31.1