From 30c8cf2e11e949408236c208d47d74aa69d1cb11 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sat, 2 Aug 2008 12:25:07 +0200
Subject: [PATCH] Variables: fixed various error handling issues.

---
 Makefile.am                                   |  1 +
 .../variables/ext-variables-arguments.c       | 10 ++++----
 tests/extensions/variables/errors.svtest      | 24 +++++++++++++++++++
 .../variables/errors/namespace.sieve          |  8 +++++++
 tests/extensions/variables/errors/set.sieve   | 16 +++++++++++++
 5 files changed, 54 insertions(+), 5 deletions(-)
 create mode 100644 tests/extensions/variables/errors.svtest
 create mode 100644 tests/extensions/variables/errors/namespace.sieve
 create mode 100644 tests/extensions/variables/errors/set.sieve

diff --git a/Makefile.am b/Makefile.am
index c518ec9b5..7e5a978c0 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -35,6 +35,7 @@ test_cases = \
 	tests/extensions/variables/modifiers.svtest \
 	tests/extensions/variables/quoting.svtest \
 	tests/extensions/variables/string.svtest \
+	tests/extensions/variables/errors.svtest \
 	tests/extensions/include/variables.svtest \
 	tests/extensions/imapflags/basic.svtest \
 	tests/extensions/imapflags/rfc.svtest \
diff --git a/src/lib-sieve/plugins/variables/ext-variables-arguments.c b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
index 146fd8f2d..3f8382650 100644
--- a/src/lib-sieve/plugins/variables/ext-variables-arguments.c
+++ b/src/lib-sieve/plugins/variables/ext-variables-arguments.c
@@ -31,7 +31,7 @@ const struct sieve_argument variable_argument = {
 
 static struct sieve_ast_argument *ext_variables_variable_argument_create
 (struct sieve_validator *validator, struct sieve_ast *ast, 
-	unsigned int source_line,	const char *variable)
+	unsigned int source_line, const char *variable)
 {
 	struct sieve_variable *var;
 	struct sieve_ast_argument *arg;
@@ -70,7 +70,7 @@ bool sieve_variable_argument_activate
 	
 		if ( nelements < 0 || varstr != varend ) {
 			sieve_command_validate_error(validator, cmd, 
-				"invalid variable name '%s'", varstr);
+				"invalid variable name '%s'", str_c(variable));
 		} else if ( nelements == 1 ) {
 			const struct ext_variable_name *cur_element = 
 				array_idx(&vname, 0);
@@ -84,7 +84,7 @@ bool sieve_variable_argument_activate
 				
 				result = TRUE;
 			} else {
-				if ( assignment ) {
+				if ( !assignment ) {
 					arg->argument = &match_value_argument;
 					arg->context = (void *) cur_element->num_variable;
 				
@@ -105,8 +105,8 @@ bool sieve_variable_argument_activate
 			 */
 
 			sieve_command_validate_error(validator, cmd, 
-				"cannot assign to variable in unknown namespace '%s'", 
-				str_c(cur_element->identifier));
+				"cannot %s to variable in unknown namespace '%s'", 
+				assignment ? "assign" : "refer", str_c(cur_element->identifier));
 		}
 	} T_END;
 
diff --git a/tests/extensions/variables/errors.svtest b/tests/extensions/variables/errors.svtest
new file mode 100644
index 000000000..2fbdeb516
--- /dev/null
+++ b/tests/extensions/variables/errors.svtest
@@ -0,0 +1,24 @@
+require "vnd.dovecot.testsuite";
+
+require "comparator-i;ascii-numeric";
+require "relational";
+
+test "Invalid namespaces (FIXME: count only)" {
+	if test_compile "errors/namespace.sieve" {
+		test_fail "compile should have failed";
+	}
+
+	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
+		test_fail "wrong number of errors reported";
+	}
+}
+
+test "Invalid set command invocations (FIXME: count only)" {
+	if test_compile "errors/set.sieve" {
+		test_fail "compile should have failed";
+	}
+
+	if not test_error :count "eq" :comparator "i;ascii-numeric" "5" {
+		test_fail "wrong number of errors reported";
+	}
+}
diff --git a/tests/extensions/variables/errors/namespace.sieve b/tests/extensions/variables/errors/namespace.sieve
new file mode 100644
index 000000000..e11ac6d78
--- /dev/null
+++ b/tests/extensions/variables/errors/namespace.sieve
@@ -0,0 +1,8 @@
+require "variables";
+require "fileinto";
+
+set "namespace.frop" "value";
+set "complex.struct.frop" "value";
+
+fileinto "${namespace.frop}";
+fileinto "${complex.struct.frop}";
diff --git a/tests/extensions/variables/errors/set.sieve b/tests/extensions/variables/errors/set.sieve
new file mode 100644
index 000000000..f3cb5fd46
--- /dev/null
+++ b/tests/extensions/variables/errors/set.sieve
@@ -0,0 +1,16 @@
+require "variables";
+
+# Invalid variable name
+set "${frop}" "frop";
+set "...." "frop";
+set "name." "frop";
+set ".name" "frop";
+
+# Not an error
+set "\n\a\m\e" "frop";
+
+# Trying to assign match variable;
+set "0" "frop";
+
+# Not an error
+set :UPPER "name" "frop";
-- 
GitLab