From patchwork Mon Apr 21 14:41:33 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 340998 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 8E08B1400E0 for ; Tue, 22 Apr 2014 13:13:38 +1000 (EST) Received: from localhost ([::1]:49805 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcGHL-0004Jm-7L for incoming@patchwork.ozlabs.org; Mon, 21 Apr 2014 11:37:07 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59899) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFSN-0006t3-OV for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WcFSH-0004XY-Je for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:27 -0400 Received: from mx1.redhat.com ([209.132.183.28]:23119) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFSH-0004XM-Bv for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:21 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s3LEiJhb032007 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 21 Apr 2014 10:44:20 -0400 Received: from trasno.mitica (ovpn-116-41.ams2.redhat.com [10.36.116.41]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id s3LEfi7x019220; Mon, 21 Apr 2014 10:44:18 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Mon, 21 Apr 2014 16:41:33 +0200 Message-Id: <1398091304-10677-114-git-send-email-quintela@redhat.com> In-Reply-To: <1398091304-10677-1-git-send-email-quintela@redhat.com> References: <1398091304-10677-1-git-send-email-quintela@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH 113/124] vmstate: Test for VMSTATE_STRUCT{_TEST} X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Juan Quintela --- tests/test-vmstate.c | 148 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index 7e69b14..93537f7 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -1393,6 +1393,152 @@ static void test_varray_test(void) #undef ELEM_EQUAL #undef ELEM_NOTEQUAL +typedef struct SubStruct { + int32_t i32; + int64_t i64; + uint8_t buffer[13]; +} SubStruct; + +static const VMStateDescription vmstate_sub_struct = { + .name = "struct/substruct", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_INT32(i32, SubStruct), + VMSTATE_INT64(i64, SubStruct), + VMSTATE_BUFFER(buffer, SubStruct), + VMSTATE_END_OF_LIST() + } +}; + +typedef struct TestStruct { + SubStruct sub; + SubStruct sub2; +} TestStruct; + +static const VMStateDescription vmstate_struct_simple = { + .name = "struct/simple", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_STRUCT(sub, TestStruct, vmstate_sub_struct, SubStruct), + VMSTATE_END_OF_LIST() + } +}; + +TestStruct obj_struct = { + .sub = { + .i32 = 33, + .i64 = 77, + .buffer = "hello world!", + }, + .sub2 = { + .i32 = 66, + .i64 = 99, + .buffer = "bye world!", + }, +}; + +/* This is the binary representation on the wire of that struct */ +uint8_t wire_struct_simple[] = { + /* i32 */ 0x00, 0x00, 0x00, 0x21, + /* i64 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4d, + /* buffer */ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x21, 0x00, + QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ +}; + +static void obj_struct_copy(void *arg1, void *arg2) +{ + TestStruct *target = arg1; + TestStruct *source = arg2; + + target->sub.i32 = source->sub.i32; + target->sub.i64 = source->sub.i64; + memcpy(target->sub.buffer, source->sub.buffer, 13); + + target->sub2.i32 = source->sub2.i32; + target->sub2.i64 = source->sub2.i64; + memcpy(target->sub2.buffer, source->sub2.buffer, 13); +} + +static void test_struct_simple(void) +{ + TestStruct obj, obj_clone; + + + memset(&obj, 0, sizeof(obj)); + + save_vmstate(&vmstate_struct_simple, &obj_struct); + + compare_vmstate(wire_struct_simple, sizeof(wire_struct_simple)); + + SUCCESS(load_vmstate(&vmstate_struct_simple, &obj, &obj_clone, + obj_struct_copy, 1, wire_struct_simple, + sizeof(wire_struct_simple))); + +#define STRUCT_EQUAL(s1, s2) \ + do { \ + g_assert_cmpint(s1->i32, ==, s2->i32); \ + g_assert_cmpint(s1->i64, ==, s2->i64); \ + SUCCESS(memcmp(s1->buffer, s2->buffer, \ + sizeof(s1->buffer))); \ + } while (0) + +#define STRUCT_NOT_EQUAL(s1, s2) \ + do { \ + g_assert_cmpint(s1->i32, !=, s2->i32); \ + g_assert_cmpint(s1->i64, !=, s2->i64); \ + FAILURE(memcmp(s1->buffer, s2->buffer, \ + sizeof(s1->buffer))); \ + } while (0) + + STRUCT_EQUAL((&obj.sub), (&obj_struct.sub)); + STRUCT_NOT_EQUAL((&obj.sub2), (&obj_struct.sub2)); +} + +static const VMStateDescription vmstate_struct_test = { + .name = "struct/test", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_STRUCT(sub, TestStruct, vmstate_sub_struct, SubStruct), + VMSTATE_END_OF_LIST() + } +}; + +/* This is the binary representation on the wire of that struct */ +uint8_t wire_struct_test[] = { + /* i32 */ 0x00, 0x00, 0x00, 0x21, + /* i64 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4d, + /* buffer */ 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x21, 0x00, + QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ +}; + +static void test_struct_test(void) +{ + TestStruct obj, obj_clone; + + memset(&obj, 0, sizeof(obj)); + + save_vmstate(&vmstate_struct_test, &obj_struct); + + compare_vmstate(wire_struct_test, sizeof(wire_struct_test)); + + SUCCESS(load_vmstate(&vmstate_struct_test, &obj, &obj_clone, + obj_struct_copy, 1, wire_struct_test, + sizeof(wire_struct_test))); + + STRUCT_EQUAL((&obj.sub), (&obj_struct.sub)); + STRUCT_NOT_EQUAL((&obj.sub2), (&obj_struct.sub2)); +} +#undef STRUCT_EQUAL +#undef STRUCT_NOT_EQUAL + typedef struct TestVersioned { uint32_t a, b, c, e; uint64_t d, f; @@ -1635,6 +1781,8 @@ int main(int argc, char **argv) g_test_add_func("/vmstate/pointer/simple", test_pointer_simple); g_test_add_func("/vmstate/varray/simple", test_varray_simple); g_test_add_func("/vmstate/varray/test", test_varray_test); + g_test_add_func("/vmstate/struct/simple", test_struct_simple); + g_test_add_func("/vmstate/struct/test", test_struct_test); g_test_run(); close(temp_fd);