FallbackPasswordHasherTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Auth;
  16. use Cake\Auth\DefaultPasswordHasher;
  17. use Cake\Auth\FallbackPasswordHasher;
  18. use Cake\Auth\WeakPasswordHasher;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Test case for FallbackPasswordHasher
  22. *
  23. */
  24. class FallbackPasswordHasherTest extends TestCase {
  25. /**
  26. * Tests that only the first hasher is user for hashing a password
  27. *
  28. * @return void
  29. */
  30. public function testHash() {
  31. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  32. $weak = new WeakPasswordHasher();
  33. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  34. $simple = new DefaultPasswordHasher();
  35. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  36. $this->assertSame($weak->hash('foo'), $hasher->hash('foo'));
  37. }
  38. /**
  39. * Tests that the check method will check with configured hashers until a match
  40. * is found
  41. *
  42. * @return void
  43. */
  44. public function testCheck() {
  45. $hasher = new FallbackPasswordHasher(['hashers' => ['Weak', 'Default']]);
  46. $weak = new WeakPasswordHasher();
  47. $simple = new DefaultPasswordHasher();
  48. $hash = $simple->hash('foo');
  49. $otherHash = $weak->hash('foo');
  50. $this->assertTrue($hasher->check('foo', $hash));
  51. $this->assertTrue($hasher->check('foo', $otherHash));
  52. }
  53. /**
  54. * Tests that the password only needs to be re-built according to the first hasher
  55. *
  56. * @return void
  57. */
  58. public function testNeedsRehash() {
  59. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak']]);
  60. $weak = new WeakPasswordHasher();
  61. $otherHash = $weak->hash('foo');
  62. $this->assertTrue($hasher->needsRehash($otherHash));
  63. $simple = new DefaultPasswordHasher();
  64. $hash = $simple->hash('foo');
  65. $this->assertFalse($hasher->needsRehash($hash));
  66. }
  67. }