qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH 31/97] vmstate: Test for VMSTATE_ARRAY_BOOL_TEST


From: Juan Quintela
Subject: [Qemu-devel] [PATCH 31/97] vmstate: Test for VMSTATE_ARRAY_BOOL_TEST
Date: Mon, 7 Apr 2014 05:20:49 +0200

We add all the boilerplate code for testing arrays.
We change only user of VMSTATE_ARRAY_BOOL_V to VMSTATE_ARRAY_BOOL_TEST.

Signed-off-by: Juan Quintela <address@hidden>
---
 hw/audio/hda-codec.c        |   3 +-
 include/migration/vmstate.h |   8 +--
 tests/test-vmstate.c        | 171 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 177 insertions(+), 5 deletions(-)

diff --git a/hw/audio/hda-codec.c b/hw/audio/hda-codec.c
index 09559ef..1c1a138 100644
--- a/hw/audio/hda-codec.c
+++ b/hw/audio/hda-codec.c
@@ -603,7 +603,8 @@ static const VMStateDescription vmstate_hda_audio = {
                              vmstate_hda_audio_stream,
                              HDAAudioStream),
         VMSTATE_BOOL_ARRAY(running_compat, HDAAudioState, 16),
-        VMSTATE_BOOL_ARRAY_V(running_real, HDAAudioState, 2 * 16, 2),
+        VMSTATE_BOOL_ARRAY_TEST(running_real, HDAAudioState, 2 * 16,
+                                vmstate_2_plus),
         VMSTATE_END_OF_LIST()
     }
 };
diff --git a/include/migration/vmstate.h b/include/migration/vmstate.h
index 956a358..ac5a939 100644
--- a/include/migration/vmstate.h
+++ b/include/migration/vmstate.h
@@ -600,11 +600,11 @@ extern const VMStateInfo vmstate_info_bitmap;
 #define VMSTATE_TIMER_ARRAY(_f, _s, _n)                               \
     VMSTATE_ARRAY_OF_POINTER(_f, _s, _n, 0, vmstate_info_timer, QEMUTimer *)

-#define VMSTATE_BOOL_ARRAY_V(_f, _s, _n, _v)                         \
-    VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_bool, bool)
+#define VMSTATE_BOOL_ARRAY_TEST(_f, _s, _n, _t)                       \
+    VMSTATE_ARRAY_TEST(_f, _s, _n, _t, vmstate_info_bool, bool)

-#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                               \
-    VMSTATE_BOOL_ARRAY_V(_f, _s, _n, 0)
+#define VMSTATE_BOOL_ARRAY(_f, _s, _n)                                \
+    VMSTATE_BOOL_ARRAY_TEST(_f, _s, _n, NULL)

 #define VMSTATE_UINT16_ARRAY_V(_f, _s, _n, _v)                         \
     VMSTATE_ARRAY(_f, _s, _n, _v, vmstate_info_uint16, uint16_t)
diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index 846ed39..656e563 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -534,6 +534,175 @@ static void test_simple_bitmap(void)

 #undef FIELD_ASSERT

+#define VMSTATE_ARRAY_SIZE 5
+
+typedef struct TestArray {
+    int32_t  size;
+    bool     b_1[VMSTATE_ARRAY_SIZE];
+    bool     b_2[VMSTATE_ARRAY_SIZE];
+} TestArray;
+
+TestArray obj_array = {
+    .size = VMSTATE_ARRAY_SIZE,
+    .b_1 = { false, true, false, true, false},
+    .b_2 = { true, false, true, false, true},
+};
+
+static const VMStateDescription vmstate_array_primitive = {
+    .name = "array/primitive",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_INT32(size, TestArray),
+        VMSTATE_BOOL_ARRAY(b_1, TestArray, VMSTATE_ARRAY_SIZE),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+uint8_t wire_array_primitive[] = {
+    /* size */  0x00, 0x00, 0x00, 0x05,
+    /* b_1 */   0x00, 0x01, 0x00, 0x01, 0x00,
+    QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
+};
+
+static void test_array_primitive(void)
+{
+    QEMUFile *fsave = open_test_file(true);
+    int i;
+
+    /* Save file with vmstate */
+    vmstate_save_state(fsave, &vmstate_array_primitive, &obj_array);
+    g_assert(!qemu_file_get_error(fsave));
+    qemu_fclose(fsave);
+
+    QEMUFile *loading = open_test_file(false);
+    /* we don't need QEMU_VM_EOF */
+    uint8_t result[sizeof(wire_array_primitive)-1];
+
+    /* read back as binary */
+
+    g_assert_cmpint(qemu_get_buffer(loading, result, sizeof(result)), ==,
+                    sizeof(result));
+    g_assert(!qemu_file_get_error(loading));
+
+    /* Compare that what is on the file is the same that what we
+       expected to be there */
+    SUCCESS(memcmp(result, wire_array_primitive, sizeof(result)));
+
+    /* Must reach EOF */
+    qemu_get_byte(loading);
+    g_assert_cmpint(qemu_file_get_error(loading), ==, -EIO);
+
+    qemu_fclose(loading);
+
+    /* We save the file again.  We want the EOF this time */
+
+    fsave = open_test_file(true);
+    qemu_put_buffer(fsave, wire_array_primitive, sizeof(wire_array_primitive));
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    TestArray obj;
+    SUCCESS(vmstate_load_state(loading, &vmstate_array_primitive, &obj, 1));
+    g_assert(!qemu_file_get_error(loading));
+    qemu_fclose(loading);
+
+#define FIELD_ASSERT(name)   g_assert_cmpint(obj.name, ==, obj_array.name)
+#define ELEM_ASSERT(name, i) \
+    g_assert_cmpint(obj.name[i], ==, obj_array.name[i])
+
+    FIELD_ASSERT(size);
+    for (i = 0; i < VMSTATE_ARRAY_SIZE; i++) {
+        ELEM_ASSERT(b_1, i);
+    }
+
+    /* We save the file again.  We want the EOF this time */
+
+    fsave = open_test_file(true);
+    /* this time we don't write the whole file */
+    qemu_put_buffer(fsave, wire_array_primitive, 4);
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    FAILURE(vmstate_load_state(loading, &vmstate_array_primitive, &obj, 1));
+
+    /* Now it has to have an error */
+    g_assert(qemu_file_get_error(loading));
+    qemu_fclose(loading);
+}
+
+static const VMStateDescription vmstate_array_test = {
+    .name = "array/test",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_INT32(size, TestArray),
+        VMSTATE_BOOL_ARRAY_TEST(b_1, TestArray, VMSTATE_ARRAY_SIZE, test_true),
+        VMSTATE_BOOL_ARRAY_TEST(b_2, TestArray, VMSTATE_ARRAY_SIZE, 
test_false),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+uint8_t wire_array_test[] = {
+    /* size */  0x00, 0x00, 0x00, 0x05,
+    /* b_1 */   0x00, 0x01, 0x00, 0x01, 0x00,
+    QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
+};
+
+static void test_array_test(void)
+{
+    QEMUFile *fsave = open_test_file(true);
+    int i;
+
+    /* Save file with vmstate */
+    vmstate_save_state(fsave, &vmstate_array_test, &obj_array);
+    g_assert(!qemu_file_get_error(fsave));
+    qemu_fclose(fsave);
+
+    QEMUFile *loading = open_test_file(false);
+    /* we don't need QEMU_VM_EOF */
+    uint8_t result[sizeof(wire_array_test)-1];
+
+    /* read back as binary */
+
+    g_assert_cmpint(qemu_get_buffer(loading, result, sizeof(result)), ==,
+                    sizeof(result));
+    g_assert(!qemu_file_get_error(loading));
+
+    /* Compare that what is on the file is the same that what we
+       expected to be there */
+    SUCCESS(memcmp(result, wire_array_test, sizeof(result)));
+
+    /* Must reach EOF */
+    qemu_get_byte(loading);
+    g_assert_cmpint(qemu_file_get_error(loading), ==, -EIO);
+
+    qemu_fclose(loading);
+
+    /* We save the file again.  We want the EOF this time */
+
+    fsave = open_test_file(true);
+    qemu_put_buffer(fsave, wire_array_test, sizeof(wire_array_test));
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    TestArray obj;
+    SUCCESS(vmstate_load_state(loading, &vmstate_array_test, &obj, 1));
+    g_assert(!qemu_file_get_error(loading));
+
+    qemu_fclose(loading);
+
+    FIELD_ASSERT(size);
+    for (i = 0; i < VMSTATE_ARRAY_SIZE; i++) {
+        ELEM_ASSERT(b_1, i);
+    }
+}
+#undef FIELD_ASSERT
+#undef ELEM_ASSERT
+
+
 typedef struct TestStruct {
     uint32_t a, b, c, e;
     uint64_t d, f;
@@ -755,6 +924,8 @@ int main(int argc, char **argv)
     g_test_add_func("/vmstate/simple/test", test_simple_test);
     g_test_add_func("/vmstate/simple/compare", test_simple_compare);
     g_test_add_func("/vmstate/simple/bitmap", test_simple_bitmap);
+    g_test_add_func("/vmstate/array/primitive", test_array_primitive);
+    g_test_add_func("/vmstate/array/test", test_array_test);
     g_test_add_func("/vmstate/versioned/load/v1", test_load_v1);
     g_test_add_func("/vmstate/versioned/load/v2", test_load_v2);
     g_test_add_func("/vmstate/field_exists/load/noskip", test_load_noskip);
-- 
1.9.0




reply via email to

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