- <?php
- * @file
- * Provides unit tests for password.inc.
- */
-
- * Unit tests for password hashing API.
- */
- class PasswordHashingTest extends BackdropWebTestCase {
- protected $profile = 'testing';
-
- function setUp() {
- require_once BACKDROP_ROOT . '/' . settings_get('password_inc', 'core/includes/password.inc');
- parent::setUp();
- }
-
-
- * Test password hashing.
- */
- function testPasswordHashing() {
-
-
- $GLOBALS['settings']['password_count_log2'] = 1;
-
- $password = 'baz';
- $account = (object) array('name' => 'foo', 'pass' => md5($password));
-
- $this->assertTrue(user_needs_new_hash($account), 'User with md5 password needs a new hash.');
-
- $old_hash = $account->pass;
- $account->pass = user_hash_password($password);
- $this->assertIdentical(_password_get_count_log2($account->pass), BACKDROP_MIN_HASH_COUNT, 'Re-hashed password has the minimum number of log2 iterations.');
- $this->assertTrue($account->pass != $old_hash, 'Password hash changed.');
- $this->assertTrue(user_check_password($password, $account), 'Password check succeeds.');
-
-
- $this->assertFalse(user_needs_new_hash($account), 'User does not need a new hash.');
-
- $GLOBALS['settings']['password_count_log2'] = BACKDROP_MIN_HASH_COUNT + 1;
- $this->assertTrue(user_needs_new_hash($account), 'User needs a new hash after incrementing the log2 count.');
-
- $old_hash = $account->pass;
- $account->pass = user_hash_password($password);
- $this->assertIdentical(_password_get_count_log2($account->pass), BACKDROP_MIN_HASH_COUNT + 1, 'Re-hashed password has the correct number of log2 iterations.');
- $this->assertTrue($account->pass != $old_hash, 'Password hash changed again.');
-
- $this->assertFalse(user_needs_new_hash($account), 'Re-hashed password does not need a new hash.');
- $this->assertTrue(user_check_password($password, $account), 'Password check succeeds with re-hashed password.');
- }
-
-
- * Verifies that passwords longer than 512 bytes are not hashed.
- */
- public function testLongPassword() {
- $password = str_repeat('x', 512);
- $result = user_hash_password($password);
- $this->assertFalse(empty($result), '512 byte long password is allowed.');
- $password = str_repeat('x', 513);
- $result = user_hash_password($password);
- $this->assertFalse($result, '513 byte long password is not allowed.');
-
- $password = str_repeat('€', 170);
- $result = user_hash_password($password);
- $this->assertFalse(empty($result), '510 byte long password is allowed.');
- $password .= 'xx';
- $this->assertFalse(empty($result), '512 byte long password is allowed.');
- $password = str_repeat('€', 171);
- $result = user_hash_password($password);
- $this->assertFalse($result, '513 byte long password is not allowed.');
- }
- }