[Qemu-devel] [PATCH v2 57/60] tests/drive_del-test: Fix harmless JSON interpolation bug

Markus Armbruster posted 60 patches 7 years, 2 months ago
There is a newer version of this series
[Qemu-devel] [PATCH v2 57/60] tests/drive_del-test: Fix harmless JSON interpolation bug
Posted by Markus Armbruster 7 years, 2 months ago
test_after_failed_device_add() does this:

    response = qmp("{'execute': 'device_add',"
                   " 'arguments': {"
                   "   'driver': 'virtio-blk-%s',"
                   "   'drive': 'drive0'"
                   "}}", qvirtio_get_dev_type());

Wrong.  An interpolation specification must be a JSON token, it
doesn't work within JSON string tokens.  The code above doesn't use
the value of qvirtio_get_dev_type(), and sends arguments

    {"driver": "virtio-blk-%s", "drive": "drive0"}}

The command fails because there is no driver named "virtio-blk-%".
Harmless, since the test wants the command to fail.  Screwed up in
commit 2f84a92ec63.

Fix the obvious way.  The command now fails because the drive is
empty, like it did before commit 2f84a92ec63.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 tests/drive_del-test.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tests/drive_del-test.c b/tests/drive_del-test.c
index 2d0b176b36..673c10140f 100644
--- a/tests/drive_del-test.c
+++ b/tests/drive_del-test.c
@@ -65,9 +65,13 @@ static void test_drive_without_dev(void)
 
 static void test_after_failed_device_add(void)
 {
+    char driver[32];
     QDict *response;
     QDict *error;
 
+    snprintf(driver, sizeof(driver), "virtio-blk-%s",
+             qvirtio_get_dev_type());
+
     qtest_start("-drive if=none,id=drive0");
 
     /* Make device_add fail. If this leaks the virtio-blk device then a
@@ -75,9 +79,9 @@ static void test_after_failed_device_add(void)
      */
     response = qmp("{'execute': 'device_add',"
                    " 'arguments': {"
-                   "   'driver': 'virtio-blk-%s',"
+                   "   'driver': %s,"
                    "   'drive': 'drive0'"
-                   "}}", qvirtio_get_dev_type());
+                   "}}", driver);
     g_assert(response);
     error = qdict_get_qdict(response, "error");
     g_assert_cmpstr(qdict_get_try_str(error, "class"), ==, "GenericError");
-- 
2.17.1


Re: [Qemu-devel] [PATCH v2 57/60] tests/drive_del-test: Fix harmless JSON interpolation bug
Posted by Eric Blake 7 years, 2 months ago
On 08/17/2018 10:05 AM, Markus Armbruster wrote:
> test_after_failed_device_add() does this:
> 
>      response = qmp("{'execute': 'device_add',"
>                     " 'arguments': {"
>                     "   'driver': 'virtio-blk-%s',"
>                     "   'drive': 'drive0'"
>                     "}}", qvirtio_get_dev_type());
> 
> Wrong.  An interpolation specification must be a JSON token, it
> doesn't work within JSON string tokens.  The code above doesn't use
> the value of qvirtio_get_dev_type(), and sends arguments
> 
>      {"driver": "virtio-blk-%s", "drive": "drive0"}}
> 
> The command fails because there is no driver named "virtio-blk-%".
> Harmless, since the test wants the command to fail.  Screwed up in
> commit 2f84a92ec63.
> 
> Fix the obvious way.  The command now fails because the drive is
> empty, like it did before commit 2f84a92ec63.
> 
> Signed-off-by: Markus Armbruster <armbru@redhat.com>
> ---
>   tests/drive_del-test.c | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org