HashTest.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use PHPUnit\Framework\TestCase;
  5. use App\Utils\Hash;
  6. class HashTest extends TestCase
  7. {
  8. /**
  9. * @covers App\Utils\Hash::cookieHash
  10. */
  11. public function testCookieHash()
  12. {
  13. $_ENV['key'] = 'cookie_key';
  14. $passHash = 'password';
  15. $expire_in = '1 hour';
  16. $result = Hash::cookieHash($passHash, $expire_in);
  17. $this->assertIsString($result);
  18. $this->assertEquals(45, strlen($result));
  19. }
  20. /**
  21. * @covers App\Utils\Hash::ipHash
  22. */
  23. public function testIpHash()
  24. {
  25. $_ENV['key'] = 'cookie_key';
  26. $ip = '192.168.0.1';
  27. $uid = 'user_id';
  28. $expire_in = '1 hour';
  29. $result = Hash::ipHash($ip, $uid, $expire_in);
  30. $this->assertIsString($result);
  31. $this->assertEquals(45, strlen($result));
  32. }
  33. /**
  34. * @covers App\Utils\Hash::checkPassword
  35. * @covers App\Utils\Hash::passwordHash
  36. * @covers App\Utils\Hash::md5WithSalt
  37. * @covers App\Utils\Hash::sha256WithSalt
  38. */
  39. public function testPasswordFunctions()
  40. {
  41. $_ENV['salt'] = 'password_salt';
  42. $_ENV['pwdMethod'] = 'bcrypt';
  43. $password = 'password';
  44. $hashedPassword = Hash::passwordHash($password);
  45. $this->assertTrue(Hash::checkPassword($hashedPassword, $password));
  46. $this->assertFalse(Hash::checkPassword($hashedPassword, 'wrong_password'));
  47. $this->assertIsString(Hash::passwordHash($password));
  48. $this->assertIsString(Hash::md5WithSalt($password));
  49. $this->assertIsString(Hash::sha256WithSalt($password));
  50. }
  51. }