From cacbab512d4102e6db19c6487c4d291006dd0fda Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Fri, 21 Nov 2014 11:02:46 -0500 Subject: Initial commit. --- t/bin.t | 39 +++++++++++++++++ t/conf.t | 18 ++++++++ t/delivert | 25 +++++++++++ t/fixtures/bar | 14 ++++++ t/fixtures/foo | 14 ++++++ t/fixtures/foo.expected | 14 ++++++ t/fixtures/foo2 | 14 ++++++ t/fixtures/foo2.expected | 14 ++++++ t/fixtures/multiple | 15 +++++++ t/fixtures/multiple.expected | 15 +++++++ t/fixtures/nosubj | 13 ++++++ t/fixtures/nosubj.expected | 14 ++++++ t/fixtures/sample.conf | 6 +++ t/fixtures/wrongdomain | 14 ++++++ t/fixtures/wrongdomain.expected | 14 ++++++ t/lib.t | 94 +++++++++++++++++++++++++++++++++++++++++ 16 files changed, 337 insertions(+) create mode 100644 t/bin.t create mode 100644 t/conf.t create mode 100755 t/delivert create mode 100644 t/fixtures/bar create mode 100644 t/fixtures/foo create mode 100644 t/fixtures/foo.expected create mode 100644 t/fixtures/foo2 create mode 100644 t/fixtures/foo2.expected create mode 100644 t/fixtures/multiple create mode 100644 t/fixtures/multiple.expected create mode 100644 t/fixtures/nosubj create mode 100644 t/fixtures/nosubj.expected create mode 100644 t/fixtures/sample.conf create mode 100644 t/fixtures/wrongdomain create mode 100644 t/fixtures/wrongdomain.expected create mode 100644 t/lib.t (limited to 't') diff --git a/t/bin.t b/t/bin.t new file mode 100644 index 0000000..41a82c2 --- /dev/null +++ b/t/bin.t @@ -0,0 +1,39 @@ +#!/usr/bin/perl + +use Test::More tests => 1; + +use IO::File; + +use strict; +use warnings; + +system "/usr/bin/env > /tmp/fpp"; + +my $spamcat = 'bin/spamcat'; +my $conffile = 't/fixtures/sample.conf'; + +# Add testlib which has createdb and possibly population of said db. + +my @dumpconfig = `$spamcat -c t/fixtures/sample.conf --dumpconfig`; +my %got = parse_configdump(@dumpconfig); +my %expected = (DBPATH => '/tmp/spamcat.sqlite3', + DEFAULT_COUNT => 10, + DELIVER => 't/delivert', + DOMAINS => "spamcat.example.com, spamcat2.example.com, spamcat3"); +is_deeply(\%got, \%expected); + +# Test for proper delivery. +my $fh = IO::File->new("|$spamcat -c $conffile") || + die "Couldn't open pipe to $spamcat: $!\n"; +$fh->close; + +sub parse_configdump { + my %rc; + + while (my $line = shift) { + $line =~ /(.*) = (.*)/; + $rc{$1} = $2; + } + + %rc; +} diff --git a/t/conf.t b/t/conf.t new file mode 100644 index 0000000..dbe2217 --- /dev/null +++ b/t/conf.t @@ -0,0 +1,18 @@ +# -*- mode: cperl -*- + +use Test::More tests => 7; + +use strict; +use warnings; + +require_ok 'SpamCat::Conf'; + +ok(SpamCat::Conf->can('read')); +my %conf = SpamCat::Conf::read('t/fixtures/sample.conf'); +ok(%conf); + +is($conf{dbpath}, '/tmp/spamcat.sqlite3'); +is($conf{default_count}, 10); +is($conf{deliver}, 't/delivert'); +is_deeply($conf{domains}, + ['spamcat.example.com', 'spamcat2.example.com', 'spamcat3']); diff --git a/t/delivert b/t/delivert new file mode 100755 index 0000000..63c3864 --- /dev/null +++ b/t/delivert @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use Email::Simple; +use IO::File; + +use strict; +use warnings; + +die usage() unless $#ARGV == 0; +my $path = shift; + +local $/; +my $email = Email::Simple->new(<>); + +my $msgid = $email->header('Message-ID'); +$msgid =~ s/<(.*)@.*>/$1/; + +my $fh = IO::File->new(">$path/$msgid") || + die "Couldn't write to $path/$msgid: $!\n"; +print $fh $email->as_string; +$fh->close; + +sub usage { + "Usage: $0 path\n"; +} diff --git a/t/fixtures/bar b/t/fixtures/bar new file mode 100644 index 0000000..fd600a6 --- /dev/null +++ b/t/fixtures/bar @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: bar@spamcat.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/foo b/t/fixtures/foo new file mode 100644 index 0000000..6365fab --- /dev/null +++ b/t/fixtures/foo @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: foo@spamcat.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/foo.expected b/t/fixtures/foo.expected new file mode 100644 index 0000000..9da87a9 --- /dev/null +++ b/t/fixtures/foo.expected @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: foo@spamcat.example.com +Subject: test - [18/20] +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/foo2 b/t/fixtures/foo2 new file mode 100644 index 0000000..1696186 --- /dev/null +++ b/t/fixtures/foo2 @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: foo@spamcat2.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/foo2.expected b/t/fixtures/foo2.expected new file mode 100644 index 0000000..e4d9335 --- /dev/null +++ b/t/fixtures/foo2.expected @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: foo@spamcat2.example.com +Subject: test - [17/20] +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/multiple b/t/fixtures/multiple new file mode 100644 index 0000000..2da9b27 --- /dev/null +++ b/t/fixtures/multiple @@ -0,0 +1,15 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: name1@spamcat.example.com, "Someone else" , + foo@wrongdomain.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/multiple.expected b/t/fixtures/multiple.expected new file mode 100644 index 0000000..d5c17a5 --- /dev/null +++ b/t/fixtures/multiple.expected @@ -0,0 +1,15 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: name1@spamcat.example.com, "Someone else" , + foo@wrongdomain.example.com +Subject: test - [20/20] +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/nosubj b/t/fixtures/nosubj new file mode 100644 index 0000000..3a58556 --- /dev/null +++ b/t/fixtures/nosubj @@ -0,0 +1,13 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: nosubj@spamcat.example.com +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/nosubj.expected b/t/fixtures/nosubj.expected new file mode 100644 index 0000000..94db30e --- /dev/null +++ b/t/fixtures/nosubj.expected @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: nosubj@spamcat.example.com +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) +Subject: [20/20] + +Sample email. diff --git a/t/fixtures/sample.conf b/t/fixtures/sample.conf new file mode 100644 index 0000000..e0d644b --- /dev/null +++ b/t/fixtures/sample.conf @@ -0,0 +1,6 @@ +# Comments and newlines are ignored. + +DBPATH=/tmp/spamcat.sqlite3 # So should most whitespace and inline comments. +DEFAULT_COUNT =10 # Make sure key=val doesn't work in comments +DELIVER= t/delivert # by saying DEFAULT_COUNT=8 +DOMAINS = spamcat.example.com, spamcat2.example.com spamcat3 diff --git a/t/fixtures/wrongdomain b/t/fixtures/wrongdomain new file mode 100644 index 0000000..a65cb0f --- /dev/null +++ b/t/fixtures/wrongdomain @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: bar@wrongdomain.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/fixtures/wrongdomain.expected b/t/fixtures/wrongdomain.expected new file mode 100644 index 0000000..a65cb0f --- /dev/null +++ b/t/fixtures/wrongdomain.expected @@ -0,0 +1,14 @@ +Return-Path: +Delivered-To: spamcat@example.com +Received: from mta.example.com + by mta.example.com (Dovecot) with LMTP id 9DDcI25oblTvgAEAQc1eRg + for ; Thu, 20 Nov 2014 17:17:18 -0500 +Received: by mta.example.com (Postfix, from userid 1001) + id 844956F1EE; Thu, 20 Nov 2014 17:17:18 -0500 (EST) +To: bar@wrongdomain.example.com +Subject: test +Message-Id: +Date: Thu, 20 Nov 2014 17:17:18 -0500 (EST) +From: sender@example.com (Spamcat Sender) + +Sample email. diff --git a/t/lib.t b/t/lib.t new file mode 100644 index 0000000..21e1c09 --- /dev/null +++ b/t/lib.t @@ -0,0 +1,94 @@ +# -*- Mode: cperl -*- + +use Test::More tests => 31; + +use strict; +use warnings; + +my ($tmpdir, %conf); +BEGIN { + $tmpdir = "/tmp/spamcat.t.$$"; + %conf = (dbpath => "$tmpdir/spamcat.sqlite3", + default_count => 20, + deliver => "t/delivert $tmpdir", + domains => ['spamcat.example.com', 'spamcat2.example.com']); + + system "rm -rf $tmpdir"; + mkdir $tmpdir; + system "/usr/local/bin/sqlite3 $conf{dbpath} < config/create-tables.sql"; +} + +END { + system "rm -rf $tmpdir"; +} + +require_ok 'SpamCat'; + +ok(SpamCat->can('new'), 'Has constructor'); +my $sch = SpamCat->new(%conf); +ok(defined $sch, 'Constructor returns instance'); + +ok(SpamCat->can('decrement_count'), 'Has count decrementor'); +is($sch->decrement_count('foo'), $conf{default_count}, + 'Default count for new sender'); +is($sch->decrement_count('foo'), 19, 'Existing sender decrements'); + +ok(SpamCat->can('get_count'), 'Has count getter'); +is($sch->get_count('foo'), 19, 'Returns existing sender count'); +ok(!defined $sch->get_count('doesntexist'), + 'Non-existant sender has undefined count'); + +ok(SpamCat->can('set_count'), 'Has count setter'); +is($sch->set_count('bar', 1), 1, 'Setting count returns existing count'); +is($sch->decrement_count('bar'), 0, 'Decrementing count from 1 returns 0'); +is($sch->decrement_count('bar'), 0, 'Decrementing count from 0 returns 0'); + +ok(SpamCat->can('parse_to')); +my @addrs; +@addrs = $sch->parse_to('foo@bar.com'); +is($addrs[0], 'foo@bar.com'); +@addrs = $sch->parse_to('"FooBar" '); +is($addrs[0], 'foo@bar.com'); +@addrs = $sch->parse_to('"Foo@Bar" '); +is($addrs[0], 'baz@pham.com'); +@addrs = $sch->parse_to('"Foo@Bar" ', '"a@b "'); +is($addrs[0], 'baz@pham.com'); +is($addrs[1], 'one@two.com'); + +ok(SpamCat->can('deliver'), 'Has delivery method'); +test_file('foo', 1); +test_file('foo2', 1); +test_file('multiple', 1); +test_file('wrongdomain', 1); +test_file('nosubj', 1); +test_file('bar', 0); + +sub test_file { + my ($filen, $should_exist) = @_; + + my $input = IO::File->new("&$inputfd" || die "Couldn't open $inputfd: $!\n"; + + $sch->deliver(); + + if ($should_exist) { + ok(-f "$tmpdir/$filen") || diag("$tmpdir/$filen doesn't exist."); + + local $/; + my $fh = IO::File->new("<$tmpdir/$filen") || + die "Couldn't open $tmpdir/$filen for reading: $!\n"; + my $got = <$fh>; + $fh->close; + + $fh = IO::File->new("; + $fh->close; + + is($got, $expected) || diag("Test for $filen output failed."); + } else { + ok(! -f "$tmpdir/$filen") || diag("$tmpdir/$filen exists."); + } +} -- cgit v1.2.3