PasswordHashTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. class PasswordHashTest extends PHPUnit_Framework_TestCase {
  3. public function testFuncExists() {
  4. $this->assertTrue(function_exists('password_hash'));
  5. }
  6. public function testStringLength() {
  7. $this->assertEquals(60, strlen(password_hash('foo', PASSWORD_BCRYPT)));
  8. }
  9. public function testHash() {
  10. $hash = password_hash('foo', PASSWORD_BCRYPT);
  11. $this->assertEquals($hash, crypt('foo', $hash));
  12. }
  13. public function testKnownSalt() {
  14. $hash = password_hash("rasmuslerdorf", PASSWORD_BCRYPT, array("cost" => 7, "salt" => "usesomesillystringforsalt"));
  15. $this->assertEquals('$2y$07$usesomesillystringfore2uDLvp1Ii2e./U9C8sBjqp8I90dH6hi', $hash);
  16. }
  17. public function testRawSalt() {
  18. $hash = password_hash("test", PASSWORD_BCRYPT, array("salt" => "123456789012345678901" . chr(0)));
  19. $this->assertEquals('$2y$10$MTIzNDU2Nzg5MDEyMzQ1Nej0NmcAWSLR.oP7XOR9HD/vjUuOj100y', $hash);
  20. }
  21. /**
  22. * @expectedException PHPUnit_Framework_Error
  23. */
  24. public function testInvalidAlgo() {
  25. password_hash('foo', array());
  26. }
  27. /**
  28. * @expectedException PHPUnit_Framework_Error
  29. */
  30. public function testInvalidAlgo2() {
  31. password_hash('foo', 2);
  32. }
  33. /**
  34. * @expectedException PHPUnit_Framework_Error
  35. */
  36. public function testInvalidPassword() {
  37. password_hash(array(), 1);
  38. }
  39. /**
  40. * @expectedException PHPUnit_Framework_Error
  41. */
  42. public function testInvalidSalt() {
  43. password_hash('foo', PASSWORD_BCRYPT, array('salt' => array()));
  44. }
  45. /**
  46. * @expectedException PHPUnit_Framework_Error
  47. */
  48. public function testInvalidBcryptCostLow() {
  49. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 3));
  50. }
  51. /**
  52. * @expectedException PHPUnit_Framework_Error
  53. */
  54. public function testInvalidBcryptCostHigh() {
  55. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 32));
  56. }
  57. /**
  58. * @expectedException PHPUnit_Framework_Error
  59. */
  60. public function testInvalidBcryptCostInvalid() {
  61. password_hash('foo', PASSWORD_BCRYPT, array('cost' => 'foo'));
  62. }
  63. /**
  64. * @expectedException PHPUnit_Framework_Error
  65. */
  66. public function testInvalidBcryptSaltShort() {
  67. password_hash('foo', PASSWORD_BCRYPT, array('salt' => 'abc'));
  68. }
  69. }