OriginalBindTest.php 6.1 KB

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