BindTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. /* Note: The ldap_connect function does not actually try to connect. This
  27. * is why many tests attempt to bind with invalid credentials. If the
  28. * bind returns 'Invalid credentials' we know the transport related work
  29. * was successful.
  30. */
  31. /**
  32. * @category Zend
  33. * @package Zend_Ldap
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. * @group Zend_Ldap
  38. */
  39. class Zend_Ldap_BindTest extends PHPUnit_Framework_TestCase
  40. {
  41. protected $_options = null;
  42. protected $_principalName = TESTS_ZEND_LDAP_PRINCIPAL_NAME;
  43. protected $_altUsername = TESTS_ZEND_LDAP_ALT_USERNAME;
  44. protected $_bindRequiresDn = false;
  45. public function setUp()
  46. {
  47. $this->_options = array(
  48. 'host' => TESTS_ZEND_LDAP_HOST,
  49. 'username' => TESTS_ZEND_LDAP_USERNAME,
  50. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  51. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  52. );
  53. if (defined('TESTS_ZEND_LDAP_PORT'))
  54. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  55. if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
  56. $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
  57. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  58. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  59. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  60. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  61. if (defined('TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT'))
  62. $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
  63. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
  64. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  65. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  66. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  67. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME'))
  68. $this->_altUsername = TESTS_ZEND_LDAP_ALT_USERNAME;
  69. if (isset($this->_options['bindRequiresDn']))
  70. $this->_bindRequiresDn = $this->_options['bindRequiresDn'];
  71. }
  72. public function testEmptyOptionsBind()
  73. {
  74. $ldap = new Zend_Ldap(array());
  75. try {
  76. $ldap->bind();
  77. $this->fail('Expected exception for empty options');
  78. } catch (Zend_Ldap_Exception $zle) {
  79. $this->assertContains('A host parameter is required', $zle->getMessage());
  80. }
  81. }
  82. public function testAnonymousBind()
  83. {
  84. $options = $this->_options;
  85. unset($options['password']);
  86. $ldap = new Zend_Ldap($options);
  87. try {
  88. $ldap->bind();
  89. } catch (Zend_Ldap_Exception $zle) {
  90. // or I guess the server doesn't allow unauthenticated binds
  91. $this->assertContains('unauthenticated bind', $zle->getMessage());
  92. }
  93. }
  94. public function testNoBaseDnBind()
  95. {
  96. $options = $this->_options;
  97. unset($options['baseDn']);
  98. $options['bindRequiresDn'] = true;
  99. $ldap = new Zend_Ldap($options);
  100. try {
  101. $ldap->bind('invalid', 'ignored');
  102. $this->fail('Expected exception for baseDn missing');
  103. } catch (Zend_Ldap_Exception $zle) {
  104. $this->assertContains('Base DN not set', $zle->getMessage());
  105. }
  106. }
  107. public function testNoDomainNameBind()
  108. {
  109. $options = $this->_options;
  110. unset($options['accountDomainName']);
  111. $options['bindRequiresDn'] = false;
  112. $options['accountCanonicalForm'] = Zend_Ldap::ACCTNAME_FORM_PRINCIPAL;
  113. $ldap = new Zend_Ldap($options);
  114. try {
  115. $ldap->bind('invalid', 'ignored');
  116. $this->fail('Expected exception for missing accountDomainName');
  117. } catch (Zend_Ldap_Exception $zle) {
  118. $this->assertContains('Option required: accountDomainName', $zle->getMessage());
  119. }
  120. }
  121. public function testPlainBind()
  122. {
  123. $ldap = new Zend_Ldap($this->_options);
  124. $ldap->bind();
  125. $this->assertNotNull($ldap->getResource());
  126. }
  127. public function testConnectBind()
  128. {
  129. $ldap = new Zend_Ldap($this->_options);
  130. $ldap->connect()->bind();
  131. $this->assertNotNull($ldap->getResource());
  132. }
  133. public function testExplicitParamsBind()
  134. {
  135. $options = $this->_options;
  136. $username = $options['username'];
  137. $password = $options['password'];
  138. unset($options['username']);
  139. unset($options['password']);
  140. $ldap = new Zend_Ldap($options);
  141. $ldap->bind($username, $password);
  142. $this->assertNotNull($ldap->getResource());
  143. }
  144. public function testRequiresDnBind()
  145. {
  146. $options = $this->_options;
  147. $options['bindRequiresDn'] = true;
  148. $ldap = new Zend_Ldap($options);
  149. try {
  150. $ldap->bind($this->_altUsername, 'invalid');
  151. $this->fail('Expected exception not thrown');
  152. } catch (Zend_Ldap_Exception $zle) {
  153. $this->assertContains('Invalid credentials', $zle->getMessage());
  154. }
  155. }
  156. public function testRequiresDnWithoutDnBind()
  157. {
  158. $options = $this->_options;
  159. $options['bindRequiresDn'] = true;
  160. unset($options['username']);
  161. $ldap = new Zend_Ldap($options);
  162. try {
  163. $ldap->bind($this->_principalName);
  164. $this->fail('Expected exception not thrown');
  165. } catch (Zend_Ldap_Exception $zle) {
  166. /* Note that if your server actually allows anonymous binds this test will fail.
  167. */
  168. $this->assertContains('Failed to retrieve DN', $zle->getMessage());
  169. }
  170. }
  171. public function testBindWithEmptyPassword()
  172. {
  173. $options = $this->_options;
  174. $options['allowEmptyPassword'] = false;
  175. $ldap = new Zend_Ldap($options);
  176. try {
  177. $ldap->bind($this->_altUsername, '');
  178. $this->fail('Expected exception for empty password');
  179. } catch (Zend_Ldap_Exception $zle) {
  180. $this->assertContains('Empty password not allowed - see allowEmptyPassword option.',
  181. $zle->getMessage());
  182. }
  183. $options['allowEmptyPassword'] = true;
  184. $ldap = new Zend_Ldap($options);
  185. try {
  186. $ldap->bind($this->_altUsername, '');
  187. } catch (Zend_Ldap_Exception $zle) {
  188. if ($zle->getMessage() ===
  189. 'Empty password not allowed - see allowEmptyPassword option.') {
  190. $this->fail('Exception for empty password');
  191. } else {
  192. $message = $zle->getMessage();
  193. $this->assertTrue(strstr($message, 'Invalid credentials') ||
  194. strstr($message, 'Server is unwilling to perform'));
  195. return;
  196. }
  197. }
  198. $this->assertNotNull($ldap->getResource());
  199. }
  200. public function testBindWithoutDnUsernameAndDnRequired()
  201. {
  202. $options = $this->_options;
  203. $options['username'] = TESTS_ZEND_LDAP_ALT_USERNAME;
  204. $options['bindRequiresDn'] = true;
  205. $ldap = new Zend_Ldap($options);
  206. try {
  207. $ldap->bind();
  208. $this->fail('Expected exception for empty password');
  209. } catch (Zend_Ldap_Exception $zle) {
  210. $this->assertContains('Binding requires username in DN form',
  211. $zle->getMessage());
  212. }
  213. }
  214. /**
  215. * @group ZF-8259
  216. */
  217. public function testBoundUserIsFalseIfNotBoundToLDAP()
  218. {
  219. $ldap = new Zend_Ldap($this->_options);
  220. $this->assertFalse($ldap->getBoundUser());
  221. }
  222. /**
  223. * @group ZF-8259
  224. */
  225. public function testBoundUserIsReturnedAfterBinding()
  226. {
  227. $ldap = new Zend_Ldap($this->_options);
  228. $ldap->bind();
  229. $this->assertEquals(TESTS_ZEND_LDAP_USERNAME, $ldap->getBoundUser());
  230. }
  231. /**
  232. * @group ZF-8259
  233. */
  234. public function testResourceIsAlwaysReturned()
  235. {
  236. $ldap = new Zend_Ldap($this->_options);
  237. $this->assertNotNull($ldap->getResource());
  238. $this->assertTrue(is_resource($ldap->getResource()));
  239. $this->assertEquals(TESTS_ZEND_LDAP_USERNAME, $ldap->getBoundUser());
  240. }
  241. /**
  242. * @see https://net.educause.edu/ir/library/pdf/csd4875.pdf
  243. */
  244. public function testBindWithNullPassword()
  245. {
  246. $ldap = new Zend_Ldap($this->_options);
  247. $this->setExpectedException('Zend_Ldap_Exception', 'Invalid credentials');
  248. $ldap->bind($this->_altUsername, "\0invalidpassword");
  249. }
  250. }