FallbackPasswordHasherTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 check method will work with configured hashers including different
  55. * configs per hasher.
  56. *
  57. * @return void
  58. */
  59. public function testCheckWithConfigs() {
  60. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak' => ['hashType' => 'md5']]]);
  61. $legacy = new WeakPasswordHasher(['hashType' => 'md5']);
  62. $simple = new DefaultPasswordHasher();
  63. $hash = $simple->hash('foo');
  64. $legacyHash = $legacy->hash('foo');
  65. $this->assertTrue($hash !== $legacyHash);
  66. $this->assertTrue($hasher->check('foo', $hash));
  67. $this->assertTrue($hasher->check('foo', $legacyHash));
  68. }
  69. /**
  70. * Tests that the password only needs to be re-built according to the first hasher
  71. *
  72. * @return void
  73. */
  74. public function testNeedsRehash() {
  75. $hasher = new FallbackPasswordHasher(['hashers' => ['Default', 'Weak']]);
  76. $weak = new WeakPasswordHasher();
  77. $otherHash = $weak->hash('foo');
  78. $this->assertTrue($hasher->needsRehash($otherHash));
  79. $simple = new DefaultPasswordHasher();
  80. $hash = $simple->hash('foo');
  81. $this->assertFalse($hasher->needsRehash($hash));
  82. }
  83. }