hw/arm: Qomify pxa2xx.c
[qemu/ar7.git] / tests / test-blockjob.c
blob740e740398ece2fb9d43d351b3afceacdbd2dafc
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),
34 0, BLK_PERM_ALL, 0, BLOCK_JOB_DEFAULT, block_job_cb,
35 NULL, &errp);
36 if (should_succeed) {
37 g_assert_null(errp);
38 g_assert_nonnull(job);
39 if (id) {
40 g_assert_cmpstr(job->id, ==, id);
41 } else {
42 g_assert_cmpstr(job->id, ==, blk_name(blk));
44 } else {
45 g_assert_nonnull(errp);
46 g_assert_null(job);
47 error_free(errp);
50 return job;
53 /* This creates a BlockBackend (optionally with a name) with a
54 * BlockDriverState inserted. */
55 static BlockBackend *create_blk(const char *name)
57 /* No I/O is performed on this device */
58 BlockBackend *blk = blk_new(0, BLK_PERM_ALL);
59 BlockDriverState *bs;
61 bs = bdrv_open("null-co://", NULL, NULL, 0, &error_abort);
62 g_assert_nonnull(bs);
64 blk_insert_bs(blk, bs, &error_abort);
65 bdrv_unref(bs);
67 if (name) {
68 Error *errp = NULL;
69 monitor_add_blk(blk, name, &errp);
70 g_assert_null(errp);
73 return blk;
76 /* This destroys the backend */
77 static void destroy_blk(BlockBackend *blk)
79 if (blk_name(blk)[0] != '\0') {
80 monitor_remove_blk(blk);
83 blk_remove_bs(blk);
84 blk_unref(blk);
87 static void test_job_ids(void)
89 BlockBackend *blk[3];
90 BlockJob *job[3];
92 blk[0] = create_blk(NULL);
93 blk[1] = create_blk("drive1");
94 blk[2] = create_blk("drive2");
96 /* No job ID provided and the block backend has no name */
97 job[0] = do_test_id(blk[0], NULL, false);
99 /* These are all invalid job IDs */
100 job[0] = do_test_id(blk[0], "0id", false);
101 job[0] = do_test_id(blk[0], "", false);
102 job[0] = do_test_id(blk[0], " ", false);
103 job[0] = do_test_id(blk[0], "123", false);
104 job[0] = do_test_id(blk[0], "_id", false);
105 job[0] = do_test_id(blk[0], "-id", false);
106 job[0] = do_test_id(blk[0], ".id", false);
107 job[0] = do_test_id(blk[0], "#id", false);
109 /* This one is valid */
110 job[0] = do_test_id(blk[0], "id0", true);
112 /* We cannot have two jobs in the same BDS */
113 do_test_id(blk[0], "id1", false);
115 /* Duplicate job IDs are not allowed */
116 job[1] = do_test_id(blk[1], "id0", false);
118 /* But once job[0] finishes we can reuse its ID */
119 block_job_unref(job[0]);
120 job[1] = do_test_id(blk[1], "id0", true);
122 /* No job ID specified, defaults to the backend name ('drive1') */
123 block_job_unref(job[1]);
124 job[1] = do_test_id(blk[1], NULL, true);
126 /* Duplicate job ID */
127 job[2] = do_test_id(blk[2], "drive1", false);
129 /* The ID of job[2] would default to 'drive2' but it is already in use */
130 job[0] = do_test_id(blk[0], "drive2", true);
131 job[2] = do_test_id(blk[2], NULL, false);
133 /* This one is valid */
134 job[2] = do_test_id(blk[2], "id_2", true);
136 block_job_unref(job[0]);
137 block_job_unref(job[1]);
138 block_job_unref(job[2]);
140 destroy_blk(blk[0]);
141 destroy_blk(blk[1]);
142 destroy_blk(blk[2]);
145 int main(int argc, char **argv)
147 qemu_init_main_loop(&error_abort);
148 bdrv_init();
150 g_test_init(&argc, &argv, NULL);
151 g_test_add_func("/blockjob/ids", test_job_ids);
152 return g_test_run();