From patchwork Mon Apr 21 14:41:43 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 341078 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 522D61400EF for ; Tue, 22 Apr 2014 13:18:22 +1000 (EST) Received: from localhost ([::1]:49719 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcGAw-00059E-7C for incoming@patchwork.ozlabs.org; Mon, 21 Apr 2014 11:30:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60036) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFSb-00075w-Gw for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:47 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WcFSU-0004as-Um for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48525) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFSU-0004ac-Lo for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:44:34 -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 s3LEiXdJ002824 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 21 Apr 2014 10:44:33 -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 s3LEfi89019220; Mon, 21 Apr 2014 10:44:32 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Mon, 21 Apr 2014 16:41:43 +0200 Message-Id: <1398091304-10677-124-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 123/124] vmstate: Test for VMSTATE_ARRAY_OF_POINTER_TO_STRUCT 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 | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index fa69209..ca1f6e2 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -1916,6 +1916,110 @@ static void test_struct_varray(void) } } } + +typedef struct TestStruct3 { + SubStruct *p_1[3]; +} TestStruct3; + +/* + * size: needs to be the 1st field + * a: needs to be the last field + * We use that fact to load with different sizes + */ + +static const VMStateDescription vmstate_struct_pointer = { + .name = "struct/pointer", + .version_id = 1, + .minimum_version_id = 1, + .minimum_version_id_old = 1, + .fields = (VMStateField[]) { + VMSTATE_ARRAY_OF_POINTER_TO_STRUCT(p_1, TestStruct3, 3, + vmstate_sub_struct, SubStruct), + VMSTATE_END_OF_LIST() + } +}; + +/* This is the binary representation on the wire of that struct */ +/* We need to change the first byte for load to work */ +uint8_t wire_struct_pointer[] = { + /* p_1[0] */ + /* i32 */ 0x00, 0x00, 0x00, 0x15, + /* i64 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, + /* buffer */ 0x62, 0x79, 0x65, 0x30, 0x20, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x21, 0x00, + /* p_1[1] */ + /* i32 */ 0x00, 0x00, 0x00, 0x16, + /* i64 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, + /* buffer */ 0x62, 0x79, 0x65, 0x31, 0x20, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x21, 0x00, + /* p_1[2] */ + /* i32 */ 0x00, 0x00, 0x00, 0x17, + /* i64 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, + /* buffer */ 0x62, 0x79, 0x65, 0x32, 0x20, 0x20, 0x77, 0x6f, + 0x72, 0x6c, 0x64, 0x21, 0x00, + QEMU_VM_EOF, /* just to ensure we won't get EOF reported prematurely */ +}; + +static void obj_struct3_copy(void *arg1, void *arg2) +{ + TestStruct3 *target = arg1; + TestStruct3 *source = arg2; + int i; + + for (i = 0; i < 3; i++) { + target->p_1[i]->i32 = source->p_1[i]->i32; + target->p_1[i]->i64 = source->p_1[i]->i64; + memcpy(target->p_1[i]->buffer, source->p_1[i]->buffer, 13); + } +} + +static TestStruct3 *create_struct3(void) +{ + TestStruct3 *obj = g_malloc0(sizeof(*obj)); + int i; + + for (i = 0; i < 3; i++) { + obj->p_1[i] = g_malloc0(sizeof(SubStruct)); + } + + return obj; +} + +static TestStruct3 *create_struct3_init(void) +{ + TestStruct3 *obj = create_struct3(); + int i; + + for (i = 0; i < 3; i++) { + obj->p_1[i]->i32 = i + 21; + obj->p_1[i]->i64 = i + 31; + snprintf((char *)obj->p_1[i]->buffer, 13, "bye%d world!", i); + } + + return obj; +} + +static void test_struct_pointer(void) +{ + TestStruct3 *obj, *obj_clone, *obj_struct3; + int i; + + obj_struct3 = create_struct3_init(); + obj = create_struct3(); + obj_clone = create_struct3(); + + save_vmstate(&vmstate_struct_pointer, obj_struct3); + + compare_vmstate(wire_struct_pointer, sizeof(wire_struct_pointer)); + + SUCCESS(load_vmstate(&vmstate_struct_pointer, obj, obj_clone, + obj_struct3_copy, 1, wire_struct_pointer, + sizeof(wire_struct_pointer))); + + for (i = 0; i < 3; i++) { + STRUCT_EQUAL(obj->p_1[i], obj_struct3->p_1[i]); + } +} #undef STRUCT_EQUAL #undef STRUCT_NOT_EQUAL @@ -2164,6 +2268,7 @@ int main(int argc, char **argv) g_test_add_func("/vmstate/struct/simple", test_struct_simple); g_test_add_func("/vmstate/struct/test", test_struct_test); g_test_add_func("/vmstate/struct/varray", test_struct_varray); + g_test_add_func("/vmstate/struct/pointer", test_struct_pointer); g_test_run(); close(temp_fd);