BindTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. /* 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-2009 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_ALT_USERNAME;
  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'))
  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_ACCOUNT_FILTER_FORMAT'))
  65. $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
  66. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
  67. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  68. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  69. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  70. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME'))
  71. $this->_altUsername = TESTS_ZEND_LDAP_ALT_USERNAME;
  72. if (isset($this->_options['bindRequiresDn']))
  73. $this->_bindRequiresDn = $this->_options['bindRequiresDn'];
  74. }
  75. public function testEmptyOptionsBind()
  76. {
  77. $ldap = new Zend_Ldap(array());
  78. try {
  79. $ldap->bind();
  80. $this->fail('Expected exception for empty options');
  81. } catch (Zend_Ldap_Exception $zle) {
  82. $this->assertContains('A host parameter is required', $zle->getMessage());
  83. }
  84. }
  85. public function testAnonymousBind()
  86. {
  87. $options = $this->_options;
  88. unset($options['password']);
  89. $ldap = new Zend_Ldap($options);
  90. try {
  91. $ldap->bind();
  92. } catch (Zend_Ldap_Exception $zle) {
  93. // or I guess the server doesn't allow unauthenticated binds
  94. $this->assertContains('unauthenticated bind', $zle->getMessage());
  95. }
  96. }
  97. public function testNoBaseDnBind()
  98. {
  99. $options = $this->_options;
  100. unset($options['baseDn']);
  101. $options['bindRequiresDn'] = true;
  102. $ldap = new Zend_Ldap($options);
  103. try {
  104. $ldap->bind('invalid', 'ignored');
  105. $this->fail('Expected exception for baseDn missing');
  106. } catch (Zend_Ldap_Exception $zle) {
  107. $this->assertContains('Base DN not set', $zle->getMessage());
  108. }
  109. }
  110. public function testNoDomainNameBind()
  111. {
  112. $options = $this->_options;
  113. unset($options['accountDomainName']);
  114. $options['bindRequiresDn'] = false;
  115. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_PRINCIPAL;
  116. $ldap = new Zend_Ldap($options);
  117. try {
  118. $ldap->bind('invalid', 'ignored');
  119. $this->fail('Expected exception for missing accountDomainName');
  120. } catch (Zend_Ldap_Exception $zle) {
  121. $this->assertContains('Option required: accountDomainName', $zle->getMessage());
  122. }
  123. }
  124. public function testPlainBind()
  125. {
  126. $ldap = new Zend_Ldap($this->_options);
  127. $ldap->bind();
  128. $this->assertNotNull($ldap->getResource());
  129. }
  130. public function testConnectBind()
  131. {
  132. $ldap = new Zend_Ldap($this->_options);
  133. $ldap->connect()->bind();
  134. $this->assertNotNull($ldap->getResource());
  135. }
  136. public function testExplicitParamsBind()
  137. {
  138. $options = $this->_options;
  139. $username = $options['username'];
  140. $password = $options['password'];
  141. unset($options['username']);
  142. unset($options['password']);
  143. $ldap = new Zend_Ldap($options);
  144. $ldap->bind($username, $password);
  145. $this->assertNotNull($ldap->getResource());
  146. }
  147. public function testRequiresDnBind()
  148. {
  149. $options = $this->_options;
  150. $options['bindRequiresDn'] = true;
  151. $ldap = new Zend_Ldap($options);
  152. try {
  153. $ldap->bind($this->_altUsername, 'invalid');
  154. $this->fail('Expected exception not thrown');
  155. } catch (Zend_Ldap_Exception $zle) {
  156. $this->assertContains('Invalid credentials', $zle->getMessage());
  157. }
  158. }
  159. public function testRequiresDnWithoutDnBind()
  160. {
  161. $options = $this->_options;
  162. $options['bindRequiresDn'] = true;
  163. unset($options['username']);
  164. $ldap = new Zend_Ldap($options);
  165. try {
  166. $ldap->bind($this->_principalName);
  167. $this->fail('Expected exception not thrown');
  168. } catch (Zend_Ldap_Exception $zle) {
  169. /* Note that if your server actually allows anonymous binds this test will fail.
  170. */
  171. $this->assertContains('Failed to retrieve DN', $zle->getMessage());
  172. }
  173. }
  174. public function testBindWithEmptyPassword()
  175. {
  176. $options = $this->_options;
  177. $options['allowEmptyPassword'] = false;
  178. $ldap = new Zend_Ldap($options);
  179. try {
  180. $ldap->bind($this->_altUsername, '');
  181. $this->fail('Expected exception for empty password');
  182. } catch (Zend_Ldap_Exception $zle) {
  183. $this->assertContains('Empty password not allowed - see allowEmptyPassword option.',
  184. $zle->getMessage());
  185. }
  186. $options['allowEmptyPassword'] = true;
  187. $ldap = new Zend_Ldap($options);
  188. try {
  189. $ldap->bind($this->_altUsername, '');
  190. } catch (Zend_Ldap_Exception $zle) {
  191. if ($zle->getMessage() ===
  192. 'Empty password not allowed - see allowEmptyPassword option.') {
  193. $this->fail('Exception for empty password');
  194. } else {
  195. $message = $zle->getMessage();
  196. $this->assertTrue(strstr($message, 'Invalid credentials') ||
  197. strstr($message, 'Server is unwilling to perform'));
  198. return;
  199. }
  200. }
  201. $this->assertNotNull($ldap->getResource());
  202. }
  203. public function testBindWithoutDnUsernameAndDnRequired()
  204. {
  205. $options = $this->_options;
  206. $options['username'] = TESTS_ZEND_LDAP_ALT_USERNAME;
  207. $options['bindRequiresDn'] = true;
  208. $ldap = new Zend_Ldap($options);
  209. try {
  210. $ldap->bind();
  211. $this->fail('Expected exception for empty password');
  212. } catch (Zend_Ldap_Exception $zle) {
  213. $this->assertContains('Binding requires username in DN form',
  214. $zle->getMessage());
  215. }
  216. }
  217. }