From 84b5f74b1e3fe2ccb64c4a8b335dafa957ad622c Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sun, 28 Dec 2008 20:00:26 +0100
Subject: [PATCH] Testsuite: added support for executing results.

---
 src/lib-sieve/sieve-result.h            |  7 +-
 src/testsuite/Makefile.am               |  3 +-
 src/testsuite/ext-testsuite.c           |  4 +-
 src/testsuite/testsuite-common.c        |  2 +-
 src/testsuite/testsuite-common.h        |  6 +-
 src/testsuite/testsuite-result.c        | 36 ++++++++++
 src/testsuite/testsuite-result.h        |  2 +
 src/testsuite/tst-test-result-execute.c | 91 +++++++++++++++++++++++++
 tests/extensions/enotify/execute.svtest | 22 +++++-
 9 files changed, 167 insertions(+), 6 deletions(-)
 create mode 100644 src/testsuite/tst-test-result-execute.c

diff --git a/src/lib-sieve/sieve-result.h b/src/lib-sieve/sieve-result.h
index 137ed099e..41d75b121 100644
--- a/src/lib-sieve/sieve-result.h
+++ b/src/lib-sieve/sieve-result.h
@@ -20,10 +20,15 @@ struct sieve_result;
 
 struct sieve_result *sieve_result_create
 	(struct sieve_error_handler *ehandler);
+
 void sieve_result_ref(struct sieve_result *result); 
+
 void sieve_result_unref(struct sieve_result **result); 
+
 pool_t sieve_result_pool(struct sieve_result *result);
-struct sieve_error_handler *sieve_result_get_error_handler(struct sieve_result *result);
+
+struct sieve_error_handler *sieve_result_get_error_handler
+	(struct sieve_result *result);
 
 /*
  * Extension support
diff --git a/src/testsuite/Makefile.am b/src/testsuite/Makefile.am
index 6e1c8e14c..a922b7525 100644
--- a/src/testsuite/Makefile.am
+++ b/src/testsuite/Makefile.am
@@ -42,7 +42,8 @@ tests = \
 	tst-test-script-compile.c \
 	tst-test-script-run.c \
 	tst-test-error.c \
-	tst-test-result.c
+	tst-test-result.c \
+	tst-test-result-execute.c
 
 testsuite_SOURCES = \
 	testsuite-common.c \
diff --git a/src/testsuite/ext-testsuite.c b/src/testsuite/ext-testsuite.c
index 79ce4fa83..47b5f2140 100644
--- a/src/testsuite/ext-testsuite.c
+++ b/src/testsuite/ext-testsuite.c
@@ -59,7 +59,8 @@ const struct sieve_operation *testsuite_operations[] = {
 	&test_script_compile_operation,
 	&test_script_run_operation,
 	&test_error_operation,
-	&test_result_operation
+	&test_result_operation,
+	&test_result_execute_operation
 };
 
 /* 
@@ -110,6 +111,7 @@ static bool ext_testsuite_validator_load(struct sieve_validator *valdtr)
 	sieve_validator_register_command(valdtr, &tst_test_script_run);
 	sieve_validator_register_command(valdtr, &tst_test_error);
 	sieve_validator_register_command(valdtr, &tst_test_result);	
+	sieve_validator_register_command(valdtr, &tst_test_result_execute);	
 
 	sieve_validator_argument_override(valdtr, SAT_VAR_STRING,
 		&testsuite_string_argument);
diff --git a/src/testsuite/testsuite-common.c b/src/testsuite/testsuite-common.c
index 5147dd0bd..fe395ea96 100644
--- a/src/testsuite/testsuite-common.c
+++ b/src/testsuite/testsuite-common.c
@@ -339,7 +339,7 @@ static struct sieve_error_handler *_testsuite_script_ehandler_create(void)
 	return ehandler;
 }
 
-static void testsuite_script_clear_messages(void)
+void testsuite_script_clear_messages(void)
 {
 	if ( _testsuite_scriptmsg_pool != NULL ) {
 		if ( array_count(&_testsuite_script_errors) == 0 )
diff --git a/src/testsuite/testsuite-common.h b/src/testsuite/testsuite-common.h
index 636ee2a37..0ee51adbd 100644
--- a/src/testsuite/testsuite-common.h
+++ b/src/testsuite/testsuite-common.h
@@ -64,6 +64,7 @@ extern const struct sieve_command tst_test_script_compile;
 extern const struct sieve_command tst_test_script_run;
 extern const struct sieve_command tst_test_error;
 extern const struct sieve_command tst_test_result;
+extern const struct sieve_command tst_test_result_execute;
 
 /* 
  * Operations 
@@ -77,7 +78,8 @@ enum testsuite_operation_code {
 	TESTSUITE_OPERATION_TEST_SCRIPT_COMPILE,
 	TESTSUITE_OPERATION_TEST_SCRIPT_RUN,
 	TESTSUITE_OPERATION_TEST_ERROR,
-	TESTSUITE_OPERATION_TEST_RESULT
+	TESTSUITE_OPERATION_TEST_RESULT,
+	TESTSUITE_OPERATION_TEST_RESULT_EXECUTE,
 };
 
 extern const struct sieve_operation test_operation;
@@ -88,6 +90,7 @@ extern const struct sieve_operation test_script_compile_operation;
 extern const struct sieve_operation test_script_run_operation;
 extern const struct sieve_operation test_error_operation;
 extern const struct sieve_operation test_result_operation;
+extern const struct sieve_operation test_result_execute_operation;
 
 /* 
  * Operands 
@@ -119,6 +122,7 @@ int testsuite_testcase_result(void);
 bool testsuite_script_compile(const char *script_path);
 bool testsuite_script_run(const struct sieve_runtime_env *renv);
 
+void testsuite_script_clear_messages(void);
 void testsuite_script_get_error_init(void);
 const char *testsuite_script_get_error_next(bool location);
 
diff --git a/src/testsuite/testsuite-result.c b/src/testsuite/testsuite-result.c
index fc48aa68a..9bdfedee2 100644
--- a/src/testsuite/testsuite-result.c
+++ b/src/testsuite/testsuite-result.c
@@ -2,7 +2,9 @@
  */
 
 #include "sieve-common.h"
+#include "sieve-error.h"
 #include "sieve-actions.h"
+#include "sieve-interpreter.h"
 #include "sieve-result.h"
 
 #include "testsuite-common.h"
@@ -41,3 +43,37 @@ struct sieve_result_iterate_context *testsuite_result_iterate_init(void)
 	return sieve_result_iterate_init(_testsuite_result);
 }
 
+bool testsuite_result_execute(const struct sieve_runtime_env *renv)
+{
+	struct sieve_script_env scriptenv;
+	struct sieve_exec_status estatus;
+	int ret;
+
+	if ( _testsuite_result == NULL ) {
+		sieve_runtime_error(renv, sieve_error_script_location(renv->script,0),
+			"testsuite: no result evaluated 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;
+	
+	/* Execute the result */	
+	ret=sieve_result_execute
+		(_testsuite_result, renv->msgdata, &scriptenv, &estatus);
+	
+	return ( ret > 0 );
+}
+
+
diff --git a/src/testsuite/testsuite-result.h b/src/testsuite/testsuite-result.h
index 404b40e1a..ab21d0f34 100644
--- a/src/testsuite/testsuite-result.h
+++ b/src/testsuite/testsuite-result.h
@@ -11,4 +11,6 @@ void testsuite_result_assign(struct sieve_result *result);
 
 struct sieve_result_iterate_context *testsuite_result_iterate_init(void);
 
+bool testsuite_result_execute(const struct sieve_runtime_env *renv);
+
 #endif /* __TESTSUITE_RESULT_H */
diff --git a/src/testsuite/tst-test-result-execute.c b/src/testsuite/tst-test-result-execute.c
new file mode 100644
index 000000000..1ccd9b238
--- /dev/null
+++ b/src/testsuite/tst-test-result-execute.c
@@ -0,0 +1,91 @@
+/* Copyright (c) 2002-2008 Dovecot Sieve authors, see the included COPYING file
+ */
+
+#include "sieve-common.h"
+#include "sieve-script.h"
+#include "sieve-commands.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"
+#include "sieve.h"
+
+#include "testsuite-common.h"
+#include "testsuite-result.h"
+
+/*
+ * Test_result_execute command
+ *
+ * Syntax:   
+ *   test_result_execute
+ */
+
+static bool tst_test_result_execute_generate
+	(const struct sieve_codegen_env *cgenv, struct sieve_command_context *ctx);
+
+const struct sieve_command tst_test_result_execute = { 
+	"test_result_execute", 
+	SCT_TEST, 
+	0, 0, FALSE, FALSE,
+	NULL, NULL, NULL,
+	tst_test_result_execute_generate, 
+	NULL 
+};
+
+/* 
+ * Operation 
+ */
+
+static int tst_test_result_execute_operation_execute
+	(const struct sieve_operation *op, 
+		const struct sieve_runtime_env *renv, sieve_size_t *address);
+
+const struct sieve_operation test_result_execute_operation = { 
+	"test_result_execute",
+	&testsuite_extension, 
+	TESTSUITE_OPERATION_TEST_RESULT_EXECUTE,
+	NULL, 
+	tst_test_result_execute_operation_execute 
+};
+
+/* 
+ * Code generation 
+ */
+
+static bool tst_test_result_execute_generate
+(const struct sieve_codegen_env *cgenv, 
+	struct sieve_command_context *tst ATTR_UNUSED)
+{
+	sieve_operation_emit_code(cgenv->sbin, &test_result_execute_operation);
+
+	return TRUE;
+}
+
+/*
+ * Intepretation
+ */
+
+static int tst_test_result_execute_operation_execute
+(const struct sieve_operation *op ATTR_UNUSED,
+	const struct sieve_runtime_env *renv, 
+	sieve_size_t *address ATTR_UNUSED)
+{
+	bool result = TRUE;
+
+	/*
+	 * Perform operation
+	 */
+
+	result = testsuite_result_execute(renv);
+
+	/* Set result */
+	sieve_interpreter_set_test_result(renv->interp, result);
+
+	return SIEVE_EXEC_OK;
+}
+
+
+
+
diff --git a/tests/extensions/enotify/execute.svtest b/tests/extensions/enotify/execute.svtest
index 625a3a0d1..ef22672ea 100644
--- a/tests/extensions/enotify/execute.svtest
+++ b/tests/extensions/enotify/execute.svtest
@@ -10,7 +10,11 @@ test "RFC Example 1" {
 	}
 
 	if not test_script_run {
-		test_fail "script execute failed";
+		test_fail "script run failed";
+	}
+
+	if not test_result_execute {
+		test_fail "result execute failed";
 	}
 }
 
@@ -22,6 +26,10 @@ test "RFC Example 2" {
 	if not test_script_run {
 		test_fail "script execute failed";
 	}
+
+	if not test_result_execute {
+		test_fail "result execute failed";
+	}
 }
 
 /* tel: not supported
@@ -33,6 +41,10 @@ test "RFC Example 3" {
 	if not test_script_run {
 		test_fail "script execute failed";
 	}
+
+	if not test_result_execute {
+		test_fail "result execute failed";
+	}
 }
 */
 
@@ -45,6 +57,10 @@ test "RFC Example 5" {
 	if not test_script_run {
 		test_fail "script execute failed";
 	}
+
+	if not test_result_execute {
+		test_fail "result execute failed";
+	}
 }
 */
 
@@ -56,4 +72,8 @@ test "RFC Example 6" {
 	if not test_script_run {
 		test_fail "script execute failed";
 	}
+
+	if not test_result_execute {
+		test_fail "result execute failed";
+	}
 }
-- 
GitLab