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