BindTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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_BindTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_options = null;
  45. protected $_principalName = TESTS_ZEND_LDAP_PRINCIPAL_NAME;
  46. protected $_altUsername = TESTS_ZEND_LDAP_PRINCIPAL_NAME;
  47. protected $_bindRequiresDn = false;
  48. public function setUp()
  49. {
  50. $this->_options = array(
  51. 'host' => TESTS_ZEND_LDAP_HOST,
  52. 'username' => TESTS_ZEND_LDAP_USERNAME,
  53. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  54. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  55. );
  56. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  57. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  58. if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
  59. $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
  60. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  61. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  62. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  63. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  64. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME'))
  65. $this->_altUsername = TESTS_ZEND_LDAP_ALT_USERNAME;
  66. if (isset($this->_options['bindRequiresDn']))
  67. $this->_bindRequiresDn = $this->_options['bindRequiresDn'];
  68. }
  69. public function testEmptyOptionsBind()
  70. {
  71. $ldap = new Zend_Ldap(array());
  72. try {
  73. $ldap->bind();
  74. $this->fail('Expected exception for empty options');
  75. } catch (Zend_Ldap_Exception $zle) {
  76. $this->assertContains('A host parameter is required', $zle->getMessage());
  77. }
  78. }
  79. public function testAnonymousBind()
  80. {
  81. $options = $this->_options;
  82. unset($options['password']);
  83. $ldap = new Zend_Ldap($options);
  84. try {
  85. $ldap->bind();
  86. } catch (Zend_Ldap_Exception $zle) {
  87. // or I guess the server doesn't allow unauthenticated binds
  88. $this->assertContains('unauthenticated bind', $zle->getMessage());
  89. }
  90. }
  91. public function testNoBaseDnBind()
  92. {
  93. $options = $this->_options;
  94. unset($options['baseDn']);
  95. $options['bindRequiresDn'] = true;
  96. $ldap = new Zend_Ldap($options);
  97. try {
  98. $ldap->bind('invalid', 'ignored');
  99. $this->fail('Expected exception for baseDn missing');
  100. } catch (Zend_Ldap_Exception $zle) {
  101. $this->assertContains('Base DN not set', $zle->getMessage());
  102. }
  103. }
  104. public function testNoDomainNameBind()
  105. {
  106. $options = $this->_options;
  107. unset($options['baseDn']);
  108. $options['bindRequiresDn'] = false;
  109. $ldap = new Zend_Ldap($options);
  110. try {
  111. $ldap->bind('invalid', 'ignored');
  112. $this->fail('Expected exception for missing accountDomainName');
  113. } catch (Zend_Ldap_Exception $zle) {
  114. $this->assertContains('Option required: accountDomainName', $zle->getMessage());
  115. }
  116. }
  117. public function testPlainBind()
  118. {
  119. $ldap = new Zend_Ldap($this->_options);
  120. $ldap->bind();
  121. }
  122. public function testConnectBind()
  123. {
  124. $ldap = new Zend_Ldap($this->_options);
  125. $ldap->connect()->bind();
  126. }
  127. public function testExplicitParamsBind()
  128. {
  129. $options = $this->_options;
  130. $username = $options['username'];
  131. $password = $options['password'];
  132. unset($options['username']);
  133. unset($options['password']);
  134. $ldap = new Zend_Ldap($options);
  135. $ldap->bind($username, $password);
  136. }
  137. public function testRequiresDnBind()
  138. {
  139. $options = $this->_options;
  140. /* Fixup filter since bindRequiresDn is used to determine default accountFilterFormat
  141. */
  142. if (!isset($options['accountFilterFormat']) && $this->_bindRequiresDn === false)
  143. $options['accountFilterFormat'] = '(&(objectClass=user)(sAMAccountName=%s))';
  144. $options['bindRequiresDn'] = true;
  145. $ldap = new Zend_Ldap($options);
  146. try {
  147. $ldap->bind($this->_altUsername, 'invalid');
  148. } catch (Zend_Ldap_Exception $zle) {
  149. $message = str_replace("\n", " ", $zle->getMessage());
  150. $this->assertContains('Invalid credentials', $message);
  151. }
  152. }
  153. public function testRequiresDnWithoutDnBind()
  154. {
  155. $options = $this->_options;
  156. /* Fixup filter since bindRequiresDn is used to determine default accountFilterFormat
  157. */
  158. if (!isset($options['accountFilterFormat']) && !$this->_bindRequiresDn)
  159. $options['accountFilterFormat'] = '(&(objectClass=user)(sAMAccountName=%s))';
  160. $options['bindRequiresDn'] = true;
  161. unset($options['username']);
  162. $ldap = new Zend_Ldap($options);
  163. try {
  164. $ldap->bind($this->_principalName);
  165. } catch (Zend_Ldap_Exception $zle) {
  166. /* Note that if your server actually allows anonymous binds this test will fail.
  167. */
  168. $this->assertContains('Failed to retrieve DN', $zle->getMessage());
  169. }
  170. }
  171. }