WeakPasswordHasherTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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\WeakPasswordHasher;
  17. use Cake\Core\Configure;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\Utility\Security;
  20. /**
  21. * Test case for WeakPasswordHasher
  22. *
  23. */
  24. class WeakPasswordHasherTest extends TestCase
  25. {
  26. /**
  27. * setUp method
  28. *
  29. * @return void
  30. */
  31. public function setUp()
  32. {
  33. parent::setUp();
  34. Security::salt('YJfIxfs2guVoUubWDYhG93b0qyJfIxfs2guwvniR2G0FgaC9mi');
  35. }
  36. /**
  37. * Tests that any password not produced by WeakPasswordHasher needs
  38. * to be rehashed
  39. *
  40. * @return void
  41. */
  42. public function testNeedsRehash()
  43. {
  44. $hasher = new WeakPasswordHasher();
  45. $this->assertTrue($hasher->needsRehash(md5('foo')));
  46. $this->assertTrue($hasher->needsRehash('bar'));
  47. $this->assertFalse($hasher->needsRehash('$2y$10$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy'));
  48. }
  49. /**
  50. * Tests hash() and check()
  51. *
  52. * @return void
  53. */
  54. public function testHashAndCheck()
  55. {
  56. $hasher = new WeakPasswordHasher();
  57. $hasher->config('hashType', 'md5');
  58. $password = $hasher->hash('foo');
  59. $this->assertTrue($hasher->check('foo', $password));
  60. $this->assertFalse($hasher->check('bar', $password));
  61. $hasher->config('hashType', 'sha1');
  62. $this->assertFalse($hasher->check('foo', $password));
  63. }
  64. }