[feature/passwords] Use randomly generated password for tests

The password will start with 'foobar' and then continue with random
characters that will be appended every iteration.

PHPBB3-11610
This commit is contained in:
Marc Alexander 2013-06-29 18:24:55 +02:00
parent ddc207fae8
commit fcb7130823

View file

@ -18,6 +18,10 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
{ {
protected $crypto_drivers; protected $crypto_drivers;
protected $pw_characters = '0123456789abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVXYZ.,_!?/\\';
protected $default_pw = 'foobar';
public function setUp() public function setUp()
{ {
global $phpbb_root_path, $phpEx; global $phpbb_root_path, $phpEx;
@ -39,10 +43,7 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
{ {
$this->phpbb_container->set($key, $driver); $this->phpbb_container->set($key, $driver);
} }
/*
$config['allow_avatar_' . get_class($this->avatar_foobar)] = true;
$config['allow_avatar_' . get_class($this->avatar_barfoo)] = false;
*/
// Set up avatar manager // Set up avatar manager
$this->manager = new phpbb_crypto_manager($config, $this->phpbb_container, $this->crypto_drivers); $this->manager = new phpbb_crypto_manager($config, $this->phpbb_container, $this->crypto_drivers);
} }
@ -74,10 +75,18 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
*/ */
public function test_hash_password($type, $prefix, $length) public function test_hash_password($type, $prefix, $length)
{ {
$hash = $this->manager->hash_password('foobar', $type); $password = $this->default_pw;
preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $hash, $match); $time = microtime(true);
$this->assertEquals($prefix, $match[1]);
$this->assertEquals($length, strlen($hash)); // Limit each test to 1 second
while ((microtime(true) - $time) < 1)
{
$hash = $this->manager->hash_password($password, $type);
preg_match('#^\$([a-zA-Z0-9\\\]*?)\$#', $hash, $match);
$this->assertEquals($prefix, $match[1]);
$this->assertEquals($length, strlen($hash));
$password .= $this->pw_characters[mt_rand(0, 66)];
}
} }
public function check_password_data() public function check_password_data()
@ -85,18 +94,18 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
if (version_compare(PHP_VERSION, '5.3.7', '<')) if (version_compare(PHP_VERSION, '5.3.7', '<'))
{ {
return array( return array(
array('foobar', 'crypto.driver.bcrypt'), array('crypto.driver.bcrypt'),
array('foobar', 'crypto.driver.salted_md5'), array('crypto.driver.salted_md5'),
array('barfoo', 'crypto.driver.phpass'), array('crypto.driver.phpass'),
); );
} }
else else
{ {
return array( return array(
array('foobar', 'crypto.driver.bcrypt_2y'), array('crypto.driver.bcrypt_2y'),
array('barfoo', 'crypto.driver.bcrypt'), array('crypto.driver.bcrypt'),
array('foobar', 'crypto.driver.salted_md5'), array('crypto.driver.salted_md5'),
array('barfoo', 'crypto.driver.phpass'), array('crypto.driver.phpass'),
); );
} }
} }
@ -104,17 +113,17 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
/** /**
* @dataProvider check_password_data * @dataProvider check_password_data
*/ */
public function test_check_password($password, $hash_type) public function test_check_password($hash_type)
{ {
$hash = $this->manager->hash_password($password, $hash_type); $password = $this->default_pw;
$test_word = $password;
$time = microtime(true); $time = microtime(true);
// Limit each test to 1 second // Limit each test to 1 second
while ((microtime(true) - $time) < 1) while ((microtime(true) - $time) < 1)
{ {
$this->assertEquals($test_word === $password, $this->manager->check_hash($test_word, $hash)); $hash = $this->manager->hash_password($password, $hash_type);
$test_word = str_shuffle($test_word); $this->assertEquals(true, $this->manager->check_hash($password, $hash));
$password .= $this->pw_characters[mt_rand(0, 66)];
$this->assertEquals(false, $this->manager->check_hash($password, $hash));
} }
} }
@ -173,17 +182,16 @@ class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
*/ */
public function test_combined_hash_password($first_type, $second_type) public function test_combined_hash_password($first_type, $second_type)
{ {
$password = 'foobar'; $password = $this->default_pw;
$test_word = $password;
$hash = $this->manager->hash_password($password, $first_type);
$combined_hash = $this->manager->hash_password($hash, $second_type);
$time = microtime(true); $time = microtime(true);
// Limit each test to 1 second // Limit each test to 1 second
while ((microtime(true) - $time) < 1) while ((microtime(true) - $time) < 1)
{ {
$this->assertEquals(($test_word === $password), $this->manager->check_hash($test_word, $combined_hash)); $hash = $this->manager->hash_password($password, $first_type);
$test_word = str_shuffle($test_word); $combined_hash = $this->manager->hash_password($hash, $second_type);
$this->assertEquals(true, $this->manager->check_hash($password, $combined_hash));
$password .= $this->pw_characters[mt_rand(0, 66)];
$this->assertEquals(false, $this->manager->check_hash($password, $combined_hash));
} }
} }
} }