BindTest.php 8.2 KB

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