| 1 |
package Test::Qpsmtpd; |
| 2 |
use strict; |
| 3 |
use lib 't'; |
| 4 |
use lib 'lib'; |
| 5 |
use Carp qw(croak); |
| 6 |
use base qw(Qpsmtpd::SMTP); |
| 7 |
use Test::More; |
| 8 |
use Qpsmtpd::Constants; |
| 9 |
use Test::Qpsmtpd::Plugin; |
| 10 |
|
| 11 |
sub new_conn { |
| 12 |
ok(my $smtpd = __PACKAGE__->new(), "new"); |
| 13 |
ok( |
| 14 |
my $conn = |
| 15 |
$smtpd->start_connection( |
| 16 |
remote_host => 'localhost', |
| 17 |
remote_ip => '127.0.0.1' |
| 18 |
), |
| 19 |
"start_connection" |
| 20 |
); |
| 21 |
is(($smtpd->response)[0], "220", "greetings"); |
| 22 |
($smtpd, $conn); |
| 23 |
} |
| 24 |
|
| 25 |
sub start_connection { |
| 26 |
my $self = shift; |
| 27 |
my %args = @_; |
| 28 |
|
| 29 |
my $remote_host = $args{remote_host} or croak "no remote_host parameter"; |
| 30 |
my $remote_info = "test\@$remote_host"; |
| 31 |
my $remote_ip = $args{remote_ip} or croak "no remote_ip parameter"; |
| 32 |
|
| 33 |
my $conn = |
| 34 |
$self->SUPER::connection->start( |
| 35 |
remote_info => $remote_info, |
| 36 |
remote_ip => $remote_ip, |
| 37 |
remote_host => $remote_host, |
| 38 |
@_ |
| 39 |
); |
| 40 |
|
| 41 |
$self->load_plugins; |
| 42 |
|
| 43 |
my $rc = $self->start_conversation; |
| 44 |
return if $rc != DONE; |
| 45 |
|
| 46 |
$conn; |
| 47 |
} |
| 48 |
|
| 49 |
sub respond { |
| 50 |
my $self = shift; |
| 51 |
$self->{_response} = [@_]; |
| 52 |
} |
| 53 |
|
| 54 |
sub response { |
| 55 |
my $self = shift; |
| 56 |
$self->{_response} ? (@{delete $self->{_response}}) : (); |
| 57 |
} |
| 58 |
|
| 59 |
sub command { |
| 60 |
my ($self, $command) = @_; |
| 61 |
$self->input($command); |
| 62 |
$self->response; |
| 63 |
} |
| 64 |
|
| 65 |
sub input { |
| 66 |
my $self = shift; |
| 67 |
my $command = shift; |
| 68 |
|
| 69 |
my $timeout = $self->config('timeout'); |
| 70 |
alarm $timeout; |
| 71 |
|
| 72 |
$command =~ s/\r?\n$//s; # advanced chomp |
| 73 |
$self->log(LOGDEBUG, "dispatching $command"); |
| 74 |
defined $self->dispatch(split / +/, $command, 2) |
| 75 |
or $self->respond(502, "command unrecognized: '$command'"); |
| 76 |
alarm $timeout; |
| 77 |
} |
| 78 |
|
| 79 |
sub config_dir { |
| 80 |
return './t/config' if $ENV{QPSMTPD_DEVELOPER}; |
| 81 |
'./config.sample'; |
| 82 |
} |
| 83 |
|
| 84 |
sub plugin_dirs { |
| 85 |
('./plugins', './plugins/ident', './plugins/async'); |
| 86 |
} |
| 87 |
|
| 88 |
sub log { |
| 89 |
my ($self, $trace, $hook, $plugin, @log) = @_; |
| 90 |
my $level = Qpsmtpd::TRACE_LEVEL() || 5; |
| 91 |
$level = $self->init_logger unless defined $level; |
| 92 |
print("# " . join(" ", $$, @log) . "\n") if $trace <= $level; |
| 93 |
} |
| 94 |
|
| 95 |
sub varlog { |
| 96 |
shift->log(@_); |
| 97 |
} |
| 98 |
|
| 99 |
# sub run |
| 100 |
# sub disconnect |
| 101 |
|
| 102 |
sub run_plugin_tests { |
| 103 |
my $self = shift; |
| 104 |
$self->{_test_mode} = 1; |
| 105 |
my @plugins = $self->load_plugins(); |
| 106 |
|
| 107 |
# First count test number |
| 108 |
my $num_tests = 0; |
| 109 |
foreach my $plugin (@plugins) { |
| 110 |
$plugin->register_tests(); |
| 111 |
$num_tests += $plugin->total_tests(); |
| 112 |
} |
| 113 |
|
| 114 |
require Test::Builder; |
| 115 |
my $Test = Test::Builder->new(); |
| 116 |
|
| 117 |
$Test->plan(tests => $num_tests); |
| 118 |
|
| 119 |
# Now run them |
| 120 |
|
| 121 |
foreach my $plugin (@plugins) { |
| 122 |
$plugin->run_tests($self); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
1; |
| 127 |
|