3 * Testing framework for the password hashes
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
26 abstract class PasswordTestCase
extends MediaWikiTestCase
{
28 * @var PasswordFactory
30 protected $passwordFactory;
32 protected function setUp() {
35 $this->passwordFactory
= new PasswordFactory();
36 foreach ( $this->getTypeConfigs() as $type => $config ) {
37 $this->passwordFactory
->register( $type, $config );
42 * Return an array of configs to be used for this class's password type.
46 abstract protected function getTypeConfigs();
49 * An array of tests in the form of (bool, string, string), where the first
50 * element is whether the second parameter (a password hash) and the third
51 * parameter (a password) should match.
55 public static function providePasswordTests() {
56 throw new MWException( "Not implemented" );
60 * @dataProvider providePasswordTests
62 public function testHashing( $shouldMatch, $hash, $password ) {
63 $passwordObj = $this->passwordFactory
->newFromCiphertext( $hash );
64 $this->assertSame( $shouldMatch, $passwordObj->verify( $password ) );
68 * @dataProvider providePasswordTests
70 public function testStringSerialization( $shouldMatch, $hash, $password ) {
71 $hashObj = $this->passwordFactory
->newFromCiphertext( $hash );
72 $serialized = $hashObj->toString();
73 $unserialized = $this->passwordFactory
->newFromCiphertext( $serialized );
74 $this->assertEquals( $hashObj->toString(), $unserialized->toString() );
78 * @dataProvider providePasswordTests
79 * @covers InvalidPassword
81 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
82 $invalid = $this->passwordFactory
->newFromCiphertext( null );
83 $normal = $this->passwordFactory
->newFromCiphertext( $hash );
85 $this->assertFalse( $invalid->verify( $hash ) );
88 protected function getValidTypes() {
89 return array_keys( $this->getTypeConfigs() );
92 public function provideTypes( $type ) {
94 foreach ( $this->getValidTypes() as $type ) {
95 $params[] = [ $type ];
101 * @dataProvider provideTypes
103 public function testCrypt( $type ) {
104 $fromType = $this->passwordFactory
->newFromType( $type );
105 $fromType->crypt( 'password' );
106 $fromPlaintext = $this->passwordFactory
->newFromPlaintext( 'password', $fromType );
107 $this->assertTrue( $fromType->verify( 'password' ) );
108 $this->assertTrue( $fromPlaintext->verify( 'password' ) );
109 $this->assertFalse( $fromType->verify( 'different password' ) );
110 $this->assertFalse( $fromPlaintext->verify( 'different password' ) );
111 $this->assertEquals( get_class( $fromType ),
112 get_class( $fromPlaintext ),
113 'newFromPlaintext() should produce instance of the same class as newFromType()'