Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_subr.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2017 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 <math.h>
33
#include <stdio.h>
34
#include <stdlib.h>
35
#include <string.h>
36
#include <stdint.h>
37
#include <unistd.h>
38
#include <sys/resource.h>
39
40
#include "vtc.h"
41
42
#include "vct.h"
43
#include "vnum.h"
44
#include "vre.h"
45
46
#include "vapi/vsig.h"
47
48
struct vsb *
49
vtc_hex_to_bin(struct vtclog *vl, const char *arg)
50 367
{
51 367
        struct vsb *vsb;
52 367
        unsigned sh = 4;
53 367
        unsigned c, b = 0;
54
55 367
        vsb = VSB_new_auto();
56 367
        AN(vsb);
57 13338
        for (; *arg != '\0'; arg++) {
58 12971
                if (vct_issp(*arg) || *arg == '\n')
59 4219
                        continue;
60 8752
                c = (uint8_t)*arg;
61 8752
                if (c >= '0' && c <= '9')
62 7302
                        b |= (c - 48U) << sh;
63 1450
                else if (c >= 'A' && c <= 'F')
64 32
                        b |= (c - 55U) << sh;
65 1418
                else if (c >= 'a' && c <= 'f')
66 1418
                        b |= (c - 87U) << sh;
67 0
                else
68 0
                        vtc_fatal(vl,"Illegal hex string");
69 8752
                sh = 4 - sh;
70 8752
                if (sh == 4) {
71 4376
                        VSB_putc(vsb, b);
72 4376
                        b = 0;
73 4376
                }
74 8752
        }
75 367
        if (sh != 4)
76 0
                VSB_putc(vsb, b);
77 367
        AZ(VSB_finish(vsb));
78 367
        return (vsb);
79 367
}
80
81
void
82
vtc_expect(struct vtclog *vl,
83
    const char *olhs, const char *lhs,
84
    const char *cmp,
85
    const char *orhs, const char *rhs)
86 9738
{
87 9738
        vre_t *vre;
88 9738
        struct vsb vsb[1];
89 9738
        int error, erroroffset;
90 9738
        int i, j, retval = -1;
91 9738
        double fl, fr;
92 9738
        char errbuf[VRE_ERROR_LEN];
93
94 9738
        j = lhs == NULL || rhs == NULL;
95 9738
        if (lhs == NULL)
96 300
                lhs = "<undef>";
97 9738
        if (rhs == NULL)
98 1
                rhs = "<undef>";
99
100 9738
        if (!vstrcmp(cmp, "~") || !vstrcmp(cmp, "!~")) {
101 71
                vre = VRE_compile(rhs, 0, &error, &erroroffset, 1);
102 71
                if (vre == NULL) {
103 0
                        AN(VSB_init(vsb, errbuf, sizeof errbuf));
104 0
                        AZ(VRE_error(vsb, error));
105 0
                        AZ(VSB_finish(vsb));
106 0
                        VSB_fini(vsb);
107 0
                        vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
108 0
                            errbuf, erroroffset, rhs);
109 0
                }
110 71
                i = VRE_match(vre, lhs, 0, 0, NULL);
111 71
                retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
112 71
                VRE_free(&vre);
113 9667
        } else if (!vstrcmp(cmp, "==")) {
114 9507
                retval = vstrcmp(lhs, rhs) == 0;
115 9507
        } else if (!vstrcmp(cmp, "!=")) {
116 51
                retval = vstrcmp(lhs, rhs) != 0;
117 109
        } else if (!vstrcmp(cmp, "-lt")) {
118 2
                retval = strtoul(lhs, NULL, 0) < strtoul(rhs, NULL, 0);
119 107
        } else if (!vstrcmp(cmp, "-le")) {
120 2
                retval = strtoul(lhs, NULL, 0) <= strtoul(rhs, NULL, 0);
121 105
        } else if (!vstrcmp(cmp, "-eq")) {
122 9
                retval = strtoul(lhs, NULL, 0) == strtoul(rhs, NULL, 0);
123 96
        } else if (!vstrcmp(cmp, "-ne")) {
124 0
                retval = strtoul(lhs, NULL, 0) != strtoul(rhs, NULL, 0);
125 96
        } else if (!vstrcmp(cmp, "-ge")) {
126 2
                retval = strtoul(lhs, NULL, 0) >= strtoul(rhs, NULL, 0);
127 94
        } else if (!vstrcmp(cmp, "-gt")) {
128 4
                retval = strtoul(lhs, NULL, 0) > strtoul(rhs, NULL, 0);
129 90
        } else if (j) {
130
                // fail inequality comparisons if either side is undef'ed
131 0
                retval = 0;
132 90
        } else {
133 90
                fl = VNUM(lhs);
134 90
                fr = VNUM(rhs);
135 90
                if (!vstrcmp(cmp, "<"))
136 10
                        retval = isless(fl, fr);
137 80
                else if (!vstrcmp(cmp, ">"))
138 49
                        retval = isgreater(fl, fr);
139 31
                else if (!vstrcmp(cmp, "<="))
140 17
                        retval = islessequal(fl, fr);
141 14
                else if (!vstrcmp(cmp, ">="))
142 14
                        retval = isgreaterequal(fl, fr);
143 90
        }
144
145 9738
        if (retval == -1)
146 0
                vtc_fatal(vl,
147 0
                    "EXPECT %s (%s) %s %s (%s) test not implemented",
148 0
                    olhs, lhs, cmp, orhs, rhs);
149 9738
        else if (retval == 0)
150 0
                vtc_fatal(vl, "EXPECT %s (%s) %s \"%s\" failed",
151 0
                    olhs, lhs, cmp, rhs);
152 9738
        else
153 9738
                vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match",
154 9738
                    olhs, lhs, cmp, rhs);
155 9738
}
156
157
/**********************************************************************
158
 * Wait for a subprocess.
159
 *
160
 * if expect_signal > 0, the process must die on that signal.
161
 * if expect_signal < 0, dying on that signal is allowed, but not required.
162
 * if allow_core > 0, a coredump is allowed, but not required.
163
 * otherwise, the process must die on exit(expect_status)
164
 */
165
166
void
167
vtc_wait4(struct vtclog *vl, long pid,
168
    int expect_status, int expect_signal, int allow_core)
169 1112
{
170 1112
        int status, r;
171 1112
        struct rusage ru;
172
173 1112
        r = wait4(pid, &status, 0, &ru);
174 1112
        if (r < 0)
175 0
                vtc_fatal(vl, "wait4 failed on pid %ld: %s",
176 0
                    pid, strerror(errno));
177 1112
        assert(r == pid);
178 1112
        vtc_log(vl, 2, "WAIT4 pid=%ld status=0x%04x (user %.6f sys %.6f)",
179 1112
            pid, status,
180 1112
            ru.ru_utime.tv_sec + 1e-6 * ru.ru_utime.tv_usec,
181 1112
            ru.ru_stime.tv_sec + 1e-6 * ru.ru_stime.tv_usec
182 1112
        );
183
184 1112
        if (WIFEXITED(status) && expect_signal <= 0 &&
185 1112
            WEXITSTATUS(status) == expect_status)
186 1097
                return;
187
188 15
        if (expect_signal < 0)
189 15
                expect_signal = -expect_signal;
190
191 15
        if (WIFSIGNALED(status) && WCOREDUMP(status) <= allow_core &&
192 15
            WTERMSIG(status) == expect_signal)
193 15
                return;
194 0
        vtc_log(vl, 1, "Expected exit: 0x%x signal: %d core: %d",
195 0
            expect_status, expect_signal, allow_core);
196 0
        vtc_fatal(vl, "Bad exit status: 0x%04x exit 0x%x signal %d core %d",
197 0
            status,
198 0
            WEXITSTATUS(status),
199 0
            WIFSIGNALED(status) ? WTERMSIG(status) : 0,
200 0
            WCOREDUMP(status));
201 15
}
202
203
void *
204
vtc_record(struct vtclog *vl, int fd, struct vsb *vsb)
205 1036
{
206 1036
        char buf[BUFSIZ];
207 1036
        int i;
208
209 11910
        while (1) {
210 11910
                errno = 0;
211 11910
                i = read(fd, buf, sizeof buf - 1);
212 11910
                if (i > 0) {
213 10874
                        if (vsb != NULL)
214 2
                                VSB_bcat(vsb, buf, i);
215 10874
                        buf[i] = '\0';
216 10874
                        vtc_dump(vl, 3, "debug", buf, -2);
217 10874
                } else if (i == 0 && errno == 0) {
218 1036
                        vtc_log(vl, 4, "STDOUT EOF");
219 1036
                        break;
220 1036
                } else {
221 0
                        vtc_log(vl, 4,
222 0
                            "STDOUT read failed with %d - %s.",
223 0
                            errno, strerror(errno));
224 0
                        break;
225 0
                }
226 11910
        }
227
        return (NULL);
228 1036
}
229