OnlineTest.php 6.9 KB

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