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
|
<?php
/**
* Testing framework for the password hashes
*
* 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
*/
use MediaWiki\Password\PasswordFactory;
/**
* @since 1.24
*/
abstract class PasswordTestCase extends MediaWikiUnitTestCase {
/**
* @var PasswordFactory
*/
protected $passwordFactory;
protected function setUp(): void {
parent::setUp();
$this->passwordFactory = new PasswordFactory();
foreach ( $this->getTypeConfigs() as $type => $config ) {
$this->passwordFactory->register( $type, $config );
}
}
/**
* Return an array of configs to be used for this class's password type.
*
* @return array[]
*/
abstract protected function getTypeConfigs();
/**
* An array of tests in the form of (bool, string, string), where the first
* element is whether the second parameter (a password hash) and the third
* parameter (a password) should match.
* @return array
*/
public static function providePasswordTests() {
throw new LogicException( "Not implemented" );
}
/**
* @dataProvider providePasswordTests
*/
public function testHashing( $shouldMatch, $hash, $password ) {
$passwordObj = $this->passwordFactory->newFromCiphertext( $hash );
$this->assertSame( $shouldMatch, $passwordObj->verify( $password ) );
}
/**
* @dataProvider providePasswordTests
*/
public function testStringSerialization( $shouldMatch, $hash, $password ) {
$hashObj = $this->passwordFactory->newFromCiphertext( $hash );
$serialized = $hashObj->toString();
$unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
$this->assertEquals( $hashObj->toString(), $unserialized->toString() );
}
/**
* @dataProvider providePasswordTests
* @covers \MediaWiki\Password\InvalidPassword
*/
public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
$invalid = $this->passwordFactory->newFromCiphertext( null );
$normal = $this->passwordFactory->newFromCiphertext( $hash );
$this->assertFalse( $invalid->verify( $hash ) );
}
protected function getValidTypes() {
return array_keys( $this->getTypeConfigs() );
}
public function provideTypes() {
$params = [];
foreach ( $this->getValidTypes() as $type ) {
$params[] = [ $type ];
}
return $params;
}
/**
* @dataProvider provideTypes
*/
public function testCrypt( $type ) {
$fromType = $this->passwordFactory->newFromType( $type );
$fromType->crypt( 'password' );
$fromPlaintext = $this->passwordFactory->newFromPlaintext( 'password', $fromType );
$this->assertTrue( $fromType->verify( 'password' ) );
$this->assertTrue( $fromPlaintext->verify( 'password' ) );
$this->assertFalse( $fromType->verify( 'different password' ) );
$this->assertFalse( $fromPlaintext->verify( 'different password' ) );
$this->assertEquals( get_class( $fromType ),
get_class( $fromPlaintext ),
'newFromPlaintext() should produce instance of the same class as newFromType()'
);
}
}
|