Made msr_log() work with unit tests.

Now display the message generated by operators for unit tests.
This commit is contained in:
brectanus 2008-02-07 21:43:56 +00:00
parent 46cb2beeba
commit 4b55882c4f
2 changed files with 51 additions and 14 deletions

View File

@ -48,6 +48,29 @@ char *get_apr_error(apr_pool_t *p, apr_status_t rc) {
}
void msr_log(modsec_rec *msr, int level, const char *text, ...) {
va_list ap;
char str1[1024] = "";
char str2[1256] = "";
if ((msr == NULL) || (level > msr->txcfg->debuglog_level)) {
return;
}
if (msr->txcfg->debuglog_fd == NOT_SET_P) {
if (apr_file_open(&msr->txcfg->debuglog_fd, msr->txcfg->debuglog_name, APR_READ|APR_WRITE|APR_CREATE|APR_APPEND|APR_BINARY, APR_OS_DEFAULT, g_mp) != APR_SUCCESS) {
fprintf(stderr, "ERROR: failed to create unit test debug log \"%s\".\n", msr->txcfg->debuglog_name);
msr->txcfg->debuglog_fd = NULL;
}
}
va_start(ap, text);
if (msr->txcfg->debuglog_fd != NULL) {
apr_size_t nbytes_written = 0;
apr_vsnprintf(str1, sizeof(str1), text, ap);
apr_snprintf(str2, sizeof(str2), "[%d] %s\n", level, str1);
apr_file_write_full(msr->txcfg->debuglog_fd, str2, strlen(str2), &nbytes_written);
}
va_end(ap);
}
const char *ap_get_remote_host(conn_rec *conn, void *dir_config, int type, int *str_is_ip) {
@ -148,6 +171,7 @@ static int test_tfn(const char *name, unsigned char *input, apr_size_t input_len
static int test_op(const char *name, const char *param, const unsigned char *input, apr_size_t input_len, char **errmsg)
{
const char *args = apr_psprintf(g_mp, "@%s %s", name, param);
char *conf_fn;
msre_ruleset *ruleset = NULL;
msre_rule *rule = NULL;
msre_var *var = NULL;
@ -156,6 +180,11 @@ static int test_op(const char *name, const char *param, const unsigned char *inp
*errmsg = NULL;
if ( apr_filepath_merge(&conf_fn, NULL, "t/unit-test.conf", APR_FILEPATH_TRUENAME, g_mp) != APR_SUCCESS) {
*errmsg = apr_psprintf(g_mp, "Failed to build a conf filename.");
return -1;
}
/* Register UNIT_TEST variable */
msre_engine_variable_register(modsecurity->msre,
"UNIT_TEST",
@ -180,7 +209,7 @@ static int test_op(const char *name, const char *param, const unsigned char *inp
*errmsg = apr_psprintf(g_mp, "Failed to create ruleset for op \"%s\".", name);
return -1;
}
rule = msre_rule_create(ruleset, RULE_TYPE_NORMAL, "unit-test", 1, "UNIT_TEST", args, "t:none,pass,nolog", errmsg);
rule = msre_rule_create(ruleset, RULE_TYPE_NORMAL, conf_fn, 1, "UNIT_TEST", args, "t:none,pass,nolog", errmsg);
if (rule == NULL) {
*errmsg = apr_psprintf(g_mp, "Failed to create rule for op \"%s\": %s", name, *errmsg);
return -1;
@ -233,9 +262,9 @@ static void init_msr() {
dcfg->resbody_access = 0;
dcfg->of_limit = RESPONSE_BODY_DEFAULT_LIMIT;
dcfg->of_limit_action = RESPONSE_BODY_LIMIT_ACTION_REJECT;
dcfg->debuglog_fd = NULL;
dcfg->debuglog_name = NULL;
dcfg->debuglog_level = 0;
dcfg->debuglog_fd = NOT_SET_P;
dcfg->debuglog_name = "msc-test-debug.log";
dcfg->debuglog_level = 9;
dcfg->cookie_format = 0;
dcfg->argument_separator = '&';
dcfg->rule_inheritance = 0;
@ -266,7 +295,6 @@ static void init_msr() {
dcfg->cache_trans_min = 15;
dcfg->cache_trans_max = 0;
dcfg->request_encoding = NULL;
dcfg->debuglog_level = 0;
g_msr = (modsec_rec *)apr_pcalloc(g_mp, sizeof(modsec_rec));
g_msr->modsecurity = modsecurity;
@ -435,6 +463,8 @@ int main(int argc, const char * const argv[])
exit(1);
}
fprintf(stdout, "%s\n", errmsg ? errmsg : "");
return 0;
}

View File

@ -10,6 +10,8 @@
use strict;
use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG);
use File::Basename qw(basename dirname);
use FileHandle;
use IPC::Open2 qw(open2);
my @TYPES = qw(tfn op);
my $TEST = "./msc_test";
@ -71,6 +73,9 @@ sub runfile {
my %t = %{$t || {}};
my $id = sprintf("%6d", $n);
my $in = $t{input};
my $out;
my $test_in = new FileHandle();
my $test_out = new FileHandle();
my $rc = 0;
my $param;
@ -86,9 +91,11 @@ sub runfile {
@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;
open2($test_out, $test_in, $TEST, @test) or quit(1, "Failed to execute test: $teststr\": $!");
print $test_in "$in";
close $test_in;
$out = join("\\n", split(/\n/, <$test_out>));
close $test_out;
$rc = $?;
if ( WIFEXITED($rc) ) {
@ -108,7 +115,7 @@ sub runfile {
$pass++;
}
msg(sprintf("%s) %s \"%s\": %s", $id, $t{type}, $t{name}, ($rc ? "failed" : "passed")));
msg(sprintf("%s) %s \"%s\": %s%s", $id, $t{type}, $t{name}, ($rc ? "failed" : "passed"), ((defined($out) && $out ne "")? " ($out)" : "")));
}