CanonTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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-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. * Zend_Ldap
  28. */
  29. require_once 'Zend/Ldap.php';
  30. /* Note: The ldap_connect function does not actually try to connect. This
  31. * is why many tests attempt to bind with invalid credentials. If the
  32. * bind returns 'Invalid credentials' we know the transport related work
  33. * was successful.
  34. */
  35. /**
  36. * @category Zend
  37. * @package Zend_Ldap
  38. * @subpackage UnitTests
  39. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  40. * @license http://framework.zend.com/license/new-bsd New BSD License
  41. */
  42. class Zend_Ldap_CanonTest extends PHPUnit_Framework_TestCase
  43. {
  44. protected $_options = null;
  45. protected $_principalName = TESTS_ZEND_LDAP_PRINCIPAL_NAME;
  46. protected $_names = array();
  47. public function setUp()
  48. {
  49. $this->_options = array(
  50. 'host' => TESTS_ZEND_LDAP_HOST,
  51. 'username' => TESTS_ZEND_LDAP_USERNAME,
  52. 'password' => TESTS_ZEND_LDAP_PASSWORD,
  53. 'baseDn' => TESTS_ZEND_LDAP_BASE_DN,
  54. );
  55. if (defined('TESTS_ZEND_LDAP_PORT') && TESTS_ZEND_LDAP_PORT != 389)
  56. $this->_options['port'] = TESTS_ZEND_LDAP_PORT;
  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_DOMAIN_NAME'))
  62. $this->_options['accountDomainName'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  63. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT'))
  64. $this->_options['accountDomainNameShort'] = TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT;
  65. if (defined('TESTS_ZEND_LDAP_ALT_USERNAME')) {
  66. $this->_names[Zend_Ldap::ACCTNAME_FORM_USERNAME] = TESTS_ZEND_LDAP_ALT_USERNAME;
  67. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME')) {
  68. $this->_names[Zend_Ldap::ACCTNAME_FORM_PRINCIPAL] =
  69. TESTS_ZEND_LDAP_ALT_USERNAME . '@' . TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME;
  70. }
  71. if (defined('TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT')) {
  72. $this->_names[Zend_Ldap::ACCTNAME_FORM_BACKSLASH] =
  73. TESTS_ZEND_LDAP_ACCOUNT_DOMAIN_NAME_SHORT . '\\' . TESTS_ZEND_LDAP_ALT_USERNAME;
  74. }
  75. }
  76. }
  77. public function testPlainCanon()
  78. {
  79. $ldap = new Zend_Ldap($this->_options);
  80. /* This test tries to canonicalize each name (uname, uname@example.com,
  81. * EXAMPLE\uname) to each of the 3 forms (username, principal and backslash)
  82. * for a total of canonicalizations.
  83. */
  84. foreach ($this->_names as $_form => $name) {
  85. foreach ($this->_names as $form => $_name) {
  86. $ret = $ldap->getCanonicalAccountName($name, $form);
  87. $this->assertTrue($ret === $this->_names[$form]);
  88. }
  89. }
  90. }
  91. public function testInvalidAccountCanon()
  92. {
  93. $ldap = new Zend_Ldap($this->_options);
  94. try {
  95. $ldap->bind('invalid', 'invalid');
  96. } catch (Zend_Ldap_Exception $zle) {
  97. $msg = $zle->getMessage();
  98. $this->assertTrue(strstr($msg, 'Invalid credentials') || strstr($msg, 'No such object'));
  99. }
  100. }
  101. public function testDnCanon()
  102. {
  103. $ldap = new Zend_Ldap($this->_options);
  104. $name = $ldap->getCanonicalAccountName(TESTS_ZEND_LDAP_ALT_DN, Zend_Ldap::ACCTNAME_FORM_DN);
  105. }
  106. public function testMismatchDomainBind()
  107. {
  108. $ldap = new Zend_Ldap($this->_options);
  109. try {
  110. $ldap->bind('BOGUS\\doesntmatter', 'doesntmatter');
  111. } catch (Zend_Ldap_Exception $zle) {
  112. $this->assertTrue($zle->getCode() == Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH);
  113. }
  114. }
  115. public function testBindCanon()
  116. {
  117. /**
  118. * @todo test accountCanonicalForm option
  119. */
  120. }
  121. }