2013-06-15 12:14:16 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @package testing
|
|
|
|
* @copyright (c) 2013 phpBB Group
|
|
|
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once dirname(__FILE__) . '/../mock/container_builder.php';
|
2013-07-19 20:49:24 +02:00
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/phpbb/crypto/driver/bcrypt.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/phpbb/crypto/driver/bcrypt_2y.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/phpbb/crypto/driver/salted_md5.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/phpbb/crypto/driver/phpass.php';
|
|
|
|
require_once dirname(__FILE__) . '/../../phpBB/phpbb/crypto/driver/helper.php';
|
2013-06-15 12:14:16 +02:00
|
|
|
|
|
|
|
class phpbb_crypto_manager_test extends PHPUnit_Framework_TestCase
|
|
|
|
{
|
2013-06-16 23:55:33 +02:00
|
|
|
protected $crypto_drivers;
|
|
|
|
|
2013-06-29 18:24:55 +02:00
|
|
|
protected $pw_characters = '0123456789abcdefghijklmnopqrstuvwyzABCDEFGHIJKLMNOPQRSTUVXYZ.,_!?/\\';
|
|
|
|
|
|
|
|
protected $default_pw = 'foobar';
|
|
|
|
|
2013-06-15 12:14:16 +02:00
|
|
|
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(array());
|
|
|
|
|
2013-06-16 23:55:33 +02:00
|
|
|
$this->crypto_drivers = array(
|
2013-06-15 12:14:16 +02:00
|
|
|
'crypto.driver.bcrypt' => new phpbb_crypto_driver_bcrypt($config),
|
|
|
|
'crypto.driver.bcrypt_2y' => new phpbb_crypto_driver_bcrypt_2y($config),
|
|
|
|
'crypto.driver.salted_md5' => new phpbb_crypto_driver_salted_md5($config),
|
2013-06-15 17:56:29 +02:00
|
|
|
'crypto.driver.phpass' => new phpbb_crypto_driver_phpass($config),
|
2013-06-15 12:14:16 +02:00
|
|
|
);
|
|
|
|
|
2013-06-16 23:55:33 +02:00
|
|
|
foreach ($this->crypto_drivers as $key => $driver)
|
2013-06-15 12:14:16 +02:00
|
|
|
{
|
2013-08-22 19:21:42 -05:00
|
|
|
$driver->set_name($key);
|
2013-06-15 12:14:16 +02:00
|
|
|
$this->phpbb_container->set($key, $driver);
|
|
|
|
}
|
2013-06-29 18:24:55 +02:00
|
|
|
|
2013-06-15 12:14:16 +02:00
|
|
|
// Set up avatar manager
|
2013-08-22 19:21:42 -05:00
|
|
|
$this->manager = new phpbb_crypto_manager($config, $this->phpbb_container, $this->crypto_drivers, 'crypto.driver.bcrypt_2y');
|
2013-06-15 12:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function hash_password_data()
|
|
|
|
{
|
2013-06-15 16:35:27 +02:00
|
|
|
if (version_compare(PHP_VERSION, '5.3.7', '<'))
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('', '2a', 60),
|
|
|
|
array('crypto.driver.bcrypt_2y', '2a', 60),
|
|
|
|
array('crypto.driver.bcrypt', '2a', 60),
|
|
|
|
array('crypto.driver.salted_md5', 'H', 34),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('', '2y', 60),
|
|
|
|
array('crypto.driver.bcrypt_2y', '2y', 60),
|
|
|
|
array('crypto.driver.bcrypt', '2a', 60),
|
|
|
|
array('crypto.driver.salted_md5', 'H', 34),
|
|
|
|
);
|
|
|
|
}
|
2013-06-15 12:14:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider hash_password_data
|
|
|
|
*/
|
|
|
|
public function test_hash_password($type, $prefix, $length)
|
|
|
|
{
|
2013-06-29 18:24:55 +02:00
|
|
|
$password = $this->default_pw;
|
|
|
|
$time = microtime(true);
|
|
|
|
|
|
|
|
// 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)];
|
|
|
|
}
|
2013-06-15 12:14:16 +02:00
|
|
|
}
|
2013-06-15 17:56:29 +02:00
|
|
|
|
|
|
|
public function check_password_data()
|
|
|
|
{
|
|
|
|
if (version_compare(PHP_VERSION, '5.3.7', '<'))
|
|
|
|
{
|
|
|
|
return array(
|
2013-06-29 18:24:55 +02:00
|
|
|
array('crypto.driver.bcrypt'),
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
array('crypto.driver.phpass'),
|
2013-06-15 17:56:29 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return array(
|
2013-06-29 18:24:55 +02:00
|
|
|
array('crypto.driver.bcrypt_2y'),
|
|
|
|
array('crypto.driver.bcrypt'),
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
array('crypto.driver.phpass'),
|
2013-06-15 17:56:29 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider check_password_data
|
|
|
|
*/
|
2013-06-29 18:24:55 +02:00
|
|
|
public function test_check_password($hash_type)
|
2013-06-15 17:56:29 +02:00
|
|
|
{
|
2013-06-29 18:24:55 +02:00
|
|
|
$password = $this->default_pw;
|
2013-06-15 17:56:29 +02:00
|
|
|
$time = microtime(true);
|
2013-06-27 14:29:23 +02:00
|
|
|
// Limit each test to 1 second
|
|
|
|
while ((microtime(true) - $time) < 1)
|
2013-06-15 17:56:29 +02:00
|
|
|
{
|
2013-06-29 18:24:55 +02:00
|
|
|
$hash = $this->manager->hash_password($password, $hash_type);
|
|
|
|
$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));
|
2013-06-15 17:56:29 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-16 23:55:33 +02:00
|
|
|
|
2013-07-06 00:02:14 +02:00
|
|
|
|
|
|
|
public function check_hash_exceptions_data()
|
|
|
|
{
|
|
|
|
return array(
|
2013-07-19 20:14:25 +02:00
|
|
|
array('foobar', '3858f62230ac3c915f300c664312c63f', true),
|
2013-07-06 00:02:14 +02:00
|
|
|
array('foobar', '$S$b57a939fa4f2c04413a4eea9734a0903647b7adb93181295', false),
|
|
|
|
array('foobar', '$2a\S$kkkkaakdkdiej39023903204j2k3490234jk234j02349', false),
|
2013-07-06 00:22:31 +02:00
|
|
|
array('foobar', '$H$kklk938d023k//k3023', false),
|
|
|
|
array('foobar', '$H$3PtYMgXb39lrIWkgoxYLWtRkZtY3AY/', false),
|
2013-07-06 00:43:06 +02:00
|
|
|
array('foobar', '$2a$kwiweorurlaeirw', false),
|
2013-07-06 00:02:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider check_hash_exceptions_data
|
|
|
|
*/
|
|
|
|
public function test_check_hash_exceptions($password, $hash, $expected)
|
|
|
|
{
|
|
|
|
$this->assertEquals($expected, $this->manager->check_hash($password, $hash));
|
|
|
|
}
|
|
|
|
|
2013-06-16 23:55:33 +02:00
|
|
|
public function test_hash_password_length()
|
|
|
|
{
|
|
|
|
foreach ($this->crypto_drivers as $driver)
|
|
|
|
{
|
|
|
|
$this->assertEquals(false, $driver->hash('foobar', 'foobar'));
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 14:29:23 +02:00
|
|
|
|
2013-07-06 00:02:14 +02:00
|
|
|
public function test_hash_password_8bit_bcrypt()
|
|
|
|
{
|
|
|
|
$this->assertEquals(false, $this->manager->hash_password('foobar𝄞', 'crypto.driver.bcrypt'));
|
|
|
|
}
|
|
|
|
|
2013-06-27 14:29:23 +02:00
|
|
|
public function test_combined_hash_data()
|
|
|
|
{
|
2013-06-29 00:04:02 +02:00
|
|
|
if (version_compare(PHP_VERSION, '5.3.7', '<'))
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.bcrypt'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'crypto.driver.phpass',
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.phpass', 'crypto.driver.bcrypt'),
|
|
|
|
),
|
2013-07-06 00:02:14 +02:00
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
false,
|
|
|
|
),
|
2013-06-29 00:04:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.bcrypt_2y'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.bcrypt'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'crypto.driver.phpass',
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.bcrypt_2y', 'crypto.driver.bcrypt'),
|
|
|
|
),
|
2013-07-06 00:02:14 +02:00
|
|
|
array(
|
|
|
|
'crypto.driver.salted_md5',
|
|
|
|
array('crypto.driver.salted_md5'),
|
|
|
|
false,
|
|
|
|
),
|
2013-06-29 00:04:02 +02:00
|
|
|
);
|
|
|
|
}
|
2013-06-27 14:29:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider test_combined_hash_data
|
|
|
|
*/
|
2013-07-06 00:02:14 +02:00
|
|
|
public function test_combined_hash_password($first_type, $second_type, $expected = true)
|
2013-06-27 14:29:23 +02:00
|
|
|
{
|
2013-06-29 18:24:55 +02:00
|
|
|
$password = $this->default_pw;
|
2013-06-27 14:29:23 +02:00
|
|
|
$time = microtime(true);
|
|
|
|
// Limit each test to 1 second
|
|
|
|
while ((microtime(true) - $time) < 1)
|
|
|
|
{
|
2013-06-29 18:24:55 +02:00
|
|
|
$hash = $this->manager->hash_password($password, $first_type);
|
|
|
|
$combined_hash = $this->manager->hash_password($hash, $second_type);
|
2013-07-06 00:02:14 +02:00
|
|
|
$this->assertEquals($expected, $this->manager->check_hash($password, $combined_hash));
|
2013-06-29 18:24:55 +02:00
|
|
|
$password .= $this->pw_characters[mt_rand(0, 66)];
|
|
|
|
$this->assertEquals(false, $this->manager->check_hash($password, $combined_hash));
|
2013-07-06 00:02:14 +02:00
|
|
|
|
|
|
|
// If we are expecting the check to fail then there is
|
|
|
|
// no need to run this more than once
|
|
|
|
if (!$expected)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2013-06-27 14:29:23 +02:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 12:14:16 +02:00
|
|
|
}
|