summaryrefslogtreecommitdiffstats
path: root/src/global/test_main.c
blob: a783ce354618dc79feb498f07fbda23b6987d663 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*++
/* NAME
/*	test_main 3
/* SUMMARY
/*	test main program
/* SYNOPSIS
/*	#include <test_main.h>
/*
/*	NORETURN test_main(argc, argv, test_driver, key, value, ...)
/*	int	argc;
/*	char	**argv;
/*	void	(*test_driver)(int argc, char **argv);
/*	int	key;
/* DESCRIPTION
/*	This module implements a test main program for stand-alone
/*	module tests.
/*
/*	test_main() should be called from a main program. It does
/*	generic command-line options processing, and initializes
/*	configurable parameters. After calling the test_driver()
/*	function, the test_main() function terminates.
/*
/*	Arguments:
/* .IP "void (*test_driver)(int argc, char **argv)"
/*	A pointer to a function that is called after processing
/*	command-line options and initializing configuration parameters.
/*	The argc and argv specify the process name and non-option
/*	command-line arguments.
/* .PP
/*	Optional test_main() arguments are specified as a null-terminated
/*	list with macros that have zero or more arguments:
/* .IP "CA_TEST_MAIN_INT_TABLE(CONFIG_INT_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_LONG_TABLE(CONFIG_LONG_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_STR_TABLE(CONFIG_STR_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_BOOL_TABLE(CONFIG_BOOL_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_TIME_TABLE(CONFIG_TIME_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_RAW_TABLE(CONFIG_RAW_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed. Raw parameters are not subjected to $name
/*	evaluation.
/* .IP "CA_TEST_MAIN_NINT_TABLE(CONFIG_NINT_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* .IP "CA_TEST_MAIN_NBOOL_TABLE(CONFIG_NBOOL_TABLE *)"
/*	A table with configurable parameters, to be loaded from the
/*	global Postfix configuration file. Tables are loaded in the
/*	order as specified, and multiple instances of the same type
/*	are allowed.
/* DIAGNOSTICS
/*	Problems and transactions are logged stderr.
/* BUGS
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Wietse Venema
/*	Google, Inc.
/*	111 8th Avenue
/*	New York, NY 10011, USA
/*--*/

 /*
  * System library.
  */
#include <sys_defs.h>
#include <stdlib.h>

 /*
  * Utility library.
  */
#include <dict.h>
#include <msg.h>
#include <msg_vstream.h>
#include <mymalloc.h>
#include <stringops.h>

 /*
  * Global library.
  */
#include <mail_params.h>
#include <mail_conf.h>
#include <mail_dict.h>
#include <mail_task.h>
#include <mail_version.h>

 /*
  * Test library.
  */
#include <test_main.h>

/* test_driver_main - the real main program */

NORETURN test_main(int argc, char **argv, TEST_DRIVER_FN test_driver,...)
{
    const char *myname = "test_driver_main";
    va_list ap;
    int     ch;
    int     key;
    int     test_driver_argc;
    char  **test_driver_argv;

    /*
     * Set up logging.
     */
    var_procname = mystrdup(basename(argv[0]));
    msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);

    /*
     * Check the Postfix library version as soon as we enable logging.
     */
    MAIL_VERSION_CHECK;

    /*
     * Parse JCL.
     */
    while ((ch = GETOPT(argc, argv, "c:v")) > 0) {
	switch (ch) {
	case 'c':
	    if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
		msg_fatal("out of memory");
	    break;
	case 'v':
	    msg_verbose++;
	    break;
	default:
	    msg_fatal("invalid option: %c. Usage: %s [-c config_dir] [-v]",
		      optopt, argv[0]);
	    break;
	}
    }

    /*
     * Initialize generic parameters.
     */
    set_mail_conf_str(VAR_PROCNAME, var_procname);
    set_mail_conf_str(VAR_SERVNAME, var_procname);
    mail_conf_read();

    /*
     * Register higher-level dictionaries and initialize the support for
     * dynamically-loaded dictionaries.
     */
    mail_dict_init();

    /*
     * Application-specific initialization.
     */
    va_start(ap, test_driver);
    while ((key = va_arg(ap, int)) != 0) {
	switch (key) {
	case TEST_MAIN_INT_TABLE:
	    get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
	    break;
	case TEST_MAIN_LONG_TABLE:
	    get_mail_conf_long_table(va_arg(ap, CONFIG_LONG_TABLE *));
	    break;
	case TEST_MAIN_STR_TABLE:
	    get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
	    break;
	case TEST_MAIN_BOOL_TABLE:
	    get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
	    break;
	case TEST_MAIN_TIME_TABLE:
	    get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
	    break;
	case TEST_MAIN_RAW_TABLE:
	    get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
	    break;
	case TEST_MAIN_NINT_TABLE:
	    get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
	    break;
	case TEST_MAIN_NBOOL_TABLE:
	    get_mail_conf_nbool_table(va_arg(ap, CONFIG_NBOOL_TABLE *));
	    break;
	default:
	    msg_panic("%s: unknown argument type: %d", myname, key);
	}
    }
    va_end(ap);

    /*
     * Set up call-back info.
     */
    test_driver_argv = argv + optind - 1;
    if (test_driver_argv != argv)
	test_driver_argv[0] = argv[0];
    test_driver_argc = argc - optind + 1;

    /*
     * Call the test driver and terminate (if they didn't terminate already).
     */
    test_driver(test_driver_argc, test_driver_argv);
    exit(0);
}