varnish-cache/bin/varnishtest/vtc.c
1
/*-
2
 * Copyright (c) 2008-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <sys/wait.h>
32
33
#include <ctype.h>
34
#include <stdarg.h>
35
#include <stdio.h>
36
#include <stdlib.h>
37
#include <string.h>
38
#include <unistd.h>
39
40
#include "vtc.h"
41
42
#include "vav.h"
43
#include "vrnd.h"
44
#include "vtim.h"
45
46
#define         MAX_TOKENS              200
47
48
volatile sig_atomic_t   vtc_error;      /* Error encountered */
49
int                     vtc_stop;       /* Stops current test without error */
50
pthread_t               vtc_thread;
51
int                     ign_unknown_macro = 0;
52
static struct vtclog    *vltop;
53
54
static pthread_mutex_t  vtc_vrnd_mtx;
55
56
static void
57 16472
vtc_vrnd_lock(void)
58
{
59 16472
        AZ(pthread_mutex_lock(&vtc_vrnd_mtx));
60 16472
}
61
62
static void
63 16472
vtc_vrnd_unlock(void)
64
{
65 16472
        AZ(pthread_mutex_unlock(&vtc_vrnd_mtx));
66 16472
}
67
68
static const char *tfn;
69
70
/**********************************************************************
71
 * Macro facility
72
 */
73
74
struct macro {
75
        unsigned                magic;
76
#define MACRO_MAGIC             0x803423e3
77
        VTAILQ_ENTRY(macro)     list;
78
        char                    *name;
79
        char                    *val;
80
};
81
82
static VTAILQ_HEAD(,macro) macro_list = VTAILQ_HEAD_INITIALIZER(macro_list);
83
84
static const struct cmds global_cmds[] = {
85
#define CMD_GLOBAL(n) { #n, cmd_##n },
86
#include "cmds.h"
87
        { NULL, NULL }
88
};
89
90
static const struct cmds cmds[] = {
91
#define CMD_TOP(n) { #n, cmd_##n },
92
#include "cmds.h"
93
        { NULL, NULL }
94
};
95
96
/**********************************************************************/
97
98
static struct macro *
99 48548
macro_def_int(const char *name, const char *fmt, va_list ap)
100
{
101
        struct macro *m;
102
        char buf[256];
103
104 409456
        VTAILQ_FOREACH(m, &macro_list, list)
105 362024
                if (!strcmp(name, m->name))
106 1116
                        break;
107 48548
        if (m == NULL) {
108 47432
                ALLOC_OBJ(m, MACRO_MAGIC);
109 47432
                AN(m);
110 47432
                REPLACE(m->name, name);
111 47432
                AN(m->name);
112 47432
                VTAILQ_INSERT_TAIL(&macro_list, m, list);
113 47432
        }
114 48548
        AN(m);
115 48548
        vbprintf(buf, fmt, ap);
116 48548
        REPLACE(m->val, buf);
117 48548
        AN(m->val);
118 48548
        return (m);
119
}
120
121
122
/**********************************************************************
123
 * This is for defining macros before we fork the child process which
124
 * runs the test-case.
125
 */
126
127
void
128 15516
extmacro_def(const char *name, const char *fmt, ...)
129
{
130
        va_list ap;
131
132 15516
        va_start(ap, fmt);
133 15516
        (void)macro_def_int(name, fmt, ap);
134 15516
        va_end(ap);
135 15516
}
136
137
/**********************************************************************
138
 * Below this point is run inside the testing child-process.
139
 */
140
141
static pthread_mutex_t          macro_mtx;
142
143
static void
144 3184
init_macro(void)
145
{
146
        struct macro *m;
147
148
        /* Dump the extmacros for completeness */
149 19084
        VTAILQ_FOREACH(m, &macro_list, list)
150 15900
                vtc_log(vltop, 4, "extmacro def %s=%s", m->name, m->val);
151
152 3184
        AZ(pthread_mutex_init(&macro_mtx, NULL));
153 3184
}
154
155
void
156 33032
macro_def(struct vtclog *vl, const char *instance, const char *name,
157
    const char *fmt, ...)
158
{
159
        char buf1[256];
160
        struct macro *m;
161
        va_list ap;
162
163 33032
        AN(fmt);
164
165 33032
        if (instance != NULL) {
166 26664
                bprintf(buf1, "%s_%s", instance, name);
167 26664
                name = buf1;
168 26664
        }
169
170 33032
        AZ(pthread_mutex_lock(&macro_mtx));
171 33032
        va_start(ap, fmt);
172 33032
        m = macro_def_int(name, fmt, ap);
173 33032
        va_end(ap);
174 33032
        vtc_log(vl, 4, "macro def %s=%s", name, m->val);
175 33032
        AZ(pthread_mutex_unlock(&macro_mtx));
176 33032
}
177
178
void
179 11042
macro_undef(struct vtclog *vl, const char *instance, const char *name)
180
{
181
        char buf1[256];
182
        struct macro *m;
183
184 11042
        if (instance != NULL) {
185 11042
                bprintf(buf1, "%s_%s", instance, name);
186 11042
                name = buf1;
187 11042
        }
188
189 11042
        AZ(pthread_mutex_lock(&macro_mtx));
190 93226
        VTAILQ_FOREACH(m, &macro_list, list)
191 93076
                if (!strcmp(name, m->name))
192 10892
                        break;
193 11042
        if (m != NULL) {
194 10892
                if (!vtc_stop)
195 408
                        vtc_log(vl, 4, "macro undef %s", name);
196 10892
                VTAILQ_REMOVE(&macro_list, m, list);
197 10892
                free(m->name);
198 10892
                free(m->val);
199 10892
                FREE_OBJ(m);
200 10892
        }
201 11042
        AZ(pthread_mutex_unlock(&macro_mtx));
202 11042
}
203
204
char *
205 28875
macro_get(const char *b, const char *e)
206
{
207
        struct macro *m;
208
        int l;
209 28875
        char *retval = NULL;
210
211 28875
        AN(b);
212 28876
        if (e == NULL)
213 12496
                e = strchr(b, '\0');
214 28876
        AN(e);
215 28876
        l = e - b;
216
217 28876
        if (l == 4 && !memcmp(b, "date", l)) {
218 12
                double t = VTIM_real();
219 12
                retval = malloc(64);
220 12
                AN(retval);
221 12
                VTIM_format(t, retval);
222 12
                return (retval);
223
        }
224
225 28864
        AZ(pthread_mutex_lock(&macro_mtx));
226 165704
        VTAILQ_FOREACH(m, &macro_list, list) {
227 165576
                CHECK_OBJ_NOTNULL(m, MACRO_MAGIC);
228 165576
                if (!strncmp(b, m->name, l) && m->name[l] == '\0')
229 28736
                        break;
230 136840
        }
231 28864
        if (m != NULL)
232 28736
                retval = strdup(m->val);
233 28864
        AZ(pthread_mutex_unlock(&macro_mtx));
234 28864
        return (retval);
235 28876
}
236
237
struct vsb *
238 4084
macro_expandf(struct vtclog *vl, const char *fmt, ...)
239
{
240
        va_list ap;
241
        struct vsb *vsb1, *vsb2;
242
243 4084
        vsb1 = VSB_new_auto();
244 4084
        AN(vsb1);
245 4084
        va_start(ap, fmt);
246 4084
        VSB_vprintf(vsb1, fmt, ap);
247 4084
        va_end(ap);
248 4084
        AZ(VSB_finish(vsb1));
249 4084
        vsb2 = macro_expand(vl, VSB_data(vsb1));
250 4084
        VSB_destroy(&vsb1);
251 4084
        return (vsb2);
252
}
253
254
struct vsb *
255 15704
macro_expand(struct vtclog *vl, const char *text)
256
{
257
        struct vsb *vsb;
258
        const char *p, *q;
259
        char *m;
260
261 15704
        vsb = VSB_new_auto();
262 15704
        AN(vsb);
263 32084
        while (*text != '\0') {
264 26332
                p = strstr(text, "${");
265 26332
                if (p == NULL) {
266 9952
                        VSB_cat(vsb, text);
267 9952
                        break;
268
                }
269 16380
                VSB_bcat(vsb, text, p - text);
270 16380
                q = strchr(p, '}');
271 16380
                if (q == NULL) {
272 0
                        VSB_cat(vsb, text);
273 0
                        break;
274
                }
275 16380
                assert(p[0] == '$');
276 16380
                assert(p[1] == '{');
277 16380
                assert(q[0] == '}');
278 16380
                p += 2;
279 16380
                m = macro_get(p, q);
280 16380
                if (m == NULL) {
281 128
                        if (!ign_unknown_macro) {
282 0
                                VSB_destroy(&vsb);
283 0
                                vtc_fatal(vl, "Macro ${%.*s} not found",
284 0
                                          (int)(q - p), p);
285
                                NEEDLESS(return (NULL));
286
                        }
287 128
                        VSB_printf(vsb, "${%.*s}", (int)(q - p), p);
288 128
                } else {
289 16252
                        VSB_printf(vsb, "%s", m);
290 16252
                        free(m);
291
                }
292 16380
                text = q + 1;
293
        }
294 15704
        AZ(VSB_finish(vsb));
295 15704
        return (vsb);
296
}
297
298
/**********************************************************************
299
 * Parse a string
300
 *
301
 * We make a copy of the string and deliberately leak it, so that all
302
 * the cmd functions we call don't have to strdup(3) all over the place.
303
 *
304
 * Static checkers like Coverity may bitch about this, but we don't care.
305
 */
306
307
308
void
309 19738
parse_string(const char *spec, const struct cmds *cmd, void *priv,
310
    struct vtclog *vl)
311
{
312
        char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
313
        struct vsb *token_exp;
314
        char *e, *p, *q, *f, *buf;
315
        int nest_brace;
316
        int tn;
317
        const struct cmds *cp;
318
319 19738
        AN(spec);
320 19738
        buf = strdup(spec);
321 19738
        AN(buf);
322 19737
        e = strchr(buf, '\0');
323 19737
        AN(e);
324 268099
        for (p = buf; p < e; p++) {
325 248518
                if (vtc_error || vtc_stop)
326 39
                        break;
327
                /* Start of line */
328 248480
                if (isspace(*p))
329 133388
                        continue;
330 115106
                if (*p == '\n')
331 0
                        continue;
332
333 115107
                if (*p == '#') {
334 260612
                        for (; *p != '\0' && *p != '\n'; p++)
335
                                ;
336 6360
                        if (*p == '\0')
337 0
                                break;
338 6360
                        continue;
339
                }
340
341 108748
                q = strchr(p, '\n');
342 108748
                if (q == NULL)
343 988
                        q = strchr(p, '\0');
344 108746
                if (q - p > 60)
345 5316
                        vtc_log(vl, 2, "=== %.60s...", p);
346
                else
347 103430
                        vtc_log(vl, 2, "=== %.*s", (int)(q - p), p);
348
349
                /* First content on line, collect tokens */
350 108751
                memset(token_s, 0, sizeof token_s);
351 108751
                memset(token_e, 0, sizeof token_e);
352 108751
                tn = 0;
353 108751
                f = p;
354 723022
                while (p < e) {
355 721959
                        assert(tn < MAX_TOKENS);
356 721972
                        assert(p < e);
357 721955
                        if (*p == '\n') { /* End on NL */
358 107759
                                break;
359
                        }
360 614223
                        if (isspace(*p)) { /* Inter-token whitespace */
361 259233
                                p++;
362 259233
                                continue;
363
                        }
364 355034
                        if (*p == '\\' && p[1] == '\n') { /* line-cont */
365 3660
                                p += 2;
366 3660
                                continue;
367
                        }
368 351373
                        if (*p == '"') { /* quotes */
369 36340
                                token_s[tn] = ++p;
370 36340
                                q = p;
371 850219
                                for (; *p != '\0'; p++) {
372 850218
                                        assert(p < e);
373 850219
                                        if (*p == '"')
374 36340
                                                break;
375 813879
                                        if (*p == '\\') {
376 9732
                                                p += VAV_BackSlash(p, q) - 1;
377 9732
                                                q++;
378 9732
                                        } else {
379 804147
                                                if (*p == '\n')
380 0
                                                        vtc_fatal(vl,
381
                                "Unterminated quoted string in line: %*.*s",
382 0
                                (int)(p - f), (int)(p - f), f);
383 804147
                                                assert(*p != '\n');
384 804147
                                                *q++ = *p;
385
                                        }
386 813879
                                }
387 36340
                                token_e[tn++] = q;
388 36340
                                p++;
389 351373
                        } else if (*p == '{') { /* Braces */
390 22474
                                nest_brace = 0;
391 22474
                                token_s[tn] = p + 1;
392 3514640
                                for (; p < e; p++) {
393 3514611
                                        if (*p == '{')
394 40186
                                                nest_brace++;
395 3474461
                                        else if (*p == '}') {
396 40185
                                                if (--nest_brace == 0)
397 22473
                                                        break;
398 17712
                                        }
399 3492196
                                }
400 22473
                                assert(*p == '}');
401 22473
                                token_e[tn++] = p++;
402 22473
                        } else { /* other tokens */
403 292558
                                token_s[tn] = p;
404 1922611
                                for (; p < e && !isspace(*p); p++)
405 1630025
                                        continue;
406 292565
                                token_e[tn++] = p;
407
                        }
408
                }
409
410 108751
                assert(p <= e);
411 108751
                assert(tn < MAX_TOKENS);
412 108751
                token_s[tn] = NULL;
413 460115
                for (tn = 0; token_s[tn] != NULL; tn++) {
414 351362
                        AN(token_e[tn]);        /*lint !e771 */
415 351358
                        *token_e[tn] = '\0';    /*lint !e771 */
416 351358
                        if (NULL != strstr(token_s[tn], "${")) {
417 2920
                                token_exp = macro_expand(vl, token_s[tn]);
418 2920
                                if (vtc_error)
419 0
                                        return;
420 2920
                                token_s[tn] = VSB_data(token_exp);
421 2920
                                token_e[tn] = strchr(token_s[tn], '\0');
422 2920
                        }
423 351366
                }
424
425 1943863
                for (cp = cmd; cp->name != NULL; cp++)
426 1938276
                        if (!strcmp(token_s[0], cp->name))
427 103295
                                break;
428
429 108747
                if (cp->name == NULL) {
430 10444
                        for (cp = global_cmds; cp->name != NULL; cp++)
431 10444
                                if (!strcmp(token_s[0], cp->name))
432 5452
                                        break;
433 5452
                }
434
435 108748
                if (cp->name == NULL)
436 0
                        vtc_fatal(vl, "Unknown command: \"%s\"", token_s[0]);
437
438 108748
                assert(cp->cmd != NULL);
439 108748
                cp->cmd(token_s, priv, cmd, vl);
440 108748
        }
441 19602
}
442
443
/**********************************************************************
444
 * Reset commands (between tests)
445
 */
446
447
static void
448 6368
reset_cmds(const struct cmds *cmd)
449
{
450
451 54128
        for (; cmd->name != NULL; cmd++)
452 47760
                cmd->cmd(NULL, NULL, NULL, NULL);
453 6368
}
454
455
/**********************************************************************
456
 * Execute a file
457
 */
458
459
int
460 3184
fail_out(void)
461
{
462
        unsigned old_err;
463
        static int once = 0;
464
465 3184
        if (once++) {
466 0
                vtc_log(vltop, 1, "failure during reset");
467 0
                return (vtc_error);
468
        }
469 3184
        old_err = vtc_error;
470 3184
        if (!vtc_stop)
471 3148
                vtc_stop = 1;
472 3184
        vtc_log(vltop, 1, "RESETTING after %s", tfn);
473 3184
        reset_cmds(global_cmds);
474 3184
        reset_cmds(cmds);
475 3184
        vtc_error |= old_err;
476
477 3184
        if (vtc_error)
478 8
                vtc_log(vltop, 1, "TEST %s FAILED", tfn);
479
        else
480 3176
                vtc_log(vltop, 1, "TEST %s completed", tfn);
481
482 3184
        if (vtc_stop > 1)
483 36
                return (1);
484 3148
        return (vtc_error);
485 3184
}
486
487
int
488 3184
exec_file(const char *fn, const char *script, const char *tmpdir,
489
    char *logbuf, unsigned loglen)
490
{
491
        FILE *f;
492
        struct vsb *vsb;
493
        const char *p;
494
        char *q;
495
496 3184
        (void)signal(SIGPIPE, SIG_IGN);
497
498 3184
        AZ(pthread_mutex_init(&vtc_vrnd_mtx, NULL));
499 3184
        VRND_Lock = vtc_vrnd_lock;
500 3184
        VRND_Unlock = vtc_vrnd_unlock;
501 3184
        VRND_SeedAll();
502
503 3184
        tfn = fn;
504 3184
        vtc_loginit(logbuf, loglen);
505 3184
        vltop = vtc_logopen("top");
506 3184
        AN(vltop);
507
508 3184
        vtc_log(vltop, 1, "TEST %s starting", fn);
509
510 3184
        init_macro();
511 3184
        init_server();
512 3184
        init_syslog();
513
514 3184
        vsb = VSB_new_auto();
515 3184
        AN(vsb);
516 3184
        if (*fn != '/') {
517 3184
                q = macro_get("pwd", NULL);
518 3184
                AN(q);
519 3184
                VSB_cat(vsb, q);
520 3184
                free(q);
521 3184
        }
522 3184
        p = strrchr(fn, '/');
523 3184
        if (p != NULL) {
524 3156
                VSB_putc(vsb, '/');
525 3156
                VSB_bcat(vsb, fn, p - fn);
526 3156
        }
527 3184
        if (VSB_len(vsb) == 0)
528 0
                VSB_putc(vsb, '/');
529 3184
        AZ(VSB_finish(vsb));
530 3184
        macro_def(vltop, NULL, "testdir", "%s", VSB_data(vsb));
531 3184
        VSB_destroy(&vsb);
532
533
        /* Move into our tmpdir */
534 3184
        AZ(chdir(tmpdir));
535 3184
        macro_def(vltop, NULL, "tmpdir", "%s", tmpdir);
536
537
        /* Drop file to tell what was going on here */
538 3184
        f = fopen("INFO", "w");
539 3184
        AN(f);
540 3184
        fprintf(f, "Test case: %s\n", fn);
541 3184
        AZ(fclose(f));
542
543 3184
        vtc_stop = 0;
544
545 3184
        vtc_thread = pthread_self();
546 3184
        parse_string(script, cmds, NULL, vltop);
547 3184
        return (fail_out());
548
}