WeakPasswordHasherTest.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * setUp method
  27. *
  28. * @return void
  29. */
  30. public function setUp() {
  31. parent::setUp();
  32. Security::salt('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. $hasher = new WeakPasswordHasher();
  42. $this->assertTrue($hasher->needsRehash(md5('foo')));
  43. $this->assertTrue($hasher->needsRehash('bar'));
  44. $this->assertFalse($hasher->needsRehash('$2y$10$juOA0XVFpvZa0KTxRxEYVuX5kIS7U1fKDRcxyYhhUQECN1oHYnBMy'));
  45. }
  46. /**
  47. * Tests hash() and check()
  48. *
  49. * @return void
  50. */
  51. public function testHashAndCheck() {
  52. $hasher = new WeakPasswordHasher();
  53. $hasher->config('hashType', 'md5');
  54. $password = $hasher->hash('foo');
  55. $this->assertTrue($hasher->check('foo', $password));
  56. $this->assertFalse($hasher->check('bar', $password));
  57. $hasher->config('hashType', 'sha1');
  58. $this->assertFalse($hasher->check('foo', $password));
  59. }
  60. }