qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC PATCH 3/3] qtest: add virtio-test test-case


From: Stefan Hajnoczi
Subject: [Qemu-devel] [RFC PATCH 3/3] qtest: add virtio-test test-case
Date: Fri, 13 Apr 2012 15:27:52 +0100

Signed-off-by: Stefan Hajnoczi <address@hidden>
---
 tests/Makefile      |    3 +-
 tests/virtio-test.c |   88 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+), 1 deletion(-)
 create mode 100644 tests/virtio-test.c

diff --git a/tests/Makefile b/tests/Makefile
index a98a848..fd0abf1 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -18,7 +18,7 @@ check-block-$(CONFIG_POSIX) += tests/qemu-iotests-quick.sh
 
 # All QTests for now are POSIX-only, but the dependencies are
 # really in libqtest, not in the testcases themselves.
-check-qtest-i386-y = tests/rtc-test
+check-qtest-i386-y = tests/rtc-test tests/virtio-test
 check-qtest-x86_64-y = $(check-qtest-i386-y)
 
 GENERATED_HEADERS += tests/test-qapi-types.h tests/test-qapi-visit.h 
tests/test-qmp-commands.h
@@ -64,6 +64,7 @@ tests/test-qmp-input-strict$(EXESUF): 
tests/test-qmp-input-strict.o $(test-qapi-
 tests/test-qmp-commands$(EXESUF): tests/test-qmp-commands.o 
tests/test-qmp-marshal.o $(test-qapi-obj-y)
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o $(trace-obj-y)
+tests/virtio-test$(EXESUF): tests/virtio-test.o tests/libpci.o $(trace-obj-y)
 
 # QTest rules
 
diff --git a/tests/virtio-test.c b/tests/virtio-test.c
new file mode 100644
index 0000000..7ed564e
--- /dev/null
+++ b/tests/virtio-test.c
@@ -0,0 +1,88 @@
+/*
+ * QTest testcase demo for virtio-pci devices
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Stefan Hajnoczi <address@hidden>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include <glib.h>
+#include <string.h>
+#include "bswap.h"
+#include "libpci.h"
+#include "hw/virtio-defs.h"
+#include "hw/virtio-pci-defs.h"
+#include "libqtest.h"
+
+enum {
+    /* Device address for this test */
+    TEST_PCI_SLOT = 5,
+    TEST_PCI_FUNC = 0,
+    TEST_BAR0_IOADDR = 0x1000,
+};
+
+static void virtio_probe(void)
+{
+    PciDevice dev;
+
+    if (!pci_probe(&dev, TEST_PCI_SLOT, TEST_PCI_FUNC)) {
+        g_test_message("Probe failed, no device present\n");
+        return;
+    }
+
+    /* "2.1 PCI Discovery" defines vendor/device IDs */
+    g_assert_cmpint(pci_config_readw(&dev, PCI_VENDOR_ID), ==, 0x1af4);
+    g_assert_cmpint(pci_config_readw(&dev, PCI_DEVICE_ID), ==, 0x1002);
+
+    /* "2.1 PCI Discovery" defines the revision ID */
+    g_assert_cmpint(pci_config_readb(&dev, PCI_REVISION_ID), ==, 0);
+
+    /* "2.1 PCI Discovery" defines the subsystem IDs */
+    g_assert_cmpint(pci_config_readw(&dev, PCI_SUBSYSTEM_ID), ==, 5);
+
+    pci_map_bar_io(&dev, PCI_BASE_ADDRESS_0, TEST_BAR0_IOADDR);
+    pci_enable(&dev);
+
+    g_test_message("host features: %#x\n",
+            le32_to_cpu(inl(TEST_BAR0_IOADDR + VIRTIO_PCI_HOST_FEATURES)));
+    g_test_message("status: %#x\n", inb(TEST_BAR0_IOADDR + VIRTIO_PCI_STATUS));
+
+    outl(TEST_BAR0_IOADDR + VIRTIO_PCI_STATUS,
+         VIRTIO_CONFIG_S_ACKNOWLEDGE | VIRTIO_CONFIG_S_DRIVER);
+}
+
+int main(int argc, char **argv)
+{
+    const char *arch;
+    QTestState *s = NULL;
+    int ret;
+
+    g_test_init(&argc, &argv, NULL);
+
+    arch = qtest_get_arch();
+    /* These tests only work on i386 and x86_64 */
+    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
+        gchar *args =
+            g_strdup_printf("-vnc none -device virtio-balloon-pci,addr=%u.%u",
+                            TEST_PCI_SLOT, TEST_PCI_FUNC);
+        s = qtest_start(args);
+        g_free(args);
+
+        qtest_add_func("/virtio/probe", virtio_probe);
+    } else {
+        g_test_message("Skipping unsupported arch `%s'\n", arch);
+    }
+
+    ret = g_test_run();
+
+    if (s) {
+        qtest_quit(s);
+    }
+
+    return ret;
+}
-- 
1.7.9.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]