Signed-off-by: Juan Quintela <quint...@redhat.com>
---
 tests/test-vmstate.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index a087007..2fbcc02 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -949,10 +949,78 @@ static void test_array_sub(void)
         ELEM_NOT_ASSERT(u32_2, i);
     }
 }
-#undef FIELD_ASSERT
 #undef ELEM_ASSERT
 #undef ELEM_NOT_ASSERT

+typedef struct TestBuffer {
+    uint8_t  buffer[6];
+} TestBuffer;
+
+TestBuffer obj_buffer = {
+    .buffer = "hello",
+};
+
+static const VMStateDescription vmstate_buffer_simple = {
+    .name = "array/test",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_BUFFER(buffer, TestBuffer),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+uint8_t wire_buffer_simple[] = {
+    /* buffer */       0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00,
+    QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
+};
+
+static void test_buffer_simple(void)
+{
+    QEMUFile *fsave = open_test_file(true);
+
+    /* Save file with vmstate */
+    vmstate_save_state(fsave, &vmstate_buffer_simple, &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_simple)-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_simple, 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_simple, sizeof(wire_buffer_simple));
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    TestBuffer obj;
+    SUCCESS(vmstate_load_state(loading, &vmstate_buffer_simple, &obj, 1));
+    g_assert(!qemu_file_get_error(loading));
+
+    qemu_fclose(loading);
+    SUCCESS(memcmp(obj.buffer, obj_buffer.buffer, sizeof(obj.buffer)));
+}
+#undef FIELD_ASSERT
+

 typedef struct TestStruct {
     uint32_t a, b, c, e;
@@ -1178,6 +1246,7 @@ int main(int argc, char **argv)
     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/array/sub", test_array_sub);
+    g_test_add_func("/vmstate/buffer/simple", test_buffer_simple);
     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