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