xref: /openbsd-src/regress/lib/libc/sys/atf-c.c (revision 50b4491963c4dcfb13a03bc0252a527df944a1aa)
1 /*	$OpenBSD: atf-c.c,v 1.2 2019/11/22 15:59:53 bluhm Exp $	*/
2 /*
3  * Copyright (c) 2019 Moritz Buhl <openbsd@moritzbuhl.de>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/wait.h>
19 
20 #include <err.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <pwd.h>
27 #include <unistd.h>
28 
29 #include "atf-c.h"
30 
31 void usage(void);
32 
33 int cleanup;
34 int count;
35 int inspect;
36 int run;
37 int test;
38 
39 int
main(int argc,char * argv[])40 main(int argc, char *argv[])
41 {
42 	int ch, test;
43 	const char *errstr, *num;
44 
45 	while ((ch = getopt(argc, argv, "c:i:nr:")) != -1) {
46 		switch(ch) {
47 		case 'c':
48 			cleanup = 1;
49 			num = optarg;
50 			break;
51 		case 'i':
52 			inspect = 1;
53 			num = optarg;
54 			break;
55 		case 'n':
56 			count = 1;
57 			break;
58 		case 'r':
59 			run = 1;
60 			num = optarg;
61 			break;
62 		default:
63 			usage();
64 		}
65 	}
66 	argc -= optind;
67 	argv += optind;
68 
69 	if (cleanup + count + inspect + run > 1)
70 		usage();
71 
72 	if (cleanup || inspect || run) {
73 		test = strtonum(num, 1, INT_MAX, &errstr);
74 		if (errstr != NULL)
75 			errx(1, "test # is %s: %s", errstr, argv[1]);
76 	}
77 	if (count)
78 		printf("%d\n", atf_test(0, 0));
79 	else if (cleanup)
80 		ATF_CLEANUP(test);
81 	else if (run)
82 		ATF_RUN(test);
83 	else if (inspect)
84 		ATF_INSPECT(test);
85 	else
86 		usage();
87 
88 	return 0;
89 }
90 
91 void
usage(void)92 usage(void)
93 {
94 	fprintf(stderr, "usage: %s [-n] [-c|i|r test#]\n", getprogname());
95 	exit(1);
96 }
97 
98 void
atf_require(int exp,int expected_errno,const char * expstr,const char * src,const int lineno,char * fmt,...)99 atf_require(int exp, int expected_errno, const char *expstr, const char *src,
100     const int lineno, char *fmt, ...)
101 {
102 	va_list args;
103 	if (!(exp)) {
104 		fprintf(stderr, "\n%s:%d: ", src, lineno);
105 		if (fmt != NULL) {
106 			va_start(args, fmt);
107 			vfprintf(stderr, fmt, args);
108 			va_end(args);
109 		} else {
110 			fprintf(stderr, "'%s' evaluated to false\n", expstr);
111 		}
112 		exit(1);
113 	} else if (expected_errno >= 0 && errno != expected_errno) {
114 		fprintf(stderr, "\n%s:%d: ", src, lineno);
115 		fprintf(stderr, "expected errno %d but got %d instead\n",
116 		    expected_errno, errno);
117 		exit(1);
118 	}
119 	return;
120 }
121 
122 void
atf_tc_fail(char * fmt,...)123 atf_tc_fail(char *fmt, ...)
124 {
125 	va_list args;
126 	va_start(args, fmt);
127 	verrx(1, fmt, args);
128 }
129