1236099Sdes/*-
2236099Sdes * Copyright (c) 2012 Dag-Erling Sm��rgrav
3236099Sdes * All rights reserved.
4236099Sdes *
5236099Sdes * Redistribution and use in source and binary forms, with or without
6236099Sdes * modification, are permitted provided that the following conditions
7236099Sdes * are met:
8236099Sdes * 1. Redistributions of source code must retain the above copyright
9255376Sdes *    notice, this list of conditions and the following disclaimer.
10236099Sdes * 2. Redistributions in binary form must reproduce the above copyright
11236099Sdes *    notice, this list of conditions and the following disclaimer in the
12236099Sdes *    documentation and/or other materials provided with the distribution.
13236099Sdes * 3. The name of the author may not be used to endorse or promote
14236099Sdes *    products derived from this software without specific prior written
15236099Sdes *    permission.
16236099Sdes *
17236099Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18236099Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19236099Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20236099Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21236099Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22236099Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23236099Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24236099Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25236099Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26236099Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27236099Sdes * SUCH DAMAGE.
28236099Sdes *
29255376Sdes * $Id: t_main.c 651 2013-03-05 18:11:59Z des $
30236099Sdes */
31236099Sdes
32236099Sdes#ifdef HAVE_CONFIG_H
33236099Sdes# include "config.h"
34236099Sdes#endif
35236099Sdes
36236099Sdes#include <err.h>
37236099Sdes#include <stdarg.h>
38236099Sdes#include <stdio.h>
39236099Sdes#include <stdlib.h>
40236099Sdes#include <string.h>
41255376Sdes#include <syslog.h>
42236099Sdes#include <unistd.h>
43236099Sdes
44236099Sdes#include "t.h"
45236099Sdes
46236099Sdesconst char *t_progname;
47236099Sdes
48236099Sdesstatic int verbose;
49236099Sdes
50236099Sdesvoid
51236099Sdest_verbose(const char *fmt, ...)
52236099Sdes{
53236099Sdes	va_list ap;
54236099Sdes
55236099Sdes	if (verbose) {
56236099Sdes		va_start(ap, fmt);
57236099Sdes		vfprintf(stderr, fmt, ap);
58236099Sdes		va_end(ap);
59236099Sdes	}
60236099Sdes}
61236099Sdes
62236099Sdesstatic void
63236099Sdesusage(void)
64236099Sdes{
65236099Sdes
66255376Sdes	fprintf(stderr, "usage: %s [-v]\n", t_progname);
67236099Sdes	exit(1);
68236099Sdes}
69236099Sdes
70236099Sdesint
71236099Sdesmain(int argc, char *argv[])
72236099Sdes{
73236099Sdes	const struct t_test **t_plan;
74236099Sdes	const char *desc;
75236099Sdes	int n, pass, fail;
76236099Sdes	int opt;
77236099Sdes
78255376Sdes#ifdef HAVE_SETLOGMASK
79255376Sdes	/* suppress openpam_log() */
80255376Sdes	setlogmask(LOG_UPTO(0));
81255376Sdes#endif
82255376Sdes
83255376Sdes	/* clean up temp files in case of premature exit */
84255376Sdes	atexit(t_fcloseall);
85255376Sdes
86236099Sdes	if ((t_progname = strrchr(argv[0], '/')) != NULL)
87236099Sdes		t_progname++; /* one past the slash */
88236099Sdes	else
89236099Sdes		t_progname = argv[0];
90236099Sdes
91236099Sdes	while ((opt = getopt(argc, argv, "v")) != -1)
92236099Sdes		switch (opt) {
93236099Sdes		case 'v':
94236099Sdes			verbose = 1;
95236099Sdes			break;
96236099Sdes		default:
97236099Sdes			usage();
98236099Sdes		}
99236099Sdes
100236099Sdes	argc -= optind;
101236099Sdes	argv += optind;
102236099Sdes
103236099Sdes	/* prepare the test plan */
104236099Sdes	if ((t_plan = t_prepare(argc, argv)) == NULL)
105236099Sdes		errx(1, "no plan\n");
106236099Sdes
107236099Sdes	/* count the tests */
108236099Sdes	for (n = 0; t_plan[n] != NULL; ++n)
109236099Sdes		/* nothing */;
110236099Sdes	printf("1..%d\n", n);
111236099Sdes
112236099Sdes	/* run the tests */
113236099Sdes	for (n = pass = fail = 0; t_plan[n] != NULL; ++n) {
114236099Sdes		desc = t_plan[n]->desc ? t_plan[n]->desc : "no description";
115255376Sdes		if ((*t_plan[n]->func)(t_plan[n]->arg)) {
116236099Sdes			printf("ok %d - %s\n", n + 1, desc);
117236099Sdes			++pass;
118236099Sdes		} else {
119236099Sdes			printf("not ok %d - %s\n", n + 1, desc);
120236099Sdes			++fail;
121236099Sdes		}
122236099Sdes	}
123236099Sdes
124236099Sdes	/* clean up and exit */
125236099Sdes	t_cleanup();
126236099Sdes	exit(fail > 0 ? 1 : 0);
127236099Sdes}
128