11573Srgrimes/*
21573Srgrimes * Copyright (c) 2000-2002 Proofpoint, Inc. and its suppliers.
31573Srgrimes *	All rights reserved.
41573Srgrimes *
51573Srgrimes * By using this file, you agree to the terms and conditions set
61573Srgrimes * forth in the LICENSE file which can be found at the top level of
71573Srgrimes * the sendmail distribution.
81573Srgrimes */
91573Srgrimes
101573Srgrimes#include <sm/gen.h>
111573SrgrimesSM_IDSTR(Id, "@(#)$Id: test.c,v 1.17 2013-11-22 20:51:44 ca Exp $")
121573Srgrimes
131573Srgrimes/*
141573Srgrimes**  Abstractions for writing libsm test programs.
151573Srgrimes*/
161573Srgrimes
171573Srgrimes#include <stdlib.h>
181573Srgrimes#include <unistd.h>
191573Srgrimes#include <stdio.h>
201573Srgrimes#include <sm/debug.h>
211573Srgrimes#include <sm/test.h>
221573Srgrimes
231573Srgrimesextern char *optarg;
241573Srgrimesextern int optind;
251573Srgrimesextern int optopt;
261573Srgrimesextern int opterr;
271573Srgrimes
281573Srgrimesint SmTestIndex;
291573Srgrimesint SmTestNumErrors;
301573Srgrimesbool SmTestVerbose;
311573Srgrimes
321573Srgrimesstatic char Help[] = "\
331573Srgrimes%s [-h] [-d debugging] [-v]\n\
341573Srgrimes\n\
351573Srgrimes%s\n\
3692986Sobrien\n\
3792986Sobrien-h		Display this help information.\n\
381573Srgrimes-d debugging	Set debug activation levels.\n\
391573Srgrimes-v		Verbose output.\n\
401573Srgrimes";
411573Srgrimes
421573Srgrimesstatic char Usage[] = "\
431573SrgrimesUsage: %s [-h] [-v]\n\
441573SrgrimesUse %s -h for help.\n\
451573Srgrimes";
461573Srgrimes
471573Srgrimes/*
481573Srgrimes**  SM_TEST_BEGIN -- initialize test system.
491573Srgrimes**
501573Srgrimes**	Parameters:
511573Srgrimes**		argc -- argument counter.
521573Srgrimes**		argv -- argument vector.
531573Srgrimes**		testname -- description of tests.
541573Srgrimes**
551573Srgrimes**	Results:
561573Srgrimes**		none.
57*/
58
59void
60sm_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
108bool
109sm_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
144int
145sm_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