xref: /openbsd/regress/sys/kern/ptrace2/atf-c.c (revision d415bd75)
1 /*	$OpenBSD: atf-c.c,v 1.1 2020/02/28 12:48:30 mpi Exp $	*/
2 
3 #include <sys/wait.h>
4 
5 #include <err.h>
6 #include <errno.h>
7 #include <limits.h>
8 #include <stdarg.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <pwd.h>
12 #include <unistd.h>
13 
14 #include "atf-c.h"
15 
16 void usage(void);
17 
18 int cleanup;
19 int count;
20 int inspect;
21 int run;
22 int test;
23 
24 int
25 main(int argc, char *argv[])
26 {
27 	int ch, test;
28 	const char *errstr, *num;
29 
30 	while ((ch = getopt(argc, argv, "c:i:nr:")) != -1) {
31 		switch(ch) {
32 		case 'c':
33 			cleanup = 1;
34 			num = optarg;
35 			break;
36 		case 'i':
37 			inspect = 1;
38 			num = optarg;
39 			break;
40 		case 'n':
41 			count = 1;
42 			break;
43 		case 'r':
44 			run = 1;
45 			num = optarg;
46 			break;
47 		default:
48 			usage();
49 		}
50 	}
51 	argc -= optind;
52 	argv += optind;
53 
54 	if (cleanup + count + inspect + run > 1)
55 		usage();
56 
57 	if (cleanup || inspect || run) {
58 		test = strtonum(num, 1, INT_MAX, &errstr);
59 		if (errstr != NULL)
60 			errx(1, "test # is %s: %s", errstr, argv[1]);
61 	}
62 	if (count)
63 		printf("%d\n", atf_test(0, 0));
64 	else if (cleanup)
65 		ATF_CLEANUP(test);
66 	else if (run)
67 		ATF_RUN(test);
68 	else if (inspect)
69 		ATF_INSPECT(test);
70 	else
71 		usage();
72 
73 	return 0;
74 }
75 
76 void
77 usage(void)
78 {
79 	fprintf(stderr, "usage: %s [-n] [-c|i|r test#]\n", getprogname());
80 	exit(1);
81 }
82 
83 void
84 atf_require(int exp, int expected_errno, const char *expstr, const char *src,
85     const int lineno, char *fmt, ...)
86 {
87 	va_list args;
88 	if (!(exp)) {
89 		fprintf(stderr, "\n%s:%d: ", src, lineno);
90 		if (fmt != NULL) {
91 			va_start(args, fmt);
92 			vfprintf(stderr, fmt, args);
93 			va_end(args);
94 		} else {
95 			fprintf(stderr, "'%s' evaluated to false\n", expstr);
96 		}
97 		exit(1);
98 	} else if (expected_errno >= 0 && errno != expected_errno) {
99 		fprintf(stderr, "\n%s:%d: ", src, lineno);
100 		fprintf(stderr, "expected errno %d but got %d instead\n",
101 		    expected_errno, errno);
102 		exit(1);
103 	}
104 	return;
105 }
106 
107 void
108 atf_tc_fail(char *fmt, ...)
109 {
110 	va_list args;
111 	va_start(args, fmt);
112 	verrx(1, fmt, args);
113 }
114