|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Security\Core\Tests\Encoder; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Security\Core\Encoder\MigratingPasswordEncoder; |
| 16 | +use Symfony\Component\Security\Core\Encoder\NativePasswordEncoder; |
| 17 | +use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface; |
| 18 | + |
| 19 | +class MigratingPasswordEncoderTest extends TestCase |
| 20 | +{ |
| 21 | + public function testValidation() |
| 22 | + { |
| 23 | + $bestEncoder = new NativePasswordEncoder(4, 12000, 4); |
| 24 | + |
| 25 | + $extraEncoder = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock(); |
| 26 | + $extraEncoder->expects($this->never())->method('encodePassword'); |
| 27 | + $extraEncoder->expects($this->never())->method('isPasswordValid'); |
| 28 | + $extraEncoder->expects($this->never())->method('needsRehash'); |
| 29 | + |
| 30 | + $encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder); |
| 31 | + |
| 32 | + $this->assertTrue($encoder->needsRehash('foo')); |
| 33 | + |
| 34 | + $hash = $encoder->encodePassword('foo', 'salt'); |
| 35 | + $this->assertFalse($encoder->needsRehash($hash)); |
| 36 | + |
| 37 | + $this->assertTrue($encoder->isPasswordValid($hash, 'foo', 'salt')); |
| 38 | + $this->assertFalse($encoder->isPasswordValid($hash, 'bar', 'salt')); |
| 39 | + } |
| 40 | + |
| 41 | + public function testFallback() |
| 42 | + { |
| 43 | + $bestEncoder = new NativePasswordEncoder(4, 12000, 4); |
| 44 | + |
| 45 | + $extraEncoder1 = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock(); |
| 46 | + $extraEncoder1->expects($this->any()) |
| 47 | + ->method('isPasswordValid') |
| 48 | + ->with('abc', 'foo', 'salt') |
| 49 | + ->willReturn(true); |
| 50 | + ; |
| 51 | + |
| 52 | + $encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder1); |
| 53 | + |
| 54 | + $this->assertTrue($encoder->isPasswordValid('abc', 'foo', 'salt')); |
| 55 | + |
| 56 | + $extraEncoder2 = $this->getMockBuilder(TestPasswordEncoderInterface::class)->getMock(); |
| 57 | + $extraEncoder2->expects($this->any()) |
| 58 | + ->method('isPasswordValid') |
| 59 | + ->willReturn(false); |
| 60 | + ; |
| 61 | + |
| 62 | + $encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder2); |
| 63 | + |
| 64 | + $this->assertFalse($encoder->isPasswordValid('abc', 'foo', 'salt')); |
| 65 | + |
| 66 | + $encoder = new MigratingPasswordEncoder($bestEncoder, $extraEncoder2, $extraEncoder1); |
| 67 | + |
| 68 | + $this->assertTrue($encoder->isPasswordValid('abc', 'foo', 'salt')); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +interface TestPasswordEncoderInterface extends PasswordEncoderInterface |
| 73 | +{ |
| 74 | + public function needsRehash(string $encoded): bool; |
| 75 | +} |
0 commit comments