From bff1bf548f452ade171d91a52f63f1169a252b85 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Fri, 2 Jan 2009 12:35:13 +0100
Subject: [PATCH] Testsuite: split off script and error handler implementations
 into separate modules.

---
 src/testsuite/Makefile.am             |   4 +
 src/testsuite/testsuite-common.c      | 194 +-------------------------
 src/testsuite/testsuite-result.c      |   3 +-
 src/testsuite/tst-test-error.c        |   5 +-
 tests/extensions/enotify/basic.svtest |  13 +-
 5 files changed, 20 insertions(+), 199 deletions(-)

diff --git a/src/testsuite/Makefile.am b/src/testsuite/Makefile.am
index a922b7525..284efb282 100644
--- a/src/testsuite/Makefile.am
+++ b/src/testsuite/Makefile.am
@@ -50,6 +50,8 @@ testsuite_SOURCES = \
 	testsuite-objects.c \
 	testsuite-substitutions.c \
 	testsuite-arguments.c \
+	testsuite-log.c \
+	testsuite-script.c \
 	testsuite-result.c \
 	$(commands) \
 	$(tests) \
@@ -61,5 +63,7 @@ noinst_HEADERS = \
 	testsuite-objects.h \
 	testsuite-substitutions.h \
 	testsuite-arguments.h \
+	testsuite-log.h \
+	testsuite-script.h \
 	testsuite-result.h
 
diff --git a/src/testsuite/testsuite-common.c b/src/testsuite/testsuite-common.c
index fe395ea96..c217c6a9e 100644
--- a/src/testsuite/testsuite-common.c
+++ b/src/testsuite/testsuite-common.c
@@ -11,8 +11,7 @@
 
 #include "mail-raw.h"
 
-#include "sieve.h"
-#include "sieve-error-private.h"
+#include "sieve-common.h"
 #include "sieve-code.h"
 #include "sieve-message.h"
 #include "sieve-commands.h"
@@ -25,6 +24,8 @@
 
 #include "testsuite-common.h"
 #include "testsuite-objects.h"
+#include "testsuite-log.h"
+#include "testsuite-script.h"
 #include "testsuite-result.h"
 
 /*
@@ -41,10 +42,6 @@ static string_t *test_name;
 unsigned int test_index;
 unsigned int test_failures;
 
-/* Tested script context */
-
-static struct sieve_error_handler *test_script_ehandler;
-
 /* 
  * Testsuite message environment 
  */
@@ -288,189 +285,6 @@ int testsuite_testcase_result(void)
 	return 0;
 }
 
-/*
- * Tested script environment
- */ 
-
-/* Special error handler */
-
-struct _testsuite_script_message {
-	const char *location;
-	const char *message;
-};
-
-unsigned int _testsuite_script_error_index = 0;
-
-static pool_t _testsuite_scriptmsg_pool = NULL;
-ARRAY_DEFINE(_testsuite_script_errors, struct _testsuite_script_message);
-
-struct sieve_binary *_testsuite_compiled_script;
-
-static void _testsuite_script_verror
-(struct sieve_error_handler *ehandler ATTR_UNUSED, const char *location,
-    const char *fmt, va_list args)
-{
-	pool_t pool = _testsuite_scriptmsg_pool;
-	struct _testsuite_script_message msg;
-
-	msg.location = p_strdup(pool, location);
-	msg.message = p_strdup_vprintf(pool, fmt, args);
-
-//	printf("error: %s: %s.\n", location, t_strdup_vprintf(fmt, args));
-	
-	array_append(&_testsuite_script_errors, &msg, 1);	
-}
-
-static struct sieve_error_handler *_testsuite_script_ehandler_create(void)
-{
-	pool_t pool;
-	struct sieve_error_handler *ehandler;
-
-	/* Pool is not strictly necessary, but other handler types will need a pool,
-	 * so this one will have one too.
-	 */
-	pool = pool_alloconly_create
-		("testsuite_script_error_handler", sizeof(struct sieve_error_handler));
-	ehandler = p_new(pool, struct sieve_error_handler, 1);
-	sieve_error_handler_init(ehandler, pool, 0);
-
-	ehandler->verror = _testsuite_script_verror;
-
-	return ehandler;
-}
-
-void testsuite_script_clear_messages(void)
-{
-	if ( _testsuite_scriptmsg_pool != NULL ) {
-		if ( array_count(&_testsuite_script_errors) == 0 )
-			return;
-		pool_unref(&_testsuite_scriptmsg_pool);
-	}
-
-	_testsuite_scriptmsg_pool = pool_alloconly_create
-		("testsuite_script_messages", 8192);
-	
-	p_array_init(&_testsuite_script_errors, _testsuite_scriptmsg_pool, 128);	
-
-	sieve_error_handler_reset(test_script_ehandler);
-}
-
-void testsuite_script_get_error_init(void)
-{
-	_testsuite_script_error_index = 0;
-}
-
-const char *testsuite_script_get_error_next(bool location)
-{
-	const struct _testsuite_script_message *msg;
-
-	if ( _testsuite_script_error_index >= array_count(&_testsuite_script_errors) )
-		return NULL;
-
-	msg = array_idx(&_testsuite_script_errors, _testsuite_script_error_index++);
-
-	if ( location ) 
-		return msg->location;
-
-	return msg->message;		
-}
-
-static void testsuite_script_init(void)
-{
-	test_script_ehandler = _testsuite_script_ehandler_create(); 	
-	sieve_error_handler_accept_infolog(test_script_ehandler, TRUE);
-
-	testsuite_script_clear_messages();
-
-	_testsuite_compiled_script = NULL;
-}
-
-bool testsuite_script_compile(const char *script_path)
-{
-	struct sieve_binary *sbin;
-	const char *sieve_dir;
-
-	testsuite_script_clear_messages();
-
-	/* Initialize environment */
-	sieve_dir = strrchr(script_path, '/');
-	if ( sieve_dir == NULL )
-		sieve_dir= "./";
-	else
-		sieve_dir = t_strdup_until(script_path, sieve_dir+1);
-
-	/* Currently needed for include (FIXME) */
-	env_put(t_strconcat("SIEVE_DIR=", sieve_dir, "included", NULL));
-	env_put(t_strconcat("SIEVE_GLOBAL_DIR=", sieve_dir, "included-global", NULL));
-
-	if ( (sbin = sieve_compile(script_path, test_script_ehandler)) == NULL )
-		return FALSE;
-
-	if ( _testsuite_compiled_script != NULL ) {
-		sieve_close(&_testsuite_compiled_script);
-	}
-
-	_testsuite_compiled_script = sbin;
-
-	return TRUE;
-}
-
-bool testsuite_script_run(const struct sieve_runtime_env *renv)
-{
-	struct sieve_script_env scriptenv;
-	struct sieve_exec_status estatus;
-	struct sieve_result *result;
-	struct sieve_interpreter *interp;
-	int ret;
-
-	if ( _testsuite_compiled_script == NULL ) {
-		sieve_runtime_error(renv, sieve_error_script_location(renv->script,0),
-			"testsuite: no script compiled yet");
-		return FALSE;
-	}
-
-	testsuite_script_clear_messages();
-
-	/* Compose script execution environment */
-	memset(&scriptenv, 0, sizeof(scriptenv));
-	scriptenv.default_mailbox = "INBOX";
-	scriptenv.namespaces = NULL;
-	scriptenv.username = "user";
-	scriptenv.hostname = "host.example.com";
-	scriptenv.postmaster_address = "postmaster@example.com";
-	scriptenv.smtp_open = NULL;
-	scriptenv.smtp_close = NULL;
-	scriptenv.duplicate_mark = NULL;
-	scriptenv.duplicate_check = NULL;
-	
-	result = sieve_result_create(test_script_ehandler);
-
-	/* Execute the script */
-	interp=sieve_interpreter_create(_testsuite_compiled_script, test_script_ehandler, NULL);
-	
-	if ( interp == NULL )
-		return SIEVE_EXEC_BIN_CORRUPT;
-		
-	ret = sieve_interpreter_run(interp, renv->msgdata, &scriptenv, &result, &estatus);
-
-	sieve_interpreter_free(&interp);
-
-	testsuite_result_assign(result);
-	
-	return ( ret > 0 );
-}
-
-static void testsuite_script_deinit(void)
-{
-	sieve_error_handler_unref(&test_script_ehandler);
-
-	if ( _testsuite_compiled_script != NULL ) {
-		sieve_close(&_testsuite_compiled_script);
-	}
-
-	pool_unref(&_testsuite_scriptmsg_pool);
-	//str_free(test_script_error_buf);
-}
 
 /*
  * Main testsuite init/deinit
@@ -479,6 +293,7 @@ static void testsuite_script_deinit(void)
 void testsuite_init(void)
 {
 	testsuite_test_context_init();
+	testsuite_log_init();
 	testsuite_script_init();
 	testsuite_result_init();
 }
@@ -487,6 +302,7 @@ void testsuite_deinit(void)
 {
 	testsuite_result_deinit();
 	testsuite_script_deinit();
+	testsuite_log_deinit();
 	testsuite_test_context_deinit();
 }
 
diff --git a/src/testsuite/testsuite-result.c b/src/testsuite/testsuite-result.c
index bfa95fbb2..b9873b5eb 100644
--- a/src/testsuite/testsuite-result.c
+++ b/src/testsuite/testsuite-result.c
@@ -8,6 +8,7 @@
 #include "sieve-result.h"
 
 #include "testsuite-common.h"
+#include "testsuite-log.h"
 #include "testsuite-result.h"
 
 static struct sieve_result *_testsuite_result;
@@ -53,7 +54,7 @@ bool testsuite_result_execute(const struct sieve_runtime_env *renv)
 		return FALSE;
 	}
 
-	testsuite_script_clear_messages();
+	testsuite_log_clear_messages();
 
 	/* Compose script execution environment */
 	memset(&scriptenv, 0, sizeof(scriptenv));
diff --git a/src/testsuite/tst-test-error.c b/src/testsuite/tst-test-error.c
index 39eda3429..4611f9549 100644
--- a/src/testsuite/tst-test-error.c
+++ b/src/testsuite/tst-test-error.c
@@ -16,6 +16,7 @@
 #include "sieve-match.h"
 
 #include "testsuite-common.h"
+#include "testsuite-log.h"
 
 /*
  * Test_error command
@@ -263,7 +264,7 @@ static int tst_test_error_operation_execute
 	
 	sieve_runtime_trace(renv, "TEST_ERROR test (index: %d)", index);
 
-	testsuite_script_get_error_init();
+	testsuite_log_get_error_init();
 
 	/* Initialize match */
 	mctx = sieve_match_begin(renv->interp, mtch, cmp, NULL, key_list);
@@ -274,7 +275,7 @@ static int tst_test_error_operation_execute
 	cur_index = 1;
 	ret = 0;
 	while ( result && !matched &&
-		(error=testsuite_script_get_error_next(FALSE)) != NULL ) {
+		(error=testsuite_log_get_error_next(FALSE)) != NULL ) {
 		
 		if ( index == 0 || index == cur_index ) {
 			if ( (ret=sieve_match_value(mctx, error, strlen(error))) < 0 ) {
diff --git a/tests/extensions/enotify/basic.svtest b/tests/extensions/enotify/basic.svtest
index 54ad5af50..052be61b7 100644
--- a/tests/extensions/enotify/basic.svtest
+++ b/tests/extensions/enotify/basic.svtest
@@ -5,12 +5,11 @@ test "Execute" {
 	/* Test to catch runtime segfaults */
 	if valid_notify_method 
 		"mailto:stephan@example.com" {
-		stop;
-	}
 
-	/* Test to catch runtime segfaults */
-	notify 
-		:message "This is probably very important"
-		:importance "1" 
-		"mailto:stephan@example.com%2cstephan@rename-it.nl?subject=Important%20message%20received";
+		/* Test to catch runtime segfaults */
+		notify 
+			:message "This is probably very important"
+			:importance "1" 
+			"mailto:stephan@example.com%2cstephan@rename-it.nl?subject=Important%20message%20received";
+	}
 }
-- 
GitLab