1 # Copyright (C) 2016 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
9 use File::Temp qw/tempdir/;
10 use Fcntl qw(:DEFAULT SEEK_SET);
11 my $tmpdir = tempdir('spamcheck_spamc-XXXXXX', TMPDIR => 1, CLEANUP => 1);
13 use_ok 'PublicInbox::Spamcheck::Spamc';
14 my $spamc = PublicInbox::Spamcheck::Spamc->new;
15 $spamc->{checkcmd} = [qw(cat)];
18 open my $fh, '+>', "$tmpdir/file" or die "open failed: $!";
19 ok(!$spamc->spamcheck($fh), 'empty '.ref($fh));
21 ok(!$spamc->spamcheck(IO::File->new_tmpfile), 'IO::File->new_tmpfile');
25 Date: Thu, 01 Jan 1970 00:00:00 +0000
29 Message-ID: <testmessage@example.com>
32 ok($spamc->spamcheck(Email::Simple->new($src), \$dst), 'Email::Simple works');
33 is($dst, $src, 'input == output');
36 $spamc->{checkcmd} = ['sh', '-c', 'cat; false'];
37 ok(!$spamc->spamcheck(Email::Simple->new($src), \$dst), 'Failed check works');
38 is($dst, $src, 'input == output for spammy example');
40 for my $l (qw(ham spam)) {
41 my $file = "$tmpdir/$l.out";
42 $spamc->{$l.'cmd'} = ['tee', $file ];
43 my $method = $l.'learn';
44 ok($spamc->$method(Email::Simple->new($src)), "$method OK");
45 open my $fh, '<', $file or die "failed to open $file: $!";
46 is(eval { local $/, <$fh> }, $src, "$l command ran alright");