3
6
use lib '.'; use lib 't';
4
8
use SATest; sa_t_init("dkim");
7
use constant num_tests => 21;
11
use vars qw(%patterns %anti_patterns);
13
use constant num_tests => 199;
9
15
use constant TEST_ENABLED => conf_bool('run_net_tests');
10
use constant HAS_MODULES => eval { require Mail::DKIM; require Mail::DKIM::Verifier; };
11
# use constant IS_LINUX => $^O eq 'linux';
12
# use constant IS_WINDOWS => ($^O =~ /^(mswin|dos|os2)/oi);
13
# use constant AM_ROOT => $< == 0;
15
# Since the plugin is disabled by default, so are the tests
17
# Dec 28 2007 jm: as Daryl notes at
18
# http://issues.apache.org/SpamAssassin/show_bug.cgi?id=5662#c25 , this test is
19
# broken, since dkim.org have removed the "testing1234" selector from their
20
# DNS. TODO: we need working DKIM tests :(
22
use constant DO_RUN => TEST_ENABLED && HAS_MODULES && 0;
16
use constant HAS_MODULES => eval {
17
require Mail::DKIM::Verifier;
18
Mail::DKIM::Verifier->VERSION >= 0.31;
21
use constant DO_RUN => TEST_ENABLED && HAS_MODULES;
24
if (-e 't/test_dir') {
29
unshift(@INC, '../blib/lib');
26
32
plan tests => (DO_RUN ? num_tests : 0);
30
35
exit unless (DO_RUN);
38
if (-e 'test_dir') { # running from test directory, not ..
43
use Mail::SpamAssassin;
32
46
# ---------------------------------------------------------------------------
34
# ensure all rules will fire
36
score DKIM_SIGNED -0.001
37
score DKIM_VERIFIED -0.001
38
score DKIM_POLICY_SIGNSOME 0.001
39
score DKIM_POLICY_SIGNALL 0.001
40
score DKIM_POLICY_TESTING 0.001
49
sub process_sample_file($) {
50
my($fn) = @_; # file name
51
my($mail_obj, $per_msg_status, $spam_report);
52
$spamassassin_obj->timer_reset;
53
my $fh = IO::File->new;
54
$fh->open($fn,'<') or die "cannot open file $fn: $!";
55
$mail_obj = $spamassassin_obj->parse($fh,0);
57
local($1,$2,$3,$4,$5,$6); # avoid Perl 5.8.x bug, $1 can get tainted
58
$per_msg_status = $spamassassin_obj->check($mail_obj);
60
if ($per_msg_status) {
61
$spam_report = $per_msg_status->get_tag('REPORT');
62
$per_msg_status->finish;
67
$fh->close or die "error closing file $fn: $!";
68
$spam_report =~ s/\A(\s*\n)+//s;
69
# print "\t$spam_report\n";
73
sub test_samples($$) {
74
my($test_filenames, $patt_antipatt_list) = @_;
75
for my $fn (sort { $a cmp $b } @$test_filenames) {
76
my $el = $patt_antipatt_list->[0];
77
shift @$patt_antipatt_list if @$patt_antipatt_list > 1; # last autorepeats
78
my($patt,$anti) = split(m{\s* / \s*}x, $el, 2);
79
%patterns = map { (" $_ ", $_) } split(' ',$patt);
80
%anti_patterns = map { (" $_ ", $_) } split(' ',$anti);
81
print "Testing sample $fn\n";
82
my $spam_report = process_sample_file($fn);
83
clear_pattern_counters();
84
patterns_run_cb($spam_report);
85
my $status = ok_all_patterns();
86
printf("\nTest on file %s failed:\n%s\n", $fn,$spam_report) if !$status;
90
# ensure rules will fire, and disable some expensive ones
92
score DKIM_SIGNED -0.1
94
score DKIM_VALID_AU -0.1
95
score DKIM_ADSP_NXDOMAIN 0.1
96
score DKIM_ADSP_DISCARD 0.1
97
score DKIM_ADSP_ALL 0.1
98
score DKIM_ADSP_CUSTOM_LOW 0.1
99
score DKIM_ADSP_CUSTOM_MED 0.1
100
score DKIM_ADSP_CUSTOM_HIGH 0.1
101
header DKIM_ADSP_SEL_TEST eval:check_dkim_adsp('*', .spamassassin.org)
102
score DKIM_ADSP_SEL_TEST 0.1
104
score RAZOR2_CF_RANGE_51_100 0
105
score RAZOR2_CF_RANGE_E4_51_100 0
106
score RAZOR2_CF_RANGE_E8_51_100 0
44
# see DKIM corpus documentation at http://testing.dkim.org/documentation.html
45
# for details of the test messages....
47
# TODO: we should use a test-config setting to control whether the
48
# testing.dkim.org message corpus is used, defaulting to off; and add a small
49
# set of test messages for general make test use; since otherwise "make test"
50
# in the field will wind up relying on the third-party DNS records at dkim.org.
53
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
55
sarun ("-t < data/nice/dkim/BasicTest_01", \&patterns_run_cb);
58
# skip this test; it fails under current releases of Mail::DKIM.
61
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
63
sarun ("-t < data/nice/dkim/Simple_02", \&patterns_run_cb);
68
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
70
sarun ("-t < data/nice/dkim/Nowsp_03", \&patterns_run_cb);
74
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
76
sarun ("-t < data/nice/dkim/MIMEsimple_04", \&patterns_run_cb);
80
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
82
sarun ("-t < data/nice/dkim/MIMEnowsp_05", \&patterns_run_cb);
86
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
88
sarun ("-t < data/nice/dkim/MultipleSig_06", \&patterns_run_cb);
92
# Message with the presence of the "v=" tag (Message: AddedVtag_07)
94
# '7. The draft states the following about the v= tag in the Signature: v=
95
# Verifiers MUST ignore DKIM-Signature header fields with a 'v=' tag. Existence
96
# of such a tag indicates a new, incompatible version of the DKIM-Signature
97
# header field. * The message present in the file "AddedVtag_07" is signed with
98
# the presence of the "v=" tag value set at "DKIM1". * The expected result is
99
# Authentication-Results: <your_verifying_machine>;
100
# header.From=mickey@dkim.org; dkim=neutral or fail; based on the Signing
103
# not yet tested -- Mail::DKIM fails this test, by calling this "invalid"
104
# instead of "neutral" or "fail"
108
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
110
sarun ("-t < data/nice/dkim/AddedVtag_07", \&patterns_run_cb);
111
ok ok_all_patterns();
115
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
117
sarun ("-t < data/nice/dkim/MultipleReceived_08", \&patterns_run_cb);
118
ok ok_all_patterns();
121
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
123
sarun ("-t < data/nice/dkim/NonExistingHeader_09", \&patterns_run_cb);
124
ok ok_all_patterns();
126
# '10. Presence of Multiple Authentication-Results headers (Message:
127
# MultipleAuthRes_10) The message file "MutlipleAuthRes_10" does NOT contain
128
# any DKIM Signature but carries two pre-existing invalid
129
# "Authentication-Results" headers. * There is no particular requirement on how
130
# multiple Authentication-Results headers should be handled. * Determine the
131
# behavior of your verifier with the presence of these multiple headers. '
133
# no need to worry about this -- this is really an exceptional case
134
# and its up to Mail::DKIM how it handles it.
138
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
140
sarun ("-t < data/nice/dkim/MultipleAuthRes_10", \&patterns_run_cb);
141
ok ok_all_patterns();
109
my $dirname = "data/dkim";
111
$spamassassin_obj = Mail::SpamAssassin->new({
112
rules_filename => "$prefix/t/log/test_rules_copy",
113
site_rules_filename => "$prefix/t/log/localrules.tmp",
114
userprefs_filename => "$prefix/masses/spamassassin/user_prefs",
115
dont_copy_prefs => 1,
118
post_config_text => q{
126
ok($spamassassin_obj);
127
$spamassassin_obj->compile_now; # try to preloaded most modules
129
my $version = Mail::DKIM::Verifier->VERSION;
130
print "Using Mail::DKIM version $version\n";
132
# mail samples test-pass* should all pass DKIM validation
133
my($fn, @test_filenames, @patt_antipatt_list);
135
opendir(DIR, $dirname) or die "Cannot open directory $dirname: $!";
136
while (defined($fn = readdir(DIR))) {
137
next if $fn eq '.' || $fn eq '..';
138
next if $fn !~ /^test-pass-\d*\.msg$/;
139
push(@test_filenames, "$dirname/$fn");
141
closedir(DIR) or die "Error closing directory $dirname: $!";
142
@patt_antipatt_list = (
143
'DKIM_SIGNED DKIM_VALID DKIM_VALID_AU / DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD DKIM_ADSP_ALL DKIM_ADSP_SEL_TEST'
145
test_samples(\@test_filenames, \@patt_antipatt_list);
147
# this mail sample is special, doesn't have any signature
148
@patt_antipatt_list = ( '/ DKIM_SIGNED DKIM_VALID' );
149
test_samples(["$dirname/test-fail-01.msg"], \@patt_antipatt_list);
151
# mail samples test-fail* should all fail DKIM validation
152
@test_filenames = ();
153
opendir(DIR, $dirname) or die "Cannot open directory $dirname: $!";
154
while (defined($fn = readdir(DIR))) {
155
next if $fn eq '.' || $fn eq '..';
156
next if $fn !~ /^test-fail-\d*\.msg$/;
157
next if $fn eq "test-fail-01.msg"; # no signature
158
push(@test_filenames, "$dirname/$fn");
160
closedir(DIR) or die "Error closing directory $dirname: $!";
161
@patt_antipatt_list = ( 'DKIM_SIGNED / DKIM_VALID' );
162
test_samples(\@test_filenames, \@patt_antipatt_list);
164
# mail samples test-adsp* should all fail DKIM validation, testing ADSP
165
@test_filenames = ();
166
opendir(DIR, $dirname) or die "Cannot open directory $dirname: $!";
167
while (defined($fn = readdir(DIR))) {
168
next if $fn eq '.' || $fn eq '..';
169
next if $fn !~ /^test-adsp-\d*\.msg$/;
170
push(@test_filenames, "$dirname/$fn");
172
closedir(DIR) or die "Error closing directory $dirname: $!";
173
@patt_antipatt_list = (
174
' / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD DKIM_ADSP_ALL', # 11
175
'DKIM_ADSP_NXDOMAIN / DKIM_VALID DKIM_ADSP_DISCARD DKIM_ADSP_ALL', # 12
176
'DKIM_ADSP_ALL / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD', # 13
177
'DKIM_ADSP_DISCARD / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_ALL', # 14
178
'DKIM_ADSP_DISCARD / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_ALL', # 15
179
' / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD DKIM_ADSP_ALL', # 16 foo
180
' / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD DKIM_ADSP_ALL', # 17 unk
181
'DKIM_ADSP_ALL / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_DISCARD', # 18 all
182
'DKIM_ADSP_DISCARD / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_ALL', # 19 dis
183
'DKIM_ADSP_DISCARD / DKIM_VALID DKIM_ADSP_NXDOMAIN DKIM_ADSP_ALL', # 20 di2
184
'DKIM_ADSP_DISCARD / DKIM_VALID DKIM_ADSP_ALL', # 21 nxd
185
'DKIM_ADSP_NXDOMAIN / DKIM_VALID DKIM_ADSP_DISCARD DKIM_ADSP_ALL', # 22 xxx
187
test_samples(\@test_filenames, \@patt_antipatt_list);
189
STDOUT->autoflush(1);
190
if ($version < 0.34) {
191
print STDERR "\n\n*** Mail::DKIM $version, Tests 105, 109, 113, 117, 120 ".
192
"are expected to fail with versions older than 0.34\n\n";
193
} elsif ($version < 0.37) {
194
print STDERR "\n\n*** Mail::DKIM $version, Test 120 ".
195
"is expected to fail with versions older than 0.36_5\n\n";
199
$spamassassin_obj->finish if $spamassassin_obj;