UserPasswordEncoderCommandTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\SecurityBundle\Tests\Functional;
  11. use Symfony\Bundle\FrameworkBundle\Console\Application;
  12. use Symfony\Bundle\SecurityBundle\Command\UserPasswordEncoderCommand;
  13. use Symfony\Component\Console\Tester\CommandTester;
  14. use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
  15. use Symfony\Component\Security\Core\Encoder\Pbkdf2PasswordEncoder;
  16. /**
  17. * Tests UserPasswordEncoderCommand.
  18. *
  19. * @author Sarah Khalil <mkhalil.sarah@gmail.com>
  20. */
  21. class UserPasswordEncoderCommandTest extends WebTestCase
  22. {
  23. private $passwordEncoderCommandTester;
  24. public function testEncodePasswordEmptySalt()
  25. {
  26. $this->passwordEncoderCommandTester->execute(array(
  27. 'command' => 'security:encode-password',
  28. 'password' => 'password',
  29. 'user-class' => 'Symfony\Component\Security\Core\User\User',
  30. '--empty-salt' => true,
  31. ), array('decorated' => false));
  32. $expected = str_replace("\n", PHP_EOL, file_get_contents(__DIR__.'/app/PasswordEncode/emptysalt.txt'));
  33. $this->assertEquals($expected, $this->passwordEncoderCommandTester->getDisplay());
  34. }
  35. public function testEncodeNoPasswordNoInteraction()
  36. {
  37. $statusCode = $this->passwordEncoderCommandTester->execute(array(
  38. 'command' => 'security:encode-password',
  39. ), array('interactive' => false));
  40. $this->assertContains('[ERROR] The password must not be empty.', $this->passwordEncoderCommandTester->getDisplay());
  41. $this->assertEquals($statusCode, 1);
  42. }
  43. public function testEncodePasswordBcrypt()
  44. {
  45. $this->passwordEncoderCommandTester->execute(array(
  46. 'command' => 'security:encode-password',
  47. 'password' => 'password',
  48. 'user-class' => 'Custom\Class\Bcrypt\User',
  49. ), array('interactive' => false));
  50. $output = $this->passwordEncoderCommandTester->getDisplay();
  51. $this->assertContains('Password encoding succeeded', $output);
  52. $encoder = new BCryptPasswordEncoder(17);
  53. preg_match('# Encoded password\s{1,}([\w+\/$.]+={0,2})\s+#', $output, $matches);
  54. $hash = $matches[1];
  55. $this->assertTrue($encoder->isPasswordValid($hash, 'password', null));
  56. }
  57. public function testEncodePasswordPbkdf2()
  58. {
  59. $this->passwordEncoderCommandTester->execute(array(
  60. 'command' => 'security:encode-password',
  61. 'password' => 'password',
  62. 'user-class' => 'Custom\Class\Pbkdf2\User',
  63. ), array('interactive' => false));
  64. $output = $this->passwordEncoderCommandTester->getDisplay();
  65. $this->assertContains('Password encoding succeeded', $output);
  66. $encoder = new Pbkdf2PasswordEncoder('sha512', true, 1000);
  67. preg_match('# Encoded password\s{1,}([\w+\/]+={0,2})\s+#', $output, $matches);
  68. $hash = $matches[1];
  69. preg_match('# Generated salt\s{1,}([\w+\/]+={0,2})\s+#', $output, $matches);
  70. $salt = $matches[1];
  71. $this->assertTrue($encoder->isPasswordValid($hash, 'password', $salt));
  72. }
  73. public function testEncodePasswordOutput()
  74. {
  75. $this->passwordEncoderCommandTester->execute(
  76. array(
  77. 'command' => 'security:encode-password',
  78. 'password' => 'p@ssw0rd',
  79. ), array('interactive' => false)
  80. );
  81. $this->assertContains('Password encoding succeeded', $this->passwordEncoderCommandTester->getDisplay());
  82. $this->assertContains(' Encoded password p@ssw0rd', $this->passwordEncoderCommandTester->getDisplay());
  83. $this->assertContains(' Generated salt ', $this->passwordEncoderCommandTester->getDisplay());
  84. }
  85. public function testEncodePasswordEmptySaltOutput()
  86. {
  87. $this->passwordEncoderCommandTester->execute(
  88. array(
  89. 'command' => 'security:encode-password',
  90. 'password' => 'p@ssw0rd',
  91. '--empty-salt' => true,
  92. )
  93. );
  94. $this->assertContains('Password encoding succeeded', $this->passwordEncoderCommandTester->getDisplay());
  95. $this->assertContains(' Encoded password p@ssw0rd', $this->passwordEncoderCommandTester->getDisplay());
  96. $this->assertNotContains(' Generated salt ', $this->passwordEncoderCommandTester->getDisplay());
  97. }
  98. public function testEncodePasswordBcryptOutput()
  99. {
  100. $this->passwordEncoderCommandTester->execute(
  101. array(
  102. 'command' => 'security:encode-password',
  103. 'password' => 'p@ssw0rd',
  104. 'user-class' => 'Custom\Class\Bcrypt\User',
  105. )
  106. );
  107. $this->assertNotContains(' Generated salt ', $this->passwordEncoderCommandTester->getDisplay());
  108. }
  109. public function testEncodePasswordNoConfigForGivenUserClass()
  110. {
  111. if (method_exists($this, 'expectException')) {
  112. $this->expectException('\RuntimeException');
  113. $this->expectExceptionMessage('No encoder has been configured for account "Foo\Bar\User".');
  114. } else {
  115. $this->setExpectedException('\RuntimeException', 'No encoder has been configured for account "Foo\Bar\User".');
  116. }
  117. $this->passwordEncoderCommandTester->execute(array(
  118. 'command' => 'security:encode-password',
  119. 'password' => 'password',
  120. 'user-class' => 'Foo\Bar\User',
  121. ), array('interactive' => false));
  122. }
  123. protected function setUp()
  124. {
  125. $kernel = $this->createKernel(array('test_case' => 'PasswordEncode'));
  126. $kernel->boot();
  127. $application = new Application($kernel);
  128. $application->add(new UserPasswordEncoderCommand());
  129. $passwordEncoderCommand = $application->find('security:encode-password');
  130. $this->passwordEncoderCommandTester = new CommandTester($passwordEncoderCommand);
  131. }
  132. protected function tearDown()
  133. {
  134. $this->passwordEncoderCommandTester = null;
  135. }
  136. }