2010-10-15 05:03:19 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2018-11-13 13:08:28 +01:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\PasswordValidator;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class PasswordValidatorTest extends SapphireTest
|
|
|
|
{
|
2016-12-29 12:42:52 +01:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $usesDatabase = true;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-11-13 13:08:28 +01:00
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Unset framework default values
|
|
|
|
PasswordValidator::config()
|
|
|
|
->remove('min_length')
|
|
|
|
->remove('historic_count');
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testValidate()
|
|
|
|
{
|
|
|
|
$v = new PasswordValidator();
|
|
|
|
$r = $v->validate('', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Empty password is valid by default');
|
|
|
|
|
|
|
|
$r = $v->validate('mypassword', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Non-Empty password is valid by default');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMinLength()
|
|
|
|
{
|
|
|
|
$v = new PasswordValidator();
|
|
|
|
|
2018-04-18 00:35:31 +02:00
|
|
|
$v->setMinLength(4);
|
2016-12-16 05:34:21 +01:00
|
|
|
$r = $v->validate('123', new Member());
|
|
|
|
$this->assertFalse($r->isValid(), 'Password too short');
|
|
|
|
|
2018-04-18 00:35:31 +02:00
|
|
|
$v->setMinLength(4);
|
2016-12-16 05:34:21 +01:00
|
|
|
$r = $v->validate('1234', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Password long enough');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateMinScore()
|
|
|
|
{
|
2018-04-18 00:35:31 +02:00
|
|
|
// Set both score and set of tests
|
2016-12-16 05:34:21 +01:00
|
|
|
$v = new PasswordValidator();
|
2018-04-18 00:35:31 +02:00
|
|
|
$v->setMinTestScore(3);
|
|
|
|
$v->setTestNames(["lowercase", "uppercase", "digits", "punctuation"]);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$r = $v->validate('aA', new Member());
|
|
|
|
$this->assertFalse($r->isValid(), 'Passing too few tests');
|
|
|
|
|
|
|
|
$r = $v->validate('aA1', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests');
|
2018-04-18 00:35:31 +02:00
|
|
|
|
|
|
|
// Ensure min score without tests works (uses default tests)
|
|
|
|
$v = new PasswordValidator();
|
|
|
|
$v->setMinTestScore(3);
|
|
|
|
|
|
|
|
$r = $v->validate('aA', new Member());
|
|
|
|
$this->assertFalse($r->isValid(), 'Passing too few tests');
|
|
|
|
|
|
|
|
$r = $v->validate('aA1', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests');
|
|
|
|
|
|
|
|
// Ensure that min score is only triggered if there are any failing tests at all
|
|
|
|
$v->setMinTestScore(1000);
|
|
|
|
$r = $v->validate('aA1!', new Member());
|
|
|
|
$this->assertTrue($r->isValid(), 'Passing enough tests');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2016-12-29 12:42:52 +01:00
|
|
|
/**
|
|
|
|
* Test that a certain number of historical passwords are checked if specified
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testHistoricalPasswordCount()
|
|
|
|
{
|
2016-12-29 12:42:52 +01:00
|
|
|
$validator = new PasswordValidator;
|
2018-04-18 00:35:31 +02:00
|
|
|
$validator->setHistoricCount(3);
|
2016-12-29 12:42:52 +01:00
|
|
|
Member::set_password_validator($validator);
|
|
|
|
|
|
|
|
$member = new Member;
|
|
|
|
$member->FirstName = 'Repeat';
|
|
|
|
$member->Surname = 'Password-Man';
|
|
|
|
$member->Password = 'honk';
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
// Create a set of used passwords
|
|
|
|
$member->changePassword('foobar');
|
|
|
|
$member->changePassword('foobaz');
|
|
|
|
$member->changePassword('barbaz');
|
|
|
|
|
|
|
|
$this->assertFalse($member->changePassword('barbaz')->isValid());
|
|
|
|
$this->assertFalse($member->changePassword('foobaz')->isValid());
|
|
|
|
$this->assertFalse($member->changePassword('foobar')->isValid());
|
|
|
|
$this->assertTrue($member->changePassword('honk')->isValid());
|
|
|
|
$this->assertTrue($member->changePassword('newpassword')->isValid());
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|