Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_misc.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
#include <sys/socket.h>
34
35
#include <grp.h>
36
#include <math.h>
37
#include <pwd.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
#include <string.h>
41
#include <sys/un.h>
42
#include <unistd.h>
43
44
#ifdef HAVE_SYS_PERSONALITY_H
45
#  include <sys/personality.h>
46
#endif
47
48
#include "vtc.h"
49
50
#include "vfil.h"
51
#include "vnum.h"
52
#include "vre.h"
53
#include "vtcp.h"
54
#include "vsa.h"
55
#include "vss.h"
56
#include "vtim.h"
57
#include "vus.h"
58
59
/* SECTION: vtest vtest
60
 *
61
 * This should be the first command in your vtc as it will identify the test
62
 * case with a short yet descriptive sentence. It takes exactly one argument, a
63
 * string, eg::
64
 *
65
 *         vtest "Check that vtest is actually a valid command"
66
 *
67
 * It will also print that string in the log.
68
 */
69
70
void v_matchproto_(cmd_f)
71
cmd_vtest(CMD_ARGS)
72 2238
{
73
74 2238
        (void)priv;
75 2238
        if (av == NULL)
76 1122
                return;
77 1116
        AZ(vstrcmp(av[0], "vtest"));
78
79 1116
        ARGN(vl, av, 1);
80 1116
        vtc_log(vl, 1, "VTEST %s", av[1]);
81 1116
        ARGZ(vl, av, 2);
82 1116
}
83
84
/* SECTION: varnishtest varnishtest
85
 *
86
 * Alternate name for 'vtest', see above.
87
 *
88
 */
89
90
void v_matchproto_(cmd_f)
91
cmd_varnishtest(CMD_ARGS)
92 1128
{
93
94 1128
        (void)priv;
95 1128
        if (av == NULL)
96 1122
                return;
97 6
        AZ(vstrcmp(av[0], "varnishtest"));
98
99 6
        ARGN(vl, av, 1);
100 6
        vtc_log(vl, 1, "VTEST %s", av[1]);
101 6
        ARGZ(vl, av, 2);
102 6
}
103
104
/* SECTION: shell shell
105
 *
106
 * NOTE: This command is available everywhere commands are given.
107
 *
108
 * Pass the string given as argument to a shell. If you have multiple
109
 * commands to run, you can use curly brackets to describe a multi-lines
110
 * script, eg::
111
 *
112
 *         shell {
113
 *                 echo begin
114
 *                 cat /etc/fstab
115
 *                 echo end
116
 *         }
117
 *
118
 * By default a zero exit code is expected, otherwise the vtc will fail.
119
 *
120
 * Notice that the commandstring is prefixed with "exec 2>&1;" to combine
121
 * stderr and stdout back to the test process.
122
 *
123
 * Optional arguments:
124
 *
125
 * \-err
126
 *      Expect non-zero exit code.
127
 *
128
 * \-exit N
129
 *      Expect exit code N instead of zero.
130
 *
131
 * \-expect STRING
132
 *      Expect string to be found in stdout+err.
133
 *
134
 * \-match REGEXP
135
 *      Expect regexp to match the stdout+err output.
136
 */
137
/* SECTION: client-server.spec.shell
138
 *
139
 * shell
140
 *      Same as for the top-level shell.
141
 */
142
143
static void
144
cmd_shell_engine(struct vtclog *vl, int ok, const char *cmd,
145
    const char *expect, const char *re)
146 525
{
147 525
        struct vsb *vsb, re_vsb[1];
148 525
        FILE *fp;
149 525
        vre_t *vre = NULL;
150 525
        int r, c;
151 525
        int err, erroff;
152 525
        char errbuf[VRE_ERROR_LEN];
153
154 525
        AN(vl);
155 525
        AN(cmd);
156 525
        vsb = VSB_new_auto();
157 525
        AN(vsb);
158 525
        if (re != NULL) {
159 86
                vre = VRE_compile(re, 0, &err, &erroff, 1);
160 86
                if (vre == NULL) {
161 0
                        AN(VSB_init(re_vsb, errbuf, sizeof errbuf));
162 0
                        AZ(VRE_error(re_vsb, err));
163 0
                        AZ(VSB_finish(re_vsb));
164 0
                        VSB_fini(re_vsb);
165 0
                        vtc_fatal(vl,
166 0
                            "shell_match invalid regexp (\"%s\" at %d)",
167 0
                            errbuf, erroff);
168 0
                }
169 86
        }
170 525
        VSB_printf(vsb, "set -e ;");
171 525
        VSB_printf(vsb, "exec 2>&1 ; %s", cmd);
172 525
        AZ(VSB_finish(vsb));
173 525
        vtc_dump(vl, 4, "shell_cmd", VSB_data(vsb), -1);
174 525
        fp = popen(VSB_data(vsb), "r");
175 525
        if (fp == NULL)
176 0
                vtc_fatal(vl, "popen fails: %s", strerror(errno));
177 525
        VSB_clear(vsb);
178 816598
        do {
179 816598
                c = getc(fp);
180 816598
                if (c != EOF)
181 816073
                        VSB_putc(vsb, c);
182 816598
        } while (c != EOF);
183 525
        r = pclose(fp);
184 525
        AZ(VSB_finish(vsb));
185 525
        vtc_dump(vl, 4, "shell_out", VSB_data(vsb), VSB_len(vsb));
186 525
        vtc_log(vl, 4, "shell_status = 0x%04x", WEXITSTATUS(r));
187 525
        if (WIFSIGNALED(r))
188 0
                vtc_log(vl, 4, "shell_signal = %d", WTERMSIG(r));
189
190 525
        if (ok < 0 && !WEXITSTATUS(r) && !WIFSIGNALED(r))
191 0
                vtc_fatal(vl, "shell did not fail as expected");
192 525
        else if (ok >= 0 && WEXITSTATUS(r) != ok)
193 2
                vtc_fatal(vl, "shell_exit not as expected: "
194 2
                    "got 0x%04x wanted 0x%04x", WEXITSTATUS(r), ok);
195
196 523
        if (expect != NULL) {
197 243
                if (strstr(VSB_data(vsb), expect) == NULL)
198 0
                        vtc_fatal(vl,
199 0
                            "shell_expect not found: (\"%s\")", expect);
200 243
                else
201 243
                        vtc_log(vl, 4, "shell_expect found");
202 280
        } else if (vre != NULL) {
203 86
                if (VRE_match(vre, VSB_data(vsb), VSB_len(vsb), 0, NULL) < 1)
204 0
                        vtc_fatal(vl, "shell_match failed: (\"%s\")", re);
205 86
                else
206 86
                        vtc_log(vl, 4, "shell_match succeeded");
207 86
                VRE_free(&vre);
208 86
        }
209 523
        VSB_destroy(&vsb);
210 523
}
211
212
213
void
214
cmd_shell(CMD_ARGS)
215 1647
{
216 1647
        const char *expect = NULL;
217 1647
        const char *re = NULL;
218 1647
        int n;
219 1647
        int ok = 0;
220
221 1647
        (void)priv;
222
223 1647
        if (av == NULL)
224 1122
                return;
225 1089
        for (n = 1; av[n] != NULL; n++) {
226 1089
                if (!vstrcmp(av[n], "-err")) {
227 212
                        ok = -1;
228 877
                } else if (!vstrcmp(av[n], "-exit")) {
229 23
                        n += 1;
230 23
                        ok = atoi(av[n]);
231 854
                } else if (!vstrcmp(av[n], "-expect")) {
232 243
                        if (re != NULL)
233 0
                                vtc_fatal(vl,
234 0
                                    "Cannot use -expect with -match");
235 243
                        n += 1;
236 243
                        expect = av[n];
237 611
                } else if (!vstrcmp(av[n], "-match")) {
238 86
                        if (expect != NULL)
239 0
                                vtc_fatal(vl,
240 0
                                    "Cannot use -match with -expect");
241 86
                        n += 1;
242 86
                        re = av[n];
243 525
                } else {
244 525
                        break;
245 525
                }
246 1089
        }
247 525
        ARGN(vl, av, n);
248 525
        cmd_shell_engine(vl, ok, av[n], expect, re);
249 525
}
250
251
/* SECTION: filewrite filewrite
252
 *
253
 * Write strings to file
254
 *
255
 *         filewrite [-a] /somefile "Hello" " " "World\n"
256
 *
257
 * The -a flag opens the file in append mode.
258
 *
259
 */
260
261
void v_matchproto_(cmd_f)
262
cmd_filewrite(CMD_ARGS)
263 1157
{
264 1157
        FILE *fo;
265 1157
        int n;
266 1157
        const char *mode = "w";
267
268 1157
        (void)priv;
269
270 1157
        if (av == NULL)
271 1122
                return;
272 35
        if (av[1] != NULL && !vstrcmp(av[1], "-a")) {
273 17
                av++;
274 17
                mode = "a";
275 17
        }
276 35
        if (av[1] == NULL)
277 0
                vtc_fatal(vl, "Need filename");
278 35
        fo = fopen(av[1], mode);
279 35
        if (fo == NULL)
280 0
                vtc_fatal(vl, "Cannot open %s: %s", av[1], strerror(errno));
281 84
        for (n = 2; av[n] != NULL; n++)
282 49
                (void)fputs(av[n], fo);
283 35
        AZ(fclose(fo));
284 35
}
285
286
/* SECTION: setenv setenv
287
 *
288
 * Set or change an environment variable::
289
 *
290
 *         setenv FOO "bar baz"
291
 *
292
 * The above will set the environment variable $FOO to the value
293
 * provided. There is also an ``-ifunset`` argument which will only
294
 * set the value if the environment variable does not already
295
 * exist::
296
 *
297
 *        setenv -ifunset FOO quux
298
 */
299
300
void v_matchproto_(cmd_f)
301
cmd_setenv(CMD_ARGS)
302 1129
{
303 1129
        int r;
304 1129
        int force;
305
306 1129
        (void)priv;
307
308 1129
        if (av == NULL)
309 1122
                return;
310 7
        ARGN(vl, av, 1);
311 7
        ARGN(vl, av, 2);
312
313 7
        force = 1;
314 7
        if (vstrcmp("-ifunset", av[1]) == 0) {
315 2
                force = 0;
316 2
                av++;
317 2
                ARGN(vl, av, 2);
318 2
        }
319 7
        ARGZ(vl, av, 3);
320 7
        r = setenv(av[1], av[2], force);
321 7
        if (r != 0)
322 0
                vtc_log(vl, 0, "CMD setenv %s=\"%s\" failed: %s",
323 0
                    av[1], av[2], strerror(errno));
324 7
}
325
326
/* SECTION: delay delay
327
 *
328
 * NOTE: This command is available everywhere commands are given.
329
 *
330
 * Sleep for the number of seconds specified in the argument. The number
331
 * can include a fractional part, e.g. 1.5.
332
 */
333
void
334
cmd_delay(CMD_ARGS)
335 1845
{
336 1845
        double f;
337
338 1845
        (void)priv;
339 1845
        if (av == NULL)
340 1122
                return;
341 723
        ARGN(vl, av, 1);
342 723
        ARGZ(vl, av, 2);
343 723
        f = VNUM(av[1]);
344 723
        if (isnan(f))
345 0
                vtc_fatal(vl, "Syntax error in number (%s)", av[1]);
346 723
        vtc_log(vl, 3, "delaying %g second(s)", f);
347 723
        VTIM_sleep(f);
348 723
}
349
350
/* SECTION: include include
351
 *
352
 * Executes a vtc fragment::
353
 *
354
 *         include FILE [...]
355
 *
356
 * Open a file and execute it as a VTC fragment. This command is available
357
 * everywhere commands are given.
358
 *
359
 */
360
void
361
cmd_include(CMD_ARGS)
362 1126
{
363 1126
        char *spec;
364 1126
        unsigned i;
365
366 1126
        if (av == NULL)
367 1122
                return;
368
369 4
        if (av[1] == NULL)
370 0
                vtc_fatal(vl, "CMD include: At least 1 argument required");
371
372 10
        for (i = 1; av[i] != NULL; i++) {
373 6
                spec = VFIL_readfile(NULL, av[i], NULL);
374 6
                if (spec == NULL)
375 0
                        vtc_fatal(vl, "CMD include: Unable to read file '%s' "
376 0
                            "(%s)", av[i], strerror(errno));
377 6
                vtc_log(vl, 2, "Begin include '%s'", av[i]);
378 6
                parse_string(vl, priv, spec);
379 6
                vtc_log(vl, 2, "End include '%s'", av[i]);
380 6
                free(spec);
381 6
        }
382 4
}
383
384
/**********************************************************************
385
 * Most test-cases use only numeric IP#'s but a few requires non-demented
386
 * DNS services.  This is a basic sanity check for those.
387
 */
388
389
static int
390
dns_works(void)
391 1
{
392 1
        const struct suckaddr *sa;
393 1
        char abuf[VTCP_ADDRBUFSIZE];
394 1
        char pbuf[VTCP_PORTBUFSIZE];
395
396 1
        sa = VSS_ResolveOne(NULL, "dns-canary.vinyl-cache.org", NULL,
397 1
            AF_INET, SOCK_STREAM, 0);
398 1
        if (sa == NULL)
399 0
                return (0);
400 1
        VTCP_name(sa, abuf, sizeof abuf, pbuf, sizeof pbuf);
401 1
        VSA_free(&sa);
402 1
        if (vstrcmp(abuf, "192.0.2.255"))
403 0
                return (0);
404
405 1
        sa = VSS_ResolveOne(NULL, "dns-canary.vinyl-cache.org", NULL,
406 1
            AF_INET6, SOCK_STREAM, 0);
407 1
        if (sa == NULL)
408 1
                return (1); /* the canary is ipv4 only */
409 0
        VSA_free(&sa);
410 0
        return (0);
411 1
}
412
413
/**********************************************************************
414
 * Test if IPv4/IPv6 works
415
 */
416
417
static int
418
ipvx_works(const char *target)
419 11
{
420 11
        const struct suckaddr *sa;
421 11
        int fd;
422
423 11
        sa = VSS_ResolveOne(NULL, target, "0", 0, SOCK_STREAM, 0);
424 11
        if (sa == NULL)
425 0
                return (0);
426 11
        fd = VTCP_bind(sa, NULL);
427 11
        VSA_free(&sa);
428 11
        if (fd >= 0) {
429 11
                VTCP_close(&fd);
430 11
                return (1);
431 11
        }
432 0
        return (0);
433 11
}
434
435
/**********************************************************************/
436
437
static int
438
addr_no_randomize_works(void)
439 0
{
440 0
        int r = 0;
441
442
#ifdef HAVE_SYS_PERSONALITY_H
443
        r = personality(0xffffffff);
444
        r = personality(r | ADDR_NO_RANDOMIZE);
445
#endif
446 0
        return (r >= 0);
447 0
}
448
449
/**********************************************************************/
450
451
static int
452
uds_socket(void *priv, const struct sockaddr_un *uds)
453 1
{
454
455 1
        return (VUS_bind(uds, priv));
456 1
}
457
static int
458
abstract_uds_works(void)
459 1
{
460 1
        const char *err;
461 1
        int fd;
462
463 1
        fd = VUS_resolver("@vtc.feature.abstract_uds", uds_socket, NULL, &err);
464 1
        if (fd < 0)
465 1
                return (0);
466 0
        AZ(close(fd));
467 0
        return (1);
468 0
}
469
470
/* SECTION: feature feature
471
 *
472
 * Test that the required feature(s) for a test are available, and skip
473
 * the test otherwise; or change the interpretation of the test, as
474
 * documented below. feature takes any number of arguments from this list:
475
 *
476
 * 64bit
477
 *        The environment is 64 bits
478
 * ipv4
479
 *        127.0.0.1 works
480
 * ipv6
481
 *        [::1] works
482
 * dns
483
 *        DNS lookups are working
484
 * topbuild
485
 *        The test has been started with '-i'
486
 * root
487
 *        The test has been invoked by the root user
488
 * user <username>
489
 *        The <username> user is present
490
 * group <groupname>
491
 *        The <groupname> group is present
492
 * user_varnish
493
 *        The varnish user is present
494
 * user_vinyl
495
 *        The vinyl user is present
496
 * user_vcache
497
 *        The vcache user is present
498
 * group_varnish
499
 *        The varnish group is present
500
 * group_vinyl
501
 *        The vinyl group is present
502
 * cmd <command-line>
503
 *        A command line that should execute with a zero exit status
504
 * ignore_unknown_macro
505
 *        Do not fail the test if a string of the form ${...} is not
506
 *        recognized as a macro.
507
 * abstract_uds
508
 *        Creation of an abstract unix domain socket succeeded.
509
 * disable_aslr
510
 *        ASLR can be disabled.
511
 * vtest_cmd <command>
512
 *        vtest command <command> is available
513
 *
514
 * A feature name can be prefixed with an exclamation mark (!) to skip a
515
 * test if the feature is present.
516
 *
517
 * Be careful with ignore_unknown_macro, because it may cause a test with a
518
 * misspelled macro to fail silently. You should only need it if you must
519
 * run a test with strings of the form "${...}".
520
 */
521
522
void v_matchproto_(cmd_f)
523
cmd_feature(CMD_ARGS)
524 1240
{
525 1240
        const char *feat;
526 1240
        int r, good, skip, neg;
527
528 1240
        (void)priv;
529
530 1240
        if (av == NULL)
531 1122
                return;
532
533 118
#define FEATURE(nm, tst)                                \
534 1534
        do {                                            \
535 1534
                if (!vstrcmp(feat, nm)) {               \
536 47
                        good = 1;                       \
537 47
                        if (tst) {                      \
538 41
                                skip = neg;             \
539 41
                        } else {                        \
540 6
                                skip = !neg;            \
541 6
                        }                               \
542 47
                }                                       \
543 1534
        } while (0)
544
545 118
        skip = 0;
546
547 212
        for (av++; *av != NULL; av++) {
548 118
                good = 0;
549 118
                neg = 0;
550 118
                feat = *av;
551
552 118
                if (feat[0] == '!') {
553 0
                        neg = 1;
554 0
                        feat++;
555 0
                }
556
557 118
                FEATURE("ipv4", ipvx_works("127.0.0.1"));
558 118
                FEATURE("ipv6", ipvx_works("[::1]"));
559 118
                FEATURE("64bit", sizeof(void*) == 8);
560 118
                FEATURE("disable_aslr", addr_no_randomize_works());
561 118
                FEATURE("dns", dns_works());
562 118
                FEATURE("topbuild", iflg);
563 118
                FEATURE("root", !geteuid());
564 118
                FEATURE("user_varnish", getpwnam("varnish") != NULL);   // XXX remove?
565 118
                FEATURE("user_vinyl", getpwnam("vinyl") != NULL);
566 118
                FEATURE("user_vcache", getpwnam("vcache") != NULL);
567 118
                FEATURE("group_varnish", getgrnam("varnish") != NULL); // XXX remove?
568 118
                FEATURE("group_vinyl", getgrnam("vinyl") != NULL);
569 118
                FEATURE("abstract_uds", abstract_uds_works());
570
571 118
                if (!vstrcmp(feat, "user")) {
572 0
                        av++;
573 0
                        if (*av == NULL)
574 0
                                vtc_fatal(vl, "Missing username");
575 0
                        good = 1;
576 0
                        if (getpwnam(*av) != NULL)
577 0
                                skip = neg;
578 0
                        else
579 0
                                skip = !neg;
580 118
                } else if (!vstrcmp(feat, "group")) {
581 0
                        av++;
582 0
                        if (*av == NULL)
583 0
                                vtc_fatal(vl, "Missing groupname");
584 0
                        good = 1;
585 0
                        if (getgrnam(*av) != NULL)
586 0
                                skip = neg;
587 0
                        else
588 0
                                skip = !neg;
589 118
                } else if (!vstrcmp(feat, "cmd")) {
590 47
                        good = 1;
591 47
                        skip = neg;
592 47
                        av++;
593 47
                        if (*av == NULL)
594 0
                                vtc_fatal(vl, "Missing the command-line");
595 47
                        r = system(*av);
596 47
                        if (WEXITSTATUS(r) != 0)
597 13
                                skip = !neg;
598 71
                } else if (!vstrcmp(feat, "ignore_unknown_macro")) {
599 15
                        ign_unknown_macro = 1;
600 15
                        good = 1;
601 56
                } else if (!vstrcmp(feat, "vtest_cmd")) {
602 9
                        good = 1;
603 9
                        skip = !neg;
604 9
                        av++;
605 9
                        if (*av == NULL)
606 0
                                vtc_fatal(vl, "vtest_cmd needs the command name");
607 9
                        if (find_cmd(*av) != NULL)
608 4
                                skip = neg;
609 9
                }
610 118
                if (!good)
611 0
                        vtc_fatal(vl, "FAIL test, unknown feature: %s", feat);
612
613 118
                if (!skip)
614 94
                        continue;
615
616 24
                vtc_stop = 2;
617 24
                if (neg)
618 0
                        vtc_log(vl, 1,
619 0
                            "SKIPPING test, conflicting feature: %s", feat);
620 24
                else
621 24
                        vtc_log(vl, 1,
622 24
                            "SKIPPING test, lacking feature: %s", feat);
623 24
                return;
624 118
        }
625 118
}