mac99: replace debug printf with trace points
[qemu/ar7.git] / tests / test-blockjob.c
blob60b78a334272665f088197d10fbcf42ebfed6b72
1 /*
2 * Blockjob tests
4 * Copyright Igalia, S.L. 2016
6 * Authors:
7 * Alberto Garcia <berto@igalia.com>
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.
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"
19 static const BlockJobDriver test_block_job_driver = {
20 .instance_size = sizeof(BlockJob),
23 static void block_job_cb(void *opaque, int ret)
27 static BlockJob *do_test_id(BlockBackend *blk, const char *id,
28 bool should_succeed)
30 BlockJob *job;
31 Error *errp = NULL;
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));
43 } else {
44 g_assert_nonnull(errp);
45 g_assert_null(job);
46 error_free(errp);
49 return job;
52 /* This creates a BlockBackend (optionally with a name) with a
53 * BlockDriverState inserted. */
54 static BlockBackend *create_blk(const char *name)
56 BlockBackend *blk = blk_new();
57 BlockDriverState *bs = bdrv_new();
59 blk_insert_bs(blk, bs);
60 bdrv_unref(bs);
62 if (name) {
63 Error *errp = NULL;
64 monitor_add_blk(blk, name, &errp);
65 g_assert_null(errp);
68 return blk;
71 /* This destroys the backend */
72 static void destroy_blk(BlockBackend *blk)
74 if (blk_name(blk)[0] != '\0') {
75 monitor_remove_blk(blk);
78 blk_remove_bs(blk);
79 blk_unref(blk);
82 static void test_job_ids(void)
84 BlockBackend *blk[3];
85 BlockJob *job[3];
87 blk[0] = create_blk(NULL);
88 blk[1] = create_blk("drive1");
89 blk[2] = create_blk("drive2");
91 /* No job ID provided and the block backend has no name */
92 job[0] = do_test_id(blk[0], NULL, false);
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);
104 /* This one is valid */
105 job[0] = do_test_id(blk[0], "id0", true);
107 /* We cannot have two jobs in the same BDS */
108 do_test_id(blk[0], "id1", false);
110 /* Duplicate job IDs are not allowed */
111 job[1] = do_test_id(blk[1], "id0", false);
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);
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);
121 /* Duplicate job ID */
122 job[2] = do_test_id(blk[2], "drive1", false);
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);
128 /* This one is valid */
129 job[2] = do_test_id(blk[2], "id_2", true);
131 block_job_unref(job[0]);
132 block_job_unref(job[1]);
133 block_job_unref(job[2]);
135 destroy_blk(blk[0]);
136 destroy_blk(blk[1]);
137 destroy_blk(blk[2]);
140 int main(int argc, char **argv)
142 qemu_init_main_loop(&error_abort);
144 g_test_init(&argc, &argv, NULL);
145 g_test_add_func("/blockjob/ids", test_job_ids);
146 return g_test_run();