ConnectTest.php 8.0 KB

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