qemu-devel
[Top][All Lists]
Advanced

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

[Qemu-devel] [PATCH 88/97] vmstate: Add tests for VMSTATE_BUFFER_TEST


From: Juan Quintela
Subject: [Qemu-devel] [PATCH 88/97] vmstate: Add tests for VMSTATE_BUFFER_TEST
Date: Mon, 7 Apr 2014 05:21:46 +0200

Signed-off-by: Juan Quintela <address@hidden>
---
 tests/test-vmstate.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index 5609ff0..afcfbde 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -1031,6 +1031,68 @@ static void test_buffer_simple(void)
     FAILURE(memcmp(obj.middle, obj_buffer.middle, 6));
     SUCCESS(memcmp(obj.middle+6, obj_buffer.middle+6, sizeof(obj.middle)-6));
 }
+
+static const VMStateDescription vmstate_buffer_test = {
+    .name = "buffer/test",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER_TEST(buffer, TestBuffer, test_true),
+        VMSTATE_BUFFER_TEST(partial, TestBuffer, test_false),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+uint8_t wire_buffer_test[] = {
+    /* buffer */       0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00,
+    QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
+};
+
+static void test_buffer_test(void)
+{
+    QEMUFile *fsave = open_test_file(true);
+
+    /* Save file with vmstate */
+    vmstate_save_state(fsave, &vmstate_buffer_test, &obj_buffer);
+    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_buffer_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_buffer_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_buffer_test, sizeof(wire_buffer_test));
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    TestBuffer obj;
+    SUCCESS(vmstate_load_state(loading, &vmstate_buffer_test, &obj, 1));
+    g_assert(!qemu_file_get_error(loading));
+
+    qemu_fclose(loading);
+    SUCCESS(memcmp(obj.buffer, obj_buffer.buffer, sizeof(obj.buffer)));
+    FAILURE(memcmp(obj.partial, obj_buffer.partial, sizeof(obj.partial)));
+}
 #undef FIELD_ASSERT


@@ -1259,6 +1321,7 @@ int main(int argc, char **argv)
     g_test_add_func("/vmstate/array/test", test_array_test);
     g_test_add_func("/vmstate/array/sub", test_array_sub);
     g_test_add_func("/vmstate/buffer/simple", test_buffer_simple);
+    g_test_add_func("/vmstate/buffer/test", test_buffer_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]