Skip to content

Commit

Permalink
Fix POP3 test failures
Browse files Browse the repository at this point in the history
Fake pop server can now specify a port to run on
  • Loading branch information
Synchro committed Aug 5, 2013
1 parent 73093a3 commit cd4ed61
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
22 changes: 18 additions & 4 deletions class.pop3.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,19 @@ public function __construct()
* @param bool $tval
* @param string $username
* @param string $password
* @param int $debug_level
* @return bool
*/
public static function popBeforeSmtp($host, $port = false, $tval = false, $username = '', $password = '')
{
public static function popBeforeSmtp(
$host,
$port = false,
$tval = false,
$username = '',
$password = '',
$debug_level = 0
) {
$pop = new POP3;
return $pop->authorise($host, $port, $tval, $username, $password);
return $pop->authorise($host, $port, $tval, $username, $password, $debug_level);
}

/**
Expand Down Expand Up @@ -330,7 +337,11 @@ public function disconnect()
*/
private function getResponse($size = 128)
{
return fgets($this->pop_conn, $size);
$r = fgets($this->pop_conn, $size);
if ($this->do_debug >= 1) {
echo "Server -> Client: $r";
}
return $r;
}

/**
Expand All @@ -342,6 +353,9 @@ private function getResponse($size = 128)
private function sendString($string)
{
if ($this->pop_conn) {
if ($this->do_debug >= 2) { //Show client messages when debug >= 2
echo "Client -> Server: $string";
}
return fwrite($this->pop_conn, $string, strlen($string));
}
return 0;
Expand Down
8 changes: 4 additions & 4 deletions test/fakepopserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ LOGFILE=${LOGFOLDER}/fakepop.log

LOGGING=1
DEBUG=1
TIMEOUT=10
TIMEOUT=60

POP_USER=
POP_PASSWRD=test
Expand Down Expand Up @@ -78,10 +78,10 @@ while [ ${BREAK} -eq 0 ] ; do
;;
PASS)
if [ "${POP_PASSWRD}" == "${ARGS}" ] ; then
ANSWER="+OK Logged in.\r\n"
ANSWER="+OK Logged in."
AUTH=1
else
ANSWER="-ERR Login failed\r\n"
ANSWER="-ERR Login failed."
fi
;;
LIST)
Expand Down Expand Up @@ -117,7 +117,7 @@ while [ ${BREAK} -eq 0 ] ; do
;;
esac
else
echo "+OK Connection timed out\r\n"
echo "+OK Connection timed out"
break
fi
done
Expand Down
22 changes: 18 additions & 4 deletions test/phpmailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1280,30 +1280,44 @@ public function testLineBreaks()

/**
* Use a fake POP3 server to test POP-before-SMTP auth
* With a known-good login
*/
public function testPopBeforeSmtp()
public function testPopBeforeSmtpGood()
{
//Start a fake POP server
$pid = shell_exec('nohup ./runfakepopserver.sh >/dev/null 2>/dev/null & printf "%u" $!');
$this->pids[] = $pid;

sleep(2);
//Test a known-good login
$this->assertTrue(
POP3::popBeforeSmtp('localhost', 1100, 10, 'user', 'test'),
POP3::popBeforeSmtp('localhost', 1100, 10, 'user', 'test', 0),
'POP before SMTP failed'
);
//Kill the fake server
shell_exec('kill -TERM '.escapeshellarg($pid));
sleep(2);
}

$pid = shell_exec('nohup ./runfakepopserver.sh >/dev/null 2>/dev/null & printf "%u" $!');
/**
* Use a fake POP3 server to test POP-before-SMTP auth
* With a known-bad login
*/
public function testPopBeforeSmtpBad()
{
//Start a fake POP server on a different port
//so we don't inadvertently connect to the previous instance
$pid = shell_exec('nohup ./runfakepopserver.sh 1101 >/dev/null 2>/dev/null & printf "%u" $!');
$this->pids[] = $pid;

sleep(2);
//Test a known-bad login
$this->assertFalse(
POP3::popBeforeSmtp('localhost', 1100, 10, 'user', 'xxx'),
POP3::popBeforeSmtp('localhost', 1101, 10, 'user', 'xxx', 0),
'POP before SMTP should have failed'
);
shell_exec('kill -TERM '.escapeshellarg($pid));
sleep(2);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion test/runfakepopserver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Run the fake pop server from bash
# Idea from http://blog.ale-re.net/2007/09/ipersimple-remote-shell-with-netcat.html
# Defaults to port 1100 so it can be run by unpriv users and not clash with a real server
# Optionally, pass in in a port number as the first arg

mkfifo fifo
nc -l 1100 <fifo |bash ./fakepopserver.sh >fifo
nc -l ${1:-1100} <fifo |bash ./fakepopserver.sh >fifo
rm fifo

0 comments on commit cd4ed61

Please sign in to comment.