OriginalBindTest.php 6.0 KB

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