Debian Codegraph @

tests/phpunit/unit/includes/password/PasswordTestCase.php

raw symbols SCIP
1 <?php
2 /**
3 * Testing framework for the password hashes
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\Password\PasswordFactory;
24
25 /**
26 * @since 1.24
27 */
28 abstract class PasswordTestCase extends MediaWikiUnitTestCase {
29 /**
30 * @var PasswordFactory
31 */
32 protected $passwordFactory;
33
34 protected function setUp(): void {
35 parent::setUp();
36
37 $this->passwordFactory = new PasswordFactory();
38 foreach ( $this->getTypeConfigs() as $type => $config ) {
39 $this->passwordFactory->register( $type, $config );
40 }
41 }
42
43 /**
44 * Return an array of configs to be used for this class's password type.
45 *
46 * @return array[]
47 */
48 abstract protected function getTypeConfigs();
49
50 /**
51 * An array of tests in the form of (bool, string, string), where the first
52 * element is whether the second parameter (a password hash) and the third
53 * parameter (a password) should match.
54 * @return array
55 */
56 public static function providePasswordTests() {
57 throw new LogicException( "Not implemented" );
58 }
59
60 /**
61 * @dataProvider providePasswordTests
62 */
63 public function testHashing( $shouldMatch, $hash, $password ) {
64 $passwordObj = $this->passwordFactory->newFromCiphertext( $hash );
65 $this->assertSame( $shouldMatch, $passwordObj->verify( $password ) );
66 }
67
68 /**
69 * @dataProvider providePasswordTests
70 */
71 public function testStringSerialization( $shouldMatch, $hash, $password ) {
72 $hashObj = $this->passwordFactory->newFromCiphertext( $hash );
73 $serialized = $hashObj->toString();
74 $unserialized = $this->passwordFactory->newFromCiphertext( $serialized );
75 $this->assertEquals( $hashObj->toString(), $unserialized->toString() );
76 }
77
78 /**
79 * @dataProvider providePasswordTests
80 * @covers \MediaWiki\Password\InvalidPassword
81 */
82 public function testInvalidUnequalNormal( $shouldMatch, $hash, $password ) {
83 $invalid = $this->passwordFactory->newFromCiphertext( null );
84 $normal = $this->passwordFactory->newFromCiphertext( $hash );
85
86 $this->assertFalse( $invalid->verify( $hash ) );
87 }
88
89 protected function getValidTypes() {
90 return array_keys( $this->getTypeConfigs() );
91 }
92
93 public function provideTypes() {
94 $params = [];
95 foreach ( $this->getValidTypes() as $type ) {
96 $params[] = [ $type ];
97 }
98 return $params;
99 }
100
101 /**
102 * @dataProvider provideTypes
103 */
104 public function testCrypt( $type ) {
105 $fromType = $this->passwordFactory->newFromType( $type );
106 $fromType->crypt( 'password' );
107 $fromPlaintext = $this->passwordFactory->newFromPlaintext( 'password', $fromType );
108 $this->assertTrue( $fromType->verify( 'password' ) );
109 $this->assertTrue( $fromPlaintext->verify( 'password' ) );
110 $this->assertFalse( $fromType->verify( 'different password' ) );
111 $this->assertFalse( $fromPlaintext->verify( 'different password' ) );
112 $this->assertEquals( get_class( $fromType ),
113 get_class( $fromPlaintext ),
114 'newFromPlaintext() should produce instance of the same class as newFromType()'
115 );
116 }
117 }