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

« back to all changes in this revision

Viewing changes to t/dkim2.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
 
#!/usr/bin/perl
2
 
 
3
 
use strict;
4
 
use warnings;
5
 
use re 'taint';
6
 
use lib '.'; use lib 't';
7
 
 
8
 
use SATest; sa_t_init("dkim2");
9
 
use Test;
10
 
 
11
 
use vars qw(%patterns %anti_patterns);
12
 
 
13
 
use constant num_tests => 199;
14
 
 
15
 
use constant TEST_ENABLED => conf_bool('run_net_tests');
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;
22
 
 
23
 
BEGIN {
24
 
  if (-e 't/test_dir') {
25
 
    chdir 't';
26
 
  }
27
 
 
28
 
  if (-e 'test_dir') {
29
 
    unshift(@INC, '../blib/lib');
30
 
  }
31
 
  
32
 
  plan tests => (DO_RUN ? num_tests : 0);
33
 
};
34
 
 
35
 
exit unless (DO_RUN);
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
 
 
46
 
# ---------------------------------------------------------------------------
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
107
 
");
108
 
 
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
 
}