Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/thsmi/sieve.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorThomas Schmid <schmid-thomas@gmx.net>2018-11-26 15:27:30 +0300
committerThomas Schmid <schmid-thomas@gmx.net>2018-11-26 15:27:30 +0300
commitd9326c8119e4b65cd6294bec622af2e3ba945e10 (patch)
tree45815833a81af329b90595c20f24c2c007afe032 /tests
parentaff2845286c33e786bd48fc5669798ad590e0ea9 (diff)
Convert examples to tests
Diffstat (limited to 'tests')
-rw-r--r--tests/tests/sieve/SieveDovecotTest.js347
-rw-r--r--tests/tests/sieve/SieveTty1Test.js228
-rwxr-xr-xtests/tests/tests.js29
3 files changed, 604 insertions, 0 deletions
diff --git a/tests/tests/sieve/SieveDovecotTest.js b/tests/tests/sieve/SieveDovecotTest.js
new file mode 100644
index 00000000..dc8352a7
--- /dev/null
+++ b/tests/tests/sieve/SieveDovecotTest.js
@@ -0,0 +1,347 @@
+/*
+ * The contents of this file are licensed. You may obtain a copy of
+ * the license at https://github.com/thsmi/sieve/ or request it via
+ * email from the author.
+ *
+ * Do not remove or change this comment.
+ *
+ * The initial author of the code is:
+ * Thomas Schmid <schmid-thomas@gmx.net>
+ *
+ */
+
+(function () {
+
+ "use strict";
+
+ /* global net */
+
+ let suite = net.tschmid.yautt.test;
+
+ if (!suite)
+ throw new Error("Could not initialize test suite");
+
+
+ suite.add(function () {
+ suite.log("Examples from Dovecot...");
+ suite.log("https://wiki2.dovecot.org/Pigeonhole/Sieve/Examples");
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Dovecot - Mail filtering by various headers I");
+
+ let script = ''
+ + 'require ["fileinto", "envelope"];\r\n'
+ + 'if address :is "to" "dovecot@dovecot.org" {\r\n'
+ + ' fileinto "Dovecot-list";\r\n'
+ + '} elsif envelope :is "from" "owner-cipe-l@inka.de" {\r\n'
+ + ' fileinto "lists.cipe";\r\n'
+ + '} elsif anyof (header :contains "X-listname" "lugog@cip.rz.fh-offenburg.de",\r\n'
+ + ' header :contains "List-Id" "Linux User Group Offenburg") {\r\n'
+ + ' fileinto "ml.lugog";\r\n'
+ + '} else {\r\n'
+ + ' # The rest goes into INBOX\r\n'
+ + ' # default is "implicit keep", we do it explicitly here\r\n'
+ + ' keep;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto", "envelope"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Mail filtering by various headers II");
+
+ let script = ''
+ + 'if header :contains "subject" ["order", "buy"] {\r\n'
+ + ' redirect "orders@company.dom";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, []);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Flagging or Highlighting your mail I");
+
+ let script = ''
+ + 'require "imap4flags";\r\n'
+ + 'require "regex";\r\n'
+ + 'if anyof (exists "X-Cron-Env",\r\n'
+ + ' header :regex ["subject"] [".* security run output",\r\n'
+ + ' ".* monthly run output",\r\n'
+ + ' ".* daily run output",\r\n'
+ + ' ".* weekly run output"]) {\r\n'
+ + ' addflag "$label1"; # ie \'Important\'/red label within Thunderbird\r\n'
+ + '\r\n'
+ + '# Other flags:\r\n'
+ + '# addflag "$label1"; # Important: #ff0000 => red\r\n'
+ + '# addflag "$label2"; # Work: #ff9900 => orange\r\n'
+ + '# addflag "$label3"; # personal: #009900 => green\r\n'
+ + '# addflag "$label4"; # todo: #3333ff => blue\r\n'
+ + '# addflag "$label5"; # later: #993399 => violet\r\n'
+ + '#\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["imap4flags", "regex"]);
+ });
+
+ suite.add(function () {
+
+ suite.log("Flagging or Highlighting your mail II");
+
+ let script = ''
+ + 'require ["envelope", "imap4flags"];\r\n'
+ + 'if envelope "from" "my_address@my_domain.com"\r\n'
+ + '{\r\n'
+ + ' setflag "\\\\seen";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["envelope", "imap4flags"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Direct filtering using message header I");
+
+ let script = ''
+ + 'require "fileinto";\r\n'
+ + 'if header :contains "X-Spam-Flag" "YES" {\r\n'
+ + ' fileinto "Spam";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Direct filtering using message header II");
+
+ let script = ''
+ + 'if header :contains "X-Spam-Level" "**********" {\r\n'
+ + ' discard;\r\n'
+ + ' stop;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, []);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Direct filtering using message header III");
+
+ let script = ''
+ + 'require ["comparator-i;ascii-numeric","relational"];\r\n'
+ + 'if allof (\r\n'
+ + ' not header :matches "x-spam-score" "-*",\r\n'
+ + ' header :value "ge" :comparator "i;ascii-numeric" "x-spam-score" "10" )\r\n'
+ + '{\r\n'
+ + ' discard;\r\n'
+ + ' stop;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["comparator-i;ascii-numeric", "relational"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Filtering using the spamtest and virustest extensions I");
+
+ let script = ''
+ + 'require "spamtestplus";\r\n'
+ + 'require "fileinto";\r\n'
+ + 'require "relational";\r\n'
+ + 'require "comparator-i;ascii-numeric";\r\n'
+ + '\r\n'
+ + '/* If the spamtest fails for some reason, e.g. spam header is missing, file\r\n'
+ + ' * file it in a special folder.\r\n'
+ + ' */\r\n'
+ + 'if spamtest :value "eq" :comparator "i;ascii-numeric" "0" {\r\n'
+ + ' fileinto "Unclassified";\r\n'
+ + '\r\n'
+ + '/* If the spamtest score (in the range 1-10) is larger than or equal to 3,\r\n'
+ + ' * file it into the spam folder:\r\n'
+ + ' */\r\n'
+ + '} elsif spamtest :value "ge" :comparator "i;ascii-numeric" "3" {\r\n'
+ + ' fileinto "Spam";\r\n'
+ + '\r\n'
+ + '/* For more fine-grained score evaluation, the :percent tag can be used. The\r\n'
+ + ' * following rule discards all messages with a percent score\r\n'
+ + ' * (relative to maximum) of more than 85 %:\r\n'
+ + ' */\r\n'
+ + '} elsif spamtest :value "gt" :comparator "i;ascii-numeric" :percent "85" {\r\n'
+ + ' discard;\r\n'
+ + '}\r\n'
+ + '\r\n'
+ + '/* Other messages get filed into INBOX */\r\n';
+
+ suite.expectValidScript(script,
+ ["spamtestplus", "fileinto", "relational", "comparator-i;ascii-numeric"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Filtering using the spamtest and virustest extensions II");
+
+ let script = ''
+ + 'require "virustest";\r\n'
+ + 'require "fileinto";\r\n'
+ + 'require "relational";\r\n'
+ + 'require "comparator-i;ascii-numeric";\r\n'
+ + '\r\n'
+ + '/* Not scanned ? */\r\n'
+ + 'if virustest :value "eq" :comparator "i;ascii-numeric" "0" {\r\n'
+ + ' fileinto "Unscanned";\r\n'
+ + '\r\n'
+ + '/* Infected with high probability (value range in 1-5) */\r\n'
+ + '} if virustest :value "eq" :comparator "i;ascii-numeric" "4" {\r\n'
+ + ' /* Quarantine it in special folder (still somewhat dangerous) */\r\n'
+ + ' fileinto "Quarantine";\r\n'
+ + '\r\n'
+ + '/* Definitely infected */\r\n'
+ + '} elsif virustest :value "eq" :comparator "i;ascii-numeric" "5" {\r\n'
+ + ' /* Just get rid of it */\r\n'
+ + ' discard;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script,
+ ["virustest", "fileinto", "relational", "comparator-i;ascii-numeric"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Plus Addressed mail filtering I");
+
+ let script = ''
+ + 'require ["fileinto", "envelope", "subaddress"];\r\n'
+ + 'if envelope :detail "to" "spam"{\r\n'
+ + ' fileinto "Spam";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto", "envelope", "subaddress"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Plus Addressed mail filtering II");
+
+ let script = ''
+ + 'require ["variables", "envelope", "fileinto", "subaddress"];\r\n'
+ + '\r\n'
+ + 'if envelope :is :user "to" "sales" {\r\n'
+ + ' if envelope :matches :detail "to" "*" {\r\n'
+ + ' /* Save name in ${name} in all lowercase except for the first letter.\r\n'
+ + ' * Joe, joe, jOe thus all become \'Joe\'.\r\n'
+ + ' */\r\n'
+ + ' set :lower :upperfirst "name" "${1}";\r\n'
+ + ' }\r\n'
+ + '\r\n'
+ + ' if string :is "${name}" "" {\r\n'
+ + ' /* Default case if no detail is specified */\r\n'
+ + ' fileinto "sales";\r\n'
+ + ' } else {\r\n'
+ + ' /* For sales+joe@ this will become users/Joe */\r\n'
+ + ' fileinto "users/${name}";\r\n'
+ + ' }\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["variables", "envelope", "fileinto", "subaddress"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Vacation auto-reply I");
+
+ let script = ''
+ + 'require ["fileinto", "vacation"];\r\n'
+ + '# Move spam to spam folder\r\n'
+ + 'if header :contains "X-Spam-Flag" "YES" {\r\n'
+ + ' fileinto "spam";\r\n'
+ + ' # Stop here so that we do not reply on spams\r\n'
+ + ' stop;\r\n'
+ + '}\r\n'
+ + 'vacation\r\n'
+ + ' # Reply at most once a day to a same sender\r\n'
+ + ' :days 1\r\n'
+ + ' :subject "Out of office reply"\r\n'
+ + ' # List of additional recipient addresses which are included in the auto replying.\r\n'
+ + ' # If a mail\'s recipient is not the envelope recipient and it\'s not on this list,\r\n'
+ + ' # no vacation reply is sent for it.\r\n'
+ + ' :addresses ["j.doe@company.dom", "john.doe@company.dom"]\r\n'
+ + '"I\'m out of office, please contact Joan Doe instead.\r\n'
+ + 'Best regards\r\n'
+ + 'John Doe";\r\n';
+
+ suite.expectValidScript(script, ["fileinto", "vacation"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Vacation auto-reply II");
+
+ let script = ''
+ + 'require ["variables", "vacation"];\r\n'
+ + '# Store old Subject line so it can be used in vacation message\r\n'
+ + 'if header :matches "Subject" "*" {\r\n'
+ + ' set "subjwas" ": ${1}";\r\n'
+ + '}\r\n'
+ + 'vacation\r\n'
+ + ' :days 1\r\n'
+ + ' :subject "Out of office reply${subjwas}"\r\n'
+ + ' :addresses ["j.doe@company.dom", "john.doe@company.dom"]\r\n'
+ + '"I\'m out of office, please contact Joan Doe instead.\r\n'
+ + 'Best regards\r\n'
+ + 'John Doe";\r\n';
+
+
+ suite.expectValidScript(script, ["variables", "vacation"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Include scripts");
+
+ let script = ''
+ + 'require ["include"];\r\n'
+ + 'include :global "global-spam";\r\n'
+ + 'include :personal "my-own-spam";\r\n';
+
+ suite.expectValidScript(script, ["include"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("Archiving a Mailinglist by Date");
+
+ let script = ''
+ + 'require ["variables","date","fileinto","mailbox"];\r\n'
+ + '\r\n'
+ + '# Extract date info\r\n'
+ + 'if currentdate :matches "year" "*" { set "year" "${1}"; }\r\n'
+ + 'if currentdate :matches "month" "*" { set "month" "${1}"; }\r\n'
+ + '\r\n'
+ + '# Archive Dovecot mailing list items by year and month.\r\n'
+ + '# Create folder when it does not exist.\r\n'
+ + 'if header :is "list-id" "dovecot.dovecot.org" {\r\n'
+ + ' fileinto :create "INBOX.Lists.${year}.${month}.dovecot";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["variables", "date", "fileinto", "mailbox"]);
+ });
+
+})();
diff --git a/tests/tests/sieve/SieveTty1Test.js b/tests/tests/sieve/SieveTty1Test.js
new file mode 100644
index 00000000..29c61bf8
--- /dev/null
+++ b/tests/tests/sieve/SieveTty1Test.js
@@ -0,0 +1,228 @@
+/*
+ * The contents of this file are licensed. You may obtain a copy of
+ * the license at https://github.com/thsmi/sieve/ or request it via
+ * email from the author.
+ *
+ * Do not remove or change this comment.
+ *
+ * The initial author of the code is:
+ * Thomas Schmid <schmid-thomas@gmx.net>
+ *
+ */
+
+(function () {
+
+ "use strict";
+
+ /* global net */
+
+ let suite = net.tschmid.yautt.test;
+
+ if (!suite)
+ throw new Error("Could not initialize test suite");
+
+
+ suite.add(function () {
+ suite.log("Examples from tty1.net...");
+ suite.log("http://www.tty1.net/blog/2011-07-16-sieve-tutorial_en.html");
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example I");
+
+ let script = ''
+ + 'require ["fileinto", "reject"];\r\n'
+ + '\r\n'
+ + '# Daffy Duck is a good friend of mine.\r\n'
+ + 'if address :is "from" "daffy.duck@example.com"\r\n'
+ + '{\r\n'
+ + ' fileinto "friends";\r\n'
+ + '}\r\n'
+ + '\r\n'
+ + '# Reject mails from the hunting enthusiasts at example.com.\r\n'
+ + 'if header :contains "list-id" "<duck-hunting.example.com>"\r\n'
+ + '{\r\n'
+ + ' reject "No violence, please";\r\n'
+ + '}\r\n'
+ + '\r\n'
+ + '# The command "keep" is executed automatically, if no other action is taken.\r\n';
+
+ suite.expectValidScript(script, ["fileinto", "reject"]);
+ });
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example II");
+
+ let script = ''
+ + '# The hash character starts a one-line comment.\r\n'
+ + '# Everything after a # character until the end of line is ignored.\r\n'
+ + '\r\n'
+ + '/* this is a bracketed (C-style) comment. This type of comment can stretch\r\n'
+ + ' * over many lines. A bracketed comment begins with a forward slash, followed\r\n'
+ + ' * by an asterisk and ends with the inverse sequence: an asterisk followed\r\n'
+ + ' * by a forward slash. */\r\n';
+
+ suite.expectValidScript(script, []);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example III");
+
+ let script = ''
+ + 'require ["fileinto"];\r\n'
+ + '\r\n'
+ + '# The two test below are equivalent;\r\n'
+ + '# The first variant is clearer and probably also more efficient.\r\n'
+ + 'if address :is :domain "to" "example.com"\r\n'
+ + '{\r\n'
+ + ' fileinto "examplecom";\r\n'
+ + '}\r\n'
+ + 'if address :matches :all "to" "*@example.com"\r\n'
+ + '{\r\n'
+ + ' fileinto "examplecom";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example IV");
+
+ let script = ''
+ + 'require ["fileinto"];\r\n'
+ + '\r\n'
+ + '# File mails with a Spamassassin score of 4.0 or more\r\n'
+ + '# into the "junk" folder.\r\n'
+ + 'if header :contains "x-spam-level" "****"\r\n'
+ + '{\r\n'
+ + ' fileinto "junk";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example V");
+
+ let script = ''
+ + 'require ["reject"];\r\n'
+ + '\r\n'
+ + '# Reject all messages that contain the string "viagra"in the Subject.\r\n'
+ + 'if header :contains "subject" "viagra"\r\n'
+ + '{\r\n'
+ + ' reject "go away!";\r\n'
+ + '}\r\n'
+ + '# Silently discard all messages sent from the tax man\r\n'
+ + 'elsif address :matches :domain "from" "*hmrc.gov.uk"\r\n'
+ + '{\r\n'
+ + ' discard;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["reject"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example VI");
+
+ let script = ''
+ + 'require ["fileinto"];\r\n'
+ + '\r\n'
+ + '# A mail to any of the recipients in the list of strings is filed to the folder "friends".\r\n'
+ + 'if address :is "from" ["daffy.duck@example.com", "porky.pig@example.com", "speedy.gonzales@example.com"]\r\n'
+ + '{\r\n'
+ + ' fileinto "friends";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example VII");
+
+ let script = ''
+ + 'require ["fileinto"];\r\n'
+ + '\r\n'
+ + '# Check if either the "from" or the "sender" header is from Porky.\r\n'
+ + 'if address :is ["from", "sender"] "porky.pig@example.com"\r\n'
+ + '{\r\n'
+ + ' fileinto "friends";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example VIII");
+
+ let script = ''
+ + 'require ["fileinto"];\r\n'
+ + '\r\n'
+ + '# Match "from" or the "sender" file with any of Daffy, Porky or Speedy.\r\n'
+ + 'if address :is ["from", "sender"] ["daffy.duck@example.com", "porky.pig@example.com", "speedy.gonzales@example.com"]\r\n'
+ + '{\r\n'
+ + ' fileinto "friends";\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, ["fileinto"]);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example IX");
+
+ let script = ''
+ + '# This test checks against Spamassassin\'s header fields:\r\n'
+ + '# If the spam level ls 4 or more and the Subject contains too\r\n'
+ + '# many illegal characters, then silently discard the mail.\r\n'
+ + 'if allof (header :contains "X-Spam-Level" "****",\r\n'
+ + ' header :contains "X-Spam-Report" "FROM_ILLEGAL_CHARS")\r\n'
+ + '{\r\n'
+ + ' discard;\r\n'
+ + '}\r\n'
+ + '# Discard mails that do not have a Date: or From: header field\r\n'
+ + '# or mails that are sent from the marketing department at example.com.\r\n'
+ + 'elsif anyof (not exists ["from", "date"],\r\n'
+ + ' header :contains "from" "marketing@example.com") {\r\n'
+ + ' discard;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, []);
+ });
+
+
+ suite.add(function () {
+
+ suite.log("TTY1 Example X");
+
+ let script = ''
+ + '# Delete messages greater than half a MB\r\n'
+ + 'if size :over 500K\r\n'
+ + '{\r\n'
+ + ' discard;\r\n'
+ + '}\r\n'
+ + '# Also delete small mails, under 1k\r\n'
+ + 'if size :under 1k\r\n'
+ + '{\r\n'
+ + ' discard;\r\n'
+ + '}\r\n';
+
+ suite.expectValidScript(script, []);
+ });
+
+})();
diff --git a/tests/tests/tests.js b/tests/tests/tests.js
index b88cf157..3d7a4dec 100755
--- a/tests/tests/tests.js
+++ b/tests/tests/tests.js
@@ -254,6 +254,35 @@
});
suite.add({
+ "examples-dovecot": {
+ script: "./sieve/SieveDovecotTest.js",
+ extend: "rfc5228",
+ require: [
+ "./../common/libSieve/imapflags/logic/SieveImapFlags.js",
+ "./../common/libSieve/relational/logic/SieveRelational.js",
+ "./../common/libSieve/regex/logic/SieveRegularExpression.js",
+ "./../common/libSieve/include/logic/SieveInclude.js",
+ "./../common/libSieve/spamtest/logic/SieveSpamtest.js",
+ "./../common/libSieve/subaddress/logic/SieveSubaddress.js",
+ "./../common/libSieve/variables/logic/SieveVariables.js",
+ "./../common/libSieve/vacation/logic/SieveVacation.js",
+ "./../common/libSieve/date/logic/SieveDate.js",
+ "./../common/libSieve/mailbox/logic/SieveMailbox.js"
+ ]
+ }
+ });
+
+ suite.add({
+ "examples-tty1": {
+ script: "./sieve/SieveTty1Test.js",
+ extend: "rfc5228",
+ require: [
+ "./../common/libSieve/reject/logic/SieveReject.js"
+ ]
+ }
+ });
+
+ suite.add({
"managesieve": {
script: "./managesieve/ManageSieveTest.js",
agents: ["Firefox"],