Commit af0c665e authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Un-copy&paste the code to compare two strings in expect.

parent 2e610e1c
......@@ -24,7 +24,6 @@
//
-e732 // Loss of sign (arg. no. 2) (int to unsigned
-e713 // Loss of precision (assignment) (unsigned long long to long long)
-e574 // Signed-unsigned mix with relational
-emacro(835, STRTOU32_CHECK) // A zero has been given as ___ argument to operator '___'
-e788 // enum value not used in defaulted switch
......
......@@ -132,3 +132,5 @@ void b64_settings(const struct http *hp, const char *s);
/* vtc_subr.c */
struct vsb *vtc_hex_to_bin(struct vtclog *vl, const char *arg);
void vtc_expect(struct vtclog *, const char *, const char *, const char *,
const char *, const char *);
......@@ -46,7 +46,6 @@
#include "vfil.h"
#include "vgz.h"
#include "vnum.h"
#include "vre.h"
#include "vtcp.h"
#include "hpack.h"
......@@ -345,13 +344,9 @@ static void
cmd_http_expect(CMD_ARGS)
{
struct http *hp;
const char *lhs, *clhs;
const char *lhs;
char *cmp;
const char *rhs, *crhs;
vre_t *vre;
const char *error;
int erroroffset;
int i, retval = -1;
const char *rhs;
(void)cmd;
(void)vl;
......@@ -367,41 +362,7 @@ cmd_http_expect(CMD_ARGS)
cmp = av[1];
rhs = cmd_var_resolve(hp, av[2]);
clhs = lhs ? lhs : "<undef>";
crhs = rhs ? rhs : "<undef>";
if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) {
vre = VRE_compile(crhs, 0, &error, &erroroffset);
if (vre == NULL)
vtc_fatal(hp->vl, "REGEXP error: %s (@%d) (%s)",
error, erroroffset, crhs);
i = VRE_exec(vre, clhs, strlen(clhs), 0, 0, NULL, 0, 0);
retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
VRE_free(&vre);
} else if (!strcmp(cmp, "==")) {
retval = strcmp(clhs, crhs) == 0;
} else if (!strcmp(cmp, "!=")) {
retval = strcmp(clhs, crhs) != 0;
} else if (lhs == NULL || rhs == NULL) {
// fail inequality comparisons if either side is undef'ed
retval = 0;
} else if (!strcmp(cmp, "<")) {
retval = isless(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, ">")) {
retval = isgreater(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, "<=")) {
retval = islessequal(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, ">=")) {
retval = isgreaterequal(VNUM(lhs), VNUM(rhs));
}
if (retval == -1)
vtc_fatal(hp->vl,
"EXPECT %s (%s) %s %s (%s) test not implemented",
av[0], clhs, av[1], av[2], crhs);
else
vtc_log(hp->vl, retval ? 4 : 0, "EXPECT %s (%s) %s \"%s\" %s",
av[0], clhs, cmp, crhs, retval ? "match" : "failed");
vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
}
static void
......
......@@ -46,8 +46,6 @@
#include "vfil.h"
#include "vgz.h"
#include "vnum.h"
#include "vre.h"
#include "hpack.h"
#include "vend.h"
......@@ -2412,13 +2410,9 @@ cmd_expect(CMD_ARGS)
{
struct http *hp;
struct stream *s;
const char *lhs, *clhs;
const char *lhs;
char *cmp;
const char *rhs, *crhs;
vre_t *vre;
const char *error;
int erroroffset;
int i, retval = -1;
const char *rhs;
char buf[20];
(void)cmd;
......@@ -2437,42 +2431,7 @@ cmd_expect(CMD_ARGS)
lhs = cmd_var_resolve(s, av[0], buf);
cmp = av[1];
rhs = cmd_var_resolve(s, av[2], buf);
clhs = lhs ? lhs : "<undef>";
crhs = rhs ? rhs : "<undef>";
if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) {
vre = VRE_compile(crhs, 0, &error, &erroroffset);
if (vre == NULL)
vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
error, erroroffset, crhs);
i = VRE_exec(vre, clhs, strlen(clhs), 0, 0, NULL, 0, 0);
retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
VRE_free(&vre);
} else if (!strcmp(cmp, "==")) {
retval = strcmp(clhs, crhs) == 0;
} else if (!strcmp(cmp, "!=")) {
retval = strcmp(clhs, crhs) != 0;
} else if (lhs == NULL || rhs == NULL) {
// fail inequality comparisons if either side is undef'ed
retval = 0;
} else if (!strcmp(cmp, "<")) {
retval = isless(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, ">")) {
retval = isgreater(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, "<=")) {
retval = islessequal(VNUM(lhs), VNUM(rhs));
} else if (!strcmp(cmp, ">=")) {
retval = isgreaterequal(VNUM(lhs), VNUM(rhs));
}
if (retval == -1)
vtc_fatal(vl,
"EXPECT %s (%s) %s %s (%s) test not implemented",
av[0], clhs, av[1], av[2], crhs);
else
vtc_log(vl, retval ? 4 : 0, "(s%d) EXPECT %s (%s) %s \"%s\" %s",
s->id, av[0], clhs, cmp, crhs, retval ? "match" : "failed");
vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
AZ(pthread_mutex_unlock(&s->hp->mtx));
}
......
......@@ -28,11 +28,14 @@
#include "config.h"
#include <math.h>
#include <string.h>
#include <sys/types.h>
#include "vtc.h"
#include "vct.h"
#include "vnum.h"
#include "vre.h"
#include "vtc.h"
struct vsb *
vtc_hex_to_bin(struct vtclog *vl, const char *arg)
......@@ -66,3 +69,61 @@ vtc_hex_to_bin(struct vtclog *vl, const char *arg)
AZ(VSB_finish(vsb));
return (vsb);
}
void
vtc_expect(struct vtclog *vl,
const char *olhs, const char *lhs,
const char *cmp,
const char *orhs, const char *rhs)
{
vre_t *vre;
const char *error;
int erroroffset;
int i, j, retval = -1;
double fl, fr;
j = lhs == NULL || rhs == NULL;
if (lhs == NULL)
lhs = "<undef>";
if (rhs == NULL)
rhs = "<undef>";
if (!strcmp(cmp, "~") || !strcmp(cmp, "!~")) {
vre = VRE_compile(rhs, 0, &error, &erroroffset);
if (vre == NULL)
vtc_fatal(vl, "REGEXP error: %s (@%d) (%s)",
error, erroroffset, rhs);
i = VRE_exec(vre, lhs, strlen(lhs), 0, 0, NULL, 0, 0);
retval = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
VRE_free(&vre);
} else if (!strcmp(cmp, "==")) {
retval = strcmp(lhs, rhs) == 0;
} else if (!strcmp(cmp, "!=")) {
retval = strcmp(lhs, rhs) != 0;
} else if (j) {
// fail inequality comparisons if either side is undef'ed
retval = 0;
} else {
fl = VNUM(lhs);
fr = VNUM(rhs);
if (!strcmp(cmp, "<"))
retval = isless(fl, fr);
else if (!strcmp(cmp, ">"))
retval = isgreater(fl, fr);
else if (!strcmp(cmp, "<="))
retval = islessequal(fl, fr);
else if (!strcmp(cmp, ">="))
retval = isgreaterequal(fl, fr);
}
if (retval == -1)
vtc_fatal(vl,
"EXPECT %s (%s) %s %s (%s) test not implemented",
olhs, lhs, cmp, orhs, rhs);
else if (retval == 0)
vtc_fatal(vl, "EXPECT %s (%s) %s \"%s\" failed",
olhs, lhs, cmp, rhs);
else
vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match",
olhs, lhs, cmp, rhs);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment