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