From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51246) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WX092-0006AN-C1 for qemu-devel@nongnu.org; Sun, 06 Apr 2014 23:22:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WX08x-0008It-D3 for qemu-devel@nongnu.org; Sun, 06 Apr 2014 23:22:48 -0400 Received: from mx1.redhat.com ([209.132.183.28]:6017) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WX08w-0008Ij-Nk for qemu-devel@nongnu.org; Sun, 06 Apr 2014 23:22:43 -0400 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s373Mgna025673 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sun, 6 Apr 2014 23:22:42 -0400 From: Juan Quintela Date: Mon, 7 Apr 2014 05:20:49 +0200 Message-Id: <1396840915-10384-32-git-send-email-quintela@redhat.com> In-Reply-To: <1396840915-10384-1-git-send-email-quintela@redhat.com> References: <1396840915-10384-1-git-send-email-quintela@redhat.com> Subject: [Qemu-devel] [PATCH 31/97] vmstate: Test for VMSTATE_ARRAY_BOOL_TEST List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org 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 --- 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