1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
<?php
/**
* Testing password-policy check functions
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Tests\Unit;
use MediaWiki\Password\PasswordPolicyChecks;
use MediaWiki\User\User;
use MediaWikiUnitTestCase;
use PHPUnit\Framework\MockObject\MockObject;
/**
* Split from \PasswordPolicyChecksTest integration tests
*
* @coversDefaultClass \MediaWiki\Password\PasswordPolicyChecks
*/
class PasswordPolicyChecksTest extends MediaWikiUnitTestCase {
/**
* @param string $name
* @return User|MockObject
*/
private function getUser( $name = 'user' ) {
$user = $this->createMock( User::class );
$user->method( 'getName' )->willReturn( $name );
return $user;
}
/**
* @covers ::checkMinimalPasswordLength
*/
public function testCheckMinimalPasswordLength() {
$statusOK = PasswordPolicyChecks::checkMinimalPasswordLength(
3, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' );
$statusShort = PasswordPolicyChecks::checkMinimalPasswordLength(
10, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusWarning( 'passwordtooshort', $statusShort,
'Password is shorter than minimal policy, not fatal'
);
}
/**
* @covers ::checkMinimumPasswordLengthToLogin
*/
public function testCheckMinimumPasswordLengthToLogin() {
$statusOK = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin(
3, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusGood( $statusOK, 'Password is longer than minimal policy' );
$statusShort = PasswordPolicyChecks::checkMinimumPasswordLengthToLogin(
10, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusError( 'passwordtooshort', $statusShort,
'Password is shorter than minimum login policy, fatal'
);
}
/**
* @covers ::checkMaximalPasswordLength
*/
public function testCheckMaximalPasswordLength() {
$statusOK = PasswordPolicyChecks::checkMaximalPasswordLength(
100, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusGood( $statusOK, 'Password is shorter than maximal policy' );
$statusLong = PasswordPolicyChecks::checkMaximalPasswordLength(
4, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusError( 'passwordtoolong', $statusLong,
'Password is longer than maximal policy, fatal'
);
}
/**
* @covers ::checkPasswordCannotBeSubstringInUsername
*/
public function testCheckPasswordCannotBeSubstringInUsername() {
$statusOK = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername(
1, // policy value
$this->getUser(), // User
'password' // password
);
$this->assertStatusGood( $statusOK, 'Password is not a substring of username' );
$statusLong = PasswordPolicyChecks::checkPasswordCannotBeSubstringInUsername(
1, // policy value
$this->getUser( '123user123' ), // User
'user' // password
);
$this->assertStatusWarning( 'password-substring-username-match', $statusLong,
'Password is a substring of username, not fatal' );
}
/**
* @covers ::checkPasswordCannotMatchDefaults
* @dataProvider provideCheckPasswordCannotMatchDefaults
*/
public function testCheckPasswordCannotMatchDefaults(
bool $failureExpected,
bool $policyValue,
string $username,
string $password
) {
$status = PasswordPolicyChecks::checkPasswordCannotMatchDefaults(
$policyValue,
$this->getUser( $username ),
$password
);
if ( $failureExpected ) {
$this->assertStatusWarning( 'password-login-forbidden', $status );
} else {
$this->assertStatusGood( $status, 'Password is not on defaults list' );
}
}
public static function provideCheckPasswordCannotMatchDefaults() {
return [
'Unique username and password' => [ false, true, 'Unique username', 'AUniquePassword' ],
'Invalid combination' => [ true, true, 'Useruser1', 'Passpass1' ],
'Invalid password' => [ true, true, 'Whatever username', 'ExamplePassword' ],
'Uniques but no policy' => [ false, false, 'Unique username', 'AUniquePassword' ],
'Invalid combination but no policy' => [ false, false, 'Useruser1', 'Passpass1' ],
'Invalid password but no policy' => [ false, false, 'Whatever username', 'ExamplePassword' ],
];
}
/**
* @covers ::checkPasswordNotInCommonList
* @dataProvider provideCommonList
*/
public function testCheckNotInCommonList( $expected, $password ) {
$status = PasswordPolicyChecks::checkPasswordNotInCommonList(
true,
$this->getUser( 'username' ),
$password
);
$this->assertSame( $expected, $status->isGood() );
}
public static function provideCommonList() {
return [
[ false, 'testpass' ],
[ false, 'password' ],
[ false, '12345' ],
[ true, 'DKn17egcA4' ],
[ true, 'testwikijenkinspass' ],
];
}
}
|