ConnectTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-2008 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Ldap_ConnectTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_options = null;
  45. public function setUp()
  46. {
  47. $this->_options = array('host' => TESTS_ZEND_LDAP_HOST);
  48. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  49. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  50. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  51. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  52. }
  53. public function testEmptyOptionsConnect()
  54. {
  55. $ldap = new Zend_Ldap(array());
  56. try {
  57. $ldap->connect();
  58. $this->fail('Expected exception for empty options');
  59. } catch (Zend_Ldap_Exception $zle) {
  60. $this->assertContains('host parameter is required', $zle->getMessage());
  61. }
  62. }
  63. public function testUnknownHostConnect()
  64. {
  65. $ldap = new Zend_Ldap(array('host' => 'bogus.example.com'));
  66. try {
  67. // connect doesn't actually try to connect until bind is called
  68. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  69. $this->fail('Expected exception for unknown host');
  70. } catch (Zend_Ldap_Exception $zle) {
  71. $this->assertContains('Can\'t contact LDAP server', $zle->getMessage());
  72. }
  73. }
  74. public function testPlainConnect()
  75. {
  76. $ldap = new Zend_Ldap($this->_options);
  77. try {
  78. // Connect doesn't actually try to connect until bind is called
  79. // but if we get 'Invalid credentials' then we know the connect
  80. // succeeded.
  81. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  82. $this->fail('Expected exception for invalid username');
  83. } catch (Zend_Ldap_Exception $zle) {
  84. $this->assertContains('Invalid credentials', $zle->getMessage());
  85. }
  86. }
  87. public function testExplicitParamsConnect()
  88. {
  89. $host = TESTS_ZEND_LDAP_HOST;
  90. $port = 0;
  91. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  92. $port = TESTS_ZEND_LDAP_PORT;
  93. $useSsl = false;
  94. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  95. $useSsl = TESTS_ZEND_LDAP_USE_SSL;
  96. $ldap = new Zend_Ldap();
  97. try {
  98. $ldap->connect($host, $port, $useSsl)
  99. ->bind('CN=ignored,DC=example,DC=com', 'ignored');
  100. $this->fail('Expected exception for invalid username');
  101. } catch (Zend_Ldap_Exception $zle) {
  102. $this->assertContains('Invalid credentials', $zle->getMessage());
  103. }
  104. }
  105. public function testExplicitPortConnect()
  106. {
  107. $port = 389;
  108. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT)
  109. $port = TESTS_ZEND_LDAP_PORT;
  110. if (defined('TESTS_ZEND_LDAP_USE_SSL') && TESTS_ZEND_LDAP_USE_SSL)
  111. $port = 636;
  112. $ldap = new Zend_Ldap($this->_options);
  113. try {
  114. $ldap->connect(null, $port)
  115. ->bind('CN=ignored,DC=example,DC=com', 'ignored');
  116. $this->fail('Expected exception for invalid username');
  117. } catch (Zend_Ldap_Exception $zle) {
  118. $this->assertContains('Invalid credentials', $zle->getMessage());
  119. }
  120. }
  121. public function testBadPortConnect()
  122. {
  123. $options = $this->_options;
  124. $options['port'] = 10;
  125. $ldap = new Zend_Ldap($options);
  126. try {
  127. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  128. $this->fail('Expected exception for unknown username');
  129. } catch (Zend_Ldap_Exception $zle) {
  130. $this->assertContains('Can\'t contact LDAP server', $zle->getMessage());
  131. }
  132. }
  133. public function testSetOptionsConnect()
  134. {
  135. $ldap = new Zend_Ldap();
  136. $ldap->setOptions($this->_options);
  137. try {
  138. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  139. $this->fail('Expected exception for invalid username');
  140. } catch (Zend_Ldap_Exception $zle) {
  141. $this->assertContains('Invalid credentials', $zle->getMessage());
  142. }
  143. }
  144. public function testMultiConnect()
  145. {
  146. $ldap = new Zend_Ldap($this->_options);
  147. for ($i = 0; $i < 3; $i++) {
  148. try {
  149. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  150. $this->fail('Expected exception for unknown username');
  151. } catch (Zend_Ldap_Exception $zle) {
  152. $this->assertContains('Invalid credentials', $zle->getMessage());
  153. }
  154. }
  155. }
  156. public function testDisconnect()
  157. {
  158. $ldap = new Zend_Ldap($this->_options);
  159. for ($i = 0; $i < 3; $i++) {
  160. $ldap->disconnect();
  161. try {
  162. $ldap->connect()->bind('CN=ignored,DC=example,DC=com', 'ignored');
  163. $this->fail('Expected exception for unknown username');
  164. } catch (Zend_Ldap_Exception $zle) {
  165. $this->assertContains('Invalid credentials', $zle->getMessage());
  166. }
  167. }
  168. }
  169. }