yeahuh / rpms / qemu-kvm

Forked from rpms/qemu-kvm 2 years ago
Clone
ae23c9
From 3722729c29997c440ff1a289228f2953d10b2e5b Mon Sep 17 00:00:00 2001
ae23c9
From: Max Reitz <mreitz@redhat.com>
ae23c9
Date: Wed, 3 Apr 2019 17:13:12 +0100
ae23c9
Subject: [PATCH 05/11] tests: Add unit tests for image locking
ae23c9
ae23c9
RH-Author: Max Reitz <mreitz@redhat.com>
ae23c9
Message-id: <20190403171315.20841-6-mreitz@redhat.com>
ae23c9
Patchwork-id: 85402
ae23c9
O-Subject: [RHEL-8.1 qemu-kvm PATCH 5/8] tests: Add unit tests for image locking
ae23c9
Bugzilla: 1694148
ae23c9
RH-Acked-by: John Snow <jsnow@redhat.com>
ae23c9
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
ae23c9
RH-Acked-by: Stefano Garzarella <sgarzare@redhat.com>
ae23c9
ae23c9
From: Fam Zheng <famz@redhat.com>
ae23c9
ae23c9
Signed-off-by: Fam Zheng <famz@redhat.com>
ae23c9
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
ae23c9
(cherry picked from commit aef96d7d4f0b6746e329bfa7a1ea38e1611237e3)
ae23c9
Signed-off-by: Max Reitz <mreitz@redhat.com>
ae23c9
Signed-off-by: Danilo C. L. de Paula <ddepaula@redhat.com>
ae23c9
---
ae23c9
 tests/Makefile.include     |   2 +
ae23c9
 tests/test-image-locking.c | 157 +++++++++++++++++++++++++++++++++++++++++++++
ae23c9
 2 files changed, 159 insertions(+)
ae23c9
 create mode 100644 tests/test-image-locking.c
ae23c9
ae23c9
diff --git a/tests/Makefile.include b/tests/Makefile.include
ae23c9
index d200288..06ed6df 100644
ae23c9
--- a/tests/Makefile.include
ae23c9
+++ b/tests/Makefile.include
ae23c9
@@ -97,6 +97,7 @@ check-unit-y += tests/test-bdrv-drain$(EXESUF)
ae23c9
 check-unit-y += tests/test-blockjob$(EXESUF)
ae23c9
 check-unit-y += tests/test-blockjob-txn$(EXESUF)
ae23c9
 check-unit-y += tests/test-block-backend$(EXESUF)
ae23c9
+check-unit-y += tests/test-image-locking$(EXESUF)
ae23c9
 check-unit-y += tests/test-x86-cpuid$(EXESUF)
ae23c9
 # all code tested by test-x86-cpuid is inside topology.h
ae23c9
 gcov-files-test-x86-cpuid-y =
ae23c9
@@ -635,6 +636,7 @@ tests/test-bdrv-drain$(EXESUF): tests/test-bdrv-drain.o $(test-block-obj-y) $(te
ae23c9
 tests/test-blockjob$(EXESUF): tests/test-blockjob.o $(test-block-obj-y) $(test-util-obj-y)
ae23c9
 tests/test-blockjob-txn$(EXESUF): tests/test-blockjob-txn.o $(test-block-obj-y) $(test-util-obj-y)
ae23c9
 tests/test-block-backend$(EXESUF): tests/test-block-backend.o $(test-block-obj-y) $(test-util-obj-y)
ae23c9
+tests/test-image-locking$(EXESUF): tests/test-image-locking.o $(test-block-obj-y) $(test-util-obj-y)
ae23c9
 tests/test-thread-pool$(EXESUF): tests/test-thread-pool.o $(test-block-obj-y)
ae23c9
 tests/test-iov$(EXESUF): tests/test-iov.o $(test-util-obj-y)
ae23c9
 tests/test-hbitmap$(EXESUF): tests/test-hbitmap.o $(test-util-obj-y) $(test-crypto-obj-y)
ae23c9
diff --git a/tests/test-image-locking.c b/tests/test-image-locking.c
ae23c9
new file mode 100644
ae23c9
index 0000000..7614cbf
ae23c9
--- /dev/null
ae23c9
+++ b/tests/test-image-locking.c
ae23c9
@@ -0,0 +1,157 @@
ae23c9
+/*
ae23c9
+ * Image locking tests
ae23c9
+ *
ae23c9
+ * Copyright (c) 2018 Red Hat Inc.
ae23c9
+ *
ae23c9
+ * Author: Fam Zheng <famz@redhat.com>
ae23c9
+ *
ae23c9
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
ae23c9
+ * of this software and associated documentation files (the "Software"), to deal
ae23c9
+ * in the Software without restriction, including without limitation the rights
ae23c9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ae23c9
+ * copies of the Software, and to permit persons to whom the Software is
ae23c9
+ * furnished to do so, subject to the following conditions:
ae23c9
+ *
ae23c9
+ * The above copyright notice and this permission notice shall be included in
ae23c9
+ * all copies or substantial portions of the Software.
ae23c9
+ *
ae23c9
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ae23c9
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ae23c9
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ae23c9
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ae23c9
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ae23c9
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ae23c9
+ * THE SOFTWARE.
ae23c9
+ */
ae23c9
+
ae23c9
+#include "qemu/osdep.h"
ae23c9
+#include "block/block.h"
ae23c9
+#include "sysemu/block-backend.h"
ae23c9
+#include "qapi/error.h"
ae23c9
+#include "qapi/qmp/qdict.h"
ae23c9
+
ae23c9
+static BlockBackend *open_image(const char *path,
ae23c9
+                                uint64_t perm, uint64_t shared_perm,
ae23c9
+                                Error **errp)
ae23c9
+{
ae23c9
+    Error *local_err = NULL;
ae23c9
+    BlockBackend *blk;
ae23c9
+    QDict *options = qdict_new();
ae23c9
+
ae23c9
+    qdict_put_str(options, "driver", "raw");
ae23c9
+    blk = blk_new_open(path, NULL, options, BDRV_O_RDWR, &local_err);
ae23c9
+    if (blk) {
ae23c9
+        g_assert_null(local_err);
ae23c9
+        if (blk_set_perm(blk, perm, shared_perm, errp)) {
ae23c9
+            blk_unref(blk);
ae23c9
+            blk = NULL;
ae23c9
+        }
ae23c9
+    } else {
ae23c9
+        error_propagate(errp, local_err);
ae23c9
+    }
ae23c9
+    return blk;
ae23c9
+}
ae23c9
+
ae23c9
+static void check_locked_bytes(int fd, uint64_t perm_locks,
ae23c9
+                               uint64_t shared_perm_locks)
ae23c9
+{
ae23c9
+    int i;
ae23c9
+
ae23c9
+    if (!perm_locks && !shared_perm_locks) {
ae23c9
+        g_assert(!qemu_lock_fd_test(fd, 0, 0, true));
ae23c9
+        return;
ae23c9
+    }
ae23c9
+    for (i = 0; (1ULL << i) <= BLK_PERM_ALL; i++) {
ae23c9
+        uint64_t bit = (1ULL << i);
ae23c9
+        bool perm_expected = !!(bit & perm_locks);
ae23c9
+        bool shared_perm_expected = !!(bit & shared_perm_locks);
ae23c9
+        g_assert_cmpint(perm_expected, ==,
ae23c9
+                        !!qemu_lock_fd_test(fd, 100 + i, 1, true));
ae23c9
+        g_assert_cmpint(shared_perm_expected, ==,
ae23c9
+                        !!qemu_lock_fd_test(fd, 200 + i, 1, true));
ae23c9
+    }
ae23c9
+}
ae23c9
+
ae23c9
+static void test_image_locking_basic(void)
ae23c9
+{
ae23c9
+    BlockBackend *blk1, *blk2, *blk3;
ae23c9
+    char img_path[] = "/tmp/qtest.XXXXXX";
ae23c9
+    uint64_t perm, shared_perm;
ae23c9
+
ae23c9
+    int fd = mkstemp(img_path);
ae23c9
+    assert(fd >= 0);
ae23c9
+
ae23c9
+    perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
ae23c9
+    shared_perm = BLK_PERM_ALL;
ae23c9
+    blk1 = open_image(img_path, perm, shared_perm, &error_abort);
ae23c9
+    g_assert(blk1);
ae23c9
+
ae23c9
+    check_locked_bytes(fd, perm, ~shared_perm);
ae23c9
+
ae23c9
+    /* compatible perm between blk1 and blk2 */
ae23c9
+    blk2 = open_image(img_path, perm | BLK_PERM_RESIZE, shared_perm, NULL);
ae23c9
+    g_assert(blk2);
ae23c9
+    check_locked_bytes(fd, perm | BLK_PERM_RESIZE, ~shared_perm);
ae23c9
+
ae23c9
+    /* incompatible perm with already open blk1 and blk2 */
ae23c9
+    blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, NULL);
ae23c9
+    g_assert_null(blk3);
ae23c9
+
ae23c9
+    blk_unref(blk2);
ae23c9
+
ae23c9
+    /* Check that extra bytes in blk2 are correctly unlocked */
ae23c9
+    check_locked_bytes(fd, perm, ~shared_perm);
ae23c9
+
ae23c9
+    blk_unref(blk1);
ae23c9
+
ae23c9
+    /* Image is unused, no lock there */
ae23c9
+    check_locked_bytes(fd, 0, 0);
ae23c9
+    blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, &error_abort);
ae23c9
+    g_assert(blk3);
ae23c9
+    blk_unref(blk3);
ae23c9
+    close(fd);
ae23c9
+    unlink(img_path);
ae23c9
+}
ae23c9
+
ae23c9
+static void test_set_perm_abort(void)
ae23c9
+{
ae23c9
+    BlockBackend *blk1, *blk2;
ae23c9
+    char img_path[] = "/tmp/qtest.XXXXXX";
ae23c9
+    uint64_t perm, shared_perm;
ae23c9
+    int r;
ae23c9
+    int fd = mkstemp(img_path);
ae23c9
+    assert(fd >= 0);
ae23c9
+
ae23c9
+    perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
ae23c9
+    shared_perm = BLK_PERM_ALL;
ae23c9
+    blk1 = open_image(img_path, perm, shared_perm, &error_abort);
ae23c9
+    g_assert(blk1);
ae23c9
+
ae23c9
+    blk2 = open_image(img_path, perm, shared_perm, &error_abort);
ae23c9
+    g_assert(blk2);
ae23c9
+
ae23c9
+    check_locked_bytes(fd, perm, ~shared_perm);
ae23c9
+
ae23c9
+    /* A failed blk_set_perm mustn't change perm status (locked bytes) */
ae23c9
+    r = blk_set_perm(blk2, perm | BLK_PERM_RESIZE, BLK_PERM_WRITE_UNCHANGED,
ae23c9
+                     NULL);
ae23c9
+    g_assert_cmpint(r, !=, 0);
ae23c9
+    check_locked_bytes(fd, perm, ~shared_perm);
ae23c9
+    blk_unref(blk1);
ae23c9
+    blk_unref(blk2);
ae23c9
+}
ae23c9
+
ae23c9
+int main(int argc, char **argv)
ae23c9
+{
ae23c9
+    bdrv_init();
ae23c9
+    qemu_init_main_loop(&error_abort);
ae23c9
+
ae23c9
+    g_test_init(&argc, &argv, NULL);
ae23c9
+
ae23c9
+    if (qemu_has_ofd_lock()) {
ae23c9
+        g_test_add_func("/image-locking/basic", test_image_locking_basic);
ae23c9
+        g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort);
ae23c9
+    }
ae23c9
+
ae23c9
+    return g_test_run();
ae23c9
+}
ae23c9
-- 
ae23c9
1.8.3.1
ae23c9