about summary refs log tree commit homepage
path: root/t
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2016-06-24 01:15:13 +0000
committerEric Wong <e@80x24.org>2016-06-24 02:00:26 +0000
commitc515264dd69156fc89c59685f788b1093afb8731 (patch)
treeada1a21aa82b369dbe1ac8c865474a9318228f7d /t
parent3c24b7e7e47be9646226d921897cc9ec92e9be8a (diff)
downloadpublic-inbox-c515264dd69156fc89c59685f788b1093afb8731.tar.gz
This should hopefully make it easier to try other anti-spam
systems (or none at all) in the future.
Diffstat (limited to 't')
-rw-r--r--t/spamcheck_spamc.t49
1 files changed, 49 insertions, 0 deletions
diff --git a/t/spamcheck_spamc.t b/t/spamcheck_spamc.t
new file mode 100644
index 00000000..65ac5c2e
--- /dev/null
+++ b/t/spamcheck_spamc.t
@@ -0,0 +1,49 @@
+# Copyright (C) 2016 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+use strict;
+use warnings;
+use Test::More;
+use Cwd;
+use Email::Simple;
+use IO::File;
+use File::Temp qw/tempdir/;
+use Fcntl qw(:DEFAULT SEEK_SET);
+my $tmpdir = tempdir('spamcheck_spamc-XXXXXX', TMPDIR => 1, CLEANUP => 1);
+
+use_ok 'PublicInbox::Spamcheck::Spamc';
+my $spamc = PublicInbox::Spamcheck::Spamc->new;
+$spamc->{checkcmd} = [qw(cat)];
+
+{
+        open my $fh, '+>', "$tmpdir/file" or die "open failed: $!";
+        ok(!$spamc->spamcheck($fh), 'empty '.ref($fh));
+}
+ok(!$spamc->spamcheck(IO::File->new_tmpfile), 'IO::File->new_tmpfile');
+
+my $dst = '';
+my $src = <<'EOF';
+Date: Thu, 01 Jan 1970 00:00:00 +0000
+To: <e@example.com>
+From: <e@example.com>
+Subject: test
+Message-ID: <testmessage@example.com>
+
+EOF
+ok($spamc->spamcheck(Email::Simple->new($src), \$dst), 'Email::Simple works');
+is($dst, $src, 'input == output');
+
+$dst = '';
+$spamc->{checkcmd} = ['sh', '-c', 'cat; false'];
+ok(!$spamc->spamcheck(Email::Simple->new($src), \$dst), 'Failed check works');
+is($dst, $src, 'input == output for spammy example');
+
+for my $l (qw(ham spam)) {
+        my $file = "$tmpdir/$l.out";
+        $spamc->{$l.'cmd'} = ['tee', $file ];
+        my $method = $l.'learn';
+        ok($spamc->$method(Email::Simple->new($src)), "$method OK");
+        open my $fh, '<', $file or die "failed to open $file: $!";
+        is(eval { local $/, <$fh> }, $src, "$l command ran alright");
+}
+
+done_testing();