summaryrefslogtreecommitdiffstats
path: root/Tests/Core/Encoder/BCryptPasswordEncoderTest.php
diff options
context:
space:
mode:
authorFabien Potencier <fabien.potencier@gmail.com>2013-02-07 17:43:41 +0100
committerFabien Potencier <fabien.potencier@gmail.com>2013-02-07 17:43:41 +0100
commit4b78c06d5461d28884ca4181383bfa7b960426b6 (patch)
tree42c5d8918cf0014123fff8349b1c4da4ca1f25bb /Tests/Core/Encoder/BCryptPasswordEncoderTest.php
parent49f2eb420b3835d1032dbad4c52e1caa70930cf5 (diff)
parentb570b85dcdea40e070678c08af85d94083111fd4 (diff)
downloadsymfony-security-4b78c06d5461d28884ca4181383bfa7b960426b6.zip
symfony-security-4b78c06d5461d28884ca4181383bfa7b960426b6.tar.gz
symfony-security-4b78c06d5461d28884ca4181383bfa7b960426b6.tar.bz2
Merge branch '2.2'
* 2.2: [HttpFoundation] fixed Request::create() method [HttpKernel] fixed the creation of the Profiler directory [HttpKernel] fixed the hinclude fragment renderer when the template is empty bumped Symfony version to 2.2.0-RC2-DEV [DependencyInjection] enhanced some error messages [FrameworkBundle] fixed typo fixed typo tweaked previous merge [Security] fixed interface implementation (closes #6974) Add "'property_path' => false" deprecation message for forms fixed CS Added BCrypt password encoder. updated VERSION for 2.2.0-RC1 Removed underscores from test method names to be consistent with other components. [Security] fixed session creation when none is needed (closes #6917) [FrameworkBundle] removed obsolete comment (see 2e356c1) Micro-optimization [FrameworkBundle] removed extra whitespaces [Security] renamed Constraint namespace to Constraints for validator classes in order to be consistent with the whole current validator API. [FrameworkBundle] fixed wrong indentation on route debug output
Diffstat (limited to 'Tests/Core/Encoder/BCryptPasswordEncoderTest.php')
-rw-r--r--Tests/Core/Encoder/BCryptPasswordEncoderTest.php112
1 files changed, 112 insertions, 0 deletions
diff --git a/Tests/Core/Encoder/BCryptPasswordEncoderTest.php b/Tests/Core/Encoder/BCryptPasswordEncoderTest.php
new file mode 100644
index 0000000..bfaf5fc
--- /dev/null
+++ b/Tests/Core/Encoder/BCryptPasswordEncoderTest.php
@@ -0,0 +1,112 @@
+<?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\BCryptPasswordEncoder;
+
+/**
+ * @author Elnur Abdurrakhimov <elnur@elnur.pro>
+ */
+class BCryptPasswordEncoderTest extends \PHPUnit_Framework_TestCase
+{
+ const PASSWORD = 'password';
+ const BYTES = '0123456789abcdef';
+ const VALID_COST = '04';
+
+ const SECURE_RANDOM_INTERFACE = 'Symfony\Component\Security\Core\Util\SecureRandomInterface';
+
+ /**
+ * @var \PHPUnit_Framework_MockObject_MockObject
+ */
+ private $secureRandom;
+
+ protected function setUp()
+ {
+ $this->secureRandom = $this->getMock(self::SECURE_RANDOM_INTERFACE);
+
+ $this->secureRandom
+ ->expects($this->any())
+ ->method('nextBytes')
+ ->will($this->returnValue(self::BYTES))
+ ;
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testCostBelowRange()
+ {
+ new BCryptPasswordEncoder($this->secureRandom, 3);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testCostAboveRange()
+ {
+ new BCryptPasswordEncoder($this->secureRandom, 32);
+ }
+
+ public function testCostInRange()
+ {
+ for ($cost = 4; $cost <= 31; $cost++) {
+ new BCryptPasswordEncoder($this->secureRandom, $cost);
+ }
+ }
+
+ public function testResultLength()
+ {
+ $encoder = new BCryptPasswordEncoder($this->secureRandom, self::VALID_COST);
+ $result = $encoder->encodePassword(self::PASSWORD, null);
+ $this->assertEquals(60, strlen($result));
+ }
+
+ public function testValidation()
+ {
+ $encoder = new BCryptPasswordEncoder($this->secureRandom, self::VALID_COST);
+ $result = $encoder->encodePassword(self::PASSWORD, null);
+ $this->assertTrue($encoder->isPasswordValid($result, self::PASSWORD, null));
+ $this->assertFalse($encoder->isPasswordValid($result, 'anotherPassword', null));
+ }
+
+ public function testValidationKnownPassword()
+ {
+ $encoder = new BCryptPasswordEncoder($this->secureRandom, self::VALID_COST);
+ $prefix = '$'.(version_compare(phpversion(), '5.3.7', '>=')
+ ? '2y' : '2a').'$';
+
+ $encrypted = $prefix.'04$ABCDEFGHIJKLMNOPQRSTU.uTmwd4KMSHxbUsG7bng8x7YdA0PM1iq';
+ $this->assertTrue($encoder->isPasswordValid($encrypted, self::PASSWORD, null));
+ }
+
+ public function testSecureRandomIsUsed()
+ {
+ if (function_exists('mcrypt_create_iv')) {
+ return;
+ }
+
+ $this->secureRandom
+ ->expects($this->atLeastOnce())
+ ->method('nextBytes')
+ ;
+
+ $encoder = new BCryptPasswordEncoder($this->secureRandom, self::VALID_COST);
+ $result = $encoder->encodePassword(self::PASSWORD, null);
+
+ $prefix = '$'.(version_compare(phpversion(), '5.3.7', '>=')
+ ? '2y' : '2a').'$';
+ $salt = 'MDEyMzQ1Njc4OWFiY2RlZe';
+ $expected = crypt(self::PASSWORD, $prefix . self::VALID_COST . '$' . $salt);
+
+ $this->assertEquals($expected, $result);
+ }
+}