From 645bd1220eab21e96c166d7b42c21c8bd42eb2c5 Mon Sep 17 00:00:00 2001
From: Stephan Bosch <stephan@rename-it.nl>
Date: Sun, 3 Jan 2016 22:06:50 +0100
Subject: [PATCH] test suite: mime extension: Added more test for exists and
 address tests with :mime tag.

---
 Makefile.am                          |   2 +
 tests/extensions/mime/address.svtest | 281 +++++++++++++++++++++++++++
 tests/extensions/mime/exists.svtest  | 237 ++++++++++++++++++++++
 3 files changed, 520 insertions(+)
 create mode 100644 tests/extensions/mime/address.svtest
 create mode 100644 tests/extensions/mime/exists.svtest

diff --git a/Makefile.am b/Makefile.am
index 883f67cc9..943185084 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -168,6 +168,8 @@ test_cases = \
 	tests/extensions/metadata/errors.svtest \
 	tests/extensions/mime/errors.svtest \
 	tests/extensions/mime/header.svtest \
+	tests/extensions/mime/exists.svtest \
+	tests/extensions/mime/address.svtest \
 	tests/extensions/mime/execute.svtest \
 	tests/extensions/mime/content-header.svtest \
 	tests/extensions/mime/foreverypart.svtest \
diff --git a/tests/extensions/mime/address.svtest b/tests/extensions/mime/address.svtest
new file mode 100644
index 000000000..160745087
--- /dev/null
+++ b/tests/extensions/mime/address.svtest
@@ -0,0 +1,281 @@
+require "vnd.dovecot.testsuite";
+require "mime";
+require "foreverypart";
+
+/*
+ * Basic functionionality
+ */
+
+test_set "message" text:
+From: stephan@example.com
+To: nico@nl.example.com, harry@de.example.com
+cc: Timo <tss(no spam)@fi.iki>
+Subject: Frobnitzm
+
+Test.
+.
+;
+
+test "Basic functionality" {
+	/* Must match */
+	if not address :mime :anychild :contains ["to", "from"] "harry" {
+		test_fail "failed to match address (1)";
+	}
+
+	if not address :mime :anychild :contains ["to", "from"] "de.example" {
+		test_fail "failed to match address (2)";
+	}
+
+	if not address :mime :anychild :matches "to" "*@*.example.com" {
+		test_fail "failed to match address (3)";
+	}
+
+	if not address :mime :anychild :is "to" "harry@de.example.com" {
+		test_fail "failed to match address (4)";
+	}
+
+	/* Must not match */
+	if address :mime :anychild :is ["to", "from"] "nonsense@example.com" {
+		test_fail "matches erroneous address";
+	}
+
+	/* Match first key */
+	if not address :mime :anychild :contains ["to"] ["nico", "fred", "henk"] {
+		test_fail "failed to match first key";
+	}
+
+	/* Match second key */
+	if not address :mime :anychild :contains ["to"] ["fred", "nico", "henk"] {
+		test_fail "failed to match second key";
+	}
+
+	/* Match last key */
+	if not address :mime :anychild :contains ["to"] ["fred", "henk", "nico"] {
+		test_fail "failed to match last key";
+	}
+
+	/* First header */
+	if not address :mime :anychild :contains
+		["to", "from"] ["fred", "nico", "henk"] {
+		test_fail "failed to match first header";
+	}
+
+	/* Second header */
+	if not address :mime :anychild :contains
+		["from", "to"] ["fred", "nico", "henk"] {
+		test_fail "failed to match second header";
+	}
+
+	/* Comment */
+	if not address :mime :anychild :is "cc" "tss@fi.iki" {
+		test_fail "failed to ignore comment in address";
+	}
+}
+
+/*
+ * Basic functionionality - foreverypart
+ */
+
+test "Basic functionality - foreverypart" {
+	foreverypart {
+		/* Must match */
+		if not address :mime :anychild :contains ["to", "from"] "harry" {
+			test_fail "failed to match address (1)";
+		}
+
+		if not address :mime :anychild :contains ["to", "from"] "de.example" {
+			test_fail "failed to match address (2)";
+		}
+
+		if not address :mime :anychild :matches "to" "*@*.example.com" {
+			test_fail "failed to match address (3)";
+		}
+
+		if not address :mime :anychild :is "to" "harry@de.example.com" {
+			test_fail "failed to match address (4)";
+		}
+
+		/* Must not match */
+		if address :mime :anychild :is ["to", "from"] "nonsense@example.com" {
+			test_fail "matches erroneous address";
+		}
+
+		/* Match first key */
+		if not address :mime :anychild :contains ["to"] ["nico", "fred", "henk"] {
+			test_fail "failed to match first key";
+		}
+
+		/* Match second key */
+		if not address :mime :anychild :contains ["to"] ["fred", "nico", "henk"] {
+			test_fail "failed to match second key";
+		}
+
+		/* Match last key */
+		if not address :mime :anychild :contains ["to"] ["fred", "henk", "nico"] {
+			test_fail "failed to match last key";
+		}
+
+		/* First header */
+		if not address :mime :anychild :contains
+			["to", "from"] ["fred", "nico", "henk"] {
+			test_fail "failed to match first header";
+		}
+
+		/* Second header */
+		if not address :mime :anychild :contains
+			["from", "to"] ["fred", "nico", "henk"] {
+			test_fail "failed to match second header";
+		}
+
+		/* Comment */
+		if not address :mime :anychild :is "cc" "tss@fi.iki" {
+			test_fail "failed to ignore comment in address";
+		}
+	}
+}
+
+/*
+ * Address headers
+ */
+
+test_set "message" text:
+From: stephan@friep.frop
+To: henk@tukkerland.ex
+CC: ivo@boer.ex
+Bcc: joop@hooibaal.ex
+Sender: s.bosch@friep.frop
+Resent-From: ivo@boer.ex
+Resent-To: idioot@dombo.ex
+Subject: Berichtje
+
+Test.
+.
+;
+
+test "Address headers" {
+	if not address :mime :anychild "from" "stephan@friep.frop" {
+		test_fail "from header not recognized";
+	}
+
+	if not address :mime :anychild "to" "henk@tukkerland.ex" {
+		test_fail "to header not recognized";
+	}
+
+	if not address :mime :anychild "cc" "ivo@boer.ex" {
+		test_fail "cc header not recognized";
+	}
+
+	if not address :mime :anychild "bcc" "joop@hooibaal.ex" {
+		test_fail "bcc header not recognized";
+	}
+
+	if not address :mime :anychild "sender" "s.bosch@friep.frop" {
+		test_fail "sender header not recognized";
+	}
+
+	if not address :mime :anychild "resent-from" "ivo@boer.ex" {
+		test_fail "resent-from header not recognized";
+	}
+
+	if not address :mime :anychild "resent-to" "idioot@dombo.ex" {
+		test_fail "resent-to header not recognized";
+	}
+}
+
+/*
+ * Address headers - foreverypart
+ */
+
+test "Address headers - foreverypart" {
+	foreverypart {
+		if not address :mime :anychild "from" "stephan@friep.frop" {
+			test_fail "from header not recognized";
+		}
+
+		if not address :mime :anychild "to" "henk@tukkerland.ex" {
+			test_fail "to header not recognized";
+		}
+
+		if not address :mime :anychild "cc" "ivo@boer.ex" {
+			test_fail "cc header not recognized";
+		}
+
+		if not address :mime :anychild "bcc" "joop@hooibaal.ex" {
+			test_fail "bcc header not recognized";
+		}
+
+		if not address :mime :anychild "sender" "s.bosch@friep.frop" {
+			test_fail "sender header not recognized";
+		}
+
+		if not address :mime :anychild "resent-from" "ivo@boer.ex" {
+			test_fail "resent-from header not recognized";
+		}
+
+		if not address :mime :anychild "resent-to" "idioot@dombo.ex" {
+			test_fail "resent-to header not recognized";
+		}
+	}
+}
+
+/*
+ * Multipart anychild
+ */
+
+test_set "message" text:
+From: Hendrik <hendrik@example.com>
+To: Harrie <harrie@example.com>
+Date: Sat, 11 Oct 2010 00:31:44 +0200
+Subject: Harrie is een prutser
+Content-Type: multipart/mixed; boundary=AA
+CC: AA@example.com
+
+This is a multi-part message in MIME format.
+--AA
+Content-Type: multipart/mixed; boundary=BB
+CC: BB@example.com
+
+This is a multi-part message in MIME format.
+--BB
+Content-Type: text/plain; charset="us-ascii"
+CC: CC@example.com
+
+Hello
+
+--BB
+Content-Type: text/plain; charset="us-ascii"
+CC: DD@example.com
+
+Hello again
+
+--BB--
+This is the end of MIME multipart.
+
+--AA
+Content-Type: text/plain; charset="us-ascii"
+CC: EE@example.com
+
+And again
+
+--AA--
+This is the end of  MIME multipart.
+.
+;
+
+test "Multipart anychild" {
+	if not address :mime :anychild :localpart "Cc" "AA" {
+		test_fail "AA Cc repient does not exist";
+	}
+	if not address :mime :anychild :localpart "Cc" "BB" {
+		test_fail "BB Cc repient does not exist";
+	}
+	if not address :mime :anychild :localpart "Cc" "CC" {
+		test_fail "CC Cc repient does not exist";
+	}
+	if not address :mime :anychild :localpart "Cc" "DD" {
+		test_fail "DD Cc repient does not exist";
+	}
+	if not address :mime :anychild :localpart "Cc" "EE" {
+		test_fail "EE Cc repient does not exist";
+	}
+}
diff --git a/tests/extensions/mime/exists.svtest b/tests/extensions/mime/exists.svtest
new file mode 100644
index 000000000..517deebda
--- /dev/null
+++ b/tests/extensions/mime/exists.svtest
@@ -0,0 +1,237 @@
+require "vnd.dovecot.testsuite";
+require "mime";
+require "foreverypart";
+
+test_set "message" text:
+From: stephan@example.org
+To: nico@vestingbar.bl
+Subject: Test message
+Date: Wed, 29 Jul 2009 18:21:44 +0300
+X-Spam-Status: Not Spam
+Resent-To: nico@frop.example.com
+
+Test!
+.
+;
+
+/*
+ * One header
+ */
+
+test "One header" {
+	if not exists :mime :anychild "from" {
+		test_fail "exists test missed from header";
+	}
+
+	if exists :mime :anychild "x-nonsense" {
+		test_fail "exists test found non-existent header";
+	}
+}
+
+/*
+ * One header - foreverypart
+ */
+
+test "One header - foreverypart" {
+	foreverypart {
+		if not exists :mime :anychild "from" {
+			test_fail "exists test missed from header";
+		}
+
+		if exists :mime :anychild "x-nonsense" {
+			test_fail "exists test found non-existent header";
+		}
+	}
+}
+
+/*
+ * Two headers
+ */
+
+test "Two headers" {
+	if not exists :mime :anychild ["from","to"] {
+		test_fail "exists test missed from or to header";
+	}
+
+	if exists :mime :anychild ["from","x-nonsense"] {
+		test_fail "exists test found non-existent header (1)";
+	}
+
+	if exists :mime :anychild ["x-nonsense","to"] {
+		test_fail "exists test found non-existent header (2)";
+	}
+
+	if exists :mime :anychild ["x-nonsense","x-nonsense2"] {
+		test_fail "exists test found non-existent header (3)";
+	}
+}
+
+/*
+ * Two headers - foreverypart
+ */
+
+test "Two headers - foreverypart" {
+	foreverypart {
+		if not exists :mime :anychild ["from","to"] {
+			test_fail "exists test missed from or to header";
+		}
+
+		if exists :mime :anychild ["from","x-nonsense"] {
+			test_fail "exists test found non-existent header (1)";
+		}
+
+		if exists :mime :anychild ["x-nonsense","to"] {
+			test_fail "exists test found non-existent header (2)";
+		}
+
+		if exists :mime :anychild ["x-nonsense","x-nonsense2"] {
+			test_fail "exists test found non-existent header (3)";
+		}
+	}
+}
+
+/*
+ * Three headers
+ */
+
+test "Three headers" {
+	if not exists :mime :anychild ["Subject","date","resent-to"] {
+		test_fail "exists test missed subject, date or resent-to header";
+	}
+
+	if exists :mime :anychild ["x-nonsense","date","resent-to"] {
+		test_fail "exists test found non-existent header (1)";
+	}
+
+	if exists :mime :anychild ["subject", "x-nonsense","resent-to"] {
+		test_fail "exists test found non-existent header (2)";
+	}
+
+	if exists :mime :anychild ["subject","date","x-nonsense"] {
+		test_fail "exists test found non-existent header (3)";
+	}
+
+	if exists :mime :anychild ["subject", "x-nonsense","x-nonsense2"] {
+		test_fail "exists test found non-existent header (4)";
+	}
+
+	if exists :mime :anychild ["x-nonsense","date","x-nonsense2"] {
+		test_fail "exists test found non-existent header (5)";
+	}
+
+	if exists :mime :anychild ["x-nonsense","x-nonsense2","resent-to"] {
+		test_fail "exists test found non-existent header (6)";
+	}
+
+	if exists :mime :anychild ["x-nonsense","x-nonsense2","x-nonsense3"] {
+		test_fail "exists test found non-existent header (7)";
+	}
+}
+
+/*
+ * Three headers - foreverypart
+ */
+
+test "Three headers - foreverypart " {
+	foreverypart {
+		if not exists :mime :anychild ["Subject","date","resent-to"] {
+			test_fail "exists test missed subject, date or resent-to header";
+		}
+
+		if exists :mime :anychild ["x-nonsense","date","resent-to"] {
+			test_fail "exists test found non-existent header (1)";
+		}
+
+		if exists :mime :anychild ["subject", "x-nonsense","resent-to"] {
+			test_fail "exists test found non-existent header (2)";
+		}
+
+		if exists :mime :anychild ["subject","date","x-nonsense"] {
+			test_fail "exists test found non-existent header (3)";
+		}
+
+		if exists :mime :anychild ["subject", "x-nonsense","x-nonsense2"] {
+			test_fail "exists test found non-existent header (4)";
+		}
+
+		if exists :mime :anychild ["x-nonsense","date","x-nonsense2"] {
+			test_fail "exists test found non-existent header (5)";
+		}
+
+		if exists :mime :anychild ["x-nonsense","x-nonsense2","resent-to"] {
+			test_fail "exists test found non-existent header (6)";
+		}
+
+		if exists :mime :anychild ["x-nonsense","x-nonsense2","x-nonsense3"] {
+			test_fail "exists test found non-existent header (7)";
+		}
+	}
+}
+
+/*
+ * Multipart anychild
+ */
+
+test_set "message" text:
+From: Hendrik <hendrik@example.com>
+To: Harrie <harrie@example.com>
+Date: Sat, 11 Oct 2010 00:31:44 +0200
+Subject: Harrie is een prutser
+Content-Type: multipart/mixed; boundary=AA
+X-Test1: AA
+
+This is a multi-part message in MIME format.
+--AA
+Content-Type: multipart/mixed; boundary=BB
+X-Test2: BB
+
+This is a multi-part message in MIME format.
+--BB
+Content-Type: text/plain; charset="us-ascii"
+X-Test3: CC
+
+Hello
+
+--BB
+Content-Type: text/plain; charset="us-ascii"
+X-Test4: DD
+
+Hello again
+
+--BB--
+This is the end of MIME multipart.
+
+--AA
+Content-Type: text/plain; charset="us-ascii"
+X-Test5: EE
+
+And again
+
+--AA--
+This is the end of  MIME multipart.
+.
+;
+
+test "Multipart anychild" {
+	if not exists :mime :anychild "X-Test1" {
+		test_fail "X-Test1 header does exist";
+	}
+	if not exists :mime :anychild "X-Test2" {
+		test_fail "X-Test2 header does exist";
+	}
+	if not exists :mime :anychild "X-Test3" {
+		test_fail "X-Test3 header does exist";
+	}
+	if not exists :mime :anychild "X-Test4" {
+		test_fail "X-Test4 header does exist";
+	}
+	if not exists :mime :anychild "X-Test5" {
+		test_fail "X-Test5 header does exist";
+	}
+	if not exists :mime :anychild
+			["X-Test1", "X-Test2", "X-Test3", "X-Test4", "X-Test5"] {
+		test_fail "Not all headers exist";
+	}
+}
+
+
-- 
GitLab