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

diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c
index b3d77a2..49e98a8 100644
--- a/tests/test-vmstate.c
+++ b/tests/test-vmstate.c
@@ -864,6 +864,81 @@ static void test_array_test(void)
         ELEM_NOT_ASSERT(i32_2, i);
     }
 }
+
+static const VMStateDescription vmstate_array_sub = {
+    .name = "array/test",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .minimum_version_id_old = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT8_SUB_ARRAY(u8_1, TestArray, 0, VMSTATE_ARRAY_SIZE),
+        VMSTATE_UINT8_SUB_ARRAY(u8_2, TestArray, 2 , 2),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+uint8_t wire_array_sub[] = {
+    /* u8_1 */         0x01, 0x02, 0x03, 0x04, 0x05,
+    /* u8_2 {3,4} */               0x03, 0x02,
+    QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */
+};
+
+static void test_array_sub(void)
+{
+    QEMUFile *fsave = open_test_file(true);
+    int i;
+
+    /* Save file with vmstate */
+    vmstate_save_state(fsave, &vmstate_array_sub, &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_sub)-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_sub, 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_sub, sizeof(wire_array_sub));
+    qemu_fclose(fsave);
+
+    loading = open_test_file(false);
+    TestArray obj;
+    SUCCESS(vmstate_load_state(loading, &vmstate_array_sub, &obj, 1));
+    g_assert(!qemu_file_get_error(loading));
+
+    qemu_fclose(loading);
+
+    for (i = 0; i < VMSTATE_ARRAY_SIZE; i++) {
+        ELEM_ASSERT(u8_1, i);
+    }
+    for (i = 0; i < 2; i++) {
+        ELEM_NOT_ASSERT(u8_2, i);
+    }
+    for (i = 2; i < 4; i++) {
+        ELEM_ASSERT(u8_2, i);
+    }
+    for (i = 4; i < VMSTATE_ARRAY_SIZE; i++) {
+        ELEM_NOT_ASSERT(u8_2, i);
+    }
+}
 #undef FIELD_ASSERT
 #undef ELEM_ASSERT
 #undef ELEM_NOT_ASSERT
@@ -1092,6 +1167,7 @@ int main(int argc, char **argv)
     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/array/sub", test_array_sub);
     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