OnlineTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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-2008 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-2008 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') && TESTS_ZEND_LDAP_PORT != 389)
  62. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  63. if (defined('TESTS_ZEND_LDAP_USE_SSL'))
  64. $this->_options['useSsl'] = TESTS_ZEND_LDAP_USE_SSL;
  65. if (defined('TESTS_ZEND_LDAP_BIND_REQUIRES_DN'))
  66. $this->_options['bindRequiresDn'] = TESTS_ZEND_LDAP_BIND_REQUIRES_DN;
  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->_names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
  73. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
  74. $this->_names[Zend_Ldap::ACCTNAME_FORM_PRINCIPAL] =
  75. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  76. }
  77. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT')) {
  78. $this->_names[Zend_Ldap::ACCTNAME_FORM_BACKSLASH] =
  79. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME;
  80. }
  81. }
  82. }
  83. public function testSimpleAuth()
  84. {
  85. $adapter = new Zend_Auth_Adapter_Ldap(
  86. array($this->_options),
  87. TESTS_ZEND_LDAP_ALT_USERNAME,
  88. TESTS_ZEND_LDAP_ALT_PASSWORD
  89. );
  90. $result = $adapter->authenticate();
  91. $this->assertTrue($result instanceof Zend_Auth_Result);
  92. $this->assertTrue($result->isValid());
  93. $this->assertTrue($result->getCode() == Zend_Auth_Result::SUCCESS);
  94. }
  95. public function testCanonAuth()
  96. {
  97. /* This test authenticates with each of the account name forms
  98. * (uname, uname@example.com, EXAMPLE\uname) AND it does so with
  99. * the accountCanonicalForm set to each of the account name forms
  100. * (e.g. authenticate with uname@example.com but getIdentity() returns
  101. * EXAMPLE\uname). A total of 9 authentications are performed.
  102. */
  103. foreach ($this->_names as $form => $formName) {
  104. $options = $this->_options;
  105. $options['accountCanonicalForm'] = $form;
  106. $adapter = new Zend_Auth_Adapter_Ldap(array($options));
  107. $adapter->setPassword(TESTS_ZEND_LDAP_ALT_PASSWORD);
  108. foreach ($this->_names as $username) {
  109. $adapter->setUsername($username);
  110. $result = $adapter->authenticate();
  111. $this->assertTrue($result instanceof Zend_Auth_Result);
  112. $this->assertTrue($result->isValid());
  113. $this->assertTrue($result->getCode() == Zend_Auth_Result::SUCCESS);
  114. $this->assertTrue($result->getIdentity() === $formName);
  115. }
  116. }
  117. }
  118. public function testInvalidPassAuth()
  119. {
  120. $adapter = new Zend_Auth_Adapter_Ldap(
  121. array($this->_options),
  122. TESTS_ZEND_LDAP_ALT_USERNAME,
  123. 'invalid'
  124. );
  125. $result = $adapter->authenticate();
  126. $this->assertTrue($result instanceof Zend_Auth_Result);
  127. $this->assertTrue($result->isValid() === false);
  128. $this->assertTrue($result->getCode() == Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID);
  129. }
  130. public function testInvalidUserAuth()
  131. {
  132. $adapter = new Zend_Auth_Adapter_Ldap(
  133. array($this->_options),
  134. 'invalid',
  135. 'doesntmatter'
  136. );
  137. $result = $adapter->authenticate();
  138. $this->assertTrue($result instanceof Zend_Auth_Result);
  139. $this->assertTrue($result->isValid() === false);
  140. $this->assertTrue(
  141. $result->getCode() == Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND ||
  142. $result->getCode() == Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID
  143. );
  144. }
  145. public function testMismatchDomainAuth()
  146. {
  147. $adapter = new Zend_Auth_Adapter_Ldap(
  148. array($this->_options),
  149. 'EXAMPLE\\doesntmatter',
  150. 'doesntmatter'
  151. );
  152. $result = $adapter->authenticate();
  153. $this->assertTrue($result instanceof Zend_Auth_Result);
  154. $this->assertFalse($result->isValid());
  155. $this->assertThat($result->getCode(), $this->lessThanOrEqual(Zend_Auth_Result::FAILURE));
  156. $messages = $result->getMessages();
  157. $this->assertContains('not found', $messages[0]);
  158. }
  159. }