OriginalBindTest.php 6.1 KB

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