varnish-cache/bin/varnishtest/vtc_subr.c
1
/*-
2
 * Copyright (c) 2008-2017 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/types.h>
32
#include <math.h>
33
#include <string.h>
34
#include <stdint.h>
35
36
#include "vct.h"
37
#include "vnum.h"
38
#include "vre.h"
39
#include "vtc.h"
40
41
struct vsb *
42 165
vtc_hex_to_bin(struct vtclog *vl, const char *arg)
43
{
44
        struct vsb *vsb;
45 165
        unsigned sh = 4;
46 165
        unsigned c, b = 0;
47
48 165
        vsb = VSB_new_auto();
49 165
        AN(vsb);
50 5997
        for (; *arg != '\0'; arg++) {
51 5832
                if (vct_issp(*arg) || *arg == '\n')
52 1728
                        continue;
53 4104
                c = (uint8_t)*arg;
54 4104
                if (c >= '0' && c <= '9')
55 3459
                        b |= (c - 48U) << sh;
56 645
                else if (c >= 'A' && c <= 'F')
57 32
                        b |= (c - 55U) << sh;
58 613
                else if (c >= 'a' && c <= 'f')
59 613
                        b |= (c - 87U) << sh;
60
                else
61 0
                        vtc_fatal(vl,"Illegal hex string");
62 4104
                sh = 4 - sh;
63 4104
                if (sh == 4) {
64 2052
                        VSB_putc(vsb, b);
65 2052
                        b = 0;
66
                }
67
        }
68 165
        if (sh != 4)
69 0
                VSB_putc(vsb, b);
70 165
        AZ(VSB_finish(vsb));
71 165
        return (vsb);
72
}
73
74
void
75 4555
vtc_expect(struct vtclog *vl,
76
    const char *olhs, const char *lhs,
77
    const char *cmp,
78
    const char *orhs, const char *rhs)
79
{
80
        vre_t *vre;
81
        const char *error;
82
        int erroroffset;
83 4555
        int i, j, retval = -1;
84
        double fl, fr;
85
86 4555
        j = lhs == NULL || rhs == NULL;
87 4555
        if (lhs == NULL)
88 144
                lhs = "<undef>";
89 4555
        if (rhs == NULL)
90 0
                rhs = "<undef>";
91
92 4555
        if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) {
93 7
                vre = VRE_compile(rhs, 0, &error, &erroroffset);
94 5
                if (vre == NULL)
95 0
                        vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
96
                            error, erroroffset, rhs);
97 5
                i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0);
98 5
                retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
99 5
                VRE_free(&vre);
100 4548
        } else if (!strcmp(cmp, "==")) {
101 4502
                retval = strcmp(lhs, rhs) == 0;
102 46
        } else if (!strcmp(cmp, "!=")) {
103 18
                retval = strcmp(lhs, rhs) != 0;
104 28
        } else if (j) {
105
                // fail inequality comparisons if either side is undef'ed
106 0
                retval = 0;
107
        } else {
108 28
                fl = VNUM(lhs);
109 28
                fr = VNUM(rhs);
110 28
                if (!strcmp(cmp, "<"))
111 5
                        retval = isless(fl, fr);
112 23
                else if (!strcmp(cmp, ">"))
113 6
                        retval = isgreater(fl, fr);
114 17
                else if (!strcmp(cmp, "<="))
115 8
                        retval = islessequal(fl, fr);
116 9
                else if (!strcmp(cmp, ">="))
117 9
                        retval = isgreaterequal(fl, fr);
118
        }
119
120 4554
        if (retval == -1)
121 0
                vtc_fatal(vl,
122
                    "EXPECT %s (%s) %s %s (%s) test not implemented",
123
                    olhs, lhs, cmp, orhs, rhs);
124 4554
        else if (retval == 0)
125 0
                vtc_fatal(vl, "EXPECT %s (%s) %s \"%s\" failed",
126
                    olhs, lhs, cmp, rhs);
127
        else
128 4554
                vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match",
129
                    olhs, lhs, cmp, rhs);
130 4555
}