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