From e30335325c2335a0b3d06cd9f2822cde71d495c3 Mon Sep 17 00:00:00 2001 From: Stephan Bosch <stephan@rename-it.nl> Date: Sun, 29 Jun 2008 17:58:11 +0200 Subject: [PATCH] Introduced trace macro for runtime tracing and improved testsuite implementation. --- src/lib-sieve/cmd-discard.c | 2 +- src/lib-sieve/cmd-keep.c | 2 +- src/lib-sieve/cmd-redirect.c | 2 +- src/lib-sieve/plugins/body/tst-body.c | 2 +- src/lib-sieve/plugins/imapflags/cmd-addflag.c | 2 +- .../plugins/imapflags/cmd-removeflag.c | 2 +- src/lib-sieve/plugins/imapflags/cmd-setflag.c | 2 +- src/lib-sieve/plugins/imapflags/tst-hasflag.c | 2 +- src/lib-sieve/plugins/include/cmd-include.c | 4 +- src/lib-sieve/plugins/include/cmd-return.c | 2 + src/lib-sieve/plugins/vacation/cmd-vacation.c | 4 +- src/lib-sieve/plugins/variables/cmd-set.c | 2 +- src/lib-sieve/plugins/variables/tst-string.c | 2 +- src/lib-sieve/sieve-code.c | 23 ++++--- src/lib-sieve/sieve-interpreter.c | 14 +++- src/lib-sieve/sieve-interpreter.h | 11 ++++ src/lib-sieve/tst-address.c | 2 +- src/lib-sieve/tst-exists.c | 2 +- src/lib-sieve/tst-header.c | 6 +- src/lib-sieve/tst-size.c | 2 +- src/testsuite/cmd-test-fail.c | 3 +- src/testsuite/cmd-test-set.c | 2 +- src/testsuite/cmd-test.c | 32 ++++++++- src/testsuite/ext-testsuite.c | 8 ++- .../tests/match-types/matches.svtest | 48 +++++++------- src/testsuite/testsuite-common.c | 65 +++++++++++++++++++ src/testsuite/testsuite-common.h | 10 +++ src/testsuite/testsuite.c | 4 ++ 28 files changed, 203 insertions(+), 59 deletions(-) diff --git a/src/lib-sieve/cmd-discard.c b/src/lib-sieve/cmd-discard.c index 901a3bb89..feff4843a 100644 --- a/src/lib-sieve/cmd-discard.c +++ b/src/lib-sieve/cmd-discard.c @@ -84,7 +84,7 @@ static bool cmd_discard_operation_execute const struct sieve_runtime_env *renv ATTR_UNUSED, sieve_size_t *address ATTR_UNUSED) { - printf(">> DISCARD\n"); + sieve_runtime_trace(renv, "DISCARD action"); return ( sieve_result_add_action(renv, &act_discard, NULL, NULL) >= 0 ); } diff --git a/src/lib-sieve/cmd-keep.c b/src/lib-sieve/cmd-keep.c index 982e49e34..c2ec8a587 100644 --- a/src/lib-sieve/cmd-keep.c +++ b/src/lib-sieve/cmd-keep.c @@ -68,7 +68,7 @@ static bool cmd_keep_operation_execute struct sieve_side_effects_list *slist = NULL; int ret = 0; - printf(">> KEEP\n"); + sieve_runtime_trace(renv, "KEEP action"); if ( !sieve_interpreter_handle_optional_operands(renv, address, &slist) ) return FALSE; diff --git a/src/lib-sieve/cmd-redirect.c b/src/lib-sieve/cmd-redirect.c index f8f9be884..e3e256fcf 100644 --- a/src/lib-sieve/cmd-redirect.c +++ b/src/lib-sieve/cmd-redirect.c @@ -170,7 +170,7 @@ static bool cmd_redirect_operation_execute return FALSE; } - printf(">> REDIRECT \"%s\"\n", str_c(redirect)); + sieve_runtime_trace(renv, "REDIRECT action (\"%s\")", str_c(redirect)); /* Add redirect action to the result */ pool = sieve_result_pool(renv->result); diff --git a/src/lib-sieve/plugins/body/tst-body.c b/src/lib-sieve/plugins/body/tst-body.c index 90c2a2628..fac486e90 100644 --- a/src/lib-sieve/plugins/body/tst-body.c +++ b/src/lib-sieve/plugins/body/tst-body.c @@ -312,7 +312,7 @@ static bool ext_body_operation_execute struct ext_body_part *body_parts; bool matched; - printf("?? BODY\n"); + sieve_runtime_trace(renv, "BODY action"); /* Handle any optional arguments */ if ( sieve_operand_optional_present(renv->sbin, address) ) { diff --git a/src/lib-sieve/plugins/imapflags/cmd-addflag.c b/src/lib-sieve/plugins/imapflags/cmd-addflag.c index b57b2f308..8546a3ee0 100644 --- a/src/lib-sieve/plugins/imapflags/cmd-addflag.c +++ b/src/lib-sieve/plugins/imapflags/cmd-addflag.c @@ -74,7 +74,7 @@ static bool cmd_addflag_operation_execute struct sieve_variable_storage *storage; unsigned int var_index; - printf("ADDFLAG\n"); + sieve_runtime_trace(renv, "ADDFLAG command"); t_push(); diff --git a/src/lib-sieve/plugins/imapflags/cmd-removeflag.c b/src/lib-sieve/plugins/imapflags/cmd-removeflag.c index ffb363944..099021456 100644 --- a/src/lib-sieve/plugins/imapflags/cmd-removeflag.c +++ b/src/lib-sieve/plugins/imapflags/cmd-removeflag.c @@ -75,7 +75,7 @@ static bool cmd_removeflag_operation_execute struct sieve_variable_storage *storage; unsigned int var_index; - printf("REMOVEFLAG\n"); + sieve_runtime_trace(renv, "REMOVEFLAG action"); t_push(); diff --git a/src/lib-sieve/plugins/imapflags/cmd-setflag.c b/src/lib-sieve/plugins/imapflags/cmd-setflag.c index b035d59c8..4ee5cf918 100644 --- a/src/lib-sieve/plugins/imapflags/cmd-setflag.c +++ b/src/lib-sieve/plugins/imapflags/cmd-setflag.c @@ -73,7 +73,7 @@ static bool cmd_setflag_operation_execute struct sieve_variable_storage *storage; unsigned int var_index; - printf("SETFLAG\n"); + sieve_runtime_trace(renv, "SETFLAG action"); t_push(); diff --git a/src/lib-sieve/plugins/imapflags/tst-hasflag.c b/src/lib-sieve/plugins/imapflags/tst-hasflag.c index 6f2263a2d..937042495 100644 --- a/src/lib-sieve/plugins/imapflags/tst-hasflag.c +++ b/src/lib-sieve/plugins/imapflags/tst-hasflag.c @@ -166,7 +166,7 @@ static bool tst_hasflag_operation_execute const char *flag; bool matched; - printf("?? HASFLAG\n"); + sieve_runtime_trace(renv, "HASFLAG test"); /* Handle any optional arguments */ if ( sieve_operand_optional_present(renv->sbin, address) ) { diff --git a/src/lib-sieve/plugins/include/cmd-include.c b/src/lib-sieve/plugins/include/cmd-include.c index ea181eac9..eafcf22a6 100644 --- a/src/lib-sieve/plugins/include/cmd-include.c +++ b/src/lib-sieve/plugins/include/cmd-include.c @@ -254,10 +254,12 @@ static bool opc_include_execute const struct sieve_runtime_env *renv, sieve_size_t *address) { int block; - + if ( !sieve_binary_read_offset(renv->sbin, address, &block) ) return FALSE; + sieve_runtime_trace(renv, "INCLUDE command (BLOCK: %d)", block); + return ext_include_execute_include(renv, (unsigned int) block); } diff --git a/src/lib-sieve/plugins/include/cmd-return.c b/src/lib-sieve/plugins/include/cmd-return.c index ea7db1a70..2df76ca03 100644 --- a/src/lib-sieve/plugins/include/cmd-return.c +++ b/src/lib-sieve/plugins/include/cmd-return.c @@ -64,6 +64,8 @@ static bool opc_return_execute const struct sieve_runtime_env *renv, sieve_size_t *address ATTR_UNUSED) { + sieve_runtime_trace(renv, "RETURN command"); + ext_include_execute_return(renv); return TRUE; diff --git a/src/lib-sieve/plugins/vacation/cmd-vacation.c b/src/lib-sieve/plugins/vacation/cmd-vacation.c index 0003d8479..2255f13cf 100644 --- a/src/lib-sieve/plugins/vacation/cmd-vacation.c +++ b/src/lib-sieve/plugins/vacation/cmd-vacation.c @@ -371,6 +371,8 @@ static bool ext_vacation_operation_execute struct sieve_coded_stringlist *addresses = NULL; string_t *reason, *subject = NULL, *from = NULL, *handle = NULL; + sieve_runtime_trace(renv, "VACATION action"); + if ( sieve_operand_optional_present(renv->sbin, address) ) { while ( opt_code != 0 ) { if ( !sieve_operand_optional_read(renv->sbin, address, &opt_code) ) @@ -412,8 +414,6 @@ static bool ext_vacation_operation_execute if ( !sieve_opr_string_read(renv, address, &reason) ) return FALSE; - printf(">> VACATION \"%s\"\n", str_c(reason)); - /* Add vacation action to the result */ pool = sieve_result_pool(renv->result); act = p_new(pool, struct act_vacation_context, 1); diff --git a/src/lib-sieve/plugins/variables/cmd-set.c b/src/lib-sieve/plugins/variables/cmd-set.c index bec04cd76..99ded9b36 100644 --- a/src/lib-sieve/plugins/variables/cmd-set.c +++ b/src/lib-sieve/plugins/variables/cmd-set.c @@ -351,7 +351,7 @@ static bool cmd_set_operation_execute unsigned int var_index, mdfs, i; string_t *value; - printf(">> SET\n"); + sieve_runtime_trace(renv, "SET action"); /* Read the variable */ if ( !sieve_variable_operand_read diff --git a/src/lib-sieve/plugins/variables/tst-string.c b/src/lib-sieve/plugins/variables/tst-string.c index e14d9e282..97617f309 100644 --- a/src/lib-sieve/plugins/variables/tst-string.c +++ b/src/lib-sieve/plugins/variables/tst-string.c @@ -171,7 +171,7 @@ static bool tst_string_operation_execute string_t *src_item; bool matched; - printf("?? STRING\n"); + sieve_runtime_trace(renv, "STRING test"); /* Handle any optional arguments */ if ( sieve_operand_optional_present(renv->sbin, address) ) { diff --git a/src/lib-sieve/sieve-code.c b/src/lib-sieve/sieve-code.c index 22e1c90c7..d76443266 100644 --- a/src/lib-sieve/sieve-code.c +++ b/src/lib-sieve/sieve-code.c @@ -795,7 +795,8 @@ static bool opc_jmp_execute (const struct sieve_operation *op ATTR_UNUSED, const struct sieve_runtime_env *renv, sieve_size_t *address ATTR_UNUSED) { - printf("JMP\n"); + sieve_runtime_trace(renv, "JMP"); + if ( !sieve_interpreter_program_jump(renv->interp, TRUE) ) return FALSE; @@ -806,11 +807,12 @@ static bool opc_jmptrue_execute (const struct sieve_operation *op ATTR_UNUSED, const struct sieve_runtime_env *renv, sieve_size_t *address ATTR_UNUSED) { - if ( !sieve_interpreter_program_jump(renv->interp, - sieve_interpreter_get_test_result(renv->interp)) ) + bool result = sieve_interpreter_get_test_result(renv->interp); + + sieve_runtime_trace(renv, "JMPTRUE (%s)", result ? "true" : "false"); + + if ( !sieve_interpreter_program_jump(renv->interp, result) ) return FALSE; - - printf("JMPTRUE\n"); return TRUE; } @@ -819,11 +821,12 @@ static bool opc_jmpfalse_execute (const struct sieve_operation *op ATTR_UNUSED, const struct sieve_runtime_env *renv, sieve_size_t *address ATTR_UNUSED) { - if ( !sieve_interpreter_program_jump(renv->interp, - !sieve_interpreter_get_test_result(renv->interp)) ) - return FALSE; - - printf("JMPFALSE\n"); + bool result = sieve_interpreter_get_test_result(renv->interp); + + sieve_runtime_trace(renv, "JMPFALSE (%s)", result ? "true" : "false" ); + if ( !sieve_interpreter_program_jump(renv->interp, !result) ) + return FALSE; + return TRUE; } diff --git a/src/lib-sieve/sieve-interpreter.c b/src/lib-sieve/sieve-interpreter.c index 81be1b436..3b4accb5d 100644 --- a/src/lib-sieve/sieve-interpreter.c +++ b/src/lib-sieve/sieve-interpreter.c @@ -236,6 +236,19 @@ void sieve_runtime_log va_end(args); } +void _sieve_runtime_trace + (const struct sieve_runtime_env *runenv, const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + T_BEGIN { + printf("%08x: %s\n", runenv->interp->current_op_addr, + t_strdup_vprintf(fmt, args)); + } T_END; + va_end(args); +} + /* Extension support */ void sieve_interpreter_extension_set_context @@ -384,7 +397,6 @@ int sieve_interpreter_continue while ( ret >= 0 && !interp->interrupted && interp->pc < sieve_binary_get_code_size(interp->runenv.sbin) ) { - printf("%08x: ", interp->pc); if ( !sieve_interpreter_execute_operation(interp) ) { printf("Execution aborted.\n"); diff --git a/src/lib-sieve/sieve-interpreter.h b/src/lib-sieve/sieve-interpreter.h index c33020022..15f53e8c5 100644 --- a/src/lib-sieve/sieve-interpreter.h +++ b/src/lib-sieve/sieve-interpreter.h @@ -75,6 +75,17 @@ void sieve_runtime_warning void sieve_runtime_log (const struct sieve_runtime_env *runenv, const char *fmt, ...) ATTR_FORMAT(2, 3); + +void _sieve_runtime_trace + (const struct sieve_runtime_env *runenv, const char *fmt, ...) + ATTR_FORMAT(2, 3); + +#ifdef SIEVE_TRACE +# define sieve_runtime_trace(runenv, ...) \ + _sieve_runtime_trace(runenv, __VA_ARGS__) +#else +# define sieve_runtime_trace(runenv, ...) +#endif /* Extension support */ diff --git a/src/lib-sieve/tst-address.c b/src/lib-sieve/tst-address.c index cef90e8c3..245974d4b 100644 --- a/src/lib-sieve/tst-address.c +++ b/src/lib-sieve/tst-address.c @@ -145,7 +145,7 @@ static bool tst_address_operation_execute string_t *hdr_item; bool matched; - printf("?? ADDRESS\n"); + sieve_runtime_trace(renv, "ADDRESS test"); if ( !sieve_addrmatch_default_get_optionals (renv, address, &addrp, &mtch, &cmp) ) diff --git a/src/lib-sieve/tst-exists.c b/src/lib-sieve/tst-exists.c index 6c44cb6f1..38af7b877 100644 --- a/src/lib-sieve/tst-exists.c +++ b/src/lib-sieve/tst-exists.c @@ -100,7 +100,7 @@ static bool tst_exists_operation_execute string_t *hdr_item; bool matched; - printf("?? EXISTS\n"); + sieve_runtime_trace(renv, "EXISTS test"); t_push(); diff --git a/src/lib-sieve/tst-header.c b/src/lib-sieve/tst-header.c index 6c05ee4f7..0087f4f65 100644 --- a/src/lib-sieve/tst-header.c +++ b/src/lib-sieve/tst-header.c @@ -168,7 +168,7 @@ static bool tst_header_operation_execute string_t *hdr_item; bool matched; - printf("?? HEADER\n"); + sieve_runtime_trace(renv, "HEADER test"); /* Handle any optional arguments */ if ( sieve_operand_optional_present(renv->sbin, address) ) { @@ -213,15 +213,11 @@ static bool tst_header_operation_execute while ( !matched && (result=sieve_coded_stringlist_next_item(hdr_list, &hdr_item)) && hdr_item != NULL ) { const char *const *headers; - - printf("HEADER: %s\n", str_c(hdr_item)); - if ( mail_get_headers_utf8(renv->msgdata->mail, str_c(hdr_item), &headers) >= 0 ) { int i; for ( i = 0; !matched && headers[i] != NULL; i++ ) { - printf("HEADER: %s %s\n", str_c(hdr_item), headers[i]); if ( sieve_match_value(mctx, headers[i], strlen(headers[i])) ) matched = TRUE; } diff --git a/src/lib-sieve/tst-size.c b/src/lib-sieve/tst-size.c index f95d5135e..97da0b1c6 100644 --- a/src/lib-sieve/tst-size.c +++ b/src/lib-sieve/tst-size.c @@ -221,7 +221,7 @@ static bool tst_size_operation_execute { sieve_size_t mail_size, limit; - printf("%s\n", op->mnemonic); + sieve_runtime_trace(renv, "%s test", op->mnemonic); if ( !sieve_opr_number_read(renv, address, &limit) ) return FALSE; diff --git a/src/testsuite/cmd-test-fail.c b/src/testsuite/cmd-test-fail.c index 13a775356..6203afbd2 100644 --- a/src/testsuite/cmd-test-fail.c +++ b/src/testsuite/cmd-test-fail.c @@ -135,7 +135,8 @@ static bool cmd_test_fail_operation_execute return FALSE; } - printf("TEST FAILED: %s\n", str_c(reason)); + sieve_runtime_trace(renv, "TEST FAIL"); + testsuite_test_fail(reason); t_pop(); diff --git a/src/testsuite/cmd-test-set.c b/src/testsuite/cmd-test-set.c index 1db6aacb6..6942bf44f 100644 --- a/src/testsuite/cmd-test-set.c +++ b/src/testsuite/cmd-test-set.c @@ -146,7 +146,7 @@ static bool cmd_test_set_operation_execute return FALSE; } - printf(">> TEST SET %s = \"%s\"\n", + sieve_runtime_trace(renv, "TEST SET command (%s = \"%s\")", testsuite_object_member_name(object, member_id), str_c(value)); if ( object->set_member == NULL ) { diff --git a/src/testsuite/cmd-test.c b/src/testsuite/cmd-test.c index eb41afb54..85b884b9b 100644 --- a/src/testsuite/cmd-test.c +++ b/src/testsuite/cmd-test.c @@ -2,6 +2,7 @@ #include "sieve-commands-private.h" #include "sieve-validator.h" #include "sieve-generator.h" +#include "sieve-interpreter.h" #include "sieve-code.h" #include "sieve-binary.h" #include "sieve-dump.h" @@ -16,6 +17,9 @@ static bool cmd_test_operation_dump static bool cmd_test_operation_execute (const struct sieve_operation *op, const struct sieve_runtime_env *renv, sieve_size_t *address); +static bool cmd_test_finish_operation_execute + (const struct sieve_operation *op, + const struct sieve_runtime_env *renv, sieve_size_t *address); static bool cmd_test_validate (struct sieve_validator *validator, struct sieve_command_context *cmd); @@ -47,6 +51,14 @@ const struct sieve_operation test_operation = { cmd_test_operation_execute }; +const struct sieve_operation test_finish_operation = { + "TEST-FINISH", + &testsuite_extension, + TESTSUITE_OPERATION_TEST_FINISH, + NULL, + cmd_test_finish_operation_execute +}; + /* Validation */ static bool cmd_test_validate @@ -98,6 +110,9 @@ static bool cmd_test_generate /* Test body */ sieve_generate_block(gentr, ctx->ast_node); + sieve_generator_emit_operation_ext(gentr, &test_finish_operation, + ext_testsuite_my_id); + /* Resolve exit jumps to this point */ sieve_jumplist_resolve(genctx->exit_jumps); @@ -135,14 +150,29 @@ static bool cmd_test_operation_execute t_pop(); return FALSE; } + + testsuite_test_start(test_name); - printf("TEST: %s\n", str_c(test_name)); + sieve_runtime_trace(renv, "TEST \"%s\"", str_c(test_name)); t_pop(); return TRUE; } +static bool cmd_test_finish_operation_execute +(const struct sieve_operation *op ATTR_UNUSED, + const struct sieve_runtime_env *renv ATTR_UNUSED, + sieve_size_t *address ATTR_UNUSED) +{ + sieve_runtime_trace(renv, "TEST FINISHED"); + + testsuite_test_succeed(NULL); + + return TRUE; +} + + diff --git a/src/testsuite/ext-testsuite.c b/src/testsuite/ext-testsuite.c index 514755cf8..7bccc2a33 100644 --- a/src/testsuite/ext-testsuite.c +++ b/src/testsuite/ext-testsuite.c @@ -41,8 +41,12 @@ extern const struct sieve_command cmd_test_set; /* Operations */ -const struct sieve_operation *testsuite_operations[] = - { &test_operation, &test_fail_operation, &test_set_operation }; +const struct sieve_operation *testsuite_operations[] = { + &test_operation, + &test_finish_operation, + &test_fail_operation, + &test_set_operation +}; /* Operands */ diff --git a/src/testsuite/tests/match-types/matches.svtest b/src/testsuite/tests/match-types/matches.svtest index ce0643317..ea9bcb2b8 100644 --- a/src/testsuite/tests/match-types/matches.svtest +++ b/src/testsuite/tests/match-types/matches.svtest @@ -16,136 +16,140 @@ Het werkt! . ; +# Matching tests + test "MATCH-A" { if not address :matches "from" "*@d*ksn*ers.com" { - test_fail "A case should have matched"; + test_fail "should have matched"; } } test "MATCH-B" { if not address :matches "from" "stephan+sieve@drunksnipers.*" { - test_fail "B case should have matched"; + test_fail "should have matched"; } } test "MATCH-C" { if not address :matches "from" "*+sieve@drunksnipers.com" { - test_fail "C case should have matched"; + test_fail "should have matched"; } } test "MATCH-D" { if not address :matches "from" "stephan+sieve?drunksnipers.com" { - test_fail "D case should have matched"; + test_fail "should have matched"; } } test "MATCH-E" { if not address :matches "from" "?tephan+sieve@drunksnipers.com" { - test_fail "E case should have matched"; + test_fail "should have matched"; } } test "MATCH-F" { if not address :matches "from" "stephan+sieve@drunksnipers.co?" { - test_fail "F case should have matched"; + test_fail "should have matched"; } } test "MATCH-G" { if not address :matches "from" "?t?phan?sieve?drunksnip?rs.co?" { - test_fail "G case should have matched"; + test_fail "should have matched"; } } test "MATCH-H" { if not header :matches "x-bullshit" "33333\\?\\?\\??" { - test_fail "H case should have matched"; + test_fail "should have matched"; } } test "MATCH-I" { if not header :matches "x-bullshit" "33333*\\?\\??" { - test_fail "I case should have matched"; + test_fail "should have matched"; } } test "MATCH-J" { if not header :matches "X-Bullshit" "*\\?\\?\\?a" { - test_fail "J case should have matched"; + test_fail "should have matched"; } } test "MATCH-K" { if not header :matches "x-bullshit" "*3333\\?\\?\\?a" { - test_fail "K case should have matched"; + test_fail "should have matched"; } } +# Non-matching tests + test "NO-MATCH-A" { if address :matches "from" "*@d*kn*ers.com" { - test_fail "A case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-B" { if address :matches "from" "stepan+sieve@drunksnipers.*" { - test_fail "B case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-C" { if address :matches "from" "*+sieve@drunksnipers.om" { - test_fail "C case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-D" { if address :matches "from" "stephan+sieve?drunksipers.com" { - test_fail "D case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-E" { if address :matches "from" "?tephan+sievedrunksnipers.com" { - test_fail "E case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-F" { if address :matches "from" "sephan+sieve@drunksnipers.co?" { - test_fail "F case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-G" { if address :matches "from" "?t?phan?sieve?dunksnip?rs.co?" { - test_fail "G case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-H" { if header :matches "x-bullshit" "33333\\?\\?\\?" { - test_fail "H case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-I" { if header :matches "x-bullshit" "33333\\?\\?\\?aa" { - test_fail "I case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-J" { if header :matches "x-bullshit" "\\*3333\\?\\?\\?a" { - test_fail "J case should not have matched"; + test_fail "should not have matched"; } } test "NO-MATCH-K" { if header :matches "x-bullshit" "\\f3333\\?\\?\\?a" { - test_fail "K case should not have matched"; + test_fail "should not have matched"; } } diff --git a/src/testsuite/testsuite-common.c b/src/testsuite/testsuite-common.c index fab9752b1..e61a35e3b 100644 --- a/src/testsuite/testsuite-common.c +++ b/src/testsuite/testsuite-common.c @@ -1,4 +1,5 @@ #include "lib.h" +#include "str.h" #include "string.h" #include "ostream.h" #include "hash.h" @@ -25,6 +26,11 @@ struct sieve_message_data testsuite_msgdata; +/* Test context */ + +static string_t *test_name; +unsigned int test_index; + /* * Testsuite message environment */ @@ -156,3 +162,62 @@ bool testsuite_generator_context_initialize(struct sieve_generator *gentr) return TRUE; } + +/* + * Test context + */ + +void testsuite_test_context_init(void) +{ + test_name = str_new(default_pool, 128); + test_index = 0; +} + +void testsuite_test_start(string_t *name) +{ + str_truncate(test_name, 0); + str_append_str(test_name, name); + + test_index++; +} + +void testsuite_test_fail(string_t *reason) +{ + if ( str_len(test_name) == 0 ) { + if ( reason == NULL || str_len(reason) == 0 ) + printf("%d: Test FAILED\n", test_index); + else + printf("%d: Test FAILED: %s\n", test_index, str_c(reason)); + } else { + if ( reason == NULL || str_len(reason) == 0 ) + printf("%d: Test '%s' FAILED\n", test_index, str_c(test_name)); + else + printf("%d: Test '%s' FAILED: %s\n", test_index, + str_c(test_name), str_c(reason)); + } + + str_truncate(test_name, 0); +} + +void testsuite_test_succeed(string_t *reason) +{ + if ( str_len(test_name) == 0 ) { + if ( reason == NULL || str_len(reason) == 0 ) + printf("%d: Test SUCCEEDED\n", test_index); + else + printf("%d: Test SUCCEEDED: %s\n", test_index, str_c(reason)); + } else { + if ( reason == NULL || str_len(reason) == 0 ) + printf("%d: Test '%s' SUCCEEDED\n", test_index, str_c(test_name)); + else + printf("%d: Test '%s' SUCCEEDED: %s\n", test_index, + str_c(test_name), str_c(reason)); + } + str_truncate(test_name, 0); +} + +void testsuite_test_context_deinit(void) +{ + //str_free(test_name); +} + diff --git a/src/testsuite/testsuite-common.h b/src/testsuite/testsuite-common.h index b5bb806e3..2edbea9f2 100644 --- a/src/testsuite/testsuite-common.h +++ b/src/testsuite/testsuite-common.h @@ -38,11 +38,13 @@ bool testsuite_generator_context_initialize(struct sieve_generator *gentr); enum testsuite_operation_code { TESTSUITE_OPERATION_TEST, + TESTSUITE_OPERATION_TEST_FINISH, TESTSUITE_OPERATION_TEST_FAIL, TESTSUITE_OPERATION_TEST_SET }; extern const struct sieve_operation test_operation; +extern const struct sieve_operation test_finish_operation; extern const struct sieve_operation test_fail_operation; extern const struct sieve_operation test_set_operation; @@ -54,4 +56,12 @@ enum testsuite_operand_code { TESTSUITE_OPERAND_OBJECT }; +/* Test context */ + +void testsuite_test_context_init(void); +void testsuite_test_start(string_t *name); +void testsuite_test_fail(string_t *reason); +void testsuite_test_succeed(string_t *reason); +void testsuite_test_context_deinit(void); + #endif /* __TESTSUITE_COMMON_H */ diff --git a/src/testsuite/testsuite.c b/src/testsuite/testsuite.c index 1d9c2e918..51b5e0116 100644 --- a/src/testsuite/testsuite.c +++ b/src/testsuite/testsuite.c @@ -52,10 +52,14 @@ static void testsuite_init(void) i_fatal("Failed to initialize sieve implementation\n"); (void) sieve_extension_register(&testsuite_extension); + + testsuite_test_context_init(); } static void testsuite_deinit(void) { + testsuite_test_context_deinit(); + sieve_deinit(); lib_signals_deinit(); -- GitLab