rdbms: Rename "memCache" to "memStash" in LBFactory
[mediawiki.git] / tests / phpunit / includes / password / PasswordTestCase.php
blob80b9838d8eba111846d2f78c5e47d8e8706f46b6
1 <?php
2 /**
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
20 * @file
23 /**
24 * @since 1.24
26 abstract class PasswordTestCase extends MediaWikiTestCase {
27 /**
28 * @var PasswordFactory
30 protected $passwordFactory;
32 protected function setUp() {
33 parent::setUp();
35 $this->passwordFactory = new PasswordFactory();
36 foreach ( $this->getTypeConfigs() as $type => $config ) {
37 $this->passwordFactory->register( $type, $config );
41 /**
42 * Return an array of configs to be used for this class's password type.
44 * @return array[]
46 abstract protected function getTypeConfigs();
48 /**
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.
52 * @return array
53 * @throws MWException
54 * @abstract
56 public static function providePasswordTests() {
57 throw new MWException( "Not implemented" );
60 /**
61 * @dataProvider providePasswordTests
63 public function testHashing( $shouldMatch, $hash, $password ) {
64 $hash = $this->passwordFactory->newFromCiphertext( $hash );
65 $password = $this->passwordFactory->newFromPlaintext( $password, $hash );
66 $this->assertSame( $shouldMatch, $hash->equals( $password ) );
69 /**
70 * @dataProvider providePasswordTests
72 public function testStringSerialization( $shouldMatch, $hash, $password ) {
73 $hashObj = $this->passwordFactory->newFromCiphertext( $hash );
74 $serialized = $hashObj->toString();
75 $unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
76 $this->assertTrue( $hashObj->equals( $unserialized ) );
79 /**
80 * @dataProvider providePasswordTests
81 * @covers InvalidPassword
83 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
84 $invalid = $this->passwordFactory->newFromCiphertext( null );
85 $normal = $this->passwordFactory->newFromCiphertext( $hash );
87 $this->assertFalse( $invalid->equals( $normal ) );
88 $this->assertFalse( $normal->equals( $invalid ) );
91 protected function getValidTypes() {
92 return array_keys( $this->getTypeConfigs() );
95 public function provideTypes( $type ) {
96 $params = [];
97 foreach ( $this->getValidTypes() as $type ) {
98 $params[] = [ $type ];
100 return $params;
104 * @dataProvider provideTypes
106 public function testCrypt( $type ) {
107 $fromType = $this->passwordFactory->newFromType( $type );
108 $fromType->crypt( 'password' );
109 $fromPlaintext = $this->passwordFactory->newFromPlaintext( 'password', $fromType );
110 $this->assertTrue( $fromType->equals( $fromPlaintext ) );