From patchwork Thu Aug 9 10:12:31 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wayne Xia X-Patchwork-Id: 176047 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)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 267F12C00C8 for ; Thu, 9 Aug 2012 20:13:18 +1000 (EST) Received: from localhost ([::1]:55366 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SzPjw-0005CN-5S for incoming@patchwork.ozlabs.org; Thu, 09 Aug 2012 06:13:16 -0400 Received: from eggs.gnu.org ([208.118.235.92]:55373) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SzPjU-0004Bb-Gx for qemu-devel@nongnu.org; Thu, 09 Aug 2012 06:12:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SzPjQ-0007Ec-1K for qemu-devel@nongnu.org; Thu, 09 Aug 2012 06:12:48 -0400 Received: from e28smtp02.in.ibm.com ([122.248.162.2]:51756) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SzPjP-0007EF-2o for qemu-devel@nongnu.org; Thu, 09 Aug 2012 06:12:43 -0400 Received: from /spool/local by e28smtp02.in.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 9 Aug 2012 15:42:40 +0530 Received: from d28relay02.in.ibm.com (9.184.220.59) by e28smtp02.in.ibm.com (192.168.1.132) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 9 Aug 2012 15:42:38 +0530 Received: from d28av02.in.ibm.com (d28av02.in.ibm.com [9.184.220.64]) by d28relay02.in.ibm.com (8.13.8/8.13.8/NCO v10.0) with ESMTP id q79ACbAT29098064 for ; Thu, 9 Aug 2012 15:42:37 +0530 Received: from d28av02.in.ibm.com (loopback [127.0.0.1]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVout) with ESMTP id q79ACbkE002205 for ; Thu, 9 Aug 2012 20:12:37 +1000 Received: from RedHat62GAWSWenchao (wenchaox.cn.ibm.com [9.115.122.25]) by d28av02.in.ibm.com (8.14.4/8.13.1/NCO v10.0 AVin) with ESMTP id q79ACYI9002039; Thu, 9 Aug 2012 20:12:36 +1000 From: Wenchao Xia To: qemu-devel@nongnu.org Date: Thu, 9 Aug 2012 18:12:31 +0800 Message-Id: <1344507151-11217-1-git-send-email-xiawenc@linux.vnet.ibm.com> X-Mailer: git-send-email 1.7.1 x-cbid: 12080910-5816-0000-0000-000003ED2713 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 122.248.162.2 Cc: stefanha@gmail.com, aliguori@us.ibm.com, Wenchao Xia , pbonzini@redhat.com Subject: [Qemu-devel] [qemu-devel] [PATCH V2 3/3] [RFC] libqblock-test case. 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 This file simulate the caller to test the library. Signed-off-by: Wenchao Xia --- libqblock-test.c | 197 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 197 insertions(+), 0 deletions(-) create mode 100644 libqblock-test.c diff --git a/libqblock-test.c b/libqblock-test.c new file mode 100644 index 0000000..6198924 --- /dev/null +++ b/libqblock-test.c @@ -0,0 +1,197 @@ +#include "libqblock.h" + +#include +#include +#include +#include + +static unsigned char buf0[1024]; +static unsigned char buf1[1024] = {4, 0, 0, 2}; + + +static int qbi_print_test(struct QBlockInfoImage *info) +{ + printf("name:%s, protocol %d, format %d, virt_size %" PRId64 " " + " allocated_size %" PRId64 + " encrypt %d, backing file %s.\n", + info->filename, info->protocol, info->format, info->virt_size, + info->allocated_size, + info->encrypt, info->backing_filename); + return 0; +} + + +int main(int argc, char **argv) +{ + struct QBlockState *qbs = NULL; + struct QBlockOptionOpen *qboo = NULL; + struct QBlockOptionCreate *qboc = NULL; + struct QBlockInfoImage *qbi = NULL; + char *filename1, *filename2; + int ret, err_no; + const char *err_msg = NULL; + + int i; + unsigned long op_size = 512; + unsigned long op_start = 1024; + + if (argc < 3) { + printf("usage: libqblock-test [filename1] [filename2].\n"); + return 0; + } + filename1 = argv[1]; + printf("qemu test, file name1 is %s.\n", filename1); + filename2 = argv[2]; + printf("qemu test, file name2 is %s.\n", filename2); + + libqblock_init(); + + ret = qb_state_new(&qbs); + if (ret < 0) { + goto free; + } + + ret = qb_oc_new(&qboc, QB_FMT_QCOW); + if (ret < 0) { + goto free; + } + + qboc->o_loc.filename = filename1; + qboc->o_loc.protocol = QB_PROTO_FILE; + qboc->o_fmt.fmt_type = QB_FMT_QCOW; + qboc->o_fmt.fmt_op.o_qcow.virt_size = 128 * 1024 * 1024; + + ret = qb_create(qbs, qboc); + if (ret < 0) { + printf("failed to create image, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto free; + } + + qboc->o_loc.filename = filename2; + qboc->o_loc.protocol = QB_PROTO_FILE; + qboc->o_fmt.fmt_type = QB_FMT_QCOW2; + qboc->o_fmt.fmt_op.o_qcow2.backing_file = filename1; + + ret = qb_create(qbs, qboc); + if (ret < 0) { + printf("failed to create image, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto free; + } + + ret = qb_oo_new(&qboo); + if (ret < 0) { + goto unlink; + } + + qboo->o_loc.filename = filename2; + qboo->o_loc.protocol = QB_PROTO_FILE; + qboo->o_fmt_type = QB_FMT_QCOW2; + qboo->o_flag = LIBQBLOCK_O_RDWR; + + ret = qb_open(qbs, qboo); + if (ret < 0) { + printf("failed to open image, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto unlink; + } + + ret = qb_write(qbs, buf1, op_size, op_start); + if (ret < 0) { + printf("failed to write image, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto close; + } + ret = qb_read(qbs, buf0, op_size, op_start); + if (ret < 0) { + printf("failed to read image, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto close; + } + + for (i = 0; i < op_size; i++) { + if (buf0[i] != buf1[i]) { + printf("mismatch found at %d.\n", i); + break; + } + } + + /* check backing chain */ + ret = qb_infoimage_get(qbs, &qbi); + if (ret < 0) { + printf("failed to get image info, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto close; + } + qbi_print_test(qbi); + + while (qbi->backing_filename != NULL) { + qb_close(qbs); + qboo->o_loc.filename = qbi->backing_filename; + qboo->o_loc.protocol = QB_PROTO_FILE; + qboo->o_fmt_type = QB_FMT_NONE; + qboo->o_flag = 0; + ret = qb_open(qbs, qboo); + if (ret < 0) { + printf("failed to open back image %s, ret is %d.\n", + qbi->backing_filename, ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto close; + } + qb_infoimage_free(&qbi); + ret = qb_infoimage_get(qbs, &qbi); + if (ret < 0) { + printf("failed to get image info, ret is %d.\n", ret); + if (ret == QB_ERR_INTERNAL_ERR) { + err_msg = qb_error_get_detail(qbs, &err_no); + printf("errno is %d, msg is %s.\n", err_no, err_msg); + } + goto close; + } + qbi_print_test(qbi); + } + + printf("test done.\n"); + + close: + qb_close(qbs); + unlink: + unlink(filename1); + unlink(filename2); + free: + if (qbs != NULL) { + qb_state_free(&qbs); + } + if (qboo != NULL) { + qb_oo_free(&qboo); + } + if (qboc != NULL) { + qb_oc_free(&qboc); + } + if (qbi != NULL) { + qb_infoimage_free(&qbi); + } + return 0; +}