mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-10 05:18:52 +00:00
[feature/passwords] Increase test coverage to 35 out ouf 36 methods
Only one small code part in the salted md5 driver can't be tested right now. Passwords helper and passwords driver helper are now fully covered by tests. PHPBB3-11610
This commit is contained in:
parent
3b6038cfcd
commit
cd74fb0946
4 changed files with 111 additions and 14 deletions
|
@ -120,12 +120,13 @@ class helper
|
|||
* Get random salt with specified length
|
||||
*
|
||||
* @param int $length Salt length
|
||||
* @param string $rand_seed Seed for random data (optional). For tests.
|
||||
*/
|
||||
public function get_random_salt($length)
|
||||
public function get_random_salt($length, $rand_seed = '/dev/urandom')
|
||||
{
|
||||
$random = '';
|
||||
|
||||
if (($fh = @fopen('/dev/urandom', 'rb')))
|
||||
if (($fh = @fopen($rand_seed, 'rb')))
|
||||
{
|
||||
$random = fread($fh, $length);
|
||||
fclose($fh);
|
||||
|
|
|
@ -46,10 +46,7 @@ class salted_md5 extends \phpbb\passwords\driver\base
|
|||
}
|
||||
else
|
||||
{
|
||||
if (($settings = $this->get_hash_settings($this->generate_salt())) === false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
$settings = $this->get_hash_settings($this->generate_salt());
|
||||
}
|
||||
|
||||
$hash = md5($settings['salt'] . $password, true);
|
||||
|
|
86
tests/passwords/drivers_test.php
Normal file
86
tests/passwords/drivers_test.php
Normal file
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* @package testing
|
||||
* @copyright (c) 2013 phpBB Group
|
||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||
*
|
||||
*/
|
||||
|
||||
class phpbb_passwords_helper_test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
// Prepare dependencies for drivers
|
||||
$config = new \phpbb\config\config(array());
|
||||
$this->driver_helper = new \phpbb\passwords\driver\helper($config);
|
||||
|
||||
$this->passwords_drivers = array(
|
||||
'passwords.driver.bcrypt' => new \phpbb\passwords\driver\bcrypt($config, $this->driver_helper),
|
||||
'passwords.driver.bcrypt_2y' => new \phpbb\passwords\driver\bcrypt_2y($config, $this->driver_helper),
|
||||
'passwords.driver.salted_md5' => new \phpbb\passwords\driver\salted_md5($config, $this->driver_helper),
|
||||
'passwords.driver.phpass' => new \phpbb\passwords\driver\phpass($config, $this->driver_helper),
|
||||
);
|
||||
|
||||
foreach ($this->passwords_drivers as $key => $driver)
|
||||
{
|
||||
$driver->set_name($key);
|
||||
}
|
||||
}
|
||||
|
||||
public function data_helper_encode64()
|
||||
{
|
||||
return array(
|
||||
array('foobar', 6, 'axqPW3aQ'),
|
||||
array('foobar', 7, 'axqPW3aQ..'),
|
||||
array('foobar', 5, 'axqPW34'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_helper_encode64
|
||||
*/
|
||||
public function test_helper_encode64($input, $length, $output)
|
||||
{
|
||||
$return = $this->driver_helper->hash_encode64($input, $length);
|
||||
$this->assertEquals($output, $return);
|
||||
}
|
||||
|
||||
public function data_get_random_salt()
|
||||
{
|
||||
return array(
|
||||
array(24, false),
|
||||
array(24, '/dev/foobar'),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider data_get_random_salt
|
||||
*/
|
||||
public function test_get_random_salt($length, $rand_seed)
|
||||
{
|
||||
$rand_string = (empty($rand_seed)) ? $this->driver_helper->get_random_salt($length) : $this->driver_helper->get_random_salt($length, $rand_seed);
|
||||
$start = microtime(true);
|
||||
|
||||
// Run each test for max. 1 second
|
||||
while ((microtime(true) - $start) < 1)
|
||||
{
|
||||
$urandom_string = (empty($rand_seed)) ? $this->driver_helper->get_random_salt($length) : $this->driver_helper->get_random_salt($length, $rand_seed);
|
||||
$this->assertEquals($length, strlen($urandom_string));
|
||||
$this->assertNotEquals($rand_string, $urandom_string);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_get_hash_settings_salted_md5()
|
||||
{
|
||||
$settings = $this->passwords_drivers['passwords.driver.salted_md5']->get_hash_settings('$H$9isfrtKXWqrz8PvztXlL3.daw4U0zI1');
|
||||
$this->assertEquals(array(
|
||||
'count' => pow(2, 11),
|
||||
'salt' => 'isfrtKXW',
|
||||
'full' => '$H$9isfrtKXW',
|
||||
),
|
||||
$settings
|
||||
);
|
||||
$this->assertEquals(false, $this->passwords_drivers['passwords.driver.salted_md5']->get_hash_settings(false));
|
||||
}
|
||||
}
|
|
@ -7,8 +7,6 @@
|
|||
*
|
||||
*/
|
||||
|
||||
require_once dirname(__FILE__) . '/../mock/container_builder.php';
|
||||
|
||||
class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $passwords_drivers;
|
||||
|
@ -19,11 +17,6 @@ class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
|
|||
|
||||
public function setUp()
|
||||
{
|
||||
global $phpbb_root_path, $phpEx;
|
||||
|
||||
// Mock phpbb_container
|
||||
$this->phpbb_container = new phpbb_mock_container_builder;
|
||||
|
||||
// Prepare dependencies for manager and driver
|
||||
$config = new \phpbb\config\config(array());
|
||||
$this->driver_helper = new \phpbb\passwords\driver\helper($config);
|
||||
|
@ -38,7 +31,6 @@ class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
|
|||
foreach ($this->passwords_drivers as $key => $driver)
|
||||
{
|
||||
$driver->set_name($key);
|
||||
$this->phpbb_container->set($key, $driver);
|
||||
}
|
||||
|
||||
$this->helper = new \phpbb\passwords\helper;
|
||||
|
@ -218,6 +210,11 @@ class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
|
|||
array('passwords.driver.salted_md5'),
|
||||
false,
|
||||
),
|
||||
array(
|
||||
'passwords.driver.bcrypt_2y',
|
||||
array('passwords.driver.salted_md4'),
|
||||
false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -257,4 +254,20 @@ class phpbb_passwords_manager_test extends PHPUnit_Framework_TestCase
|
|||
$this->assertNotEquals($first_id, $this->driver_helper->unique_id());
|
||||
}
|
||||
}
|
||||
|
||||
public function test_check_hash_with_large_input()
|
||||
{
|
||||
// 16 MB password, should be rejected quite fast
|
||||
$start_time = time();
|
||||
$this->assertFalse($this->manager->check(str_repeat('a', 1024 * 1024 * 16), '$H$9isfrtKXWqrz8PvztXlL3.daw4U0zI1'));
|
||||
$this->assertLessThanOrEqual(5, time() - $start_time);
|
||||
}
|
||||
|
||||
public function test_hash_password_with_large_input()
|
||||
{
|
||||
// 16 MB password, should be rejected quite fast
|
||||
$start_time = time();
|
||||
$this->assertFalse($this->manager->hash(str_repeat('a', 1024 * 1024 * 16)));
|
||||
$this->assertLessThanOrEqual(5, time() - $start_time);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue