xref: /openbmc/qemu/tests/qtest/pvpanic-test.c (revision 30a1690f)
1 /*
2  * QTest testcase for PV Panic
3  *
4  * Copyright (c) 2014 SUSE LINUX Products GmbH
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "libqtest.h"
12 #include "qapi/qmp/qdict.h"
13 #include "hw/misc/pvpanic.h"
14 
15 static void test_panic_nopause(void)
16 {
17     uint8_t val;
18     QDict *response, *data;
19     QTestState *qts;
20 
21     qts = qtest_init("-device pvpanic -action panic=none");
22 
23     val = qtest_inb(qts, 0x505);
24     g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
25 
26     qtest_outb(qts, 0x505, 0x1);
27 
28     response = qtest_qmp_eventwait_ref(qts, "GUEST_PANICKED");
29     g_assert(qdict_haskey(response, "data"));
30     data = qdict_get_qdict(response, "data");
31     g_assert(qdict_haskey(data, "action"));
32     g_assert_cmpstr(qdict_get_str(data, "action"), ==, "run");
33     qobject_unref(response);
34 
35     qtest_quit(qts);
36 }
37 
38 static void test_panic(void)
39 {
40     uint8_t val;
41     QDict *response, *data;
42     QTestState *qts;
43 
44     qts = qtest_init("-device pvpanic -action panic=pause");
45 
46     val = qtest_inb(qts, 0x505);
47     g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
48 
49     qtest_outb(qts, 0x505, 0x1);
50 
51     response = qtest_qmp_eventwait_ref(qts, "GUEST_PANICKED");
52     g_assert(qdict_haskey(response, "data"));
53     data = qdict_get_qdict(response, "data");
54     g_assert(qdict_haskey(data, "action"));
55     g_assert_cmpstr(qdict_get_str(data, "action"), ==, "pause");
56     qobject_unref(response);
57 
58     qtest_quit(qts);
59 }
60 
61 static void test_pvshutdown(void)
62 {
63     uint8_t val;
64     QDict *response, *data;
65     QTestState *qts;
66 
67     qts = qtest_init("-device pvpanic");
68 
69     val = qtest_inb(qts, 0x505);
70     g_assert_cmpuint(val, ==, PVPANIC_EVENTS);
71 
72     qtest_outb(qts, 0x505, PVPANIC_SHUTDOWN);
73 
74     response = qtest_qmp_eventwait_ref(qts, "GUEST_PVSHUTDOWN");
75     qobject_unref(response);
76 
77     response = qtest_qmp_eventwait_ref(qts, "SHUTDOWN");
78     g_assert(qdict_haskey(response, "data"));
79     data = qdict_get_qdict(response, "data");
80     g_assert(qdict_haskey(data, "guest"));
81     g_assert(qdict_get_bool(data, "guest"));
82     g_assert(qdict_haskey(data, "reason"));
83     g_assert_cmpstr(qdict_get_str(data, "reason"), ==, "guest-shutdown");
84     qobject_unref(response);
85 
86     qtest_quit(qts);
87 }
88 
89 int main(int argc, char **argv)
90 {
91     g_test_init(&argc, &argv, NULL);
92     qtest_add_func("/pvpanic/panic", test_panic);
93     qtest_add_func("/pvpanic/panic-nopause", test_panic_nopause);
94     qtest_add_func("/pvpanic/pvshutdown", test_pvshutdown);
95 
96     return g_test_run();
97 }
98