cryptospore / rpms / qemu-kvm

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