ConnectTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_Ldap
  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_Ldap
  24. */
  25. require_once 'Zend/Ldap.php';
  26. /* Note: The ldap_connect function does not actually try to connect. This
  27. * is why many tests attempt to bind with invalid credentials. If the
  28. * bind returns 'Invalid credentials' we know the transport related work
  29. * was successful.
  30. */
  31. /**
  32. * @category Zend
  33. * @package Zend_Ldap
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Ldap
  38. */
  39. class Zend_Ldap_ConnectTest extends PHPUnit_Framework_TestCase
  40. {
  41. protected $_options = null;
  42. public function setUp()
  43. {
  44. $this->_options = array('host' => TESTS_ZEND_LDAP_HOST);
  45. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  46. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  47. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  48. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  49. }
  50. public function testEmptyOptionsConnect()
  51. {
  52. $ldap = new Zend_Ldap(array());
  53. try {
  54. $ldap->connect();
  55. $this->fail('Expected exception for empty options');
  56. } catch (Zend_Ldap_Exception $zle) {
  57. $this->assertContains('host parameter is required', $zle->getMessage());
  58. }
  59. }
  60. public function testUnknownHostConnect()
  61. {
  62. $ldap = new Zend_Ldap(array('host' => 'bogus.example.com'));
  63. try {
  64. // connect doesn't actually try to connect until bind is called
  65. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  66. $this->fail('Expected exception for unknown host');
  67. } catch (Zend_Ldap_Exception $zle) {
  68. $this->assertContains('Can\'t contact LDAP server', $zle->getMessage());
  69. }
  70. }
  71. public function testPlainConnect()
  72. {
  73. $ldap = new Zend_Ldap($this->_options);
  74. try {
  75. // Connect doesn't actually try to connect until bind is called
  76. // but if we get 'Invalid credentials' then we know the connect
  77. // succeeded.
  78. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  79. $this->fail('Expected exception for invalid username');
  80. } catch (Zend_Ldap_Exception $zle) {
  81. $this->assertContains('Invalid credentials', $zle->getMessage());
  82. }
  83. }
  84. public function testExplicitParamsConnect()
  85. {
  86. $host = TESTS_ZEND_LDAP_HOST;
  87. $port = 0;
  88. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  89. $port = TESTS_ZEND_LDAP_PORT;
  90. $useSsl = false;
  91. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  92. $useSsl = TESTS_ZEND_LDAP_USE_SSL;
  93. $ldap = new Zend_Ldap();
  94. try {
  95. $ldap->connect($host, $port, $useSsl)
  96. ->bind('CN=ignored,DC=example,DC=com', 'ignored');
  97. $this->fail('Expected exception for invalid username');
  98. } catch (Zend_Ldap_Exception $zle) {
  99. $this->assertContains('Invalid credentials', $zle->getMessage());
  100. }
  101. }
  102. public function testExplicitPortConnect()
  103. {
  104. $port = 389;
  105. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT)
  106. $port = TESTS_ZEND_LDAP_PORT;
  107. if (defined('TESTS_ZEND_LDAP_USE_SSL') && TESTS_ZEND_LDAP_USE_SSL)
  108. $port = 636;
  109. $ldap = new Zend_Ldap($this->_options);
  110. try {
  111. $ldap->connect(null, $port)
  112. ->bind('CN=ignored,DC=example,DC=com', 'ignored');
  113. $this->fail('Expected exception for invalid username');
  114. } catch (Zend_Ldap_Exception $zle) {
  115. $this->assertContains('Invalid credentials', $zle->getMessage());
  116. }
  117. }
  118. public function testBadPortConnect()
  119. {
  120. $options = $this->_options;
  121. $options['port'] = 10;
  122. $ldap = new Zend_Ldap($options);
  123. try {
  124. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  125. $this->fail('Expected exception for unknown username');
  126. } catch (Zend_Ldap_Exception $zle) {
  127. $this->assertContains('Can\'t contact LDAP server', $zle->getMessage());
  128. }
  129. }
  130. public function testSetOptionsConnect()
  131. {
  132. $ldap = new Zend_Ldap();
  133. $ldap->setOptions($this->_options);
  134. try {
  135. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  136. $this->fail('Expected exception for invalid username');
  137. } catch (Zend_Ldap_Exception $zle) {
  138. $this->assertContains('Invalid credentials', $zle->getMessage());
  139. }
  140. }
  141. public function testMultiConnect()
  142. {
  143. $ldap = new Zend_Ldap($this->_options);
  144. for ($i = 0; $i < 3; $i++) {
  145. try {
  146. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  147. $this->fail('Expected exception for unknown username');
  148. } catch (Zend_Ldap_Exception $zle) {
  149. $this->assertContains('Invalid credentials', $zle->getMessage());
  150. }
  151. }
  152. }
  153. public function testDisconnect()
  154. {
  155. $ldap = new Zend_Ldap($this->_options);
  156. for ($i = 0; $i < 3; $i++) {
  157. $ldap->disconnect();
  158. try {
  159. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  160. $this->fail('Expected exception for unknown username');
  161. } catch (Zend_Ldap_Exception $zle) {
  162. $this->assertContains('Invalid credentials', $zle->getMessage());
  163. }
  164. }
  165. }
  166. public function testGetErrorCode()
  167. {
  168. $ldap = new Zend_Ldap($this->_options);
  169. try {
  170. // Connect doesn't actually try to connect until bind is called
  171. // but if we get 'Invalid credentials' then we know the connect
  172. // succeeded.
  173. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  174. $this->fail('Expected exception for invalid username');
  175. } catch (Zend_Ldap_Exception $zle) {
  176. $this->assertContains('Invalid credentials', $zle->getMessage());
  177. $this->assertEquals(0x31, $zle->getCode());
  178. $this->assertEquals(0x0, Zend_Ldap_Exception::getLdapCode($ldap));
  179. $this->assertEquals(0x0, Zend_Ldap_Exception::getLdapCode(null));
  180. }
  181. }
  182. /**
  183. * @group ZF-8274
  184. */
  185. public function testConnectWithUri()
  186. {
  187. $host = TESTS_ZEND_LDAP_HOST;
  188. $port = 0;
  189. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389) $port = TESTS_ZEND_LDAP_PORT;
  190. $useSsl = false;
  191. if (defined('TESTS_ZEND_LDAP_USE_SSL')) $useSsl = TESTS_ZEND_LDAP_USE_SSL;
  192. if ($useSsl) {
  193. $host = 'ldaps://' . $host;
  194. } else {
  195. $host = 'ldap://' . $host;
  196. }
  197. if ($port) {
  198. $host = $host . ':' . $port;
  199. }
  200. $ldap = new Zend_Ldap();
  201. try {
  202. $ldap->connect($host)
  203. ->bind('CN=ignored,DC=example,DC=com', 'ignored');
  204. $this->fail('Expected exception for invalid username');
  205. } catch (Zend_Ldap_Exception $zle) {
  206. $this->assertContains('Invalid credentials', $zle->getMessage());
  207. }
  208. }
  209. }