1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176
|
"""
Unit tests for the SIEVE language parser.
"""
import unittest
import os.path
import codecs
import io
from sievelib.parser import Parser
from sievelib.factory import FiltersSet
import sievelib.commands
class MytestCommand(sievelib.commands.ActionCommand):
args_definition = [
{
"name": "testtag",
"type": ["tag"],
"write_tag": True,
"values": [":testtag"],
"extra_arg": {"type": "number", "required": False},
"required": False,
},
{"name": "recipients", "type": ["string", "stringlist"], "required": True},
]
class Quota_notificationCommand(sievelib.commands.ActionCommand):
args_definition = [
{
"name": "subject",
"type": ["tag"],
"write_tag": True,
"values": [":subject"],
"extra_arg": {"type": "string"},
"required": False,
},
{
"name": "recipient",
"type": ["tag"],
"write_tag": True,
"values": [":recipient"],
"extra_arg": {"type": "stringlist"},
"required": True,
},
]
class SieveTest(unittest.TestCase):
def setUp(self):
self.parser = Parser()
def __checkCompilation(self, script, result):
self.assertEqual(self.parser.parse(script), result)
def compilation_ok(self, script, **kwargs):
self.__checkCompilation(script, True, **kwargs)
def compilation_ko(self, script):
self.__checkCompilation(script, False)
def representation_is(self, content):
target = io.StringIO()
self.parser.dump(target)
repr_ = target.getvalue()
target.close()
self.assertEqual(repr_, content.lstrip())
def sieve_is(self, content):
filtersset = FiltersSet("Testfilterset")
filtersset.from_parser_result(self.parser)
target = io.StringIO()
filtersset.tosieve(target)
repr_ = target.getvalue()
target.close()
self.assertEqual(repr_, content)
class AdditionalCommands(SieveTest):
def test_add_command(self):
self.assertRaises(
sievelib.commands.UnknownCommand,
sievelib.commands.get_command_instance,
"mytest",
)
sievelib.commands.add_commands(MytestCommand)
sievelib.commands.get_command_instance("mytest")
self.compilation_ok(
b"""
mytest :testtag 10 ["testrecp1@example.com"];
"""
)
def test_quota_notification(self):
sievelib.commands.add_commands(Quota_notificationCommand)
quota_notification_sieve = """# Filter: Testrule\nquota_notification :subject "subject here" :recipient ["somerecipient@example.com"];\n"""
self.compilation_ok(quota_notification_sieve)
self.sieve_is(quota_notification_sieve)
class ValidEncodings(SieveTest):
def test_utf8_file(self):
utf8_sieve = os.path.join(os.path.dirname(__file__), "files", "utf8_sieve.txt")
with codecs.open(utf8_sieve, encoding="utf8") as fobj:
source_sieve = fobj.read()
self.parser.parse_file(utf8_sieve)
self.sieve_is(source_sieve)
class ValidSyntaxes(SieveTest):
def test_hash_comment(self):
self.compilation_ok(
b"""
if size :over 100k { # this is a comment
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
size (type: test)
:over
100k
discard (type: action)
"""
)
def test_bracket_comment(self):
self.compilation_ok(
b"""
if size :over 100K { /* this is a comment
this is still a comment */ discard /* this is a comment
*/ ;
}
"""
)
self.representation_is(
"""
if (type: control)
size (type: test)
:over
100K
discard (type: action)
"""
)
def test_string_with_bracket_comment(self):
self.compilation_ok(
b"""
if header :contains "Cc" "/* comment */" {
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
header (type: test)
:contains
"Cc"
"/* comment */"
discard (type: action)
"""
)
def test_multiline_string(self):
self.compilation_ok(
b"""
require "reject";
if allof (false, address :is ["From", "Sender"] ["blka@bla.com"]) {
reject text:
noreply
============================
Your email has been canceled
============================
.
;
stop;
} else {
reject text:
================================
Your email has been canceled too
================================
.
;
}
"""
)
self.representation_is(
"""
require (type: control)
"reject"
if (type: control)
allof (type: test)
false (type: test)
address (type: test)
:is
["From","Sender"]
["blka@bla.com"]
reject (type: action)
text:
noreply
============================
Your email has been canceled
============================
.
stop (type: action)
else (type: control)
reject (type: action)
text:
================================
Your email has been canceled too
================================
.
"""
)
def test_complex_allof_with_not(self):
"""Test for allof/anyof commands including a not test.
See https://github.com/tonioo/sievelib/issues/69.
"""
self.compilation_ok(
b"""
require ["fileinto", "reject"];
if allof (not allof (address :is ["From","sender"] ["test1@test2.priv","test2@test2.priv"], header :matches "Subject" "INACTIVE*"), address :is "From" "user3@test3.priv")
{
reject;
}
"""
)
self.representation_is(
"""
require (type: control)
["fileinto","reject"]
if (type: control)
allof (type: test)
not (type: test)
allof (type: test)
address (type: test)
:is
["From","sender"]
["test1@test2.priv","test2@test2.priv"]
header (type: test)
:matches
"Subject"
"INACTIVE*"
address (type: test)
:is
"From"
"user3@test3.priv"
reject (type: action)
"""
)
def test_nested_blocks(self):
self.compilation_ok(
b"""
if header :contains "Sender" "example.com" {
if header :contains "Sender" "me@" {
discard;
} elsif header :contains "Sender" "you@" {
keep;
}
}
"""
)
self.representation_is(
"""
if (type: control)
header (type: test)
:contains
"Sender"
"example.com"
if (type: control)
header (type: test)
:contains
"Sender"
"me@"
discard (type: action)
elsif (type: control)
header (type: test)
:contains
"Sender"
"you@"
keep (type: action)
"""
)
def test_true_test(self):
self.compilation_ok(
b"""
if true {
}
"""
)
self.representation_is(
"""
if (type: control)
true (type: test)
"""
)
def test_rfc5228_extended(self):
self.compilation_ok(
b"""
#
# Example Sieve Filter
# Declare any optional features or extension used by the script
#
require ["fileinto"];
#
# Handle messages from known mailing lists
# Move messages from IETF filter discussion list to filter mailbox
#
if header :is "Sender" "owner-ietf-mta-filters@imc.org"
{
fileinto "filter"; # move to "filter" mailbox
}
#
# Keep all messages to or from people in my company
#
elsif address :domain :is ["From", "To"] "example.com"
{
keep; # keep in "In" mailbox
}
#
# Try and catch unsolicited email. If a message is not to me,
# or it contains a subject known to be spam, file it away.
#
elsif anyof (NOT address :all :contains
["To", "Cc", "Bcc"] "me@example.com",
header :matches "subject"
["*make*money*fast*", "*university*dipl*mas*"])
{
fileinto "spam"; # move to "spam" mailbox
}
else
{
# Move all other (non-company) mail to "personal"
# mailbox.
fileinto "personal";
}
"""
)
self.representation_is(
"""
require (type: control)
["fileinto"]
if (type: control)
header (type: test)
:is
"Sender"
"owner-ietf-mta-filters@imc.org"
fileinto (type: action)
"filter"
elsif (type: control)
address (type: test)
:domain
:is
["From","To"]
"example.com"
keep (type: action)
elsif (type: control)
anyof (type: test)
not (type: test)
address (type: test)
:all
:contains
["To","Cc","Bcc"]
"me@example.com"
header (type: test)
:matches
"subject"
["*make*money*fast*","*university*dipl*mas*"]
fileinto (type: action)
"spam"
else (type: control)
fileinto (type: action)
"personal"
"""
)
def test_explicit_comparator(self):
self.compilation_ok(
b"""
if header :contains :comparator "i;octet" "Subject" "MAKE MONEY FAST" {
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
header (type: test)
:comparator
"i;octet"
:contains
"Subject"
"MAKE MONEY FAST"
discard (type: action)
"""
)
def test_non_ordered_args(self):
self.compilation_ok(
b"""
if address :all :is "from" "tim@example.com" {
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
address (type: test)
:all
:is
"from"
"tim@example.com"
discard (type: action)
"""
)
def test_multiple_not(self):
self.compilation_ok(
b"""
if not not not not true {
stop;
}
"""
)
self.representation_is(
"""
if (type: control)
not (type: test)
not (type: test)
not (type: test)
not (type: test)
true (type: test)
stop (type: action)
"""
)
def test_just_one_command(self):
self.compilation_ok(b"keep;")
self.representation_is(
"""
keep (type: action)
"""
)
def test_singletest_testlist(self):
self.compilation_ok(
b"""
if anyof (true) {
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
anyof (type: test)
true (type: test)
discard (type: action)
"""
)
def test_multitest_testlist(self):
self.compilation_ok(
b"""
if anyof(allof(address :contains "From" ""), allof(header :contains "Subject" "")) {}
"""
)
def test_truefalse_testlist(self):
self.compilation_ok(
b"""
if anyof(true, false) {
discard;
}
"""
)
self.representation_is(
"""
if (type: control)
anyof (type: test)
true (type: test)
false (type: test)
discard (type: action)
"""
)
def test_vacationext_basic(self):
self.compilation_ok(
b"""
require "vacation";
if header :contains "subject" "cyrus" {
vacation "I'm out -- send mail to cyrus-bugs";
} else {
vacation "I'm out -- call me at +1 304 555 0123";
}
"""
)
def test_vacationext_medium(self):
self.compilation_ok(
b"""
require "vacation";
if header :contains "subject" "lunch" {
vacation :handle "ran-away" "I'm out and can't meet for lunch";
} else {
vacation :handle "ran-away" "I'm out";
}
"""
)
def test_vacationext_with_limit(self):
self.compilation_ok(
b"""
require "vacation";
vacation :days 23 :addresses ["tjs@example.edu",
"ts4z@landru.example.edu"]
"I'm away until October 19.
If it's an emergency, call 911, I guess." ;
"""
)
def test_vacationext_with_single_mail_address(self):
self.compilation_ok(
"""
require "vacation";
vacation :days 23 :addresses "tjs@example.edu"
"I'm away until October 19.
If it's an emergency, call 911, I guess." ;
"""
)
def test_vacationext_with_multiline(self):
self.compilation_ok(
b"""
require "vacation";
vacation :mime text:
Content-Type: multipart/alternative; boundary=foo
--foo
I'm at the beach relaxing. Mmmm, surf...
--foo
Content-Type: text/html; charset=us-ascii
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"
"http://www.w3.org/TR/REC-html40/strict.dtd">
<HTML><HEAD><TITLE>How to relax</TITLE>
<BASE HREF="http://home.example.com/pictures/"></HEAD>
<BODY><P>I'm at the <A HREF="beach.gif">beach</A> relaxing.
Mmmm, <A HREF="ocean.gif">surf</A>...
</BODY></HTML>
--foo--
.
;
"""
)
def test_vacation_seconds(self):
self.compilation_ok(
"""
require ["vacation", "vacation-seconds"];
vacation :seconds 10 :addresses ["test@example.org"] "Gone";
"""
)
def test_reject_extension(self):
self.compilation_ok(
b"""
require "reject";
if header :contains "subject" "viagra" {
reject;
}
"""
)
def test_fileinto_create(self):
self.compilation_ok(
b"""require ["fileinto", "mailbox"];
if header :is "Sender" "owner-ietf-mta-filters@imc.org"
{
fileinto :create "filter"; # move to "filter" mailbox
}
"""
)
def test_imap4flags_extension(self):
self.compilation_ok(
rb"""
require ["fileinto", "imap4flags", "variables"];
if size :over 1M {
addflag "MyFlags" "Big";
if header :is "From" "boss@company.example.com" {
# The message will be marked as "\Flagged Big" when filed into
# mailbox "Big messages"
addflag "MyFlags" "\\Flagged";
}
fileinto :flags "${MyFlags}" "Big messages";
}
"""
)
def test_imap4flags_hasflag(self):
self.compilation_ok(
b"""
require ["imap4flags", "fileinto"];
if hasflag ["test", "toto"] {
fileinto "Test";
}
addflag "Var1" "Truc";
if hasflag "Var1" "Truc" {
fileinto "Truc";
}
"""
)
def test_body_extension(self):
self.compilation_ok(
b"""
require ["body", "fileinto"];
if body :content "text" :contains ["missile", "coordinates"] {
fileinto "secrets";
}
"""
)
self.compilation_ok(
b"""
require "body";
if body :raw :contains "MAKE MONEY FAST" {
discard;
}
"""
)
self.compilation_ok(
b"""
require ["body", "fileinto"];
# Save messages mentioning the project schedule in the
# project/schedule folder.
if body :text :contains "project schedule" {
fileinto "project/schedule";
}
"""
)
class InvalidSyntaxes(SieveTest):
def test_nested_comments(self):
self.compilation_ko(
b"""
/* this is a comment /* with a nested comment inside */
it is allowed by the RFC :p */
"""
)
def test_nonopened_block(self):
self.compilation_ko(
b"""
if header :is "Sender" "me@example.com"
discard;
}
"""
)
def test_nonclosed_block(self):
self.compilation_ko(
b"""
if header :is "Sender" "me@example.com" {
discard;
"""
)
def test_nonopened_parenthesis(self):
self.compilation_ko(
b"""
if header :is "Sender" "me@example.com") {
discard;
}
"""
)
def test_nonopened_block2(self):
self.compilation_ko(b"""}""")
def test_unknown_token(self):
self.compilation_ko(
b"""
if header :is "Sender" "Toto" & header :contains "Cc" "Tata" {
}
"""
)
def test_empty_string_list(self):
self.compilation_ko(b"require [];")
def test_unopened_string_list(self):
self.compilation_ko(b'require "fileinto"];')
def test_unclosed_string_list(self):
self.compilation_ko(b'require ["toto", "tata";')
def test_misplaced_comma_in_string_list(self):
self.compilation_ko(b'require ["toto",];')
def test_nonopened_tests_list(self):
self.compilation_ko(
b"""
if anyof header :is "Sender" "me@example.com",
header :is "Sender" "myself@example.com") {
fileinto "trash";
}
"""
)
def test_nonclosed_tests_list(self):
self.compilation_ko(
b"""
if anyof (header :is "Sender" "me@example.com",
header :is "Sender" "myself@example.com" {
fileinto "trash";
}
"""
)
def test_nonclosed_tests_list2(self):
self.compilation_ko(
b"""
if anyof (header :is "Sender" {
fileinto "trash";
}
"""
)
def test_misplaced_comma_in_tests_list(self):
self.compilation_ko(
b"""
if anyof (header :is "Sender" "me@example.com",) {
}
"""
)
def test_comma_inside_arguments(self):
self.compilation_ko(
b"""
require "fileinto", "enveloppe";
"""
)
def test_non_ordered_args(self):
self.compilation_ko(
b"""
if address "From" :is "tim@example.com" {
discard;
}
"""
)
def test_extra_arg(self):
self.compilation_ko(
b"""
if address :is "From" "tim@example.com" "tutu" {
discard;
}
"""
)
def test_empty_not(self):
self.compilation_ko(
b"""
if not {
discard;
}
"""
)
def test_missing_semicolon(self):
self.compilation_ko(
b"""
require ["fileinto"]
"""
)
def test_missing_semicolon_in_block(self):
self.compilation_ko(
b"""
if true {
stop
}
"""
)
def test_misplaced_parenthesis(self):
self.compilation_ko(
b"""
if (true) {
}
"""
)
def test_control_command_in_test(self):
self.compilation_ko(
b"""
if stop;
"""
)
def test_extra_test_in_simple_control(self):
self.compilation_ko(
b"""
if address "From" "example.com" header "Subject" "Example" { stop; }
"""
)
def test_missing_comma_in_test_list(self):
self.compilation_ko(
b"""
if allof(anyof(address "From" "example.com") header "Subject" "Example") { stop; }
"""
)
def test_vacation_seconds_no_arg(self):
self.compilation_ko(
"""
require ["vacation", "vacation-seconds"];
vacation :seconds :addresses ["test@example.org"] "Gone";
"""
)
class LanguageRestrictions(SieveTest):
def test_unknown_control(self):
self.compilation_ko(
b"""
macommande "Toto";
"""
)
def test_misplaced_elsif(self):
self.compilation_ko(
b"""
elsif true {
}
"""
)
def test_misplaced_elsif2(self):
self.compilation_ko(
b"""
elsif header :is "From" "toto" {
}
"""
)
def test_misplaced_nested_elsif(self):
self.compilation_ko(
b"""
if true {
elsif false {
}
}
"""
)
def test_unexpected_argument(self):
self.compilation_ko(b'stop "toto";')
def test_bad_arg_value(self):
self.compilation_ko(
b"""
if header :isnot "Sent" "me@example.com" {
stop;
}
"""
)
def test_bad_arg_value2(self):
self.compilation_ko(
b"""
if header :isnot "Sent" 10000 {
stop;
}
"""
)
def test_bad_comparator_value(self):
self.compilation_ko(
b"""
if header :contains :comparator "i;prout" "Subject" "MAKE MONEY FAST" {
discard;
}
"""
)
def test_not_included_extension(self):
self.compilation_ko(
b"""
if header :contains "Subject" "MAKE MONEY FAST" {
fileinto "spam";
}
"""
)
def test_test_outside_control(self):
self.compilation_ko(b"true;")
def test_fileinto_create_without_mailbox(self):
self.compilation_ko(
b"""require ["fileinto"];
if header :is "Sender" "owner-ietf-mta-filters@imc.org"
{
fileinto :create "filter"; # move to "filter" mailbox
}
"""
)
self.assertEqual(self.parser.error, "line 4: extension 'mailbox' not loaded")
def test_fileinto_create_without_fileinto(self):
self.compilation_ko(
b"""require ["mailbox"];
if header :is "Sender" "owner-ietf-mta-filters@imc.org"
{
fileinto :create "filter"; # move to "filter" mailbox
}
"""
)
self.assertEqual(self.parser.error, "line 4: extension 'fileinto' not loaded")
def test_unknown_command(self):
self.compilation_ko(
b"""require ["mailbox"];
if header :is "Sender" "owner-ietf-mta-filters@imc.org"
{
foobar :create "filter"; # move to "filter" mailbox
}
"""
)
self.assertEqual(self.parser.error, "line 4: unknown command 'foobar'")
def test_exists_get_string_or_list(self):
self.compilation_ok(
b"""
if exists "subject"
{
discard;
}
"""
)
self.compilation_ok(
b"""
if exists ["subject"]
{
discard;
}
"""
)
class DateCommands(SieveTest):
def test_date_command(self):
self.compilation_ok(
b"""require ["date", "relational", "fileinto"];
if allof(header :is "from" "boss@example.com",
date :value "ge" :originalzone "date" "hour" "09",
date :value "lt" :originalzone "date" "hour" "17")
{ fileinto "urgent"; }
"""
)
def test_currentdate_command(self):
self.compilation_ok(
b"""require ["date", "relational"];
if allof(currentdate :value "ge" "date" "2013-10-23",
currentdate :value "le" "date" "2014-10-12")
{
discard;
}
"""
)
def test_currentdate_command_timezone(self):
self.compilation_ok(
b"""require ["date", "relational"];
if allof(currentdate :zone "+0100" :value "ge" "date" "2013-10-23",
currentdate :value "le" "date" "2014-10-12")
{
discard;
}
"""
)
def test_currentdate_norel(self):
self.compilation_ok(
b"""require ["date"];
if allof (
currentdate :zone "+0100" :is "date" "2013-10-23"
)
{
discard;
}"""
)
def test_currentdate_extension_not_loaded(self):
self.compilation_ko(
b"""require ["date"];
if allof ( currentdate :value "ge" "date" "2013-10-23" , currentdate :value "le" "date" "2014-10-12" )
{
discard;
}
"""
)
class VariablesCommands(SieveTest):
def test_set_command(self):
self.compilation_ok(
b"""require ["variables"];
set "matchsub" "testsubject";
if allof (
header :contains ["Subject"] "${header}"
)
{
discard;
}
"""
)
class CopyWithoutSideEffectsTestCase(SieveTest):
"""RFC3894 test cases."""
def test_redirect_with_copy(self):
self.compilation_ko(
b"""
if header :contains "subject" "test" {
redirect :copy "dev@null.com";
}
"""
)
self.compilation_ok(
b"""require "copy";
if header :contains "subject" "test" {
redirect :copy "dev@null.com";
}
"""
)
def test_fileinto_with_copy(self):
self.compilation_ko(
b"""require "fileinto";
if header :contains "subject" "test" {
fileinto :copy "Spam";
}
"""
)
self.assertEqual(self.parser.error, "line 3: extension 'copy' not loaded")
self.compilation_ok(
b"""require ["fileinto", "copy"];
if header :contains "subject" "test" {
fileinto :copy "Spam";
}
"""
)
class RegexMatchTestCase(SieveTest):
def test_header_regex(self):
self.compilation_ok(
b"""require "regex";
if header :regex "Subject" "^Test" {
discard;
}
"""
)
def test_header_regex_no_middle(self):
self.compilation_ko(
b"""require "regex";
if header "Subject" :regex "^Test" {
discard;
}
"""
)
def test_envelope_regex(self):
self.compilation_ok(
b"""require ["regex","envelope"];
if envelope :regex "from" "^test@example\\.org$" {
discard;
}
"""
)
def test_envelope_regex_no_middle(self):
self.compilation_ko(
b"""require "regex";
if envelope "from" :regex "^test@example\\.org$" {
discard;
}
"""
)
def test_address_regex(self):
self.compilation_ok(
b"""require "regex";
if address :regex "from" "^test@example\\.org$" {
discard;
}
"""
)
def test_address_regex_no_middle(self):
self.compilation_ko(
b"""require "regex";
if address "from" :regex "^test@example\\.org$" {
discard;
}
"""
)
def test_body_raw_regex(self):
self.compilation_ok(
b"""require ["body", "regex"];
if body :raw :regex "Sample" {
discard;
}
"""
)
def test_body_content_regex(self):
self.compilation_ok(
b"""require ["body", "regex"];
if body :content "text" :regex "Sample" {
discard;
}
"""
)
if __name__ == "__main__":
unittest.main()
|