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