|
|
@@ -33,11 +33,24 @@ class SmtpTransportTest extends TestCase
|
|
|
protected $SmtpTransport;
|
|
|
|
|
|
/**
|
|
|
- * @var \Cake\Network\Socket|\PHPUnit\Framework\MockObject\MockObject
|
|
|
+ * @var \Cake\Network\Socket&\PHPUnit\Framework\MockObject\MockObject
|
|
|
*/
|
|
|
protected $socket;
|
|
|
|
|
|
/**
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $credentials = [
|
|
|
+ 'username' => 'mark',
|
|
|
+ 'password' => 'story',
|
|
|
+ ];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $credentialsEncoded;
|
|
|
+
|
|
|
+ /**
|
|
|
* Setup
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -52,6 +65,8 @@ class SmtpTransportTest extends TestCase
|
|
|
$this->SmtpTransport = new SmtpTestTransport();
|
|
|
$this->SmtpTransport->setSocket($this->socket);
|
|
|
$this->SmtpTransport->setConfig(['client' => 'localhost']);
|
|
|
+
|
|
|
+ $this->credentialsEncoded = base64_encode(chr(0) . 'mark' . chr(0) . 'story');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -126,13 +141,18 @@ class SmtpTransportTest extends TestCase
|
|
|
{
|
|
|
$this->expectException(\Cake\Network\Exception\SocketException::class);
|
|
|
$this->expectExceptionMessage('SMTP authentication method not allowed, check if SMTP server requires TLS.');
|
|
|
- $this->SmtpTransport->setConfig(['tls' => false, 'username' => 'user', 'password' => 'pass']);
|
|
|
+ $this->SmtpTransport->setConfig(['tls' => false] + $this->credentials);
|
|
|
+
|
|
|
$this->socket->expects($this->any())->method('connect')->will($this->returnValue(true));
|
|
|
$this->socket->expects($this->at(1))->method('read')->will($this->returnValue("220 Welcome message\r\n"));
|
|
|
$this->socket->expects($this->at(2))->method('write')->with("EHLO localhost\r\n");
|
|
|
$this->socket->expects($this->at(3))->method('read')->will($this->returnValue("250 Accepted\r\n"));
|
|
|
- $this->socket->expects($this->at(4))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(5))->method('read')
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(4))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(6))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(7))->method('read')
|
|
|
->will($this->returnValue("504 5.7.4 Unrecognized authentication type\r\n"));
|
|
|
$this->SmtpTransport->connect();
|
|
|
}
|
|
|
@@ -179,20 +199,31 @@ class SmtpTransportTest extends TestCase
|
|
|
$this->assertStringContainsString('200 Not Accepted', $e->getPrevious()->getMessage());
|
|
|
}
|
|
|
|
|
|
+ public function testAuthPlain()
|
|
|
+ {
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("235 OK\r\n"));
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
+ $this->SmtpTransport->auth();
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* testAuth method
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function testAuth()
|
|
|
+ public function testAuthLogin()
|
|
|
{
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
- $this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
|
|
|
- $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Pass\r\n"));
|
|
|
- $this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n");
|
|
|
- $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("235 OK\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
+ $this->socket->expects($this->at(4))->method('write')->with("bWFyaw==\r\n");
|
|
|
+ $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
|
|
|
+ $this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
|
|
|
+ $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("235 OK\r\n"));
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
$this->SmtpTransport->auth();
|
|
|
}
|
|
|
|
|
|
@@ -205,10 +236,14 @@ class SmtpTransportTest extends TestCase
|
|
|
{
|
|
|
$this->expectException(\Cake\Network\Exception\SocketException::class);
|
|
|
$this->expectExceptionMessage('AUTH command not recognized or not implemented, SMTP server may not require authentication.');
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))->method('read')
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))->method('read')
|
|
|
->will($this->returnValue("500 5.3.3 Unrecognized command\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
$this->SmtpTransport->auth();
|
|
|
}
|
|
|
|
|
|
@@ -221,10 +256,14 @@ class SmtpTransportTest extends TestCase
|
|
|
{
|
|
|
$this->expectException(\Cake\Network\Exception\SocketException::class);
|
|
|
$this->expectExceptionMessage('AUTH command not recognized or not implemented, SMTP server may not require authentication.');
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))->method('read')
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))->method('read')
|
|
|
->will($this->returnValue("502 5.3.3 Command not implemented\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
$this->SmtpTransport->auth();
|
|
|
}
|
|
|
|
|
|
@@ -237,10 +276,14 @@ class SmtpTransportTest extends TestCase
|
|
|
{
|
|
|
$this->expectException(\Cake\Network\Exception\SocketException::class);
|
|
|
$this->expectExceptionMessage('SMTP Error: 503 5.5.1 Already authenticated');
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))
|
|
|
->method('read')->will($this->returnValue("503 5.5.1 Already authenticated\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
$this->SmtpTransport->auth();
|
|
|
}
|
|
|
|
|
|
@@ -251,12 +294,15 @@ class SmtpTransportTest extends TestCase
|
|
|
*/
|
|
|
public function testAuthBadUsername()
|
|
|
{
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
- $this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
|
|
|
- $this->socket->expects($this->at(3))->method('read')
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
+ $this->socket->expects($this->at(4))->method('write')->with("bWFyaw==\r\n");
|
|
|
+ $this->socket->expects($this->at(5))->method('read')
|
|
|
->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
|
|
|
$e = null;
|
|
|
try {
|
|
|
@@ -277,13 +323,16 @@ class SmtpTransportTest extends TestCase
|
|
|
*/
|
|
|
public function testAuthBadPassword()
|
|
|
{
|
|
|
- $this->socket->expects($this->at(0))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
- $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
- $this->socket->expects($this->at(2))->method('write')->with("bWFyaw==\r\n");
|
|
|
- $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Pass\r\n"));
|
|
|
- $this->socket->expects($this->at(4))->method('write')->with("c3Rvcnk=\r\n");
|
|
|
- $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
|
|
|
- $this->SmtpTransport->setConfig(['username' => 'mark', 'password' => 'story']);
|
|
|
+ $this->socket->expects($this->at(0))->method('write')->with("AUTH PLAIN {$this->credentialsEncoded}\r\n");
|
|
|
+ $this->socket->expects($this->at(1))->method('read')->will($this->returnValue("504 5.7.4 Unrecognized Authentication Type\r\n"));
|
|
|
+
|
|
|
+ $this->socket->expects($this->at(2))->method('write')->with("AUTH LOGIN\r\n");
|
|
|
+ $this->socket->expects($this->at(3))->method('read')->will($this->returnValue("334 Login\r\n"));
|
|
|
+ $this->socket->expects($this->at(4))->method('write')->with("bWFyaw==\r\n");
|
|
|
+ $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("334 Pass\r\n"));
|
|
|
+ $this->socket->expects($this->at(6))->method('write')->with("c3Rvcnk=\r\n");
|
|
|
+ $this->socket->expects($this->at(7))->method('read')->will($this->returnValue("535 5.7.8 Authentication failed\r\n"));
|
|
|
+ $this->SmtpTransport->setConfig($this->credentials);
|
|
|
|
|
|
$e = null;
|
|
|
try {
|