From 402f6318bb9fea6223b57931120821d011975bb2 Mon Sep 17 00:00:00 2001 From: brectanus Date: Fri, 21 Dec 2007 16:20:51 +0000 Subject: [PATCH] More test updates. --- apache2/msc_test.c | 22 +++++++---- apache2/t/op/beginsWith.t | 44 +++++++++++++++++++++ apache2/t/op/contains.t | 51 ++++++++++++++++++++++++ apache2/t/op/containsWord.t | 79 +++++++++++++++++++++++++++++++++++++ apache2/t/op/endsWith.t | 51 ++++++++++++++++++++++++ apache2/t/op/noMatch.t | 22 +++++++++++ apache2/t/op/within.t | 51 ++++++++++++++++++++++++ apache2/t/run-tests.pl | 26 ++++++++++-- 8 files changed, 336 insertions(+), 10 deletions(-) diff --git a/apache2/msc_test.c b/apache2/msc_test.c index fa839f11..d5ae8b82 100644 --- a/apache2/msc_test.c +++ b/apache2/msc_test.c @@ -192,18 +192,26 @@ static int test_op(const char *name, const char *param, const unsigned char *inp var->value = apr_pstrmemdup(g_mp, (char *)input, input_len); var->value_len = input_len; var->metadata = msre_resolve_var(modsecurity->msre, var->name); + if (var->metadata == NULL) { + *errmsg = apr_psprintf(g_mp, "Failed to resolve variable for op \"%s\": %s", name, var->name); + return -1; + } /* Initialize the operator parameter */ - rc = metadata->param_init(rule, errmsg); - if (rc < 0) { - *errmsg = apr_psprintf(g_mp, "Failed to init op \"%s\": %s", name, *errmsg); - return rc; + if (metadata->param_init != NULL) { + rc = metadata->param_init(rule, errmsg); + if (rc < 0) { + *errmsg = apr_psprintf(g_mp, "Failed to init op \"%s\": %s", name, *errmsg); + return rc; + } } /* Execute the operator */ - rc = metadata->execute(g_msr, rule, var, errmsg); - if (rc < 0) { - *errmsg = apr_psprintf(g_mp, "Failed to execute op \"%s\": %s", name, *errmsg); + if (metadata->execute != NULL) { + rc = metadata->execute(g_msr, rule, var, errmsg); + if (rc < 0) { + *errmsg = apr_psprintf(g_mp, "Failed to execute op \"%s\": %s", name, *errmsg); + } } return rc; diff --git a/apache2/t/op/beginsWith.t b/apache2/t/op/beginsWith.t index 8b137891..128f25a2 100644 --- a/apache2/t/op/beginsWith.t +++ b/apache2/t/op/beginsWith.t @@ -1 +1,45 @@ +### Empty +{ + type => "op", + name => "beginsWith", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "beginsWith", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "beginsWith", + param => "", + input => "TestCase", + ret => 0, +}, +### General +{ + type => "op", + name => "beginsWith", + param => "abcdef", + input => "abcdef", + ret => 1, +}, +{ + type => "op", + name => "beginsWith", + param => "abcdef", + input => "abcdefghi", + ret => 1, +}, +{ + type => "op", + name => "beginsWith", + param => "abcdef", + input => "abc", + ret => 0, +}, diff --git a/apache2/t/op/contains.t b/apache2/t/op/contains.t index 8b137891..e79f102b 100644 --- a/apache2/t/op/contains.t +++ b/apache2/t/op/contains.t @@ -1 +1,52 @@ +### Empty +{ + type => "op", + name => "contains", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "contains", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "contains", + param => "", + input => "TestCase", + ret => 0, +}, +### General +{ + type => "op", + name => "contains", + param => "abc", + input => "abcdefghi", + ret => 1, +}, +{ + type => "op", + name => "contains", + param => "def", + input => "abcdefghi", + ret => 1, +}, +{ + type => "op", + name => "contains", + param => "ghi", + input => "abcdefghi", + ret => 1, +}, +{ + type => "op", + name => "contains", + param => "ghij", + input => "abcdefghi", + ret => 0, +}, diff --git a/apache2/t/op/containsWord.t b/apache2/t/op/containsWord.t index 8b137891..b4b36624 100644 --- a/apache2/t/op/containsWord.t +++ b/apache2/t/op/containsWord.t @@ -1 +1,80 @@ +### Empty +{ + type => "op", + name => "containsWord", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "containsWord", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "containsWord", + param => "", + input => "TestCase", + ret => 0, +}, +### General +{ + type => "op", + name => "containsWord", + param => "abc", + input => "abcdefghi", + ret => 0, +}, +{ + type => "op", + name => "containsWord", + param => "def", + input => "abcdefghi", + ret => 0, +}, +{ + type => "op", + name => "containsWord", + param => "ghi", + input => "abcdefghi", + ret => 0, +}, +{ + type => "op", + name => "containsWord", + param => "abc", + input => "abc def ghi", + ret => 1, +}, +{ + type => "op", + name => "containsWord", + param => "def", + input => "abc def ghi", + ret => 1, +}, +{ + type => "op", + name => "containsWord", + param => "ghi", + input => "abc def ghi", + ret => 1, +}, +{ + type => "op", + name => "containsWord", + param => "abc", + input => "abc\0def ghi", + ret => 1, +}, +{ + type => "op", + name => "containsWord", + param => "def", + input => "abc\0def ghi", + ret => 1, +}, diff --git a/apache2/t/op/endsWith.t b/apache2/t/op/endsWith.t index 8b137891..8728c752 100644 --- a/apache2/t/op/endsWith.t +++ b/apache2/t/op/endsWith.t @@ -1 +1,52 @@ +### Empty +{ + type => "op", + name => "endsWith", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "endsWith", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "endsWith", + param => "", + input => "TestCase", + ret => 0, +}, +### General +{ + type => "op", + name => "endsWith", + param => "abc", + input => "abcdefghi", + ret => 0, +}, +{ + type => "op", + name => "endsWith", + param => "def", + input => "abcdefghi", + ret => 0, +}, +{ + type => "op", + name => "endsWith", + param => "ghi", + input => "abcdefghi", + ret => 1, +}, +{ + type => "op", + name => "endsWith", + param => "ghi", + input => "abcdef\0ghi", + ret => 1, +}, diff --git a/apache2/t/op/noMatch.t b/apache2/t/op/noMatch.t index 8b137891..8775dd59 100644 --- a/apache2/t/op/noMatch.t +++ b/apache2/t/op/noMatch.t @@ -1 +1,23 @@ +### Empty +{ + type => "op", + name => "noMatch", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "noMatch", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "noMatch", + param => "", + input => "TestCase", + ret => 0, +}, diff --git a/apache2/t/op/within.t b/apache2/t/op/within.t index 8b137891..c383d473 100644 --- a/apache2/t/op/within.t +++ b/apache2/t/op/within.t @@ -1 +1,52 @@ +### Empty +{ + type => "op", + name => "within", + param => "", + input => "", + ret => 0, +}, +{ + type => "op", + name => "within", + param => "TestCase", + input => "", + ret => 0, +}, +{ + type => "op", + name => "within", + param => "", + input => "TestCase", + ret => 0, +}, +### General +{ + type => "op", + name => "within", + param => "abcdefghi", + input => "abc", + ret => 1, +}, +{ + type => "op", + name => "within", + param => "abcdefghi", + input => "def", + ret => 1, +}, +{ + type => "op", + name => "within", + param => "abcdefghi", + input => "ghi", + ret => 1, +}, +{ + type => "op", + name => "within", + param => "abcdefghi", + input => "ghij", + ret => 0, +}, diff --git a/apache2/t/run-tests.pl b/apache2/t/run-tests.pl index cb0a6cd5..9f0be5f3 100755 --- a/apache2/t/run-tests.pl +++ b/apache2/t/run-tests.pl @@ -8,6 +8,7 @@ # Nth in file: run-tests.pl file N # use strict; +use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG); use File::Basename qw(basename dirname); my @TYPES = qw(tfn op); @@ -45,6 +46,8 @@ sub runfile { my @data = (); my $edata; my @C = (); + my @test = (); + my $teststr; my $n = 0; my $pass = 0; @@ -81,13 +84,30 @@ sub runfile { quit(1, "Unknown type \"$t{type}\" - should be one of: " . join(",",@TYPES)); } - open(TEST, "|-", $TEST, $t{type}, $t{name}, $param, (exists($t{ret}) ? ($t{ret}) : ())) or quit(1, "Failed to execute test \"$cfg\": $!"); + @test = ($t{type}, $t{name}, $param, (exists($t{ret}) ? ($t{ret}) : ())); + $teststr = "$TEST " . join(" ", map { "\"$_\"" } @test); + open(TEST, "|-", $TEST, @test) or quit(1, "Failed to execute test: $teststr\": $!"); print TEST "$in"; close TEST; - $rc = $?; - $pass += $rc ? 0 : 1; + if ( WIFEXITED($rc) ) { + $rc = WEXITSTATUS($rc); + } + elsif( WIFSIGNALED($rc) ) { + msg("Test exited with signal " . WTERMSIG($rc) . "."); + msg("Executed: $teststr"); + $rc = -1; + } + else { + msg("Test exited with unknown error."); + $rc = -1; + } + + if ($rc == 0) { + $pass++; + } + msg(sprintf("%s) %s \"%s\": %s", $id, $t{type}, $t{name}, ($rc ? "failed" : "passed"))); }