From nobody Wed Nov 5 02:51:21 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1500645718934739.9214588364448; Fri, 21 Jul 2017 07:01:58 -0700 (PDT) Received: from localhost ([::1]:43372 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dYYV7-0006ZG-7H for importer@patchew.org; Fri, 21 Jul 2017 10:01:53 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dYYN4-0007jp-E2 for qemu-devel@nongnu.org; Fri, 21 Jul 2017 09:53:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dYYN2-00046u-2H for qemu-devel@nongnu.org; Fri, 21 Jul 2017 09:53:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:33676) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dYYN1-00045p-Ok for qemu-devel@nongnu.org; Fri, 21 Jul 2017 09:53:31 -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 mx1.redhat.com (Postfix) with ESMTPS id A5E3D883B4 for ; Fri, 21 Jul 2017 13:53:30 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-60.ams2.redhat.com [10.36.116.60]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2C3F874440; Fri, 21 Jul 2017 13:53:28 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 8E63D113864C; Fri, 21 Jul 2017 15:53:26 +0200 (CEST) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com A5E3D883B4 Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=armbru@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com A5E3D883B4 From: Markus Armbruster To: qemu-devel@nongnu.org Date: Fri, 21 Jul 2017 15:53:21 +0200 Message-Id: <1500645206-13559-5-git-send-email-armbru@redhat.com> In-Reply-To: <1500645206-13559-1-git-send-email-armbru@redhat.com> References: <1500645206-13559-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 21 Jul 2017 13:53:30 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 4/9] tests: Clean up string interpolation into QMP input (simple cases) X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When you build QMP input manually like this cmd =3D g_strdup_printf("{ 'execute': 'migrate'," "'arguments': { 'uri': '%s' } }", uri); rsp =3D qmp(cmd); g_free(cmd); you're responsible for escaping the interpolated values for JSON. Not done here, and therefore works only for sufficiently nice @uri. For instance, if @uri contained a single "'", qobject_from_jsonv() would fail, qmp_fd_sendv() would misinterpret the failure as empty input and do nothing, and the test would hang waiting for a response that never comes. Leaving interpolation into JSON to qmp() is more robust: rsp =3D qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri= ); It's also more concise. Clean up the simple cases where we interpolate exactly a JSON value. Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral. Signed-off-by: Markus Armbruster --- tests/libqos/libqos.c | 16 +---- tests/libqos/pci-pc.c | 9 +-- tests/postcopy-test.c | 8 +-- tests/test-qga.c | 160 +++++++++++++++++++++-----------------------= ---- tests/vhost-user-test.c | 6 +- 5 files changed, 77 insertions(+), 122 deletions(-) diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c index 6226546..42c5315 100644 --- a/tests/libqos/libqos.c +++ b/tests/libqos/libqos.c @@ -86,20 +86,12 @@ void set_context(QOSState *s) =20 static QDict *qmp_execute(const char *command) { - char *fmt; - QDict *rsp; - - fmt =3D g_strdup_printf("{ 'execute': '%s' }", command); - rsp =3D qmp(fmt); - g_free(fmt); - - return rsp; + return qmp("{ 'execute': %s }", command); } =20 void migrate(QOSState *from, QOSState *to, const char *uri) { const char *st; - char *s; QDict *rsp, *sub; bool running; =20 @@ -114,11 +106,7 @@ void migrate(QOSState *from, QOSState *to, const char = *uri) QDECREF(rsp); =20 /* Issue the migrate command. */ - s =3D g_strdup_printf("{ 'execute': 'migrate'," - "'arguments': { 'uri': '%s' } }", - uri); - rsp =3D qmp(s); - g_free(s); + rsp =3D qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", ur= i); g_assert(qdict_haskey(rsp, "return")); QDECREF(rsp); =20 diff --git a/tests/libqos/pci-pc.c b/tests/libqos/pci-pc.c index ded1c54..d40aa9d 100644 --- a/tests/libqos/pci-pc.c +++ b/tests/libqos/pci-pc.c @@ -159,14 +159,9 @@ void qpci_free_pc(QPCIBus *bus) void qpci_unplug_acpi_device_test(const char *id, uint8_t slot) { QDict *response; - char *cmd; =20 - cmd =3D g_strdup_printf("{'execute': 'device_del'," - " 'arguments': {" - " 'id': '%s'" - "}}", id); - response =3D qmp(cmd); - g_free(cmd); + response =3D qmp("{'execute': 'device_del', 'arguments': {'id': %s}}", + id); g_assert(response); g_assert(!qdict_haskey(response, "error")); QDECREF(response); diff --git a/tests/postcopy-test.c b/tests/postcopy-test.c index 8142f2a..e2ead87 100644 --- a/tests/postcopy-test.c +++ b/tests/postcopy-test.c @@ -358,7 +358,7 @@ static void test_migrate(void) char *uri =3D g_strdup_printf("unix:%s/migsocket", tmpfs); QTestState *global =3D global_qtest, *from, *to; unsigned char dest_byte_a, dest_byte_b, dest_byte_c, dest_byte_d; - gchar *cmd, *cmd_src, *cmd_dst; + gchar *cmd_src, *cmd_dst; QDict *rsp; =20 char *bootpath =3D g_strdup_printf("%s/bootsect", tmpfs); @@ -445,11 +445,7 @@ static void test_migrate(void) /* Wait for the first serial output from the source */ wait_for_serial("src_serial"); =20 - cmd =3D g_strdup_printf("{ 'execute': 'migrate'," - "'arguments': { 'uri': '%s' } }", - uri); - rsp =3D qmp(cmd); - g_free(cmd); + rsp =3D qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", ur= i); g_assert(qdict_haskey(rsp, "return")); QDECREF(rsp); =20 diff --git a/tests/test-qga.c b/tests/test-qga.c index 06783e7..91a7b6e 100644 --- a/tests/test-qga.c +++ b/tests/test-qga.c @@ -144,12 +144,11 @@ static void test_qga_sync_delimited(gconstpointer fix) guint32 v, r =3D g_random_int(); unsigned char c; QDict *ret; - gchar *cmd; =20 - cmd =3D g_strdup_printf("\xff{'execute': 'guest-sync-delimited'," - " 'arguments': {'id': %u } }", r); - qmp_fd_send(fixture->fd, cmd); - g_free(cmd); + qmp_fd_send(fixture->fd, + "\xff{'execute': 'guest-sync-delimited'," + " 'arguments': {'id': %u } }", + r); =20 /* * Read and ignore garbage until resynchronized. @@ -186,7 +185,6 @@ static void test_qga_sync(gconstpointer fix) const TestFixture *fixture =3D fix; guint32 v, r =3D g_random_int(); QDict *ret; - gchar *cmd; =20 /* * TODO guest-sync is inherently limited: we cannot distinguish @@ -199,10 +197,9 @@ static void test_qga_sync(gconstpointer fix) * invalid JSON. Testing of '\xff' handling is done in * guest-sync-delimited instead. */ - cmd =3D g_strdup_printf("{'execute': 'guest-sync'," - " 'arguments': {'id': %u } }", r); - ret =3D qmp_fd(fixture->fd, cmd); - g_free(cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-sync', 'arguments': {'id': %u } }", + r); =20 g_assert_nonnull(ret); qmp_assert_no_error(ret); @@ -393,7 +390,7 @@ static void test_qga_file_ops(gconstpointer fix) const TestFixture *fixture =3D fix; const unsigned char helloworld[] =3D "Hello World!\n"; const char *b64; - gchar *cmd, *path, *enc; + gchar *path, *enc; unsigned char *dec; QDict *ret, *val; int64_t id, eof; @@ -411,10 +408,10 @@ static void test_qga_file_ops(gconstpointer fix) =20 enc =3D g_base64_encode(helloworld, sizeof(helloworld)); /* write */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-write'," - " 'arguments': { 'handle': %" PRId64 "," - " 'buf-b64': '%s' } }", id, enc); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-write'," + " 'arguments': { 'handle': %" PRId64 ", 'buf-b64': %s } }= ", + id, enc); g_assert_nonnull(ret); qmp_assert_no_error(ret); =20 @@ -424,23 +421,20 @@ static void test_qga_file_ops(gconstpointer fix) g_assert_cmpint(count, =3D=3D, sizeof(helloworld)); g_assert_cmpint(eof, =3D=3D, 0); QDECREF(ret); - g_free(cmd); =20 /* flush */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-flush'," - " 'arguments': {'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-flush'," + " 'arguments': {'handle': %" PRId64 "} }", + id); QDECREF(ret); - g_free(cmd); =20 /* close */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-close'," - " 'arguments': {'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-close'," + " 'arguments': {'handle': %" PRId64 "} }", + id); QDECREF(ret); - g_free(cmd); =20 /* check content */ path =3D g_build_filename(fixture->test_dir, "foo", NULL); @@ -462,10 +456,10 @@ static void test_qga_file_ops(gconstpointer fix) QDECREF(ret); =20 /* read */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-read'," - " 'arguments': { 'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-read'," + " 'arguments': { 'handle': %" PRId64 "} }", + id); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "count"); eof =3D qdict_get_bool(val, "eof"); @@ -475,14 +469,13 @@ static void test_qga_file_ops(gconstpointer fix) g_assert_cmpstr(b64, =3D=3D, enc); =20 QDECREF(ret); - g_free(cmd); g_free(enc); =20 /* read eof */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-read'," - " 'arguments': { 'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-read'," + " 'arguments': { 'handle': %" PRId64 "} }", + id); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "count"); eof =3D qdict_get_bool(val, "eof"); @@ -491,14 +484,13 @@ static void test_qga_file_ops(gconstpointer fix) g_assert(eof); g_assert_cmpstr(b64, =3D=3D, ""); QDECREF(ret); - g_free(cmd); =20 /* seek */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-seek'," - " 'arguments': { 'handle': %" PRId64 ", " - " 'offset': %d, 'whence': '%s' } }", - id, 6, "set"); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-seek'," + " 'arguments': { 'handle': %" PRId64 ", " + " 'offset': %d, 'whence': %s } }", + id, 6, "set"); qmp_assert_no_error(ret); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "position"); @@ -506,13 +498,12 @@ static void test_qga_file_ops(gconstpointer fix) g_assert_cmpint(count, =3D=3D, 6); g_assert(!eof); QDECREF(ret); - g_free(cmd); =20 /* partial read */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-read'," - " 'arguments': { 'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-read'," + " 'arguments': { 'handle': %" PRId64 "} }", + id); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "count"); eof =3D qdict_get_bool(val, "eof"); @@ -525,15 +516,13 @@ static void test_qga_file_ops(gconstpointer fix) g_free(dec); =20 QDECREF(ret); - g_free(cmd); =20 /* close */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-close'," - " 'arguments': {'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-close'," + " 'arguments': {'handle': %" PRId64 "} }", + id); QDECREF(ret); - g_free(cmd); } =20 static void test_qga_file_write_read(gconstpointer fix) @@ -541,7 +530,7 @@ static void test_qga_file_write_read(gconstpointer fix) const TestFixture *fixture =3D fix; const unsigned char helloworld[] =3D "Hello World!\n"; const char *b64; - gchar *cmd, *enc; + gchar *enc; QDict *ret, *val; int64_t id, eof; gsize count; @@ -556,10 +545,10 @@ static void test_qga_file_write_read(gconstpointer fi= x) =20 enc =3D g_base64_encode(helloworld, sizeof(helloworld)); /* write */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-write'," - " 'arguments': { 'handle': %" PRId64 "," - " 'buf-b64': '%s' } }", id, enc); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-write'," + " 'arguments': { 'handle': %" PRId64 "," + " 'buf-b64': %s } }", id, enc); g_assert_nonnull(ret); qmp_assert_no_error(ret); =20 @@ -569,13 +558,12 @@ static void test_qga_file_write_read(gconstpointer fi= x) g_assert_cmpint(count, =3D=3D, sizeof(helloworld)); g_assert_cmpint(eof, =3D=3D, 0); QDECREF(ret); - g_free(cmd); =20 /* read (check implicit flush) */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-read'," - " 'arguments': { 'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-read'," + " 'arguments': { 'handle': %" PRId64 "} }", + id); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "count"); eof =3D qdict_get_bool(val, "eof"); @@ -584,14 +572,13 @@ static void test_qga_file_write_read(gconstpointer fi= x) g_assert(eof); g_assert_cmpstr(b64, =3D=3D, ""); QDECREF(ret); - g_free(cmd); =20 /* seek to 0 */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-seek'," - " 'arguments': { 'handle': %" PRId64 ", " - " 'offset': %d, 'whence': '%s' } }", - id, 0, "set"); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-seek'," + " 'arguments': { 'handle': %" PRId64 ", " + " 'offset': %d, 'whence': %s } }", + id, 0, "set"); qmp_assert_no_error(ret); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "position"); @@ -599,13 +586,12 @@ static void test_qga_file_write_read(gconstpointer fi= x) g_assert_cmpint(count, =3D=3D, 0); g_assert(!eof); QDECREF(ret); - g_free(cmd); =20 /* read */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-read'," - " 'arguments': { 'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-read'," + " 'arguments': { 'handle': %" PRId64 "} }", + id); val =3D qdict_get_qdict(ret, "return"); count =3D qdict_get_int(val, "count"); eof =3D qdict_get_bool(val, "eof"); @@ -614,16 +600,14 @@ static void test_qga_file_write_read(gconstpointer fi= x) g_assert(eof); g_assert_cmpstr(b64, =3D=3D, enc); QDECREF(ret); - g_free(cmd); g_free(enc); =20 /* close */ - cmd =3D g_strdup_printf("{'execute': 'guest-file-close'," - " 'arguments': {'handle': %" PRId64 "} }", - id); - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-file-close'," + " 'arguments': {'handle': %" PRId64 "} }", + id); QDECREF(ret); - g_free(cmd); } =20 static void test_qga_get_time(gconstpointer fix) @@ -647,7 +631,6 @@ static void test_qga_set_time(gconstpointer fix) const TestFixture *fixture =3D fix; QDict *ret; int64_t current, time; - gchar *cmd; =20 /* get current time */ ret =3D qmp_fd(fixture->fd, "{'execute': 'guest-get-time'}"); @@ -673,11 +656,10 @@ static void test_qga_set_time(gconstpointer fix) QDECREF(ret); =20 /* set back current time */ - cmd =3D g_strdup_printf("{'execute': 'guest-set-time'," - " 'arguments': { 'time': %" PRId64 " } }", - current + time * 1000); - ret =3D qmp_fd(fixture->fd, cmd); - g_free(cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-set-time'," + " 'arguments': { 'time': %" PRId64 " } }", + current + time * 1000); g_assert_nonnull(ret); qmp_assert_no_error(ret); QDECREF(ret); @@ -864,7 +846,6 @@ static void test_qga_guest_exec(gconstpointer fix) int64_t pid, now, exitcode; gsize len; bool exited; - char *cmd; =20 /* exec 'echo foo bar' */ ret =3D qmp_fd(fixture->fd, "{'execute': 'guest-exec', 'arguments': {" @@ -879,10 +860,10 @@ static void test_qga_guest_exec(gconstpointer fix) =20 /* wait for completion */ now =3D g_get_monotonic_time(); - cmd =3D g_strdup_printf("{'execute': 'guest-exec-status'," - " 'arguments': { 'pid': %" PRId64 " } }", pid); do { - ret =3D qmp_fd(fixture->fd, cmd); + ret =3D qmp_fd(fixture->fd, + "{'execute': 'guest-exec-status'," + " 'arguments': { 'pid': %" PRId64 " } }", pid); g_assert_nonnull(ret); val =3D qdict_get_qdict(ret, "return"); exited =3D qdict_get_bool(val, "exited"); @@ -892,7 +873,6 @@ static void test_qga_guest_exec(gconstpointer fix) } while (!exited && g_get_monotonic_time() < now + 5 * G_TIME_SPAN_SECOND); g_assert(exited); - g_free(cmd); =20 /* check stdout */ exitcode =3D qdict_get_int(val, "exitcode"); diff --git a/tests/vhost-user-test.c b/tests/vhost-user-test.c index d4da09f..f2a2b6c 100644 --- a/tests/vhost-user-test.c +++ b/tests/vhost-user-test.c @@ -662,11 +662,7 @@ static void test_migrate(void) g_assert(qdict_haskey(rsp, "return")); QDECREF(rsp); =20 - cmd =3D g_strdup_printf("{ 'execute': 'migrate'," - "'arguments': { 'uri': '%s' } }", - uri); - rsp =3D qmp(cmd); - g_free(cmd); + rsp =3D qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", ur= i); g_assert(qdict_haskey(rsp, "return")); QDECREF(rsp); =20 --=20 2.7.5