]> git.proxmox.com Git - mirror_qemu.git/blob - tests/test-blockjob.c
util/cutils: Rename qemu_strtoll(), qemu_strtoull()
[mirror_qemu.git] / tests / test-blockjob.c
1 /*
2 * Blockjob tests
3 *
4 * Copyright Igalia, S.L. 2016
5 *
6 * Authors:
7 * Alberto Garcia <berto@igalia.com>
8 *
9 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
10 * See the COPYING.LIB file in the top-level directory.
11 */
12
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/main-loop.h"
16 #include "block/blockjob_int.h"
17 #include "sysemu/block-backend.h"
18
19 static const BlockJobDriver test_block_job_driver = {
20 .instance_size = sizeof(BlockJob),
21 };
22
23 static void block_job_cb(void *opaque, int ret)
24 {
25 }
26
27 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
28 bool should_succeed)
29 {
30 BlockJob *job;
31 Error *errp = NULL;
32
33 job = block_job_create(id, &test_block_job_driver, blk_bs(blk), 0,
34 BLOCK_JOB_DEFAULT, block_job_cb, NULL, &errp);
35 if (should_succeed) {
36 g_assert_null(errp);
37 g_assert_nonnull(job);
38 if (id) {
39 g_assert_cmpstr(job->id, ==, id);
40 } else {
41 g_assert_cmpstr(job->id, ==, blk_name(blk));
42 }
43 } else {
44 g_assert_nonnull(errp);
45 g_assert_null(job);
46 error_free(errp);
47 }
48
49 return job;
50 }
51
52 /* This creates a BlockBackend (optionally with a name) with a
53 * BlockDriverState inserted. */
54 static BlockBackend *create_blk(const char *name)
55 {
56 BlockBackend *blk = blk_new();
57 BlockDriverState *bs = bdrv_new();
58
59 blk_insert_bs(blk, bs);
60 bdrv_unref(bs);
61
62 if (name) {
63 Error *errp = NULL;
64 monitor_add_blk(blk, name, &errp);
65 g_assert_null(errp);
66 }
67
68 return blk;
69 }
70
71 /* This destroys the backend */
72 static void destroy_blk(BlockBackend *blk)
73 {
74 if (blk_name(blk)[0] != '\0') {
75 monitor_remove_blk(blk);
76 }
77
78 blk_remove_bs(blk);
79 blk_unref(blk);
80 }
81
82 static void test_job_ids(void)
83 {
84 BlockBackend *blk[3];
85 BlockJob *job[3];
86
87 blk[0] = create_blk(NULL);
88 blk[1] = create_blk("drive1");
89 blk[2] = create_blk("drive2");
90
91 /* No job ID provided and the block backend has no name */
92 job[0] = do_test_id(blk[0], NULL, false);
93
94 /* These are all invalid job IDs */
95 job[0] = do_test_id(blk[0], "0id", false);
96 job[0] = do_test_id(blk[0], "", false);
97 job[0] = do_test_id(blk[0], " ", false);
98 job[0] = do_test_id(blk[0], "123", false);
99 job[0] = do_test_id(blk[0], "_id", false);
100 job[0] = do_test_id(blk[0], "-id", false);
101 job[0] = do_test_id(blk[0], ".id", false);
102 job[0] = do_test_id(blk[0], "#id", false);
103
104 /* This one is valid */
105 job[0] = do_test_id(blk[0], "id0", true);
106
107 /* We cannot have two jobs in the same BDS */
108 do_test_id(blk[0], "id1", false);
109
110 /* Duplicate job IDs are not allowed */
111 job[1] = do_test_id(blk[1], "id0", false);
112
113 /* But once job[0] finishes we can reuse its ID */
114 block_job_unref(job[0]);
115 job[1] = do_test_id(blk[1], "id0", true);
116
117 /* No job ID specified, defaults to the backend name ('drive1') */
118 block_job_unref(job[1]);
119 job[1] = do_test_id(blk[1], NULL, true);
120
121 /* Duplicate job ID */
122 job[2] = do_test_id(blk[2], "drive1", false);
123
124 /* The ID of job[2] would default to 'drive2' but it is already in use */
125 job[0] = do_test_id(blk[0], "drive2", true);
126 job[2] = do_test_id(blk[2], NULL, false);
127
128 /* This one is valid */
129 job[2] = do_test_id(blk[2], "id_2", true);
130
131 block_job_unref(job[0]);
132 block_job_unref(job[1]);
133 block_job_unref(job[2]);
134
135 destroy_blk(blk[0]);
136 destroy_blk(blk[1]);
137 destroy_blk(blk[2]);
138 }
139
140 int main(int argc, char **argv)
141 {
142 qemu_init_main_loop(&error_abort);
143
144 g_test_init(&argc, &argv, NULL);
145 g_test_add_func("/blockjob/ids", test_job_ids);
146 return g_test_run();
147 }