OnlineTest.php 6.9 KB

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