WeakPasswordHasherTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Auth;
  16. use Cake\Auth\WeakPasswordHasher;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\Utility\Security;
  19. /**
  20. * Test case for WeakPasswordHasher
  21. */
  22. class WeakPasswordHasherTest extends TestCase
  23. {
  24. /**
  25. * setUp method
  26. *
  27. * @return void
  28. */
  29. public function setUp()
  30. {
  31. parent::setUp();
  32. Security::setSalt('YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  33. }
  34. /**
  35. * Tests that any password not produced by WeakPasswordHasher needs
  36. * to be rehashed
  37. *
  38. * @return void
  39. */
  40. public function testNeedsRehash()
  41. {
  42. $hasher = new WeakPasswordHasher();
  43. $this->assertTrue($hasher->needsRehash(md5('foo')));
  44. $this->assertTrue($hasher->needsRehash('bar'));
  45. $this->assertFalse($hasher->needsRehash('$2y$10$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy'));
  46. }
  47. /**
  48. * Tests hash() and check()
  49. *
  50. * @return void
  51. */
  52. public function testHashAndCheck()
  53. {
  54. $hasher = new WeakPasswordHasher();
  55. $hasher->setConfig('hashType', 'md5');
  56. $password = $hasher->hash('foo');
  57. $this->assertTrue($hasher->check('foo', $password));
  58. $this->assertFalse($hasher->check('bar', $password));
  59. $hasher->setConfig('hashType', 'sha1');
  60. $this->assertFalse($hasher->check('foo', $password));
  61. }
  62. }