]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/testcode/testbound.c
daf8ddd41547dd6c57acf5d35cde586503cb1856
2 * testcode/testbound.c - test program for unbound.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Exits with code 1 on a failure. 0 if all unit tests are successfull.
45 #include "testcode/testpkts.h"
46 #include "testcode/replay.h"
47 #include "testcode/fake_event.h"
48 #include "daemon/remote.h"
49 #include "util/config_file.h"
50 #include "ldns/keyraw.h"
53 /** signal that this is a testbound compile */
54 #define unbound_testbound 1
56 * include the main program from the unbound daemon.
57 * rename main to daemon_main to call it
59 #define main daemon_main
60 #include "daemon/unbound.c"
63 /** maximum line length for lines in the replay file. */
64 #define MAX_LINE_LEN 1024
65 /** config files (removed at exit) */
66 static struct config_strlist
* cfgfiles
= NULL
;
68 /** give commandline usage for testbound. */
72 printf("usage: testbound [options]\n");
73 printf("\ttest the unbound daemon.\n");
74 printf("-h this help\n");
75 printf("-p file playback text file\n");
76 printf("-2 detect SHA256 support (exit code 0 or 1)\n");
77 printf("-g detect GOST support (exit code 0 or 1)\n");
78 printf("-e detect ECDSA support (exit code 0 or 1)\n");
79 printf("-s testbound self-test - unit test of testbound parts.\n");
80 printf("-o str unbound commandline options separated by spaces.\n");
81 printf("Version %s\n", PACKAGE_VERSION
);
82 printf("BSD licensed, see LICENSE file in source package.\n");
83 printf("Report bugs to %s.\n", PACKAGE_BUGREPORT
);
86 /** Max number of arguments to pass to unbound. */
90 * Add options from string to passed argc. splits on whitespace.
91 * @param args: the option argument, "-v -p 12345" or so.
92 * @param pass_argc: ptr to the argc for unbound. Modified.
93 * @param pass_argv: the argv to pass to unbound. Modified.
96 add_opts(const char* args
, int* pass_argc
, char* pass_argv
[])
98 const char *p
= args
, *np
;
100 while(p
&& isspace((unsigned char)*p
))
103 /* find location of next string and length of this one */
104 if((np
= strchr(p
, ' ')))
105 len
= (size_t)(np
-p
);
106 else len
= strlen(p
);
107 /* allocate and copy option */
108 if(*pass_argc
>= MAXARG
-1)
109 fatal_exit("too many arguments: '%s'", p
);
110 pass_argv
[*pass_argc
] = (char*)malloc(len
+1);
111 if(!pass_argv
[*pass_argc
])
112 fatal_exit("add_opts: out of memory");
113 memcpy(pass_argv
[*pass_argc
], p
, len
);
114 pass_argv
[*pass_argc
][len
] = 0;
116 /* go to next option */
118 while(p
&& isspace((unsigned char)*p
))
123 /** pretty print commandline for unbound in this test */
125 echo_cmdline(int argc
, char* argv
[])
128 fprintf(stderr
, "testbound is starting:");
129 for(i
=0; i
<argc
; i
++) {
130 fprintf(stderr
, " [%s]", argv
[i
]);
132 fprintf(stderr
, "\n");
135 /** spool autotrust file */
137 spool_auto_file(FILE* in
, int* lineno
, FILE* cfg
, char* id
)
139 char line
[MAX_LINE_LEN
];
142 /* find filename for new file */
143 while(isspace((unsigned char)*id
))
146 fatal_exit("AUTROTRUST_FILE must have id, line %d", *lineno
);
147 id
[strlen(id
)-1]=0; /* remove newline */
148 fake_temp_file("_auto_", id
, line
, sizeof(line
));
149 /* add option for the file */
150 fprintf(cfg
, "server: auto-trust-anchor-file: \"%s\"\n", line
);
151 /* open file and spool to it */
152 spool
= fopen(line
, "w");
153 if(!spool
) fatal_exit("could not open %s: %s", line
, strerror(errno
));
154 fprintf(stderr
, "testbound is spooling key file: %s\n", line
);
155 if(!cfg_strlist_insert(&cfgfiles
, strdup(line
)))
156 fatal_exit("out of memory");
157 line
[sizeof(line
)-1] = 0;
158 while(fgets(line
, MAX_LINE_LEN
-1, in
)) {
161 while(isspace((unsigned char)*parse
))
163 if(strncmp(parse
, "AUTOTRUST_END", 13) == 0) {
169 fatal_exit("no AUTOTRUST_END in input file");
172 /** process config elements */
174 setup_config(FILE* in
, int* lineno
, int* pass_argc
, char* pass_argv
[])
176 char configfile
[MAX_LINE_LEN
];
177 char line
[MAX_LINE_LEN
];
180 fake_temp_file("_cfg", "", configfile
, sizeof(configfile
));
181 add_opts("-c", pass_argc
, pass_argv
);
182 add_opts(configfile
, pass_argc
, pass_argv
);
183 cfg
= fopen(configfile
, "w");
184 if(!cfg
) fatal_exit("could not open %s: %s",
185 configfile
, strerror(errno
));
186 if(!cfg_strlist_insert(&cfgfiles
, strdup(configfile
)))
187 fatal_exit("out of memory");
188 line
[sizeof(line
)-1] = 0;
189 /* some basic settings to not pollute the host system */
190 fprintf(cfg
, "server: use-syslog: no\n");
191 fprintf(cfg
, " directory: \"\"\n");
192 fprintf(cfg
, " chroot: \"\"\n");
193 fprintf(cfg
, " username: \"\"\n");
194 fprintf(cfg
, " pidfile: \"\"\n");
195 fprintf(cfg
, " val-log-level: 2\n");
196 fprintf(cfg
, "remote-control: control-enable: no\n");
197 while(fgets(line
, MAX_LINE_LEN
-1, in
)) {
200 while(isspace((unsigned char)*parse
))
202 if(!*parse
|| parse
[0] == ';')
204 if(strncmp(parse
, "COMMANDLINE", 11) == 0) {
205 parse
[strlen(parse
)-1] = 0; /* strip off \n */
206 add_opts(parse
+11, pass_argc
, pass_argv
);
209 if(strncmp(parse
, "AUTOTRUST_FILE", 14) == 0) {
210 spool_auto_file(in
, lineno
, cfg
, parse
+14);
213 if(strncmp(parse
, "CONFIG_END", 10) == 0) {
219 fatal_exit("No CONFIG_END in input file");
223 /** read playback file */
224 static struct replay_scenario
*
225 setup_playback(const char* filename
, int* pass_argc
, char* pass_argv
[])
227 struct replay_scenario
* scen
= NULL
;
231 FILE *in
= fopen(filename
, "rb");
236 setup_config(in
, &lineno
, pass_argc
, pass_argv
);
237 scen
= replay_scenario_read(in
, filename
, &lineno
);
240 fatal_exit("Could not read: %s", filename
);
242 else fatal_exit("need a playback file (-p)");
243 log_info("Scenario: %s", scen
->title
);
247 /** remove config file at exit */
248 void remove_configfile(void)
250 struct config_strlist
* p
;
251 for(p
=cfgfiles
; p
; p
=p
->next
)
253 config_delstrlist(cfgfiles
);
258 * Main fake event test program. Setup, teardown and report errors.
259 * @param argc: arg count.
260 * @param argv: array of commandline arguments.
261 * @return program failure if test fails.
264 main(int argc
, char* argv
[])
268 char* pass_argv
[MAXARG
];
269 char* playback_file
= NULL
;
270 int init_optind
= optind
;
271 char* init_optarg
= optarg
;
272 struct replay_scenario
* scen
= NULL
;
274 /* we do not want the test to depend on the timezone */
275 (void)putenv("TZ=UTC");
277 log_init(NULL
, 0, NULL
);
278 /* determine commandline options for the daemon */
280 pass_argv
[0] = "unbound";
281 add_opts("-d", &pass_argc
, pass_argv
);
282 while( (c
=getopt(argc
, argv
, "2egho:p:s")) != -1) {
286 testbound_selftest();
287 printf("selftest successful\n");
290 #if (defined(HAVE_EVP_SHA256) || defined(HAVE_NSS)) && defined(USE_SHA2)
291 printf("SHA256 supported\n");
294 printf("SHA256 not supported\n");
299 #if defined(USE_ECDSA)
300 printf("ECDSA supported\n");
303 printf("ECDSA not supported\n");
309 if(sldns_key_EVP_load_gost_id()) {
310 printf("GOST supported\n");
313 printf("GOST not supported\n");
317 printf("GOST not supported\n");
322 playback_file
= optarg
;
325 add_opts(optarg
, &pass_argc
, pass_argv
);
340 log_info("Start of %s testbound program.", PACKAGE_STRING
);
341 if(atexit(&remove_configfile
) != 0)
342 fatal_exit("atexit() failed: %s", strerror(errno
));
344 /* setup test environment */
345 scen
= setup_playback(playback_file
, &pass_argc
, pass_argv
);
346 /* init fake event backend */
347 fake_event_init(scen
);
349 pass_argv
[pass_argc
] = NULL
;
350 echo_cmdline(pass_argc
, pass_argv
);
352 /* reset getopt processing */
353 optind
= init_optind
;
354 optarg
= init_optarg
;
356 /* run the normal daemon */
357 res
= daemon_main(pass_argc
, pass_argv
);
359 fake_event_cleanup();
360 for(c
=1; c
<pass_argc
; c
++)
363 log_info("Testbound Exit Success");
365 /* dlopen frees its thread state (dlopen of gost engine) */
372 /* fake remote control */
373 struct listen_port
* daemon_remote_open_ports(struct config_file
*
379 struct daemon_remote
* daemon_remote_create(struct config_file
* ATTR_UNUSED(cfg
))
381 return (struct daemon_remote
*)calloc(1,1);
384 void daemon_remote_delete(struct daemon_remote
* rc
)
389 void daemon_remote_clear(struct daemon_remote
* ATTR_UNUSED(rc
))
394 int daemon_remote_open_accept(struct daemon_remote
* ATTR_UNUSED(rc
),
395 struct listen_port
* ATTR_UNUSED(ports
),
396 struct worker
* ATTR_UNUSED(worker
))
401 int remote_accept_callback(struct comm_point
* ATTR_UNUSED(c
),
402 void* ATTR_UNUSED(arg
), int ATTR_UNUSED(error
),
403 struct comm_reply
* ATTR_UNUSED(repinfo
))
409 int remote_control_callback(struct comm_point
* ATTR_UNUSED(c
),
410 void* ATTR_UNUSED(arg
), int ATTR_UNUSED(error
),
411 struct comm_reply
* ATTR_UNUSED(repinfo
))
417 void remote_get_opt_ssl(char* ATTR_UNUSED(str
), void* ATTR_UNUSED(arg
))
422 void wsvc_command_option(const char* ATTR_UNUSED(wopt
),
423 const char* ATTR_UNUSED(cfgfile
), int ATTR_UNUSED(v
),
429 void wsvc_setup_worker(struct worker
* ATTR_UNUSED(worker
))
434 void wsvc_desetup_worker(struct worker
* ATTR_UNUSED(worker
))
440 void worker_win_stop_cb(int ATTR_UNUSED(fd
), short ATTR_UNUSED(ev
),
441 void* ATTR_UNUSED(arg
))
446 void wsvc_cron_cb(void* ATTR_UNUSED(arg
))
450 #endif /* UB_ON_WINDOWS */