From 6f1a5e1062631301668057e04222078f20650d5e Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Fri, 1 Aug 2008 19:25:36 +0200
Subject: [PATCH] Testsuite: added RFC compliance tests for the variables
 extension and fixed use of wrong default comparator.

---
 Makefile.am                                 |   2 +
 src/lib-sieve/ext-envelope.c                |   2 +-
 src/lib-sieve/tst-address.c                 |   2 +-
 src/lib-sieve/tst-header.c                  |   2 +-
 tests/extensions/variables/basic.svtest     | 106 ---------------
 tests/extensions/variables/match.svtest     | 138 ++++++++++++++++++++
 tests/extensions/variables/modifiers.svtest | 109 ++++++++++++++++
 tests/match-types/contains.svtest           |   2 +-
 8 files changed, 253 insertions(+), 110 deletions(-)
 create mode 100644 tests/extensions/variables/modifiers.svtest

diff --git a/Makefile.am b/Makefile.am
index d2c9cab7f..3090495a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,6 +32,8 @@ test_cases = \
 	tests/extensions/envelope.svtest \
 	tests/extensions/variables/basic.svtest \
 	tests/extensions/variables/match.svtest \
+	tests/extensions/variables/modifiers.svtest \
+	tests/extensions/variables/quoting.svtest \
 	tests/extensions/include/variables.svtest \
 	tests/extensions/imapflags/basic.svtest \
 	tests/extensions/imapflags/rfc.svtest \
diff --git a/src/lib-sieve/ext-envelope.c b/src/lib-sieve/ext-envelope.c
index 058705474..ccd1f72c8 100644
--- a/src/lib-sieve/ext-envelope.c
+++ b/src/lib-sieve/ext-envelope.c
@@ -412,7 +412,7 @@ static int ext_envelope_operation_execute
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {
 	bool result = TRUE;
-	const struct sieve_comparator *cmp = &i_octet_comparator;
+	const struct sieve_comparator *cmp = &i_ascii_casemap_comparator;
 	const struct sieve_match_type *mtch = &is_match_type;
 	const struct sieve_address_part *addrp = &all_address_part;
 	struct sieve_match_context *mctx;
diff --git a/src/lib-sieve/tst-address.c b/src/lib-sieve/tst-address.c
index 5b5bc4308..4761affcc 100644
--- a/src/lib-sieve/tst-address.c
+++ b/src/lib-sieve/tst-address.c
@@ -210,7 +210,7 @@ static int tst_address_operation_execute
 	const struct sieve_runtime_env *renv, sieve_size_t *address)
 {	
 	bool result = TRUE;
-	const struct sieve_comparator *cmp = &i_octet_comparator;
+	const struct sieve_comparator *cmp = &i_ascii_casemap_comparator;
 	const struct sieve_match_type *mtch = &is_match_type;
 	const struct sieve_address_part *addrp = &all_address_part;
 	struct sieve_match_context *mctx;
diff --git a/src/lib-sieve/tst-header.c b/src/lib-sieve/tst-header.c
index 3ca310678..4335aab70 100644
--- a/src/lib-sieve/tst-header.c
+++ b/src/lib-sieve/tst-header.c
@@ -164,7 +164,7 @@ static int tst_header_operation_execute
 {
 	bool result = TRUE;
 	int opt_code = 0;
-	const struct sieve_comparator *cmp = &i_octet_comparator;
+	const struct sieve_comparator *cmp = &i_ascii_casemap_comparator;
 	const struct sieve_match_type *mtch = &is_match_type;
 	struct sieve_match_context *mctx;
 	struct sieve_coded_stringlist *hdr_list;
diff --git a/tests/extensions/variables/basic.svtest b/tests/extensions/variables/basic.svtest
index c14459fea..f628480ce 100644
--- a/tests/extensions/variables/basic.svtest
+++ b/tests/extensions/variables/basic.svtest
@@ -150,112 +150,6 @@ test "Variables case-insensitive" {
 	}
 }
 
-/*
- * Modifiers
- */
-
-test "Modifier :lower" {
-	set :lower "test" "VaLuE";
-
-	if not string :is "${test}" "value" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifiers :lower :upperfirst" {
-	set :lower :upperfirst "test" "vAlUe";
-
-	if string :is "${test}" "value" {
-		test_fail "modifiers applied with wrong precedence";
-	}
-
-	if not string :is "${test}" "Value" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifiers :upperfirst :lower" {
-	set :upperfirst :lower "test" "vAlUe";
-
-	if string :is "${test}" "value" {
-		test_fail "modifiers applied with wrong precedence";
-	}
-
-	if not string :is "${test}" "Value" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifier :upper" {
-	set :upper "test" "vAlUe";
-
-	if not string :is "${test}" "VALUE" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifiers :upper :lowerfirst" {
-	set :upper :lowerfirst "test" "VaLuE";
-
-	if string :is "${test}" "VALUE" {
-		test_fail "modifiers applied with wrong precedence";
-	}
-
-	if not string :is "${test}" "vALUE" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifiers :lowerfirst :upper" {
-	set :lowerfirst :upper "test" "VaLuE";
-
-	if string :is "${test}" "VALUE" {
-		test_fail "modifiers applied with wrong precedence";
-	}
-
-	if not string :is "${test}" "vALUE" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifier :length" {
-	set :length "test" "VaLuE";
-
-	if not string :is "${test}" "5" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifier :length (elaborate)" {
-	set "a" "abcdefghijklmnopqrstuvwxyz";
-	set "b" "1234567890";
-	set :length "test" " ${a}:${b}  ";
-
-	if not string :is "${test}" "40" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifier :quotewildcard" {
-	set :quotewildcard "test" "^^***??**^^";
-
-	if not string :is "${test}" "^^\\*\\*\\*\\?\\?\\*\\*^^" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
-test "Modifier :length :quotewildcard" {
-	set :length :quotewildcard "test" "^^***??**^^";
-
-	if string :is "${test}" "11" {
-		test_fail "modifiers applied with wrong precedence";
-	}
-
-	if not string :is "${test}" "18" {
-		test_fail "modified variable assignment failed";
-	}
-}
-
 /*
  * Variable substitution
  */
diff --git a/tests/extensions/variables/match.svtest b/tests/extensions/variables/match.svtest
index 67c734afc..6871924a8 100644
--- a/tests/extensions/variables/match.svtest
+++ b/tests/extensions/variables/match.svtest
@@ -2,6 +2,143 @@ require "vnd.dovecot.testsuite";
 
 require "variables";
 
+
+/*
+ * RFC compliance
+ */
+
+# Test acceptance of leading zeroes
+test "RFC - leading zeroes" {
+	if not string :matches "frop:frup:frop" "*:*:*" {
+		test_fail "failed to match";
+	}
+
+	if not string :is "${0000002}" "frup" {
+		test_fail "incorrect match value (0000002): ${0000002}";
+	}
+}
+
+# Test non-greedyness
+test "RFC - not greedy" {
+	if not string :matches "frop.......frop.........frop...." "?*frop*" {
+		test_fail "failed to match";
+	}
+
+	if not string :is "${1}${2}${3}" "frop................frop...." {
+		test_fail "incorrect match values: ${1}${2}${3}";
+	}
+}
+
+# Index out of range
+test "RFC - index out of range" {
+	if not string :matches "test" "*" {
+		test_fail "failed to match (impossible)";
+	}
+
+	if not string :is "${2}" "" {
+		test_fail "incorrect match value: '${2}'";
+	}
+}
+
+# Index 0
+test "RFC - index 0" {
+	if not string :matches "a b c d e f g" "? ? ? ? ? ? ?" {
+		test_fail "failed to match";
+	}
+
+	if not string :is "${0}" "a b c d e f g" {
+        test_fail "incorrect match value: ${0}";
+    }
+}
+
+# Test short-circuit
+test "RFC - test short-circuit" {
+	if not anyof (
+		string :matches "a b c d e f g" "? ?",
+		string :matches "puk pok puk pok" "pu*ok",
+		string :matches "snot kip snot" "snot*snot"
+	) {
+		test_fail "failed to match any";
+	}
+
+	if string :is "${1}" " kip " {
+		test_fail "did not short-circuit test execution or intented test failed.";
+	}
+
+	if not string :is "${1}" "k pok puk p" {
+		test_fail "incorrect match value: ${1}";
+	}
+}
+
+test "RFC - example" {
+	test_set "message" text:
+Subject: [acme-users] [fwd] version 1.0 is out
+List-Id: Dovecot Mailing List <dovecot@dovecot.org>     
+To: coyote@ACME.Example.COM
+Fom: stephan@rename-it.nl
+
+Test message.
+.
+;
+	if header :matches "List-ID" "*<*@*" {	
+		if not string "INBOX.lists.${2}" "INBOX.lists.dovecot" {
+			test_fail "incorrect match value: INBOX.lists.${2}";
+		}
+	} else {
+		test_fail "failed to match list header";
+	}
+
+	# Imagine the header
+	# Subject: [acme-users] [fwd] version 1.0 is out
+	if header :matches "Subject" "[*] *" {
+		# ${1} will hold "acme-users",
+		# ${2} will hold "[fwd] version 1.0 is out"
+
+		if anyof (
+			not string "${1}" "acme-users",
+			not string "${2}" "[fwd] version 1.0 is out"
+		) {
+			test_fail "invalid match values: ${1} ${2}";
+		}
+	} else {
+		test_fail "failed to match subject";
+	}
+
+	# Imagine the header
+	# To: coyote@ACME.Example.COM
+	if address :matches ["To", "Cc"] ["coyote@**.com", 
+		"wile@**.com"] {
+		# ${0} is the matching address
+		# ${1} is always the empty string
+		# ${2} is part of the domain name ("ACME.Example")
+
+		if anyof (
+			not string "${0}" "coyote@ACME.Example.COM",
+			not string "${1}" "",
+			not string "${2}" "ACME.Example"
+		) {
+			test_fail "invalid match values: ${0}, ${1}, ${2}";
+		}      
+	} else {
+		# Control wouldn't reach this block if any match was
+		# successful, so no match variables are set at this
+ 		# point.
+
+		test_fail "failed to match to address";
+ 	}
+
+	if anyof (true, address :domain :matches "To" "*.com") {
+		# The second test is never evaluated, so there are
+		# still no match variables set.
+		
+		/* FIXME: not compliant */
+	}
+}
+
+/*
+ * Generic tests
+ */
+
 set "match1" "Test of general stupidity";
 
 test "Begin" {
@@ -165,3 +302,4 @@ test "Letters words *? first" {
 		test_fail "incorrect match values: ${val}";
 	}
 }
+
diff --git a/tests/extensions/variables/modifiers.svtest b/tests/extensions/variables/modifiers.svtest
new file mode 100644
index 000000000..04fe9d67c
--- /dev/null
+++ b/tests/extensions/variables/modifiers.svtest
@@ -0,0 +1,109 @@
+require "vnd.dovecot.testsuite";
+require "variables";
+
+/*
+ * Modifiers
+ */
+
+test "Modifier :lower" {
+	set :lower "test" "VaLuE";
+
+	if not string :is "${test}" "value" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifiers :lower :upperfirst" {
+	set :lower :upperfirst "test" "vAlUe";
+
+	if string :is "${test}" "value" {
+		test_fail "modifiers applied with wrong precedence";
+	}
+
+	if not string :is "${test}" "Value" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifiers :upperfirst :lower" {
+	set :upperfirst :lower "test" "vAlUe";
+
+	if string :is "${test}" "value" {
+		test_fail "modifiers applied with wrong precedence";
+	}
+
+	if not string :is "${test}" "Value" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifier :upper" {
+	set :upper "test" "vAlUe";
+
+	if not string :is "${test}" "VALUE" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifiers :upper :lowerfirst" {
+	set :upper :lowerfirst "test" "VaLuE";
+
+	if string :is "${test}" "VALUE" {
+		test_fail "modifiers applied with wrong precedence";
+	}
+
+	if not string :is "${test}" "vALUE" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifiers :lowerfirst :upper" {
+	set :lowerfirst :upper "test" "VaLuE";
+
+	if string :is "${test}" "VALUE" {
+		test_fail "modifiers applied with wrong precedence";
+	}
+
+	if not string :is "${test}" "vALUE" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifier :length" {
+	set :length "test" "VaLuE";
+
+	if not string :is "${test}" "5" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifier :length (elaborate)" {
+	set "a" "abcdefghijklmnopqrstuvwxyz";
+	set "b" "1234567890";
+	set :length "test" " ${a}:${b}  ";
+
+	if not string :is "${test}" "40" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifier :quotewildcard" {
+	set :quotewildcard "test" "^^***??**^^";
+
+	if not string :is "${test}" "^^\\*\\*\\*\\?\\?\\*\\*^^" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
+test "Modifier :length :quotewildcard" {
+	set :length :quotewildcard "test" "^^***??**^^";
+
+	if string :is "${test}" "11" {
+		test_fail "modifiers applied with wrong precedence";
+	}
+
+	if not string :is "${test}" "18" {
+		test_fail "modified variable assignment failed";
+	}
+}
+
diff --git a/tests/match-types/contains.svtest b/tests/match-types/contains.svtest
index d8f512285..719f20581 100644
--- a/tests/match-types/contains.svtest
+++ b/tests/match-types/contains.svtest
@@ -54,7 +54,7 @@ test "Match case-insensitive" {
 		test_fail "match fails to apply correct comparator";	
 	}
 
-	if address :contains "from" "RENAME-IT" {
+	if not address :contains "from" "RENAME-IT" {
         test_fail "default comparator is wrong";
     }
 }
-- 
GitLab