Commit 0e3794c3 authored by Geoff Simmons's avatar Geoff Simmons

add some more tests adapted from VMOD re and re2 testing code, and

fix error handling and the fini call
parent 509b716e
# looks like -*- vcl -*-
varnishtest "strings over 256 bytes (== varnish r00896.vtc)"
server s1 {
rxreq
txresp
} -start
varnish v1 -vcl+backend {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
sub vcl_init {
new longregex = re2.regex("^(abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij|abcdefghij)");
}
sub vcl_recv {
if (longregex.match(req.http.host)) {
return(synth(500, "not ok"));
}
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# looks like -*- vcl -*-
varnishtest "test null match (varnish r00913.vtc)"
server s1 {
rxreq
txresp -body "foobar"
} -start
varnish v1 -vcl+backend {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
sub vcl_init {
new end = re2.regex("$");
new empty = re2.regex("");
}
sub vcl_backend_response {
if (end.match(beresp.http.bar)) {
set beresp.http.foo = "XXX";
}
if (empty.match(beresp.http.foo)) {
set beresp.http.bar = "YYY";
}
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
expect resp.http.foo == "XXX"
expect resp.http.bar == "YYY"
} -run
# looks like -*- vcl -*-
varnishtest "regex compilation failure"
varnish v1 -vcl { backend b { .host="${bad_ip}"; } } -start
varnish v1 -errvcl {vmod re2 error: Cannot compile '(' in bad constructor: missing ): (} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("(");
}
}
# From re2_test.cc, 'Rejects'
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a\1' in bad constructor: invalid escape sequence: \1} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a\1");
}
}
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a[x' in bad constructor: missing ]: [x} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a[x");
}
}
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a[z-a]' in bad constructor: invalid character class range: z-a} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a[z-a]");
}
}
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a[[:foobar:]]' in bad constructor: invalid character class range: [:foobar:]} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a[[:foobar:]]");
}
}
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a(b' in bad constructor: missing ): a(b} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a(b");
}
}
varnish v1 -errvcl {vmod re2 error: Cannot compile 'a\' in bad constructor: trailing \} {
import re2 from "${vmod_topbuild}/src/.libs/libvmod_re2.so";
backend b { .host = "${bad_ip}"; }
sub vcl_init {
new bad = re2.regex("a\");
}
}
......@@ -53,6 +53,7 @@ struct vmod_re2_regex {
#define VMOD_RE2_REGEX_MAGIC 0x5c3f6f24
pthread_key_t ovk;
vre2 *vre2;
char *vcl_name;
};
#if 0
......@@ -107,13 +108,11 @@ vmod_regex__init(const struct vrt_ctx *ctx, struct vmod_re2_regex **rep,
AZ(pthread_key_create(&re->ovk, NULL));
err = vre2_init(&re->vre2, pattern);
if (err != NULL) {
vre2_fini(&re->vre2);
re->vre2 = NULL;
FREE_OBJ(re);
*rep = NULL;
VERR(ctx, "Cannot compile '%s' in regex object %s: %s", pattern,
VERR(ctx, "Cannot compile '%s' in %s constructor: %s", pattern,
vcl_name, err);
return;
}
re->vcl_name = strdup(vcl_name);
}
VCL_VOID
......@@ -123,9 +122,11 @@ vmod_regex__fini(struct vmod_re2_regex **rep)
re = *rep;
*rep = NULL;
AZ(pthread_key_delete(re->ovk));
CHECK_OBJ_NOTNULL(re, VMOD_RE2_REGEX_MAGIC);
vre2_fini(&re->vre2);
AZ(pthread_key_delete(re->ovk));
if (re->vcl_name != NULL)
free(re->vcl_name);
FREE_OBJ(re);
}
......@@ -139,7 +140,7 @@ vmod_regex_match(const struct vrt_ctx *ctx, struct vmod_re2_regex *re,
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_NOTNULL(re, VMOD_RE2_REGEX_MAGIC);
if ((err = vre2_match(re->vre2, subject, &match)) != NULL) {
VERR(ctx, ".match(): %s", err);
VERR(ctx, "%s.match(\"%s\"): %s", re->vcl_name, subject, err);
return 0;
}
return match;
......
......@@ -30,6 +30,9 @@
#include "vre2.h"
#define CATCHALL \
catch (const runtime_error& err) { \
return err.what(); \
} \
catch (const exception& ex) { \
return ex.what(); \
} \
......@@ -40,10 +43,9 @@
using namespace std;
vre2::vre2(const char *pattern) {
re_ = new RE2(pattern);
re_ = new RE2(pattern, RE2::Quiet);
if (!re_->ok())
throw new runtime_error(re_->error() + " at '"
+ re_->error_arg() + "'");
throw runtime_error(re_->error());
}
vre2::~vre2() {
......
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