|
From: <jgr...@us...> - 2003-07-31 14:40:43
|
Update of /cvsroot/popfile/engine/tests
In directory sc8-pr-cvs1:/tmp/cvs-serv25500
Modified Files:
TestPOP3.tst
Log Message:
Test suite for POP3 proxy include POP3 server and client; not yet complete but covers the major POP3 commands: USER, PASS, QUIT, DELE, RETR, LIST, UIDL
Index: TestPOP3.tst
===================================================================
RCS file: /cvsroot/popfile/engine/tests/TestPOP3.tst,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** TestPOP3.tst 30 Jul 2003 22:50:29 -0000 1.1
--- TestPOP3.tst 31 Jul 2003 14:20:47 -0000 1.2
***************
*** 11,14 ****
--- 11,15 ----
use POPFile::Logger;
use Proxy::POP3;
+ use Classifier::Bayes;
use IO::Handle;
use IO::Socket;
***************
*** 62,67 ****
sub server
{
! my $client;
! my @messages = glob '*.msg';
while ( <$client> ) {
--- 63,70 ----
sub server
{
! my ( $client ) = @_;
! my @messages = sort glob 'TestMailParse*.msg';
!
! print $client "+OK Ready$eol";
while ( <$client> ) {
***************
*** 72,76 ****
if ( $command =~ /USER (.*)/i ) {
! if ( $1 =~ /gooduser/ ) {
print $client "+OK Welcome $1$eol";
} else {
--- 75,79 ----
if ( $command =~ /USER (.*)/i ) {
! if ( $1 =~ /(gooduser)/ ) {
print $client "+OK Welcome $1$eol";
} else {
***************
*** 86,89 ****
--- 89,93 ----
print $client "-ERR Bad Password$eol";
}
+ next;
}
***************
*** 120,133 ****
if ( $command =~ /QUIT/i ) {
print $client "+OK Bye$eol";
! next;
}
if ( $command =~ /__QUIT__/i ) {
print $client "+OK Bye$eol";
! last;
}
if ( $command =~ /RSET/i ) {
! @messages = glob '*.msg';
print $client "+OK Reset$eol";
next;
--- 124,137 ----
if ( $command =~ /QUIT/i ) {
print $client "+OK Bye$eol";
! last;
}
if ( $command =~ /__QUIT__/i ) {
print $client "+OK Bye$eol";
! return 0;
}
if ( $command =~ /RSET/i ) {
! @messages = sort glob 'TestMailParse*.msg';
print $client "+OK Reset$eol";
next;
***************
*** 140,153 ****
if ( $command =~ /DELE (.*)/i ) {
! $messages[$1] = '';
! print $client "+OK Deleted $1$eol";
next;
}
! if ( $command =~ /RETR (.*)/i ) {
! if ( $messages[$1] ne '' ) {
! print $client "+OK " . ( -s $messages[$1] ) . "$eol";
! open FILE, "<$messages[$1]";
while ( <FILE> ) {
print $client $_;
--- 144,163 ----
if ( $command =~ /DELE (.*)/i ) {
! my $index = $1 - 1;
! if ( defined( $messages[$index] ) && ( $messages[$index] ne '' ) ) {
! $messages[$index] = '';
! print $client "+OK Deleted $1$eol";
! } else {
! print $client "-ERR No such message $1$eol";
! }
next;
}
! if ( $command =~ /RETR (\d+)/i ) {
! my $index = $1 - 1;
! if ( defined( $messages[$index] ) && ( $messages[$index] ne '' ) ) {
! print $client "+OK " . ( -s $messages[$index] ) . "$eol";
! open FILE, "<$messages[$index]";
while ( <FILE> ) {
print $client $_;
***************
*** 164,171 ****
if ( $command =~ /TOP (.*) (.*)/i ) {
! if ( $messages[$1] ne '' ) {
! print $client "+OK " . ( -s $messages[$1] ) . "$eol";
! open FILE, "<$messages[$1]";
while ( <FILE> ) {
print $client $_;
--- 174,182 ----
if ( $command =~ /TOP (.*) (.*)/i ) {
! my $index = $1 - 1;
! if ( $messages[$index] ne '' ) {
! print $client "+OK " . ( -s $messages[$index] ) . "$eol";
! open FILE, "<$messages[$index]";
while ( <FILE> ) {
print $client $_;
***************
*** 191,194 ****
--- 202,206 ----
if ( $command =~ /CAPA/i ) {
+ print $client "+OK I can handle$eol" . "AUTH$eol" . "USER$eol" . "APOP$eol.$eol";
next;
}
***************
*** 206,213 ****
--- 218,265 ----
}
+ if ( $command =~ /JOHN/ ) {
+ print $client "+OK Hello John$eol";
+ next;
+ }
+
print $client "-ERR unknown command or bad syntax$eol";
}
+
+ return 0;
}
+ test_assert( `rm -rf corpus` == 0 );
+ test_assert( `cp -R corpus.base corpus` == 0 );
+
+ my $c = new POPFile::Configuration;
+ my $mq = new POPFile::MQ;
+ my $l = new POPFile::Logger;
+ my $b = new Classifier::Bayes;
+
+ $c->configuration( $c );
+ $c->mq( $mq );
+ $c->logger( $l );
+
+ $c->initialize();
+
+ $l->configuration( $c );
+ $l->mq( $mq );
+ $l->logger( $l );
+
+ $l->initialize();
+
+ $mq->configuration( $c );
+ $mq->mq( $mq );
+ $mq->logger( $l );
+
+ $b->configuration( $c );
+ $b->mq( $mq );
+ $b->logger( $l );
+
+ $b->initialize();
+ $b->module_config_( 'html', 'port', 8080 );
+ $b->config_( 'hostname', '127.0.0.1' );
+ $b->start();
+
my $pid = fork();
***************
*** 216,219 ****
--- 268,288 ----
# CHILD THAT WILL RUN THE POP3 SERVER
+ my $server = IO::Socket::INET->new( Proto => 'tcp',
+ LocalAddr => 'localhost',
+ LocalPort => 8110,
+ Listen => SOMAXCONN,
+ Reuse => 1 );
+
+ my $selector = new IO::Select( $server );
+
+ while ( 1 ) {
+ if ( defined( $selector->can_read(0) ) ) {
+ if ( my $client = $server->accept() ) {
+ last if !server($client);
+ close $client;
+ }
+ }
+ }
+
exit(0);
} else {
***************
*** 226,253 ****
# CHILD THAT WILL RUN THE POP3 PROXY
! my $c = new POPFile::Configuration;
! my $mq = new POPFile::MQ;
! my $l = new POPFile::Logger;
! my $p = new Proxy::POP3
!
! $c->configuration( $c );
! $c->mq( $mq );
! $c->logger( $l );
!
! $c->initialize();
!
! $l->configuration( $c );
! $l->mq( $mq );
! $l->logger( $l );
!
! $l->initialize();
!
! $mq->configuration( $c );
! $mq->mq( $mq );
! $mq->logger( $l );
$p->configuration( $c );
$p->mq( $mq );
$p->logger( $l );
$p->forker( \&forker );
--- 295,304 ----
# CHILD THAT WILL RUN THE POP3 PROXY
! my $p = new Proxy::POP3;
$p->configuration( $c );
$p->mq( $mq );
$p->logger( $l );
+ $p->classifier( $b );
$p->forker( \&forker );
***************
*** 257,261 ****
$p->initialize();
$p->config_( 'port', $port );
! test_assert_equal( $p->start(), 1 );
my $now = time;
--- 308,313 ----
$p->initialize();
$p->config_( 'port', $port );
! $p->global_config_( 'timeout', 1 );
! $p->start();
my $now = time;
***************
*** 285,288 ****
--- 337,481 ----
test_assert( defined( $client ) );
test_assert( $client->connected );
+
+ # Make sure that POPFile sends an appropriate banner
+
+ my $result = <$client>;
+ test_assert_equal( $result, "+OK POP3 POPFile (test suite) server ready$eol" );
+
+ # Try a connection to a server that does not exist
+
+ print $client "USER 127.0.0.1:8111:gooduser$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "-ERR can't connect to 127.0.0.1:8111$eol" );
+
+ # Check that we can connect to the remote POP3 server (should still be waiting
+ # for us)
+
+ print $client "USER 127.0.0.1:8110:gooduser$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK Welcome gooduser$eol" );
+
+ # TODO check for LOGIN message
+
+ # Now send a bad password
+
+ print $client "PASS password$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "-ERR Bad Password$eol" );
+
+ # And then a good one
+
+ print $client "PASS secret$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK Now logged in$eol" );
+
+ # Test that the catch all code works for connected servers
+
+ print $client "JOHN$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK Hello John$eol" );
+
+ # Check that we handle HELO locally
+
+ print $client "HELO$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK HELO POPFile Server Ready$eol" );
+
+ # Get the LIST response and verify
+
+ print $client "LIST$eol";
+
+ my $count = 0;
+ my $size = 0;
+ my @messages = sort glob 'TestMailParse*.msg';
+ for my $i (0..$#messages) {
+ if ( $messages[$i] ne '' ) {
+ $count += 1;
+ $size += ( -s $messages[$i] );
+ }
+ }
+
+ $result = <$client>;
+ test_assert_equal( $result, "+OK $count $size$eol" );
+
+ for my $i (0..$#messages) {
+ if ( $messages[$i] ne '' ) {
+ my $resp = ( -s $messages[$i] );
+ $result = <$client>;
+ test_assert_equal( $result, ($i+1) . " $resp$eol" );
+ }
+ }
+
+ $result = <$client>;
+ test_assert_equal( $result, ".$eol" );
+
+ # Make such that UIDL works
+
+ print $client "UIDL$eol";
+
+ $result = <$client>;
+ test_assert_equal( $result, "+OK $count $size$eol" );
+
+ for my $i (0..$#messages) {
+ if ( $messages[$i] ne '' ) {
+ my $resp = $messages[$i];
+ $result = <$client>;
+ test_assert_equal( $result, ($i+1) . " $resp$eol" );
+ }
+ }
+
+ $result = <$client>;
+ test_assert_equal( $result, ".$eol" );
+
+ # Try getting a message (first a bad message number)
+
+ my $notexist = $count + 1;
+ print $client "RETR $notexist$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "-ERR No such message $notexist$eol" );
+
+ # Now get a message that actually exists
+
+ print $client "RETR 1$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK " . ( -s $messages[0] ) . "$eol" );
+ my $cam = $messages[0];
+ $cam =~ s/msg$/cam/;
+
+ open FILE, "<$cam";
+ while ( <FILE> ) {
+ my $line = $_;
+ $result = <$client>;
+ $result =~ s/popfile1=1/popfile0=0/;
+ test_assert_equal( $result, $line );
+ }
+ close FILE;
+
+ $result = <$client>;
+ test_assert_equal( $result, "$eol" );
+ $result = <$client>;
+ test_assert_equal( $result, ".$eol" );
+
+ # TODO check for NEWFL and CLASS messages
+
+ # TODO check that the file was written to the history
+
+ # Try an unsuccessful delete
+
+ print $client "DELE $notexist$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "-ERR No such message $notexist$eol" );
+
+ # Try a successful delete
+
+ print $client "DELE 1$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK Deleted 1$eol" );
+
+ # Check that we echo the remote servers QUIT response
+
+ print $client "QUIT$eol";
+ $result = <$client>;
+ test_assert_equal( $result, "+OK Bye$eol" );
close $client;
|