Blame SOURCES/kvm-tests-Add-unit-tests-for-image-locking.patch

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