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