~ubuntu-branches/ubuntu/utopic/spamassassin/utopic-proposed

« back to all changes in this revision

Viewing changes to t/spamd_client.t

  • Committer: Bazaar Package Importer
  • Author(s): Noah Meyerhans
  • Date: 2010-01-26 22:53:12 UTC
  • mfrom: (1.1.13 upstream) (5.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100126225312-wkftb10idc1kz2aq
Tags: 3.3.0-1
* New upstream version.
* Switch to dpkg-source 3.0 (quilt) format

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
 
 
3
BEGIN {
 
4
  if (-e 't/test_dir') { # if we are running "t/rule_tests.t", kluge around ...
 
5
    chdir 't';
 
6
  }
 
7
 
 
8
  if (-e 'test_dir') {            # running from test directory, not ..
 
9
    unshift(@INC, '../blib/lib');
 
10
    unshift(@INC, '../lib');
 
11
  }
 
12
}
 
13
 
 
14
my $prefix = '.';
 
15
if (-e 'test_dir') {            # running from test directory, not ..
 
16
  $prefix = '..';
 
17
}
 
18
 
 
19
use lib '.'; use lib 't';
 
20
use SATest; sa_t_init("spamd_client");
 
21
 
 
22
use constant TEST_ENABLED => conf_bool('run_long_tests');
 
23
use constant HAS_SDBM_FILE => eval { require SDBM_File; };
 
24
 
 
25
our $DO_RUN = !$SKIP_SPAMD_TESTS && TEST_ENABLED;
 
26
 
 
27
my $num_tests = 18;
 
28
 
 
29
# UNIX socket tests
 
30
if (!$RUNNING_ON_WINDOWS) {
 
31
  $num_tests += 13;
 
32
}
 
33
 
 
34
# learn tests
 
35
if (HAS_SDBM_FILE) {
 
36
  $num_tests += 21;
 
37
}
 
38
 
 
39
use Test; plan tests => ($DO_RUN ? $num_tests : 0);
 
40
 
 
41
exit unless $DO_RUN;
 
42
 
 
43
# ---------------------------------------------------------------------------
 
44
 
 
45
my $testmsg = getmessage("data/spam/gtube.eml");
 
46
 
 
47
ok($testmsg);
 
48
 
 
49
%patterns = (
 
50
q{ X-Spam-Flag: YES}, 'flag',
 
51
q{ BODY: Generic Test for Unsolicited Bulk Email }, 'gtube',
 
52
q{ XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X }, 'gtube string',
 
53
);
 
54
 
 
55
ok(start_spamd("-L"));
 
56
 
 
57
my $client = create_clientobj({
 
58
                               port => $spamdport,
 
59
                               host => $spamdhost,
 
60
                              });
 
61
 
 
62
ok($client);
 
63
 
 
64
ok($client->ping());
 
65
 
 
66
my $result = $client->check($testmsg);
 
67
 
 
68
ok($result);
 
69
 
 
70
ok($result->{isspam} eq 'True');
 
71
ok(!$result->{message});
 
72
 
 
73
$result = $client->process($testmsg);
 
74
 
 
75
ok($result);
 
76
 
 
77
ok($result->{isspam} eq 'True');
 
78
ok($result->{message});
 
79
 
 
80
patterns_run_cb($result->{message});
 
81
ok_all_patterns();
 
82
 
 
83
clear_pattern_counters();
 
84
%patterns = (
 
85
q{ X-Spam-Flag: YES}, 'flag',
 
86
);
 
87
 
 
88
%anti_patterns = (
 
89
q{ XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X }, 'gtube string',
 
90
);
 
91
 
 
92
$result = $client->headers($testmsg);
 
93
 
 
94
ok($result);
 
95
 
 
96
ok($result->{message});
 
97
 
 
98
patterns_run_cb($result->{message});
 
99
ok_all_patterns();
 
100
 
 
101
ok(stop_spamd());
 
102
 
 
103
if (!$RUNNING_ON_WINDOWS) {
 
104
 
 
105
  clear_pattern_counters();
 
106
  $spamd_already_killed = undef;
 
107
 
 
108
  %patterns = (
 
109
    q{ X-Spam-Flag: YES}, 'flag',
 
110
    q{ BODY: Generic Test for Unsolicited Bulk Email }, 'gtube',
 
111
    q{ XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X }, 'gtube string',
 
112
      );
 
113
 
 
114
  %anti_patterns = ();
 
115
 
 
116
  my $sockpath = mk_safe_tmpdir()."/spamd.sock";
 
117
  ok(start_spamd("-L --socketpath=$sockpath"));
 
118
 
 
119
  $client = create_clientobj({
 
120
                              socketpath => $sockpath,
 
121
                             });
 
122
 
 
123
  ok($client);
 
124
 
 
125
  ok($client->ping());
 
126
 
 
127
  $result = $client->check($testmsg);
 
128
 
 
129
  ok($result);
 
130
 
 
131
  ok($result->{isspam} eq 'True');
 
132
  ok(!$result->{message});
 
133
 
 
134
  $result = $client->process($testmsg);
 
135
 
 
136
  ok($result);
 
137
  
 
138
  ok($result->{isspam} eq 'True');
 
139
  ok($result->{message});
 
140
 
 
141
  patterns_run_cb($result->{message});
 
142
  ok_all_patterns();
 
143
 
 
144
  ok(stop_spamd());
 
145
  cleanup_safe_tmpdir();
 
146
}
 
147
 
 
148
if (HAS_SDBM_FILE) {
 
149
 
 
150
  clear_pattern_counters();
 
151
  $spamd_already_killed = undef;
 
152
  tstlocalrules ("
 
153
        bayes_store_module Mail::SpamAssassin::BayesStore::SDBM
 
154
  ");
 
155
 
 
156
  ok(start_spamd("-L --allow-tell"));
 
157
 
 
158
  my $client = create_clientobj({
 
159
                               port => $spamdport,
 
160
                               host => $spamdhost,
 
161
                              });
 
162
 
 
163
  ok($client);
 
164
 
 
165
  my $spammsg = getmessage("data/spam/001");
 
166
  ok($spammsg);
 
167
 
 
168
  ok($client->learn($spammsg, 0));
 
169
 
 
170
  ok(!$client->learn($spammsg, 0));
 
171
 
 
172
  %patterns = ( '1 0  non-token data: nspam' => 'spam in database' );
 
173
  ok(salearnrun("--dump magic", \&patterns_run_cb));
 
174
  ok_all_patterns();
 
175
  clear_pattern_counters();
 
176
 
 
177
  ok($client->learn($spammsg, 2));
 
178
 
 
179
  %patterns = ( '0 0  non-token data: nspam' => 'spam in database',
 
180
                '0 0  non-token data: nham' => 'ham in database' );
 
181
  ok(salearnrun("--dump magic", \&patterns_run_cb));
 
182
  ok_all_patterns();
 
183
  clear_pattern_counters();
 
184
 
 
185
  my $hammsg = getmessage("data/nice/001");
 
186
  ok($hammsg);
 
187
 
 
188
  ok($client->learn($spammsg, 1));
 
189
 
 
190
  ok(!$client->learn($spammsg, 1));
 
191
 
 
192
  %patterns = ( '1 0  non-token data: nham' => 'ham in database' );
 
193
  ok(salearnrun("--dump magic", \&patterns_run_cb));
 
194
  ok_all_patterns();
 
195
  clear_pattern_counters();
 
196
 
 
197
  ok($client->learn($spammsg, 2));
 
198
 
 
199
  %patterns = ( '0 0  non-token data: nspam' => 'spam in database',
 
200
                '0 0  non-token data: nham' => 'ham in database' );
 
201
  ok(salearnrun("--dump magic", \&patterns_run_cb));
 
202
  ok_all_patterns();
 
203
  clear_pattern_counters();
 
204
 
 
205
  ok(stop_spamd());
 
206
}
 
207
 
 
208
 
 
209
sub getmessage {
 
210
  my ($msgpath) = @_;
 
211
 
 
212
  open(MSG, $msgpath) || return undef;
 
213
 
 
214
  my @file = <MSG>;
 
215
  my $msg = join('', @file);
 
216
 
 
217
  close(MSG);
 
218
 
 
219
  return $msg;
 
220
}