[RFC v5 3/5] tests/qtest: Add raw command response helper

Tao Tang <[email protected]>
Newsgroups gmane.comp.emulators.qemu
Message-ID <[email protected]>
Split raw qtest response tokenization and qtest_raw_cmd() from the
memory attributes helpers so this protocol support can be reviewed
independently.

Signed-off-by: Tao Tang <[email protected]>
---
 tests/qtest/libqtest.c | 27 +++++++++++++++++++++++++--
 tests/qtest/libqtest.h | 13 +++++++++++++
 2 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index 453d0dddd09..37aa69e1297 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -746,11 +746,10 @@ static GString *qtest_client_socket_recv_line(QTestState *s)
     return line;
 }
 
-static gchar **qtest_rsp_args(QTestState *s, int expected_args)
+static gchar **qtest_rsp_words(QTestState *s)
 {
     GString *line;
     gchar **words;
-    int i;
 
 redo:
     line = s->ops.recv_line(s);
@@ -780,6 +779,15 @@ redo:
     }
 
     g_assert(words[0] != NULL);
+
+    return words;
+}
+
+static gchar **qtest_rsp_args(QTestState *s, int expected_args)
+{
+    gchar **words = qtest_rsp_words(s);
+    int i;
+
     g_assert_cmpstr(words[0], ==, "OK");
 
     for (i = 0; i < expected_args; i++) {
@@ -789,6 +797,21 @@ redo:
     return words;
 }
 
+gchar **qtest_raw_cmd(QTestState *s, const char *fmt, ...)
+{
+    va_list ap;
+    gchar *str;
+
+    va_start(ap, fmt);
+    str = g_strdup_vprintf(fmt, ap);
+    va_end(ap);
+
+    s->ops.send(s, str);
+    g_free(str);
+
+    return qtest_rsp_words(s);
+}
+
 static void qtest_rsp(QTestState *s)
 {
     gchar **words = qtest_rsp_args(s, 0);
diff --git a/tests/qtest/libqtest.h b/tests/qtest/libqtest.h
index 7c0b239e5b5..715c363de30 100644
--- a/tests/qtest/libqtest.h
+++ b/tests/qtest/libqtest.h
@@ -239,6 +239,19 @@ void qtest_qmp_send(QTestState *s, const char *fmt, ...)
 void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
     G_GNUC_PRINTF(2, 3);
 
+/**
+ * qtest_raw_cmd:
+ * @s: #QTestState instance to operate on.
+ * @fmt: raw qtest protocol text to send, formatted like sprintf().
+ *
+ * Sends a raw qtest command and returns the response split on spaces.
+ * The response is not required to start with ``OK``; callers can inspect
+ * ``args[0]`` for ``OK``, ``ERR``, or ``FAIL`` and must free the returned
+ * vector with g_strfreev().
+ */
+gchar **qtest_raw_cmd(QTestState *s, const char *fmt, ...)
+    G_GNUC_PRINTF(2, 3);
+
 /**
  * qtest_socket_server:
  * @socket_path: the UNIX domain socket path
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.