Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <sys/wait.h>
33
34
#include <ctype.h>
35
#include <dlfcn.h>
36
#include <stdarg.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#include "vtc.h"
43
#include "vtc_log.h"
44
45
#include "vav.h"
46
#include "vsub.h"
47
#include "vrnd.h"
48
49
#define         MAX_TOKENS              200
50
51
volatile sig_atomic_t   vtc_error;      /* Error encountered */
52
int                     vtc_stop;       /* Stops current test without error */
53
pthread_t               vtc_thread;
54
int                     ign_unknown_macro = 0;
55
static struct vtclog    *vltop;
56
57
static pthread_mutex_t  vtc_vrnd_mtx;
58
59
static void
60
vtc_vrnd_lock(void)
61 17031
{
62 17031
        PTOK(pthread_mutex_lock(&vtc_vrnd_mtx));
63 17031
}
64
65
static void
66
vtc_vrnd_unlock(void)
67 17031
{
68 17031
        PTOK(pthread_mutex_unlock(&vtc_vrnd_mtx));
69 17031
}
70
71
static const char *tfn;
72
73
/**********************************************************************
74
 * Commands
75
 */
76
77
struct icmds {
78
        unsigned                magic;
79
#define ICMDS_MAGIC             0xa743d463
80
        struct cmds             *cmd;
81
        VTAILQ_ENTRY(icmds)     list;
82
};
83
84
static VTAILQ_HEAD(,icmds) icmd_list = VTAILQ_HEAD_INITIALIZER(icmd_list);
85
86
void
87
add_cmd(const char *name, cmd_f *cmd, unsigned flags)
88 22220
{
89 22220
        struct icmds *cp;
90
91 22220
        AN(name);
92 22220
        AN(cmd);
93 22220
        ALLOC_OBJ(cp, ICMDS_MAGIC);
94 22220
        AN(cp);
95 22220
        ALLOC_OBJ(cp->cmd, CMDS_MAGIC);
96 22220
        AN(cp->cmd);
97 22220
        cp->cmd->name = strdup(name);
98 22220
        AN(cp->cmd->name);
99 22220
        cp->cmd->cmd = cmd;
100 22220
        cp->cmd->flags = flags;
101 22220
        VTAILQ_INSERT_HEAD(&icmd_list, cp, list);
102 22220
}
103
104
struct cmds *
105
find_cmd(const char *name)
106 15206
{
107 15206
        struct icmds *cp;
108 15206
        char buf[BUFSIZ];
109
110 186968
        VTAILQ_FOREACH(cp, &icmd_list, list) {
111 186968
                CHECK_OBJ_NOTNULL(cp, ICMDS_MAGIC);
112 186968
                CHECK_OBJ_NOTNULL(cp->cmd, CMDS_MAGIC);
113 186968
                if (!vstrcmp(name, cp->cmd->name))
114 15201
                        return (cp->cmd);
115 186968
        }
116
117 5
        bprintf(buf, "libvtest_ext_%s.so", name);
118 5
        void *dlp = dlopen(buf, RTLD_NOW);
119 5
        if (dlp == NULL) {
120 5
                vtc_log(vltop, 4, "Autoload %s failed: %s", buf, dlerror());
121 5
                return (NULL);
122 5
        }
123 0
        vtc_log(vltop, 4, "Autoloaded %s", buf);
124
125 0
        VTAILQ_FOREACH(cp, &icmd_list, list) {
126 0
                CHECK_OBJ_NOTNULL(cp, ICMDS_MAGIC);
127 0
                CHECK_OBJ_NOTNULL(cp->cmd, CMDS_MAGIC);
128 0
                if (!vstrcmp(name, cp->cmd->name))
129 0
                        return (cp->cmd);
130 0
        }
131 0
        return (NULL);
132 0
}
133
134
static void
135
init_cmd_list(void)
136 1122
{
137 4488
#define CMD_GLOBAL(n) add_cmd(#n, cmd_##n, CMDS_F_GLOBAL);
138 12342
#define CMD_TOP(n) add_cmd(#n, cmd_##n, CMDS_F_NONE);
139 1122
#include "cmds.h"
140 1122
}
141
142
/**********************************************************************
143
 * Extensions
144
 *
145
 * We need to open the extension while doing argument processing
146
 * in order for relative paths to work.  This complicates the
147
 * VSUB_closefrom() a little bit.
148
 */
149
150
struct extension {
151
       unsigned                 magic;
152
#define EXTENSION_MAGIC         0x69e788db
153
       VTAILQ_ENTRY(extension)  list;
154
       const char               *name;
155
};
156
157
static VTAILQ_HEAD(,extension) extension_list =
158
    VTAILQ_HEAD_INITIALIZER(extension_list);
159
160
void
161
add_extension(const char *name)
162 0
{
163 0
        struct extension *ep;
164
165 0
        AN(name);
166
167 0
        if (! dlopen(name, RTLD_NOW)) {
168 0
                fprintf(stderr, "%s\n", dlerror());
169 0
                exit (2);
170 0
        }
171
172 0
        ALLOC_OBJ(ep, EXTENSION_MAGIC);
173 0
        AN(ep);
174 0
        ep->name = strdup(name);
175 0
        AN(ep->name);
176 0
        VTAILQ_INSERT_HEAD(&extension_list, ep, list);
177 0
}
178
179
/**********************************************************************
180
 * Macro facility
181
 */
182
183
struct macro {
184
        unsigned                magic;
185
#define MACRO_MAGIC             0x803423e3
186
        VTAILQ_ENTRY(macro)     list;
187
        char                    *name;
188
        char                    *val;
189
        macro_f                 *func;
190
};
191
192
static VTAILQ_HEAD(,macro) macro_list = VTAILQ_HEAD_INITIALIZER(macro_list);
193
194
/**********************************************************************/
195
196
static struct macro *
197
macro_def_int(const char *name, macro_f *func, const char *fmt, va_list ap)
198 34967
{
199 34967
        struct macro *m;
200 34967
        char buf[2048];
201
202 34967
        VTAILQ_FOREACH(m, &macro_list, list)
203 435911
                if (!vstrcmp(name, m->name))
204 6993
                        break;
205 34967
        if (m == NULL) {
206 27974
                ALLOC_OBJ(m, MACRO_MAGIC);
207 27974
                AN(m);
208 27974
                REPLACE(m->name, name);
209 27974
                AN(m->name);
210 27974
                VTAILQ_INSERT_TAIL(&macro_list, m, list);
211 27974
        }
212 34967
        AN(m);
213 34967
        if (func != NULL) {
214 2156
                AZ(fmt);
215 2156
                m->func = func;
216 32811
        } else {
217 32811
                AN(fmt);
218 32811
                vbprintf(buf, fmt, ap);
219 32811
                REPLACE(m->val, buf);
220 32811
                AN(m->val);
221 32811
        }
222 34967
        return (m);
223 34967
}
224
225
226
/**********************************************************************
227
 * This is for defining macros before we fork the child process which
228
 * runs the test-case.
229
 */
230
231
void
232
extmacro_def(const char *name, macro_f *func, const char *fmt, ...)
233 13942
{
234 13942
        va_list ap;
235
236 13942
        va_start(ap, fmt);
237 13942
        (void)macro_def_int(name, func, fmt, ap);
238 13942
        va_end(ap);
239 13942
}
240
241
/**********************************************************************
242
 * Below this point is run inside the testing child-process.
243
 */
244
245
static pthread_mutex_t          macro_mtx;
246
247
static void
248
init_macro(void)
249 1122
{
250 1122
        struct macro *m;
251
252
        /* Dump the extmacros for completeness */
253 12304
        VTAILQ_FOREACH(m, &macro_list, list) {
254 12304
                if (m->val != NULL)
255 10060
                        vtc_log(vltop, 4,
256 10060
                            "extmacro def %s=%s", m->name, m->val);
257 2244
                else
258 2244
                        vtc_log(vltop, 4, "extmacro def %s(...)", m->name);
259 12304
        }
260
261 1122
        PTOK(pthread_mutex_init(&macro_mtx, NULL));
262 1122
}
263
264
void
265
macro_def(struct vtclog *vl, const char *instance, const char *name,
266
    const char *fmt, ...)
267 21025
{
268 21025
        char buf1[256];
269 21025
        struct macro *m;
270 21025
        va_list ap;
271
272 21025
        AN(fmt);
273
274 21025
        if (instance != NULL) {
275 12297
                bprintf(buf1, "%s_%s", instance, name);
276 12297
                name = buf1;
277 12297
        }
278
279 21025
        PTOK(pthread_mutex_lock(&macro_mtx));
280 21025
        va_start(ap, fmt);
281 21025
        m = macro_def_int(name, NULL, fmt, ap);
282 21025
        va_end(ap);
283 21025
        vtc_log(vl, 4, "macro def %s=%s", name, m->val);
284 21025
        PTOK(pthread_mutex_unlock(&macro_mtx));
285 21025
}
286
287
void
288
macro_undef(struct vtclog *vl, const char *instance, const char *name)
289 3961
{
290 3961
        char buf1[256];
291 3961
        struct macro *m;
292
293 3961
        if (instance != NULL) {
294 3961
                bprintf(buf1, "%s_%s", instance, name);
295 3961
                name = buf1;
296 3961
        }
297
298 3961
        PTOK(pthread_mutex_lock(&macro_mtx));
299 3961
        VTAILQ_FOREACH(m, &macro_list, list)
300 68089
                if (!vstrcmp(name, m->name))
301 3661
                        break;
302 3961
        if (m != NULL) {
303 3661
                if (!vtc_stop)
304 223
                        vtc_log(vl, 4, "macro undef %s", name);
305 3661
                CHECK_OBJ(m, MACRO_MAGIC);
306 3661
                VTAILQ_REMOVE(&macro_list, m, list);
307 3661
                free(m->name);
308 3661
                free(m->val);
309 3661
                FREE_OBJ(m);
310 3661
        }
311 3961
        PTOK(pthread_mutex_unlock(&macro_mtx));
312 3961
}
313
314
unsigned
315
macro_isdef(const char *instance, const char *name)
316 2050
{
317 2050
        char buf1[256];
318 2050
        struct macro *m;
319
320 2050
        if (instance != NULL) {
321 0
                bprintf(buf1, "%s_%s", instance, name);
322 0
                name = buf1;
323 0
        }
324
325 2050
        PTOK(pthread_mutex_lock(&macro_mtx));
326 2050
        VTAILQ_FOREACH(m, &macro_list, list)
327 37694
                if (!vstrcmp(name, m->name))
328 0
                        break;
329 2050
        PTOK(pthread_mutex_unlock(&macro_mtx));
330
331 2050
        return (m != NULL);
332 2050
}
333
334
void
335
macro_cat(struct vtclog *vl, struct vsb *vsb, const char *b, const char *e)
336 13605
{
337 13605
        struct macro *m;
338 13605
        char **argv, *retval = NULL;
339 13605
        const char *err = NULL;
340 13605
        int argc;
341
342 13605
        AN(b);
343 13605
        if (e == NULL)
344 7036
                e = strchr(b, '\0');
345 13605
        AN(e);
346
347 13605
        argv = VAV_ParseTxt(b, e, &argc, ARGV_COMMA);
348 13605
        AN(argv);
349
350 13605
        if (*argv != NULL)
351 0
                vtc_fatal(vl, "Macro ${%.*s} parsing failed: %s",
352 0
                    (int)(e - b), b, *argv);
353
354 13605
        assert(argc >= 2);
355
356 13605
        PTOK(pthread_mutex_lock(&macro_mtx));
357 132483
        VTAILQ_FOREACH(m, &macro_list, list) {
358 132483
                CHECK_OBJ_NOTNULL(m, MACRO_MAGIC);
359 132483
                if (!vstrcmp(argv[1], m->name))
360 13532
                        break;
361 132483
        }
362 13605
        if (m != NULL) {
363 13532
                if (m->func != NULL) {
364 2452
                        AZ(m->val);
365 2452
                        retval = m->func(argc, argv, &err);
366 2452
                        if (err == NULL)
367 2452
                                AN(retval);
368 11080
                } else {
369 11080
                        AN(m->val);
370 11080
                        if (argc == 2)
371 11080
                                REPLACE(retval, m->val);
372 0
                        else
373 0
                                err = "macro does not take arguments";
374 11080
                }
375 13532
        }
376 13605
        PTOK(pthread_mutex_unlock(&macro_mtx));
377
378 13605
        VAV_Free(argv);
379
380 13605
        if (err != NULL)
381 0
                vtc_fatal(vl, "Macro ${%.*s} failed: %s",
382 0
                    (int)(e - b), b, err);
383
384 13605
        if (retval == NULL) {
385 73
                if (!ign_unknown_macro)
386 0
                        vtc_fatal(vl, "Macro ${%.*s} not found",
387 0
                            (int)(e - b), b);
388 73
                VSB_printf(vsb, "${%.*s}", (int)(e - b), b);
389 73
                return;
390 73
        }
391
392 13532
        VSB_cat(vsb, retval);
393 13532
        free(retval);
394 13532
}
395
396
struct vsb *
397
macro_expandf(struct vtclog *vl, const char *fmt, ...)
398 1628
{
399 1628
        va_list ap;
400 1628
        struct vsb *vsb1, *vsb2;
401
402 1628
        vsb1 = VSB_new_auto();
403 1628
        AN(vsb1);
404 1628
        va_start(ap, fmt);
405 1628
        VSB_vprintf(vsb1, fmt, ap);
406 1628
        va_end(ap);
407 1628
        AZ(VSB_finish(vsb1));
408 1628
        vsb2 = macro_expand(vl, VSB_data(vsb1));
409 1628
        VSB_destroy(&vsb1);
410 1628
        return (vsb2);
411 1628
}
412
413
struct vsb *
414
macro_expand(struct vtclog *vl, const char *text)
415 10185
{
416 10185
        struct vsb *vsb;
417 10185
        const char *p, *q;
418
419 10185
        vsb = VSB_new_auto();
420 10185
        AN(vsb);
421 16754
        while (*text != '\0') {
422 14587
                p = strstr(text, "${");
423 14587
                if (p == NULL) {
424 8018
                        VSB_cat(vsb, text);
425 8018
                        break;
426 8018
                }
427 6569
                VSB_bcat(vsb, text, p - text);
428 6569
                q = strchr(p, '}');
429 6569
                if (q == NULL) {
430 0
                        VSB_cat(vsb, text);
431 0
                        break;
432 0
                }
433 6569
                assert(p[0] == '$');
434 6569
                assert(p[1] == '{');
435 6569
                assert(q[0] == '}');
436 6569
                p += 2;
437 6569
                macro_cat(vl, vsb, p, q);
438 6569
                text = q + 1;
439 6569
        }
440 10185
        AZ(VSB_finish(vsb));
441 10185
        return (vsb);
442 10185
}
443
444
/**********************************************************************
445
 * Parse a string
446
 *
447
 * We make a copy of the string and deliberately leak it, so that all
448
 * the cmd functions we call don't have to strdup(3) all over the place.
449
 *
450
 * Static checkers like Coverity may bitch about this, but we don't care.
451
 */
452
453
454
void
455
parse_string(struct vtclog *vl, void *priv, const char *spec)
456 11099
{
457 11099
        char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
458 11099
        struct vsb *token_exp;
459 11099
        char *e, *p, *q, *f, *buf;
460 11099
        int nest_brace;
461 11099
        int tn;
462 11099
        unsigned n, m;
463 11099
        const struct cmds *cp;
464
465 11099
        AN(spec);
466 11099
        buf = strdup(spec);
467 11099
        AN(buf);
468 11099
        e = strchr(buf, '\0');
469 11099
        AN(e);
470 133710
        for (p = buf; p < e; p++) {
471 122634
                if (vtc_error || vtc_stop) {
472 22
                        vtc_log(vl, 1, "Aborting execution, test %s",
473 22
                            vtc_error ? "failed" : "ended");
474 22
                        break;
475 22
                }
476
                /* Start of line */
477 122611
                if (isspace(*p))
478 73927
                        continue;
479 48684
                if (*p == '\n')
480 0
                        continue;
481
482 48684
                if (*p == '#') {
483 103767
                        for (; *p != '\0' && *p != '\n'; p++)
484 101255
                                ;
485 2512
                        if (*p == '\0')
486 0
                                break;
487 2512
                        continue;
488 2512
                }
489
490 46172
                q = strchr(p, '\n');
491 46172
                if (q == NULL)
492 402
                        q = strchr(p, '\0');
493 46172
                if (q - p > 60)
494 3220
                        vtc_log(vl, 2, "=== %.60s...", p);
495 42952
                else
496 42952
                        vtc_log(vl, 2, "=== %.*s", (int)(q - p), p);
497
498
                /* First content on line, collect tokens */
499 46172
                memset(token_s, 0, sizeof token_s);
500 46172
                memset(token_e, 0, sizeof token_e);
501 46172
                tn = 0;
502 46172
                f = p;
503 307091
                while (p < e) {
504 306688
                        assert(tn < MAX_TOKENS);
505 306688
                        assert(p < e);
506 306688
                        if (*p == '\n') { /* End on NL */
507 45769
                                break;
508 45769
                        }
509 260919
                        if (isspace(*p)) { /* Inter-token whitespace */
510 110167
                                p++;
511 110167
                                continue;
512 110167
                        }
513 150752
                        if (*p == '\\' && p[1] == '\n') { /* line-cont */
514 1171
                                p += 2;
515 1171
                                continue;
516 1171
                        }
517 149581
                        if (*p == '"') { /* quotes */
518 14535
                                token_s[tn] = ++p;
519 14535
                                q = p;
520 332868
                                for (; *p != '\0'; p++) {
521 332852
                                        assert(p < e);
522 332852
                                        if (*p == '"')
523 14519
                                                break;
524 318333
                                        if (*p == '\\') {
525 5869
                                                p += VAV_BackSlash(p, q) - 1;
526 5869
                                                q++;
527 312464
                                        } else {
528 312464
                                                if (*p == '\n')
529 0
                                                        vtc_fatal(vl,
530 0
                                "Unterminated quoted string in line: %*.*s",
531 0
                                (int)(p - f), (int)(p - f), f);
532 312464
                                                assert(*p != '\n');
533 312464
                                                *q++ = *p;
534 312464
                                        }
535 318333
                                }
536 14535
                                token_e[tn++] = q;
537 14535
                                p++;
538 135046
                        } else if (*p == '{') { /* Braces */
539 8587
                                nest_brace = 0;
540 8587
                                token_s[tn] = p + 1;
541 1402675
                                for (; p < e; p++) {
542 1402675
                                        if (*p == '{')
543 15837
                                                nest_brace++;
544 1386838
                                        else if (*p == '}') {
545 15837
                                                if (--nest_brace == 0)
546 8587
                                                        break;
547 15837
                                        }
548 1402675
                                }
549 8587
                                assert(*p == '}');
550 8587
                                token_e[tn++] = p++;
551 126459
                        } else { /* other tokens */
552 126459
                                token_s[tn] = p;
553 4910433
                                for (; p < e && !isspace(*p); p++)
554 4783974
                                        continue;
555 126459
                                token_e[tn++] = p;
556 126459
                        }
557 149581
                }
558
559 46172
                assert(p <= e);
560 46172
                assert(tn < MAX_TOKENS);
561 46172
                token_s[tn] = NULL;
562 195767
                for (tn = 0; token_s[tn] != NULL; tn++) {
563 149595
                        AN(token_e[tn]);        /*lint !e771 */
564 149595
                        *token_e[tn] = '\0';    /*lint !e771 */
565 149595
                        if (NULL != strstr(token_s[tn], "${")) {
566 1215
                                token_exp = macro_expand(vl, token_s[tn]);
567 1215
                                if (vtc_error)
568 0
                                        return;
569 1215
                                token_s[tn] = VSB_data(token_exp);
570 1215
                                token_e[tn] = strchr(token_s[tn], '\0');
571 1215
                        }
572 149595
                }
573
574
575
/* SECTION: loop loop
576
 *
577
 * loop NUMBER STRING
578
 *         Process STRING as a specification, NUMBER times.
579
 *
580
 * This works inside all specification strings
581
 *
582
 * The special macro ${n} is expanded in STRING to the
583
 * the iteration [1…NUMBER].
584
 */
585
586 46172
                if (!vstrcmp(token_s[0], "loop")) {
587 67
                        n = strtoul(token_s[1], NULL, 0);
588 4249
                        for (m = 0; m < n && !vtc_error && !vtc_stop; m++) {
589 4182
                                vtc_log(vl, 4, "Loop #%u", m);
590 4182
                                macro_def(vl, NULL, "n", "%d", m + 1);
591 4182
                                token_exp = macro_expand(vl, token_s[2]);
592 4182
                                parse_string(vl, priv, VSB_data(token_exp));
593 4182
                                VSB_destroy(&token_exp);
594 4182
                        }
595 67
                        macro_def(vl, NULL, "n", "${n}");
596 67
                        continue;
597 67
                }
598
599 46105
                cp = NULL;
600 46105
                if (vl->cmds != NULL) {
601 747469
                        for (cp = vl->cmds; cp->name != NULL; cp++) {
602 746465
                                CHECK_OBJ_NOTNULL(cp, CMDS_MAGIC);
603 746465
                                if (!vstrcmp(token_s[0], cp->name))
604 33150
                                        break;
605 746465
                        }
606 34154
                }
607
608 46105
                if (cp == NULL || cp->name == NULL) {
609 12953
                        cp = find_cmd(token_s[0]);
610 12953
                        if (cp != NULL && vl->cmds != NULL &&
611 12953
                            !(cp->flags & CMDS_F_GLOBAL))
612 0
                                cp = NULL;
613 12953
                }
614
615 46105
                if (cp == NULL)
616 0
                        vtc_fatal(vl, "Unknown command: \"%s\"", token_s[0]);
617
618 46105
                AN(cp->name);
619 46105
                AN(cp->cmd);
620 46105
                cp->cmd(token_s, priv, vl);
621 46105
        }
622 11099
}
623
624
/**********************************************************************
625
 * Reset commands (between tests)
626
 */
627
628
static void
629
reset_cmd(const char *name)
630 2244
{
631 2244
        struct cmds *cp;
632
633 2244
        cp = find_cmd(name);
634 2244
        AN(cp);
635 2244
        cp->cmd(NULL, NULL, NULL);
636 2244
        cp->flags |= CMDS_F_SHUT;
637 2244
}
638
639
static void
640
reset_cmds(void)
641 1122
{
642 1122
        struct icmds *cp;
643
644
        // Cleanup is easier if the sockets are closed first.
645 1122
        reset_cmd("client");
646 1122
        reset_cmd("server");
647
648 22440
        VTAILQ_FOREACH(cp, &icmd_list, list) {
649 22440
                CHECK_OBJ_NOTNULL(cp, ICMDS_MAGIC);
650 22440
                CHECK_OBJ_NOTNULL(cp->cmd, CMDS_MAGIC);
651 22440
                if (cp->cmd->flags & CMDS_F_SHUT) {
652 2244
                        cp->cmd->flags &= ~CMDS_F_SHUT;
653 20196
                } else {
654 20196
                        cp->cmd->cmd(NULL, NULL, NULL);
655 20196
                }
656 22440
        }
657 1122
}
658
659
/**********************************************************************
660
 * Execute a file
661
 */
662
663
int
664
fail_out(void)
665 1122
{
666 1122
        unsigned old_err;
667 1122
        static int once = 0;
668
669 1122
        if (once++) {
670 0
                vtc_log(vltop, 1, "failure during reset");
671 0
                return (vtc_error);
672 0
        }
673 1122
        old_err = vtc_error;
674 1122
        if (!vtc_stop)
675 1098
                vtc_stop = 1;
676 1122
        vtc_log(vltop, 1, "RESETTING after %s", tfn);
677 1122
        reset_cmds();
678 1122
        vtc_error |= old_err;
679
680 1122
        if (vtc_error)
681 3
                vtc_log(vltop, 1, "TEST %s FAILED", tfn);
682 1119
        else
683 1119
                vtc_log(vltop, 1, "TEST %s completed", tfn);
684
685 1122
        if (vtc_stop > 1)
686 24
                return (1);
687 1098
        return (vtc_error);
688 1122
}
689
690
int
691
exec_file(const char *fn, const char *script, const char *tmpdir,
692
    char *logbuf, unsigned loglen)
693 1122
{
694 1122
        FILE *f;
695 1122
        struct vsb *vsb;
696 1122
        const char *p;
697
698 1122
        AN(tmpdir);
699
700 1122
        (void)signal(SIGPIPE, SIG_IGN);
701
702 1122
        VSUB_closefrom(STDERR_FILENO + 1);
703
704 1122
        PTOK(pthread_mutex_init(&vtc_vrnd_mtx, NULL));
705 1122
        VRND_Lock = vtc_vrnd_lock;
706 1122
        VRND_Unlock = vtc_vrnd_unlock;
707 1122
        VRND_SeedAll();
708
709 1122
        tfn = fn;
710 1122
        vtc_loginit(logbuf, loglen);
711 1122
        vltop = vtc_logopen("top");
712 1122
        AN(vltop);
713
714 1122
        vtc_log(vltop, 1, "TEST %s starting", fn);
715
716 1122
        init_cmd_list();
717 1122
        init_macro();
718 1122
        init_server();
719 1122
        init_syslog();
720 1122
        init_tunnel();
721
722 1122
        vsb = VSB_new_auto();
723 1122
        AN(vsb);
724 1122
        if (*fn != '/')
725 1111
                macro_cat(vltop, vsb, "pwd", NULL);
726 1122
        p = strrchr(fn, '/');
727 1122
        if (p != NULL) {
728 1113
                VSB_putc(vsb, '/');
729 1113
                VSB_bcat(vsb, fn, p - fn);
730 1113
        }
731 1122
        if (VSB_len(vsb) == 0)
732 0
                VSB_putc(vsb, '/');
733 1122
        AZ(VSB_finish(vsb));
734 1122
        macro_def(vltop, NULL, "testdir", "%s", VSB_data(vsb));
735 1122
        VSB_destroy(&vsb);
736
737
        /* Move into our tmpdir */
738 1122
        AZ(chdir(tmpdir));
739 1122
        macro_def(vltop, NULL, "tmpdir", "%s", tmpdir);
740 1122
        p = strrchr(tmpdir, '/');
741 1122
        AN(p);
742 1122
        p++;
743 1122
        AN(*p);
744 1122
        macro_def(vltop, NULL, "vtcid", "%s", p);
745
746
        /* Drop file to tell what was going on here */
747 1122
        f = fopen("INFO", "w");
748 1122
        AN(f);
749 1122
        fprintf(f, "Test case: %s\n", fn);
750 1122
        AZ(fclose(f));
751
752 1122
        vtc_stop = 0;
753
754 1122
        macro_def(vltop, NULL, "n", "${n}");
755
756 1122
        vtc_thread = pthread_self();
757
        parse_string(vltop, NULL, script);
758 1122
        return (fail_out());
759 1122
}