~ubuntu-branches/ubuntu/utopic/spamassassin/utopic-updates

« back to all changes in this revision

Viewing changes to t/dkim.t

  • Committer: Package Import Robot
  • Author(s): Noah Meyerhans
  • Date: 2014-02-14 22:45:15 UTC
  • mfrom: (0.8.1) (0.6.2) (5.1.22 sid)
  • Revision ID: package-import@ubuntu.com-20140214224515-z1es2twos8xh7n2y
Tags: 3.4.0-1
* New upstream version! (Closes: 738963, 738872, 738867)
* Scrub the environment when switching to the debian-spamd user in
  postinst and cron.daily. (Closes: 738951)
* Enhancements to postinst to better manage ownership of
  /var/lib/spamassassin, via Iain Lane <iain.lane@canonical.com>
  (Closes: 738974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/perl
2
2
 
 
3
use strict;
 
4
use warnings;
 
5
use re 'taint';
3
6
use lib '.'; use lib 't';
 
7
 
4
8
use SATest; sa_t_init("dkim");
5
9
use Test;
6
10
 
7
 
use constant num_tests => 21;
 
11
use vars qw(%patterns %anti_patterns);
 
12
 
 
13
use constant num_tests => 199;
8
14
 
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;
14
 
 
15
 
# Since the plugin is disabled by default, so are the tests
16
 
 
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 :(
21
 
 
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;
 
19
};
 
20
 
 
21
use constant DO_RUN => TEST_ENABLED && HAS_MODULES;
23
22
 
24
23
BEGIN {
 
24
  if (-e 't/test_dir') {
 
25
    chdir 't';
 
26
  }
 
27
 
 
28
  if (-e 'test_dir') {
 
29
    unshift(@INC, '../blib/lib');
 
30
  }
25
31
  
26
32
  plan tests => (DO_RUN ? num_tests : 0);
27
 
 
28
33
};
29
34
 
30
35
exit unless (DO_RUN);
31
36
 
 
37
my $prefix = '.';
 
38
if (-e 'test_dir') {            # running from test directory, not ..
 
39
  $prefix = '..';
 
40
}
 
41
 
 
42
use IO::File;
 
43
use Mail::SpamAssassin;
 
44
 
 
45
 
32
46
# ---------------------------------------------------------------------------
33
 
 
34
 
# ensure all rules will fire
35
 
tstlocalrules ("
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
41
 
 
 
47
my $spamassassin_obj;
 
48
 
 
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);
 
56
  if ($mail_obj) {
 
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);
 
59
  }
 
60
  if ($per_msg_status) {
 
61
    $spam_report = $per_msg_status->get_tag('REPORT');
 
62
    $per_msg_status->finish;
 
63
  }
 
64
  if ($mail_obj) {
 
65
    $mail_obj->finish;
 
66
  }
 
67
  $fh->close or die "error closing file $fn: $!";
 
68
  $spam_report =~ s/\A(\s*\n)+//s;
 
69
# print "\t$spam_report\n";
 
70
  return $spam_report;
 
71
}
 
72
 
 
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;
 
87
  }
 
88
}
 
89
 
 
90
# ensure rules will fire, and disable some expensive ones
 
91
tstlocalrules("
 
92
  score DKIM_SIGNED          -0.1
 
93
  score DKIM_VALID           -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
 
103
  score RAZOR2_CHECK 0
 
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
42
107
");
43
108
 
44
 
# see DKIM corpus documentation at http://testing.dkim.org/documentation.html
45
 
# for details of the test messages....
46
 
#
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.
51
 
 
52
 
%patterns = (
53
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
54
 
);
55
 
sarun ("-t < data/nice/dkim/BasicTest_01", \&patterns_run_cb);
56
 
ok ok_all_patterns();
57
 
 
58
 
# skip this test; it fails under current releases of Mail::DKIM.
59
 
if (0) {
60
 
  %patterns = (
61
 
  q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
62
 
  );
63
 
  sarun ("-t < data/nice/dkim/Simple_02", \&patterns_run_cb);
64
 
  ok ok_all_patterns();
65
 
}
66
 
 
67
 
%patterns = (
68
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
69
 
);
70
 
sarun ("-t < data/nice/dkim/Nowsp_03", \&patterns_run_cb);
71
 
ok ok_all_patterns();
72
 
 
73
 
%patterns = (
74
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
75
 
);
76
 
sarun ("-t < data/nice/dkim/MIMEsimple_04", \&patterns_run_cb);
77
 
ok ok_all_patterns();
78
 
 
79
 
%patterns = (
80
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
81
 
);
82
 
sarun ("-t < data/nice/dkim/MIMEnowsp_05", \&patterns_run_cb);
83
 
ok ok_all_patterns();
84
 
 
85
 
%patterns = (
86
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
87
 
);
88
 
sarun ("-t < data/nice/dkim/MultipleSig_06", \&patterns_run_cb);
89
 
ok ok_all_patterns();
90
 
 
91
 
 
92
 
# Message with the presence of the "v=" tag (Message: AddedVtag_07)
93
 
 
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
101
 
# Policy'
102
 
#
103
 
# not yet tested -- Mail::DKIM fails this test, by calling this "invalid"
104
 
# instead of "neutral" or "fail"
105
 
 
106
 
if (0) {
107
 
  %patterns = (
108
 
  q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
109
 
  );
110
 
  sarun ("-t < data/nice/dkim/AddedVtag_07", \&patterns_run_cb);
111
 
  ok ok_all_patterns();
112
 
}
113
 
 
114
 
%patterns = (
115
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
116
 
);
117
 
sarun ("-t < data/nice/dkim/MultipleReceived_08", \&patterns_run_cb);
118
 
ok ok_all_patterns();
119
 
 
120
 
%patterns = (
121
 
q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
122
 
);
123
 
sarun ("-t < data/nice/dkim/NonExistingHeader_09", \&patterns_run_cb);
124
 
ok ok_all_patterns();
125
 
 
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. '
132
 
#
133
 
# no need to worry about this -- this is really an exceptional case
134
 
# and its up to Mail::DKIM how it handles it.
135
 
 
136
 
if (0) {
137
 
  %patterns = (
138
 
  q{ DKIM_SIGNED }, 'DKIM_SIGNED', q{ DKIM_VERIFIED }, 'DKIM_VERIFIED',
139
 
  );
140
 
  sarun ("-t < data/nice/dkim/MultipleAuthRes_10", \&patterns_run_cb);
141
 
  ok ok_all_patterns();
142
 
}
143
 
 
 
109
my $dirname = "data/dkim";
 
110
 
 
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,
 
116
  require_rules       => 1,
 
117
# debug               => 'dkim',
 
118
  post_config_text => q{
 
119
    use_auto_whitelist 0
 
120
    use_bayes 0
 
121
    use_razor2 0
 
122
    use_pyzor 0
 
123
    use_dcc 0
 
124
  },
 
125
});
 
126
ok($spamassassin_obj);
 
127
$spamassassin_obj->compile_now;  # try to preloaded most modules
 
128
 
 
129
my $version = Mail::DKIM::Verifier->VERSION;
 
130
print "Using Mail::DKIM version $version\n";
 
131
 
 
132
# mail samples test-pass* should all pass DKIM validation
 
133
my($fn, @test_filenames, @patt_antipatt_list);
 
134
local *DIR;
 
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");
 
140
}
 
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'
 
144
);
 
145
test_samples(\@test_filenames, \@patt_antipatt_list);
 
146
 
 
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);
 
150
 
 
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");
 
159
}
 
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);
 
163
 
 
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");
 
171
}
 
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
 
186
);
 
187
test_samples(\@test_filenames, \@patt_antipatt_list);
 
188
 
 
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";
 
196
}
 
197
 
 
198
END {
 
199
  $spamassassin_obj->finish  if $spamassassin_obj;
 
200
}