Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_main.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/mman.h>
33
#include <sys/socket.h>
34
#include <sys/stat.h>
35
#include <sys/wait.h>
36
37
#include <ctype.h>
38
#include <dirent.h>
39
#include <poll.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <unistd.h>
44
45
#include "vtc.h"
46
47
#include "vev.h"
48
#include "vfil.h"
49
#include "vnum.h"
50
#include "vrnd.h"
51
#include "vsa.h"
52
#include "vss.h"
53
#include "vtcp.h"
54
#include "vtim.h"
55
#include "vct.h"
56
57
#include "version.h"
58
59
static const char *argv0;
60
61
struct buf {
62
        unsigned                magic;
63
#define BUF_MAGIC               0x39d1258a
64
        VTAILQ_ENTRY(buf)       list;
65
        char                    *buf;
66
        struct vsb              *diag;
67
        size_t                  bufsiz;
68
};
69
70
static VTAILQ_HEAD(, buf) free_bufs = VTAILQ_HEAD_INITIALIZER(free_bufs);
71
72
struct vtc_tst {
73
        unsigned                magic;
74
#define TST_MAGIC               0x618d8b88
75
        VTAILQ_ENTRY(vtc_tst)   list;
76
        const char              *filename;
77
        char                    *script;
78
        unsigned                ntodo;
79
        unsigned                nwait;
80
};
81
82
struct vtc_job {
83
        unsigned                magic;
84
#define JOB_MAGIC               0x1b5fc419
85
        struct vtc_tst          *tst;
86
        pid_t                   child;
87
        struct vev              *ev;
88
        struct vev              *evt;
89
        struct buf              *bp;
90
        char                    *tmpdir;
91
        double                  t0;
92
        int                     killed;
93
};
94
95
96
int iflg = 0;
97
vtim_dur vtc_maxdur = 60;
98
static unsigned vtc_bufsiz = 1024 * 1024;
99
100
static VTAILQ_HEAD(, vtc_tst) tst_head = VTAILQ_HEAD_INITIALIZER(tst_head);
101
static struct vev_root *vb;
102
static int njob = 0;
103
static int npar = 1;                    /* Number of parallel tests */
104
static int vtc_continue;                /* Continue on error */
105
static int vtc_verbosity = 1;           /* Verbosity Level */
106
static int vtc_good;
107
static int vtc_fail;
108
static int vtc_skip;
109
static int vtc_use_cleaner = 0;
110
static int vtc_ntest = 1;               /* Run tests this many times */
111
static char *tmppath;
112
static char *cwd = NULL;
113
char *vmod_path = NULL;
114
struct vsb *params_vsb = NULL;
115
int leave_temp;
116
static struct vsb *cbvsb;
117
static int bad_backend_fd;
118
119
static int cleaner_fd = -1;
120
static pid_t cleaner_pid;
121
const char *default_listen_addr;
122
123
static int has_color = 0;
124
125
/**********************************************************************
126
 * autocrap test-driver command arguments
127
 */
128
129
#define TDARGS(MACRO) \
130 9504
        MACRO("--test-name",            td_test_name) \
131 9504
        MACRO("--log-file",             td_log_file) \
132 8448
        MACRO("--trs-file",             td_trs_file) \
133 7392
        MACRO("--color-tests",          td_color_tests) \
134 6336
        MACRO("--collect_skipped_logs", td_collect_skipped_logs) \
135 5280
        MACRO("--expect-failure",       td_expect_failure) \
136 5280
        MACRO("--enable-hard-errors",   td_enable_hard_errors)
137
138
#define TDVAR(name, dst) static const char *dst;
139
        TDARGS(TDVAR)
140
#undef TDVAR
141
142
/**********************************************************************/
143
144
static struct buf *
145
get_buf(void)
146 1122
{
147 1122
        struct buf *bp;
148
149 1122
        bp = VTAILQ_FIRST(&free_bufs);
150 1122
        CHECK_OBJ_ORNULL(bp, BUF_MAGIC);
151 1122
        if (bp != NULL) {
152 48
                VTAILQ_REMOVE(&free_bufs, bp, list);
153 48
                VSB_clear(bp->diag);
154 1074
        } else {
155 1074
                ALLOC_OBJ(bp, BUF_MAGIC);
156 1074
                AN(bp);
157 1074
                bp->bufsiz = vtc_bufsiz;
158 1074
                bp->buf = mmap(NULL, bp->bufsiz, PROT_READ|PROT_WRITE,
159 1074
                    MAP_ANON | MAP_SHARED, -1, 0);
160 1074
                assert(bp->buf != MAP_FAILED);
161 1074
                bp->diag = VSB_new_auto();
162 1074
                AN(bp->diag);
163 1074
        }
164 1122
        memset(bp->buf, 0, bp->bufsiz);
165 1122
        return (bp);
166 1122
}
167
168
static void
169
rel_buf(struct buf **bp)
170 1122
{
171 1122
        CHECK_OBJ_NOTNULL(*bp, BUF_MAGIC);
172
173 1122
        VTAILQ_INSERT_HEAD(&free_bufs, (*bp), list);
174 1122
        *bp = NULL;
175 1122
}
176
177
/**********************************************************************
178
 * Parse a -D option argument into a name/val pair, and insert
179
 * into extmacro list
180
 */
181
182
static int
183
parse_D_opt(char *arg)
184 2
{
185 2
        char *p, *q;
186
187 2
        p = arg;
188 2
        q = strchr(p, '=');
189 2
        if (!q)
190 0
                return (0);
191 2
        *q++ = '\0';
192 2
        extmacro_def(p, NULL, "%s", q);
193
194 2
        return (1);
195 2
}
196
197
/**********************************************************************
198
 * Print usage
199
 */
200
201
static void v_noreturn_
202
usage(void)
203 5
{
204 5
        fprintf(stderr, "usage: %s [options] file ...\n", argv0);
205 75
#define FMT "    %-28s # %s\n"
206 5
        fprintf(stderr, FMT, "-b size",
207 5
            "Set internal buffer size (default: 1M)");
208 5
        fprintf(stderr, FMT, "-C", "Use cleaner subprocess");
209 5
        fprintf(stderr, FMT, "-D name=val", "Define macro");
210 5
        fprintf(stderr, FMT, "-E extension_shlib", "Load extension");
211 5
        fprintf(stderr, FMT, "-i", "Find varnish binaries in build tree");
212 5
        fprintf(stderr, FMT, "-j jobs", "Run this many tests in parallel");
213 5
        fprintf(stderr, FMT, "-k", "Continue on test failure");
214 5
        fprintf(stderr, FMT, "-L", "Always leave temporary vtc.*");
215 5
        fprintf(stderr, FMT, "-l", "Leave temporary vtc.* if test fails");
216 5
        fprintf(stderr, FMT, "-n iterations", "Run tests this many times");
217 5
        fprintf(stderr, FMT, "-p name=val", "Pass a varnishd parameter");
218 5
        fprintf(stderr, FMT, "-q", "Quiet mode: report only failures");
219 5
        fprintf(stderr, FMT, "-t duration", "Time tests out after this long");
220 5
        fprintf(stderr, FMT, "-v", "Verbose mode: always report test log");
221 5
        fprintf(stderr, FMT, "-V", "print version and exit");
222 5
        exit(1);
223 5
}
224
225
/**********************************************************************
226
 * When running many tests, cleaning the tmpdir with "rm -rf" becomes
227
 * chore which limits our performance.
228
 * When the number of tests are above 100, we spawn a child-process
229
 * to do that for us.
230
 */
231
232
static void
233
cleaner_do(const char *dirname)
234 1122
{
235 1122
        char buf[BUFSIZ];
236
237 1122
        AZ(vmemcmp(dirname, tmppath, vstrlen(tmppath)));
238 1122
        if (cleaner_pid > 0) {
239 56
                bprintf(buf, "%s\n", dirname);
240 56
                assert(write(cleaner_fd, buf, vstrlen(buf)) == vstrlen(buf));
241 56
                return;
242 56
        }
243 1066
        bprintf(buf, "exec /bin/rm -rf %s\n", dirname);
244 1066
        AZ(system(buf));
245 1066
}
246
247
static void
248
cleaner_setup(void)
249 2
{
250 2
        int p[2], st;
251 2
        char buf[BUFSIZ];
252 2
        char *q;
253 2
        pid_t pp;
254
255 2
        AZ(pipe(p));
256 2
        assert(p[0] > STDERR_FILENO);
257 2
        assert(p[1] > STDERR_FILENO);
258 2
        cleaner_pid = fork();
259 2
        assert(cleaner_pid >= 0);
260 2
        if (cleaner_pid == 0) {
261 2
                closefd(&p[1]);
262 2
                (void)!nice(1);         /* Not important */
263 2
                setbuf(stdin, NULL);
264 2
                AZ(dup2(p[0], STDIN_FILENO));
265 56
                while (fgets(buf, sizeof buf, stdin)) {
266 56
                        AZ(vmemcmp(buf, tmppath, vstrlen(tmppath)));
267 56
                        q = buf + vstrlen(buf);
268 56
                        assert(q > buf);
269 56
                        assert(q[-1] == '\n');
270 56
                        q[-1] = '\0';
271
272
                        /* Dont expend a shell on running /bin/rm */
273 56
                        pp = fork();
274 56
                        assert(pp >= 0);
275 56
                        if (pp == 0)
276 56
                                exit(execlp(
277 56
                                    "rm", "rm", "-rf", buf, (char*)0));
278 0
                        assert(waitpid(pp, &st, 0) == pp);
279 0
                        AZ(st);
280 0
                }
281 18446744073709551562
                exit(0);
282 2
        }
283 0
        closefd(&p[0]);
284 0
        cleaner_fd = p[1];
285 0
}
286
287
static void
288
cleaner_neuter(void)
289 1122
{
290 1122
        if (cleaner_pid > 0)
291 56
                closefd(&cleaner_fd);
292 1122
}
293
294
static void
295
cleaner_finish(void)
296 1067
{
297 1067
        int st;
298
299 1067
        if (cleaner_pid > 0) {
300 2
                closefd(&cleaner_fd);
301 2
                assert(waitpid(cleaner_pid, &st, 0) == cleaner_pid);
302 2
                AZ(st);
303 2
        }
304 1067
}
305
306
/**********************************************************************
307
 * CallBack
308
 */
309
310
// from automake, ^[ == \x1b
311
static const char * col_red="\x1b[0;31m";
312
static const char * col_grn="\x1b[0;32m";
313
static const char * col_blu="\x1b[1;34m";
314
static const char * col_std="\x1b[m";
315
#ifdef COL_UNUSED
316
static const char * col_lgn="\x1b[1;32m";
317
static const char * col_mgn="\x1b[0;35m";
318
static const char * col_brg="\x1b[1m";
319
#endif
320
321
static int
322
tst_cb(const struct vev *ve, int what)
323 1131
{
324 1131
        struct vtc_job *jp;
325 1131
        char buf[BUFSIZ];
326 1131
        int ecode;
327 1131
        int i, stx;
328 1131
        pid_t px;
329 1131
        double t;
330 1131
        FILE *f;
331 1131
        char *p;
332
333 1131
        CAST_OBJ_NOTNULL(jp, ve->priv, JOB_MAGIC);
334 1131
        CHECK_OBJ_NOTNULL(jp->tst, TST_MAGIC);
335
336
        // printf("CB %p %s %d\n", ve, jp->tst->filename, what);
337 1131
        if (what == 0) {
338 0
                jp->killed = 1;
339 0
                AZ(kill(-jp->child, SIGKILL)); /* XXX: Timeout */
340 1131
        } else {
341 1131
                assert(what & (VEV__RD | VEV__HUP));
342 1131
        }
343
344 1131
        *buf = '\0';
345 1131
        i = read(ve->fd, buf, sizeof buf);
346 1131
        if (i > 0)
347 9
                VSB_bcat(jp->bp->diag, buf, i);
348 1131
        if (i == 0) {
349
350 1122
                njob--;
351 1122
                px = wait4(jp->child, &stx, 0, NULL);
352 1122
                assert(px == jp->child);
353 1122
                t = VTIM_mono() - jp->t0;
354 1122
                AZ(close(ve->fd));
355
356 1122
                ecode = WTERMSIG(stx);
357 1122
                if (ecode == 0)
358 1122
                        ecode = WEXITSTATUS(stx);
359
360 1122
                AZ(VSB_finish(jp->bp->diag));
361
362 1122
                VSB_clear(cbvsb);
363 1122
                VSB_cat(cbvsb, jp->bp->buf);
364 1122
                p = strchr(jp->bp->buf, '\0');
365 1122
                if (p > jp->bp->buf && p[-1] != '\n')
366 0
                        VSB_putc(cbvsb, '\n');
367 1122
                VSB_quote_pfx(cbvsb, "*    diag  0.0 ",
368 1122
                    VSB_data(jp->bp->diag), -1, VSB_QUOTE_NONL);
369 1122
                AZ(VSB_finish(cbvsb));
370 1122
                rel_buf(&jp->bp);
371
372 1122
                if ((ecode > 1 && vtc_verbosity) || vtc_verbosity > 1)
373 18
                        printf("%s", VSB_data(cbvsb));
374
375 1122
                if (!ecode)
376 1095
                        vtc_good++;
377 27
                else if (ecode == 1)
378 24
                        vtc_skip++;
379 3
                else
380 3
                        vtc_fail++;
381
382 1122
                if (leave_temp == 0 || (leave_temp == 1 && ecode <= 1)) {
383 1122
                        cleaner_do(jp->tmpdir);
384 1122
                } else {
385 0
                        bprintf(buf, "%s/LOG", jp->tmpdir);
386 0
                        f = fopen(buf, "w");
387 0
                        AN(f);
388 0
                        (void)fprintf(f, "%s\n", VSB_data(cbvsb));
389 0
                        AZ(fclose(f));
390 0
                }
391 1122
                free(jp->tmpdir);
392
393 1122
                if (td_log_file != NULL) {
394 1056
                        f = fopen(td_log_file, "w");
395 1056
                        AN(f);
396 1056
                        (void)fprintf(f, "%s\n", VSB_data(cbvsb));
397 1056
                        AZ(fclose(f));
398 1056
                }
399 1122
                if (td_trs_file != NULL) {
400 1056
                        f = fopen(td_trs_file, "w");
401 1056
                        AN(f);
402 1056
                        if (jp->killed || ecode > 1) {
403 0
                                printf("%sFAIL%s: %s\n",
404 0
                                    col_red, col_std, jp->tst->filename);
405 0
                                fprintf(f, ":test-result: FAIL\n");
406 0
                                fprintf(f, ":copy-in-global-log: yes\n");
407 0
                                fprintf(f, ":recheck: yes\n");
408 1056
                        } else if (ecode) {
409 17
                                printf("%sSKIP%s: %s\n",
410 17
                                    col_blu, col_std, jp->tst->filename);
411 17
                                fprintf(f, ":test-result: SKIP\n");
412 17
                                fprintf(f, ":copy-in-global-log: yes\n");
413 17
                                fprintf(f, ":recheck: no\n");
414 1039
                        } else {
415 1039
                                printf("%sPASS%s: %s\n",
416 1039
                                    col_grn, col_std, jp->tst->filename);
417 1039
                                fprintf(f, ":test-result: PASS\n");
418 1039
                                fprintf(f, ":copy-in-global-log: no\n");
419 1039
                                fprintf(f, ":recheck: no\n");
420 1039
                        }
421 1056
                        AZ(fclose(f));
422 1056
                } else {
423 66
                        if (jp->killed) {
424 0
                                printf(
425 0
                                    "#    top  TEST %s TIMED OUT (kill -9)\n",
426 0
                                    jp->tst->filename);
427 0
                        }
428 66
                        if (ecode > 1) {
429 3
                                printf("#    top  TEST %s FAILED (%.3f)",
430 3
                                    jp->tst->filename, t);
431 3
                                if (WIFSIGNALED(stx))
432 0
                                        printf(" signal=%d\n", WTERMSIG(stx));
433 3
                                else if (WIFEXITED(stx))
434 3
                                        printf(" exit=%d\n", WEXITSTATUS(stx));
435 3
                                if (!vtc_continue) {
436
                                        /* XXX kill -9 other jobs ? */
437 3
                                        exit(2);
438 3
                                }
439 63
                        } else if (vtc_verbosity) {
440 61
                                printf("#    top  TEST %s %s (%.3f)\n",
441 61
                                    jp->tst->filename,
442 61
                                    ecode ? "skipped" : "passed", t);
443 61
                        }
444 66
                }
445 1119
                if (jp->evt != NULL) {
446 1119
                        VEV_Stop(vb, jp->evt);
447 1119
                        free(jp->evt);
448 1119
                }
449 1119
                jp->tst->nwait--;
450 1119
                if (jp->tst->nwait == 0) {
451 1110
                        free(jp->tst->script);
452 1110
                        FREE_OBJ(jp->tst);
453 1110
                }
454 1119
                FREE_OBJ(jp);
455 1119
                return (1);
456 1122
        }
457 9
        return (0);
458 1131
}
459
460
/**********************************************************************
461
 * Start Test
462
 */
463
464
static void
465
start_test(void)
466 1122
{
467 1122
        struct vtc_tst *tp;
468 1122
        int p[2], retval;
469 1122
        struct vtc_job *jp;
470 1122
        char tmpdir[PATH_MAX];
471 1122
        char default_n[PATH_MAX];
472
473 1122
        ALLOC_OBJ(jp, JOB_MAGIC);
474 1122
        AN(jp);
475
476 1122
        jp->bp = get_buf();
477
478 1122
        bprintf(tmpdir, "%s/vtc.%d.%08x", tmppath, (int)getpid(),
479 1122
                (unsigned)random());
480 1122
        AZ(mkdir(tmpdir, 0755));
481
482 1122
        bprintf(default_n, "%s/default_n", tmpdir);
483
484 1122
        AZ(setenv("VARNISH_DEFAULT_N", default_n, 1));
485
486 1122
        tp = VTAILQ_FIRST(&tst_head);
487 1122
        CHECK_OBJ_NOTNULL(tp, TST_MAGIC);
488 1122
        AN(tp->ntodo);
489 1122
        tp->ntodo--;
490 1122
        VTAILQ_REMOVE(&tst_head, tp, list);
491 1122
        if (tp->ntodo > 0)
492 9
                VTAILQ_INSERT_TAIL(&tst_head, tp, list);
493
494 1122
        jp->tst = tp;
495 1122
        REPLACE(jp->tmpdir, tmpdir);
496
497 1122
        AZ(pipe(p));
498 1122
        assert(p[0] > STDERR_FILENO);
499 1122
        assert(p[1] > STDERR_FILENO);
500 1122
        jp->t0 = VTIM_mono();
501 1122
        jp->child = fork();
502 1122
        assert(jp->child >= 0);
503 1122
        if (jp->child == 0) {
504 1122
                cleaner_neuter();       // Too dangerous to have around
505 1122
                AZ(setpgid(getpid(), 0));
506 1122
                VFIL_null_fd(STDIN_FILENO);
507 1122
                assert(dup2(p[1], STDOUT_FILENO) == STDOUT_FILENO);
508 1122
                assert(dup2(p[1], STDERR_FILENO) == STDERR_FILENO);
509 1122
                retval = exec_file(jp->tst->filename, jp->tst->script,
510 1122
                    jp->tmpdir, jp->bp->buf, jp->bp->bufsiz);
511 1122
                exit(retval);
512 1122
        }
513 0
        closefd(&p[1]);
514
515 0
        jp->ev = VEV_Alloc();
516 0
        AN(jp->ev);
517 0
        jp->ev->fd_flags = VEV__RD | VEV__HUP | VEV__ERR;
518 0
        jp->ev->fd = p[0];
519 0
        jp->ev->priv = jp;
520 0
        jp->ev->callback = tst_cb;
521 0
        AZ(VEV_Start(vb, jp->ev));
522
523 0
        jp->evt = VEV_Alloc();
524 0
        AN(jp->evt);
525 0
        jp->evt->fd = -1;
526 0
        jp->evt->timeout = vtc_maxdur;
527 0
        jp->evt->priv = jp;
528 0
        jp->evt->callback = tst_cb;
529 0
        AZ(VEV_Start(vb, jp->evt));
530 0
}
531
532
/**********************************************************************
533
 * i-mode = "we're inside a src-tree"
534
 *
535
 * Find the abs path to top of source dir from Makefile, if that
536
 * fails, fall back on "../../"
537
 *
538
 * Set PATH to all programs build directories
539
 * Set vmod_path to all vmods build directories
540
 *
541
 */
542
543
static char *
544
top_dir(const char *makefile, const char *top_var)
545 2114
{
546 2114
        const char *b, *e;
547 2114
        char *var;
548
549 2114
        AN(makefile);
550 2114
        AN(top_var);
551 2114
        assert(*top_var == '\n');
552
553 2114
        b = strstr(makefile, top_var);
554 2114
        top_var++;
555
556 2114
        if (b == NULL) {
557 0
                fprintf(stderr, "could not find '%s' in Makefile\n", top_var);
558 0
                return (NULL);
559 0
        }
560
561 2114
        e = strchr(b + 1, '\n');
562 2114
        if (e == NULL) {
563 0
                fprintf(stderr, "No NL after '%s' in Makefile\n", top_var);
564 0
                return (NULL);
565 0
        }
566
567 2114
        b = vmemchr(b, '/', e - b);
568 2114
        if (b == NULL) {
569 0
                fprintf(stderr, "No '/' after '%s' in Makefile\n", top_var);
570 0
                return (NULL);
571 0
        }
572 2114
        var = strndup(b, e - b);
573 2114
        AN(var);
574 2114
        return (var);
575 2114
}
576
577
static void
578
build_path(const char *topdir, const char *subdir,
579
    const char *pfx, const char *sfx, struct vsb *vsb)
580 3171
{
581 3171
        char buf[PATH_MAX];
582 3171
        DIR *dir;
583 3171
        struct dirent *de;
584 3171
        struct stat st;
585 3171
        const char *topsep = "";
586
587 3171
        if (*subdir != '\0')
588 2114
                topsep = "/";
589 3171
        bprintf(buf, "%s%s%s/", topdir, topsep, subdir);
590 3171
        dir = opendir(buf);
591 3171
        XXXAN(dir);
592 271652
        while (1) {
593 271652
                de = readdir(dir);
594 271652
                if (de == NULL)
595 3171
                        break;
596 268481
                if (vstrncmp(de->d_name, pfx, vstrlen(pfx)))
597 257911
                        continue;
598 10570
                bprintf(buf, "%s%s%s/%s", topdir, topsep, subdir, de->d_name);
599 10570
                if (!stat(buf, &st) && S_ISDIR(st.st_mode)) {
600 10570
                        VSB_cat(vsb, buf);
601 10570
                        VSB_cat(vsb, sfx);
602 10570
                        VSB_cat(vsb, ":");
603 10570
                }
604 10570
        }
605 3171
        AZ(closedir(dir));
606 3171
}
607
608
struct i_mode_cfg {
609
        unsigned                        magic;
610
#define I_MODE_CFG_MAGIC                0xbc9d1820
611
        enum i_mode_where_e             where;
612
        const char                      *subdir;
613
        const char                      *pfx;
614
        const char                      *sfx;
615
        VTAILQ_ENTRY(i_mode_cfg)        list;
616
};
617
618
static VTAILQ_HEAD(, i_mode_cfg) i_mode_head = VTAILQ_HEAD_INITIALIZER(i_mode_head);
619
620
static void
621
i_mode(void)
622 1057
{
623 1057
        struct vsb *vsb;
624 1057
        char *p, *topbuild, *topsrc;
625
626
        /*
627
         * This code has a rather intimate knowledge of auto* generated
628
         * makefiles.
629
         */
630
631 1057
        vsb = VSB_new_auto();
632 1057
        AN(vsb);
633
634 1057
        p = VFIL_readfile(NULL, "Makefile", NULL);
635 1057
        if (p == NULL) {
636 0
                fprintf(stderr, "No Makefile to search for -i flag.\n");
637 0
                exit(2);
638 0
        }
639
640 1057
        topbuild = top_dir(p, "\nabs_top_builddir");
641 1057
        topsrc = top_dir(p, "\nabs_top_srcdir");
642 1057
        free(p);
643 1057
        if (topbuild == NULL || topsrc == NULL) {
644 0
                free(topbuild);
645 0
                free(topsrc);
646 0
                exit(2);
647 0
        }
648 1057
        extmacro_def("topbuild", NULL, "%s", topbuild);
649 1057
        extmacro_def("topsrc", NULL, "%s", topsrc);
650
651
        /*
652
         * Build $PATH which can find all programs in the build tree
653
         */
654 1057
        VSB_clear(vsb);
655 1057
        VSB_cat(vsb, "PATH=");
656 1057
#ifdef WITH_CONTRIB
657 1057
        build_path(topsrc, "", "contrib", "", vsb);
658 1057
        VSB_putc(vsb, ':');
659 1057
#endif
660
661 1057
        struct i_mode_cfg *cfg;
662 1057
        VTAILQ_FOREACH(cfg, &i_mode_head, list) {
663 1057
                const char *top = cfg->where == top_builddir ? topbuild : topsrc;
664 1057
                build_path(top, cfg->subdir, cfg->pfx, cfg->sfx, vsb);
665 1057
                VSB_putc(vsb, ':');
666 1057
        }
667
668 1057
        VSB_printf(vsb, "%s", getenv("PATH"));
669 1057
        AZ(VSB_finish(vsb));
670 1057
        AZ(putenv(strdup(VSB_data(vsb))));
671
672
        /*
673
         * Build vmod_path which can find all VMODs in the build tree
674
         */
675
676 1057
        VSB_clear(vsb);
677 1057
        build_path(topbuild, "vmod", ".libs", "", vsb);
678 1057
        AZ(VSB_finish(vsb));
679 1057
        vmod_path = strdup(VSB_data(vsb));
680 1057
        AN(vmod_path);
681
682 1057
        free(topbuild);
683 1057
        free(topsrc);
684 1057
        VSB_destroy(&vsb);
685
686
        /*
687
         * strict jemalloc checking
688
         */
689 1057
        AZ(putenv(strdup("MALLOC_CONF=abort:true,junk:true")));
690 1057
}
691
692
// to be called by extensions to register i_mode paths
693
// strings will be referenced
694
void
695
i_mode_add(enum i_mode_where_e where, const char *subdir, const char *pfx,
696
    const char *sfx)
697 1078
{
698 1078
        struct i_mode_cfg *i;
699
700 1078
        ALLOC_OBJ(i, I_MODE_CFG_MAGIC);
701 1078
        AN(i);
702 1078
        i->where = where;
703 1078
        i->subdir = subdir;
704 1078
        i->pfx = pfx;
705 1078
        i->sfx = sfx;
706
707 1078
        VTAILQ_INSERT_TAIL(&i_mode_head, i, list);
708 1078
}
709
710
711
/**********************************************************************
712
 * Figure out what IP related magic
713
 */
714
715
static void
716
ip_magic(void)
717 1070
{
718 1070
        const struct suckaddr *sa;
719 1070
        char abuf[VTCP_ADDRBUFSIZE];
720 1070
        char pbuf[VTCP_PORTBUFSIZE];
721 1070
        char *s;
722
723
        /*
724
         * In FreeBSD jails localhost/127.0.0.1 becomes the jails IP#
725
         * XXX: IPv6-only hosts would have similar issue, but it is not
726
         * XXX: obvious how to cope.  Ideally "127.0.0.1" would be
727
         * XXX: "localhost", but that doesn't work out of the box.
728
         * XXX: Things like "prefer_ipv6" parameter complicates things.
729
         */
730 1070
        sa = VSS_ResolveOne(NULL, "127.0.0.1", "0", 0, SOCK_STREAM, 0);
731 1070
        AN(sa);
732 1070
        bad_backend_fd = VTCP_bind(sa, NULL);
733 1070
        if (bad_backend_fd < 0) {
734 0
                VSA_free(&sa);
735 0
                sa = VSS_ResolveFirst(NULL, "localhost", "0", 0, SOCK_STREAM, 0);
736 0
                AN(sa);
737 0
                bad_backend_fd = VTCP_bind(sa, NULL);
738 0
        }
739 1070
        assert(bad_backend_fd >= 0);
740 1070
        VTCP_myname(bad_backend_fd, abuf, sizeof abuf, pbuf, sizeof(pbuf));
741 1070
        extmacro_def("localhost", NULL, "%s", abuf);
742 1070
        s = strdup(abuf);
743 1070
        AN(s);
744
745
#if defined (__APPLE__)
746
        /*
747
         * In macOS a bound socket that is not listening will timeout
748
         * instead of refusing the connection so close it and hope
749
         * for the best.
750
         */
751
        VTCP_close(&bad_backend_fd);
752
#endif
753
754
        /* Expose a backend that is forever down. */
755 1070
        if (VSA_Get_Proto(sa) == AF_INET)
756 1070
                extmacro_def("bad_backend", NULL, "%s:%s", abuf, pbuf);
757 0
        else
758 0
                extmacro_def("bad_backend", NULL, "[%s]:%s", abuf, pbuf);
759
760
        /* our default bind/listen address */
761 1070
        if (VSA_Get_Proto(sa) == AF_INET)
762 1070
                bprintf(abuf, "%s:0", s);
763 0
        else
764 0
                bprintf(abuf, "[%s]:0", s);
765 1070
        free(s);
766
767 1070
        extmacro_def("listen_addr", NULL, "%s", abuf);
768 1070
        default_listen_addr = strdup(abuf);
769 1070
        AN(default_listen_addr);
770 1070
        VSA_free(&sa);
771
772
        /*
773
         * We need an IP number which will not respond, ever, and that is a
774
         * lot harder than it sounds.  This IP# is from RFC5737 and a
775
         * C-class broadcast at that.
776
         * If tests involving ${bad_ip} fails and you run linux, you should
777
         * check your /proc/sys/net/ipv4/ip_nonlocal_bind setting.
778
         */
779
780 1070
        extmacro_def("bad_ip", NULL, "%s", "192.0.2.255");
781 1070
}
782
783
/**********************************************************************
784
 * Macros
785
 */
786
787
static char * v_matchproto_(macro_f)
788
macro_func_date(int argc, char *const *argv, const char **err)
789 2396
{
790 2396
        double t;
791 2396
        char *s;
792
793 2396
        assert(argc >= 2);
794 2396
        AN(argv);
795 2396
        AN(err);
796
797 2396
        if (argc > 2) {
798 0
                *err = "macro does not take arguments";
799 0
                return (NULL);
800 0
        }
801
802 2396
        t = VTIM_real();
803 2396
        s = malloc(VTIM_FORMAT_SIZE);
804 2396
        AN(s);
805 2396
        VTIM_format(t, s);
806 2396
        return (s);
807 2396
}
808
809
static char *
810
macro_func_string_repeat(int argc, char *const *argv, const char **err)
811 56
{
812 56
        struct vsb vsb[1];
813 56
        const char *p;
814 56
        char *res;
815 56
        size_t l;
816 56
        int i;
817
818 56
        if (argc != 4) {
819 0
                *err = "repeat takes 2 arguments";
820 0
                return (NULL);
821 0
        }
822
823 56
        p = argv[2];
824 56
        i = SF_Parse_Integer(&p, err);
825
826 56
        if (*err != NULL)
827 0
                return (NULL);
828
829 56
        if (*p != '\0' || i < 0) {
830 0
                *err = "invalid number of repetitions";
831 0
                return (NULL);
832 0
        }
833
834 56
        l = (vstrlen(argv[3]) * i) + 1;
835 56
        res = malloc(l);
836 56
        AN(res);
837 56
        AN(VSB_init(vsb, res, l));
838 42412
        while (i > 0) {
839 42356
                AZ(VSB_cat(vsb, argv[3]));
840 42356
                i--;
841 42356
        }
842 56
        AZ(VSB_finish(vsb));
843 56
        VSB_fini(vsb);
844 56
        return (res);
845 56
}
846
847
static char *
848
macro_func_string(int argc, char *const *argv, const char **err)
849 56
{
850
851 56
        assert(argc >= 2);
852 56
        AN(argv);
853 56
        AN(err);
854
855 56
        if (argc == 2) {
856 0
                *err = "missing action";
857 0
                return (NULL);
858 0
        }
859
860 56
        if (!vstrcmp(argv[2], "repeat"))
861 56
                return (macro_func_string_repeat(argc - 1, argv + 1, err));
862
863 0
        *err = "unknown action";
864 0
        return (NULL);
865 56
}
866
867
/**********************************************************************
868
 * Main
869
 */
870
871
static int
872
read_file(const char *fn)
873 1118
{
874 1118
        struct vtc_tst *tp;
875 1118
        char *p, *q;
876
877 1118
        p = VFIL_readfile(NULL, fn, NULL);
878 1118
        if (p == NULL) {
879 1
                fprintf(stderr, "Cannot stat file \"%s\": %s\n",
880 1
                    fn, strerror(errno));
881 1
                return (2);
882 1
        }
883 1119
        for (q = p ;q != NULL && *q != '\0'; q++) {
884 1119
                if (vct_islws(*q))
885 2
                        continue;
886 1117
                if (*q != '#')
887 1117
                        break;
888 0
                q = strchr(q, '\n');
889 0
                if (q == NULL)
890 0
                        break;
891 0
        }
892
893 1117
        if (q == NULL || *q == '\0') {
894 0
                fprintf(stderr, "File \"%s\" has no content.\n", fn);
895 0
                free(p);
896 0
                return (2);
897 0
        }
898
899 1117
        if ((vstrncmp(q, "varnishtest", 11) || !isspace(q[11])) &&
900 1117
            (vstrncmp(q, "vtest", 5) || !isspace(q[5]))) {
901 4
                fprintf(stderr,
902 4
                    "File \"%s\" doesn't start with"
903 4
                    " 'vtest' or 'varnishtest'\n", fn);
904 4
                free(p);
905 4
                vtc_skip++;
906 4
                return (2);
907 4
        }
908 1113
        ALLOC_OBJ(tp, TST_MAGIC);
909 1113
        AN(tp);
910 1113
        tp->filename = fn;
911 1113
        tp->script = p;
912 1113
        tp->ntodo = vtc_ntest;
913 1113
        tp->nwait = vtc_ntest;
914 1113
        VTAILQ_INSERT_TAIL(&tst_head, tp, list);
915 1113
        return (0);
916 1113
}
917
918
/**********************************************************************
919
 * Usual command arguments
920
 */
921
922
static void
923
usual_arguments(int argc, char *const *argv)
924 1078
{
925 1078
        int ch;
926 1078
        uintmax_t bufsiz;
927
928 1115
        while ((ch = getopt(argc, argv, "b:CD:E:hij:kLln:p:qt:vVW")) != -1) {
929 41
                switch (ch) {
930 2
                case 'b':
931 2
                        if (VNUM_2bytes(optarg, &bufsiz, 0)) {
932 0
                                fprintf(stderr, "Cannot parse b opt '%s'\n",
933 0
                                    optarg);
934 0
                                exit(2);
935 0
                        }
936 2
                        if (bufsiz > UINT_MAX) {
937 0
                                fprintf(stderr, "Invalid b opt '%s'\n",
938 0
                                    optarg);
939 0
                                exit(2);
940 0
                        }
941 2
                        vtc_bufsiz = (unsigned)bufsiz;
942 2
                        break;
943 3
                case 'C':
944 3
                        vtc_use_cleaner = !vtc_use_cleaner;
945 3
                        break;
946 2
                case 'D':
947 2
                        if (!parse_D_opt(optarg)) {
948 0
                                fprintf(stderr, "Cannot parse D opt '%s'\n",
949 0
                                    optarg);
950 0
                                exit(2);
951 0
                        }
952 2
                        break;
953 2
                case 'E':
954 0
                        add_extension(optarg);
955 0
                        break;
956 1
                case 'i':
957 1
                        iflg = 1;
958 1
                        break;
959 3
                case 'j':
960 3
                        npar = strtoul(optarg, NULL, 0);
961 3
                        break;
962 2
                case 'L':
963 2
                        leave_temp = 2;
964 2
                        break;
965 3
                case 'l':
966 3
                        leave_temp = 1;
967 3
                        break;
968 2
                case 'k':
969 2
                        vtc_continue = !vtc_continue;
970 2
                        break;
971 2
                case 'n':
972 2
                        vtc_ntest = strtoul(optarg, NULL, 0);
973 2
                        break;
974 1
                case 'p':
975 1
                        VSB_cat(params_vsb, " -p ");
976 1
                        VSB_quote(params_vsb, optarg, -1, 0);
977 1
                        break;
978 2
                case 'q':
979 2
                        if (vtc_verbosity > 0)
980 2
                                vtc_verbosity--;
981 2
                        break;
982 1
                case 't':
983 1
                        vtc_maxdur = strtoul(optarg, NULL, 0);
984 1
                        break;
985 0
                case 'V':
986 0
                        printf("%s\n", VTEST_VERSION);
987 0
                        exit(0);
988 13
                case 'v':
989 13
                        if (vtc_verbosity < 2)
990 13
                                vtc_verbosity++;
991 13
                        break;
992 4
                default:
993 4
                        usage();
994 41
                }
995 41
        }
996 1074
        argc -= optind;
997 1074
        argv += optind;
998
999 1074
        if (argc < 1)
1000 1
                usage();
1001
1002 2188
        for (; argc > 0; argc--, argv++) {
1003 1118
                if (!read_file(*argv))
1004 1113
                        continue;
1005 5
                if (!vtc_continue)
1006 3
                        exit(2);
1007 5
        }
1008 1073
}
1009
1010
/**********************************************************************
1011
 * autocrap test-driver command arguments
1012
 */
1013
1014
static void
1015
automake_test_driver_arguments(int argc, char *const *argv)
1016 1056
{
1017
1018 1056
        argc -= 1;
1019 1056
        argv += 1;
1020
1021 9504
        while (argc > 1) {
1022 9504
#define TDSAVE(name, dst) \
1023 46464
                if (!vstrcmp(*argv, name)) { \
1024 6336
                        dst = argv[1]; \
1025 6336
                        AN(dst); \
1026 6336
                        argc -= 2; \
1027 6336
                        argv += 2; \
1028 6336
                        continue; \
1029 6336
                }
1030 46464
        TDARGS(TDSAVE)
1031 3168
#undef TDSAVE
1032 3168
                if (!vstrcmp(*argv, "--extension")) {
1033 0
                        add_extension(argv[1]);
1034 0
                        argc -= 2;
1035 0
                        argv += 2;
1036 0
                        continue;
1037 0
                }
1038 3168
                if (!vstrcmp(*argv, "--verbose")) {
1039 1056
                        vtc_verbosity++;
1040 1056
                        argc -= 1;
1041 1056
                        argv += 1;
1042 1056
                        continue;
1043 1056
                }
1044 2112
                if (!vstrcmp(*argv, "--in-tree")) {
1045 1056
                        iflg++;
1046 1056
                        argc -= 1;
1047 1056
                        argv += 1;
1048 1056
                        continue;
1049 1056
                }
1050 1056
                if (vstrcmp(*argv, "--")) {
1051 0
                        fprintf(stderr, "Not '--': '%s'\n", *argv);
1052 0
                        usage();
1053 0
                }
1054 1056
                usual_arguments(argc, argv);
1055 1056
                break;
1056 1056
        }
1057 1056
        vtc_verbosity = 0;
1058 1056
}
1059
1060
/**********************************************************************
1061
 * Main
1062
 */
1063
1064
int
1065
main(int argc, char * const *argv)
1066 1078
{
1067 1078
        int i;
1068 1078
        int nstart = 0;
1069 1078
        const char *p;
1070 1078
        char buf[PATH_MAX];
1071
1072 1078
        argv0 = strrchr(argv[0], '/');
1073 1078
        if (argv0 == NULL)
1074 0
                argv0 = argv[0];
1075 1078
        else
1076 1078
                argv0++;
1077
1078 1078
        if (getenv("TMPDIR") != NULL)
1079 0
                tmppath = strdup(getenv("TMPDIR"));
1080 1078
        else
1081 1078
                tmppath = strdup("/tmp");
1082
1083 1078
#ifdef PACKAGE_VERSION
1084 1078
        extmacro_def("pkg_version", NULL, PACKAGE_VERSION);
1085 1078
#endif
1086 1078
#ifdef PACKAGE_BRANCH
1087 1078
        extmacro_def("pkg_branch", NULL, PACKAGE_BRANCH);
1088 1078
#endif
1089
1090 1078
        cwd = getcwd(buf, sizeof buf);
1091 1078
        extmacro_def("pwd", NULL, "%s", cwd);
1092
1093 1078
        extmacro_def("date", macro_func_date, NULL);
1094 1078
        extmacro_def("string", macro_func_string, NULL);
1095
1096 1078
        vmod_path = NULL;
1097
1098 1078
        params_vsb = VSB_new_auto();
1099 1078
        AN(params_vsb);
1100 1078
        p = getenv("VTEST_DURATION");
1101 1078
        if (p == NULL)
1102 1077
                p = getenv("VARNISHTEST_DURATION");
1103 1078
        if (p != NULL)
1104 1
                vtc_maxdur = atoi(p);
1105
1106 1078
        VRND_SeedAll();
1107 1078
        cbvsb = VSB_new_auto();
1108 1078
        AN(cbvsb);
1109 1078
        setbuf(stdout, NULL);
1110 1078
        setbuf(stderr, NULL);
1111
1112 1078
        if (argc < 2)
1113 0
                usage();
1114
1115 1078
        if (argv[1][0] == '-' && argv[1][1] == '-') {
1116 1056
                automake_test_driver_arguments(argc, argv);
1117 1056
        } else {
1118 22
                usual_arguments(argc, argv);
1119 22
        }
1120
1121 1078
        AZ(VSB_finish(params_vsb));
1122
1123 1078
        p = getenv("TERM");
1124 1078
        has_color = getenv("NO_COLOR") == NULL &&
1125 1078
            isatty(fileno(stdout)) &&
1126 1078
            p != NULL && vstrcmp(p, "dumb");
1127 1078
        if (! has_color)
1128 1070
                col_red = col_grn = col_blu = col_std = "";
1129
1130 1078
        ip_magic();
1131
1132 1078
        if (iflg)
1133 1057
                i_mode();
1134
1135 1078
        vb = VEV_New();
1136
1137 1078
        if (vtc_use_cleaner)
1138 2
                cleaner_setup();
1139
1140 1078
        i = 0;
1141 4391
        while (!VTAILQ_EMPTY(&tst_head) || i) {
1142 3313
                if (!VTAILQ_EMPTY(&tst_head) && njob < npar) {
1143 1122
                        start_test();
1144 1122
                        njob++;
1145
                        /* Stagger ramp-up */
1146 1122
                        if (nstart++ < npar)
1147 1074
                                (void)usleep(random() % 100000L);
1148 1122
                        i = 1;
1149 1122
                        continue;
1150 1122
                }
1151 2191
                i = VEV_Once(vb);
1152 2191
        }
1153 1078
        cleaner_finish();
1154 1078
        (void)close(bad_backend_fd);
1155 1078
        if (td_trs_file != 0)
1156 1056
                return(0);
1157 22
        if (vtc_continue)
1158 2
                fprintf(stderr,
1159 2
                    "%d tests failed, %d tests skipped, %d tests passed\n",
1160 2
                    vtc_fail, vtc_skip, vtc_good);
1161 22
        if (vtc_fail)
1162 0
                return (1);
1163 22
        if (vtc_skip && !vtc_good)
1164 4
                return (77);
1165 18
        return (0);
1166 22
}