summaryrefslogtreecommitdiffstats
path: root/Tests/Core/Encoder
diff options
context:
space:
mode:
Diffstat (limited to 'Tests/Core/Encoder')
-rw-r--r--Tests/Core/Encoder/BasePasswordEncoderTest.php85
-rw-r--r--Tests/Core/Encoder/EncoderFactoryTest.php94
-rw-r--r--Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php45
-rw-r--r--Tests/Core/Encoder/PlaintextPasswordEncoderTest.php39
4 files changed, 263 insertions, 0 deletions
diff --git a/Tests/Core/Encoder/BasePasswordEncoderTest.php b/Tests/Core/Encoder/BasePasswordEncoderTest.php
new file mode 100644
index 0000000..2ef1dcc
--- /dev/null
+++ b/Tests/Core/Encoder/BasePasswordEncoderTest.php
@@ -0,0 +1,85 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Tests\Core\Encoder;
+
+use Symfony\Component\Security\Core\Encoder\BasePasswordEncoder;
+
+class PasswordEncoder extends BasePasswordEncoder
+{
+ public function encodePassword($raw, $salt)
+ {
+ }
+
+ public function isPasswordValid($encoded, $raw, $salt)
+ {
+ }
+}
+
+class BasePasswordEncoderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testComparePassword()
+ {
+ $this->assertTrue($this->invokeComparePasswords('password', 'password'));
+ $this->assertFalse($this->invokeComparePasswords('password', 'foo'));
+ }
+
+ public function testDemergePasswordAndSalt()
+ {
+ $this->assertEquals(array('password', 'salt'), $this->invokeDemergePasswordAndSalt('password{salt}'));
+ $this->assertEquals(array('password', ''), $this->invokeDemergePasswordAndSalt('password'));
+ $this->assertEquals(array('', ''), $this->invokeDemergePasswordAndSalt(''));
+ }
+
+ public function testMergePasswordAndSalt()
+ {
+ $this->assertEquals('password{salt}', $this->invokeMergePasswordAndSalt('password', 'salt'));
+ $this->assertEquals('password', $this->invokeMergePasswordAndSalt('password', ''));
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testMergePasswordAndSaltWithException()
+ {
+ $this->invokeMergePasswordAndSalt('password', '{foo}');
+ }
+
+ protected function invokeDemergePasswordAndSalt($password)
+ {
+ $encoder = new PasswordEncoder();
+ $r = new \ReflectionObject($encoder);
+ $m = $r->getMethod('demergePasswordAndSalt');
+ $m->setAccessible(true);
+
+ return $m->invoke($encoder, $password);
+ }
+
+ protected function invokeMergePasswordAndSalt($password, $salt)
+ {
+ $encoder = new PasswordEncoder();
+ $r = new \ReflectionObject($encoder);
+ $m = $r->getMethod('mergePasswordAndSalt');
+ $m->setAccessible(true);
+
+ return $m->invoke($encoder, $password, $salt);
+ }
+
+ protected function invokeComparePasswords($p1, $p2)
+ {
+ $encoder = new PasswordEncoder();
+ $r = new \ReflectionObject($encoder);
+ $m = $r->getMethod('comparePasswords');
+ $m->setAccessible(true);
+
+ return $m->invoke($encoder, $p1, $p2);
+ }
+}
diff --git a/Tests/Core/Encoder/EncoderFactoryTest.php b/Tests/Core/Encoder/EncoderFactoryTest.php
new file mode 100644
index 0000000..2e55a4b
--- /dev/null
+++ b/Tests/Core/Encoder/EncoderFactoryTest.php
@@ -0,0 +1,94 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Tests\Core\Encoder;
+
+use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
+use Symfony\Component\Security\Core\Encoder\EncoderFactory;
+use Symfony\Component\Security\Core\User\User;
+use Symfony\Component\Security\Core\User\UserInterface;
+
+class EncoderFactoryTest extends \PHPUnit_Framework_TestCase
+{
+ public function testGetEncoderWithMessageDigestEncoder()
+ {
+ $factory = new EncoderFactory(array('Symfony\Component\Security\Core\User\UserInterface' => array(
+ 'class' => 'Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder',
+ 'arguments' => array('sha512', true, 5),
+ )));
+
+ $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha512', true, 5);
+
+ $this->assertEquals($expectedEncoder->encodePassword('foo', 'moo'), $encoder->encodePassword('foo', 'moo'));
+ }
+
+ public function testGetEncoderWithService()
+ {
+ $factory = new EncoderFactory(array(
+ 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'),
+ ));
+
+ $encoder = $factory->getEncoder($this->getMock('Symfony\Component\Security\Core\User\UserInterface'));
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha1');
+ $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', ''));
+
+ $encoder = $factory->getEncoder(new User('user', 'pass'));
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha1');
+ $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', ''));
+ }
+
+ public function testGetEncoderWithClassName()
+ {
+ $factory = new EncoderFactory(array(
+ 'Symfony\Component\Security\Core\User\UserInterface' => new MessageDigestPasswordEncoder('sha1'),
+ ));
+
+ $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser');
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha1');
+ $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', ''));
+ }
+
+ public function testGetEncoderConfiguredForConcreteClassWithService()
+ {
+ $factory = new EncoderFactory(array(
+ 'Symfony\Component\Security\Core\User\User' => new MessageDigestPasswordEncoder('sha1'),
+ ));
+
+ $encoder = $factory->getEncoder(new User('user', 'pass'));
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha1');
+ $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', ''));
+ }
+
+ public function testGetEncoderConfiguredForConcreteClassWithClassName()
+ {
+ $factory = new EncoderFactory(array(
+ 'Symfony\Component\Security\Tests\Core\Encoder\SomeUser' => new MessageDigestPasswordEncoder('sha1'),
+ ));
+
+ $encoder = $factory->getEncoder('Symfony\Component\Security\Tests\Core\Encoder\SomeChildUser');
+ $expectedEncoder = new MessageDigestPasswordEncoder('sha1');
+ $this->assertEquals($expectedEncoder->encodePassword('foo', ''), $encoder->encodePassword('foo', ''));
+ }
+}
+
+class SomeUser implements UserInterface
+{
+ public function getRoles() {}
+ public function getPassword() {}
+ public function getSalt() {}
+ public function getUsername() {}
+ public function eraseCredentials() {}
+}
+
+class SomeChildUser extends SomeUser
+{
+}
diff --git a/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php b/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php
new file mode 100644
index 0000000..64032c4
--- /dev/null
+++ b/Tests/Core/Encoder/MessageDigestPasswordEncoderTest.php
@@ -0,0 +1,45 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Tests\Core\Encoder;
+
+use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
+
+class MessageDigestPasswordEncoderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testIsPasswordValid()
+ {
+ $encoder = new MessageDigestPasswordEncoder('sha256', false, 1);
+
+ $this->assertTrue($encoder->isPasswordValid(hash('sha256', 'password'), 'password', ''));
+ }
+
+ public function testEncodePassword()
+ {
+ $encoder = new MessageDigestPasswordEncoder('sha256', false, 1);
+ $this->assertSame(hash('sha256', 'password'), $encoder->encodePassword('password', ''));
+
+ $encoder = new MessageDigestPasswordEncoder('sha256', true, 1);
+ $this->assertSame(base64_encode(hash('sha256', 'password', true)), $encoder->encodePassword('password', ''));
+
+ $encoder = new MessageDigestPasswordEncoder('sha256', false, 2);
+ $this->assertSame(hash('sha256', hash('sha256', 'password', true).'password'), $encoder->encodePassword('password', ''));
+ }
+
+ /**
+ * @expectedException LogicException
+ */
+ public function testEncodePasswordAlgorithmDoesNotExist()
+ {
+ $encoder = new MessageDigestPasswordEncoder('foobar');
+ $encoder->encodePassword('password', '');
+ }
+}
diff --git a/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php b/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php
new file mode 100644
index 0000000..af0008f
--- /dev/null
+++ b/Tests/Core/Encoder/PlaintextPasswordEncoderTest.php
@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Security\Tests\Core\Encoder;
+
+use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
+
+class PlaintextPasswordEncoderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testIsPasswordValid()
+ {
+ $encoder = new PlaintextPasswordEncoder();
+
+ $this->assertTrue($encoder->isPasswordValid('foo', 'foo', ''));
+ $this->assertFalse($encoder->isPasswordValid('bar', 'foo', ''));
+ $this->assertFalse($encoder->isPasswordValid('FOO', 'foo', ''));
+
+ $encoder = new PlaintextPasswordEncoder(true);
+
+ $this->assertTrue($encoder->isPasswordValid('foo', 'foo', ''));
+ $this->assertFalse($encoder->isPasswordValid('bar', 'foo', ''));
+ $this->assertTrue($encoder->isPasswordValid('FOO', 'foo', ''));
+ }
+
+ public function testEncodePassword()
+ {
+ $encoder = new PlaintextPasswordEncoder();
+
+ $this->assertSame('foo', $encoder->encodePassword('foo', ''));
+ }
+}