SmtpProtocolTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * Zend_Mail
  24. */
  25. require_once 'Zend/Mail.php';
  26. /**
  27. * Zend_Mail_Protocol_Smtp
  28. */
  29. require_once 'Zend/Mail/Protocol/Smtp.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Mail
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Mail
  37. */
  38. class Zend_Mail_SmtpProtocolTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * @var Zend_Mail_Protocol_Smtp
  42. */
  43. protected $_protocol;
  44. public function setUp()
  45. {
  46. $this->_protocol = new ProtocolMock();
  47. }
  48. public function testEhlo()
  49. {
  50. $this->_connectAndEhlo(); // expects 250 response
  51. $this->assertEquals(array(
  52. '220 example.com ESMTP welcome',
  53. 'EHLO 127.0.0.1',
  54. '250 Hello 127.0.0.1, go ahead'
  55. ), $this->_protocol->dialog);
  56. }
  57. /**
  58. * @depends testEhlo
  59. * @expectedException Zend_Mail_Protocol_Exception
  60. */
  61. public function testHeloIsOnlyAllowedOncePerSession()
  62. {
  63. $this->_connectAndEhlo(); // do it once
  64. $this->_protocol->helo(); // do it again
  65. }
  66. /**
  67. * @depends testEhlo
  68. */
  69. public function testEhloFallsBackToHelo()
  70. {
  71. $this->_protocol->responseBuffer = array(
  72. '220 example.com ESMTP welcome',
  73. '500 Unrecognized', /* 500 or 502 error on unrecognized EHLO */
  74. '250 Hello 127.0.0.1, go ahead'
  75. );
  76. $this->_protocol->connect();
  77. $this->_protocol->helo();
  78. $this->assertEquals(array(
  79. '220 example.com ESMTP welcome',
  80. 'EHLO 127.0.0.1', // tries EHLO
  81. '500 Unrecognized', // .. which fails
  82. 'HELO 127.0.0.1', // continues to HELO
  83. '250 Hello 127.0.0.1, go ahead' // success
  84. ), $this->_protocol->dialog);
  85. }
  86. /**
  87. * @depends testEhlo
  88. */
  89. public function testMail()
  90. {
  91. $p = $this->_protocol;
  92. $expectedDialog = $this->_connectAndEhlo();
  93. $expectedDialog[] = 'MAIL FROM:<from@example.com>';
  94. $expectedDialog[] = $p->responseBuffer[] = '250 Sender accepted';
  95. $this->_protocol->mail('from@example.com');
  96. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  97. }
  98. /**
  99. * @depends testMail
  100. */
  101. public function testRcptExpects250()
  102. {
  103. $p = $this->_protocol;
  104. $expectedDialog = $this->_connectAndEhlo();
  105. $expectedDialog[] = 'MAIL FROM:<from@example.com>';
  106. $expectedDialog[] = $p->responseBuffer[] = '250 Sender accepted';
  107. $this->_protocol->mail('from@example.com');
  108. $expectedDialog[] = 'RCPT TO:<to@example.com>';
  109. $expectedDialog[] = $p->responseBuffer[] = '250 Recipient OK';
  110. $this->_protocol->rcpt('to@example.com');
  111. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  112. }
  113. /**
  114. * @depends testMail
  115. */
  116. public function testRcptExpects251()
  117. {
  118. $p = $this->_protocol;
  119. $expectedDialog = $this->_connectAndEhlo();
  120. $expectedDialog[] = 'MAIL FROM:<from@example.com>';
  121. $expectedDialog[] = $p->responseBuffer[] = '250 Sender accepted';
  122. $this->_protocol->mail('from@example.com');
  123. $expectedDialog[] = 'RCPT TO:<to@example.com>';
  124. $expectedDialog[] = $p->responseBuffer[] = '251 Recipient OK';
  125. $this->_protocol->rcpt('to@example.com');
  126. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  127. }
  128. /**
  129. * @depends testRcptExpects250
  130. */
  131. public function testData()
  132. {
  133. $p = $this->_protocol;
  134. $expectedDialog = $this->_connectAndEhlo();
  135. $expectedDialog[] = 'MAIL FROM:<from@example.com>';
  136. $expectedDialog[] = $p->responseBuffer[] = '250 Sender accepted';
  137. $this->_protocol->mail('from@example.com');
  138. $expectedDialog[] = 'RCPT TO:<to@example.com>';
  139. $expectedDialog[] = $p->responseBuffer[] = '250 Recipient OK';
  140. $this->_protocol->rcpt('to@example.com');
  141. $expectedDialog[] = 'DATA';
  142. $expectedDialog[] = $p->responseBuffer[] = '354 Go ahead';
  143. $expectedDialog[] = 'foo';
  144. $expectedDialog[] = '.'; // end of data marker
  145. $expectedDialog[] = $p->responseBuffer[] = '250 Accepted';
  146. $this->_protocol->data('foo');
  147. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  148. }
  149. /**
  150. * @depends testEhlo
  151. */
  152. public function testRset()
  153. {
  154. $expectedDialog = $this->_connectAndEhlo();
  155. $this->_protocol->responseBuffer = array('250 OK');
  156. $expectedDialog[] = 'RSET';
  157. $expectedDialog[] = '250 OK';
  158. $this->_protocol->rset();
  159. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  160. }
  161. /**
  162. * @depends testEhlo
  163. * @group ZF-1377
  164. */
  165. public function testRsetExpects220()
  166. {
  167. $expectedDialog = $this->_connectAndEhlo();
  168. // Microsoft ESMTP server responds to RSET with 220 rather than 250
  169. $this->_protocol->responseBuffer = array('220 OK');
  170. $expectedDialog[] = 'RSET';
  171. $expectedDialog[] = '220 OK';
  172. $this->_protocol->rset();
  173. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  174. }
  175. /**
  176. * @depends testEhlo
  177. */
  178. public function testQuit()
  179. {
  180. $p = $this->_protocol;
  181. $expectedDialog = $this->_connectAndEhlo();
  182. $expectedDialog[] = 'QUIT';
  183. $expectedDialog[] = $p->responseBuffer[] = '221 goodbye';
  184. $this->_protocol->quit();
  185. $this->assertEquals($expectedDialog, $this->_protocol->dialog);
  186. }
  187. /**
  188. * @depends testMail
  189. * @group ZF-8511
  190. */
  191. public function testMultilineResponsesAreNotTruncated()
  192. {
  193. $this->_connectAndEhlo();
  194. $this->_protocol->responseBuffer[] = '550-line one';
  195. $this->_protocol->responseBuffer[] = '550 line two';
  196. try {
  197. $this->_protocol->mail('from@example.com');
  198. $this->fail('Expected exception on 550 response');
  199. } catch (Zend_Mail_Protocol_Exception $e) {
  200. $this->assertEquals('line one line two', $e->getMessage());
  201. }
  202. }
  203. /**
  204. * @depends testMail
  205. * @group ZF-10249
  206. */
  207. public function testExceptionCodeIsSmtpStatusCode()
  208. {
  209. $p = $this->_protocol;
  210. $this->_connectAndEhlo();
  211. $p->responseBuffer[] = '550 failure';
  212. try {
  213. $this->_protocol->mail('from@example.com');
  214. $this->fail('Expected exception on 550 response');
  215. } catch (Zend_Mail_Protocol_Exception $e) {
  216. $this->assertEquals(550, $e->getCode());
  217. }
  218. }
  219. /**
  220. * @depends testMail
  221. * @expectedException Zend_Mail_Protocol_Exception
  222. */
  223. public function testRcptThrowsExceptionOnUnexpectedResponse()
  224. {
  225. $p = $this->_protocol;
  226. $expectedDialog = $this->_connectAndEhlo();
  227. $expectedDialog[] = 'MAIL FROM:<from@example.com>';
  228. $expectedDialog[] = $p->responseBuffer[] = '250 Sender accepted';
  229. $this->_protocol->mail('from@example.com');
  230. $expectedDialog[] = 'RCPT TO:<to@example.com>';
  231. $expectedDialog[] = $p->responseBuffer[] = '500 error';
  232. $this->_protocol->rcpt('to@example.com');
  233. }
  234. public function testMailBeforeHeloThrowsException()
  235. {
  236. try {
  237. $this->_protocol->mail('from@example.com');
  238. $this->fail('mail() before helo() should throw exception');
  239. } catch (Zend_Mail_Protocol_Exception $e) {
  240. $this->assertEquals('A valid session has not been started', $e->getMessage());
  241. }
  242. }
  243. /**
  244. * @depends testEhlo
  245. */
  246. public function testRcptBeforeMailThrowsException()
  247. {
  248. $this->_connectAndEhlo();
  249. try {
  250. $this->_protocol->rcpt('to@example.com');
  251. $this->fail('rcpt() before mail() should throw exception');
  252. } catch (Zend_Mail_Protocol_Exception $e) {
  253. $this->assertEquals('No sender reverse path has been supplied', $e->getMessage());
  254. }
  255. }
  256. /**
  257. * @depends testEhlo
  258. * @expectedException Zend_Mail_Protocol_Exception
  259. */
  260. public function testDataBeforeRcptThrowsException()
  261. {
  262. $this->_connectAndEhlo();
  263. $this->_protocol->data('foo');
  264. }
  265. /**
  266. * Performs the initial EHLO dialog
  267. */
  268. protected function _connectAndEhlo()
  269. {
  270. $this->_protocol->responseBuffer = array(
  271. '220 example.com ESMTP welcome',
  272. '250 Hello 127.0.0.1, go ahead'
  273. );
  274. $this->_protocol->connect();
  275. $this->_protocol->helo();
  276. return $this->_protocol->dialog;
  277. }
  278. }
  279. class ProtocolMock extends Zend_Mail_Protocol_Smtp
  280. {
  281. public $dialog = array();
  282. public $responseBuffer = array();
  283. /**
  284. * Override connect function to use local file for testing
  285. *
  286. * @param string $remote
  287. */
  288. protected function _connect($remote)
  289. {
  290. $this->_socket = tmpfile();
  291. }
  292. protected function _send($request)
  293. {
  294. $this->dialog[] = $request;
  295. }
  296. protected function _receive($timeout = null)
  297. {
  298. $line = array_shift($this->responseBuffer);
  299. $this->dialog[] = $line;
  300. return $line;
  301. }
  302. }