| 1 | <?php |
| 2 | |
| 3 | use MediaWiki\Password\PasswordError; |
| 4 | use MediaWiki\Password\PasswordFactory; |
| 5 | |
| 6 | abstract class Pbkdf2PasswordTestCase extends PasswordTestCase { |
| 7 | abstract protected static function getPbkdf2PasswordClass(); |
| 8 | |
| 9 | protected function getTypeConfigs() { |
| 10 | return [ 'pbkdf2' => [ |
| 11 | 'class' => static::getPbkdf2PasswordClass(), |
| 12 | 'algo' => 'sha256', |
| 13 | 'cost' => '1000', |
| 14 | 'length' => '128', |
| 15 | ] ]; |
| 16 | } |
| 17 | |
| 18 | public static function providePasswordTests() { |
| 19 | return [ |
| 20 | [ true, ":pbkdf2:sha512:1:20:c2FsdA==:hn9wzxreAs/zdSWZo6U9xK80x6Y=", 'password' ], |
| 21 | [ true, ":pbkdf2:sha512:2:20:c2FsdA==:4dnBaqaBcIpF9cfE4hXOtm4BGi4=", 'password' ], |
| 22 | [ true, ":pbkdf2:sha512:4096:20:c2FsdA==:0Zexsz2wFD4BixLz0dFHnmzevcw=", 'password' ], |
| 23 | [ true, ":pbkdf2:sha512:4096:16:c2EAbHQ=:nZ6cTNIf5L4k1bgkTHWWZQ==", "pass\x00word" ], |
| 24 | ]; |
| 25 | } |
| 26 | |
| 27 | public function testCryptThrowsOnInvalidAlgo() { |
| 28 | $factory = new PasswordFactory(); |
| 29 | $class = static::getPbkdf2PasswordClass(); |
| 30 | $password = new $class( |
| 31 | $factory, |
| 32 | [ |
| 33 | 'type' => 'pbkdf2', |
| 34 | 'algo' => 'fail', |
| 35 | 'cost' => '10000', |
| 36 | 'length' => '128', |
| 37 | ] |
| 38 | ); |
| 39 | $this->expectException( PasswordError::class ); |
| 40 | $this->expectExceptionMessage( 'Unknown or unsupported algo: fail' ); |
| 41 | $password->crypt( 'whatever' ); |
| 42 | } |
| 43 | |
| 44 | public function testCryptThrowsOnInvalidCost() { |
| 45 | $factory = new PasswordFactory(); |
| 46 | $class = static::getPbkdf2PasswordClass(); |
| 47 | $password = new $class( |
| 48 | $factory, |
| 49 | [ |
| 50 | 'type' => 'pbkdf2', |
| 51 | 'algo' => 'sha256', |
| 52 | 'cost' => '0', |
| 53 | 'length' => '128', |
| 54 | ] |
| 55 | ); |
| 56 | $this->expectException( PasswordError::class ); |
| 57 | $this->expectExceptionMessage( 'Invalid number of rounds.' ); |
| 58 | $password->crypt( 'whatever' ); |
| 59 | } |
| 60 | |
| 61 | public function testCryptThrowsOnInvalidLength() { |
| 62 | $factory = new PasswordFactory(); |
| 63 | $class = static::getPbkdf2PasswordClass(); |
| 64 | $password = new $class( |
| 65 | $factory, |
| 66 | [ |
| 67 | 'type' => 'pbkdf2', |
| 68 | 'algo' => 'sha256', |
| 69 | 'cost' => '10000', |
| 70 | 'length' => '0', |
| 71 | ] |
| 72 | ); |
| 73 | $this->expectException( PasswordError::class ); |
| 74 | $this->expectExceptionMessage( 'Invalid length.' ); |
| 75 | $password->crypt( 'whatever' ); |
| 76 | } |
| 77 | } |