| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
use MediaWiki\Password\PasswordFactory; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
abstract class PasswordTestCase extends MediaWikiUnitTestCase { |
| 29 |
|
| 30 |
|
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
abstract protected function getTypeConfigs(); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static function providePasswordTests() { |
| 57 |
throw new LogicException( "Not implemented" ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 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 |
|
| 80 |
|
| 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 |
|
| 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 |
} |