Debian Codegraph @

t/plugin_tests/spamassassin

raw
1 #!perl -w
2
3 use strict;
4 use warnings;
5
6 use Mail::Header;
7 use Qpsmtpd::Address;
8 use Qpsmtpd::Constants;
9
10 my @sample_headers = (
11 'No, score=-5.4 required=4.0 autolearn=ham',
12 'No, score=-8.2 required=4.0 autolearn=ham',
13 'No, score=-102.3 required=4.0 autolearn=disabled',
14 'No, score=-0.1 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RDNS_NONE autolearn=no version=3.3.2',
15 'No, score=4.4 required=5.0 autolearn=no',
16 'Yes, score=14.3 required=5.0 autolearn=no',
17 'Yes, score=18.3 required=5.0 autolearn=spam',
18 'Yes, score=26.6 required=4.0 autolearn=unavailable',
19 'No, score=-1.7 required=4.0 autolearn=unavailable version=3.3.2',
20 'No, hits=-1.0 required=4.0 autolearn=unavailable version=3.3.2',
21 );
22
23 sub register_tests {
24 my $self = shift;
25
26 $self->register_test('test_connect_to_spamd', 4);
27 $self->register_test('test_parse_spam_header', 10);
28 $self->register_test('test_get_spam_results', 20);
29 $self->register_test('test_munge_subject', 4);
30 $self->register_test('test_reject', 2);
31 }
32
33 sub test_connect_to_spamd {
34 my $self = shift;
35
36 my $transaction = $self->qp->transaction;
37 $transaction->add_recipient( Qpsmtpd::Address->new( '<[email protected]>' ) );
38 my $username = $self->select_spamd_username( $transaction );
39 my $message = $self->test_message();
40 my $length = length $message;
41
42 # Try a unix socket
43 $self->{_args}{spamd_socket} = '/var/run/spamd/spamd.socket';
44 my $SPAMD = $self->connect_to_spamd();
45 if ( $SPAMD ) {
46 ok( $SPAMD, "socket");
47
48 $self->print_to_spamd( $SPAMD, $message, $length, $username );
49 shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done)
50 my $headers = $self->parse_spamd_response( $SPAMD );
51 #warn Data::Dumper::Dumper($headers);
52 ok( $headers, "socket response\n");
53 }
54 else {
55 ok( 1 == 1, "socket connect FAILED");
56 ok( 1 == 1, "socket response FAILED");
57 };
58
59 # Try a TCP/IP connection
60 $self->{_args}{spamd_socket} = '127.0.0.1:783';
61 $SPAMD = $self->connect_to_spamd();
62 if ( $SPAMD ) {
63 ok( $SPAMD, "tcp/ip");
64 #warn Data::Dumper::Dumper($SPAMD);
65 $self->print_to_spamd( $SPAMD, $message, $length, $username );
66 shutdown($SPAMD, 1); # close our side of the socket (tell spamd we're done)
67 my $headers = $self->parse_spamd_response( $SPAMD );
68 #warn Data::Dumper::Dumper($headers);
69 ok( $headers, "tcp/ip response\n");
70 }
71 else {
72 ok( 1 == 1, "tcp/ip connect FAILED");
73 ok( 1 == 1, "tcp/ip response FAILED");
74 };
75 };
76
77 sub test_reject {
78 my $self = shift;
79
80 my $transaction = $self->qp->transaction;
81 $self->setup_headers();
82
83 # message scored a 10, should pass
84 $self->{_args}{reject} = 12;
85 $transaction->notes('spamassassin', { is_spam => 'Yes', score => 10 } );
86 my $r = $self->reject($transaction);
87 cmp_ok( DECLINED, '==', $r, "r: $r");
88
89 # message scored a 15, should fail
90 $self->{_args}{reject} = 12;
91 $transaction->notes('spamassassin', { is_spam => 'Yes', score => 15 } );
92 ($r) = $self->reject($transaction);
93 cmp_ok( DENY, '==', $r, "r: $r");
94 };
95
96 sub test_munge_subject {
97 my $self = shift;
98
99 my $transaction = $self->qp->transaction;
100 $self->setup_headers();
101 my $subject = 'DSPAM smells better than SpamAssassin';
102
103 $self->{_args}{munge_subject_threshold} = 5;
104 $transaction->notes('spamassassin', { score => 6 } );
105 $transaction->header->add('Subject', $subject);
106 $self->munge_subject($transaction);
107 my $r = $transaction->header->get('Subject'); chomp $r;
108 cmp_ok($r, 'eq', "*** SPAM *** $subject", "+");
109
110 $transaction->header->delete('Subject'); # cleanup
111 $self->{_args}{munge_subject_threshold} = 5;
112 $transaction->notes('spamassassin', { score => 3 } );
113 $transaction->header->add('Subject', $subject);
114 $self->munge_subject($transaction);
115 $r = $transaction->header->get('Subject'); chomp $r;
116 cmp_ok($r, 'eq', $subject, "-");
117
118 $transaction->header->delete('Subject'); # cleanup
119 $transaction->notes('spamassassin', { score => 3, required => 4 } );
120 $transaction->header->add('Subject', $subject);
121 $self->munge_subject($transaction);
122 $r = $transaction->header->get('Subject'); chomp $r;
123 cmp_ok($r, 'eq', $subject, "-");
124
125 $transaction->header->delete('Subject'); # cleanup
126 $transaction->notes('spamassassin', { score => 5, required => 4 } );
127 $transaction->header->add('Subject', $subject);
128 $self->munge_subject($transaction);
129 $r = $transaction->header->get('Subject'); chomp $r;
130 cmp_ok($r, 'eq', "$subject", "+");
131 };
132
133 sub test_get_spam_results {
134 my $self = shift;
135
136 my $transaction = $self->qp->transaction;
137 $self->setup_headers();
138
139 foreach my $h ( @sample_headers ) {
140 $transaction->notes('spamassassin', undef); # empty cache
141 $transaction->header->delete('X-Spam-Status'); # delete previous header
142 $transaction->header->add('X-Spam-Status', $h);
143 my $r_ref = $self->get_spam_results($transaction);
144 if ( $h =~ /hits=/ ) {
145 $r_ref->{hits} = delete $r_ref->{score}; # SA v2 compat
146 };
147 my $r2 = _reassemble_header($r_ref);
148 cmp_ok( $h, 'eq', $r2, $h );
149
150 # this time it should be cached
151 $r_ref = $self->get_spam_results($transaction);
152 if ( $h =~ /hits=/ ) {
153 ok( 1 );
154 next;
155 }; # caching is broken for SA v2 headers
156 $r2 = _reassemble_header($r_ref);
157 cmp_ok( $h, 'eq', $r2, $h );
158 };
159 };
160
161 sub test_parse_spam_header {
162 my $self = shift;
163
164 foreach my $h ( @sample_headers ) {
165 my $r_ref = $self->parse_spam_header($h);
166 if ( $h =~ /hits=/ ) {
167 $r_ref->{hits} = delete $r_ref->{score}; # SA v2 compat
168 };
169 my $r2 = _reassemble_header($r_ref);
170 cmp_ok( $h, 'eq', $r2, $h );
171 };
172 };
173
174 sub setup_headers {
175 my $self = shift;
176
177 my $transaction = $self->qp->transaction;
178 my $header = Mail::Header->new(Modify => 0, MailFrom => "COERCE");
179 $transaction->header( $header );
180 };
181
182 sub test_message {
183 return <<'EO_MESSAGE'
184 To: Fictitious User <[email protected]>
185 From: No Such <[email protected]>
186 Subject: jose can you see, by the dawns early light?
187
188 What so proudly we.
189 EO_MESSAGE
190
191
192 };
193
194 sub _reassemble_header {
195 my $info_ref = shift;
196 my $string = $info_ref->{'is_spam'};
197 $string .= ",";
198 foreach ( qw/ hits score required tests autolearn version / ) {
199 next if ! defined $info_ref->{$_};
200 $string .= " $_=$info_ref->{$_}";
201 };
202 return $string;
203 };
204