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 12354
vtc_vrnd_lock(void)
58
{
59 12354
        AZ(pthread_mutex_lock(&vtc_vrnd_mtx));
60 12354
}
61
62
static void
63 12354
vtc_vrnd_unlock(void)
64
{
65 12354
        AZ(pthread_mutex_unlock(&vtc_vrnd_mtx));
66 12354
}
67
68
/**********************************************************************
69
 * Macro facility
70
 */
71
72
struct macro {
73
        unsigned                magic;
74
#define MACRO_MAGIC             0x803423e3
75
        VTAILQ_ENTRY(macro)     list;
76
        char                    *name;
77
        char                    *val;
78
};
79
80
static VTAILQ_HEAD(,macro) macro_list = VTAILQ_HEAD_INITIALIZER(macro_list);
81
82
/**********************************************************************/
83
84
static struct macro *
85 34494
macro_def_int(const char *name, const char *fmt, va_list ap)
86
{
87
        struct macro *m;
88
        char buf[256];
89
90 286761
        VTAILQ_FOREACH(m, &macro_list, list)
91 253041
                if (!strcmp(name, m->name))
92 774
                        break;
93 34494
        if (m == NULL) {
94 33720
                ALLOC_OBJ(m, MACRO_MAGIC);
95 33720
                AN(m);
96 33720
                REPLACE(m->name, name);
97 33720
                AN(m->name);
98 33720
                VTAILQ_INSERT_TAIL(&macro_list, m, list);
99
        }
100 34494
        AN(m);
101 34494
        vbprintf(buf, fmt, ap);
102 34494
        REPLACE(m->val, buf);
103 34494
        AN(m->val);
104 34494
        return (m);
105
}
106
107
108
/**********************************************************************
109
 * This is for defining macros before we fork the child process which
110
 * runs the test-case.
111
 */
112
113
void
114 11127
extmacro_def(const char *name, const char *fmt, ...)
115
{
116
        va_list ap;
117
118 11127
        va_start(ap, fmt);
119 11127
        (void)macro_def_int(name, fmt, ap);
120 11127
        va_end(ap);
121 11127
}
122
123
/**********************************************************************
124
 * Below this point is run inside the testing child-process.
125
 */
126
127
static pthread_mutex_t          macro_mtx;
128
129
static void
130 2289
init_macro(void)
131
{
132
        struct macro *m;
133
134
        /* Dump the extmacros for completeness */
135 13719
        VTAILQ_FOREACH(m, &macro_list, list)
136 11430
                vtc_log(vltop, 4, "extmacro def %s=%s", m->name, m->val);
137
138 2289
        AZ(pthread_mutex_init(&macro_mtx, NULL));
139 2289
}
140
141
void
142 23367
macro_def(struct vtclog *vl, const char *instance, const char *name,
143
    const char *fmt, ...)
144
{
145
        char buf1[256];
146
        struct macro *m;
147
        va_list ap;
148
149 23367
        AN(fmt);
150
151 23367
        if (instance != NULL) {
152 18789
                bprintf(buf1, "%s_%s", instance, name);
153 18789
                name = buf1;
154
        }
155
156 23367
        AZ(pthread_mutex_lock(&macro_mtx));
157 23367
        va_start(ap, fmt);
158 23367
        m = macro_def_int(name, fmt, ap);
159 23367
        va_end(ap);
160 23367
        vtc_log(vl, 4, "macro def %s=%s", name, m->val);
161 23367
        AZ(pthread_mutex_unlock(&macro_mtx));
162 23367
}
163
164
void
165 7713
macro_undef(struct vtclog *vl, const char *instance, const char *name)
166
{
167
        char buf1[256];
168
        struct macro *m;
169
170 7713
        if (instance != NULL) {
171 7713
                bprintf(buf1, "%s_%s", instance, name);
172 7713
                name = buf1;
173
        }
174
175 7713
        AZ(pthread_mutex_lock(&macro_mtx));
176 64623
        VTAILQ_FOREACH(m, &macro_list, list)
177 64515
                if (!strcmp(name, m->name))
178 7605
                        break;
179 7713
        if (m != NULL) {
180 7605
                if (!vtc_stop)
181 228
                        vtc_log(vl, 4, "macro undef %s", name);
182 7605
                VTAILQ_REMOVE(&macro_list, m, list);
183 7605
                free(m->name);
184 7605
                free(m->val);
185 7605
                FREE_OBJ(m);
186
        }
187 7713
        AZ(pthread_mutex_unlock(&macro_mtx));
188 7713
}
189
190
char *
191 19818
macro_get(const char *b, const char *e)
192
{
193
        struct macro *m;
194
        int l;
195 19818
        char *retval = NULL;
196
197 19818
        AN(b);
198 19818
        if (e == NULL)
199 8553
                e = strchr(b, '\0');
200 19818
        AN(e);
201 19818
        l = e - b;
202
203 19818
        if (l == 4 && !memcmp(b, "date", l)) {
204 9
                double t = VTIM_real();
205 9
                retval = malloc(64);
206 9
                AN(retval);
207 9
                VTIM_format(t, retval);
208 9
                return (retval);
209
        }
210
211 19809
        AZ(pthread_mutex_lock(&macro_mtx));
212 111660
        VTAILQ_FOREACH(m, &macro_list, list) {
213 111612
                CHECK_OBJ_NOTNULL(m, MACRO_MAGIC);
214 111612
                if (!strncmp(b, m->name, l) && m->name[l] == '\0')
215 19761
                        break;
216
        }
217 19809
        if (m != NULL)
218 19761
                retval = strdup(m->val);
219 19809
        AZ(pthread_mutex_unlock(&macro_mtx));
220 19809
        return (retval);
221
}
222
223
struct vsb *
224 2817
macro_expandf(struct vtclog *vl, const char *fmt, ...)
225
{
226
        va_list ap;
227
        struct vsb *vsb1, *vsb2;
228
229 2817
        vsb1 = VSB_new_auto();
230 2817
        AN(vsb1);
231 2817
        va_start(ap, fmt);
232 2817
        VSB_vprintf(vsb1, fmt, ap);
233 2817
        va_end(ap);
234 2817
        AZ(VSB_finish(vsb1));
235 2817
        vsb2 = macro_expand(vl, VSB_data(vsb1));
236 2817
        VSB_destroy(&vsb1);
237 2817
        return (vsb2);
238
}
239
240
struct vsb *
241 10794
macro_expand(struct vtclog *vl, const char *text)
242
{
243
        struct vsb *vsb;
244
        const char *p, *q;
245
        char *m;
246
247 10794
        vsb = VSB_new_auto();
248 10794
        AN(vsb);
249 32853
        while (*text != '\0') {
250 18153
                p = strstr(text, "${");
251 18153
                if (p == NULL) {
252 6888
                        VSB_cat(vsb, text);
253 6888
                        break;
254
                }
255 11265
                VSB_bcat(vsb, text, p - text);
256 11265
                q = strchr(p, '}');
257 11265
                if (q == NULL) {
258 0
                        VSB_cat(vsb, text);
259 0
                        break;
260
                }
261 11265
                assert(p[0] == '$');
262 11265
                assert(p[1] == '{');
263 11265
                assert(q[0] == '}');
264 11265
                p += 2;
265 11265
                m = macro_get(p, q);
266 11265
                if (m == NULL) {
267 48
                        if (!ign_unknown_macro) {
268 0
                                VSB_destroy(&vsb);
269 0
                                vtc_fatal(vl, "Macro ${%.*s} not found",
270 0
                                          (int)(q - p), p);
271
                                NEEDLESS(return (NULL));
272
                        }
273 48
                        VSB_printf(vsb, "${%.*s}", (int)(q - p), p);
274
                } else {
275 11217
                        VSB_printf(vsb, "%s", m);
276 11217
                        free(m);
277
                }
278 11265
                text = q + 1;
279
        }
280 10794
        AZ(VSB_finish(vsb));
281 10794
        return (vsb);
282
}
283
284
/**********************************************************************
285
 * Parse a string
286
 *
287
 * We make a copy of the string and deliberately leak it, so that all
288
 * the cmd functions we call don't have to strdup(3) all over the place.
289
 *
290
 * Static checkers like Coverity may bitch about this, but we don't care.
291
 */
292
293
void
294 13843
parse_string(const char *spec, const struct cmds *cmd, void *priv,
295
    struct vtclog *vl)
296
{
297
        char *token_s[MAX_TOKENS], *token_e[MAX_TOKENS];
298
        struct vsb *token_exp[MAX_TOKENS];
299
        char *e, *p, *q, *f, *buf;
300
        int nest_brace;
301
        int tn;
302
        const struct cmds *cp;
303
304 13843
        AN(spec);
305 13843
        buf = strdup(spec);
306 13843
        AN(buf);
307 13843
        e = strchr(buf, '\0');
308 13843
        AN(e);
309 183981
        for (p = buf; p < e; p++) {
310 170258
                if (vtc_error || vtc_stop)
311
                        break;
312
                /* Start of line */
313 170229
                if (isspace(*p))
314 91642
                        continue;
315 78595
                if (*p == '\n')
316 0
                        continue;
317
318 78595
                if (*p == '#') {
319 4335
                        for (; *p != '\0' && *p != '\n'; p++)
320
                                ;
321 4335
                        if (*p == '\0')
322 0
                                break;
323 4335
                        continue;
324
                }
325
326 74260
                q = strchr(p, '\n');
327 74260
                if (q == NULL)
328 693
                        q = strchr(p, '\0');
329 74260
                if (q - p > 60)
330 3789
                        vtc_log(vl, 2, "=== %.60s...", p);
331
                else
332 70471
                        vtc_log(vl, 2, "=== %.*s", (int)(q - p), p);
333
334
                /* First content on line, collect tokens */
335 74254
                tn = 0;
336 74254
                f = p;
337 564928
                while (p < e) {
338 489979
                        assert(tn < MAX_TOKENS);
339 489982
                        assert(p < e);
340 489982
                        if (*p == '\n') { /* End on NL */
341 73564
                                break;
342
                        }
343 416418
                        if (isspace(*p)) { /* Inter-token whitespace */
344 175061
                                p++;
345 175061
                                continue;
346
                        }
347 241370
                        if (*p == '\\' && p[1] == '\n') { /* line-cont */
348 2193
                                p += 2;
349 2193
                                continue;
350
                        }
351 239177
                        if (*p == '"') { /* quotes */
352 24586
                                token_s[tn] = ++p;
353 24586
                                q = p;
354 574815
                                for (; *p != '\0'; p++) {
355 574815
                                        assert(p < e);
356 574815
                                        if (*p == '"')
357 24586
                                                break;
358 550229
                                        if (*p == '\\') {
359 7194
                                                p += VAV_BackSlash(p, q) - 1;
360 7194
                                                q++;
361
                                        } else {
362 543035
                                                if (*p == '\n')
363 0
                                                        vtc_fatal(vl,
364
                                "Unterminated quoted string in line: %*.*s",
365 0
                                (int)(p - f), (int)(p - f), f);
366 543035
                                                assert(*p != '\n');
367 543035
                                                *q++ = *p;
368
                                        }
369
                                }
370 24586
                                token_e[tn++] = q;
371 24586
                                p++;
372 214591
                        } else if (*p == '{') { /* Braces */
373 15651
                                nest_brace = 0;
374 15651
                                token_s[tn] = p + 1;
375 2404720
                                for (; p < e; p++) {
376 2404647
                                        if (*p == '{')
377 27873
                                                nest_brace++;
378 2376774
                                        else if (*p == '}') {
379 27873
                                                if (--nest_brace == 0)
380 15578
                                                        break;
381
                                        }
382
                                }
383 15651
                                assert(*p == '}');
384 15651
                                token_e[tn++] = p++;
385
                        } else { /* other tokens */
386 198940
                                token_s[tn] = p;
387 1304137
                                for (; p < e && !isspace(*p); p++)
388 1105197
                                        continue;
389 198929
                                token_e[tn++] = p;
390
                        }
391
                }
392
393 74259
                assert(p <= e);
394 74259
                assert(tn < MAX_TOKENS);
395 74259
                token_s[tn] = NULL;
396 313430
                for (tn = 0; token_s[tn] != NULL; tn++) {
397 239169
                        token_exp[tn] = NULL;
398 239169
                        AN(token_e[tn]);        /*lint !e771 */
399 239171
                        *token_e[tn] = '\0';    /*lint !e771 */
400 239171
                        if (NULL != strstr(token_s[tn], "${")) {
401 1980
                                token_exp[tn] = macro_expand(vl, token_s[tn]);
402 1980
                                if (vtc_error)
403 0
                                        return;
404 1980
                                token_s[tn] = VSB_data(token_exp[tn]);
405 1980
                                token_e[tn] = strchr(token_s[tn], '\0');
406
                        }
407
                }
408
409 1360669
                for (cp = cmd; cp->name != NULL; cp++)
410 1360575
                        if (!strcmp(token_s[0], cp->name))
411 74167
                                break;
412
413 74261
                if (cp->name == NULL)
414 0
                        vtc_fatal(vl, "Unknown command: \"%s\"", token_s[0]);
415
416 74261
                assert(cp->cmd != NULL);
417 74261
                cp->cmd(token_s, priv, cmd, vl);
418
        }
419
}
420
421
/**********************************************************************
422
 * Reset commands (between tests)
423
 */
424
425
static void
426 2289
reset_cmds(const struct cmds *cmd)
427
{
428
429 34335
        for (; cmd->name != NULL; cmd++)
430 32046
                cmd->cmd(NULL, NULL, NULL, NULL);
431 2289
}
432
433
/**********************************************************************
434
 * Execute a file
435
 */
436
437
static const struct cmds cmds[] = {
438
#define CMD(n) { #n, cmd_##n },
439
#include "cmds.h"
440
        { NULL, NULL }
441
};
442
443
static const char *tfn;
444
445
int
446 2289
fail_out(void)
447
{
448
        unsigned old_err;
449
        static int once = 0;
450
451 2289
        if (once++) {
452 0
                vtc_log(vltop, 1, "failure during reset");
453 0
                return(vtc_error);
454
        }
455 2289
        old_err = vtc_error;
456 2289
        if (!vtc_stop)
457 2262
                vtc_stop = 1;
458 2289
        vtc_log(vltop, 1, "RESETTING after %s", tfn);
459 2289
        reset_cmds(cmds);
460 2289
        vtc_error |= old_err;
461
462 2289
        if (vtc_error)
463 6
                vtc_log(vltop, 1, "TEST %s FAILED", tfn);
464
        else
465 2283
                vtc_log(vltop, 1, "TEST %s completed", tfn);
466
467 2289
        if (vtc_stop > 1)
468 27
                return (1);
469 2262
        return (vtc_error);
470
}
471
472
int
473 2289
exec_file(const char *fn, const char *script, const char *tmpdir,
474
    char *logbuf, unsigned loglen)
475
{
476
        FILE *f;
477
        struct vsb *vsb;
478
        const char *p;
479
        char *q;
480
481 2289
        (void)signal(SIGPIPE, SIG_IGN);
482
483 2289
        AZ(pthread_mutex_init(&vtc_vrnd_mtx, NULL));
484 2289
        VRND_Lock = vtc_vrnd_lock;
485 2289
        VRND_Unlock = vtc_vrnd_unlock;
486 2289
        VRND_SeedAll();
487
488 2289
        tfn = fn;
489 2289
        vtc_loginit(logbuf, loglen);
490 2289
        vltop = vtc_logopen("top");
491 2289
        AN(vltop);
492
493 2289
        init_macro();
494 2289
        init_server();
495 2289
        init_syslog();
496
497 2289
        vsb = VSB_new_auto();
498 2289
        AN(vsb);
499 2289
        if (*fn != '/') {
500 2289
                q = macro_get("pwd", NULL);
501 2289
                AN(q);
502 2289
                VSB_cat(vsb, q);
503 2289
                free(q);
504
        }
505 2289
        p = strrchr(fn, '/');
506 2289
        if (p != NULL) {
507 2268
                VSB_putc(vsb, '/');
508 2268
                VSB_bcat(vsb, fn, p - fn);
509
        }
510 2289
        if (VSB_len(vsb) == 0)
511 0
                VSB_putc(vsb, '/');
512 2289
        AZ(VSB_finish(vsb));
513 2289
        macro_def(vltop, NULL, "testdir", "%s", VSB_data(vsb));
514 2289
        VSB_destroy(&vsb);
515
516
        /* Move into our tmpdir */
517 2289
        AZ(chdir(tmpdir));
518 2289
        macro_def(vltop, NULL, "tmpdir", "%s", tmpdir);
519
520
        /* Drop file to tell what was going on here */
521 2289
        f = fopen("INFO", "w");
522 2289
        AN(f);
523 2289
        fprintf(f, "Test case: %s\n", fn);
524 2289
        AZ(fclose(f));
525
526 2289
        vtc_stop = 0;
527 2289
        vtc_log(vltop, 1, "TEST %s starting", fn);
528
529 2289
        vtc_thread = pthread_self();
530 2289
        parse_string(script, cmds, NULL, vltop);
531 2283
        return(fail_out());
532
}