Skip to contentSkip to conversation

tests: Compile-time format string checking for libqtest.h

PATCH Superseded Markus Armbruster 3 participants 69 messages Jul 27, 2018 – Aug 2, 2018

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#40 lore raw

PATCH v2 17/23 migration-test: Clean up string interpolation into QMP, part 1

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migrate_recover() builds QMP input manually because wait_command() can't interpolate. Well, it can since the previous commit. Simplify accordingly.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
 tests/migration-test.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
tests/migration-test.c +6 -6
index 0c92f2b1cd..323bb60535 100644
@@ -156,6 +156,7 @@ static void stop_cb(void *opaque, const char *name, QDict *data)
 /*
  * Events can get in the way of responses we are actually waiting for.
  */
+GCC_FMT_ATTR(2, 3)
 static QDict *wait_command(QTestState *who, const char *command, ...)
 {
     va_list ap;
@@ -354,13 +355,12 @@ static void migrate_pause(QTestState *who)
 static void migrate_recover(QTestState *who, const char *uri)
 {
     QDict *rsp;
-    gchar *cmd = g_strdup_printf(
-        "{ 'execute': 'migrate-recover', "
-        "  'id': 'recover-cmd', "
-        "  'arguments': { 'uri': '%s' } }", uri);
 
-    rsp = wait_command(who, cmd);
-    g_free(cmd);
+    rsp = wait_command(who,
+                       "{ 'execute': 'migrate-recover', "
+                       "  'id': 'recover-cmd', "
+                       "  'arguments': { 'uri': %s } }",
+                       uri);
     qobject_unref(rsp);
 }
 
Signature · 1 line
2.17.1
#7 lore raw

PATCH v2 3/23 libqtest: Clean up how we read device_del messages

In reply to Markus Armbruster

qtest_qmp_device_del() still uses the qmp("") hack to receive a message, even though we have qmp_receive() since commit 66e0c7b187e. Put it to use.

Bonus: gets rid of empty format strings. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---
 tests/libqtest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tests/libqtest.c +1 -1
index 9cb4096639..071d7eb7b1 100644
@@ -1090,7 +1090,7 @@ void qtest_qmp_device_del(const char *id)
     g_assert(response1);
     g_assert(!qdict_haskey(response1, "error"));
 
-    response2 = qmp("");
+    response2 = qmp_receive();
     g_assert(response2);
     g_assert(!qdict_haskey(response2, "error"));
 
Signature · 1 line
2.17.1
#17 lore raw

PATCH v2 8/23 test-qobject-input-visitor: Avoid format string ambiguity

In reply to Markus Armbruster

When visitor_input_test_init_internal()'s argument @ap is null, then @json_string is interpreted literally, else it's gets %-escapes interpolated. This is awkward.

One caller always passes null @ap, and the others never do. Lift the building of the QObject into the callers, where it can be done without such ambiguity.

---
 tests/test-qobject-input-visitor.c | 19 ++++++++++---------
 1 file changed, 10 insertions(+), 9 deletions(-)
tests/test-qobject-input-visitor.c +10 -9
index 0f4d234c3f..caa90b3d7e 100644
@@ -47,15 +47,13 @@ static void visitor_input_teardown(TestInputVisitorData *data,
 /* The various test_init functions are provided instead of a test setup
    function so that the JSON string used by the tests are kept in the test
    functions (and not in main()). */
-static Visitor *visitor_input_test_init_internal(TestInputVisitorData *data,
-                                                 bool keyval,
-                                                 const char *json_string,
-                                                 va_list *ap)
+
+static Visitor *test_init_internal(TestInputVisitorData *data, bool keyval,
+                                   QObject *obj)
 {
     visitor_input_teardown(data, NULL);
 
-    data->obj = qobject_from_jsonv(json_string, ap, &error_abort);
-    g_assert(data->obj);
+    data->obj = obj;
 
     if (keyval) {
         data->qiv = qobject_input_visitor_new_keyval(data->obj);
@@ -75,7 +73,8 @@ Visitor *visitor_input_test_init_full(TestInputVisitorData *data,
     va_list ap;
 
     va_start(ap, json_string);
-    v = visitor_input_test_init_internal(data, keyval, json_string, &ap);
+    v = test_init_internal(data, keyval,
+                           qobject_from_vjsonf_nofail(json_string, ap));
     va_end(ap);
     return v;
 }
@@ -88,7 +87,8 @@ Visitor *visitor_input_test_init(TestInputVisitorData *data,
     va_list ap;
 
     va_start(ap, json_string);
-    v = visitor_input_test_init_internal(data, false, json_string, &ap);
+    v = test_init_internal(data, false,
+                           qobject_from_vjsonf_nofail(json_string, ap));
     va_end(ap);
     return v;
 }
@@ -103,7 +103,8 @@ Visitor *visitor_input_test_init(TestInputVisitorData *data,
 static Visitor *visitor_input_test_init_raw(TestInputVisitorData *data,
                                             const char *json_string)
 {
-    return visitor_input_test_init_internal(data, false, json_string, NULL);
+    return test_init_internal(data, false,
+                              qobject_from_json(json_string, &error_abort));
 }
 
 static void test_visitor_in_int(TestInputVisitorData *data,
Signature · 1 line
2.17.1
#25 lore raw

PATCH v2 12/23 cpu-plug-test: Don't pass integers as strings to device_add

In reply to Markus Armbruster

test_plug_with_device_add_x86() plugs Haswell-i386-cpu and Haswell-x86_64-cpu with device_add. It passes socket-id, core-id, thread-id as JSON strings. The properties are actually integers.

test_plug_with_device_add_coreid() plugs power8_v2.0-spapr-cpu-core and qemu-s390x-cpu with device_add. It passes core-id as JSON string. The properties are actually integers.

Passing JSON string values to integer properties works only due to device_add implementation accidents. Fix the test to pass JSON numbers. While there, use %u rather than %i with unsigned int.

---
 tests/cpu-plug-test.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tests/cpu-plug-test.c +3 -3
index 5f39ba0df3..ab3bf6df90 100644
@@ -88,8 +88,8 @@ static void test_plug_with_device_add_x86(gconstpointer data)
         for (c = 0; c < td->cores; c++) {
             for (t = 0; t < td->threads; t++) {
                 char *id = g_strdup_printf("id-%i-%i-%i", s, c, t);
-                qtest_qmp_device_add(td->device_model, id, "'socket-id':'%i', "
-                                     "'core-id':'%i', 'thread-id':'%i'",
+                qtest_qmp_device_add(td->device_model, id, "'socket-id':%u, "
+                                     "'core-id':%u, 'thread-id':%u",
                                      s, c, t);
                 g_free(id);
             }
@@ -114,7 +114,7 @@ static void test_plug_with_device_add_coreid(gconstpointer data)
 
     for (c = td->cores; c < td->maxcpus / td->sockets / td->threads; c++) {
         char *id = g_strdup_printf("id-%i", c);
-        qtest_qmp_device_add(td->device_model, id, "'core-id':'%i'", c);
+        qtest_qmp_device_add(td->device_model, id, "'core-id':%u", c);
         g_free(id);
     }
 
Signature · 1 line
2.17.1
#13 lore raw

PATCH v2 6/23 qobject: New qobject_from_vjsonf_nofail(), qdict_from_vjsonf_nofail()

In reply to Markus Armbruster

Every printf()-like function sooner or later needs its vprintf()-like buddy. The next commit will need qobject_from_jsonf_nofail()'s buddy, and qdict_from_jsonf_nofail()'s buddy will be used later in this series. Add both.

---
 include/qapi/qmp/qjson.h |  4 ++++
 qobject/qjson.c          | 44 +++++++++++++++++++++++++++++++++-------
 2 files changed, 41 insertions(+), 7 deletions(-)
include/qapi/qmp/qjson.h +4 -0
index dc509d51ae..dce78583dc 100644
@@ -18,8 +18,12 @@ QObject *qobject_from_json(const char *string, Error **errp);
 QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
     GCC_FMT_ATTR(1, 0);
 
+QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
+    GCC_FMT_ATTR(1, 0);
 QObject *qobject_from_jsonf_nofail(const char *string, ...)
     GCC_FMT_ATTR(1, 2);
+QDict *qdict_from_vjsonf_nofail(const char *string, va_list ap)
+    GCC_FMT_ATTR(1, 0);
 QDict *qdict_from_jsonf_nofail(const char *string, ...)
     GCC_FMT_ATTR(1, 2);
 
qobject/qjson.c +37 -7

71 lines not on this page · raw message

Signature · 1 line
2.17.1
#48 lore raw

PATCH v2 20/23 libqtest: Enable compile-time format string checking

In reply to Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_vjsonf_nofail(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
 tests/libqtest.h | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
tests/libqtest.h +19 -13
index ee58fcdf6d..327e402da5 100644
@@ -82,7 +82,8 @@ void qtest_quit(QTestState *s);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
+void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_qmp:
@@ -93,7 +94,8 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
+QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_qmp_send:
@@ -104,7 +106,8 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_qmp_send(QTestState *s, const char *fmt, ...);
+void qtest_qmp_send(QTestState *s, const char *fmt, ...)
+    GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_qmpv_discard_response:
@@ -116,7 +119,8 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
+void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_qmpv:
@@ -128,7 +132,8 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
+QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_qmp_vsend:
@@ -140,7 +145,8 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap);
+void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_receive:
@@ -597,7 +603,7 @@ static inline void qtest_end(void)
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qmp(const char *fmt, ...);
+QDict *qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_send:
@@ -607,7 +613,7 @@ QDict *qmp(const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qmp_send(const char *fmt, ...);
+void qmp_send(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_discard_response:
@@ -617,7 +623,7 @@ void qmp_send(const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
-void qmp_discard_response(const char *fmt, ...);
+void qmp_discard_response(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
  * qmp_receive:
@@ -976,10 +982,10 @@ static inline int64_t clock_set(int64_t val)
 }
 
 QDict *qmp_fd_receive(int fd);
-void qmp_fd_vsend(int fd, const char *fmt, va_list ap);
-void qmp_fd_send(int fd, const char *fmt, ...);
-QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
-QDict *qmp_fd(int fd, const char *fmt, ...);
+void qmp_fd_vsend(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
+void qmp_fd_send(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
+QDict *qmp_fdv(int fd, const char *fmt, va_list ap) GCC_FMT_ATTR(2, 0);
+QDict *qmp_fd(int fd, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 
 /**
  * qtest_cb_for_every_machine:
Signature · 1 line
2.17.1
#19 lore raw

PATCH v2 9/23 qobject: qobject_from_jsonv() is dangerous, hide it away

In reply to Markus Armbruster

qobject_from_jsonv() takes ownership of %p arguments. On failure, we can't generally know whether we failed before or after %p, so ownership becomes indeterminate. To avoid leaks, callers passing %p must terminate on error, e.g. by passing &error_abort. Trap for the unwary; document and give the function internal linkage.

---
 include/qapi/qmp/qjson.h |  2 --
 qobject/qjson.c          | 13 ++++++++++++-
 2 files changed, 12 insertions(+), 3 deletions(-)
include/qapi/qmp/qjson.h +0 -2
index dce78583dc..5ebbe5a118 100644
@@ -15,8 +15,6 @@
 #define QJSON_H
 
 QObject *qobject_from_json(const char *string, Error **errp);
-QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
-    GCC_FMT_ATTR(1, 0);
 
 QObject *qobject_from_vjsonf_nofail(const char *string, va_list ap)
     GCC_FMT_ATTR(1, 0);
qobject/qjson.c +12 -1
index 2e450231ff..ab4040f235 100644
@@ -39,7 +39,18 @@ static void parse_json(JSONMessageParser *parser, GQueue *tokens)
     s->result = json_parser_parse_err(tokens, s->ap, &s->err);
 }
 
-QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
+/*
+ * Parse @string as JSON value.
+ * If @ap is non-null, interpolate %-escapes.
+ * Takes ownership of %p arguments.
+ * On success, return the JSON value.
+ * On failure, store an error through @errp and return NULL.
+ * Ownership of %p arguments becomes indeterminate then.  To avoid
+ * leaks, callers passing %p must terminate on error, e.g. by passing
+ * &error_abort.
+ */
+static QObject *qobject_from_jsonv(const char *string, va_list *ap,
+                                   Error **errp)
 {
     JSONParsingState state = {};
 
Signature · 1 line
2.17.1
#35 lore raw

PATCH v2 15/23 tests: New helper qtest_qmp_receive_success()

In reply to Markus Armbruster

Commit b21373d0713 copied wait_command() from tests/migration-test.c to tests/tpm-util.c. Replace both copies by new libqtest helper qtest_qmp_receive_success(). Also use it to simplify qtest_qmp_device_del().

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Thomas Huth <[email protected]>
Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
Cc Stefan Berger <[email protected]>
---
 tests/libqtest.c       | 69 ++++++++++++++++++++++++++++++------------
 tests/libqtest.h       | 17 +++++++++++
 tests/migration-test.c | 29 ++++++------------
 tests/tpm-util.c       | 32 +++-----------------
 4 files changed, 80 insertions(+), 67 deletions(-)
tests/libqtest.c +50 -19
index dfca3af89d..ed832cd8e6 100644
@@ -1029,6 +1029,35 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
     qobject_unref(response);
 }
 
+QDict *qtest_qmp_receive_success(QTestState *s,
+                                 void (*event_cb)(void *opaque,
+                                                  const char *event,
+                                                  QDict *data),
+                                 void *opaque)
+{
+    QDict *response, *ret, *data;
+    const char *event;
+
+    for (;;) {
+        response = qtest_qmp_receive(s);
+        g_assert(!qdict_haskey(response, "error"));
+        ret = qdict_get_qdict(response, "return");
+        if (ret) {
+            break;
+        }
+        event = qdict_get_str(response, "event");
+        data = qdict_get_qdict(response, "data");
+        if (event_cb) {
+            event_cb(opaque, event, data);
+        }
+        qobject_unref(response);
+    }
+
+    qobject_ref(ret);
+    qobject_unref(response);
+    return ret;
+}
+
 /*
  * Generic hot-plugging test via the device_add QMP command.
  */
@@ -1053,6 +1082,14 @@ void qtest_qmp_device_add(const char *driver, const char *id,
     qobject_unref(response);
 }
 
+static void device_deleted_cb(void *opaque, const char *name, QDict *data)
+{
+    bool *got_event = opaque;
+
+    g_assert_cmpstr(name, ==, "DEVICE_DELETED");
+    *got_event = true;
+}
+
 /*
  * Generic hot-unplugging test via the device_del QMP command.
  * Device deletion will get one response and one event. For example:
@@ -1073,27 +1110,21 @@ void qtest_qmp_device_add(const char *driver, const char *id,
  */
 void qtest_qmp_device_del(const char *id)
 {
-    QDict *response1, *response2, *event = NULL;
+    bool got_event = false;
+    QDict *rsp;
 
-    response1 = qmp("{'execute': 'device_del', 'arguments': {'id': %s}}",
-                    id);
-    g_assert(response1);
-    g_assert(!qdict_haskey(response1, "error"));
-
-    response2 = qmp_receive();
-    g_assert(response2);
-    g_assert(!qdict_haskey(response2, "error"));
-
-    if (qdict_haskey(response1, "event")) {
-        event = response1;
-    } else if (qdict_haskey(response2, "event")) {
-        event = response2;
+    qtest_qmp_send(global_qtest,
+                   "{'execute': 'device_del', 'arguments': {'id': %s}}",
+                   id);
+    rsp = qtest_qmp_receive_success(global_qtest, device_deleted_cb,
+                                    &got_event);
+    qobject_unref(rsp);
+    if (!got_event) {
+        rsp = qmp_receive();
+        g_assert_cmpstr(qdict_get_try_str(rsp, "event"),
+                        ==, "DEVICE_DELETED");
+        qobject_unref(rsp);
     }
-    g_assert(event);
-    g_assert_cmpstr(qdict_get_str(event, "event"), ==, "DEVICE_DELETED");
-
-    qobject_unref(response1);
-    qobject_unref(response2);
 }
 
 bool qmp_rsp_is_err(QDict *rsp)
tests/libqtest.h +17 -0
index ce6c092fc9..ee58fcdf6d 100644
@@ -169,6 +169,23 @@ void qtest_qmp_eventwait(QTestState *s, const char *event);
  */
 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event);
 
+/**
+ * qtest_qmp_receive_success:
+ * @s: #QTestState instance to operate on
+ * @event_cb: Event callback
+ * @opaque: Argument for @event_cb
+ *
+ * Poll QMP messages until a command success response is received.
+ * If @event_cb, call it for each event received, passing @opaque,
+ * the event's name and data.
+ * Return the success response's "return" member.
+ */
+QDict *qtest_qmp_receive_success(QTestState *s,
+                                 void (*event_cb)(void *opaque,
+                                                  const char *name,
+                                                  QDict *data),
+                                 void *opaque);
+
 /**
  * qtest_hmp:
  * @s: #QTestState instance to operate on.
tests/migration-test.c +9 -20
index e336399a71..860b8aa0b9 100644
@@ -146,31 +146,20 @@ static void wait_for_serial(const char *side)
     } while (true);
 }
 
+static void stop_cb(void *opaque, const char *name, QDict *data)
+{
+    if (!strcmp(name, "STOP")) {
+        got_stop = true;
+    }
+}
+
 /*
  * Events can get in the way of responses we are actually waiting for.
  */
 static QDict *wait_command(QTestState *who, const char *command)
 {
-    const char *event_string;
-    QDict *response, *ret;
-
-    response = qtest_qmp(who, command);
-
-    while (qdict_haskey(response, "event")) {
-        /* OK, it was an event */
-        event_string = qdict_get_str(response, "event");
-        if (!strcmp(event_string, "STOP")) {
-            got_stop = true;
-        }
-        qobject_unref(response);
-        response = qtest_qmp_receive(who);
-    }
-
-    ret = qdict_get_qdict(response, "return");
-    g_assert(ret);
-    qobject_ref(ret);
-    qobject_unref(response);
-    return ret;
+    qtest_qmp_send(who, command);
+    return qtest_qmp_receive_success(who, stop_cb, NULL);
 }
 
 /*
tests/tpm-util.c +4 -28
index 3bd2887f1e..9f3f156e42 100644
@@ -22,8 +22,6 @@
 #define TIS_REG(LOCTY, REG) \
     (TPM_TIS_ADDR_BASE + ((LOCTY) << 12) + REG)
 
-static bool got_stop;
-
 void tpm_util_crb_transfer(QTestState *s,
                            const unsigned char *req, size_t req_size,
                            unsigned char *rsp, size_t rsp_size)
@@ -247,41 +245,19 @@ void tpm_util_migrate(QTestState *who, const char *uri)
     qobject_unref(rsp);
 }
 
-/*
- * Events can get in the way of responses we are actually waiting for.
- */
-static QDict *tpm_util_wait_command(QTestState *who, const char *command)
-{
-    const char *event_string;
-    QDict *response;
-
-    response = qtest_qmp(who, command);
-
-    while (qdict_haskey(response, "event")) {
-        /* OK, it was an event */
-        event_string = qdict_get_str(response, "event");
-        if (!strcmp(event_string, "STOP")) {
-            got_stop = true;
-        }
-        qobject_unref(response);
-        response = qtest_qmp_receive(who);
-    }
-    return response;
-}
-
 void tpm_util_wait_for_migration_complete(QTestState *who)
 {
     while (true) {
-        QDict *rsp, *rsp_return;
+        QDict *rsp_return;
         bool completed;
         const char *status;
 
-        rsp = tpm_util_wait_command(who, "{ 'execute': 'query-migrate' }");
-        rsp_return = qdict_get_qdict(rsp, "return");
+        qtest_qmp_send(who, "{ 'execute': 'query-migrate' }");
+        rsp_return = qtest_qmp_receive_success(who, NULL, NULL);
         status = qdict_get_str(rsp_return, "status");
         completed = strcmp(status, "completed") == 0;
         g_assert_cmpstr(status, !=,  "failed");
-        qobject_unref(rsp);
+        qobject_unref(rsp_return);
         if (completed) {
             return;
         }
Signature · 1 line
2.17.1
#38 lore raw

PATCH v2 16/23 migration-test: Make wait_command() cope with '%'

In reply to Markus Armbruster

wait_command() passes its argument @command to qtest_qmp_send(). Falls apart if @command contain '%'. Two ways to disarm this trap:
suppress interpretation of '%' by passing @command as argument to format string "%s", or fix it by having wait_command() take the variable arguments to go with @command. Do the latter.

This is another step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
 tests/migration-test.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
tests/migration-test.c +7 -2
index 860b8aa0b9..0c92f2b1cd 100644
@@ -156,9 +156,14 @@ static void stop_cb(void *opaque, const char *name, QDict *data)
 /*
  * Events can get in the way of responses we are actually waiting for.
  */
-static QDict *wait_command(QTestState *who, const char *command)
+static QDict *wait_command(QTestState *who, const char *command, ...)
 {
-    qtest_qmp_send(who, command);
+    va_list ap;
+
+    va_start(ap, command);
+    qtest_qmp_vsend(who, command, ap);
+    va_end(ap);
+
     return qtest_qmp_receive_success(who, stop_cb, NULL);
 }
 
Signature · 1 line
2.17.1
#65 lore raw

PATCH v2 23/23 libqtest: Rename qtest_FOOv() to qtest_vFOO() for consistency

In reply to Markus Armbruster
---
 tests/libqtest.c | 12 ++++++------
 tests/libqtest.h |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)
tests/libqtest.c +6 -6
index a0d44793fa..3706f30aa2 100644
@@ -538,7 +538,7 @@ QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
     return qmp_fd_receive(fd);
 }
 
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
 {
     qtest_qmp_vsend(s, fmt, ap);
 
@@ -572,7 +572,7 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
     QDict *response;
 
     va_start(ap, fmt);
-    response = qtest_qmpv(s, fmt, ap);
+    response = qtest_vqmp(s, fmt, ap);
     va_end(ap);
     return response;
 }
@@ -608,7 +608,7 @@ void qtest_qmp_eventwait(QTestState *s, const char *event)
     qobject_unref(response);
 }
 
-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
+char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
 {
     char *cmd;
     QDict *resp;
@@ -637,7 +637,7 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...)
     char *ret;
 
     va_start(ap, fmt);
-    ret = qtest_hmpv(s, fmt, ap);
+    ret = qtest_vhmp(s, fmt, ap);
     va_end(ap);
     return ret;
 }
@@ -943,7 +943,7 @@ QDict *qmp(const char *fmt, ...)
     QDict *response;
 
     va_start(ap, fmt);
-    response = qtest_qmpv(global_qtest, fmt, ap);
+    response = qtest_vqmp(global_qtest, fmt, ap);
     va_end(ap);
     return response;
 }
@@ -963,7 +963,7 @@ char *hmp(const char *fmt, ...)
     char *ret;
 
     va_start(ap, fmt);
-    ret = qtest_hmpv(global_qtest, fmt, ap);
+    ret = qtest_vhmp(global_qtest, fmt, ap);
     va_end(ap);
     return ret;
 }
tests/libqtest.h +2 -2
index 3848086928..7e60abc9fd 100644
@@ -106,7 +106,7 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...)
  *
  * Sends a QMP message to QEMU and returns the response.
  */
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
 
 /**
@@ -189,7 +189,7 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
+char *qtest_vhmp(QTestState *s, const char *fmt, va_list ap)
     GCC_FMT_ATTR(2, 0);
 
 /**
Signature · 1 line
2.17.1
#1 lore raw

PATCH v2 1/23 libqtest: Document calling conventions

In reply to Markus Armbruster

From: Eric Blake <[email protected]>

We have two flavors of vararg usage in qtest: qtest_hmp() etc. work like sprintf(), and qtest_qmp() etc. work like qobject_from_jsonf(). Spell that out in the comments.

Also add GCC_FMT_ATTR() to qtest_hmp() etc. so that the compiler can flag incorrect use.

We have some cleanup work to do before we can do the same for qtest_qmp() etc. This would get us the same better-than-nothing checking we already have for qobject_from_jsonf(): common incorrect uses of supported conversion specifications will be flagged (e.g. passing a double for %d), but use of unsupported ones won't.

[Rebased, comment wording tweaked, commit message rewritten]
---
 tests/libqtest.h | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)
tests/libqtest.h +30 -11
index ac52872cbe..aa2b2cd7c1 100644
@@ -76,7 +76,9 @@ void qtest_quit(QTestState *s);
 /**
  * qtest_qmp_discard_response:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
@@ -85,7 +87,9 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
 /**
  * qtest_qmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and returns the response.
  */
@@ -94,7 +98,9 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
 /**
  * qtest_async_qmp:
  * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
@@ -103,7 +109,9 @@ void qtest_async_qmp(QTestState *s, const char *fmt, ...);
 /**
  * qtest_qmpv_discard_response:
  * @s: #QTestState instance to operate on.
- * @fmt: QMP message to send to QEMU
+ * @fmt: QMP message to send to QEMU, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
  * Sends a QMP message to QEMU and consumes the response.
@@ -113,7 +121,9 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
 /**
  * qtest_qmpv:
  * @s: #QTestState instance to operate on.
- * @fmt: QMP message to send to QEMU
+ * @fmt: QMP message to send to QEMU, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
  * Sends a QMP message to QEMU and returns the response.
@@ -123,7 +133,9 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
 /**
  * qtest_async_qmpv:
  * @s: #QTestState instance to operate on.
- * @fmt: QMP message to send to QEMU
+ * @fmt: QMP message to send to QEMU, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
@@ -172,7 +184,7 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
 /**
  * qtest_hmpv:
  * @s: #QTestState instance to operate on.
- * @fmt: HMP command to send to QEMU
+ * @fmt: HMP command to send to QEMU, formats arguments like vsprintf().
  * @ap: HMP command arguments
  *
  * Send HMP command to QEMU via QMP's human-monitor-command.
@@ -180,7 +192,8 @@ char *qtest_hmp(QTestState *s, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
  *
  * Returns: the command's output.  The caller should g_free() it.
  */
-char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap);
+char *qtest_hmpv(QTestState *s, const char *fmt, va_list ap)
+    GCC_FMT_ATTR(2, 0);
 
 /**
  * qtest_get_irq:
@@ -561,7 +574,9 @@ static inline void qtest_end(void)
 
 /**
  * qmp:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and returns the response.
  */
@@ -569,7 +584,9 @@ QDict *qmp(const char *fmt, ...);
 
 /**
  * qmp_async:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
@@ -577,7 +594,9 @@ void qmp_async(const char *fmt, ...);
 
 /**
  * qmp_discard_response:
- * @fmt...: QMP message to send to qemu
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and consumes the response.
  */
Signature · 1 line
2.17.1
#11 lore raw

PATCH v2 5/23 qobject: Replace qobject_from_jsonf() by qobject_from_jsonf_nofail()

In reply to Markus Armbruster

Commit ab45015a968 "qobject: Let qobject_from_jsonf() fail instead of abort" fails to accomplish its stated aim: the function can still abort due to its use of &error_abort.

Its rationale for letting it fail is that all remaining users cope fine with failure. Well, they're just fine with aborting, too; it's what they do on failure.

Simply reverting the broken commit would bring back the unfortunate asymmetry between qobject_from_jsonf() and qobject_from_jsonv(): one aborts, the other returns null. So also rename it to qobject_from_jsonf_nofail().

---
 include/qapi/qmp/qjson.h |  6 ++++--
 qobject/qjson.c          |  8 +++++++-
 tests/check-qjson.c      | 15 ++++++++-------
 tests/libqtest.h         | 18 +++++++++---------
 4 files changed, 28 insertions(+), 19 deletions(-)
include/qapi/qmp/qjson.h +4 -2
index 43b2ce2f33..dc509d51ae 100644
@@ -15,11 +15,13 @@
 #define QJSON_H
 
 QObject *qobject_from_json(const char *string, Error **errp);
-QObject *qobject_from_jsonf(const char *string, ...) GCC_FMT_ATTR(1, 2);
 QObject *qobject_from_jsonv(const char *string, va_list *ap, Error **errp)
     GCC_FMT_ATTR(1, 0);
 
-QDict *qdict_from_jsonf_nofail(const char *string, ...) GCC_FMT_ATTR(1, 2);
+QObject *qobject_from_jsonf_nofail(const char *string, ...)
+    GCC_FMT_ATTR(1, 2);
+QDict *qdict_from_jsonf_nofail(const char *string, ...)
+    GCC_FMT_ATTR(1, 2);
 
 QString *qobject_to_json(const QObject *obj);
 QString *qobject_to_json_pretty(const QObject *obj);
qobject/qjson.c +7 -1
index 2f6a590e44..4a9dcff343 100644
@@ -59,7 +59,12 @@ QObject *qobject_from_json(const char *string, Error **errp)
     return qobject_from_jsonv(string, NULL, errp);
 }
 
-QObject *qobject_from_jsonf(const char *string, ...)
+/*
+ * Parse @string as JSON value with %-escapes interpolated.
+ * Abort on error.  Do not use with untrusted @string.
+ * Return the resulting QObject.  It is never null.
+ */
+QObject *qobject_from_jsonf_nofail(const char *string, ...)
 {
     QObject *obj;
     va_list ap;
@@ -68,6 +73,7 @@ QObject *qobject_from_jsonf(const char *string, ...)
     obj = qobject_from_jsonv(string, &ap, &error_abort);
     va_end(ap);
 
+    assert(obj);
     return obj;
 }
 
tests/check-qjson.c +8 -7
index da582df3e9..eaf5d20663 100644
@@ -865,7 +865,8 @@ static void vararg_string(void)
         QString *str;
 
         str = qobject_to(QString,
-                         qobject_from_jsonf("%s", test_cases[i].decoded));
+                         qobject_from_jsonf_nofail("%s",
+                                                   test_cases[i].decoded));
         g_assert(str);
         g_assert(strcmp(qstring_get_str(str), test_cases[i].decoded) == 0);
 
@@ -998,17 +999,17 @@ static void vararg_number(void)
     double valuef = 2.323423423;
     int64_t val;
 
-    qnum = qobject_to(QNum, qobject_from_jsonf("%d", value));
+    qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%d", value));
     g_assert(qnum_get_try_int(qnum, &val));
     g_assert_cmpint(val, ==, value);
     qobject_unref(qnum);
 
-    qnum = qobject_to(QNum, qobject_from_jsonf("%lld", value_ll));
+    qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%lld", value_ll));
     g_assert(qnum_get_try_int(qnum, &val));
     g_assert_cmpint(val, ==, value_ll);
     qobject_unref(qnum);
 
-    qnum = qobject_to(QNum, qobject_from_jsonf("%f", valuef));
+    qnum = qobject_to(QNum, qobject_from_jsonf_nofail("%f", valuef));
     g_assert(qnum_get_double(qnum) == valuef);
     qobject_unref(qnum);
 }
@@ -1042,13 +1043,13 @@ static void keyword_literal(void)
 
     qobject_unref(qbool);
 
-    qbool = qobject_to(QBool, qobject_from_jsonf("%i", false));
+    qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", false));
     g_assert(qbool);
     g_assert(qbool_get_bool(qbool) == false);
     qobject_unref(qbool);
 
     /* Test that non-zero values other than 1 get collapsed to true */
-    qbool = qobject_to(QBool, qobject_from_jsonf("%i", 2));
+    qbool = qobject_to(QBool, qobject_from_jsonf_nofail("%i", 2));
     g_assert(qbool);
     g_assert(qbool_get_bool(qbool) == true);
     qobject_unref(qbool);
@@ -1298,7 +1299,7 @@ static void simple_varargs(void)
     embedded_obj = qobject_from_json("[32, 42]", &error_abort);
     g_assert(embedded_obj != NULL);
 
-    obj = qobject_from_jsonf("[%d, 2, %p]", 1, embedded_obj);
+    obj = qobject_from_jsonf_nofail("[%d, 2, %p]", 1, embedded_obj);
     g_assert(qlit_equal_qobject(&decoded, obj));
 
     qobject_unref(obj);
tests/libqtest.h +9 -9
index ced95f490a..a67e5e34eb 100644
@@ -77,7 +77,7 @@ void qtest_quit(QTestState *s);
  * qtest_qmp_discard_response:
  * @s: #QTestState instance to operate on.
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and consumes the response.
@@ -88,7 +88,7 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
  * qtest_qmp:
  * @s: #QTestState instance to operate on.
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and returns the response.
@@ -99,7 +99,7 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
  * qtest_qmp_send:
  * @s: #QTestState instance to operate on.
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
@@ -110,7 +110,7 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...);
  * qtest_qmpv_discard_response:
  * @s: #QTestState instance to operate on.
  * @fmt: QMP message to send to QEMU, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
@@ -122,7 +122,7 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
  * qtest_qmpv:
  * @s: #QTestState instance to operate on.
  * @fmt: QMP message to send to QEMU, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
@@ -134,7 +134,7 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
  * qtest_qmp_vsend:
  * @s: #QTestState instance to operate on.
  * @fmt: QMP message to send to QEMU, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  * @ap: QMP message arguments
  *
@@ -575,7 +575,7 @@ static inline void qtest_end(void)
 /**
  * qmp:
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and returns the response.
@@ -585,7 +585,7 @@ QDict *qmp(const char *fmt, ...);
 /**
  * qmp_send:
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
@@ -595,7 +595,7 @@ void qmp_send(const char *fmt, ...);
 /**
  * qmp_discard_response:
  * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf().
+ * qobject_from_jsonf_nofail().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and consumes the response.
Signature · 1 line
2.17.1
#33 lore raw

PATCH v2 14/23 migration-test: Make wait_command() return the "return" member

In reply to Markus Armbruster

All callers of wait_command() are only interested in the success response's "return" member. Lift its extraction into wait_command().

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
 tests/migration-test.c | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)
tests/migration-test.c +15 -23
index c6c7089ce1..e336399a71 100644
@@ -152,7 +152,7 @@ static void wait_for_serial(const char *side)
 static QDict *wait_command(QTestState *who, const char *command)
 {
     const char *event_string;
-    QDict *response;
+    QDict *response, *ret;
 
     response = qtest_qmp(who, command);
 
@@ -165,7 +165,12 @@ static QDict *wait_command(QTestState *who, const char *command)
         qobject_unref(response);
         response = qtest_qmp_receive(who);
     }
-    return response;
+
+    ret = qdict_get_qdict(response, "return");
+    g_assert(ret);
+    qobject_ref(ret);
+    qobject_unref(response);
+    return ret;
 }
 
 /*
@@ -174,15 +179,7 @@ static QDict *wait_command(QTestState *who, const char *command)
  */
 static QDict *migrate_query(QTestState *who)
 {
-    QDict *rsp, *rsp_return;
-
-    rsp = wait_command(who, "{ 'execute': 'query-migrate' }");
-    rsp_return = qdict_get_qdict(rsp, "return");
-    g_assert(rsp_return);
-    qobject_ref(rsp_return);
-    qobject_unref(rsp);
-
-    return rsp_return;
+    return wait_command(who, "{ 'execute': 'query-migrate' }");
 }
 
 /*
@@ -324,16 +321,16 @@ static void cleanup(const char *filename)
 static void migrate_check_parameter(QTestState *who, const char *parameter,
                                     const char *value)
 {
-    QDict *rsp, *rsp_return;
+    QDict *rsp_return;
     char *result;
 
-    rsp = wait_command(who, "{ 'execute': 'query-migrate-parameters' }");
-    rsp_return = qdict_get_qdict(rsp, "return");
+    rsp_return = wait_command(who,
+                              "{ 'execute': 'query-migrate-parameters' }");
     result = g_strdup_printf("%" PRId64,
                              qdict_get_try_int(rsp_return,  parameter, -1));
     g_assert_cmpstr(result, ==, value);
     g_free(result);
-    qobject_unref(rsp);
+    qobject_unref(rsp_return);
 }
 
 static void migrate_set_parameter(QTestState *who, const char *parameter,
@@ -357,7 +354,6 @@ static void migrate_pause(QTestState *who)
     QDict *rsp;
 
     rsp = wait_command(who, "{ 'execute': 'migrate-pause' }");
-    g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 }
 
@@ -370,7 +366,6 @@ static void migrate_recover(QTestState *who, const char *uri)
         "  'arguments': { 'uri': '%s' } }", uri);
 
     rsp = wait_command(who, cmd);
-    g_assert(qdict_haskey(rsp, "return"));
     g_free(cmd);
     qobject_unref(rsp);
 }
@@ -411,7 +406,6 @@ static void migrate_postcopy_start(QTestState *from, QTestState *to)
     QDict *rsp;
 
     rsp = wait_command(from, "{ 'execute': 'migrate-start-postcopy' }");
-    g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 
     if (!got_stop) {
@@ -689,7 +683,7 @@ static void test_postcopy_recovery(void)
 static void test_baddest(void)
 {
     QTestState *from, *to;
-    QDict *rsp, *rsp_return;
+    QDict *rsp_return;
     char *status;
     bool failed;
 
@@ -705,12 +699,10 @@ static void test_baddest(void)
     } while (!failed);
 
     /* Is the machine currently running? */
-    rsp = wait_command(from, "{ 'execute': 'query-status' }");
-    g_assert(qdict_haskey(rsp, "return"));
-    rsp_return = qdict_get_qdict(rsp, "return");
+    rsp_return = wait_command(from, "{ 'execute': 'query-status' }");
     g_assert(qdict_haskey(rsp_return, "running"));
     g_assert(qdict_get_bool(rsp_return, "running"));
-    qobject_unref(rsp);
+    qobject_unref(rsp_return);
 
     test_migrate_end(from, to, false);
 }
Signature · 1 line
2.17.1
#45 lore raw

PATCH v2 19/23 migration-test: Clean up string interpolation into QMP, part 3

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migration-test.c interpolates strings into JSON in a few places:

* migrate_set_parameter() interpolates string parameter @value as a JSON number. Change it to long long. This requires changing migrate_check_parameter() similarly.

* migrate_set_capability() interpolates string parameter @value as a JSON boolean. Change it to bool.

* deprecated_set_speed() interpolates string parameter @value as a JSON number. Change it to long long.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
 tests/migration-test.c | 74 +++++++++++++++++-------------------------
 1 file changed, 29 insertions(+), 45 deletions(-)
tests/migration-test.c +29 -45
index 1c1e56b15b..132c30891d 100644
@@ -315,31 +315,25 @@ static void cleanup(const char *filename)
 }
 
 static void migrate_check_parameter(QTestState *who, const char *parameter,
-                                    const char *value)
+                                    long long value)
 {
     QDict *rsp_return;
-    char *result;
 
     rsp_return = wait_command(who,
                               "{ 'execute': 'query-migrate-parameters' }");
-    result = g_strdup_printf("%" PRId64,
-                             qdict_get_try_int(rsp_return,  parameter, -1));
-    g_assert_cmpstr(result, ==, value);
-    g_free(result);
+    g_assert_cmpint(qdict_get_int(rsp_return,  parameter), ==, value);
     qobject_unref(rsp_return);
 }
 
 static void migrate_set_parameter(QTestState *who, const char *parameter,
-                                  const char *value)
+                                  long long value)
 {
     QDict *rsp;
-    gchar *cmd;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate-set-parameters',"
-                          "'arguments': { '%s': %s } }",
-                          parameter, value);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who,
+                    "{ 'execute': 'migrate-set-parameters',"
+                    "'arguments': { %s: %lld } }",
+                    parameter, value);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
     migrate_check_parameter(who, parameter, value);
@@ -366,18 +360,16 @@ static void migrate_recover(QTestState *who, const char *uri)
 }
 
 static void migrate_set_capability(QTestState *who, const char *capability,
-                                   const char *value)
+                                   bool value)
 {
     QDict *rsp;
-    gchar *cmd;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate-set-capabilities',"
-                          "'arguments': { "
-                          "'capabilities': [ { "
-                          "'capability': '%s', 'state': %s } ] } }",
-                          capability, value);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who,
+                    "{ 'execute': 'migrate-set-capabilities',"
+                    "'arguments': { "
+                    "'capabilities': [ { "
+                    "'capability': %s, 'state': %i } ] } }",
+                    capability, value);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 }
@@ -521,29 +513,21 @@ static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
 static void deprecated_set_downtime(QTestState *who, const double value)
 {
     QDict *rsp;
-    char *expected;
-    int64_t result_int;
 
     rsp = qtest_qmp(who,
                     "{ 'execute': 'migrate_set_downtime',"
                     " 'arguments': { 'value': %f } }", value);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
-    result_int = value * 1000L;
-    expected = g_strdup_printf("%" PRId64, result_int);
-    migrate_check_parameter(who, "downtime-limit", expected);
-    g_free(expected);
+    migrate_check_parameter(who, "downtime-limit", value * 1000);
 }
 
-static void deprecated_set_speed(QTestState *who, const char *value)
+static void deprecated_set_speed(QTestState *who, long long value)
 {
     QDict *rsp;
-    gchar *cmd;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate_set_speed',"
-                          "'arguments': { 'value': %s } }", value);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who, "{ 'execute': 'migrate_set_speed',"
+                          "'arguments': { 'value': %lld } }", value);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
     migrate_check_parameter(who, "max-bandwidth", value);
@@ -556,7 +540,7 @@ static void test_deprecated(void)
     from = qtest_start("");
 
     deprecated_set_downtime(from, 0.12345);
-    deprecated_set_speed(from, "12345");
+    deprecated_set_speed(from, 12345);
 
     qtest_quit(from);
 }
@@ -572,16 +556,16 @@ static int migrate_postcopy_prepare(QTestState **from_ptr,
         return -1;
     }
 
-    migrate_set_capability(from, "postcopy-ram", "true");
-    migrate_set_capability(to, "postcopy-ram", "true");
-    migrate_set_capability(to, "postcopy-blocktime", "true");
+    migrate_set_capability(from, "postcopy-ram", true);
+    migrate_set_capability(to, "postcopy-ram", true);
+    migrate_set_capability(to, "postcopy-blocktime", true);
 
     /* We want to pick a speed slow enough that the test completes
      * quickly, but that it doesn't complete precopy even on a slow
      * machine, so also set the downtime.
      */
-    migrate_set_parameter(from, "max-bandwidth", "100000000");
-    migrate_set_parameter(from, "downtime-limit", "1");
+    migrate_set_parameter(from, "max-bandwidth", 100000000);
+    migrate_set_parameter(from, "downtime-limit", 1);
 
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");
@@ -632,7 +616,7 @@ static void test_postcopy_recovery(void)
     }
 
     /* Turn postcopy speed down, 4K/s is slow enough on any machines */
-    migrate_set_parameter(from, "max-postcopy-bandwidth", "4096");
+    migrate_set_parameter(from, "max-postcopy-bandwidth", 4096);
 
     /* Now we start the postcopy */
     migrate_postcopy_start(from, to);
@@ -673,7 +657,7 @@ static void test_postcopy_recovery(void)
     g_free(uri);
 
     /* Restore the postcopy bandwidth to unlimited */
-    migrate_set_parameter(from, "max-postcopy-bandwidth", "0");
+    migrate_set_parameter(from, "max-postcopy-bandwidth", 0);
 
     migrate_postcopy_complete(from, to);
 }
@@ -719,9 +703,9 @@ static void test_precopy_unix(void)
      * machine, so also set the downtime.
      */
     /* 1 ms should make it not converge*/
-    migrate_set_parameter(from, "downtime-limit", "1");
+    migrate_set_parameter(from, "downtime-limit", 1);
     /* 1GB/s */
-    migrate_set_parameter(from, "max-bandwidth", "1000000000");
+    migrate_set_parameter(from, "max-bandwidth", 1000000000);
 
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");
@@ -731,7 +715,7 @@ static void test_precopy_unix(void)
     wait_for_migration_pass(from);
 
     /* 300 ms should converge */
-    migrate_set_parameter(from, "downtime-limit", "300");
+    migrate_set_parameter(from, "downtime-limit", 300);
 
     if (!got_stop) {
         qtest_qmp_eventwait(from, "STOP");
Signature · 1 line
2.17.1
#50 lore raw

PATCH v2 21/23 libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter. Moreover, the presence of these functions encourage sloppy testing. Remove them.

---
 tests/ahci-test.c              | 12 ++++++------
 tests/boot-order-test.c        |  4 ++--
 tests/drive_del-test.c         |  4 ++--
 tests/fdc-test.c               |  9 +++++----
 tests/ide-test.c               |  4 ++--
 tests/libqtest.c               | 27 +-------------------------
 tests/libqtest.h               | 35 ----------------------------------
 tests/migration-test.c         |  2 +-
 tests/test-filter-mirror.c     |  3 ++-
 tests/test-filter-redirector.c |  5 +++--
 tests/virtio-blk-test.c        | 19 +++++++++---------
 11 files changed, 34 insertions(+), 90 deletions(-)
tests/ahci-test.c +6 -6
index 19a7ab1bca..7a2d70fa34 100644
@@ -1596,8 +1596,8 @@ static void test_atapi_tray(void)
     rsp = qmp_receive();
     qobject_unref(rsp);
 
-    qmp_discard_response("{'execute': 'blockdev-remove-medium', "
-                         "'arguments': {'id': 'cd0'}}");
+    qobject_unref(qmp("{'execute': 'blockdev-remove-medium', "
+                      "'arguments': {'id': 'cd0'}}"));
 
     /* Test the tray without a medium */
     ahci_atapi_load(ahci, port);
@@ -1607,14 +1607,14 @@ static void test_atapi_tray(void)
     atapi_wait_tray(true);
 
     /* Re-insert media */
-    qmp_discard_response("{'execute': 'blockdev-add', "
+    qobject_unref(qmp("{'execute': 'blockdev-add', "
                           "'arguments': {'node-name': 'node0', "
                                         "'driver': 'raw', "
                                         "'file': { 'driver': 'file', "
-                                                  "'filename': %s }}}", iso);
-    qmp_discard_response("{'execute': 'blockdev-insert-medium',"
+                      "'filename': %s }}}", iso));
+    qobject_unref(qmp("{'execute': 'blockdev-insert-medium',"
                           "'arguments': { 'id': 'cd0', "
-                                         "'node-name': 'node0' }}");
+                      "'node-name': 'node0' }}"));
 
     /* Again, the event shows up first */
     qmp_send("{'execute': 'blockdev-close-tray',"
tests/boot-order-test.c +2 -2
index e70f5dedba..2ec86c0ee2 100644
@@ -13,7 +13,7 @@
 #include "qemu/osdep.h"
 #include "libqos/fw_cfg.h"
 #include "libqtest.h"
-
+#include "qapi/qmp/qdict.h"
 #include "hw/nvram/fw_cfg_keys.h"
 
 typedef struct {
@@ -36,7 +36,7 @@ static void test_a_boot_order(const char *machine,
                                 test_args);
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_boot);
-    qmp_discard_response("{ 'execute': 'system_reset' }");
+    qobject_unref(qmp("{ 'execute': 'system_reset' }"));
     /*
      * system_reset only requests reset.  We get a RESET event after
      * the actual reset completes.  Need to wait for that.
tests/drive_del-test.c +2 -2
index 852fefc8f3..92489be0ba 100644
@@ -36,8 +36,8 @@ static void device_del(void)
     QDict *response;
 
     /* Complication: ignore DEVICE_DELETED event */
-    qmp_discard_response("{'execute': 'device_del',"
-                         " 'arguments': { 'id': 'dev0' } }");
+    qobject_unref(qmp("{'execute': 'device_del',"
+                      " 'arguments': { 'id': 'dev0' } }"));
     response = qmp_receive();
     g_assert(response);
     g_assert(qdict_haskey(response, "return"));
tests/fdc-test.c +5 -4
index 325712e0f2..5775555e71 100644
@@ -26,6 +26,7 @@
 
 
 #include "libqtest.h"
+#include "qapi/qmp/qdict.h"
 #include "qemu-common.h"
 
 #define TEST_IMAGE_SIZE 1440 * 1024
@@ -298,9 +299,9 @@ static void test_media_insert(void)
 
     /* Insert media in drive. DSKCHK should not be reset until a step pulse
      * is sent. */
-    qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
+    qobject_unref(qmp("{'execute':'blockdev-change-medium', 'arguments':{"
                          " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
-                         test_image);
+                      test_image));
 
     dir = inb(FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
@@ -329,8 +330,8 @@ static void test_media_change(void)
 
     /* Eject the floppy and check that DSKCHG is set. Reading it out doesn't
      * reset the bit. */
-    qmp_discard_response("{'execute':'eject', 'arguments':{"
-                         " 'id':'floppy0' }}");
+    qobject_unref(qmp("{'execute':'eject', 'arguments':{"
+                      " 'id':'floppy0' }}"));
 
     dir = inb(FLOPPY_BASE + reg_dir);
     assert_bit_set(dir, DSKCHG);
tests/ide-test.c +2 -2
index 834ee9005b..48bb0e6519 100644
@@ -29,7 +29,7 @@
 #include "libqos/libqos.h"
 #include "libqos/pci-pc.h"
 #include "libqos/malloc-pc.h"
-
+#include "qapi/qmp/qdict.h"
 #include "qemu-common.h"
 #include "qemu/bswap.h"
 #include "hw/pci/pci_ids.h"
@@ -721,7 +721,7 @@ static void test_retry_flush(const char *machine)
     qmp_eventwait("STOP");
 
     /* Complete the command */
-    qmp_discard_response("{'execute':'cont' }");
+    qobject_unref(qmp("{'execute':'cont' }"));
 
     /* Check registers */
     data = qpci_io_readb(dev, ide_bar, reg_device);
tests/libqtest.c +1 -26
index ed832cd8e6..2f81bc6382 100644
@@ -254,7 +254,7 @@ QTestState *qtest_init(const char *extra_args)
     /* Read the QMP greeting and then do the handshake */
     greeting = qtest_qmp_receive(s);
     qobject_unref(greeting);
-    qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
+    qobject_unref(qtest_qmp(s, "{ 'execute': 'qmp_capabilities' }"));
 
     return s;
 }
@@ -587,23 +587,6 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...)
     va_end(ap);
 }
 
-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
-{
-    QDict *response = qtest_qmpv(s, fmt, ap);
-    qobject_unref(response);
-}
-
-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
-{
-    va_list ap;
-    QDict *response;
-
-    va_start(ap, fmt);
-    response = qtest_qmpv(s, fmt, ap);
-    va_end(ap);
-    qobject_unref(response);
-}
-
 QDict *qtest_qmp_eventwait_ref(QTestState *s, const char *event)
 {
     QDict *response;
@@ -975,14 +958,6 @@ void qmp_send(const char *fmt, ...)
     va_end(ap);
 }
 
-void qmp_discard_response(const char *fmt, ...)
-{
-    va_list ap;
-
-    va_start(ap, fmt);
-    qtest_qmpv_discard_response(global_qtest, fmt, ap);
-    va_end(ap);
-}
 char *hmp(const char *fmt, ...)
 {
     va_list ap;
tests/libqtest.h +0 -35
index 327e402da5..58dea82c76 100644
@@ -73,18 +73,6 @@ QTestState *qtest_init_without_qmp_handshake(bool use_oob,
  */
 void qtest_quit(QTestState *s);
 
-/**
- * qtest_qmp_discard_response:
- * @s: #QTestState instance to operate on.
- * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf_nofail().
- * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
- *
- * Sends a QMP message to QEMU and consumes the response.
- */
-void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...)
-    GCC_FMT_ATTR(2, 3);
-
 /**
  * qtest_qmp:
  * @s: #QTestState instance to operate on.
@@ -109,19 +97,6 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
 void qtest_qmp_send(QTestState *s, const char *fmt, ...)
     GCC_FMT_ATTR(2, 3);
 
-/**
- * qtest_qmpv_discard_response:
- * @s: #QTestState instance to operate on.
- * @fmt: QMP message to send to QEMU, formatted like
- * qobject_from_jsonf_nofail().
- * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
- * @ap: QMP message arguments
- *
- * Sends a QMP message to QEMU and consumes the response.
- */
-void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap)
-    GCC_FMT_ATTR(2, 0);
-
 /**
  * qtest_qmpv:
  * @s: #QTestState instance to operate on.
@@ -615,16 +590,6 @@ QDict *qmp(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
  */
 void qmp_send(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
-/**
- * qmp_discard_response:
- * @fmt...: QMP message to send to qemu, formatted like
- * qobject_from_jsonf_nofail().
- * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
- *
- * Sends a QMP message to QEMU and consumes the response.
- */
-void qmp_discard_response(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
-
 /**
  * qmp_receive:
  *
tests/migration-test.c +1 -1
index 132c30891d..1bcd32e284 100644
@@ -491,7 +491,7 @@ static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
             usleep(1000 * 10);
         } while (dest_byte_a == dest_byte_b);
 
-        qtest_qmp_discard_response(to, "{ 'execute' : 'stop'}");
+        qobject_unref(qtest_qmp(to, "{ 'execute' : 'stop'}"));
 
         /* With it stopped, check nothing changes */
         qtest_memread(to, start_address, &dest_byte_c, 1);
tests/test-filter-mirror.c +2 -1
index 6c6f710dc6..1ab4759b80 100644
@@ -10,6 +10,7 @@
 
 #include "qemu/osdep.h"
 #include "libqtest.h"
+#include "qapi/qmp/qdict.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
 #include "qemu/error-report.h"
@@ -57,7 +58,7 @@ static void test_mirror(void)
     };
 
     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qobject_unref(qmp("{ 'execute' : 'query-status'}"));
     ret = iov_send(send_sock[0], iov, 2, 0, sizeof(size) + sizeof(send_buf));
     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
     close(send_sock[0]);
tests/test-filter-redirector.c +3 -2
index fbaf19bbd8..5b6c594867 100644
@@ -52,6 +52,7 @@
 
 #include "qemu/osdep.h"
 #include "libqtest.h"
+#include "qapi/qmp/qdict.h"
 #include "qemu/iov.h"
 #include "qemu/sockets.h"
 #include "qemu/error-report.h"
@@ -104,7 +105,7 @@ static void test_redirector_tx(void)
     g_assert_cmpint(recv_sock, !=, -1);
 
     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qobject_unref(qmp("{ 'execute' : 'query-status'}"));
 
     struct iovec iov[] = {
         {
@@ -182,7 +183,7 @@ static void test_redirector_rx(void)
     send_sock = unix_connect(sock_path1, NULL);
     g_assert_cmpint(send_sock, !=, -1);
     /* send a qmp command to guarantee that 'connected' is setting to true. */
-    qmp_discard_response("{ 'execute' : 'query-status'}");
+    qobject_unref(qmp("{ 'execute' : 'query-status'}"));
 
     ret = iov_send(send_sock, iov, 2, 0, sizeof(size) + sizeof(send_buf));
     g_assert_cmpint(ret, ==, sizeof(send_buf) + sizeof(size));
tests/virtio-blk-test.c +10 -9
index d96b4c69e1..5955bf6d57 100644
@@ -16,6 +16,7 @@
 #include "libqos/virtio-pci.h"
 #include "libqos/virtio-mmio.h"
 #include "libqos/malloc-generic.h"
+#include "qapi/qmp/qdict.h"
 #include "qemu/bswap.h"
 #include "standard-headers/linux/virtio_ids.h"
 #include "standard-headers/linux/virtio_config.h"
@@ -409,9 +410,9 @@ static void pci_config(void)
 
     qvirtio_set_driver_ok(&dev->vdev);
 
-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qobject_unref(qmp("{ 'execute': 'block_resize', "
+                      " 'arguments': { 'device': 'drive0', "
+                      " 'size': %d } }", n_size));
     qvirtio_wait_config_isr(&dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
 
     capacity = qvirtio_config_readq(&dev->vdev, 0);
@@ -459,9 +460,9 @@ static void pci_msix(void)
 
     qvirtio_set_driver_ok(&dev->vdev);
 
-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qobject_unref(qmp("{ 'execute': 'block_resize', "
+                      " 'arguments': { 'device': 'drive0', "
+                      " 'size': %d } }", n_size));
 
     qvirtio_wait_config_isr(&dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
 
@@ -725,9 +726,9 @@ static void mmio_basic(void)
 
     test_basic(&dev->vdev, alloc, vq);
 
-    qmp_discard_response("{ 'execute': 'block_resize', "
-                         " 'arguments': { 'device': 'drive0', "
-                         " 'size': %d } }", n_size);
+    qobject_unref(qmp("{ 'execute': 'block_resize', "
+                      " 'arguments': { 'device': 'drive0', "
+                      " 'size': %d } }", n_size));
 
     qvirtio_wait_queue_isr(&dev->vdev, vq, QVIRTIO_BLK_TIMEOUT_US);
 
Signature · 1 line
2.17.1
#60 lore raw

PATCH v2 22/23 libqtest: Replace qtest_startf() by qtest_initf()

In reply to Markus Armbruster

qtest_init() creates a new QTestState, and leaves @global_qtest alone. qtest_start() additionally assigns it to @global_qtest, but qtest_startf() additionall sets assigns NULL to @global_qtest. This makes no sense. Replace it by qtest_initf() that works like qtest_init(), i.e. leaves @global_qtest alone.

---
 tests/boot-order-test.c        |  8 ++++----
 tests/boot-serial-test.c       | 10 +++++-----
 tests/cdrom-test.c             |  6 +++---
 tests/endianness-test.c        | 24 ++++++++++++------------
 tests/ipmi-bt-test.c           |  2 +-
 tests/libqtest.c               |  9 ++++-----
 tests/libqtest.h               | 17 ++++++++---------
 tests/m25p80-test.c            |  6 +++---
 tests/m48t59-test.c            |  2 +-
 tests/machine-none-test.c      |  2 +-
 tests/numa-test.c              |  4 ++--
 tests/pnv-xscom-test.c         |  8 ++++----
 tests/prom-env-test.c          | 10 +++++-----
 tests/qmp-test.c               |  2 +-
 tests/sdhci-test.c             |  6 +++---
 tests/tco-test.c               |  6 +++---
 tests/test-filter-mirror.c     |  2 +-
 tests/test-filter-redirector.c |  4 ++--
 tests/virtio-balloon-test.c    |  4 ++--
 tests/virtio-blk-test.c        |  8 ++++----
 tests/virtio-console-test.c    | 12 ++++++------
 tests/virtio-serial-test.c     |  4 ++--
 tests/vmgenid-test.c           |  6 +++---
 23 files changed, 80 insertions(+), 82 deletions(-)
tests/boot-order-test.c +4 -4
index 2ec86c0ee2..bf0702945b 100644
@@ -30,10 +30,10 @@ static void test_a_boot_order(const char *machine,
 {
     uint64_t actual;
 
-    global_qtest = qtest_startf("-nodefaults%s%s %s",
-                                machine ? " -M " : "",
-                                machine ?: "",
-                                test_args);
+    global_qtest = qtest_initf("-nodefaults%s%s %s",
+                               machine ? " -M " : "",
+                               machine ?: "",
+                               test_args);
     actual = read_boot_order();
     g_assert_cmphex(actual, ==, expected_boot);
     qobject_unref(qmp("{ 'execute': 'system_reset' }"));
tests/boot-serial-test.c +5 -5
index 952a2e7ead..1355df924d 100644
@@ -172,11 +172,11 @@ static void test_machine(const void *data)
      * Make sure that this test uses tcg if available: It is used as a
      * fast-enough smoketest for that.
      */
-    global_qtest = qtest_startf("%s %s -M %s,accel=tcg:kvm "
-                                "-chardev file,id=serial0,path=%s "
-                                "-no-shutdown -serial chardev:serial0 %s",
-                                codeparam, code ? codetmp : "",
-                                test->machine, serialtmp, test->extra);
+    global_qtest = qtest_initf("%s %s -M %s,accel=tcg:kvm "
+                               "-chardev file,id=serial0,path=%s "
+                               "-no-shutdown -serial chardev:serial0 %s",
+                               codeparam, code ? codetmp : "",
+                               test->machine, serialtmp, test->extra);
     if (code) {
         unlink(codetmp);
     }
tests/cdrom-test.c +3 -3
index 7a1fce5dfb..9b43dc9ab4 100644
@@ -99,7 +99,7 @@ static void test_cdrom_param(gconstpointer data)
     QTestState *qts;
     char *resp;
 
-    qts = qtest_startf("-M %s -cdrom %s", (const char *)data, isoimage);
+    qts = qtest_initf("-M %s -cdrom %s", (const char *)data, isoimage);
     resp = qtest_hmp(qts, "info block");
     g_assert(strstr(resp, isoimage) != 0);
     g_free(resp);
@@ -120,8 +120,8 @@ static void test_cdboot(gconstpointer data)
 {
     QTestState *qts;
 
-    qts = qtest_startf("-accel kvm:tcg -no-shutdown %s%s", (const char *)data,
-                       isoimage);
+    qts = qtest_initf("-accel kvm:tcg -no-shutdown %s%s", (const char *)data,
+                      isoimage);
     boot_sector_test(qts);
     qtest_quit(qts);
 }
tests/endianness-test.c +12 -12
index 546e0969e4..48680cd131 100644
@@ -115,10 +115,10 @@ static void test_endianness(gconstpointer data)
 {
     const TestCase *test = data;
 
-    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
-                                test->machine,
-                                test->superio ? " -device " : "",
-                                test->superio ?: "");
+    global_qtest = qtest_initf("-M %s%s%s -device pc-testdev",
+                               test->machine,
+                               test->superio ? " -device " : "",
+                               test->superio ?: "");
     isa_outl(test, 0xe0, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
@@ -187,10 +187,10 @@ static void test_endianness_split(gconstpointer data)
 {
     const TestCase *test = data;
 
-    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
-                                test->machine,
-                                test->superio ? " -device " : "",
-                                test->superio ?: "");
+    global_qtest = qtest_initf("-M %s%s%s -device pc-testdev",
+                               test->machine,
+                               test->superio ? " -device " : "",
+                               test->superio ?: "");
     isa_outl(test, 0xe8, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe0), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xe2), ==, 0x8765);
@@ -231,10 +231,10 @@ static void test_endianness_combine(gconstpointer data)
 {
     const TestCase *test = data;
 
-    global_qtest = qtest_startf("-M %s%s%s -device pc-testdev",
-                                test->machine,
-                                test->superio ? " -device " : "",
-                                test->superio ?: "");
+    global_qtest = qtest_initf("-M %s%s%s -device pc-testdev",
+                               test->machine,
+                               test->superio ? " -device " : "",
+                               test->superio ?: "");
     isa_outl(test, 0xe0, 0x87654321);
     g_assert_cmphex(isa_inl(test, 0xe8), ==, 0x87654321);
     g_assert_cmphex(isa_inw(test, 0xea), ==, 0x8765);
tests/ipmi-bt-test.c +1 -1
index 8be18e3f42..f4a81b5265 100644
@@ -414,7 +414,7 @@ int main(int argc, char **argv)
     /* Run the tests */
     g_test_init(&argc, &argv, NULL);
 
-    global_qtest = qtest_startf(
+    global_qtest = qtest_initf(
         " -chardev socket,id=ipmi0,host=localhost,port=%d,reconnect=10"
         " -device ipmi-bmc-extern,chardev=ipmi0,id=bmc0"
         " -device isa-ipmi-bt,bmc=bmc0", emu_port);
tests/libqtest.c +4 -5
index 2f81bc6382..a0d44793fa 100644
@@ -259,24 +259,23 @@ QTestState *qtest_init(const char *extra_args)
     return s;
 }
 
-QTestState *qtest_vstartf(const char *fmt, va_list ap)
+QTestState *qtest_vinitf(const char *fmt, va_list ap)
 {
     char *args = g_strdup_vprintf(fmt, ap);
     QTestState *s;
 
-    s = qtest_start(args);
+    s = qtest_init(args);
     g_free(args);
-    global_qtest = NULL;
     return s;
 }
 
-QTestState *qtest_startf(const char *fmt, ...)
+QTestState *qtest_initf(const char *fmt, ...)
 {
     va_list ap;
     QTestState *s;
 
     va_start(ap, fmt);
-    s = qtest_vstartf(fmt, ap);
+    s = qtest_vinitf(fmt, ap);
     va_end(ap);
     return s;
 }
tests/libqtest.h +8 -9
index 58dea82c76..3848086928 100644
@@ -22,33 +22,32 @@ typedef struct QTestState QTestState;
 extern QTestState *global_qtest;
 
 /**
- * qtest_startf:
+ * qtest_initf:
  * @fmt...: Format for creating other arguments to pass to QEMU, formatted
  * like sprintf().
  *
- * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
- * #global_qtest is left at NULL).
+ * Convenience wrapper around qtest_start().
  *
  * Returns: #QTestState instance.
  */
-QTestState *qtest_startf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
+QTestState *qtest_initf(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
 /**
- * qtest_vstartf:
+ * qtest_vinitf:
  * @fmt: Format for creating other arguments to pass to QEMU, formatted
  * like vsprintf().
  * @ap: Format arguments.
  *
- * Start QEMU and return the resulting #QTestState (but unlike qtest_start(),
- * #global_qtest is left at NULL).
+ * Convenience wrapper around qtest_start().
  *
  * Returns: #QTestState instance.
  */
-QTestState *qtest_vstartf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
+QTestState *qtest_vinitf(const char *fmt, va_list ap) GCC_FMT_ATTR(1, 0);
 
 /**
  * qtest_init:
- * @extra_args: other arguments to pass to QEMU.
+ * @extra_args: other arguments to pass to QEMU.  CAUTION: these
+ * arguments are subject to word splitting and shell evaluation.
  *
  * Returns: #QTestState instance.
  */
tests/m25p80-test.c +3 -3
index c276e738e9..055f7246a8 100644
@@ -363,9 +363,9 @@ int main(int argc, char **argv)
     g_assert(ret == 0);
     close(fd);
 
-    global_qtest = qtest_startf("-m 256 -machine palmetto-bmc "
-                                "-drive file=%s,format=raw,if=mtd",
-                                tmp_path);
+    global_qtest = qtest_initf("-m 256 -machine palmetto-bmc "
+                               "-drive file=%s,format=raw,if=mtd",
+                               tmp_path);
 
     qtest_add_func("/m25p80/read_jedec", test_read_jedec);
     qtest_add_func("/m25p80/erase_sector", test_erase_sector);
tests/m48t59-test.c +1 -1
index 5b695971c7..4abf9c605c 100644
@@ -146,7 +146,7 @@ static void cmos_get_date_time(QTestState *s, struct tm *date)
 
 static QTestState *m48t59_qtest_start(void)
 {
-    return qtest_startf("-M %s -rtc clock=vm", base_machine);
+    return qtest_initf("-M %s -rtc clock=vm", base_machine);
 }
 
 static void bcd_check_time(void)
tests/machine-none-test.c +1 -1
index f286557b3e..7e72466354 100644
@@ -84,7 +84,7 @@ static void test_machine_cpu_cli(void)
         }
         return; /* TODO: die here to force all targets have a test */
     }
-    global_qtest = qtest_startf("-machine none -cpu '%s'", cpu_model);
+    global_qtest = qtest_initf("-machine none -cpu '%s'", cpu_model);
 
     response = qmp("{ 'execute': 'quit' }");
     g_assert(qdict_haskey(response, "return"));
tests/numa-test.c +2 -2
index 893f826acb..9824fdd587 100644
@@ -267,8 +267,8 @@ static void pc_dynamic_cpu_cfg(const void *data)
     QList *cpus;
     QTestState *qs;
 
-    qs = qtest_startf("%s %s", data ? (char *)data : "",
-                              "-nodefaults --preconfig -smp 2");
+    qs = qtest_initf("%s -nodefaults --preconfig -smp 2",
+                     data ? (char *)data : "");
 
     /* create 2 numa nodes */
     g_assert(!qmp_rsp_is_err(qtest_qmp(qs, "{ 'execute': 'set-numa-node',"
tests/pnv-xscom-test.c +4 -4
index efb7c838b5..70f4c84d1b 100644
@@ -79,8 +79,8 @@ static void test_cfam_id(const void *data)
 {
     const PnvChip *chip = data;
 
-    global_qtest = qtest_startf("-M powernv,accel=tcg -cpu %s",
-                                chip->cpu_model);
+    global_qtest = qtest_initf("-M powernv,accel=tcg -cpu %s",
+                               chip->cpu_model);
     test_xscom_cfam_id(chip);
     qtest_quit(global_qtest);
 }
@@ -114,8 +114,8 @@ static void test_core(const void *data)
 {
     const PnvChip *chip = data;
 
-    global_qtest = qtest_startf("-M powernv,accel=tcg -cpu %s",
-                                chip->cpu_model);
+    global_qtest = qtest_initf("-M powernv,accel=tcg -cpu %s",
+                               chip->cpu_model);
     test_xscom_core(chip);
     qtest_quit(global_qtest);
 }
tests/prom-env-test.c +5 -5
index 8c867e631a..198d007f1b 100644
@@ -49,11 +49,11 @@ static void test_machine(const void *machine)
     /* The pseries firmware boots much faster without the default devices */
     extra_args = strcmp(machine, "pseries") == 0 ? "-nodefaults" : "";
 
-    global_qtest = qtest_startf("-M %s,accel=tcg %s "
-                                "-prom-env 'use-nvramrc?=true' "
-                                "-prom-env 'nvramrc=%x %x l!' ",
-                                (const char *)machine, extra_args,
-                                MAGIC, ADDRESS);
+    global_qtest = qtest_initf("-M %s,accel=tcg %s "
+                               "-prom-env 'use-nvramrc?=true' "
+                               "-prom-env 'nvramrc=%x %x l!' ",
+                               (const char *)machine, extra_args,
+                               MAGIC, ADDRESS);
     check_guest_memory();
     qtest_quit(global_qtest);
 }
tests/qmp-test.c +1 -1
index 5eb15daebc..487ef946ed 100644
@@ -436,7 +436,7 @@ static void add_query_tests(QmpSchema *schema)
 static void test_qmp_preconfig(void)
 {
     QDict *rsp, *ret;
-    QTestState *qs = qtest_startf("%s --preconfig", common_args);
+    QTestState *qs = qtest_initf("%s --preconfig", common_args);
 
     /* preconfig state */
     /* enabled commands, no error expected  */
tests/sdhci-test.c +3 -3
index 1d825eb010..982f5ebbb2 100644
@@ -184,8 +184,8 @@ static QSDHCI *machine_start(const struct sdhci_t *test)
         uint16_t vendor_id, device_id;
         uint64_t barsize;
 
-        global_qtest = qtest_startf("-machine %s -device sdhci-pci",
-                                    test->machine);
+        global_qtest = qtest_initf("-machine %s -device sdhci-pci",
+                                   test->machine);
 
         s->pci.bus = qpci_init_pc(global_qtest, NULL);
 
@@ -200,7 +200,7 @@ static QSDHCI *machine_start(const struct sdhci_t *test)
         qpci_device_enable(s->pci.dev);
     } else {
         /* SysBus */
-        global_qtest = qtest_startf("-machine %s", test->machine);
+        global_qtest = qtest_initf("-machine %s", test->machine);
         s->addr = test->sdhci.addr;
     }
 
tests/tco-test.c +3 -3
index 9945fb8469..6bee9a37d3 100644
@@ -58,9 +58,9 @@ static void test_init(TestData *d)
 {
     QTestState *qs;
 
-    qs = qtest_startf("-machine q35 %s %s",
-                      d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
-                      !d->args ? "" : d->args);
+    qs = qtest_initf("-machine q35 %s %s",
+                     d->noreboot ? "" : "-global ICH9-LPC.noreboot=false",
+                     !d->args ? "" : d->args);
     global_qtest = qs;
     qtest_irq_intercept_in(qs, "ioapic");
 
tests/test-filter-mirror.c +1 -1
index 1ab4759b80..1d66b5dbb0 100644
@@ -37,7 +37,7 @@ static void test_mirror(void)
     ret = mkstemp(sock_path);
     g_assert_cmpint(ret, !=, -1);
 
-    global_qtest = qtest_startf(
+    global_qtest = qtest_initf(
         "-netdev socket,id=qtest-bn0,fd=%d "
         "-device %s,netdev=qtest-bn0,id=qtest-e0 "
         "-chardev socket,id=mirror0,path=%s,server,nowait "
tests/test-filter-redirector.c +2 -2
index 5b6c594867..934ddee427 100644
@@ -87,7 +87,7 @@ static void test_redirector_tx(void)
     ret = mkstemp(sock_path1);
     g_assert_cmpint(ret, !=, -1);
 
-    global_qtest = qtest_startf(
+    global_qtest = qtest_initf(
         "-netdev socket,id=qtest-bn0,fd=%d "
         "-device %s,netdev=qtest-bn0,id=qtest-e0 "
         "-chardev socket,id=redirector0,path=%s,server,nowait "
@@ -156,7 +156,7 @@ static void test_redirector_rx(void)
     ret = mkstemp(sock_path1);
     g_assert_cmpint(ret, !=, -1);
 
-    global_qtest = qtest_startf(
+    global_qtest = qtest_initf(
         "-netdev socket,id=qtest-bn0,fd=%d "
         "-device %s,netdev=qtest-bn0,id=qtest-e0 "
         "-chardev socket,id=redirector0,path=%s,server,nowait "
tests/virtio-balloon-test.c +2 -2
index 0a07e036bb..5a1d0ccbb7 100644
@@ -23,8 +23,8 @@ int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
     qtest_add_func("/virtio/balloon/nop", balloon_nop);
 
-    global_qtest = qtest_startf("-device virtio-balloon-%s",
-                                qvirtio_get_dev_type());
+    global_qtest = qtest_initf("-device virtio-balloon-%s",
+                               qvirtio_get_dev_type());
     ret = g_test_run();
 
     qtest_end();
tests/virtio-blk-test.c +4 -4
index 5955bf6d57..d9893516bb 100644
@@ -90,10 +90,10 @@ static void arm_test_start(void)
 
     tmp_path = drive_create();
 
-    global_qtest = qtest_startf("-machine virt "
-                                "-drive if=none,id=drive0,file=%s,format=raw "
-                                "-device virtio-blk-device,drive=drive0",
-                                tmp_path);
+    global_qtest = qtest_initf("-machine virt "
+                               "-drive if=none,id=drive0,file=%s,format=raw "
+                               "-device virtio-blk-device,drive=drive0",
+                               tmp_path);
     unlink(tmp_path);
     g_free(tmp_path);
 }
tests/virtio-console-test.c +6 -6
index 945bae5a15..a7c6f167c3 100644
@@ -14,17 +14,17 @@
 /* Tests only initialization so far. TODO: Replace with functional tests */
 static void console_nop(void)
 {
-    global_qtest = qtest_startf("-device virtio-serial-%s,id=vser0 "
-                                "-device virtconsole,bus=vser0.0",
-                                qvirtio_get_dev_type());
+    global_qtest = qtest_initf("-device virtio-serial-%s,id=vser0 "
+                               "-device virtconsole,bus=vser0.0",
+                               qvirtio_get_dev_type());
     qtest_end();
 }
 
 static void serialport_nop(void)
 {
-    global_qtest = qtest_startf("-device virtio-serial-%s,id=vser0 "
-                                "-device virtserialport,bus=vser0.0",
-                                qvirtio_get_dev_type());
+    global_qtest = qtest_initf("-device virtio-serial-%s,id=vser0 "
+                               "-device virtserialport,bus=vser0.0",
+                               qvirtio_get_dev_type());
     qtest_end();
 }
 
tests/virtio-serial-test.c +2 -2
index e4b18b1c8a..8da9980a24 100644
@@ -31,8 +31,8 @@ int main(int argc, char **argv)
     qtest_add_func("/virtio/serial/nop", virtio_serial_nop);
     qtest_add_func("/virtio/serial/hotplug", hotplug);
 
-    global_qtest = qtest_startf("-device virtio-serial-%s",
-                                qvirtio_get_dev_type());
+    global_qtest = qtest_initf("-device virtio-serial-%s",
+                               qvirtio_get_dev_type());
     ret = g_test_run();
 
     qtest_end();
tests/vmgenid-test.c +3 -3
index 8d915c610c..0a6fb55f2e 100644
@@ -142,7 +142,7 @@ static void vmgenid_set_guid_test(void)
 
     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
 
-    global_qtest = qtest_startf(GUID_CMD(VGID_GUID));
+    global_qtest = qtest_initf(GUID_CMD(VGID_GUID));
 
     /* Read the GUID from accessing guest memory */
     read_guid_from_memory(&measured);
@@ -155,7 +155,7 @@ static void vmgenid_set_guid_auto_test(void)
 {
     QemuUUID measured;
 
-    global_qtest = qtest_startf(GUID_CMD("auto"));
+    global_qtest = qtest_initf(GUID_CMD("auto"));
 
     read_guid_from_memory(&measured);
 
@@ -171,7 +171,7 @@ static void vmgenid_query_monitor_test(void)
 
     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
 
-    global_qtest = qtest_startf(GUID_CMD(VGID_GUID));
+    global_qtest = qtest_initf(GUID_CMD(VGID_GUID));
 
     /* Read the GUID via the monitor */
     read_guid_from_monitor(&measured);
Signature · 1 line
2.17.1
#0 lore raw

v2 cover tests: Compile-time format string checking for libqtest.h

This is a reboot of "[PATCH 0/9] tests: Clean up around qmp() and hmp()" I sent about a year ago. Back then, Eric folded it into his "Clean up around qmp() and hmp()" series, which stalled after v4.

My reboot tries to avoid that fate by reducing mission creep. There are plenty of good ideas in Eric's series we should mine some day, but today is not that day.

v2:
* PATCH 01: Comments tweaked, ripple effect on PATCH 05+13
* PATCH 05: Commit message improved [Thomas]
* PATCH 06: Style fix [Philippe]
* PATCH 16+17: Silence a warning
* PATCH 21-23: New

Eric Blake (1):
libqtest: Document calling conventions

Markus Armbruster (22):
libqtest: Rename functions to send QMP messages
libqtest: Clean up how we read device_del messages
libqtest: Clean up how we read the QMP greeting
qobject: Replace qobject_from_jsonf() by qobject_from_jsonf_nofail()
qobject: New qobject_from_vjsonf_nofail(), qdict_from_vjsonf_nofail()
libqtest: Simplify qmp_fd_vsend() a bit test-qobject-input-visitor: Avoid format string ambiguity
qobject: qobject_from_jsonv() is dangerous, hide it away
tests: Pass literal format strings directly to qmp_FOO()
tests: Clean up string interpolation into QMP input (simple cases)
cpu-plug-test: Don't pass integers as strings to device_add
tests: Clean up string interpolation around qtest_qmp_device_add()
migration-test: Make wait_command() return the "return" member
tests: New helper qtest_qmp_receive_success()
migration-test: Make wait_command() cope with '%'
migration-test: Clean up string interpolation into QMP, part 1
migration-test: Clean up string interpolation into QMP, part 2
migration-test: Clean up string interpolation into QMP, part 3
libqtest: Enable compile-time format string checking
libqtest: Remove qtest_qmp_discard_response() & friends
libqtest: Replace qtest_startf() by qtest_initf()
libqtest: Rename qtest_FOOv() to qtest_vFOO() for consistency

 include/qapi/qmp/qjson.h           |  12 +-
 qobject/qjson.c                    |  89 ++++++++++----
 tests/ahci-test.c                  |  24 ++--
 tests/boot-order-test.c            |  12 +-
 tests/boot-serial-test.c           |  10 +-
 tests/cdrom-test.c                 |   6 +-
 tests/check-qjson.c                |  15 +--
 tests/cpu-plug-test.c              |   7 +-
 tests/drive_del-test.c             |   4 +-
 tests/e1000e-test.c                |   6 +-
 tests/endianness-test.c            |  24 ++--
 tests/fdc-test.c                   |   9 +-
 tests/ide-test.c                   |   6 +-
 tests/ipmi-bt-test.c               |   2 +-
 tests/ivshmem-test.c               |   8 +-
 tests/libqos/ahci.c                |   4 +-
 tests/libqos/pci-pc.c              |   9 +-
 tests/libqos/pci.c                 |   7 --
 tests/libqos/pci.h                 |   2 -
 tests/libqos/usb.c                 |  10 +-
 tests/libqos/usb.h                 |   2 +-
 tests/libqtest.c                   | 189 ++++++++++++++---------------
 tests/libqtest.h                   | 124 ++++++++++---------
 tests/m25p80-test.c                |   6 +-
 tests/m48t59-test.c                |   2 +-
 tests/machine-none-test.c          |   2 +-
 tests/migration-test.c             | 180 ++++++++++++---------------
 tests/numa-test.c                  |   4 +-
 tests/pnv-xscom-test.c             |   8 +-
 tests/prom-env-test.c              |  10 +-
 tests/qmp-test.c                   |  20 +--
 tests/sdhci-test.c                 |   6 +-
 tests/tco-test.c                   |   6 +-
 tests/test-filter-mirror.c         |   5 +-
 tests/test-filter-redirector.c     |   9 +-
 tests/test-qga.c                   | 150 ++++++++++-------------
 tests/test-qobject-input-visitor.c |  19 +--
 tests/tpm-util.c                   |  41 ++-----
 tests/usb-hcd-ehci-test.c          |   2 +-
 tests/usb-hcd-ohci-test.c          |   2 +-
 tests/usb-hcd-uhci-test.c          |   4 +-
 tests/usb-hcd-xhci-test.c          |  10 +-
 tests/vhost-user-test.c            |   6 +-
 tests/virtio-balloon-test.c        |   4 +-
 tests/virtio-blk-test.c            |  32 ++---
 tests/virtio-console-test.c        |  12 +-
 tests/virtio-net-test.c            |   3 +-
 tests/virtio-rng-test.c            |   3 +-
 tests/virtio-scsi-test.c           |   2 +-
 tests/virtio-serial-test.c         |   6 +-
 tests/vmgenid-test.c               |   6 +-
 51 files changed, 554 insertions(+), 587 deletions(-)
Signature · 1 line
2.17.1
#21 lore raw

PATCH v2 10/23 tests: Pass literal format strings directly to qmp_FOO()

The qmp_FOO() take a printf-like format string. In a few places, we assign a string literal to a variable and pass that instead of simply passing the literal. Clean that up.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---
 tests/ahci-test.c | 4 +---
 tests/ide-test.c  | 4 +---
 2 files changed, 2 insertions(+), 6 deletions(-)
tests/ahci-test.c +1 -3
index 7e3491b5bd..19a7ab1bca 100644
@@ -1352,7 +1352,6 @@ static void test_flush_migrate(void)
     AHCIQState *src, *dst;
     AHCICommand *cmd;
     uint8_t px;
-    const char *s;
     char *uri = g_strdup_printf("unix:%s", mig_socket);
 
     prepare_blkdebug_script(debug_path, "flush_to_disk");
@@ -1388,8 +1387,7 @@ static void test_flush_migrate(void)
     ahci_migrate(src, dst, uri);
 
     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_send(s);
+    qmp_send("{'execute':'cont' }");
     qmp_eventwait("RESUME");
     ahci_command_wait(dst, cmd);
     ahci_command_verify(dst, cmd);
tests/ide-test.c +1 -3
index 2384c2c3e2..834ee9005b 100644
@@ -694,7 +694,6 @@ static void test_retry_flush(const char *machine)
     QPCIDevice *dev;
     QPCIBar bmdma_bar, ide_bar;
     uint8_t data;
-    const char *s;
 
     prepare_blkdebug_script(debug_path, "flush_to_disk");
 
@@ -722,8 +721,7 @@ static void test_retry_flush(const char *machine)
     qmp_eventwait("STOP");
 
     /* Complete the command */
-    s = "{'execute':'cont' }";
-    qmp_discard_response(s);
+    qmp_discard_response("{'execute':'cont' }");
 
     /* Check registers */
     data = qpci_io_readb(dev, ide_bar, reg_device);
Signature · 1 line
2.17.1
#27 lore raw

PATCH v2 13/23 tests: Clean up string interpolation around qtest_qmp_device_add()

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

qtest_qmp_device_add() and its wrappers interpolate into JSON as follows:

* qtest_qmp_device_add() interpolates members into a JSON object.

* So do its wrappers qpci_plug_device_test() and usb_test_hotplug().

* usb_test_hotplug() additionally interpolates strings and numbers into JSON strings.

Clean them up:

* Have qtest_qmp_device_add() take its extra device properties as arguments for for qdict_from_jsonf_nofail() instead of a string containing JSON members.

* Drop qpci_plug_device_test(), use qtest_qmp_device_add() directly.

* Change usb_test_hotplug() parameter @port to string, to avoid interpolation. Interpolate @hcd_id separately.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---
 tests/cpu-plug-test.c      |  7 ++++---
 tests/e1000e-test.c        |  6 ++----
 tests/ivshmem-test.c       |  8 +++-----
 tests/libqos/pci.c         |  7 -------
 tests/libqos/pci.h         |  2 --
 tests/libqos/usb.c         | 10 ++++++----
 tests/libqos/usb.h         |  2 +-
 tests/libqtest.c           | 27 +++++++++++----------------
 tests/libqtest.h           |  4 +++-
 tests/usb-hcd-ehci-test.c  |  2 +-
 tests/usb-hcd-ohci-test.c  |  2 +-
 tests/usb-hcd-uhci-test.c  |  4 ++--
 tests/usb-hcd-xhci-test.c  | 10 +++++-----
 tests/virtio-blk-test.c    |  5 +++--
 tests/virtio-net-test.c    |  3 ++-
 tests/virtio-rng-test.c    |  3 ++-
 tests/virtio-scsi-test.c   |  2 +-
 tests/virtio-serial-test.c |  2 +-
 18 files changed, 48 insertions(+), 58 deletions(-)
tests/cpu-plug-test.c +4 -3
index ab3bf6df90..f5d57da60e 100644
@@ -88,8 +88,9 @@ static void test_plug_with_device_add_x86(gconstpointer data)
         for (c = 0; c < td->cores; c++) {
             for (t = 0; t < td->threads; t++) {
                 char *id = g_strdup_printf("id-%i-%i-%i", s, c, t);
-                qtest_qmp_device_add(td->device_model, id, "'socket-id':%u, "
-                                     "'core-id':%u, 'thread-id':%u",
+                qtest_qmp_device_add(td->device_model, id,
+                                     "{'socket-id':%u, 'core-id':%u,"
+                                     " 'thread-id':%u}",
                                      s, c, t);
                 g_free(id);
             }
@@ -114,7 +115,7 @@ static void test_plug_with_device_add_coreid(gconstpointer data)
 
     for (c = td->cores; c < td->maxcpus / td->sockets / td->threads; c++) {
         char *id = g_strdup_printf("id-%i", c);
-        qtest_qmp_device_add(td->device_model, id, "'core-id':%u", c);
+        qtest_qmp_device_add(td->device_model, id, "{'core-id':%u}", c);
         g_free(id);
     }
 
tests/e1000e-test.c +2 -4
index 32aa738b72..c9408a5d1f 100644
@@ -456,12 +456,10 @@ static void test_e1000e_multiple_transfers(gconstpointer data)
 
 static void test_e1000e_hotplug(gconstpointer data)
 {
-    static const uint8_t slot = 0x06;
-
     qtest_start("-device e1000e");
 
-    qpci_plug_device_test("e1000e", "e1000e_net", slot, NULL);
-    qpci_unplug_acpi_device_test("e1000e_net", slot);
+    qtest_qmp_device_add("e1000e", "e1000e_net", "{'addr': '0x06'}");
+    qpci_unplug_acpi_device_test("e1000e_net", 0x06);
 
     qtest_end();
 }
tests/ivshmem-test.c +3 -5
index 9b407a3e42..225fde7fff 100644
@@ -420,19 +420,17 @@ static void test_ivshmem_server_irq(void)
 static void test_ivshmem_hotplug(void)
 {
     const char *arch = qtest_get_arch();
-    gchar *opts;
 
     qtest_start("");
 
-    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qtest_qmp_device_add("ivshmem",
+                         "iv1", "{'addr': %s, 'shm': '%s', 'size': '1M'}",
+                         stringify(PCI_SLOT_HP), tmpshm);
     if (strcmp(arch, "ppc64") != 0) {
         qpci_unplug_acpi_device_test("iv1", PCI_SLOT_HP);
     }
 
     qtest_end();
-    g_free(opts);
 }
 
 static void test_ivshmem_memdev(void)
tests/libqos/pci.c +0 -7
index 0b73cb23d0..e8c342c257 100644
@@ -395,10 +395,3 @@ QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr)
     QPCIBar bar = { .addr = addr };
     return bar;
 }
-
-void qpci_plug_device_test(const char *driver, const char *id,
-                           uint8_t slot, const char *opts)
-{
-    qtest_qmp_device_add(driver, id, "'addr': '%d'%s%s", slot,
-                         opts ? ", " : "", opts ? opts : "");
-}
tests/libqos/pci.h +0 -2
index 429c382282..0b7e936174 100644
@@ -109,7 +109,5 @@ QPCIBar qpci_iomap(QPCIDevice *dev, int barno, uint64_t *sizeptr);
 void qpci_iounmap(QPCIDevice *dev, QPCIBar addr);
 QPCIBar qpci_legacy_iomap(QPCIDevice *dev, uint16_t addr);
 
-void qpci_plug_device_test(const char *driver, const char *id,
-                           uint8_t slot, const char *opts);
 void qpci_unplug_acpi_device_test(const char *id, uint8_t slot);
 #endif
tests/libqos/usb.c +6 -4
index 2a476049a8..49e2f4bc0a 100644
@@ -37,13 +37,14 @@ void uhci_port_test(struct qhc *hc, int port, uint16_t expect)
     g_assert((value & mask) == (expect & mask));
 }
 
-void usb_test_hotplug(const char *hcd_id, const int port,
+void usb_test_hotplug(const char *hcd_id, const char *port,
                       void (*port_check)(void))
 {
-    char  *id = g_strdup_printf("usbdev%d", port);
+    char *id = g_strdup_printf("usbdev%s", port);
+    char *bus = g_strdup_printf("%s.0", hcd_id);
 
-    qtest_qmp_device_add("usb-tablet", id, "'port': '%d', 'bus': '%s.0'",
-                         port, hcd_id);
+    qtest_qmp_device_add("usb-tablet", id, "{'port': %s, 'bus': %s}",
+                         port, bus);
 
     if (port_check) {
         port_check();
@@ -51,5 +52,6 @@ void usb_test_hotplug(const char *hcd_id, const int port,
 
     qtest_qmp_device_del(id);
 
+    g_free(bus);
     g_free(id);
 }
tests/libqos/usb.h +1 -1
index 297cfc564d..c506418a13 100644
@@ -13,6 +13,6 @@ void qusb_pci_init_one(QPCIBus *pcibus, struct qhc *hc,
 void uhci_port_test(struct qhc *hc, int port, uint16_t expect);
 void uhci_deinit(struct qhc *hc);
 
-void usb_test_hotplug(const char *bus_name, const int port,
+void usb_test_hotplug(const char *bus_name, const char *port,
                       void (*port_check)(void));
 #endif
tests/libqtest.c +11 -16
index e36cc5ede3..dfca3af89d 100644
@@ -1032,26 +1032,21 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
 /*
  * Generic hot-plugging test via the device_add QMP command.
  */
-void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
-                          ...)
+void qtest_qmp_device_add(const char *driver, const char *id,
+                          const char *fmt, ...)
 {
-    QDict *response;
-    char *cmd, *opts = NULL;
-    va_list va;
+    QDict *args, *response;
+    va_list ap;
 
-    if (fmt) {
-        va_start(va, fmt);
-        opts = g_strdup_vprintf(fmt, va);
-        va_end(va);
-    }
+    va_start(ap, fmt);
+    args = qdict_from_vjsonf_nofail(fmt, ap);
+    va_end(ap);
 
-    cmd = g_strdup_printf("{'execute': 'device_add',"
-                          " 'arguments': { 'driver': '%s', 'id': '%s'%s%s }}",
-                          driver, id, opts ? ", " : "", opts ? opts : "");
-    g_free(opts);
+    g_assert(!qdict_haskey(args, "driver") && !qdict_haskey(args, "id"));
+    qdict_put_str(args, "driver", driver);
+    qdict_put_str(args, "id", id);
 
-    response = qmp(cmd);
-    g_free(cmd);
+    response = qmp("{'execute': 'device_add', 'arguments': %p}", args);
     g_assert(response);
     g_assert(!qdict_haskey(response, "event")); /* We don't expect any events */
     g_assert(!qdict_haskey(response, "error"));
tests/libqtest.h +3 -1
index a67e5e34eb..ce6c092fc9 100644
@@ -976,7 +976,9 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine));
  * qtest_qmp_device_add:
  * @driver: Name of the device that should be added
  * @id: Identification string
- * @fmt: printf-like format string for further options to device_add
+ * @fmt...: QMP message to send to qemu, formatted like
+ * qobject_from_jsonf_nofail().
+ * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Generic hot-plugging test via the device_add QMP command.
  */
tests/usb-hcd-ehci-test.c +1 -1
index 55d4743a2a..f28ea27f37 100644
@@ -139,7 +139,7 @@ static void pci_ehci_port_3_hotplug(void)
 
 static void pci_ehci_port_hotplug(void)
 {
-    usb_test_hotplug("ich9-ehci-1", 3, pci_ehci_port_3_hotplug);
+    usb_test_hotplug("ich9-ehci-1", "3", pci_ehci_port_3_hotplug);
 }
 
 
tests/usb-hcd-ohci-test.c +1 -1
index 4758813d78..48ddbfd26d 100644
@@ -19,7 +19,7 @@ static void test_ohci_init(void)
 
 static void test_ohci_hotplug(void)
 {
-    usb_test_hotplug("ohci", 1, NULL);
+    usb_test_hotplug("ohci", "1", NULL);
 }
 
 int main(int argc, char **argv)
tests/usb-hcd-uhci-test.c +2 -2
index 6a7e5a2fed..a119d6d5c8 100644
@@ -43,12 +43,12 @@ static void test_port_2(void)
 
 static void test_uhci_hotplug(void)
 {
-    usb_test_hotplug("uhci", 2, test_port_2);
+    usb_test_hotplug("uhci", "2", test_port_2);
 }
 
 static void test_usb_storage_hotplug(void)
 {
-    qtest_qmp_device_add("usb-storage", "usbdev0", "'drive': 'drive0'");
+    qtest_qmp_device_add("usb-storage", "usbdev0", "{'drive': 'drive0'}");
 
     qtest_qmp_device_del("usbdev0");
 }
tests/usb-hcd-xhci-test.c +5 -5
index 5b1b681bf2..9eb24b00e4 100644
@@ -18,13 +18,13 @@ static void test_xhci_init(void)
 
 static void test_xhci_hotplug(void)
 {
-    usb_test_hotplug("xhci", 1, NULL);
+    usb_test_hotplug("xhci", "1", NULL);
 }
 
 static void test_usb_uas_hotplug(void)
 {
-    qtest_qmp_device_add("usb-uas", "uas", NULL);
-    qtest_qmp_device_add("scsi-hd", "scsihd", "'drive': 'drive0'");
+    qtest_qmp_device_add("usb-uas", "uas", "{}");
+    qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drive0'}");
 
     /* TODO:
         UAS HBA driver in libqos, to check that
@@ -37,10 +37,10 @@ static void test_usb_uas_hotplug(void)
 
 static void test_usb_ccid_hotplug(void)
 {
-    qtest_qmp_device_add("usb-ccid", "ccid", NULL);
+    qtest_qmp_device_add("usb-ccid", "ccid", "{}");
     qtest_qmp_device_del("ccid");
     /* check the device can be added again */
-    qtest_qmp_device_add("usb-ccid", "ccid", NULL);
+    qtest_qmp_device_add("usb-ccid", "ccid", "{}");
     qtest_qmp_device_del("ccid");
 }
 
tests/virtio-blk-test.c +3 -2
index 9be9ffb378..d96b4c69e1 100644
@@ -662,8 +662,9 @@ static void pci_hotplug(void)
     qs = pci_test_start();
 
     /* plug secondary disk */
-    qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
-                          "'drive': 'drive1'");
+    qtest_qmp_device_add("virtio-blk-pci", "drv1",
+                         "{'addr': %s, 'drive': 'drive1'}",
+                         stringify(PCI_SLOT_HP));
 
     dev = virtio_blk_pci_init(qs->pcibus, PCI_SLOT_HP);
     g_assert(dev);
tests/virtio-net-test.c +2 -1
index b285a262e9..dcb87a8b6e 100644
@@ -249,7 +249,8 @@ static void hotplug(void)
 
     qtest_start("-device virtio-net-pci");
 
-    qpci_plug_device_test("virtio-net-pci", "net1", PCI_SLOT_HP, NULL);
+    qtest_qmp_device_add("virtio-net-pci", "net1",
+                         "{'addr': %s}", stringify(PCI_SLOT_HP));
 
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         qpci_unplug_acpi_device_test("net1", PCI_SLOT_HP);
tests/virtio-rng-test.c +2 -1
index dcecf77463..657d9a4105 100644
@@ -22,7 +22,8 @@ static void hotplug(void)
 {
     const char *arch = qtest_get_arch();
 
-    qpci_plug_device_test("virtio-rng-pci", "rng1", PCI_SLOT_HP, NULL);
+    qtest_qmp_device_add("virtio-rng-pci", "rng1",
+                         "{'addr': %s}", stringify(PCI_SLOT_HP));
 
     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
         qpci_unplug_acpi_device_test("rng1", PCI_SLOT_HP);
tests/virtio-scsi-test.c +1 -1
index 037872bb98..0d4f25d369 100644
@@ -199,7 +199,7 @@ static void hotplug(void)
 
     qs = qvirtio_scsi_start(
             "-drive id=drv1,if=none,file=null-co://,format=raw");
-    qtest_qmp_device_add("scsi-hd", "scsihd", "'drive': 'drv1'");
+    qtest_qmp_device_add("scsi-hd", "scsihd", "{'drive': 'drv1'}");
     qtest_qmp_device_del("scsihd");
     qvirtio_scsi_stop(qs);
 }
tests/virtio-serial-test.c +1 -1
index 7cc7060264..e4b18b1c8a 100644
@@ -18,7 +18,7 @@ static void virtio_serial_nop(void)
 
 static void hotplug(void)
 {
-    qtest_qmp_device_add("virtserialport", "hp-port", NULL);
+    qtest_qmp_device_add("virtserialport", "hp-port", "{}");
 
     qtest_qmp_device_del("hp-port");
 }
Signature · 1 line
2.17.1
#42 lore raw

PATCH v2 18/23 migration-test: Clean up string interpolation into QMP, part 2

In reply to Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migrate() interpolates members into a JSON object. Change it to take its extra QMP arguments as arguments for qdict_from_jsonf_nofail() instead of a string containing JSON members.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
 tests/migration-test.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)
tests/migration-test.c +16 -12
index 323bb60535..1c1e56b15b 100644
@@ -14,6 +14,7 @@
 
 #include "libqtest.h"
 #include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qjson.h"
 #include "qemu/option.h"
 #include "qemu/range.h"
 #include "qemu/sockets.h"
@@ -381,16 +382,19 @@ static void migrate_set_capability(QTestState *who, const char *capability,
     qobject_unref(rsp);
 }
 
-static void migrate(QTestState *who, const char *uri, const char *extra)
+static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
 {
-    QDict *rsp;
-    gchar *cmd;
+    va_list ap;
+    QDict *args, *rsp;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "  'arguments': { 'uri': '%s' %s } }",
-                          uri, extra ? extra : "");
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    va_start(ap, fmt);
+    args = qdict_from_vjsonf_nofail(fmt, ap);
+    va_end(ap);
+
+    g_assert(!qdict_haskey(args, "uri"));
+    qdict_put_str(args, "uri", uri);
+
+    rsp = qmp("{ 'execute': 'migrate', 'arguments': %p}", args);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 }
@@ -582,7 +586,7 @@ static int migrate_postcopy_prepare(QTestState **from_ptr,
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");
 
-    migrate(from, uri, NULL);
+    migrate(from, uri, "{}");
     g_free(uri);
 
     wait_for_migration_pass(from);
@@ -665,7 +669,7 @@ static void test_postcopy_recovery(void)
      * the newly created channel
      */
     wait_for_migration_status(from, "postcopy-paused");
-    migrate(from, uri, ", 'resume': true");
+    migrate(from, uri, "{'resume': true}");
     g_free(uri);
 
     /* Restore the postcopy bandwidth to unlimited */
@@ -684,7 +688,7 @@ static void test_baddest(void)
     if (test_migrate_start(&from, &to, "tcp:0:0", true)) {
         return;
     }
-    migrate(from, "tcp:0:0", NULL);
+    migrate(from, "tcp:0:0", "{}");
     do {
         status = migrate_query_status(from);
         g_assert(!strcmp(status, "setup") || !(strcmp(status, "failed")));
@@ -722,7 +726,7 @@ static void test_precopy_unix(void)
     /* Wait for the first serial output from the source */
     wait_for_serial("src_serial");
 
-    migrate(from, uri, NULL);
+    migrate(from, uri, "{}");
 
     wait_for_migration_pass(from);
 
Signature · 1 line
2.17.1
#23 lore raw

PATCH v2 11/23 tests: Clean up string interpolation into QMP input (simple cases)

In reply to Markus Armbruster

When you build QMP input manually like this

    cmd = g_strdup_printf("{ 'execute': 'migrate',"
                          "'arguments': { 'uri': '%s' } }",
                          uri);
    rsp = 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_vjsonf_nofail() would abort. A sufficiently nasty @uri could even inject unwanted members into the arguments object.

Leaving interpolation into JSON to qmp() is more robust:

    rsp = 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.

---
 tests/libqos/pci-pc.c   |   9 +--
 tests/libqtest.c        |   7 +-
 tests/migration-test.c  |   8 +--
 tests/test-qga.c        | 150 ++++++++++++++++++----------------------
 tests/tpm-util.c        |   9 +--
 tests/vhost-user-test.c |   6 +-
 6 files changed, 77 insertions(+), 112 deletions(-)
tests/libqos/pci-pc.c +2 -7
index a7803308b7..585f5289ec 100644
@@ -160,14 +160,9 @@ void qpci_free_pc(QPCIBus *bus)
 void qpci_unplug_acpi_device_test(const char *id, uint8_t slot)
 {
     QDict *response;
-    char *cmd;
 
-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                          " 'arguments': {"
-                          "   'id': '%s'"
-                          "}}", id);
-    response = qmp(cmd);
-    g_free(cmd);
+    response = qmp("{'execute': 'device_del', 'arguments': {'id': %s}}",
+                   id);
     g_assert(response);
     g_assert(!qdict_haskey(response, "error"));
     qobject_unref(response);
tests/libqtest.c +2 -5
index 3bfb062fcb..e36cc5ede3 100644
@@ -1079,12 +1079,9 @@ void qtest_qmp_device_add(const char *driver, const char *id, const char *fmt,
 void qtest_qmp_device_del(const char *id)
 {
     QDict *response1, *response2, *event = NULL;
-    char *cmd;
 
-    cmd = g_strdup_printf("{'execute': 'device_del',"
-                          " 'arguments': { 'id': '%s' }}", id);
-    response1 = qmp(cmd);
-    g_free(cmd);
+    response1 = qmp("{'execute': 'device_del', 'arguments': {'id': %s}}",
+                    id);
     g_assert(response1);
     g_assert(!qdict_haskey(response1, "error"));
 
tests/migration-test.c +3 -5
index e079e0bdb6..c6c7089ce1 100644
@@ -529,14 +529,12 @@ static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest)
 static void deprecated_set_downtime(QTestState *who, const double value)
 {
     QDict *rsp;
-    gchar *cmd;
     char *expected;
     int64_t result_int;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
-                          "'arguments': { 'value': %g } }", value);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who,
+                    "{ 'execute': 'migrate_set_downtime',"
+                    " 'arguments': { 'value': %f } }", value);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
     result_int = value * 1000L;
tests/test-qga.c +66 -84

298 lines not on this page · raw message

tests/tpm-util.c +3 -6
index 672cedf905..3bd2887f1e 100644
@@ -239,13 +239,10 @@ void tpm_util_swtpm_kill(GPid pid)
 void tpm_util_migrate(QTestState *who, const char *uri)
 {
     QDict *rsp;
-    gchar *cmd;
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who,
+                    "{ 'execute': 'migrate', 'arguments': { 'uri': %s } }",
+                    uri);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 }
tests/vhost-user-test.c +1 -5
index fecc832d99..ca6251f5f8 100644
@@ -709,11 +709,7 @@ static void test_migrate(void)
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 
-    cmd = g_strdup_printf("{ 'execute': 'migrate',"
-                          "'arguments': { 'uri': '%s' } }",
-                          uri);
-    rsp = qmp(cmd);
-    g_free(cmd);
+    rsp = qmp("{ 'execute': 'migrate', 'arguments': { 'uri': %s } }", uri);
     g_assert(qdict_haskey(rsp, "return"));
     qobject_unref(rsp);
 
Signature · 1 line
2.17.1
#9 lore raw

PATCH v2 4/23 libqtest: Clean up how we read the QMP greeting

In reply to Markus Armbruster

qtest_init() still uses the qtest_qmp_discard_response(s, "") hack to receive the greeting, even though we have qtest_qmp_receive() since commit 66e0c7b187e. Put it to use.

Bonus: gets rid of an empty format string. A step towards compile-time format string checking without triggering -Wformat-zero-length.

---
 tests/libqtest.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tests/libqtest.c +3 -1
index 071d7eb7b1..c2c08a890c 100644
@@ -249,9 +249,11 @@ QTestState *qtest_init_without_qmp_handshake(bool use_oob,
 QTestState *qtest_init(const char *extra_args)
 {
     QTestState *s = qtest_init_without_qmp_handshake(false, extra_args);
+    QDict *greeting;
 
     /* Read the QMP greeting and then do the handshake */
-    qtest_qmp_discard_response(s, "");
+    greeting = qtest_qmp_receive(s);
+    qobject_unref(greeting);
     qtest_qmp_discard_response(s, "{ 'execute': 'qmp_capabilities' }");
 
     return s;
Signature · 1 line
2.17.1
#15 lore raw

PATCH v2 7/23 libqtest: Simplify qmp_fd_vsend() a bit

In reply to Markus Armbruster
---
 tests/libqtest.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)
tests/libqtest.c +7 -11
index c2c08a890c..3bfb062fcb 100644
@@ -488,24 +488,20 @@ QDict *qtest_qmp_receive(QTestState *s)
  */
 void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
 {
-    va_list ap_copy;
     QObject *qobj;
 
-    /* qobject_from_jsonv() silently eats leading 0xff as invalid
-     * JSON, but we want to test sending them over the wire to force
-     * resyncs */
+    /*
+     * qobject_from_vjsonf_nofail() chokes on leading 0xff as invalid
+     * JSON, but tests/test-qga.c needs to send that to test QGA
+     * synchronization
+     */
     if (*fmt == '\377') {
         socket_send(fd, fmt, 1);
         fmt++;
     }
 
-    /* Going through qobject ensures we escape strings properly.
-     * This seemingly unnecessary copy is required in case va_list
-     * is an array type.
-     */
-    va_copy(ap_copy, ap);
-    qobj = qobject_from_jsonv(fmt, &ap_copy, &error_abort);
-    va_end(ap_copy);
+    /* Going through qobject ensures we escape strings properly */
+    qobj = qobject_from_vjsonf_nofail(fmt, ap);
 
     /* No need to send anything for an empty QObject.  */
     if (qobj) {
Signature · 1 line
2.17.1
#2 lore raw

PATCH v2 2/23 libqtest: Rename functions to send QMP messages

In reply to Markus Armbruster

The functions to receive messages are called qtest_qmp_receive() and qmp_receive(), qmp_fd_receive(). The ones to send messages are called qtest_async_qmp(), qtest_async_qmpv(), qmp_async(), qmp_fd_send(), qmp_fd_sendv(). Inconsistent. Rename the *_async* ones to qmp_send(), qtest_qmp_send(), qtest_qmp_vsend(). Rename qmp_fd_sendv() to qmp_fd_vsend().

---
 tests/ahci-test.c   | 10 +++++-----
 tests/libqos/ahci.c |  4 ++--
 tests/libqtest.c    | 20 ++++++++++----------
 tests/libqtest.h    | 14 +++++++-------
 tests/qmp-test.c    | 18 +++++++++---------
 5 files changed, 33 insertions(+), 33 deletions(-)
tests/ahci-test.c +5 -5
index 1a7b761304..7e3491b5bd 100644
@@ -1389,7 +1389,7 @@ static void test_flush_migrate(void)
 
     /* Complete the command */
     s = "{'execute':'cont' }";
-    qmp_async(s);
+    qmp_send(s);
     qmp_eventwait("RESUME");
     ahci_command_wait(dst, cmd);
     ahci_command_verify(dst, cmd);
@@ -1592,8 +1592,8 @@ static void test_atapi_tray(void)
     atapi_wait_tray(false);
 
     /* Remove media */
-    qmp_async("{'execute': 'blockdev-open-tray', "
-               "'arguments': {'id': 'cd0'}}");
+    qmp_send("{'execute': 'blockdev-open-tray',"
+             " 'arguments': {'id': 'cd0'}}");
     atapi_wait_tray(true);
     rsp = qmp_receive();
     qobject_unref(rsp);
@@ -1619,8 +1619,8 @@ static void test_atapi_tray(void)
                                          "'node-name': 'node0' }}");
 
     /* Again, the event shows up first */
-    qmp_async("{'execute': 'blockdev-close-tray', "
-               "'arguments': {'id': 'cd0'}}");
+    qmp_send("{'execute': 'blockdev-close-tray',"
+             " 'arguments': {'id': 'cd0'}}");
     atapi_wait_tray(false);
     rsp = qmp_receive();
     qobject_unref(rsp);
tests/libqos/ahci.c +2 -2
index 42d3f76933..63fbc9e3c9 100644
@@ -674,7 +674,7 @@ void ahci_exec(AHCIQState *ahci, uint8_t port,
         g_assert_cmpint(rc, ==, 0);
     }
     if (opts->error) {
-        qtest_async_qmp(ahci->parent->qts, "{'execute':'cont' }");
+        qtest_qmp_send(ahci->parent->qts, "{'execute':'cont' }");
         qtest_qmp_eventwait(ahci->parent->qts, "RESUME");
     }
 
@@ -712,7 +712,7 @@ AHCICommand *ahci_guest_io_halt(AHCIQState *ahci, uint8_t port,
 void ahci_guest_io_resume(AHCIQState *ahci, AHCICommand *cmd)
 {
     /* Complete the command */
-    qtest_async_qmp(ahci->parent->qts, "{'execute':'cont' }");
+    qtest_qmp_send(ahci->parent->qts, "{'execute':'cont' }");
     qtest_qmp_eventwait(ahci->parent->qts, "RESUME");
     ahci_command_wait(ahci, cmd);
     ahci_command_verify(ahci, cmd);
tests/libqtest.c +10 -10
index 098af6aec4..9cb4096639 100644
@@ -484,7 +484,7 @@ QDict *qtest_qmp_receive(QTestState *s)
  * in the case that they choose to discard all replies up until
  * a particular EVENT is received.
  */
-void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
+void qmp_fd_vsend(int fd, const char *fmt, va_list ap)
 {
     va_list ap_copy;
     QObject *qobj;
@@ -529,21 +529,21 @@ void qmp_fd_sendv(int fd, const char *fmt, va_list ap)
     }
 }
 
-void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap)
+void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap)
 {
-    qmp_fd_sendv(s->qmp_fd, fmt, ap);
+    qmp_fd_vsend(s->qmp_fd, fmt, ap);
 }
 
 QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
 {
-    qmp_fd_sendv(fd, fmt, ap);
+    qmp_fd_vsend(fd, fmt, ap);
 
     return qmp_fd_receive(fd);
 }
 
 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
 {
-    qtest_async_qmpv(s, fmt, ap);
+    qtest_qmp_vsend(s, fmt, ap);
 
     /* Receive reply */
     return qtest_qmp_receive(s);
@@ -565,7 +565,7 @@ void qmp_fd_send(int fd, const char *fmt, ...)
     va_list ap;
 
     va_start(ap, fmt);
-    qmp_fd_sendv(fd, fmt, ap);
+    qmp_fd_vsend(fd, fmt, ap);
     va_end(ap);
 }
 
@@ -580,12 +580,12 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...)
     return response;
 }
 
-void qtest_async_qmp(QTestState *s, const char *fmt, ...)
+void qtest_qmp_send(QTestState *s, const char *fmt, ...)
 {
     va_list ap;
 
     va_start(ap, fmt);
-    qtest_async_qmpv(s, fmt, ap);
+    qtest_qmp_vsend(s, fmt, ap);
     va_end(ap);
 }
 
@@ -968,12 +968,12 @@ QDict *qmp(const char *fmt, ...)
     return response;
 }
 
-void qmp_async(const char *fmt, ...)
+void qmp_send(const char *fmt, ...)
 {
     va_list ap;
 
     va_start(ap, fmt);
-    qtest_async_qmpv(global_qtest, fmt, ap);
+    qtest_qmp_vsend(global_qtest, fmt, ap);
     va_end(ap);
 }
 
tests/libqtest.h +7 -7
index aa2b2cd7c1..ced95f490a 100644
@@ -96,7 +96,7 @@ void qtest_qmp_discard_response(QTestState *s, const char *fmt, ...);
 QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
 
 /**
- * qtest_async_qmp:
+ * qtest_qmp_send:
  * @s: #QTestState instance to operate on.
  * @fmt...: QMP message to send to qemu, formatted like
  * qobject_from_jsonf().
@@ -104,7 +104,7 @@ QDict *qtest_qmp(QTestState *s, const char *fmt, ...);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_async_qmp(QTestState *s, const char *fmt, ...);
+void qtest_qmp_send(QTestState *s, const char *fmt, ...);
 
 /**
  * qtest_qmpv_discard_response:
@@ -131,7 +131,7 @@ void qtest_qmpv_discard_response(QTestState *s, const char *fmt, va_list ap);
 QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
 
 /**
- * qtest_async_qmpv:
+ * qtest_qmp_vsend:
  * @s: #QTestState instance to operate on.
  * @fmt: QMP message to send to QEMU, formatted like
  * qobject_from_jsonf().
@@ -140,7 +140,7 @@ QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap);
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qtest_async_qmpv(QTestState *s, const char *fmt, va_list ap);
+void qtest_qmp_vsend(QTestState *s, const char *fmt, va_list ap);
 
 /**
  * qtest_receive:
@@ -583,14 +583,14 @@ static inline void qtest_end(void)
 QDict *qmp(const char *fmt, ...);
 
 /**
- * qmp_async:
+ * qmp_send:
  * @fmt...: QMP message to send to qemu, formatted like
  * qobject_from_jsonf().
  * Only understands '%((l|ll|I64)?d|[ipsf])', see parse_escape().
  *
  * Sends a QMP message to QEMU and leaves the response in the stream.
  */
-void qmp_async(const char *fmt, ...);
+void qmp_send(const char *fmt, ...);
 
 /**
  * qmp_discard_response:
@@ -959,7 +959,7 @@ static inline int64_t clock_set(int64_t val)
 }
 
 QDict *qmp_fd_receive(int fd);
-void qmp_fd_sendv(int fd, const char *fmt, va_list ap);
+void qmp_fd_vsend(int fd, const char *fmt, va_list ap);
 void qmp_fd_send(int fd, const char *fmt, ...);
 QDict *qmp_fdv(int fd, const char *fmt, va_list ap);
 QDict *qmp_fd(int fd, const char *fmt, ...);
tests/qmp-test.c +9 -9
index b9774084f8..5eb15daebc 100644
@@ -159,12 +159,12 @@ static void cleanup_blocking_cmd(void)
 
 static void send_cmd_that_blocks(QTestState *s, const char *id)
 {
-    qtest_async_qmp(s, "{ 'execute': 'blockdev-add',  'id': %s,"
-                    " 'arguments': {"
-                    " 'driver': 'blkdebug', 'node-name': %s,"
-                    " 'config': %s,"
-                    " 'image': { 'driver': 'null-co' } } }",
-                    id, id, fifo_name);
+    qtest_qmp_send(s, "{ 'execute': 'blockdev-add',  'id': %s,"
+                   " 'arguments': {"
+                   " 'driver': 'blkdebug', 'node-name': %s,"
+                   " 'config': %s,"
+                   " 'image': { 'driver': 'null-co' } } }",
+                   id, id, fifo_name);
 }
 
 static void unblock_blocked_cmd(void)
@@ -176,7 +176,7 @@ static void unblock_blocked_cmd(void)
 
 static void send_oob_cmd_that_fails(QTestState *s, const char *id)
 {
-    qtest_async_qmp(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
+    qtest_qmp_send(s, "{ 'exec-oob': 'migrate-pause', 'id': %s }", id);
 }
 
 static void recv_cmd_id(QTestState *s, const char *id)
@@ -235,7 +235,7 @@ static void test_qmp_oob(void)
     /* OOB command overtakes slow in-band command */
     setup_blocking_cmd();
     send_cmd_that_blocks(qts, "ib-blocks-1");
-    qtest_async_qmp(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
+    qtest_qmp_send(qts, "{ 'execute': 'query-name', 'id': 'ib-quick-1' }");
     send_oob_cmd_that_fails(qts, "oob-1");
     recv_cmd_id(qts, "oob-1");
     unblock_blocked_cmd();
@@ -244,7 +244,7 @@ static void test_qmp_oob(void)
 
     /* Even malformed in-band command fails in-band */
     send_cmd_that_blocks(qts, "blocks-2");
-    qtest_async_qmp(qts, "{ 'id': 'err-2' }");
+    qtest_qmp_send(qts, "{ 'id': 'err-2' }");
     unblock_blocked_cmd();
     recv_cmd_id(qts, "blocks-2");
     recv_cmd_id(qts, "err-2");
Signature · 1 line
2.17.1
Reviewed-by on v2 2/23
#3 lore raw

Re: [Qemu-devel] [PATCH v2 02/23] libqtest: Rename functions to send QMP messages

Quoted 10 lines · Markus Armbruster

The functions to receive messages are called qtest_qmp_receive() and qmp_receive(), qmp_fd_receive(). The ones to send messages are called qtest_async_qmp(), qtest_async_qmpv(), qmp_async(), qmp_fd_send(), qmp_fd_sendv(). Inconsistent. Rename the *_async* ones to qmp_send(), qtest_qmp_send(), qtest_qmp_vsend(). Rename qmp_fd_sendv() to qmp_fd_vsend().

---

tests/ahci-test.c static void test_atapi_tray(void) · line 1596

Quoted 8 lines
tests/ahci-test.c +2 -2
@@ -1592,8 +1592,8 @@ static void test_atapi_tray(void)
      atapi_wait_tray(false);
 
      /* Remove media */
-    qmp_async("{'execute': 'blockdev-open-tray', "
-               "'arguments': {'id': 'cd0'}}");
+    qmp_send("{'execute': 'blockdev-open-tray',"
+             " 'arguments': {'id': 'cd0'}}");

Could perhaps fit in one line now, but I won't insist.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 3/23
#8 lore raw

Re: [Qemu-devel] [PATCH v2 03/23] libqtest: Clean up how we read device_del messages

In reply to Markus Armbruster
Quoted 7 lines · Markus Armbruster

qtest_qmp_device_del() still uses the qmp("") hack to receive a message, even though we have qmp_receive() since commit 66e0c7b187e. Put it to use.

Bonus: gets rid of empty format strings. A step towards compile-time format string checking without triggering -Wformat-zero-length.

Quoted 6 lines
---
  tests/libqtest.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 4/23
#10 lore raw

Re: [Qemu-devel] [PATCH v2 04/23] libqtest: Clean up how we read the QMP greeting

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

qtest_init() still uses the qtest_qmp_discard_response(s, "") hack to receive the greeting, even though we have qtest_qmp_receive() since commit 66e0c7b187e. Put it to use.

Bonus: gets rid of an empty format string. A step towards compile-time format string checking without triggering -Wformat-zero-length.

Quoted 6 lines
---
  tests/libqtest.c | 4 +++-
  1 file changed, 3 insertions(+), 1 deletion(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 5/23
#12 lore raw

Re: [Qemu-devel] [PATCH v2 05/23] qobject: Replace qobject_from_jsonf() by qobject_from_jsonf_nofail()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

Commit ab45015a968 "qobject: Let qobject_from_jsonf() fail instead of abort" fails to accomplish its stated aim: the function can still abort due to its use of &error_abort.

Its rationale for letting it fail is that all remaining users cope fine with failure. Well, they're just fine with aborting, too; it's what they do on failure.

Quoted 8 lines

Simply reverting the broken commit would bring back the unfortunate asymmetry between qobject_from_jsonf() and qobject_from_jsonv(): one aborts, the other returns null. So also rename it to qobject_from_jsonf_nofail().

---

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 6/23
#14 lore raw

Re: [Qemu-devel] [PATCH v2 06/23] qobject: New qobject_from_vjsonf_nofail(), qdict_from_vjsonf_nofail()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

Every printf()-like function sooner or later needs its vprintf()-like buddy. The next commit will need qobject_from_jsonf_nofail()'s buddy, and qdict_from_jsonf_nofail()'s buddy will be used later in this series. Add both.

---

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 7/23
#16 lore raw

Re: [Qemu-devel] [PATCH v2 07/23] libqtest: Simplify qmp_fd_vsend() a bit

In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster
---
  tests/libqtest.c | 18 +++++++-----------
  1 file changed, 7 insertions(+), 11 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 8/23
#18 lore raw

Re: [Qemu-devel] [PATCH v2 08/23] test-qobject-input-visitor: Avoid format string ambiguity

In reply to Markus Armbruster
Quoted 4 lines · Markus Armbruster

When visitor_input_test_init_internal()'s argument @ap is null, then @json_string is interpreted literally, else it's gets %-escapes interpolated. This is awkward.

Quoted 9 lines

One caller always passes null @ap, and the others never do. Lift the building of the QObject into the callers, where it can be done without such ambiguity.

---
  tests/test-qobject-input-visitor.c | 19 ++++++++++---------
  1 file changed, 10 insertions(+), 9 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 9/23
#20 lore raw

Re: [Qemu-devel] [PATCH v2 09/23] qobject: qobject_from_jsonv() is dangerous, hide it away

In reply to Markus Armbruster
Quoted 6 lines · Markus Armbruster

qobject_from_jsonv() takes ownership of %p arguments. On failure, we can't generally know whether we failed before or after %p, so ownership becomes indeterminate. To avoid leaks, callers passing %p must terminate on error, e.g. by passing &error_abort. Trap for the unwary; document and give the function internal linkage.

Quoted 7 lines
---
  include/qapi/qmp/qjson.h |  2 --
  qobject/qjson.c          | 13 ++++++++++++-
  2 files changed, 12 insertions(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 10/23
#22 lore raw

Re: [Qemu-devel] [PATCH v2 10/23] tests: Pass literal format strings directly to qmp_FOO()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

The qmp_FOO() take a printf-like format string. In a few places, we assign a string literal to a variable and pass that instead of simply passing the literal. Clean that up.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Quoted 7 lines
---
  tests/ahci-test.c | 4 +---
  tests/ide-test.c  | 4 +---
  2 files changed, 2 insertions(+), 6 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 11/23
#24 lore raw

Re: [Qemu-devel] [PATCH v2 11/23] tests: Clean up string interpolation into QMP input (simple cases)

In reply to Markus Armbruster
Quoted 31 lines · Markus Armbruster

When you build QMP input manually like this

     cmd = g_strdup_printf("{ 'execute': 'migrate',"
                           "'arguments': { 'uri': '%s' } }",
                           uri);
     rsp = 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_vjsonf_nofail() would abort. A sufficiently nasty @uri could even inject unwanted members into the arguments object.

Leaving interpolation into JSON to qmp() is more robust:

     rsp = 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.

---

tests/migration-test.c static void test_migrate_end(QTestState *from, QTestState *to, bool test_dest) · line 537

Quoted 7 lines
tests/migration-test.c +3 -4
-    cmd = g_strdup_printf("{ 'execute': 'migrate_set_downtime',"
-                          "'arguments': { 'value': %g } }", value);
-    rsp = qtest_qmp(who, cmd);
-    g_free(cmd);
+    rsp = qtest_qmp(who,
+                    "{ 'execute': 'migrate_set_downtime',"
+                    " 'arguments': { 'value': %f } }", value);

Looks a bit awkward changing %g to %f, but matches the limited subset that our parser accepts and doesn't change the accuracy needed for the test to pass.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 12/23
#26 lore raw

Re: [Qemu-devel] [PATCH v2 12/23] cpu-plug-test: Don't pass integers as strings to device_add

In reply to Markus Armbruster
Quoted 12 lines · Markus Armbruster

test_plug_with_device_add_x86() plugs Haswell-i386-cpu and Haswell-x86_64-cpu with device_add. It passes socket-id, core-id, thread-id as JSON strings. The properties are actually integers.

test_plug_with_device_add_coreid() plugs power8_v2.0-spapr-cpu-core and qemu-s390x-cpu with device_add. It passes core-id as JSON string. The properties are actually integers.

Passing JSON string values to integer properties works only due to device_add implementation accidents. Fix the test to pass JSON numbers. While there, use %u rather than %i with unsigned int.

Quoted 8 lines
---
  tests/cpu-plug-test.c | 6 +++---
  1 file changed, 3 insertions(+), 3 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 13/23
#28 lore raw

Re: [Qemu-devel] [PATCH v2 13/23] tests: Clean up string interpolation around qtest_qmp_device_add()

In reply to Markus Armbruster
Quoted 10 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

qtest_qmp_device_add() and its wrappers interpolate into JSON as follows:

* qtest_qmp_device_add() interpolates members into a JSON object.

* So do its wrappers qpci_plug_device_test() and usb_test_hotplug().

Quoted 7 lines

* usb_test_hotplug() additionally interpolates strings and numbers into JSON strings.

Clean them up:

* Have qtest_qmp_device_add() take its extra device properties as arguments for for qdict_from_jsonf_nofail() instead of a string

s/for for/for/

Quoted 20 lines

containing JSON members.

* Drop qpci_plug_device_test(), use qtest_qmp_device_add() directly.

* Change usb_test_hotplug() parameter @port to string, to avoid interpolation. Interpolate @hcd_id separately.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---

+++ b/tests/ivshmem-test.c

tests/ivshmem-test.c +0 -0
@@ -420,19 +420,17 @@ static void test_ivshmem_server_irq(void)
  static void test_ivshmem_hotplug(void)
  {

tests/ivshmem-test.c static void test_ivshmem_server_irq(void) · line 427

Quoted 10 lines
tests/ivshmem-test.c +2 -4
      const char *arch = qtest_get_arch();
-    gchar *opts;
 
      qtest_start("");
 
-    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qtest_qmp_device_add("ivshmem",
+                         "iv1", "{'addr': %s, 'shm': '%s', 'size': '1M'}",

Umm, how does this still pass? You want 'shm':%s, not 'shm':'%s'.

(We really want to assert that any % interpolations in our JSON parser are NOT embedded in '').

With that error fixed,

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 14/23
#34 lore raw

Re: [Qemu-devel] [PATCH v2 14/23] migration-test: Make wait_command() return the "return" member

In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

All callers of wait_command() are only interested in the success response's "return" member. Lift its extraction into wait_command().

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 38 +++++++++++++++-----------------------
  1 file changed, 15 insertions(+), 23 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 15/23
#36 lore raw

Re: [Qemu-devel] [PATCH v2 15/23] tests: New helper qtest_qmp_receive_success()

In reply to Markus Armbruster
Quoted 8 lines · Markus Armbruster

Commit b21373d0713 copied wait_command() from tests/migration-test.c to tests/tpm-util.c. Replace both copies by new libqtest helper qtest_qmp_receive_success(). Also use it to simplify qtest_qmp_device_del().

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Where? [1]

Quoted 12 lines
Cc Thomas Huth <[email protected]>
Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
Cc Stefan Berger <[email protected]>

---

+++ b/tests/libqtest.c

tests/libqtest.c void qtest_cb_for_every_machine(void (*cb)(const char *machine)) · line 1037

Quoted 9 lines
tests/libqtest.c +6 -0
@@ -1029,6 +1029,35 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
      qobject_unref(response);
  }
 
+QDict *qtest_qmp_receive_success(QTestState *s,
+                                 void (*event_cb)(void *opaque,
+                                                  const char *event,
+                                                  QDict *data),
+                                 void *opaque)
+{

I like the new signature!

tests/migration-test.c static void wait_for_serial(const char *side) · line 160

Quoted 7 lines
tests/migration-test.c +1 -0
+++ b/tests/migration-test.c
 
  /*
   * Events can get in the way of responses we are actually waiting for.
   */
  static QDict *wait_command(QTestState *who, const char *command)
  {

[1] This is still taking a format non-literal command...

Quoted 4 lines
tests/migration-test.c +0 -4
-    const char *event_string;
-    QDict *response, *ret;
-
-    response = qtest_qmp(who, command);

...

Quoted 1 line

+ qtest_qmp_send(who, command);

and is passing it on through.

tests/tpm-util.c void tpm_util_migrate(QTestState *who, const char *uri) · line 254

Quoted 11 lines
tests/tpm-util.c +2 -5
+    return qtest_qmp_receive_success(who, stop_cb, NULL);
  }
 
 
+++ b/tests/tpm-util.c
 
-/*
- * Events can get in the way of responses we are actually waiting for.
- */
-static QDict *tpm_util_wait_command(QTestState *who, const char *command)
-{

Maybe you were counting this instance,

tests/tpm-util.c void tpm_util_migrate(QTestState *who, const char *uri) · line 255

Quoted 11 lines
  void tpm_util_wait_for_migration_complete(QTestState *who)
  {
      while (true) {
tests/tpm-util.c +2 -3
-        QDict *rsp, *rsp_return;
+        QDict *rsp_return;
          bool completed;
          const char *status;
 
-        rsp = tpm_util_wait_command(who, "{ 'execute': 'query-migrate' }");
-        rsp_return = qdict_get_qdict(rsp, "return");
+        qtest_qmp_send(who, "{ 'execute': 'query-migrate' }");

where you did indeed drop a format non-literal?

But on the assumption that there's more cleanups later in the series, this is indeed incrementally better, so

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 16/23
#39 lore raw

Re: [Qemu-devel] [PATCH v2 16/23] migration-test: Make wait_command() cope with '%'

In reply to Markus Armbruster
Quoted 9 lines · Markus Armbruster

wait_command() passes its argument @command to qtest_qmp_send(). Falls apart if @command contain '%'. Two ways to disarm this trap:
suppress interpretation of '%' by passing @command as argument to format string "%s", or fix it by having wait_command() take the variable arguments to go with @command. Do the latter.

This is another step towards compile-time format string checking without triggering -Wformat-nonliteral.

Aha - fixes up the complaints I had on 15/23.

Quoted 8 lines
Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 17/23
#41 lore raw

Re: [Qemu-devel] [PATCH v2 17/23] migration-test: Clean up string interpolation into QMP, part 1

In reply to Markus Armbruster
Quoted 25 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migrate_recover() builds QMP input manually because wait_command() can't interpolate. Well, it can since the previous commit. Simplify accordingly.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
tests/migration-test.c +0 -0
index 0c92f2b1cd..323bb60535 100644

tests/migration-test.c static void stop_cb(void *opaque, const char *name, QDict *data) · line 160

Quoted 5 lines
tests/migration-test.c +1 -0
@@ -156,6 +156,7 @@ static void stop_cb(void *opaque, const char *name, QDict *data)
  /*
   * Events can get in the way of responses we are actually waiting for.
   */
+GCC_FMT_ATTR(2, 3)
  static QDict *wait_command(QTestState *who, const char *command, ...)

Placement of function-level attributes is so weird (before the return type on implementations, but after the () in declarations) - not your fault.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 18/23
#43 lore raw

Re: [Qemu-devel] [PATCH v2 18/23] migration-test: Clean up string interpolation into QMP, part 2

In reply to Markus Armbruster
Quoted 31 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migrate() interpolates members into a JSON object. Change it to take its extra QMP arguments as arguments for qdict_from_jsonf_nofail() instead of a string containing JSON members.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 28 ++++++++++++++++------------
  1 file changed, 16 insertions(+), 12 deletions(-)

+++ b/tests/migration-test.c

tests/migration-test.c +1 -0
@@ -14,6 +14,7 @@
 
  #include "libqtest.h"
  #include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qjson.h"
  #include "qemu/option.h"
  #include "qemu/range.h"
  #include "qemu/sockets.h"

tests/migration-test.c static void migrate_set_capability(QTestState *who, const char *capability, · line 386

Quoted 6 lines
tests/migration-test.c +1 -1
@@ -381,16 +382,19 @@ static void migrate_set_capability(QTestState *who, const char *capability,
      qobject_unref(rsp);
  }
 
-static void migrate(QTestState *who, const char *uri, const char *extra)
+static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
  {

No gcc attribute?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 19/23
#46 lore raw

Re: [Qemu-devel] [PATCH v2 19/23] migration-test: Clean up string interpolation into QMP, part 3

In reply to Markus Armbruster
Quoted 37 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migration-test.c interpolates strings into JSON in a few places:

* migrate_set_parameter() interpolates string parameter @value as a JSON number. Change it to long long. This requires changing migrate_check_parameter() similarly.

* migrate_set_capability() interpolates string parameter @value as a JSON boolean. Change it to bool.

* deprecated_set_speed() interpolates string parameter @value as a JSON number. Change it to long long.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 74 +++++++++++++++++-------------------------
  1 file changed, 29 insertions(+), 45 deletions(-)
tests/migration-test.c +0 -1
index 1c1e56b15b..132c30891d 100644
@@ -315,31 +315,25 @@ static void cleanup(const char *filename)
  }
 
  static void migrate_check_parameter(QTestState *who, const char *parameter,
-                                    const char *value)

tests/migration-test.c static void cleanup(const char *filename) · line 327

Quoted 10 lines
tests/migration-test.c +1 -4
+                                    long long value)
  {
      QDict *rsp_return;
-    char *result;
 
      rsp_return = wait_command(who,
                                "{ 'execute': 'query-migrate-parameters' }");
-    result = g_strdup_printf("%" PRId64,
-                             qdict_get_try_int(rsp_return,  parameter, -1));
-    g_assert_cmpstr(result, ==, value);

The old code allows defaulting to -1 if the value is not present;

tests/migration-test.c static void cleanup(const char *filename) · line 324

Quoted 2 lines
tests/migration-test.c +1 -1
-    g_free(result);
+    g_assert_cmpint(qdict_get_int(rsp_return,  parameter), ==, value);

the new code requires the value to be found. Matters if any of the callers passed "-1" in the old code, but a search of the file doesn't spot any such callers. So I think you're okay.

Also, while touching this line, it would be nice to get rid of the double space before parameter.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 20/23
#49 lore raw

Re: [Qemu-devel] [PATCH v2 20/23] libqtest: Enable compile-time format string checking

In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster

qtest_qmp() & friends pass their format string and variable arguments to qobject_from_vjsonf_nofail(). Unlike qobject_from_jsonv(), they aren't decorated with GCC_FMT_ATTR(). Fix that to get compile-time format string checking.

---
  tests/libqtest.h | 32 +++++++++++++++++++-------------
  1 file changed, 19 insertions(+), 13 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH v2 02/23] libqtest: Rename functions to send QMP messages

In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster

[...] Rename qmp_fd_sendv() to qmp_fd_vsend().

Why? I think it's also quite common to have the "v" at the end, e.g. object_new_with_propv, gdb_do_syscallv, qobject_from_jsonv, etc.

Thomas

#51 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Markus Armbruster
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.
And it might be shorter in the compiled binary (one function call vs. two).

Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

Thomas

#52 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

The size of the test binaries is not our biggest concern.

Quoted 5 lines
Quoted 2 lines

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

I think such fixes are now easier to make, but can be separate followup patches. The mechanical conversion is fine to me.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by Reviewed-by on v2 21/23
#58 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Markus Armbruster
Quoted 7 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter. Moreover, the presence of these functions encourage sloppy testing. Remove them.

---

tests/ahci-test.c static void test_atapi_tray(void) · line 1614

Quoted 13 lines

+++ b/tests/ahci-test.c

tests/ahci-test.c +2 -3
@@ -1607,14 +1607,14 @@ static void test_atapi_tray(void)
      atapi_wait_tray(true);
 
      /* Re-insert media */
-    qmp_discard_response("{'execute': 'blockdev-add', "
+    qobject_unref(qmp("{'execute': 'blockdev-add', "
                            "'arguments': {'node-name': 'node0', "
                                          "'driver': 'raw', "
                                          "'file': { 'driver': 'file', "
-                                                  "'filename': %s }}}", iso);
-    qmp_discard_response("{'execute': 'blockdev-insert-medium',"
+                      "'filename': %s }}}", iso));

Why did you fix indentation for some, but not all, of the lines here?

tests/ahci-test.c static void test_atapi_tray(void) · line 1617

Quoted 4 lines
tests/ahci-test.c +2 -1
+    qobject_unref(qmp("{'execute': 'blockdev-insert-medium',"
                            "'arguments': { 'id': 'cd0', "
-                                         "'node-name': 'node0' }}");
+                      "'node-name': 'node0' }}"));

Again, indentation looks odd.

Quoted 10 lines
tests/fdc-test.c +2 -0
+++ b/tests/fdc-test.c
@@ -26,6 +26,7 @@
 
 
  #include "libqtest.h"
+#include "qapi/qmp/qdict.h"
  #include "qemu-common.h"
 
  #define TEST_IMAGE_SIZE 1440 * 1024

tests/fdc-test.c static void test_media_insert(void) · line 304

Quoted 8 lines
tests/fdc-test.c +2 -2
@@ -298,9 +299,9 @@ static void test_media_insert(void)
 
      /* Insert media in drive. DSKCHK should not be reset until a step pulse
       * is sent. */
-    qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
+    qobject_unref(qmp("{'execute':'blockdev-change-medium', 'arguments':{"
                           " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
-                         test_image);
+                      test_image));

Another place where indentation looks odd.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#6 lore raw

Re: [Qemu-devel] [PATCH v2 02/23] libqtest: Rename functions to send QMP messages

In reply to Thomas Huth
Quoted 5 lines · Thomas Huth
Quoted 1 line · Markus Armbruster

[...] Rename qmp_fd_sendv() to qmp_fd_vsend().

Why? I think it's also quite common to have the "v" at the end, e.g. object_new_with_propv, gdb_do_syscallv, qobject_from_jsonv, etc.

Actually, I think 'printf' vs. 'vprintf' is the model to be following, and patch 23/23 brings in even more consistency. I'm in favor of this rename.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 22/23
#61 lore raw

Re: [Qemu-devel] [PATCH v2 22/23] libqtest: Replace qtest_startf() by qtest_initf()

In reply to Markus Armbruster
Quoted 3 lines · Markus Armbruster

qtest_init() creates a new QTestState, and leaves @global_qtest alone. qtest_start() additionally assigns it to @global_qtest, but qtest_startf() additionall sets assigns NULL to @global_qtest. This

s/additionall sets/additionally/

Quoted 5 lines

makes no sense. Replace it by qtest_initf() that works like qtest_init(), i.e. leaves @global_qtest alone.

---

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
Reviewed-by on v2 23/23
#66 lore raw

Re: [Qemu-devel] [PATCH v2 23/23] libqtest: Rename qtest_FOOv() to qtest_vFOO() for consistency

In reply to Markus Armbruster
Quoted 1 line · Markus Armbruster

Might be worth adding mention of WHAT it is consistent with (as Thomas pointed out on 2/23, we still have inconsistencies with object_new_with_propv and such)

Quoted 5 lines
---
  tests/libqtest.c | 12 ++++++------
  tests/libqtest.h |  4 ++--
  2 files changed, 8 insertions(+), 8 deletions(-)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#63 lore raw

Re: [Qemu-devel] [PATCH v2 22/23] libqtest: Replace qtest_startf() by qtest_initf()

In reply to Markus Armbruster
Quoted 5 lines · Markus Armbruster

qtest_init() creates a new QTestState, and leaves @global_qtest alone. qtest_start() additionally assigns it to @global_qtest, but qtest_startf() additionall sets assigns NULL to @global_qtest. This makes no sense. Replace it by qtest_initf() that works like qtest_init(), i.e. leaves @global_qtest alone.

FWIW, the original idea was to get rid of global_qtest, and then to remove qtest_init instead:

https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg02723.html

Not sure whether Eric is still working on these patches, though.

Thomas

#67 lore raw

Re: [Qemu-devel] [PATCH v2 23/23] libqtest: Rename qtest_FOOv() to qtest_vFOO() for consistency

In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster
---
 tests/libqtest.c | 12 ++++++------
 tests/libqtest.h |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)
tests/libqtest.c +0 -0
index a0d44793fa..3706f30aa2 100644

tests/libqtest.c QDict *qmp_fdv(int fd, const char *fmt, va_list ap) · line 541

Quoted 5 lines
tests/libqtest.c +1 -1
@@ -538,7 +538,7 @@ QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
     return qmp_fd_receive(fd);
 }
 
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)

Unless you also add something to our coding conventions which mandates the naming to be always this way, I think this is just unnecessary code churn.

Thomas

#64 lore raw

Re: [Qemu-devel] [PATCH v2 22/23] libqtest: Replace qtest_startf() by qtest_initf()

In reply to Thomas Huth
Quoted 7 lines · Thomas Huth
Quoted 5 lines · Markus Armbruster

qtest_init() creates a new QTestState, and leaves @global_qtest alone. qtest_start() additionally assigns it to @global_qtest, but qtest_startf() additionall sets assigns NULL to @global_qtest. This makes no sense. Replace it by qtest_initf() that works like qtest_init(), i.e. leaves @global_qtest alone.

Quoted 6 lines

FWIW, the original idea was to get rid of global_qtest, and then to remove qtest_init instead:

https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg02723.html

Not sure whether Eric is still working on these patches, though.

As pointed out in the cover letter, there are still some good things to salvage from my series, but at this point, it's been closer to a year since I last posted anything, so it is just as easy for me to rebase/rework from scratch for salvaging the good things, after this series lands.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#4 lore raw

Re: [Qemu-devel] [PATCH v2 02/23] libqtest: Rename functions to send QMP messages

In reply to Eric Blake
Quoted 14 lines · Eric Blake
Quoted 10 lines · Markus Armbruster

The functions to receive messages are called qtest_qmp_receive() and qmp_receive(), qmp_fd_receive(). The ones to send messages are called qtest_async_qmp(), qtest_async_qmpv(), qmp_async(), qmp_fd_send(), qmp_fd_sendv(). Inconsistent. Rename the *_async* ones to qmp_send(), qtest_qmp_send(), qtest_qmp_vsend(). Rename qmp_fd_sendv() to qmp_fd_vsend().

---

Quoted 9 lines

tests/ahci-test.c static void test_atapi_tray(void) · line 1595

Quoted 7 lines
tests/ahci-test.c +2 -2
@@ -1592,8 +1592,8 @@ static void test_atapi_tray(void)
      atapi_wait_tray(false);
        /* Remove media */
-    qmp_async("{'execute': 'blockdev-open-tray', "
-               "'arguments': {'id': 'cd0'}}");
+    qmp_send("{'execute': 'blockdev-open-tray',"
+             " 'arguments': {'id': 'cd0'}}");

Could perhaps fit in one line now, but I won't insist.

I tried, it doesn't fit.

#29 lore raw

Re: [Qemu-devel] [PATCH v2 13/23] tests: Clean up string interpolation around qtest_qmp_device_add()

In reply to Eric Blake
Quoted 14 lines · Eric Blake
Quoted 13 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the commit before previous.

qtest_qmp_device_add() and its wrappers interpolate into JSON as follows:

* qtest_qmp_device_add() interpolates members into a JSON object.

* So do its wrappers qpci_plug_device_test() and usb_test_hotplug().

* usb_test_hotplug() additionally interpolates strings and numbers into JSON strings.

Quoted 6 lines
Quoted 4 lines

Clean them up:

* Have qtest_qmp_device_add() take its extra device properties as arguments for for qdict_from_jsonf_nofail() instead of a string

s/for for/for/

Fixing...

Quoted 16 lines
Quoted 15 lines

containing JSON members.

* Drop qpci_plug_device_test(), use qtest_qmp_device_add() directly.

* Change usb_test_hotplug() parameter @port to string, to avoid interpolation. Interpolate @hcd_id separately.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

---

Quoted 14 lines

tests/ivshmem-test.c static void test_ivshmem_server_irq(void) · line 426

Quoted 12 lines

+++ b/tests/ivshmem-test.c

tests/ivshmem-test.c +2 -3
@@ -420,19 +420,17 @@ static void test_ivshmem_server_irq(void)
  static void test_ivshmem_hotplug(void)
  {
      const char *arch = qtest_get_arch();
-    gchar *opts;
        qtest_start("");
  -    opts = g_strdup_printf("'shm': '%s', 'size': '1M'", tmpshm);
-
-    qpci_plug_device_test("ivshmem", "iv1", PCI_SLOT_HP, opts);
+    qtest_qmp_device_add("ivshmem",
+                         "iv1", "{'addr': %s, 'shm': '%s', 'size': '1M'}",

Umm, how does this still pass? You want 'shm':%s, not 'shm':'%s'.

Good catch.

It passes, because the value of "shm" is an arbitrary file name under /dev/shm/.

Quoted 5 lines

(We really want to assert that any % interpolations in our JSON parser are NOT embedded in '').

With that error fixed,

Thanks!

#37 lore raw

Re: [Qemu-devel] [PATCH v2 15/23] tests: New helper qtest_qmp_receive_success()

In reply to Eric Blake
Quoted 45 lines · Eric Blake
Quoted 8 lines · Markus Armbruster

Commit b21373d0713 copied wait_command() from tests/migration-test.c to tests/tpm-util.c. Replace both copies by new libqtest helper qtest_qmp_receive_success(). Also use it to simplify qtest_qmp_device_del().

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Where? [1]

Quoted 12 lines
Cc Thomas Huth <[email protected]>
Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
Cc Stefan Berger <[email protected]>

---

+++ b/tests/libqtest.c

tests/libqtest.c void qtest_cb_for_every_machine(void (*cb)(const char *machine)) · line 1036

Quoted 8 lines
tests/libqtest.c +5 -0
@@ -1029,6 +1029,35 @@ void qtest_cb_for_every_machine(void (*cb)(const char *machine))
      qobject_unref(response);
  }
  +QDict *qtest_qmp_receive_success(QTestState *s,
+                                 void (*event_cb)(void *opaque,
+                                                  const char *event,
+                                                  QDict *data),
+                                 void *opaque)
+{

I like the new signature!

tests/migration-test.c static void wait_for_serial(const char *side) · line 160

Quoted 7 lines
tests/migration-test.c +1 -0
+++ b/tests/migration-test.c
 
  /*
   * Events can get in the way of responses we are actually waiting for.
   */
  static QDict *wait_command(QTestState *who, const char *command)
  {

[1] This is still taking a format non-literal command...

Quoted 8 lines
Quoted 4 lines
tests/migration-test.c +0 -4
-    const char *event_string;
-    QDict *response, *ret;
-
-    response = qtest_qmp(who, command);

...

Quoted 1 line

+ qtest_qmp_send(who, command);

and is passing it on through.

No worse than before. Will be fixed in the next two patches.

Quoted 6 lines
Quoted 5 lines

+ return qtest_qmp_receive_success(who, stop_cb, NULL);
}

+++ b/tests/tpm-util.c

Quoted 8 lines

tests/tpm-util.c void tpm_util_migrate(QTestState *who, const char *uri) · line 254

Quoted 5 lines
tests/tpm-util.c +0 -5
-/*
- * Events can get in the way of responses we are actually waiting for.
- */
-static QDict *tpm_util_wait_command(QTestState *who, const char *command)
-{

Maybe you were counting this instance,

- const char *event_string;
- QDict *response;
-
- response = qtest_qmp(who, command);

/work/armbru/qemu/tests/tpm-util.c: In function ‘tpm_util_wait_command’:
/work/armbru/qemu/tests/tpm-util.c:258:5: warning: format not a string literal and no format arguments [-Wformat-security]
response = qtest_qmp(who, command);
^~~~~~~~

-
- while (qdict_haskey(response, "event")) {
- /* OK, it was an event */
- event_string = qdict_get_str(response, "event");
- if (!strcmp(event_string, "STOP")) {
- got_stop = true;
- }
- qobject_unref(response);
- response = qtest_qmp_receive(who);
- }
- return response;
-}

Quoted 12 lines

tests/tpm-util.c void tpm_util_migrate(QTestState *who, const char *uri) · line 255

Quoted 11 lines
  void tpm_util_wait_for_migration_complete(QTestState *who)
  {
      while (true) {
tests/tpm-util.c +1 -1
-        QDict *rsp, *rsp_return;
+        QDict *rsp_return;
          bool completed;
          const char *status;
  -        rsp = tpm_util_wait_command(who, "{ 'execute':

'query-migrate' }");

tests/tpm-util.c +1 -1
-        rsp_return = qdict_get_qdict(rsp, "return");
+        qtest_qmp_send(who, "{ 'execute': 'query-migrate' }");
Quoted 6 lines

where you did indeed drop a format non-literal?

But on the assumption that there's more cleanups later in the series, this is indeed incrementally better, so

Thanks!

#44 lore raw

Re: [Qemu-devel] [PATCH v2 18/23] migration-test: Clean up string interpolation into QMP, part 2

In reply to Eric Blake
Quoted 31 lines · Eric Blake
Quoted 30 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migrate() interpolates members into a JSON object. Change it to take its extra QMP arguments as arguments for qdict_from_jsonf_nofail() instead of a string containing JSON members.

Bonus: gets rid of a non-literal format string. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 28 ++++++++++++++++------------
  1 file changed, 16 insertions(+), 12 deletions(-)

+++ b/tests/migration-test.c

tests/migration-test.c +1 -0
@@ -14,6 +14,7 @@
    #include "libqtest.h"
  #include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qjson.h"
  #include "qemu/option.h"
  #include "qemu/range.h"
  #include "qemu/sockets.h"
@@ -381,16 +382,19 @@ static void migrate_set_capability(QTestState *who, const char *capability,
Quoted 8 lines

tests/migration-test.c static void migrate_set_capability(QTestState *who, const char *capability, · line 386

Quoted 6 lines
tests/migration-test.c +1 -0
      qobject_unref(rsp);
  }
  -static void migrate(QTestState *who, const char *uri, const char
 *extra)
+static void migrate(QTestState *who, const char *uri, const char *fmt, ...)
  {

No gcc attribute?

Good point; adding one.

Quoted 1 line

Thanks!

#47 lore raw

Re: [Qemu-devel] [PATCH v2 19/23] migration-test: Clean up string interpolation into QMP, part 3

In reply to Eric Blake
Quoted 50 lines · Eric Blake
Quoted 36 lines · Markus Armbruster

Leaving interpolation into JSON to qmp() is more robust than building QMP input manually, as explained in the recent commit "tests: Clean up string interpolation into QMP input (simple cases)".

migration-test.c interpolates strings into JSON in a few places:

* migrate_set_parameter() interpolates string parameter @value as a JSON number. Change it to long long. This requires changing migrate_check_parameter() similarly.

* migrate_set_capability() interpolates string parameter @value as a JSON boolean. Change it to bool.

* deprecated_set_speed() interpolates string parameter @value as a JSON number. Change it to long long.

Bonus: gets rid of non-literal format strings. A step towards compile-time format string checking without triggering -Wformat-nonliteral.

Cc Juan Quintela <[email protected]>
Cc Dr. David Alan Gilbert <[email protected]>
---
  tests/migration-test.c | 74 +++++++++++++++++-------------------------
  1 file changed, 29 insertions(+), 45 deletions(-)
tests/migration-test.c +0 -0
index 1c1e56b15b..132c30891d 100644
@@ -315,31 +315,25 @@ static void cleanup(const char *filename)
  }
    static void migrate_check_parameter(QTestState *who, const char
 *parameter,

tests/migration-test.c static void cleanup(const char *filename) · line 326

Quoted 10 lines
tests/migration-test.c +1 -5
-                                    const char *value)
+                                    long long value)
  {
      QDict *rsp_return;
-    char *result;
        rsp_return = wait_command(who,
                                "{ 'execute': 'query-migrate-parameters' }");
-    result = g_strdup_printf("%" PRId64,
-                             qdict_get_try_int(rsp_return,  parameter, -1));
-    g_assert_cmpstr(result, ==, value);

The old code allows defaulting to -1 if the value is not present;

Quoted 6 lines

tests/migration-test.c static void cleanup(const char *filename) · line 324

Quoted 2 lines
tests/migration-test.c +1 -1
-    g_free(result);
+    g_assert_cmpint(qdict_get_int(rsp_return,  parameter), ==, value);

the new code requires the value to be found. Matters if any of the callers passed "-1" in the old code, but a search of the file doesn't spot any such callers. So I think you're okay.

Also:

    ##
    # @MigrationParameters:
    #
    # The optional members aren't actually optional.
    #

This is due to commit 1bda8b3c695.

Quoted 2 lines

Also, while touching this line, it would be nice to get rid of the double space before parameter.

Of course.

Quoted 1 line

Thanks!

#59 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Eric Blake
Quoted 12 lines · Eric Blake
Quoted 7 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter. Moreover, the presence of these functions encourage sloppy testing. Remove them.

---

Quoted 14 lines

tests/ahci-test.c static void test_atapi_tray(void) · line 1613

Quoted 12 lines

+++ b/tests/ahci-test.c

tests/ahci-test.c +2 -3
@@ -1607,14 +1607,14 @@ static void test_atapi_tray(void)
      atapi_wait_tray(true);
        /* Re-insert media */
-    qmp_discard_response("{'execute': 'blockdev-add', "
+    qobject_unref(qmp("{'execute': 'blockdev-add', "
                            "'arguments': {'node-name': 'node0', "
                                          "'driver': 'raw', "
                                          "'file': { 'driver': 'file', "
-                                                  "'filename': %s }}}", iso);
-    qmp_discard_response("{'execute': 'blockdev-insert-medium',"
+                      "'filename': %s }}}", iso));

Why did you fix indentation for some, but not all, of the lines here?

Editing accident, fixing.

Quoted 6 lines

tests/ahci-test.c static void test_atapi_tray(void) · line 1617

Quoted 4 lines
tests/ahci-test.c +2 -1
+    qobject_unref(qmp("{'execute': 'blockdev-insert-medium',"
                            "'arguments': { 'id': 'cd0', "
-                                         "'node-name': 'node0' }}");
+                      "'node-name': 'node0' }}"));

Again, indentation looks odd.

Likewise.

Quoted 9 lines
Quoted 9 lines
tests/fdc-test.c +2 -0
+++ b/tests/fdc-test.c
@@ -26,6 +26,7 @@
      #include "libqtest.h"
+#include "qapi/qmp/qdict.h"
  #include "qemu-common.h"
    #define TEST_IMAGE_SIZE 1440 * 1024
@@ -298,9 +299,9 @@ static void test_media_insert(void)
        /* Insert media in drive. DSKCHK should not be reset until a

step pulse

Quoted 8 lines

tests/fdc-test.c static void test_media_insert(void) · line 304

Quoted 6 lines

* is sent. */

tests/fdc-test.c +2 -2
-    qmp_discard_response("{'execute':'blockdev-change-medium', 'arguments':{"
+    qobject_unref(qmp("{'execute':'blockdev-change-medium', 'arguments':{"
                           " 'id':'floppy0', 'filename': %s, 'format': 'raw' }}",
-                         test_image);
+                      test_image));

Another place where indentation looks odd.

Likewise.

Quoted 1 line

Thanks!

#53 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Eric Blake
Quoted 6 lines · Eric Blake
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Doing things sloppily looks a bit uglier now. That's a feature.

Quoted 8 lines

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

I'd be quite sympathetic to this argument...

Quoted 1 line

The size of the test binaries is not our biggest concern.

... outside tests/.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

I'd welcome such patches, but this series is already pretty long.

Quoted 2 lines

I think such fixes are now easier to make, but can be separate followup patches. The mechanical conversion is fine to me.

#62 lore raw

Re: [Qemu-devel] [PATCH v2 22/23] libqtest: Replace qtest_startf() by qtest_initf()

In reply to Eric Blake
Quoted 6 lines · Eric Blake
Quoted 3 lines · Markus Armbruster

qtest_init() creates a new QTestState, and leaves @global_qtest alone. qtest_start() additionally assigns it to @global_qtest, but qtest_startf() additionall sets assigns NULL to @global_qtest. This

s/additionall sets/additionally/

Fixing...

Quoted 7 lines
Quoted 5 lines

makes no sense. Replace it by qtest_initf() that works like qtest_init(), i.e. leaves @global_qtest alone.

---

Thanks!

#68 lore raw

Re: [Qemu-devel] [PATCH v2 23/23] libqtest: Rename qtest_FOOv() to qtest_vFOO() for consistency

In reply to Thomas Huth
Quoted 15 lines · Thomas Huth
Quoted 14 lines · Markus Armbruster
---
 tests/libqtest.c | 12 ++++++------
 tests/libqtest.h |  4 ++--
 2 files changed, 8 insertions(+), 8 deletions(-)
tests/libqtest.c +0 -0
index a0d44793fa..3706f30aa2 100644
@@ -538,7 +538,7 @@ QDict *qmp_fdv(int fd, const char *fmt, va_list ap)
     return qmp_fd_receive(fd);
 }
 
Quoted 6 lines

tests/libqtest.c QDict *qmp_fdv(int fd, const char *fmt, va_list ap) · line 541

Quoted 2 lines
tests/libqtest.c +1 -1
-QDict *qtest_qmpv(QTestState *s, const char *fmt, va_list ap)
+QDict *qtest_vqmp(QTestState *s, const char *fmt, va_list ap)

Unless you also add something to our coding conventions which mandates the naming to be always this way, I think this is just unnecessary code churn.

Consistency isn't "necessary", but it helps.

Tree-wide consistency would be nice, but my aim right now is local consistency in libqtest.h. Four out of its six functions taking va_list are sensibly named. This patch renames the remaining two. Well worth the diffstat, in my opinion.

#30 lore raw

Re: [Qemu-devel] [PATCH v2 13/23] tests: Clean up string interpolation around qtest_qmp_device_add()

In reply to Eric Blake

Eric Blake <[email protected]> writes:

[...]

Quoted 2 lines

(We really want to assert that any % interpolations in our JSON parser are NOT embedded in '').

I'll look into that, but it'll be in a separate series.

[...]

#31 lore raw

Re: [Qemu-devel] [PATCH v2 13/23] tests: Clean up string interpolation around qtest_qmp_device_add()

Quoted 7 lines · Markus Armbruster

Eric Blake <[email protected]> writes:

[...]

Quoted 2 lines

(We really want to assert that any % interpolations in our JSON parser are NOT embedded in '').

I'll look into that, but it'll be in a separate series.

Agreed. In fact, my more ambitious series had reached that point by implementing %% interpolation inside strings, combined with asserting that %% cannot occur except within strings during the JSON parse, then during the JSON interpolation that the only use of % within strings was the %% escape (so that we no longer risk consuming a va-arg during string interpolation, while still benefiting from gcc's -Wformat checking). So probably one of the easier things to revive, once this series lands.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#32 lore raw

Re: [Qemu-devel] [PATCH v2 13/23] tests: Clean up string interpolation around qtest_qmp_device_add()

Quoted 9 lines · Eric Blake
Quoted 7 lines · Markus Armbruster

Eric Blake <[email protected]> writes:

[...]

Quoted 2 lines

(We really want to assert that any % interpolations in our JSON parser are NOT embedded in '').

I'll look into that, but it'll be in a separate series.

Quoted 8 lines

Agreed. In fact, my more ambitious series had reached that point by implementing %% interpolation inside strings, combined with asserting that %% cannot occur except within strings during the JSON parse, then during the JSON interpolation that the only use of % within strings was the %% escape (so that we no longer risk consuming a va-arg during string interpolation, while still benefiting from gcc's -Wformat checking). So probably one of the easier things to revive, once this series lands.

The problem: the compiler recognizes conversion specifications the JSON parser doesn't recognize. Can lead to crashes or silent misbehavior.

The JSON lexer recognizes conversion specification tokens, and the JSON parser accepts them as JSON value. The lexer rejects conversion specification tokens we don't support. Conversion specifications within tokens are not recognized. Since only string tokens can contain '%', conversion specifications can hide from the JSON parser only there.

I can see three ways to fix that. All three make the JSON parser recognize all conversion specifications. They differ in which ones fail.

1. Obey:

The ones inside strings work as in sprintf().

Example:
"{ 'str': %s, 'act': '%.0f%%', 'max': '100%%' }", str, p * 100.0

Encourages interpolation into strings, which is problematic, as explained in PATCH 11 "tests: Clean up string interpolation into QMP input (simple cases)". Moreover, supporting some conversion specifications only in strings (e.g. %g, %.0f, %%) could be confusing.

2. Obey "%%" in strings, reject the rest

You can only interpolate strings, not into strings. To put a '%' intro a string, you have to double it.

Example:
"{ 'str': %s, 'pct': %s, 'max': '100%%' }", str, pct where pct = g_strdup_printf("%0.f%%", p * 100.0)

Strings are interpreted differently when the JSON parser has interpolation enabled. That's the price we have to pay for not having to interpolate strings containing '%'.

3. Reject:

You can't have '%' in strings. To get a string containing '%', you have to interpolate it.

Example:
"{ 'str': %s, 'pct': %s, 'max': %s }", str, pct, "100%" where pct = g_strdup_printf("%0.f%%", p * 100.0)

This is the simplest solution.

#54 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

In reply to Markus Armbruster
Quoted 24 lines · Markus Armbruster
Quoted 6 lines · Eric Blake
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Doing things sloppily looks a bit uglier now. That's a feature.

Quoted 8 lines

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

I'd be quite sympathetic to this argument...

Quoted 1 line

The size of the test binaries is not our biggest concern.

Quoted 8 lines

... outside tests/.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

I'd welcome such patches, but this series is already pretty long.

Then maybe rather drop this patch from this series, and fix the issues in a separate series instead?

Thomas

#55 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

Quoted 29 lines · Thomas Huth
Quoted 28 lines · Markus Armbruster
Quoted 6 lines · Eric Blake
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Doing things sloppily looks a bit uglier now. That's a feature.

Quoted 8 lines

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

I'd be quite sympathetic to this argument...

Quoted 1 line

The size of the test binaries is not our biggest concern.

... outside tests/.

Quoted 2 lines
Quoted 2 lines
Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

I'd welcome such patches, but this series is already pretty long.

Then maybe rather drop this patch from this series, and fix the issues in a separate series instead?

Do you insist?

I fail to see how changing

    qmp_discard_response("{ 'execute': 'system_reset' }");

to

    qobject_unref(qmp("{ 'execute': 'system_reset' }"));

is so awful it would justify demanding I pause my work on libqtest to first figure out which parts of ignored responses are worth checking, then code up the checks.

Would you accept

    rsp = qmp("{ 'execute': 'system_reset' }"));
    qobject_unref(rsp);

?

If none of the above is acceptable to you, then I'll push the crap that needs to go from libqtest into the crap-using tests, like this:

    /* TODO actually test the results and get rid of this */
    #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__));
#56 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

Quoted 34 lines · Markus Armbruster
Quoted 32 lines · Thomas Huth
Quoted 31 lines · Markus Armbruster
Quoted 6 lines · Eric Blake
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Doing things sloppily looks a bit uglier now. That's a feature.

Quoted 8 lines

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

I'd be quite sympathetic to this argument...

Quoted 1 line

The size of the test binaries is not our biggest concern.

... outside tests/.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I'd welcome such patches, but this series is already pretty long.

Then maybe rather drop this patch from this series, and fix the issues in a separate series instead?

Do you insist?

No. But I'd still like to convince you that this patch is unnecessary right now.

Quoted 11 lines

I fail to see how changing

    qmp_discard_response("{ 'execute': 'system_reset' }");

to

    qobject_unref(qmp("{ 'execute': 'system_reset' }"));

is so awful it would justify demanding I pause my work on libqtest to first figure out which parts of ignored responses are worth checking, then code up the checks.

First, you don't have to pause this series just because of this, since the remaining two patches do not depend on this one.
Then, I still fail to see the real benefit here. You've found something that needs proper clean up later (by adding checks for valid responses). So IMHO simply add a big fat warning comment to the description of qmp_discard_response would be sufficient. Then you can easily grep for "qmp_discard_response" later to find the spots that need fixing. If you replace it with that ugly nested construct instead, we still should fix it later, but it's a little bit harder to grep, and since we need to change it later again anyway, it just sounds like unnecessary code churn to me. So do you really need this so badly (for your later work?), or could you simply skip this patch?

Quoted 6 lines

Would you accept

    rsp = qmp("{ 'execute': 'system_reset' }"));
    qobject_unref(rsp);

?

While this is easier to read, I think we lose the easy way to grep for the spots that need fixing later here, so let's better not do this.

Quoted 5 lines

If none of the above is acceptable to you, then I'll push the crap that needs to go from libqtest into the crap-using tests, like this:

    /* TODO actually test the results and get rid of this */
    #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__));

Fine for me.

Thomas

#57 lore raw

Re: [Qemu-devel] [PATCH v2 21/23] libqtest: Remove qtest_qmp_discard_response() & friends

Quoted 51 lines · Thomas Huth
Quoted 34 lines · Markus Armbruster
Quoted 32 lines · Thomas Huth
Quoted 31 lines · Markus Armbruster
Quoted 6 lines · Eric Blake
Quoted 5 lines · Thomas Huth
Quoted 2 lines · Markus Armbruster

qtest_qmp_discard_response(...) is shorthand for qobject_unref(qtest_qmp(...), except it's not actually shorter.

But the latter is IMHO harder to read.

Doing things sloppily looks a bit uglier now. That's a feature.

Quoted 8 lines

Maybe, but then it lends itself well to:

QObject *rsp = qtest_qmp(...);
qobject_unref(rsp);

which is where you do insert tests for valid responses.

Quoted 1 line

And it might be shorter in the compiled binary (one function call vs. two).

I'd be quite sympathetic to this argument...

Quoted 1 line

The size of the test binaries is not our biggest concern.

... outside tests/.

Quoted 4 lines
Quoted 4 lines
Quoted 1 line

Moreover, the presence of these functions encourage sloppy testing.

Shouldn't we then rather fix the tests to check for valid responses instead of replacing this function with harder-to-read code?

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

I'd welcome such patches, but this series is already pretty long.

Then maybe rather drop this patch from this series, and fix the issues in a separate series instead?

Do you insist?

No. But I'd still like to convince you that this patch is unnecessary right now.

Quoted 6 lines

I fail to see how changing

    qmp_discard_response("{ 'execute': 'system_reset' }");

to

Quoted 8 lines
Quoted 5 lines
    qobject_unref(qmp("{ 'execute': 'system_reset' }"));

is so awful it would justify demanding I pause my work on libqtest to first figure out which parts of ignored responses are worth checking, then code up the checks.

First, you don't have to pause this series just because of this, since the remaining two patches do not depend on this one.

I intend to swap with the previous patch in v3 to reduce churn.

Quoted 4 lines

Then, I still fail to see the real benefit here. You've found something that needs proper clean up later (by adding checks for valid responses). So IMHO simply add a big fat warning comment to the description of qmp_discard_response would be sufficient.

Warnings in function comments are ineffective at counterproliferation. People copy code without examining the called functions' comments.

Quoted 18 lines

Then you can easily grep for
"qmp_discard_response" later to find the spots that need fixing. If you replace it with that ugly nested construct instead, we still should fix it later, but it's a little bit harder to grep, and since we need to change it later again anyway, it just sounds like unnecessary code churn to me. So do you really need this so badly (for your later work?), or could you simply skip this patch?

Quoted 6 lines

Would you accept

    rsp = qmp("{ 'execute': 'system_reset' }"));
    qobject_unref(rsp);

?

While this is easier to read, I think we lose the easy way to grep for the spots that need fixing later here, so let's better not do this.

Quoted 7 lines
Quoted 5 lines

If none of the above is acceptable to you, then I'll push the crap that needs to go from libqtest into the crap-using tests, like this:

    /* TODO actually test the results and get rid of this */
    #define qmp_discard_response(...) qobject_unref(qmp(__VA_ARGS__));

Fine for me.

Sold.