varnish-cache/bin/varnishtest/vtest2/src/vtc.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/wait.h>
33
34
#include <ctype.h>
35
#include <stdarg.h>
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
41
#include "vtc.h"
42
#include "vtc_log.h"
43
44
#include "vav.h"
45
#include "vrnd.h"
46
47
#define         MAX_TOKENS              200
48
49
volatile sig_atomic_t   vtc_error;      /* Error encountered */
50
int                     vtc_stop;       /* Stops current test without error */
51
pthread_t               vtc_thread;
52
int                     ign_unknown_macro = 0;
53
static struct vtclog    *vltop;
54
55
static pthread_mutex_t  vtc_vrnd_mtx;
56
57
static void
58 681240
vtc_vrnd_lock(void)
59
{
60 681240
        PTOK(pthread_mutex_lock(&vtc_vrnd_mtx));
61 681240
}
62
63
static void
64 681240
vtc_vrnd_unlock(void)
65
{
66 681240
        PTOK(pthread_mutex_unlock(&vtc_vrnd_mtx));
67 681240
}
68
69
static const char *tfn;
70
71
/**********************************************************************
72
 * Macro facility
73
 */
74
75
struct macro {
76
        unsigned                magic;
77
#define MACRO_MAGIC             0x803423e3
78
        VTAILQ_ENTRY(macro)     list;
79
        char                    *name;
80
        char                    *val;
81
        macro_f                 *func;
82
};
83
84
static VTAILQ_HEAD(,macro) macro_list = VTAILQ_HEAD_INITIALIZER(macro_list);
85
86
static const struct cmds global_cmds[] = {
87
#define CMD_GLOBAL(n) { #n, cmd_##n },
88
#include "cmds.h"
89
        { NULL, NULL }
90
};
91
92
static const struct cmds top_cmds[] = {
93
#define CMD_TOP(n) { #n, cmd_##n },
94
#include "cmds.h"
95
        { NULL, NULL }
96
};
97
98
/**********************************************************************/
99
100
static struct macro *
101 1023480
macro_def_int(const char *name, macro_f *func, const char *fmt, va_list ap)
102
{
103
        struct macro *m;
104
        char buf[2048];
105
106 13701400
        VTAILQ_FOREACH(m, &macro_list, list)
107 12699640
                if (!strcmp(name, m->name))
108 21720
                        break;
109 1023480
        if (m == NULL) {
110 1001760
                ALLOC_OBJ(m, MACRO_MAGIC);
111 1001760
                AN(m);
112 1001760
                REPLACE(m->name, name);
113 1001760
                AN(m->name);
114 1001760
                VTAILQ_INSERT_TAIL(&macro_list, m, list);
115 1001760
        }
116 1023480
        AN(m);
117 1023480
        if (func != NULL) {
118 80720
                AZ(fmt);
119 80720
                m->func = func;
120 80720
        } else {
121 942760
                AN(fmt);
122 942760
                vbprintf(buf, fmt, ap);
123 942760
                REPLACE(m->val, buf);
124 942760
                AN(m->val);
125
        }
126 1023480
        return (m);
127
}
128
129
130
/**********************************************************************
131
 * This is for defining macros before we fork the child process which
132
 * runs the test-case.
133
 */
134
135
void
136 442240
extmacro_def(const char *name, macro_f *func, const char *fmt, ...)
137
{
138
        va_list ap;
139
140 442240
        va_start(ap, fmt);
141 442240
        (void)macro_def_int(name, func, fmt, ap);
142 442240
        va_end(ap);
143 442240
}
144
145
/**********************************************************************
146
 * Below this point is run inside the testing child-process.
147
 */
148
149
static pthread_mutex_t          macro_mtx;
150
151
static void
152 41000
init_macro(void)
153
{
154
        struct macro *m;
155
156
        /* Dump the extmacros for completeness */
157 490840
        VTAILQ_FOREACH(m, &macro_list, list) {
158 449840
                if (m->val != NULL)
159 735680
                        vtc_log(vltop, 4,
160 367840
                            "extmacro def %s=%s", m->name, m->val);
161
                else
162 82000
                        vtc_log(vltop, 4, "extmacro def %s(...)", m->name);
163 449840
        }
164
165 41000
        PTOK(pthread_mutex_init(&macro_mtx, NULL));
166 41000
}
167
168
void
169 581240
macro_def(struct vtclog *vl, const char *instance, const char *name,
170
    const char *fmt, ...)
171
{
172
        char buf1[256];
173
        struct macro *m;
174
        va_list ap;
175
176 581240
        AN(fmt);
177
178 581240
        if (instance != NULL) {
179 458240
                bprintf(buf1, "%s_%s", instance, name);
180 458240
                name = buf1;
181 458240
        }
182
183 581240
        PTOK(pthread_mutex_lock(&macro_mtx));
184 581240
        va_start(ap, fmt);
185 581240
        m = macro_def_int(name, NULL, fmt, ap);
186 581240
        va_end(ap);
187 581240
        vtc_log(vl, 4, "macro def %s=%s", name, m->val);
188 581240
        PTOK(pthread_mutex_unlock(&macro_mtx));
189 581240
}
190
191
void
192 142863
macro_undef(struct vtclog *vl, const char *instance, const char *name)
193
{
194
        char buf1[256];
195
        struct macro *m;
196
197 142863
        if (instance != NULL) {
198 142855
                bprintf(buf1, "%s_%s", instance, name);
199 142855
                name = buf1;
200 142855
        }
201
202 142863
        PTOK(pthread_mutex_lock(&macro_mtx));
203 2320489
        VTAILQ_FOREACH(m, &macro_list, list)
204 2312146
                if (!strcmp(name, m->name))
205 134520
                        break;
206 142863
        if (m != NULL) {
207 134520
                if (!vtc_stop)
208 7600
                        vtc_log(vl, 4, "macro undef %s", name);
209 134520
                CHECK_OBJ(m, MACRO_MAGIC);
210 134520
                VTAILQ_REMOVE(&macro_list, m, list);
211 134520
                free(m->name);
212 134520
                free(m->val);
213 134520
                FREE_OBJ(m);
214 134520
        }
215 142863
        PTOK(pthread_mutex_unlock(&macro_mtx));
216 142863
}
217
218
unsigned
219 76720
macro_isdef(const char *instance, const char *name)
220
{
221
        char buf1[256];
222
        struct macro *m;
223
224 76720
        if (instance != NULL) {
225 0
                bprintf(buf1, "%s_%s", instance, name);
226 0
                name = buf1;
227 0
        }
228
229 76720
        PTOK(pthread_mutex_lock(&macro_mtx));
230 1407360
        VTAILQ_FOREACH(m, &macro_list, list)
231 1330640
                if (!strcmp(name, m->name))
232 0
                        break;
233 76720
        PTOK(pthread_mutex_unlock(&macro_mtx));
234
235 76720
        return (m != NULL);
236
}
237
238
void
239 483530
macro_cat(struct vtclog *vl, struct vsb *vsb, const char *b, const char *e)
240
{
241
        struct macro *m;
242 483530
        char **argv, *retval = NULL;
243 483530
        const char *err = NULL;
244
        int argc;
245
246 483530
        AN(b);
247 483530
        if (e == NULL)
248 256264
                e = strchr(b, '\0');
249 483530
        AN(e);
250
251 483530
        argv = VAV_ParseTxt(b, e, &argc, ARGV_COMMA);
252 483530
        AN(argv);
253
254 483530
        if (*argv != NULL)
255 0
                vtc_fatal(vl, "Macro ${%.*s} parsing failed: %s",
256 0
                    (int)(e - b), b, *argv);
257
258 483530
        assert(argc >= 2);
259
260 483530
        PTOK(pthread_mutex_lock(&macro_mtx));
261 4539109
        VTAILQ_FOREACH(m, &macro_list, list) {
262 4536549
                CHECK_OBJ_NOTNULL(m, MACRO_MAGIC);
263 4536549
                if (!strcmp(argv[1], m->name))
264 480970
                        break;
265 4055579
        }
266 483530
        if (m != NULL) {
267 480970
                if (m->func != NULL) {
268 88850
                        AZ(m->val);
269 88850
                        retval = m->func(argc, argv, &err);
270 88850
                        if (err == NULL)
271 88850
                                AN(retval);
272 88850
                } else {
273 392120
                        AN(m->val);
274 392120
                        if (argc == 2)
275 392120
                                REPLACE(retval, m->val);
276
                        else
277 0
                                err = "macro does not take arguments";
278
                }
279 480970
        }
280 483530
        PTOK(pthread_mutex_unlock(&macro_mtx));
281
282 483530
        VAV_Free(argv);
283
284 483530
        if (err != NULL)
285 0
                vtc_fatal(vl, "Macro ${%.*s} failed: %s",
286 0
                    (int)(e - b), b, err);
287
288 483530
        if (retval == NULL) {
289 2560
                if (!ign_unknown_macro)
290 0
                        vtc_fatal(vl, "Macro ${%.*s} not found",
291 0
                            (int)(e - b), b);
292 2560
                VSB_printf(vsb, "${%.*s}", (int)(e - b), b);
293 2560
                return;
294
        }
295
296 480970
        VSB_cat(vsb, retval);
297 480970
        free(retval);
298 483530
}
299
300
struct vsb *
301 57277
macro_expandf(struct vtclog *vl, const char *fmt, ...)
302
{
303
        va_list ap;
304
        struct vsb *vsb1, *vsb2;
305
306 57277
        vsb1 = VSB_new_auto();
307 57277
        AN(vsb1);
308 57277
        va_start(ap, fmt);
309 57277
        VSB_vprintf(vsb1, fmt, ap);
310 57277
        va_end(ap);
311 57277
        AZ(VSB_finish(vsb1));
312 57277
        vsb2 = macro_expand(vl, VSB_data(vsb1));
313 57277
        VSB_destroy(&vsb1);
314 57277
        return (vsb2);
315
}
316
317
struct vsb *
318 214200
macro_expand(struct vtclog *vl, const char *text)
319
{
320
        struct vsb *vsb;
321
        const char *p, *q;
322
323 214200
        vsb = VSB_new_auto();
324 214200
        AN(vsb);
325 441439
        while (*text != '\0') {
326 364279
                p = strstr(text, "${");
327 364279
                if (p == NULL) {
328 137040
                        VSB_cat(vsb, text);
329 137040
                        break;
330
                }
331 227239
                VSB_bcat(vsb, text, p - text);
332 227239
                q = strchr(p, '}');
333 227239
                if (q == NULL) {
334 0
                        VSB_cat(vsb, text);
335 0
                        break;
336
                }
337 227239
                assert(p[0] == '$');
338 227239
                assert(p[1] == '{');
339 227239
                assert(q[0] == '}');
340 227239
                p += 2;
341 227239
                macro_cat(vl, vsb, p, q);
342 227239
                text = q + 1;
343
        }
344 214200
        AZ(VSB_finish(vsb));
345 214200
        return (vsb);
346
}
347
348
/**********************************************************************
349
 * Parse a string
350
 *
351
 * We make a copy of the string and deliberately leak it, so that all
352
 * the cmd functions we call don't have to strdup(3) all over the place.
353
 *
354
 * Static checkers like Coverity may bitch about this, but we don't care.
355
 */
356
357
358
void
359 321624
parse_string(struct vtclog *vl, void *priv, const char *spec)
360
{
361
        char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
362
        struct vsb *token_exp;
363
        char *e, *p, *q, *f, *buf;
364
        int nest_brace;
365
        int tn;
366
        unsigned n, m;
367
        const struct cmds *cp;
368
369 321624
        AN(spec);
370 321624
        buf = strdup(spec);
371 321624
        AN(buf);
372 321624
        e = strchr(buf, '\0');
373 321624
        AN(e);
374 4191807
        for (p = buf; p < e; p++) {
375 3871489
                if (vtc_error || vtc_stop) {
376 2620
                        vtc_log(vl, 1, "Aborting execution, test %s",
377 1310
                            vtc_error ? "failed" : "ended");
378 1310
                        break;
379
                }
380
                /* Start of line */
381 3870179
                if (isspace(*p))
382 2266081
                        continue;
383 1604098
                if (*p == '\n')
384 0
                        continue;
385
386 1604098
                if (*p == '#') {
387 3683091
                        for (; *p != '\0' && *p != '\n'; p++)
388
                                ;
389 89360
                        if (*p == '\0')
390 0
                                break;
391 89360
                        continue;
392
                }
393
394 1514736
                q = strchr(p, '\n');
395 1514736
                if (q == NULL)
396 15798
                        q = strchr(p, '\0');
397 1514736
                if (q - p > 60)
398 115851
                        vtc_log(vl, 2, "=== %.60s...", p);
399
                else
400 1398885
                        vtc_log(vl, 2, "=== %.*s", (int)(q - p), p);
401
402
                /* First content on line, collect tokens */
403 1514736
                memset(token_s, 0, sizeof token_s);
404 1514736
                memset(token_e, 0, sizeof token_e);
405 1514736
                tn = 0;
406 1514736
                f = p;
407 10166284
                while (p < e) {
408 10150842
                        assert(tn < MAX_TOKENS);
409 10150842
                        assert(p < e);
410 10150842
                        if (*p == '\n') { /* End on NL */
411 1499294
                                break;
412
                        }
413 8651548
                        if (isspace(*p)) { /* Inter-token whitespace */
414 3669460
                                p++;
415 3669460
                                continue;
416
                        }
417 4982088
                        if (*p == '\\' && p[1] == '\n') { /* line-cont */
418 39640
                                p += 2;
419 39640
                                continue;
420
                        }
421 4942448
                        if (*p == '"') { /* quotes */
422 475468
                                token_s[tn] = ++p;
423 475468
                                q = p;
424 11477115
                                for (; *p != '\0'; p++) {
425 11476795
                                        assert(p < e);
426 11476795
                                        if (*p == '"')
427 475148
                                                break;
428 11001647
                                        if (*p == '\\') {
429 111024
                                                p += VAV_BackSlash(p, q) - 1;
430 111024
                                                q++;
431 111024
                                        } else {
432 10890623
                                                if (*p == '\n')
433 0
                                                        vtc_fatal(vl,
434
                                "Unterminated quoted string in line: %*.*s",
435 0
                                (int)(p - f), (int)(p - f), f);
436 10890623
                                                assert(*p != '\n');
437 10890623
                                                *q++ = *p;
438
                                        }
439 11001647
                                }
440 475468
                                token_e[tn++] = q;
441 475468
                                p++;
442 4942448
                        } else if (*p == '{') { /* Braces */
443 309966
                                nest_brace = 0;
444 309966
                                token_s[tn] = p + 1;
445 50363602
                                for (; p < e; p++) {
446 50363602
                                        if (*p == '{')
447 571405
                                                nest_brace++;
448 49792197
                                        else if (*p == '}') {
449 571406
                                                if (--nest_brace == 0)
450 309966
                                                        break;
451 261440
                                        }
452 50053636
                                }
453 309966
                                assert(*p == '}');
454 309966
                                token_e[tn++] = p++;
455 309966
                        } else { /* other tokens */
456 4157014
                                token_s[tn] = p;
457 191032662
                                for (; p < e && !isspace(*p); p++)
458 186875648
                                        continue;
459 4157014
                                token_e[tn++] = p;
460
                        }
461
                }
462
463 1514736
                assert(p <= e);
464 1514736
                assert(tn < MAX_TOKENS);
465 1514736
                token_s[tn] = NULL;
466 6457328
                for (tn = 0; token_s[tn] != NULL; tn++) {
467 4942592
                        AN(token_e[tn]);        /*lint !e771 */
468 4942592
                        *token_e[tn] = '\0';    /*lint !e771 */
469 4942592
                        if (NULL != strstr(token_s[tn], "${")) {
470 42320
                                token_exp = macro_expand(vl, token_s[tn]);
471 42320
                                if (vtc_error)
472 0
                                        return;
473 42320
                                token_s[tn] = VSB_data(token_exp);
474 42320
                                token_e[tn] = strchr(token_s[tn], '\0');
475 42320
                        }
476 4942592
                }
477
478
479
/* SECTION: loop loop
480
 *
481
 * loop NUMBER STRING
482
 *         Process STRING as a specification, NUMBER times.
483
 *
484
 * This works inside all specification strings
485
 */
486
487 1514736
                if (!strcmp(token_s[0], "loop")) {
488 1768
                        n = strtoul(token_s[1], NULL, 0);
489 72407
                        for (m = 0; m < n && !vtc_error && !vtc_stop; m++) {
490 70639
                                vtc_log(vl, 4, "Loop #%u", m);
491 70639
                                parse_string(vl, priv, token_s[2]);
492 70639
                        }
493 1768
                        continue;
494
                }
495
496 1512968
                AN(vl->cmds);
497 26821859
                for (cp = vl->cmds; cp->name != NULL; cp++)
498 26748196
                        if (!strcmp(token_s[0], cp->name))
499 1439305
                                break;
500
501 1512968
                if (cp->name == NULL) {
502 135972
                        for (cp = global_cmds; cp->name != NULL; cp++)
503 135975
                                if (!strcmp(token_s[0], cp->name))
504 73707
                                        break;
505 73710
                }
506
507 1512974
                if (cp->name == NULL)
508 0
                        vtc_fatal(vl, "Unknown command: \"%s\"", token_s[0]);
509
510 1512974
                assert(cp->cmd != NULL);
511 1512974
                cp->cmd(token_s, priv, vl);
512 1512974
        }
513 320908
}
514
515
/**********************************************************************
516
 * Reset commands (between tests)
517
 */
518
519
static void
520 82000
reset_cmds(const struct cmds *cmd)
521
{
522
523 820000
        for (; cmd->name != NULL; cmd++)
524 738000
                cmd->cmd(NULL, NULL, NULL);
525 82000
}
526
527
/**********************************************************************
528
 * Execute a file
529
 */
530
531
int
532 41000
fail_out(void)
533
{
534
        unsigned old_err;
535
        static int once = 0;
536
537 41000
        if (once++) {
538 0
                vtc_log(vltop, 1, "failure during reset");
539 0
                return (vtc_error);
540
        }
541 41000
        old_err = vtc_error;
542 41000
        if (!vtc_stop)
543 40400
                vtc_stop = 1;
544 41000
        vtc_log(vltop, 1, "RESETTING after %s", tfn);
545 41000
        reset_cmds(global_cmds);
546 41000
        reset_cmds(top_cmds);
547 41000
        vtc_error |= old_err;
548
549 41000
        if (vtc_error)
550 40
                vtc_log(vltop, 1, "TEST %s FAILED", tfn);
551
        else
552 40960
                vtc_log(vltop, 1, "TEST %s completed", tfn);
553
554 41000
        if (vtc_stop > 1)
555 600
                return (1);
556 40400
        return (vtc_error);
557 41000
}
558
559
int
560 41000
exec_file(const char *fn, const char *script, const char *tmpdir,
561
    char *logbuf, unsigned loglen)
562
{
563
        FILE *f;
564
        struct vsb *vsb;
565
        const char *p;
566
567 41000
        AN(tmpdir);
568
569 41000
        (void)signal(SIGPIPE, SIG_IGN);
570
571 41000
        PTOK(pthread_mutex_init(&vtc_vrnd_mtx, NULL));
572 41000
        VRND_Lock = vtc_vrnd_lock;
573 41000
        VRND_Unlock = vtc_vrnd_unlock;
574 41000
        VRND_SeedAll();
575
576 41000
        tfn = fn;
577 41000
        vtc_loginit(logbuf, loglen);
578 41000
        vltop = vtc_logopen("top");
579 41000
        AN(vltop);
580 41000
        vtc_log_set_cmd(vltop, top_cmds);
581
582 41000
        vtc_log(vltop, 1, "TEST %s starting", fn);
583
584 41000
        init_macro();
585 41000
        init_server();
586 41000
        init_syslog();
587 41000
        init_tunnel();
588
589 41000
        vsb = VSB_new_auto();
590 41000
        AN(vsb);
591 41000
        if (*fn != '/')
592 40600
                macro_cat(vltop, vsb, "pwd", NULL);
593 41000
        p = strrchr(fn, '/');
594 41000
        if (p != NULL) {
595 40800
                VSB_putc(vsb, '/');
596 40800
                VSB_bcat(vsb, fn, p - fn);
597 40800
        }
598 41000
        if (VSB_len(vsb) == 0)
599 0
                VSB_putc(vsb, '/');
600 41000
        AZ(VSB_finish(vsb));
601 41000
        macro_def(vltop, NULL, "testdir", "%s", VSB_data(vsb));
602 41000
        VSB_destroy(&vsb);
603
604
        /* Move into our tmpdir */
605 41000
        AZ(chdir(tmpdir));
606 41000
        macro_def(vltop, NULL, "tmpdir", "%s", tmpdir);
607 41000
        p = strrchr(tmpdir, '/');
608 41000
        AN(p);
609 41000
        p++;
610 41000
        AN(*p);
611 41000
        macro_def(vltop, NULL, "vtcid", "%s", p);
612
613
        /* Drop file to tell what was going on here */
614 41000
        f = fopen("INFO", "w");
615 41000
        AN(f);
616 41000
        fprintf(f, "Test case: %s\n", fn);
617 41000
        AZ(fclose(f));
618
619 41000
        vtc_stop = 0;
620
621 41000
        vtc_thread = pthread_self();
622 41000
        parse_string(vltop, NULL, script);
623 41000
        return (fail_out());
624
}