[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 2/3] memory: add tests for the sparse-mem device
From: |
Alexander Bulekov |
Subject: |
[PATCH 2/3] memory: add tests for the sparse-mem device |
Date: |
Thu, 11 Mar 2021 00:36:13 -0500 |
Signed-off-by: Alexander Bulekov <alxndr@bu.edu>
---
MAINTAINERS | 1 +
tests/qtest/meson.build | 3 +-
tests/qtest/sparse-mem-test.c | 88 +++++++++++++++++++++++++++++++++++
3 files changed, 91 insertions(+), 1 deletion(-)
create mode 100644 tests/qtest/sparse-mem-test.c
diff --git a/MAINTAINERS b/MAINTAINERS
index 9e3d8b1401..88b35c0d23 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2619,6 +2619,7 @@ S: Maintained
F: tests/qtest/fuzz/
F: scripts/oss-fuzz/
F: hw/mem/sparse-mem.c
+F: tests/qtest/sparse-mem-test.c
F: docs/devel/fuzzing.rst
Register API
diff --git a/tests/qtest/meson.build b/tests/qtest/meson.build
index 58efc46144..21e3b55050 100644
--- a/tests/qtest/meson.build
+++ b/tests/qtest/meson.build
@@ -78,7 +78,8 @@ qtests_i386 = \
'vmgenid-test',
'migration-test',
'test-x86-cpuid-compat',
- 'numa-test']
+ 'numa-test',
+ 'sparse-mem-test']
dbus_daemon = find_program('dbus-daemon', required: false)
if dbus_daemon.found() and config_host.has_key('GDBUS_CODEGEN')
diff --git a/tests/qtest/sparse-mem-test.c b/tests/qtest/sparse-mem-test.c
new file mode 100644
index 0000000000..cb73976889
--- /dev/null
+++ b/tests/qtest/sparse-mem-test.c
@@ -0,0 +1,88 @@
+/*
+ * QTest testcases for the sparse memory device
+ *
+ * Copyright Red Hat Inc., 2021
+ *
+ * Authors:
+ * Alexander Bulekov <alxndr@bu.edu>
+ *
+ * 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 "qemu/osdep.h"
+
+#include "libqos/libqtest.h"
+
+static void test_sparse_memwrite(void)
+{
+ QTestState *s;
+ uint8_t *buf;
+ const int bufsize = 0x10000;
+
+ s = qtest_init("-device sparse-mem");
+
+ buf = malloc(bufsize);
+
+ for (int i = 0; i < bufsize; i++) {
+ buf[i] = (uint8_t)i;
+ }
+ qtest_memwrite(s, 0x100000000, buf, bufsize);
+ memset(buf, 0, bufsize);
+ qtest_memread(s, 0x100000000, buf, bufsize);
+
+ for (int i = 0; i < bufsize; i++) {
+ assert(buf[i] == (uint8_t)i);
+ }
+
+ free(buf);
+ qtest_quit(s);
+}
+
+static void test_sparse_int_writes(void)
+{
+ QTestState *s;
+ const int num_writes = 0x1000;
+
+ s = qtest_init("-device sparse-mem");
+
+ size_t addr = 0x10000000;
+ for (uint64_t i = 0; i < num_writes; i++) {
+ qtest_writeq(s, addr, i);
+ addr += sizeof(uint64_t);
+ }
+
+ addr = 0x10000000;
+ for (uint64_t i = 0; i < num_writes; i++) {
+ assert(qtest_readq(s, addr) == i);
+ addr += sizeof(uint64_t);
+ }
+
+ addr = 0x10000002;
+ for (uint64_t i = 0; i < num_writes; i++) {
+ qtest_writeq(s, addr, i);
+ addr += sizeof(uint64_t);
+ }
+
+ addr = 0x10000002;
+ for (uint64_t i = 0; i < num_writes; i++) {
+ assert(qtest_readq(s, addr) == i);
+ addr += sizeof(uint64_t);
+ }
+
+ qtest_quit(s);
+}
+
+int main(int argc, char **argv)
+{
+ const char *arch = qtest_get_arch();
+
+ g_test_init(&argc, &argv, NULL);
+
+ if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
+ qtest_add_func("sparse-mem/memwrite", test_sparse_memwrite);
+ qtest_add_func("sparse-mem/ints", test_sparse_int_writes);
+ }
+
+ return g_test_run();
+}
--
2.28.0