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

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