2004-07-16 04:22:27 +02:00
|
|
|
package Test::Qpsmtpd;
|
|
|
|
use strict;
|
|
|
|
use Carp qw(croak);
|
|
|
|
use base qw(Qpsmtpd::SMTP);
|
|
|
|
use Test::More;
|
|
|
|
use Qpsmtpd::Constants;
|
2004-09-08 18:26:33 +02:00
|
|
|
use Test::Qpsmtpd::Plugin;
|
2004-07-16 04:22:27 +02:00
|
|
|
|
|
|
|
sub new_conn {
|
|
|
|
ok(my $smtpd = __PACKAGE__->new(), "new");
|
|
|
|
ok(my $conn = $smtpd->start_connection(remote_host => 'localhost',
|
|
|
|
remote_ip => '127.0.0.1'), "start_connection");
|
|
|
|
is(($smtpd->response)[0], "220", "greetings");
|
|
|
|
($smtpd, $conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start_connection {
|
|
|
|
my $self = shift;
|
|
|
|
my %args = @_;
|
|
|
|
|
|
|
|
my $remote_host = $args{remote_host} or croak "no remote_host parameter";
|
|
|
|
my $remote_info = "test\@$remote_host";
|
|
|
|
my $remote_ip = $args{remote_ip} or croak "no remote_ip parameter";
|
|
|
|
|
|
|
|
my $conn = $self->SUPER::connection->start(remote_info => $remote_info,
|
|
|
|
remote_ip => $remote_ip,
|
|
|
|
remote_host => $remote_host,
|
|
|
|
@_);
|
|
|
|
|
|
|
|
|
|
|
|
$self->load_plugins;
|
|
|
|
|
|
|
|
my $rc = $self->start_conversation;
|
|
|
|
return if $rc != DONE;
|
|
|
|
|
|
|
|
$conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub respond {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{_response} = [@_];
|
|
|
|
}
|
|
|
|
|
|
|
|
sub response {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{_response} ? (@{ delete $self->{_response} }) : ();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub command {
|
|
|
|
my ($self, $command) = @_;
|
|
|
|
$self->input($command);
|
|
|
|
$self->response;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub input {
|
|
|
|
my $self = shift;
|
|
|
|
my $command = shift;
|
|
|
|
|
|
|
|
my $timeout = $self->config('timeout');
|
|
|
|
alarm $timeout;
|
|
|
|
|
|
|
|
$command =~ s/\r?\n$//s; # advanced chomp
|
2004-09-05 18:25:02 +02:00
|
|
|
$self->log(LOGDEBUG, "dispatching $command");
|
2004-07-16 04:22:27 +02:00
|
|
|
defined $self->dispatch(split / +/, $command, 2)
|
|
|
|
or $self->respond(502, "command unrecognized: '$command'");
|
|
|
|
alarm $timeout;
|
|
|
|
}
|
|
|
|
|
2004-09-08 18:26:33 +02:00
|
|
|
sub config_dir {
|
2004-11-09 16:25:52 +01:00
|
|
|
'./config.sample';
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
|
|
|
|
2007-01-12 00:52:51 +01:00
|
|
|
sub plugin_dirs {
|
|
|
|
('./plugins');
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub log {
|
|
|
|
my ($self, $trace, @log) = @_;
|
|
|
|
my $level = Qpsmtpd::TRACE_LEVEL();
|
|
|
|
$level = $self->init_logger unless defined $level;
|
2004-09-08 23:53:29 +02:00
|
|
|
print("# " . join(" ", $$, @log) . "\n") if $trace <= $level;
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
|
|
|
|
2005-05-25 22:07:58 +02:00
|
|
|
sub varlog {
|
|
|
|
shift->log(@_);
|
|
|
|
}
|
|
|
|
|
2004-07-16 04:22:27 +02:00
|
|
|
# sub run
|
|
|
|
# sub disconnect
|
|
|
|
|
2004-09-08 18:26:33 +02:00
|
|
|
sub run_plugin_tests {
|
|
|
|
my $self = shift;
|
|
|
|
$self->{_test_mode} = 1;
|
|
|
|
my @plugins = $self->load_plugins();
|
|
|
|
# First count test number
|
|
|
|
my $num_tests = 0;
|
|
|
|
foreach my $plugin (@plugins) {
|
|
|
|
$plugin->register_tests();
|
|
|
|
$num_tests += $plugin->total_tests();
|
|
|
|
}
|
|
|
|
|
|
|
|
require Test::Builder;
|
|
|
|
my $Test = Test::Builder->new();
|
2004-11-09 16:25:52 +01:00
|
|
|
|
2004-09-08 18:26:33 +02:00
|
|
|
$Test->plan( tests => $num_tests );
|
|
|
|
|
|
|
|
# Now run them
|
|
|
|
|
|
|
|
foreach my $plugin (@plugins) {
|
2004-09-08 23:48:27 +02:00
|
|
|
$plugin->run_tests($self);
|
2004-09-08 18:26:33 +02:00
|
|
|
}
|
|
|
|
}
|
2004-07-16 04:22:27 +02:00
|
|
|
|
|
|
|
1;
|
|
|
|
|