xref: /freebsd/contrib/sendmail/libsm/test.c (revision 0957b409a90fd597c1e9124cbaf3edd2b488f4ac)
1 /*
2  * Copyright (c) 2000-2002 Proofpoint, Inc. and its suppliers.
3  *	All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #include <sm/gen.h>
11 SM_IDSTR(Id, "@(#)$Id: test.c,v 1.17 2013-11-22 20:51:44 ca Exp $")
12 
13 /*
14 **  Abstractions for writing libsm test programs.
15 */
16 
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <sm/debug.h>
21 #include <sm/test.h>
22 
23 extern char *optarg;
24 extern int optind;
25 extern int optopt;
26 extern int opterr;
27 
28 int SmTestIndex;
29 int SmTestNumErrors;
30 bool SmTestVerbose;
31 
32 static char Help[] = "\
33 %s [-h] [-d debugging] [-v]\n\
34 \n\
35 %s\n\
36 \n\
37 -h		Display this help information.\n\
38 -d debugging	Set debug activation levels.\n\
39 -v		Verbose output.\n\
40 ";
41 
42 static char Usage[] = "\
43 Usage: %s [-h] [-v]\n\
44 Use %s -h for help.\n\
45 ";
46 
47 /*
48 **  SM_TEST_BEGIN -- initialize test system.
49 **
50 **	Parameters:
51 **		argc -- argument counter.
52 **		argv -- argument vector.
53 **		testname -- description of tests.
54 **
55 **	Results:
56 **		none.
57 */
58 
59 void
60 sm_test_begin(argc, argv, testname)
61 	int argc;
62 	char **argv;
63 	char *testname;
64 {
65 	int c;
66 
67 	SmTestIndex = 0;
68 	SmTestNumErrors = 0;
69 	SmTestVerbose = false;
70 	opterr = 0;
71 
72 	while ((c = getopt(argc, argv, "vhd:")) != -1)
73 	{
74 		switch (c)
75 		{
76 		  case 'v':
77 			SmTestVerbose = true;
78 			break;
79 		  case 'd':
80 			sm_debug_addsettings_x(optarg);
81 			break;
82 		  case 'h':
83 			(void) fprintf(stdout, Help, argv[0], testname);
84 			exit(0);
85 		  default:
86 			(void) fprintf(stderr,
87 					"Unknown command line option -%c\n",
88 					optopt);
89 			(void) fprintf(stderr, Usage, argv[0], argv[0]);
90 			exit(1);
91 		}
92 	}
93 }
94 
95 /*
96 **  SM_TEST -- single test.
97 **
98 **	Parameters:
99 **		success -- did test succeeed?
100 **		expr -- expression that has been evaluated.
101 **		filename -- guess...
102 **		lineno -- line number.
103 **
104 **	Results:
105 **		value of success.
106 */
107 
108 bool
109 sm_test(success, expr, filename, lineno)
110 	bool success;
111 	char *expr;
112 	char *filename;
113 	int lineno;
114 {
115 	++SmTestIndex;
116 	if (SmTestVerbose)
117 		(void) fprintf(stderr, "%d..", SmTestIndex);
118 	if (!success)
119 	{
120 		++SmTestNumErrors;
121 		if (!SmTestVerbose)
122 			(void) fprintf(stderr, "%d..", SmTestIndex);
123 		(void) fprintf(stderr, "bad! %s:%d %s\n", filename, lineno,
124 				expr);
125 	}
126 	else
127 	{
128 		if (SmTestVerbose)
129 			(void) fprintf(stderr, "ok\n");
130 	}
131 	return success;
132 }
133 
134 /*
135 **  SM_TEST_END -- end of test system.
136 **
137 **	Parameters:
138 **		none.
139 **
140 **	Results:
141 **		number of errors.
142 */
143 
144 int
145 sm_test_end()
146 {
147 	(void) fprintf(stderr, "%d of %d tests completed successfully\n",
148 			SmTestIndex - SmTestNumErrors, SmTestIndex);
149 	if (SmTestNumErrors != 0)
150 		(void) fprintf(stderr, "*** %d error%s in test! ***\n",
151 				SmTestNumErrors,
152 				SmTestNumErrors > 1 ? "s" : "");
153 
154 	return SmTestNumErrors;
155 }
156