# HG changeset patch # User Maxim Dounin # Date 1222949891 -14400 # Node ID 0880e0fafde4b5db8a38a2872a9917d5b746e76c # Parent fbda19df2fc4908d5d03cbb01905360b18f34e3f Tests: implement smtp test backend and use it. diff --git a/README b/README --- a/README +++ b/README @@ -3,10 +3,14 @@ Test suit for nginx. Use prove to run tests as one usually do for perl tests. Individual tests may be run as well. +Tests assume that nginx sources are in directory nginx at the same level +as directory with tests, and nginx was already configured and built (i.e. +nginx binary available as ../nginx/objs/nginx from test directory). + Note: some tests may fail since they are for bugs not fixed in public code. Note: tests run nginx listening on localhost, and currently this includes -following ports: 8080, 10025. +following ports: 8025, 8026, 8080, 8081. Currently each test creates it's own temporary directory and uses it for logs etc. One may instruct tests not to remove the temp directory (e.g. diff --git a/lib/Test/Nginx/SMTP.pm b/lib/Test/Nginx/SMTP.pm --- a/lib/Test/Nginx/SMTP.pm +++ b/lib/Test/Nginx/SMTP.pm @@ -23,7 +23,7 @@ sub new { my $self = return $class->SUPER::new( Proto => "tcp", PeerAddr => "localhost", - PeerPort => 10025, + PeerPort => 8025, @_ ) or die "Can't connect to nginx: $!\n"; @@ -70,6 +70,45 @@ sub ok { ############################################################################### +sub smtp_test_daemon { + my $server = IO::Socket::INET->new( + Proto => 'tcp', + LocalPort => 8026, + Listen => 5, + Reuse => 1 + ) + or die "Can't create listening socket: $!\n"; + + while (my $client = $server->accept()) { + $client->autoflush(1); + print $client "220 fake esmtp server ready" . CRLF; + + while (<$client>) { + if (/^quit/i) { + print $client '221 quit ok' . CRLF; + } elsif (/^(ehlo|helo)/i) { + print $client '250 hello ok' . CRLF; + } elsif (/^rset/i) { + print $client '250 rset ok' . CRLF; + } elsif (/^mail from:[^@]+$/i) { + print $client '500 mail from error' . CRLF; + } elsif (/^mail from:/i) { + print $client '250 mail from ok' . CRLF; + } elsif (/^rcpt to:[^@]+$/i) { + print $client '500 rcpt to error' . CRLF; + } elsif (/^rcpt to:/i) { + print $client '250 rcpt to ok' . CRLF; + } else { + print $client "500 unknown command" . CRLF; + } + } + + close $client; + } +} + +############################################################################### + 1; ############################################################################### diff --git a/smtp-greeting-delay.t b/smtp-greeting-delay.t --- a/smtp-greeting-delay.t +++ b/smtp-greeting-delay.t @@ -38,7 +38,7 @@ mail { xclient off; server { - listen localhost:10025; + listen localhost:8025; protocol smtp; smtp_greeting_delay 100ms; } diff --git a/smtp.t b/smtp.t --- a/smtp.t +++ b/smtp.t @@ -25,7 +25,9 @@ use Test::Nginx::SMTP; select STDERR; $| = 1; select STDOUT; $| = 1; -my $t = Test::Nginx->new()->write_file_expand('nginx.conf', <<'EOF')->run(); +my $t = Test::Nginx->new() + ->run_daemon(\&Test::Nginx::SMTP::smtp_test_daemon) + ->write_file_expand('nginx.conf', <<'EOF')->run(); master_process off; daemon off; @@ -40,7 +42,7 @@ mail { xclient off; server { - listen localhost:10025; + listen localhost:8025; protocol smtp; smtp_auth login plain none; } @@ -65,7 +67,7 @@ http { add_header Auth-Status $reply; add_header Auth-Server 127.0.0.1; - add_header Auth-Port 25; + add_header Auth-Port 8026; add_header Auth-Wait 1; return 204; }