xref: /openbmc/qemu/tests/unit/test-block-backend.c (revision 757acb9a8295e8be4a37b2cfc1cd947e357fd29c)
1*da668aa1SThomas Huth /*
2*da668aa1SThomas Huth  * BlockBackend tests
3*da668aa1SThomas Huth  *
4*da668aa1SThomas Huth  * Copyright (c) 2017 Kevin Wolf <kwolf@redhat.com>
5*da668aa1SThomas Huth  *
6*da668aa1SThomas Huth  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*da668aa1SThomas Huth  * of this software and associated documentation files (the "Software"), to deal
8*da668aa1SThomas Huth  * in the Software without restriction, including without limitation the rights
9*da668aa1SThomas Huth  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10*da668aa1SThomas Huth  * copies of the Software, and to permit persons to whom the Software is
11*da668aa1SThomas Huth  * furnished to do so, subject to the following conditions:
12*da668aa1SThomas Huth  *
13*da668aa1SThomas Huth  * The above copyright notice and this permission notice shall be included in
14*da668aa1SThomas Huth  * all copies or substantial portions of the Software.
15*da668aa1SThomas Huth  *
16*da668aa1SThomas Huth  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*da668aa1SThomas Huth  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*da668aa1SThomas Huth  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*da668aa1SThomas Huth  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*da668aa1SThomas Huth  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*da668aa1SThomas Huth  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22*da668aa1SThomas Huth  * THE SOFTWARE.
23*da668aa1SThomas Huth  */
24*da668aa1SThomas Huth 
25*da668aa1SThomas Huth #include "qemu/osdep.h"
26*da668aa1SThomas Huth #include "block/block.h"
27*da668aa1SThomas Huth #include "sysemu/block-backend.h"
28*da668aa1SThomas Huth #include "qapi/error.h"
29*da668aa1SThomas Huth #include "qemu/main-loop.h"
30*da668aa1SThomas Huth 
test_drain_aio_error_flush_cb(void * opaque,int ret)31*da668aa1SThomas Huth static void test_drain_aio_error_flush_cb(void *opaque, int ret)
32*da668aa1SThomas Huth {
33*da668aa1SThomas Huth     bool *completed = opaque;
34*da668aa1SThomas Huth 
35*da668aa1SThomas Huth     g_assert(ret == -ENOMEDIUM);
36*da668aa1SThomas Huth     *completed = true;
37*da668aa1SThomas Huth }
38*da668aa1SThomas Huth 
test_drain_aio_error(void)39*da668aa1SThomas Huth static void test_drain_aio_error(void)
40*da668aa1SThomas Huth {
41*da668aa1SThomas Huth     BlockBackend *blk = blk_new(qemu_get_aio_context(),
42*da668aa1SThomas Huth                                 BLK_PERM_ALL, BLK_PERM_ALL);
43*da668aa1SThomas Huth     BlockAIOCB *acb;
44*da668aa1SThomas Huth     bool completed = false;
45*da668aa1SThomas Huth 
46*da668aa1SThomas Huth     acb = blk_aio_flush(blk, test_drain_aio_error_flush_cb, &completed);
47*da668aa1SThomas Huth     g_assert(acb != NULL);
48*da668aa1SThomas Huth     g_assert(completed == false);
49*da668aa1SThomas Huth 
50*da668aa1SThomas Huth     blk_drain(blk);
51*da668aa1SThomas Huth     g_assert(completed == true);
52*da668aa1SThomas Huth 
53*da668aa1SThomas Huth     blk_unref(blk);
54*da668aa1SThomas Huth }
55*da668aa1SThomas Huth 
test_drain_all_aio_error(void)56*da668aa1SThomas Huth static void test_drain_all_aio_error(void)
57*da668aa1SThomas Huth {
58*da668aa1SThomas Huth     BlockBackend *blk = blk_new(qemu_get_aio_context(),
59*da668aa1SThomas Huth                                 BLK_PERM_ALL, BLK_PERM_ALL);
60*da668aa1SThomas Huth     BlockAIOCB *acb;
61*da668aa1SThomas Huth     bool completed = false;
62*da668aa1SThomas Huth 
63*da668aa1SThomas Huth     acb = blk_aio_flush(blk, test_drain_aio_error_flush_cb, &completed);
64*da668aa1SThomas Huth     g_assert(acb != NULL);
65*da668aa1SThomas Huth     g_assert(completed == false);
66*da668aa1SThomas Huth 
67*da668aa1SThomas Huth     blk_drain_all();
68*da668aa1SThomas Huth     g_assert(completed == true);
69*da668aa1SThomas Huth 
70*da668aa1SThomas Huth     blk_unref(blk);
71*da668aa1SThomas Huth }
72*da668aa1SThomas Huth 
main(int argc,char ** argv)73*da668aa1SThomas Huth int main(int argc, char **argv)
74*da668aa1SThomas Huth {
75*da668aa1SThomas Huth     bdrv_init();
76*da668aa1SThomas Huth     qemu_init_main_loop(&error_abort);
77*da668aa1SThomas Huth 
78*da668aa1SThomas Huth     g_test_init(&argc, &argv, NULL);
79*da668aa1SThomas Huth 
80*da668aa1SThomas Huth     g_test_add_func("/block-backend/drain_aio_error", test_drain_aio_error);
81*da668aa1SThomas Huth     g_test_add_func("/block-backend/drain_all_aio_error",
82*da668aa1SThomas Huth                     test_drain_all_aio_error);
83*da668aa1SThomas Huth 
84*da668aa1SThomas Huth     return g_test_run();
85*da668aa1SThomas Huth }
86