ChangePasswordTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. */
  21. /**
  22. * Zend_Ldap_OnlineTestCase
  23. */
  24. require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'OnlineTestCase.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Ldap
  28. * @subpackage UnitTests
  29. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Zend_Ldap_ChangePasswordTest extends Zend_Ldap_OnlineTestCase
  33. {
  34. public function testAddNewUserWithPasswordOpenLdap()
  35. {
  36. if ($this->_getLdap()->getRootDse()->getServerType() !==
  37. Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP) {
  38. $this->markTestSkipped('Test can only be run on an OpenLDAP server');
  39. }
  40. $dn = $this->_createDn('uid=newuser,');
  41. $data = array();
  42. $password = 'pa$$w0rd';
  43. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'newuser', false);
  44. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'account', true);
  45. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'simpleSecurityObject', true);
  46. Zend_Ldap_Attribute::setPassword($data, $password,
  47. Zend_Ldap_Attribute::PASSWORD_HASH_SSHA, 'userPassword');
  48. try {
  49. $this->_getLdap()->add($dn, $data);
  50. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $password));
  51. $this->_getLdap()->bind();
  52. $this->_getLdap()->delete($dn);
  53. } catch (Zend_Ldap_Exception $e) {
  54. $this->_getLdap()->bind();
  55. if ($this->_getLdap()->exists($dn)) {
  56. $this->_getLdap()->delete($dn);
  57. }
  58. $this->fail($e->getMessage());
  59. }
  60. }
  61. public function testChangePasswordWithUserAccountOpenLdap()
  62. {
  63. if ($this->_getLdap()->getRootDse()->getServerType() !==
  64. Zend_Ldap_Node_RootDse::SERVER_TYPE_OPENLDAP) {
  65. $this->markTestSkipped('Test can only be run on an OpenLDAP server');
  66. }
  67. $dn = $this->_createDn('uid=newuser,');
  68. $data = array();
  69. $password = 'pa$$w0rd';
  70. Zend_Ldap_Attribute::setAttribute($data, 'uid', 'newuser', false);
  71. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'account', true);
  72. Zend_Ldap_Attribute::setAttribute($data, 'objectClass', 'simpleSecurityObject', true);
  73. Zend_Ldap_Attribute::setPassword($data, $password,
  74. Zend_Ldap_Attribute::PASSWORD_HASH_SSHA, 'userPassword');
  75. try {
  76. $this->_getLdap()->add($dn, $data);
  77. $this->_getLdap()->bind($dn, $password);
  78. $newPasswd = 'newpasswd';
  79. $newData = array();
  80. Zend_Ldap_Attribute::setPassword($newData, $newPasswd,
  81. Zend_Ldap_Attribute::PASSWORD_HASH_SHA, 'userPassword');
  82. $this->_getLdap()->update($dn, $newData);
  83. try {
  84. $this->_getLdap()->bind($dn, $password);
  85. $this->fail('Expected exception not thrown');
  86. } catch (Zend_Ldap_Exception $zle) {
  87. $message = $zle->getMessage();
  88. $this->assertTrue(strstr($message, 'Invalid credentials') ||
  89. strstr($message, 'Server is unwilling to perform'));
  90. }
  91. $this->assertType('Zend_Ldap', $this->_getLdap()->bind($dn, $newPasswd));
  92. $this->_getLdap()->bind();
  93. $this->_getLdap()->delete($dn);
  94. } catch (Zend_Ldap_Exception $e) {
  95. $this->_getLdap()->bind();
  96. if ($this->_getLdap()->exists($dn)) {
  97. $this->_getLdap()->delete($dn);
  98. }
  99. $this->fail($e->getMessage());
  100. }
  101. }
  102. }