From: Juan Quintela <quintela@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Subject: [Qemu-devel] [PULL 02/10] Tests: QEMUSizedBuffer/QEMUBuffer
Date: Wed, 15 Oct 2014 10:24:56 +0200 [thread overview]
Message-ID: <1413361504-26396-3-git-send-email-quintela@redhat.com> (raw)
In-Reply-To: <1413361504-26396-1-git-send-email-quintela@redhat.com>
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
Modify some of tests/test-vmstate.c to use the in memory file based
on QEMUSizedBuffer to provide basic testing of QEMUSizedBuffer and
the associated memory backed QEMUFile type.
Only some of the tests are changed so that the fd backed QEMUFile is
still tested.
Signed-off-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Signed-off-by: Juan Quintela <quintela@redhat.com>
---
tests/Makefile | 2 +-
tests/test-vmstate.c | 74 +++++++++++++++++++++++++++-------------------------
2 files changed, 39 insertions(+), 37 deletions(-)
diff --git a/tests/Makefile b/tests/Makefile
index ffa8312..31e867f 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -259,7 +259,7 @@ tests/test-qdev-global-props$(EXESUF): tests/test-qdev-global-props.o \
libqemuutil.a libqemustub.a
tests/test-vmstate$(EXESUF): tests/test-vmstate.o \
vmstate.o qemu-file.o \
- libqemuutil.a
+ libqemuutil.a libqemustub.a
tests/test-qapi-types.c tests/test-qapi-types.h :\
$(SRC_PATH)/tests/qapi-schema/qapi-schema-test.json $(SRC_PATH)/scripts/qapi-types.py
diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index d72c64c..5e0fd13 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -43,6 +43,12 @@ void yield_until_fd_readable(int fd)
select(fd + 1, &fds, NULL, NULL, NULL);
}
+/*
+ * Some tests use 'open_test_file' to work on a real fd, some use
+ * an in memory file (QEMUSizedBuffer+qemu_bufopen); we could pick one
+ * but this way we test both.
+ */
+
/* Duplicate temp_fd and seek to the beginning of the file */
static QEMUFile *open_test_file(bool write)
{
@@ -54,6 +60,30 @@ static QEMUFile *open_test_file(bool write)
return qemu_fdopen(fd, write ? "wb" : "rb");
}
+/* Open a read-only qemu-file from an existing memory block */
+static QEMUFile *open_mem_file_read(const void *data, size_t len)
+{
+ /* The qsb gets freed by qemu_fclose */
+ QEMUSizedBuffer *qsb = qsb_create(data, len);
+ g_assert(qsb);
+
+ return qemu_bufopen("r", qsb);
+}
+
+/*
+ * Check that the contents of the memory-buffered file f match
+ * the given size/data.
+ */
+static void check_mem_file(QEMUFile *f, void *data, size_t size)
+{
+ uint8_t *result = g_malloc(size);
+ const QEMUSizedBuffer *qsb = qemu_buf_get(f);
+ g_assert_cmpint(qsb_get_length(qsb), ==, size);
+ g_assert_cmpint(qsb_get_buffer(qsb, 0, size, result), ==, size);
+ g_assert_cmpint(memcmp(result, data, size), ==, 0);
+ g_free(result);
+}
+
#define SUCCESS(val) \
g_assert_cmpint((val), ==, 0)
@@ -371,14 +401,12 @@ static const VMStateDescription vmstate_skipping = {
static void test_save_noskip(void)
{
- QEMUFile *fsave = open_test_file(true);
+ QEMUFile *fsave = qemu_bufopen("w", NULL);
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
.skip_c_e = false };
vmstate_save_state(fsave, &vmstate_skipping, &obj);
g_assert(!qemu_file_get_error(fsave));
- qemu_fclose(fsave);
- QEMUFile *loading = open_test_file(false);
uint8_t expected[] = {
0, 0, 0, 1, /* a */
0, 0, 0, 2, /* b */
@@ -387,52 +415,31 @@ static void test_save_noskip(void)
0, 0, 0, 5, /* e */
0, 0, 0, 0, 0, 0, 0, 6, /* f */
};
- uint8_t result[sizeof(expected)];
- g_assert_cmpint(qemu_get_buffer(loading, result, sizeof(result)), ==,
- sizeof(result));
- g_assert(!qemu_file_get_error(loading));
- g_assert_cmpint(memcmp(result, expected, sizeof(result)), ==, 0);
-
- /* Must reach EOF */
- qemu_get_byte(loading);
- g_assert_cmpint(qemu_file_get_error(loading), ==, -EIO);
-
- qemu_fclose(loading);
+ check_mem_file(fsave, expected, sizeof(expected));
+ qemu_fclose(fsave);
}
static void test_save_skip(void)
{
- QEMUFile *fsave = open_test_file(true);
+ QEMUFile *fsave = qemu_bufopen("w", NULL);
TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6,
.skip_c_e = true };
vmstate_save_state(fsave, &vmstate_skipping, &obj);
g_assert(!qemu_file_get_error(fsave));
- qemu_fclose(fsave);
- QEMUFile *loading = open_test_file(false);
uint8_t expected[] = {
0, 0, 0, 1, /* a */
0, 0, 0, 2, /* b */
0, 0, 0, 0, 0, 0, 0, 4, /* d */
0, 0, 0, 0, 0, 0, 0, 6, /* f */
};
- uint8_t result[sizeof(expected)];
- g_assert_cmpint(qemu_get_buffer(loading, result, sizeof(result)), ==,
- sizeof(result));
- g_assert(!qemu_file_get_error(loading));
- g_assert_cmpint(memcmp(result, expected, sizeof(result)), ==, 0);
-
-
- /* Must reach EOF */
- qemu_get_byte(loading);
- g_assert_cmpint(qemu_file_get_error(loading), ==, -EIO);
+ check_mem_file(fsave, expected, sizeof(expected));
- qemu_fclose(loading);
+ qemu_fclose(fsave);
}
static void test_load_noskip(void)
{
- QEMUFile *fsave = open_test_file(true);
uint8_t buf[] = {
0, 0, 0, 10, /* a */
0, 0, 0, 20, /* b */
@@ -442,10 +449,8 @@ static void test_load_noskip(void)
0, 0, 0, 0, 0, 0, 0, 60, /* f */
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
};
- qemu_put_buffer(fsave, buf, sizeof(buf));
- qemu_fclose(fsave);
- QEMUFile *loading = open_test_file(false);
+ QEMUFile *loading = open_mem_file_read(buf, sizeof(buf));
TestStruct obj = { .skip_c_e = false };
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
g_assert(!qemu_file_get_error(loading));
@@ -460,7 +465,6 @@ static void test_load_noskip(void)
static void test_load_skip(void)
{
- QEMUFile *fsave = open_test_file(true);
uint8_t buf[] = {
0, 0, 0, 10, /* a */
0, 0, 0, 20, /* b */
@@ -468,10 +472,8 @@ static void test_load_skip(void)
0, 0, 0, 0, 0, 0, 0, 60, /* f */
QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
};
- qemu_put_buffer(fsave, buf, sizeof(buf));
- qemu_fclose(fsave);
- QEMUFile *loading = open_test_file(false);
+ QEMUFile *loading = open_mem_file_read(buf, sizeof(buf));
TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 };
vmstate_load_state(loading, &vmstate_skipping, &obj, 2);
g_assert(!qemu_file_get_error(loading));
--
2.1.0
next prev parent reply other threads:[~2014-10-15 8:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-15 8:24 [Qemu-devel] [PULL 00/10] Migration pull request Juan Quintela
2014-10-15 8:24 ` [Qemu-devel] [PULL 01/10] QEMUSizedBuffer based QEMUFile Juan Quintela
2014-10-15 8:24 ` Juan Quintela [this message]
2014-10-15 8:24 ` [Qemu-devel] [PULL 03/10] block/migration: Disable cache invalidate for incoming migration Juan Quintela
2014-10-15 8:24 ` [Qemu-devel] [PULL 04/10] vmstate: Allow dynamic allocation for VBUFFER during migration Juan Quintela
2014-10-15 8:24 ` [Qemu-devel] [PULL 05/10] qemu-file: Add copyright header to qemu-file.c Juan Quintela
2014-10-15 8:25 ` [Qemu-devel] [PULL 06/10] qemu-file: Make qemu_file_is_writable() non-static Juan Quintela
2014-10-15 8:25 ` [Qemu-devel] [PULL 07/10] qemu-file: Use qemu_file_is_writable() on stdio_fclose() Juan Quintela
2014-10-15 8:25 ` [Qemu-devel] [PULL 08/10] qemu-file: Move unix and socket implementations to qemu-file-unix.c Juan Quintela
2014-10-15 8:25 ` [Qemu-devel] [PULL 09/10] qemu-file: Move stdio implementation to qemu-file-stdio.c Juan Quintela
2014-10-15 8:25 ` [Qemu-devel] [PULL 10/10] migration: catch unknown flag combinations in ram_load Juan Quintela
2014-10-15 12:37 ` [Qemu-devel] [PULL 00/10] Migration pull request Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1413361504-26396-3-git-send-email-quintela@redhat.com \
--to=quintela@redhat.com \
--cc=dgilbert@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).