virtio-net: implement RX RSS processing
[qemu/kevin.git] / tests / test-image-locking.c
blobba057bd66cb942b8aa5515ae6d73c07202d729e2
1 /*
2 * Image locking tests
4 * Copyright (c) 2018 Red Hat Inc.
6 * Author: Fam Zheng <famz@redhat.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu/osdep.h"
28 #include "block/block.h"
29 #include "sysemu/block-backend.h"
30 #include "qapi/error.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qemu/main-loop.h"
34 static BlockBackend *open_image(const char *path,
35 uint64_t perm, uint64_t shared_perm,
36 Error **errp)
38 Error *local_err = NULL;
39 BlockBackend *blk;
40 QDict *options = qdict_new();
42 qdict_put_str(options, "driver", "raw");
43 blk = blk_new_open(path, NULL, options, BDRV_O_RDWR, &local_err);
44 if (blk) {
45 g_assert_null(local_err);
46 if (blk_set_perm(blk, perm, shared_perm, errp)) {
47 blk_unref(blk);
48 blk = NULL;
50 } else {
51 error_propagate(errp, local_err);
53 return blk;
56 static void check_locked_bytes(int fd, uint64_t perm_locks,
57 uint64_t shared_perm_locks)
59 int i;
61 if (!perm_locks && !shared_perm_locks) {
62 g_assert(!qemu_lock_fd_test(fd, 0, 0, true));
63 return;
65 for (i = 0; (1ULL << i) <= BLK_PERM_ALL; i++) {
66 uint64_t bit = (1ULL << i);
67 bool perm_expected = !!(bit & perm_locks);
68 bool shared_perm_expected = !!(bit & shared_perm_locks);
69 g_assert_cmpint(perm_expected, ==,
70 !!qemu_lock_fd_test(fd, 100 + i, 1, true));
71 g_assert_cmpint(shared_perm_expected, ==,
72 !!qemu_lock_fd_test(fd, 200 + i, 1, true));
76 static void test_image_locking_basic(void)
78 BlockBackend *blk1, *blk2, *blk3;
79 char img_path[] = "/tmp/qtest.XXXXXX";
80 uint64_t perm, shared_perm;
82 int fd = mkstemp(img_path);
83 assert(fd >= 0);
85 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
86 shared_perm = BLK_PERM_ALL;
87 blk1 = open_image(img_path, perm, shared_perm, &error_abort);
88 g_assert(blk1);
90 check_locked_bytes(fd, perm, ~shared_perm);
92 /* compatible perm between blk1 and blk2 */
93 blk2 = open_image(img_path, perm | BLK_PERM_RESIZE, shared_perm, NULL);
94 g_assert(blk2);
95 check_locked_bytes(fd, perm | BLK_PERM_RESIZE, ~shared_perm);
97 /* incompatible perm with already open blk1 and blk2 */
98 blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, NULL);
99 g_assert_null(blk3);
101 blk_unref(blk2);
103 /* Check that extra bytes in blk2 are correctly unlocked */
104 check_locked_bytes(fd, perm, ~shared_perm);
106 blk_unref(blk1);
108 /* Image is unused, no lock there */
109 check_locked_bytes(fd, 0, 0);
110 blk3 = open_image(img_path, perm, BLK_PERM_WRITE_UNCHANGED, &error_abort);
111 g_assert(blk3);
112 blk_unref(blk3);
113 close(fd);
114 unlink(img_path);
117 static void test_set_perm_abort(void)
119 BlockBackend *blk1, *blk2;
120 char img_path[] = "/tmp/qtest.XXXXXX";
121 uint64_t perm, shared_perm;
122 int r;
123 int fd = mkstemp(img_path);
124 assert(fd >= 0);
126 perm = BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ;
127 shared_perm = BLK_PERM_ALL;
128 blk1 = open_image(img_path, perm, shared_perm, &error_abort);
129 g_assert(blk1);
131 blk2 = open_image(img_path, perm, shared_perm, &error_abort);
132 g_assert(blk2);
134 check_locked_bytes(fd, perm, ~shared_perm);
136 /* A failed blk_set_perm mustn't change perm status (locked bytes) */
137 r = blk_set_perm(blk2, perm | BLK_PERM_RESIZE, BLK_PERM_WRITE_UNCHANGED,
138 NULL);
139 g_assert_cmpint(r, !=, 0);
140 check_locked_bytes(fd, perm, ~shared_perm);
141 blk_unref(blk1);
142 blk_unref(blk2);
145 int main(int argc, char **argv)
147 bdrv_init();
148 qemu_init_main_loop(&error_abort);
150 g_test_init(&argc, &argv, NULL);
152 if (qemu_has_ofd_lock()) {
153 g_test_add_func("/image-locking/basic", test_image_locking_basic);
154 g_test_add_func("/image-locking/set-perm-abort", test_set_perm_abort);
157 return g_test_run();