From patchwork Mon Apr 21 14:39:49 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Juan Quintela X-Patchwork-Id: 341084 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 AE143140078 for ; Tue, 22 Apr 2014 13:18:40 +1000 (EST) Received: from localhost ([::1]:49275 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFYJ-0007R7-Pw for incoming@patchwork.ozlabs.org; Mon, 21 Apr 2014 10:50:35 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:58302) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFQ6-00034I-Rb for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:42:13 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WcFQ0-0003so-LH for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:42:06 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63981) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WcFQ0-0003sb-DS for qemu-devel@nongnu.org; Mon, 21 Apr 2014 10:42:00 -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 s3LEfxKj001819 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Mon, 21 Apr 2014 10:41:59 -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 s3LEfi6F019220; Mon, 21 Apr 2014 10:41:58 -0400 From: Juan Quintela To: qemu-devel@nongnu.org Date: Mon, 21 Apr 2014 16:39:49 +0200 Message-Id: <1398091304-10677-10-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 009/124] vmstate: Refactor opening of files 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 Reviewed-by: Dr. David Alan Gilbert Reviewed-by: Amit Shah --- tests/test-vmstate.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tests/test-vmstate.c b/tests/test-vmstate.c index 30cc721..8b242c4 100644 --- a/tests/test-vmstate.c +++ b/tests/test-vmstate.c @@ -44,14 +44,14 @@ void yield_until_fd_readable(int fd) } /* Duplicate temp_fd and seek to the beginning of the file */ -static int dup_temp_fd(bool truncate) +static QEMUFile *open_test_file(bool write) { int fd = dup(temp_fd); lseek(fd, 0, SEEK_SET); - if (truncate) { + if (write) { g_assert_cmpint(ftruncate(fd, 0), ==, 0); } - return fd; + return qemu_fdopen(fd, write ? "wb" : "rb"); } typedef struct TestSruct { @@ -76,13 +76,13 @@ static const VMStateDescription vmstate_simple = { static void test_simple_save(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4 }; vmstate_save_state(fsave, &vmstate_simple, &obj); g_assert(!qemu_file_get_error(fsave)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); uint8_t expected[] = { 0, 0, 0, 1, /* a */ 0, 0, 0, 2, /* b */ @@ -104,7 +104,7 @@ static void test_simple_save(void) static void test_simple_load(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); uint8_t buf[] = { 0, 0, 0, 10, /* a */ 0, 0, 0, 20, /* b */ @@ -115,7 +115,7 @@ static void test_simple_load(void) qemu_put_buffer(fsave, buf, sizeof(buf)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); TestStruct obj; vmstate_load_state(loading, &vmstate_simple, &obj, 1); g_assert(!qemu_file_get_error(loading)); @@ -145,7 +145,7 @@ static const VMStateDescription vmstate_versioned = { static void test_load_v1(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); uint8_t buf[] = { 0, 0, 0, 10, /* a */ 0, 0, 0, 30, /* c */ @@ -155,7 +155,7 @@ static void test_load_v1(void) qemu_put_buffer(fsave, buf, sizeof(buf)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); TestStruct obj = { .b = 200, .e = 500, .f = 600 }; vmstate_load_state(loading, &vmstate_versioned, &obj, 1); g_assert(!qemu_file_get_error(loading)); @@ -170,7 +170,7 @@ static void test_load_v1(void) static void test_load_v2(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); uint8_t buf[] = { 0, 0, 0, 10, /* a */ 0, 0, 0, 20, /* b */ @@ -183,7 +183,7 @@ static void test_load_v2(void) qemu_put_buffer(fsave, buf, sizeof(buf)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); TestStruct obj; vmstate_load_state(loading, &vmstate_versioned, &obj, 2); g_assert_cmpint(obj.a, ==, 10); @@ -219,14 +219,14 @@ static const VMStateDescription vmstate_skipping = { static void test_save_noskip(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6, .skip_c_e = false }; vmstate_save_state(fsave, &vmstate_skipping, &obj); g_assert(!qemu_file_get_error(fsave)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); uint8_t expected[] = { 0, 0, 0, 1, /* a */ 0, 0, 0, 2, /* b */ @@ -250,14 +250,14 @@ static void test_save_noskip(void) static void test_save_skip(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); TestStruct obj = { .a = 1, .b = 2, .c = 3, .d = 4, .e = 5, .f = 6, .skip_c_e = true }; vmstate_save_state(fsave, &vmstate_skipping, &obj); g_assert(!qemu_file_get_error(fsave)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); uint8_t expected[] = { 0, 0, 0, 1, /* a */ 0, 0, 0, 2, /* b */ @@ -280,7 +280,7 @@ static void test_save_skip(void) static void test_load_noskip(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); uint8_t buf[] = { 0, 0, 0, 10, /* a */ 0, 0, 0, 20, /* b */ @@ -293,7 +293,7 @@ static void test_load_noskip(void) qemu_put_buffer(fsave, buf, sizeof(buf)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); TestStruct obj = { .skip_c_e = false }; vmstate_load_state(loading, &vmstate_skipping, &obj, 2); g_assert(!qemu_file_get_error(loading)); @@ -308,7 +308,7 @@ static void test_load_noskip(void) static void test_load_skip(void) { - QEMUFile *fsave = qemu_fdopen(dup_temp_fd(true), "wb"); + QEMUFile *fsave = open_test_file(true); uint8_t buf[] = { 0, 0, 0, 10, /* a */ 0, 0, 0, 20, /* b */ @@ -319,7 +319,7 @@ static void test_load_skip(void) qemu_put_buffer(fsave, buf, sizeof(buf)); qemu_fclose(fsave); - QEMUFile *loading = qemu_fdopen(dup_temp_fd(false), "rb"); + QEMUFile *loading = open_test_file(false); TestStruct obj = { .skip_c_e = true, .c = 300, .e = 500 }; vmstate_load_state(loading, &vmstate_skipping, &obj, 2); g_assert(!qemu_file_get_error(loading));