OnlineTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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_Auth
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 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. * @see Zend_Ldap
  24. */
  25. require_once 'Zend/Ldap.php';
  26. /**
  27. * @see Zend_Auth_Adapter_Ldap
  28. */
  29. require_once 'Zend/Auth/Adapter/Ldap.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Auth
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Auth
  37. */
  38. class Zend_Auth_Adapter_Ldap_OnlineTest extends PHPUnit_Framework_TestCase
  39. {
  40. /**
  41. * LDAP connection options
  42. *
  43. * @var array
  44. */
  45. protected $_options = array();
  46. /**
  47. * @var array
  48. */
  49. protected $_names = array();
  50. public function setUp()
  51. {
  52. $this->_options = array(
  53. 'host' => TESTS_ZEND_LDAP_HOST,
  54. 'username' => TESTS_ZEND_LDAP_USERNAME,
  55. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  56. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  57. );
  58. if (defined('TESTS_ZEND_LDAP_PORT'))
  59. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  60. if (defined('TESTS_ZEND_LDAP_USE_START_TLS'))
  61. $this->_options['useStartTls'] = TESTS_ZEND_LDAP_USE_START_TLS;
  62. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  63. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  64. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  65. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  66. if (defined('TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT'))
  67. $this->_options['accountFilterFormat'] = TESTS_ZEND_LDAP_ACCOUNT_FILTER_FORMAT;
  68. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME'))
  69. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  70. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  71. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  72. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME')) {
  73. $this->_names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
  74. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
  75. $this->_names[Zend_Ldap::ACCTNAME_FORM_PRINCIPAL] =
  76. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  77. }
  78. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT')) {
  79. $this->_names[Zend_Ldap::ACCTNAME_FORM_BACKSLASH] =
  80. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME;
  81. }
  82. }
  83. }
  84. public function testSimpleAuth()
  85. {
  86. $adapter = new Zend_Auth_Adapter_Ldap(
  87. array($this->_options),
  88. TESTS_ZEND_LDAP_ALT_USERNAME,
  89. TESTS_ZEND_LDAP_ALT_PASSWORD
  90. );
  91. $result = $adapter->authenticate();
  92. $this->assertTrue($result instanceof Zend_Auth_Result);
  93. $this->assertTrue($result->isValid());
  94. $this->assertTrue($result->getCode() == Zend_Auth_Result::SUCCESS);
  95. }
  96. public function testCanonAuth()
  97. {
  98. /* This test authenticates with each of the account name forms
  99. * (uname, uname@example.com, EXAMPLE\uname) AND it does so with
  100. * the accountCanonicalForm set to each of the account name forms
  101. * (e.g. authenticate with uname@example.com but getIdentity() returns
  102. * EXAMPLE\uname). A total of 9 authentications are performed.
  103. */
  104. foreach ($this->_names as $form => $formName) {
  105. $options = $this->_options;
  106. $options['accountCanonicalForm'] = $form;
  107. $adapter = new Zend_Auth_Adapter_Ldap(array($options));
  108. $adapter->setPassword(TESTS_ZEND_LDAP_ALT_PASSWORD);
  109. foreach ($this->_names as $username) {
  110. $adapter->setUsername($username);
  111. $result = $adapter->authenticate();
  112. $this->assertTrue($result instanceof Zend_Auth_Result);
  113. $this->assertTrue($result->isValid());
  114. $this->assertTrue($result->getCode() == Zend_Auth_Result::SUCCESS);
  115. $this->assertTrue($result->getIdentity() === $formName);
  116. }
  117. }
  118. }
  119. public function testInvalidPassAuth()
  120. {
  121. $adapter = new Zend_Auth_Adapter_Ldap(
  122. array($this->_options),
  123. TESTS_ZEND_LDAP_ALT_USERNAME,
  124. 'invalid'
  125. );
  126. $result = $adapter->authenticate();
  127. $this->assertTrue($result instanceof Zend_Auth_Result);
  128. $this->assertTrue($result->isValid() === false);
  129. $this->assertTrue($result->getCode() == Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID);
  130. }
  131. public function testInvalidUserAuth()
  132. {
  133. $adapter = new Zend_Auth_Adapter_Ldap(
  134. array($this->_options),
  135. 'invalid',
  136. 'doesntmatter'
  137. );
  138. $result = $adapter->authenticate();
  139. $this->assertTrue($result instanceof Zend_Auth_Result);
  140. $this->assertTrue($result->isValid() === false);
  141. $this->assertTrue(
  142. $result->getCode() == Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND ||
  143. $result->getCode() == Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
  144. );
  145. }
  146. public function testMismatchDomainAuth()
  147. {
  148. $adapter = new Zend_Auth_Adapter_Ldap(
  149. array($this->_options),
  150. 'EXAMPLE\\doesntmatter',
  151. 'doesntmatter'
  152. );
  153. $result = $adapter->authenticate();
  154. $this->assertTrue($result instanceof Zend_Auth_Result);
  155. $this->assertFalse($result->isValid());
  156. $this->assertThat($result->getCode(), $this->lessThanOrEqual(Zend_Auth_Result::FAILURE));
  157. $messages = $result->getMessages();
  158. $this->assertContains('not found', $messages[0]);
  159. }
  160. public function testAccountObjectRetrieval()
  161. {
  162. $adapter = new Zend_Auth_Adapter_Ldap(
  163. array($this->_options),
  164. TESTS_ZEND_LDAP_ALT_USERNAME,
  165. TESTS_ZEND_LDAP_ALT_PASSWORD
  166. );
  167. $result = $adapter->authenticate();
  168. $account = $adapter->getAccountObject();
  169. $this->assertTrue($result->isValid());
  170. $this->assertTrue($account instanceof stdClass);
  171. $this->assertEquals(TESTS_ZEND_LDAP_ALT_DN, $account->dn);
  172. }
  173. public function testAccountObjectRetrievalWithOmittedAttributes()
  174. {
  175. $adapter = new Zend_Auth_Adapter_Ldap(
  176. array($this->_options),
  177. TESTS_ZEND_LDAP_ALT_USERNAME,
  178. TESTS_ZEND_LDAP_ALT_PASSWORD
  179. );
  180. $result = $adapter->authenticate();
  181. $account = $adapter->getAccountObject(array(), array('userPassword'));
  182. $this->assertTrue($account instanceof stdClass);
  183. $this->assertFalse(isset($account->userpassword));
  184. }
  185. }