1 /* $NetBSD: test_main.c,v 1.2 2022/10/08 16:12:45 christos Exp $ */
2
3 /*++
4 /* NAME
5 /* test_main 3
6 /* SUMMARY
7 /* test main program
8 /* SYNOPSIS
9 /* #include <test_main.h>
10 /*
11 /* NORETURN test_main(argc, argv, test_driver, key, value, ...)
12 /* int argc;
13 /* char **argv;
14 /* void (*test_driver)(int argc, char **argv);
15 /* int key;
16 /* DESCRIPTION
17 /* This module implements a test main program for stand-alone
18 /* module tests.
19 /*
20 /* test_main() should be called from a main program. It does
21 /* generic command-line options processing, and initializes
22 /* configurable parameters. After calling the test_driver()
23 /* function, the test_main() function terminates.
24 /*
25 /* Arguments:
26 /* .IP "void (*test_driver)(int argc, char **argv)"
27 /* A pointer to a function that is called after processing
28 /* command-line options and initializing configuration parameters.
29 /* The argc and argv specify the process name and non-option
30 /* command-line arguments.
31 /* .PP
32 /* Optional test_main() arguments are specified as a null-terminated
33 /* list with macros that have zero or more arguments:
34 /* .IP "CA_TEST_MAIN_INT_TABLE(CONFIG_INT_TABLE *)"
35 /* A table with configurable parameters, to be loaded from the
36 /* global Postfix configuration file. Tables are loaded in the
37 /* order as specified, and multiple instances of the same type
38 /* are allowed.
39 /* .IP "CA_TEST_MAIN_LONG_TABLE(CONFIG_LONG_TABLE *)"
40 /* A table with configurable parameters, to be loaded from the
41 /* global Postfix configuration file. Tables are loaded in the
42 /* order as specified, and multiple instances of the same type
43 /* are allowed.
44 /* .IP "CA_TEST_MAIN_STR_TABLE(CONFIG_STR_TABLE *)"
45 /* A table with configurable parameters, to be loaded from the
46 /* global Postfix configuration file. Tables are loaded in the
47 /* order as specified, and multiple instances of the same type
48 /* are allowed.
49 /* .IP "CA_TEST_MAIN_BOOL_TABLE(CONFIG_BOOL_TABLE *)"
50 /* A table with configurable parameters, to be loaded from the
51 /* global Postfix configuration file. Tables are loaded in the
52 /* order as specified, and multiple instances of the same type
53 /* are allowed.
54 /* .IP "CA_TEST_MAIN_TIME_TABLE(CONFIG_TIME_TABLE *)"
55 /* A table with configurable parameters, to be loaded from the
56 /* global Postfix configuration file. Tables are loaded in the
57 /* order as specified, and multiple instances of the same type
58 /* are allowed.
59 /* .IP "CA_TEST_MAIN_RAW_TABLE(CONFIG_RAW_TABLE *)"
60 /* A table with configurable parameters, to be loaded from the
61 /* global Postfix configuration file. Tables are loaded in the
62 /* order as specified, and multiple instances of the same type
63 /* are allowed. Raw parameters are not subjected to $name
64 /* evaluation.
65 /* .IP "CA_TEST_MAIN_NINT_TABLE(CONFIG_NINT_TABLE *)"
66 /* A table with configurable parameters, to be loaded from the
67 /* global Postfix configuration file. Tables are loaded in the
68 /* order as specified, and multiple instances of the same type
69 /* are allowed.
70 /* .IP "CA_TEST_MAIN_NBOOL_TABLE(CONFIG_NBOOL_TABLE *)"
71 /* A table with configurable parameters, to be loaded from the
72 /* global Postfix configuration file. Tables are loaded in the
73 /* order as specified, and multiple instances of the same type
74 /* are allowed.
75 /* DIAGNOSTICS
76 /* Problems and transactions are logged stderr.
77 /* BUGS
78 /* LICENSE
79 /* .ad
80 /* .fi
81 /* The Secure Mailer license must be distributed with this software.
82 /* AUTHOR(S)
83 /* Wietse Venema
84 /* IBM T.J. Watson Research
85 /* P.O. Box 704
86 /* Yorktown Heights, NY 10598, USA
87 /*
88 /* Wietse Venema
89 /* Google, Inc.
90 /* 111 8th Avenue
91 /* New York, NY 10011, USA
92 /*--*/
93
94 /*
95 * System library.
96 */
97 #include <sys_defs.h>
98 #include <stdlib.h>
99
100 /*
101 * Utility library.
102 */
103 #include <dict.h>
104 #include <msg.h>
105 #include <msg_vstream.h>
106 #include <mymalloc.h>
107 #include <stringops.h>
108
109 /*
110 * Global library.
111 */
112 #include <mail_params.h>
113 #include <mail_conf.h>
114 #include <mail_dict.h>
115 #include <mail_task.h>
116 #include <mail_version.h>
117
118 /*
119 * Test library.
120 */
121 #include <test_main.h>
122
123 /* test_driver_main - the real main program */
124
test_main(int argc,char ** argv,TEST_DRIVER_FN test_driver,...)125 NORETURN test_main(int argc, char **argv, TEST_DRIVER_FN test_driver,...)
126 {
127 const char *myname = "test_driver_main";
128 va_list ap;
129 int ch;
130 int key;
131 int test_driver_argc;
132 char **test_driver_argv;
133
134 /*
135 * Set up logging.
136 */
137 var_procname = mystrdup(basename(argv[0]));
138 msg_vstream_init(mail_task(var_procname), VSTREAM_ERR);
139
140 /*
141 * Check the Postfix library version as soon as we enable logging.
142 */
143 MAIL_VERSION_CHECK;
144
145 /*
146 * Parse JCL.
147 */
148 while ((ch = GETOPT(argc, argv, "c:v")) > 0) {
149 switch (ch) {
150 case 'c':
151 if (setenv(CONF_ENV_PATH, optarg, 1) < 0)
152 msg_fatal("out of memory");
153 break;
154 case 'v':
155 msg_verbose++;
156 break;
157 default:
158 msg_fatal("invalid option: %c. Usage: %s [-c config_dir] [-v]",
159 optopt, argv[0]);
160 break;
161 }
162 }
163
164 /*
165 * Initialize generic parameters.
166 */
167 set_mail_conf_str(VAR_PROCNAME, var_procname);
168 set_mail_conf_str(VAR_SERVNAME, var_procname);
169 mail_conf_read();
170
171 /*
172 * Register higher-level dictionaries and initialize the support for
173 * dynamically-loaded dictionaries.
174 */
175 mail_dict_init();
176
177 /*
178 * Application-specific initialization.
179 */
180 va_start(ap, test_driver);
181 while ((key = va_arg(ap, int)) != 0) {
182 switch (key) {
183 case TEST_MAIN_INT_TABLE:
184 get_mail_conf_int_table(va_arg(ap, CONFIG_INT_TABLE *));
185 break;
186 case TEST_MAIN_LONG_TABLE:
187 get_mail_conf_long_table(va_arg(ap, CONFIG_LONG_TABLE *));
188 break;
189 case TEST_MAIN_STR_TABLE:
190 get_mail_conf_str_table(va_arg(ap, CONFIG_STR_TABLE *));
191 break;
192 case TEST_MAIN_BOOL_TABLE:
193 get_mail_conf_bool_table(va_arg(ap, CONFIG_BOOL_TABLE *));
194 break;
195 case TEST_MAIN_TIME_TABLE:
196 get_mail_conf_time_table(va_arg(ap, CONFIG_TIME_TABLE *));
197 break;
198 case TEST_MAIN_RAW_TABLE:
199 get_mail_conf_raw_table(va_arg(ap, CONFIG_RAW_TABLE *));
200 break;
201 case TEST_MAIN_NINT_TABLE:
202 get_mail_conf_nint_table(va_arg(ap, CONFIG_NINT_TABLE *));
203 break;
204 case TEST_MAIN_NBOOL_TABLE:
205 get_mail_conf_nbool_table(va_arg(ap, CONFIG_NBOOL_TABLE *));
206 break;
207 default:
208 msg_panic("%s: unknown argument type: %d", myname, key);
209 }
210 }
211 va_end(ap);
212
213 /*
214 * Set up call-back info.
215 */
216 test_driver_argv = argv + optind - 1;
217 if (test_driver_argv != argv)
218 test_driver_argv[0] = argv[0];
219 test_driver_argc = argc - optind + 1;
220
221 /*
222 * Call the test driver and terminate (if they didn't terminate already).
223 */
224 test_driver(test_driver_argc, test_driver_argv);
225 exit(0);
226 }
227