/lib/password_compat/tests/PasswordHashTest.php

https://github.com/vadimonus/moodle · PHP · 101 lines · 59 code · 18 blank · 24 comment · 2 complexity · 73d261af43e7f33dc60ecd941bd38a96 MD5 · raw file

  1. <?php
  2. global $CFG;
  3. require_once($CFG->dirroot . '/lib/password_compat/lib/password.php');
  4. class PasswordHashTest extends PHPUnit_Framework_TestCase {
  5. public function testFuncExists() {
  6. $this->assertTrue(function_exists('password_hash'));
  7. }
  8. public function testStringLength() {
  9. $this->assertEquals(60, strlen(password_hash('foo', PASSWORD_BCRYPT)));
  10. }
  11. public function testHash() {
  12. $hash = password_hash('foo', PASSWORD_BCRYPT);
  13. $this->assertEquals($hash, crypt('foo', $hash));
  14. }
  15. public function testKnownSalt() {
  16. $hash = password_hash("rasmuslerdorf", PASSWORD_BCRYPT, array("cost" => 7, "salt" => "usesomesillystringforsalt"));
  17. $this->assertEquals('$2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi', $hash);
  18. }
  19. public function testRawSalt() {
  20. $hash = password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));
  21. if (version_compare(PHP_VERSION, '5.5.0', '<')) {
  22. $this->assertEquals('$2y$10$KRGxLBS0Lxe3KBCwKxOzLexLDeu0ZfqJAKTubOfy7O/yL2hjimw3u', $hash);
  23. } else {
  24. $this->assertEquals('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', $hash);
  25. }
  26. }
  27. public function testNullBehavior() {
  28. $hash = password_hash(null, PASSWORD_BCRYPT, array("salt" => "1234567890123456789012345678901234567890"));
  29. $this->assertEquals('$2y$10$123456789012345678901uhihPb9QpE2n03zMu9TDdvO34jDn6mO.', $hash);
  30. }
  31. public function testIntegerBehavior() {
  32. $hash = password_hash(12345, PASSWORD_BCRYPT, array("salt" => "1234567890123456789012345678901234567890"));
  33. $this->assertEquals('$2y$10$123456789012345678901ujczD5TiARVFtc68bZCAlbEg1fCIexfO', $hash);
  34. }
  35. /**
  36. * @expectedException PHPUnit_Framework_Error
  37. */
  38. public function testInvalidAlgo() {
  39. password_hash('foo', array());
  40. }
  41. /**
  42. * @expectedException PHPUnit_Framework_Error
  43. */
  44. public function testInvalidAlgo2() {
  45. password_hash('foo', 2);
  46. }
  47. /**
  48. * @expectedException PHPUnit_Framework_Error
  49. */
  50. public function testInvalidPassword() {
  51. password_hash(array(), 1);
  52. }
  53. /**
  54. * @expectedException PHPUnit_Framework_Error
  55. */
  56. public function testInvalidSalt() {
  57. password_hash('foo', PASSWORD_BCRYPT, array('salt' => array()));
  58. }
  59. /**
  60. * @expectedException PHPUnit_Framework_Error
  61. */
  62. public function testInvalidBcryptCostLow() {
  63. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 3));
  64. }
  65. /**
  66. * @expectedException PHPUnit_Framework_Error
  67. */
  68. public function testInvalidBcryptCostHigh() {
  69. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 32));
  70. }
  71. /**
  72. * @expectedException PHPUnit_Framework_Error
  73. */
  74. public function testInvalidBcryptCostInvalid() {
  75. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 'foo'));
  76. }
  77. /**
  78. * @expectedException PHPUnit_Framework_Error
  79. */
  80. public function testInvalidBcryptSaltShort() {
  81. password_hash('foo', PASSWORD_BCRYPT, array('salt' => 'abc'));
  82. }
  83. }