qemu/tests/wdt_ib700-test.c
<<
>>
Prefs
   1/*
   2 * QTest testcase for the IB700 watchdog
   3 *
   4 * Copyright (c) 2014 Red Hat, Inc.
   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 <glib.h>
  12#include "libqtest.h"
  13#include "qemu/timer.h"
  14
  15static void qmp_check_no_event(void)
  16{
  17    QDict *resp = qmp("{'execute':'query-status'}");
  18    g_assert(qdict_haskey(resp, "return"));
  19    QDECREF(resp);
  20}
  21
  22static QDict *qmp_get_event(const char *name)
  23{
  24    QDict *event = qmp("");
  25    QDict *data;
  26    g_assert(qdict_haskey(event, "event"));
  27    g_assert(!strcmp(qdict_get_str(event, "event"), name));
  28
  29    if (qdict_haskey(event, "data")) {
  30        data = qdict_get_qdict(event, "data");
  31        QINCREF(data);
  32    } else {
  33        data = NULL;
  34    }
  35
  36    QDECREF(event);
  37    return data;
  38}
  39
  40static QDict *ib700_program_and_wait(QTestState *s)
  41{
  42    clock_step(NANOSECONDS_PER_SECOND * 40);
  43    qmp_check_no_event();
  44
  45    /* 2 second limit */
  46    outb(0x443, 14);
  47
  48    /* Ping */
  49    clock_step(NANOSECONDS_PER_SECOND);
  50    qmp_check_no_event();
  51    outb(0x443, 14);
  52
  53    /* Disable */
  54    clock_step(NANOSECONDS_PER_SECOND);
  55    qmp_check_no_event();
  56    outb(0x441, 1);
  57    clock_step(3 * NANOSECONDS_PER_SECOND);
  58    qmp_check_no_event();
  59
  60    /* Enable and let it fire */
  61    outb(0x443, 13);
  62    clock_step(3 * NANOSECONDS_PER_SECOND);
  63    qmp_check_no_event();
  64    clock_step(2 * NANOSECONDS_PER_SECOND);
  65    return qmp_get_event("WATCHDOG");
  66}
  67
  68
  69static void ib700_pause(void)
  70{
  71    QDict *d;
  72    QTestState *s = qtest_start("-watchdog-action pause -device ib700");
  73    qtest_irq_intercept_in(s, "ioapic");
  74    d = ib700_program_and_wait(s);
  75    g_assert(!strcmp(qdict_get_str(d, "action"), "pause"));
  76    QDECREF(d);
  77    d = qmp_get_event("STOP");
  78    QDECREF(d);
  79    qtest_end();
  80}
  81
  82static void ib700_reset(void)
  83{
  84    QDict *d;
  85    QTestState *s = qtest_start("-watchdog-action reset -device ib700");
  86    qtest_irq_intercept_in(s, "ioapic");
  87    d = ib700_program_and_wait(s);
  88    g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
  89    QDECREF(d);
  90    d = qmp_get_event("RESET");
  91    QDECREF(d);
  92    qtest_end();
  93}
  94
  95static void ib700_shutdown(void)
  96{
  97    QDict *d;
  98    QTestState *s = qtest_start("-watchdog-action reset -no-reboot -device ib700");
  99    qtest_irq_intercept_in(s, "ioapic");
 100    d = ib700_program_and_wait(s);
 101    g_assert(!strcmp(qdict_get_str(d, "action"), "reset"));
 102    QDECREF(d);
 103    d = qmp_get_event("SHUTDOWN");
 104    QDECREF(d);
 105    qtest_end();
 106}
 107
 108static void ib700_none(void)
 109{
 110    QDict *d;
 111    QTestState *s = qtest_start("-watchdog-action none -device ib700");
 112    qtest_irq_intercept_in(s, "ioapic");
 113    d = ib700_program_and_wait(s);
 114    g_assert(!strcmp(qdict_get_str(d, "action"), "none"));
 115    QDECREF(d);
 116    qtest_end();
 117}
 118
 119int main(int argc, char **argv)
 120{
 121    int ret;
 122
 123    g_test_init(&argc, &argv, NULL);
 124    qtest_add_func("/wdt_ib700/pause", ib700_pause);
 125    qtest_add_func("/wdt_ib700/reset", ib700_reset);
 126    qtest_add_func("/wdt_ib700/shutdown", ib700_shutdown);
 127    qtest_add_func("/wdt_ib700/none", ib700_none);
 128
 129    ret = g_test_run();
 130
 131    return ret;
 132}
 133