From patchwork Mon Aug 3 14:41:19 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Jones X-Patchwork-Id: 503214 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 21BE7140E6D for ; Tue, 4 Aug 2015 00:42:43 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754003AbbHCOln (ORCPT ); Mon, 3 Aug 2015 10:41:43 -0400 Received: from mx1.redhat.com ([209.132.183.28]:59058 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753492AbbHCOlm (ORCPT ); Mon, 3 Aug 2015 10:41:42 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id 7CDD08E923; Mon, 3 Aug 2015 14:41:42 +0000 (UTC) Received: from hawk.localdomain.com (dhcp-1-112.brq.redhat.com [10.34.1.112]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t73EfYNj006138; Mon, 3 Aug 2015 10:41:40 -0400 From: Andrew Jones To: kvm@vger.kernel.org, kvm-ppc@vger.kernel.org Cc: dgibson@redhat.com, david@gibson.dropbear.id.au, agraf@suse.de, thuth@redhat.com, lvivier@redhat.com, pbonzini@redhat.com Subject: [kvm-unit-tests PATCH 02/14] lib: share arm-selftest utility functions Date: Mon, 3 Aug 2015 16:41:19 +0200 Message-Id: <1438612891-3718-3-git-send-email-drjones@redhat.com> In-Reply-To: <1438612891-3718-1-git-send-email-drjones@redhat.com> References: <1438612891-3718-1-git-send-email-drjones@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.27 Sender: kvm-ppc-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm-ppc@vger.kernel.org arm-selftest has a couple utility functions that could be useful to other unit tests, even other architectures. So move them out. split_var moves to lib/util, where we can add other random utilities over time. assert_args inspires report_abort, which allows us to report a message, using the current prefix, that we're aborting (outputs ABORT vs. PASS/FAIL). This is useful for cases when unit tests can't complete due to missing dependencies of some sort, such as missing/invalid inputs from the user. Signed-off-by: Andrew Jones --- arm/selftest.c | 33 ++++++++------------------------- config/config-arm-common.mak | 1 + lib/libcflat.h | 11 ++++++----- lib/report.c | 16 ++++++++++++++++ lib/util.c | 20 ++++++++++++++++++++ lib/util.h | 23 +++++++++++++++++++++++ 6 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 lib/util.c create mode 100644 lib/util.h diff --git a/arm/selftest.c b/arm/selftest.c index fc9ec609d875e..74abd048add6c 100644 --- a/arm/selftest.c +++ b/arm/selftest.c @@ -6,6 +6,7 @@ * This work is licensed under the terms of the GNU LGPL, version 2. */ #include +#include #include #include #include @@ -18,28 +19,6 @@ #include #include -static void assert_args(int num_args, int needed_args) -{ - if (num_args < needed_args) { - printf("selftest: not enough arguments\n"); - abort(); - } -} - -static char *split_var(char *s, long *val) -{ - char *p; - - p = strchr(s, '='); - if (!p) - return NULL; - - *val = atol(p+1); - *p = '\0'; - - return s; -} - static void check_setup(int argc, char **argv) { int nr_tests = 0, i; @@ -48,7 +27,7 @@ static void check_setup(int argc, char **argv) for (i = 0; i < argc; ++i) { - var = split_var(argv[i], &val); + var = parse_keyval(argv[i], &val); if (!var) continue; @@ -72,7 +51,8 @@ static void check_setup(int argc, char **argv) report_prefix_pop(); } - assert_args(nr_tests, 2); + if (nr_tests < 2) + report_abort("missing input"); } static struct pt_regs expected_regs; @@ -344,7 +324,10 @@ static void cpu_report(void) int main(int argc, char **argv) { report_prefix_push("selftest"); - assert_args(argc, 1); + + if (argc < 1) + report_abort("no test specified"); + report_prefix_push(argv[0]); if (strcmp(argv[0], "setup") == 0) { diff --git a/config/config-arm-common.mak b/config/config-arm-common.mak index f35b8b9231c03..4cff846930796 100644 --- a/config/config-arm-common.mak +++ b/config/config-arm-common.mak @@ -32,6 +32,7 @@ CFLAGS += -I lib -I lib/libfdt asm-offsets = lib/$(ARCH)/asm-offsets.h include config/asm-offsets.mak +cflatobjs += lib/util.o cflatobjs += lib/alloc.o cflatobjs += lib/devicetree.o cflatobjs += lib/virtio.o diff --git a/lib/libcflat.h b/lib/libcflat.h index 9747ccdbc9f1d..8411f6c5d92e3 100644 --- a/lib/libcflat.h +++ b/lib/libcflat.h @@ -57,11 +57,12 @@ extern int snprintf(char *buf, int size, const char *fmt, ...); extern int vsnprintf(char *buf, int size, const char *fmt, va_list va); extern long atol(const char *ptr); -void report_prefix_push(const char *prefix); -void report_prefix_pop(void); -void report(const char *msg_fmt, bool pass, ...); -void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...); -int report_summary(void); +extern void report_prefix_push(const char *prefix); +extern void report_prefix_pop(void); +extern void report(const char *msg_fmt, bool pass, ...); +extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...); +extern void report_abort(const char *msg_fmt, ...); +extern int report_summary(void); #define ARRAY_SIZE(_a) (sizeof(_a)/sizeof((_a)[0])) diff --git a/lib/report.c b/lib/report.c index 35e664108a921..62916c4ac3c8a 100644 --- a/lib/report.c +++ b/lib/report.c @@ -96,3 +96,19 @@ int report_summary(void) spin_unlock(&lock); } + +void report_abort(const char *msg_fmt, ...) +{ + va_list va; + char buf[2000]; + + puts("ABORT: "); + puts(prefixes); + va_start(va, msg_fmt); + vsnprintf(buf, sizeof(buf), msg_fmt, va); + va_end(va); + puts(buf); + puts("\n"); + report_summary(); + abort(); +} diff --git a/lib/util.c b/lib/util.c new file mode 100644 index 0000000000000..9dcec30ac8b5f --- /dev/null +++ b/lib/util.c @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2015, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ +#include + +char *parse_keyval(char *s, long *val) +{ + char *p; + + p = strchr(s, '='); + if (!p) + return NULL; + + *val = atol(p+1); + *p = '\0'; + + return s; +} diff --git a/lib/util.h b/lib/util.h new file mode 100644 index 0000000000000..9a12bda4f61df --- /dev/null +++ b/lib/util.h @@ -0,0 +1,23 @@ +#ifndef _UTIL_H_ +#define _UTIL_H_ +/* + * Collection of utility functions to share between unit tests. + * + * Copyright (C) 2015, Red Hat Inc, Andrew Jones + * + * This work is licensed under the terms of the GNU LGPL, version 2. + */ + +/* + * parse_keyval extracts the integer from a string formatted as + * string=integer. This is useful for passing expected values in + * to the unit test on the command line, i.e. it helps parse QEMU + * command lines that include something like -append var1=1 var2=2 + * @s is the input string, likely a command line parameter, and + * @val is a pointer to where the integer will be stored. + * + * Returns a pointer to the key string of the parsed key-value pair. + */ +extern char *parse_keyval(char *s, long *val); + +#endif