3 * Testing password-policy check functions
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 class PasswordPolicyChecksTest
extends MediaWikiTestCase
{
26 * @covers PasswordPolicyChecks::checkMinimalPasswordLength
28 public function testCheckMinimalPasswordLength() {
29 $statusOK = PasswordPolicyChecks
::checkMinimalPasswordLength(
31 User
::newFromName( 'user' ), // User
32 'password' // password
34 $this->assertTrue( $statusOK->isGood(), 'Password is longer than minimal policy' );
35 $statusShort = PasswordPolicyChecks
::checkMinimalPasswordLength(
37 User
::newFromName( 'user' ), // User
38 'password' // password
41 $statusShort->isGood(),
42 'Password is shorter than minimal policy'
46 'Password is shorter than minimal policy, not fatal'
51 * @covers PasswordPolicyChecks::checkMinimumPasswordLengthToLogin
53 public function testCheckMinimumPasswordLengthToLogin() {
54 $statusOK = PasswordPolicyChecks
::checkMinimumPasswordLengthToLogin(
56 User
::newFromName( 'user' ), // User
57 'password' // password
59 $this->assertTrue( $statusOK->isGood(), 'Password is longer than minimal policy' );
60 $statusShort = PasswordPolicyChecks
::checkMinimumPasswordLengthToLogin(
62 User
::newFromName( 'user' ), // User
63 'password' // password
66 $statusShort->isGood(),
67 'Password is shorter than minimum login policy'
71 'Password is shorter than minimum login policy, fatal'
76 * @covers PasswordPolicyChecks::checkMaximalPasswordLength
78 public function testCheckMaximalPasswordLength() {
79 $statusOK = PasswordPolicyChecks
::checkMaximalPasswordLength(
81 User
::newFromName( 'user' ), // User
82 'password' // password
84 $this->assertTrue( $statusOK->isGood(), 'Password is shorter than maximal policy' );
85 $statusLong = PasswordPolicyChecks
::checkMaximalPasswordLength(
87 User
::newFromName( 'user' ), // User
88 'password' // password
90 $this->assertFalse( $statusLong->isGood(),
91 'Password is longer than maximal policy'
93 $this->assertFalse( $statusLong->isOK(),
94 'Password is longer than maximal policy, fatal'
99 * @covers PasswordPolicyChecks::checkPasswordCannotMatchUsername
101 public function testCheckPasswordCannotMatchUsername() {
102 $statusOK = PasswordPolicyChecks
::checkPasswordCannotMatchUsername(
104 User
::newFromName( 'user' ), // User
105 'password' // password
107 $this->assertTrue( $statusOK->isGood(), 'Password does not match username' );
108 $statusLong = PasswordPolicyChecks
::checkPasswordCannotMatchUsername(
110 User
::newFromName( 'user' ), // User
113 $this->assertFalse( $statusLong->isGood(), 'Password matches username' );
114 $this->assertTrue( $statusLong->isOK(), 'Password matches username, not fatal' );
118 * @covers PasswordPolicyChecks::checkPasswordCannotMatchBlacklist
120 public function testCheckPasswordCannotMatchBlacklist() {
121 $statusOK = PasswordPolicyChecks
::checkPasswordCannotMatchBlacklist(
122 true, // policy value
123 User
::newFromName( 'Username' ), // User
124 'AUniquePassword' // password
126 $this->assertTrue( $statusOK->isGood(), 'Password is not on blacklist' );
127 $statusLong = PasswordPolicyChecks
::checkPasswordCannotMatchBlacklist(
128 true, // policy value
129 User
::newFromName( 'Useruser1' ), // User
130 'Passpass1' // password
132 $this->assertFalse( $statusLong->isGood(), 'Password matches blacklist' );
133 $this->assertTrue( $statusLong->isOK(), 'Password matches blacklist, not fatal' );
136 public static function providePopularBlacklist() {
138 [ false, 'sitename' ],
139 [ false, 'password' ],
141 [ true, 'hqY98gCZ6qM8s8' ],
146 * @covers PasswordPolicyChecks::checkPopularPasswordBlacklist
147 * @dataProvider providePopularBlacklist
149 public function testCheckPopularPasswordBlacklist( $expected, $password ) {
151 $this->setMwGlobals( [
152 'wgSitename' => 'sitename',
153 'wgPopularPasswordFile' => "$IP/serialized/commonpasswords.cdb"
155 $user = User
::newFromName( 'username' );
156 $status = PasswordPolicyChecks
::checkPopularPasswordBlacklist( PHP_INT_MAX
, $user, $password );
157 $this->assertSame( $expected, $status->isGood() );