-
Notifications
You must be signed in to change notification settings - Fork 70
/
re_base_extraction.t
executable file
·500 lines (392 loc) · 14.2 KB
/
re_base_extraction.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#!/usr/bin/perl -T
# Test regular expression base-string extraction in
# Mail::SpamAssassin::Plugin::BodyRuleBaseExtractor
use lib '.'; use lib 't'; use lib '../lib';
use SATest; sa_t_init("re_base_extraction");
use strict;
use warnings;
my $debug = 0;
my $running_perl56 = ($] < 5.007);
close STDIN; # inhibits noise from sa-compile
use Test::More tests => 128;
use lib '../lib';
# ---------------------------------------------------------------------------
1 and try_extraction ('
body FOO /fooBar/
body FOOI /fooBar/i
body FOODOTSTAR /fooBar.*BAZ/
body FOODOTSTAR2 /fooBar(?:BAZ|blarg)/
', {
base_extract => 1,
bases_must_be_casei => 0,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'fooBar:FOO,[l=0] FOODOTSTAR,[l=1]',
'fooBarBAZ:FOO,[l=0] FOODOTSTAR,[l=1] FOODOTSTAR2,[l=0]',
'fooBarblarg:FOO,[l=0] FOODOTSTAR,[l=1] FOODOTSTAR2,[l=0]',
], [ ]);
# ---------------------------------------------------------------------------
1 and try_extraction ('
body FOO /foobar/
body FOOI /foobar/i
body FOODOTSTAR /foobar.*BAZ/i
body FOODOTSTAR2 /foobar(?:BAZ|blarg)/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'foobar:FOO,[l=1] FOODOTSTAR,[l=1] FOOI,[l=0]',
'foobarbaz:FOO,[l=1] FOODOTSTAR,[l=1] FOODOTSTAR2,[l=0] FOOI,[l=0]',
'foobarblarg:FOO,[l=1] FOODOTSTAR,[l=1] FOODOTSTAR2,[l=0] FOOI,[l=0]',
], [ ]);
# ---------------------------------------------------------------------------
1 and try_extraction ('
body FOO /(?:(?:bbbb)|dddd (?:eeee )?by|aaaa)/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'bbbb:FOO,[l=0]',
'dddd by:FOO,[l=0]',
'dddd eeee by:FOO,[l=0]',
'aaaa:FOO,[l=0]'
], [ ]);
# ---------------------------------------------------------------------------
1 and try_extraction ('
body TEST5 /time to refinance|refinanc\w{1,3}\b.{0,16}\bnow\b/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'refinanc:TEST5,[l=1]',
], [ ]);
# ---------------------------------------------------------------------------
1 and try_extraction ('
body TEST2 /foody* bar/
body TEST3 /foody? bar/
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'food:TEST2,[l=1]',
'food bar:TEST2,[l=1] TEST3,[l=1]',
'foody bar:TEST2,[l=1] TEST3,[l=1]',
], [ ]);
# ---------------------------------------------------------------------------
1 and try_extraction ('
body __SARE_FRAUD_BADTHINGS /(?:all funds will be returned|ass?ylum|assassinate|(?:auto|boat|car|plane|train).{1,7}(?:crash|accident|disaster|wreck)|before they both died|brutal acts|cancer|coup attempt|disease|due to the current|\bexile\b|\bfled|\bflee\b|have been frozen|impeach|\bkilled|land dispute|murder|over-invoice|political crisis|poisoned (?:to death )?by|relocate|since the demise|\bslay\b)/i
body __FRAUD_PTS /\b(?:ass?ass?inat(?:ed|ion)|murder(?:e?d)?|kill(?:ed|ing)\b[^.]{0,99}\b(?:war veterans|rebels?))\b/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'accident:__SARE_FRAUD_BADTHINGS,[l=1]',
'all funds will be returned:__SARE_FRAUD_BADTHINGS,[l=1]',
'asasinated:__FRAUD_PTS,[l=1]',
'asasination:__FRAUD_PTS,[l=1]',
'asassinated:__FRAUD_PTS,[l=1]',
'asassination:__FRAUD_PTS,[l=1]',
'assasinated:__FRAUD_PTS,[l=1]',
'assasination:__FRAUD_PTS,[l=1]',
'assassinate:__SARE_FRAUD_BADTHINGS,[l=1]',
'assassinated:__FRAUD_PTS,[l=1] __SARE_FRAUD_BADTHINGS,[l=1]',
'assassination:__FRAUD_PTS,[l=1]',
'assylum:__SARE_FRAUD_BADTHINGS,[l=1]',
'asylum:__SARE_FRAUD_BADTHINGS,[l=1]',
'before they both died:__SARE_FRAUD_BADTHINGS,[l=1]',
'brutal acts:__SARE_FRAUD_BADTHINGS,[l=1]',
'cancer:__SARE_FRAUD_BADTHINGS,[l=1]',
'coup attempt:__SARE_FRAUD_BADTHINGS,[l=1]',
'crash:__SARE_FRAUD_BADTHINGS,[l=1]',
'disaster:__SARE_FRAUD_BADTHINGS,[l=1]',
'disease:__SARE_FRAUD_BADTHINGS,[l=1]',
'due to the current:__SARE_FRAUD_BADTHINGS,[l=1]',
'exile:__SARE_FRAUD_BADTHINGS,[l=1]',
'fled:__SARE_FRAUD_BADTHINGS,[l=1]',
'flee:__SARE_FRAUD_BADTHINGS,[l=1]',
'have been frozen:__SARE_FRAUD_BADTHINGS,[l=1]',
'impeach:__SARE_FRAUD_BADTHINGS,[l=1]',
'killed:__FRAUD_PTS,[l=1] __SARE_FRAUD_BADTHINGS,[l=1]',
'killing:__FRAUD_PTS,[l=1]',
'land dispute:__SARE_FRAUD_BADTHINGS,[l=1]',
'murder:__FRAUD_PTS,[l=1] __SARE_FRAUD_BADTHINGS,[l=1]',
'over-invoice:__SARE_FRAUD_BADTHINGS,[l=1]',
'plane:__SARE_FRAUD_BADTHINGS,[l=1]',
'poisoned by:__SARE_FRAUD_BADTHINGS,[l=1]',
'poisoned to death by:__SARE_FRAUD_BADTHINGS,[l=1]',
'political crisis:__SARE_FRAUD_BADTHINGS,[l=1]',
'relocate:__SARE_FRAUD_BADTHINGS,[l=1]',
'since the demise:__SARE_FRAUD_BADTHINGS,[l=1]',
'slay:__SARE_FRAUD_BADTHINGS,[l=1]',
'train:__SARE_FRAUD_BADTHINGS,[l=1]',
'war veterans:__FRAUD_PTS,[l=1]',
'wreck:__SARE_FRAUD_BADTHINGS,[l=1]',
], [ ]);
# ---------------------------------------------------------------------------
# skip this one for perl 5.6.*; it does not truncate the long strings in the
# same place as 5.8.* and 5.9.*, although they still work fine
($running_perl56) and ok(1);
($running_perl56) and ok(1);
($running_perl56) and ok(1);
($running_perl56) and ok(1);
(!$running_perl56) and try_extraction ('
body VIRUS_WARNING345 /(This message contained attachments that have been blocked by Guinevere|This is an automatic message from the Guinevere Internet Antivirus Scanner)\./
body VIRUS_WARNING345I /(This message contained attachments that have been blocked by Guinevere|This is an automatic message from the Guinevere Internet Antivirus Scanner)\./i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'this is an automatic message from the guinevere internet ant:VIRUS_WARNING345,[l=1] VIRUS_WARNING345I,[l=0]',
'this message contained attachments that have been blocked by:VIRUS_WARNING345,[l=1] VIRUS_WARNING345I,[l=0]'
], [ ]);
# ---------------------------------------------------------------------------
# also not suitable for perl 5.6.x
($running_perl56) and ok(1);
($running_perl56) and ok(1);
($running_perl56) and ok(1);
(!$running_perl56) and try_extraction ('
body FOO /foobar\x{e2}\x{82}\x{ac}baz/
', {
base_extract => 1,
bases_must_be_casei => 0,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'foobar:FOO,[l=0]',
], [ ]);
# ---------------------------------------------------------------------------
try_extraction ('
body FOO /(?:Viagra|Valium|Xanax|Soma|Cialis){2}/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
# TODO: it would be great if these were correctly recognised as non-lossy
'cialis:FOO,[l=1]',
'soma:FOO,[l=1]',
'valium:FOO,[l=1]',
'viagra:FOO,[l=1]',
'xanax:FOO,[l=1]'
], [ ]);
# ---------------------------------------------------------------------------
try_extraction ('
body FOO /\brecords (?:[a-z_,-]+ )+?(?:feature|(?:a|re)ward)/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'records :FOO,[l=1]'
], [ ]);
# ---------------------------------------------------------------------------
try_extraction ('
body EXCUSE_REMOVE /to .{0,20}(?:mailings|offers)/i
body TEST2 /foody* bar/
body TEST1A /fo(?:oish|o) bar/
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'foo bar:TEST1A,[l=1]',
'food:TEST2,[l=1]',
'fooish bar:TEST1A,[l=1]',
'mailings:EXCUSE_REMOVE,[l=1]',
'offers:EXCUSE_REMOVE,[l=1]',
], [ ]);
# ---------------------------------------------------------------------------
try_extraction ('
body EXCUSE_REMOVE /to .{0,20}(?:mail(ings|food)|o(ffer|blarg)s)/i
body TEST2 /foody* bar/
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'food:TEST2,[l=1]',
'mailfood:EXCUSE_REMOVE,[l=1] TEST2,[l=1]',
'mailings:EXCUSE_REMOVE,[l=1]',
'oblargs:EXCUSE_REMOVE,[l=1]',
'offers:EXCUSE_REMOVE,[l=1]',
], [ ]);
# ---------------------------------------------------------------------------
try_extraction ('
body FOO /foo bar/
body EXCUSE_REMOVE /to be removed from.{0,20}(?:mailings|offers)/i
body KAM_STOCKTIP15 /(?:Nano Superlattice Technology|NSLT)/is
body TEST1 /foo(?:ish)? bar/
body TEST1A /fo(?:oish|o) bar/
body TEST1B /fo(?:oish|o)? bar/
body TEST2 /foody* bar/
body TEST3 /foody? bar/
body TEST4 /A(?i:ct) N(?i:ow)/
body TEST5 /time to refinance|refinanc\w{1,3}\b.{0,16}\bnow\b/i
body TEST6 /(?:Current|Target)(?: Price)?:\s+\$(?:O\.|\d\.O)/
body TEST7 /(?!credit)[ck\xc7\xe7@]\W?r\W?[e3\xc8\xc9\xca\xcb\xe8\xe9\xea\xeb\xa4]\W?[d\xd0]\W?[il|!1y?\xcc\xcd\xce\xcf\xec\xed\xee\xef]\W?t/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 1
}, [
'fo bar:TEST1B,[l=1]',
'foo bar:FOO,[l=1] TEST1,[l=1] TEST1A,[l=1] TEST1B,[l=1]',
'to be removed from:EXCUSE_REMOVE,[l=1]',
'nslt:KAM_STOCKTIP15,[l=1]',
'nano superlattice technology:KAM_STOCKTIP15,[l=1]',
'fooish bar:TEST1,[l=1] TEST1A,[l=1] TEST1B,[l=1]',
'act now:TEST4,[l=1]',
'food:TEST2,[l=1]',
'food bar:TEST2,[l=1] TEST3,[l=1]',
'foody bar:TEST2,[l=1] TEST3,[l=1]',
'refinanc:TEST5,[l=1]',
'target::TEST6,[l=1]',
'target price::TEST6,[l=1]',
'current::TEST6,[l=1]',
'current price::TEST6,[l=1]',
], [
# we do not want to see these
'!credit:TEST7,[l=1]',
]);
# ---------------------------------------------------------------------------
try_extraction ('
body FOO /foo bar/
body EXCUSE_REMOVE /to be removed from.{0,20}(?:mailings|offers)/i
body KAM_STOCKTIP15 /(?:Nano Superlattice Technology|NSLT)/is
# this should not result in a match on "foo bar" since we are not
# splitting alts in this test
body TEST1 /fo(?:oish|o)? b(a|b)r/
body TEST2 /fo(?:oish|o) b(a|b)r/
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 0
}, [
'foo bar:FOO,[l=1]',
'to be removed from:EXCUSE_REMOVE,[l=1]',
],[
'foo bar:FOO TEST1,[l=1]',
'foo bar:FOO TEST2,[l=1]',
'nano superlattice technology:KAM_STOCKTIP15,[l=1]',
'fooish bar:TEST1,[l=1]',
'fooish bar:TEST2,[l=1]',
]);
# ---------------------------------------------------------------------------
try_extraction ('
body TEST3 /toniospam/i
', {
base_extract => 1,
bases_must_be_casei => 1,
bases_can_use_alternations => 0,
bases_can_use_quantifiers => 0,
bases_can_use_char_classes => 0,
bases_split_out_alternations => 0
}, [
'toniospam:TEST3,[l=0]',
],[
]);
#############################################################################
use Mail::SpamAssassin;
sub try_extraction {
my ($rules, $params, $output, $notoutput) = @_;
my $sa = Mail::SpamAssassin->new({
rules_filename => $localrules,
site_rules_filename => $siterules,
userprefs_filename => $userrules,
local_tests_only => 1,
debug => $debug,
dont_copy_prefs => 1,
base_quiet => 1,
});
ok($sa);
# remove all rules and plugins; we want just our stuff
untaint_system("rm -f $siterules/*.pre");
untaint_system("rm -f $siterules/*.pm");
# keep 20_aux_tlds.cf to suppress RB warnings
rename("$localrules/20_aux_tlds.cf", "$localrules/20_aux_tlds.cf.tmp");
untaint_system("rm -f $localrules/*.cf");
rename("$localrules/20_aux_tlds.cf.tmp", "$localrules/20_aux_tlds.cf");
{ # suppress unnecessary warning:
# "Filehandle STDIN reopened as STDOUT only for output"
# See https://rt.perl.org/rt3/Public/Bug/Display.html?id=23838
no warnings 'io';
open (OUT, ">$localrules/99_test.cf")
or die "failed to write rule";
}
print OUT "
use_bayes 0 # disable bayes loading
loadplugin Mail::SpamAssassin::Plugin::Check
loadplugin Mail::SpamAssassin::Plugin::BodyRuleBaseExtractor
".$rules;
close OUT;
my ($k, $v);
while (($k, $v) = each %{$params}) { $sa->{$k}=$v; }
$sa->init();
ok ($sa->lint_rules() == 0) or warn "lint failed: $rules";
my $conf = $sa->{conf};
my $ruletype = "body_0";
foreach my $key1 (sort keys %{$conf->{base_orig}->{$ruletype}}) {
print "INPUT: $key1 $conf->{base_orig}->{$ruletype}->{$key1}\n";
}
my %found;
foreach my $key (sort keys %{$conf->{base_string}->{$ruletype}}) {
my $str = "$key:$conf->{base_string}->{$ruletype}->{$key}";
print "BASES: '$str'\n";
$found{$str} = 1;
}
# $output ||= [];
foreach my $line (@{$output}) {
ok($found{$line}) or warn "failed to find '$line'";
}
$notoutput ||= [];
foreach my $line (@{$notoutput}) {
ok(!$found{$line}) or warn "found '$line' but didn't want to";
}
}