SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/tests/admin_authTest.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. use Sprout\Helpers\AdminAuth;
  15. use Sprout\Helpers\Constants;
  16.  
  17.  
  18. class admin_authTest extends PHPUnit_Framework_TestCase
  19. {
  20.  
  21. public function dataAlgorithms()
  22. {
  23. return array(
  24. [Constants::PASSWORD_SHA_SALT],
  25. [Constants::PASSWORD_SHA_SALT_5000],
  26. [Constants::PASSWORD_BCRYPT12],
  27. );
  28. }
  29.  
  30.  
  31. /**
  32.   * Does hash creation match hash checking?
  33.   * @dataProvider dataAlgorithms
  34.   **/
  35. public function testHashMatchCheck($alg)
  36. {
  37. if (! AdminAuth::checkAlgorithm($alg)) return;
  38.  
  39. list ($a, $b, $c) = AdminAuth::hashPassword('Match', $alg);
  40. $result = AdminAuth::doPasswordCheck($a, $b, $c, 'Match');
  41. $this->assertTrue($result);
  42. $this->assertTrue($alg == $b);
  43.  
  44. list ($a, $b, $c) = AdminAuth::hashPassword('Match', $alg);
  45. $result = AdminAuth::doPasswordCheck($a, $b, $c, 'Do not match');
  46. $this->assertFalse($result);
  47. $this->assertTrue($alg == $b);
  48. }
  49.  
  50.  
  51. /**
  52.   * Does two creations create different hashes? (hashes with salts)
  53.   * @dataProvider dataAlgorithms
  54.   **/
  55. public function testHashWithSalts($alg)
  56. {
  57. if (! AdminAuth::checkAlgorithm($alg)) return;
  58. list ($a1, $b1, $c1) = AdminAuth::hashPassword('Match', $alg);
  59. list ($a2, $b2, $c2) = AdminAuth::hashPassword('Match', $alg);
  60. $this->assertTrue($b1 == $b2);
  61. $this->assertTrue($alg == $b1);
  62. $this->assertTrue($a1 != $a2);
  63. $this->assertTrue($c1 != $c2);
  64. }
  65.  
  66.  
  67. public function testCheckAlgorithm()
  68. {
  69. $this->assertTrue(AdminAuth::checkAlgorithm(Constants::PASSWORD_SHA_SALT));
  70. $this->assertTrue(AdminAuth::checkAlgorithm(Constants::PASSWORD_SHA_SALT_5000));
  71. $this->assertFalse(AdminAuth::checkAlgorithm(1234));
  72. }
  73.  
  74. }
  75.