PageRenderTime 27ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/public/typo3/sysext/saltedpasswords/Tests/Unit/Salt/SaltFactoryTest.php

https://bitbucket.org/followupcio/website
PHP | 143 lines | 75 code | 13 blank | 55 comment | 15 complexity | 7a6754e7e0f75717fea92f0a460473b0 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. <?php
  2. namespace TYPO3\CMS\Saltedpasswords\Tests\Unit\Salt;
  3. /*
  4. * This file is part of the TYPO3 CMS project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License, either version 2
  8. * of the License, or any later version.
  9. *
  10. * For the full copyright and license information, please read the
  11. * LICENSE.txt file that was distributed with this source code.
  12. *
  13. * The TYPO3 project - inspiring people to share!
  14. */
  15. use TYPO3\CMS\Core\Crypto\Random;
  16. /**
  17. * Testcase for SaltFactory
  18. */
  19. class SaltFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
  20. {
  21. /**
  22. * Keeps instance of object to test.
  23. *
  24. * @var \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt
  25. */
  26. protected $objectInstance = null;
  27. /**
  28. * Sets up the fixtures for this testcase.
  29. */
  30. protected function setUp()
  31. {
  32. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance();
  33. }
  34. /**
  35. * @test
  36. */
  37. public function objectInstanceNotNull()
  38. {
  39. $this->assertNotNull($this->objectInstance);
  40. }
  41. /**
  42. * @test
  43. */
  44. public function objectInstanceExtendsAbstractClass()
  45. {
  46. $this->assertTrue(is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt::class));
  47. }
  48. /**
  49. * @test
  50. */
  51. public function objectInstanceImplementsInterface()
  52. {
  53. $this->assertTrue(method_exists($this->objectInstance, 'checkPassword'), 'Missing method checkPassword() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  54. $this->assertTrue(method_exists($this->objectInstance, 'isHashUpdateNeeded'), 'Missing method isHashUpdateNeeded() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  55. $this->assertTrue(method_exists($this->objectInstance, 'isValidSalt'), 'Missing method isValidSalt() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  56. $this->assertTrue(method_exists($this->objectInstance, 'isValidSaltedPW'), 'Missing method isValidSaltedPW() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  57. $this->assertTrue(method_exists($this->objectInstance, 'getHashedPassword'), 'Missing method getHashedPassword() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  58. $this->assertTrue(method_exists($this->objectInstance, 'getSaltLength'), 'Missing method getSaltLength() from interface ' . \TYPO3\CMS\Saltedpasswords\Salt\SaltInterface::class . '.');
  59. }
  60. /**
  61. * @test
  62. */
  63. public function base64EncodeReturnsProperLength()
  64. {
  65. // 3 Bytes should result in a 6 char length base64 encoded string
  66. // used for MD5 and PHPass salted hashing
  67. $byteLength = 3;
  68. $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
  69. $randomBytes = (new Random())->generateRandomBytes($byteLength);
  70. $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
  71. // 16 Bytes should result in a 22 char length base64 encoded string
  72. // used for Blowfish salted hashing
  73. $byteLength = 16;
  74. $reqLengthBase64 = (int)ceil($byteLength * 8 / 6);
  75. $randomBytes = (new Random())->generateRandomBytes($byteLength);
  76. $this->assertTrue(strlen($this->objectInstance->base64Encode($randomBytes, $byteLength)) == $reqLengthBase64);
  77. }
  78. /**
  79. * @test
  80. */
  81. public function objectInstanceForMD5Salts()
  82. {
  83. $saltMD5 = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
  84. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltMD5);
  85. $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class));
  86. }
  87. /**
  88. * @test
  89. */
  90. public function objectInstanceForBlowfishSalts()
  91. {
  92. $saltBlowfish = '$2a$07$abcdefghijklmnopqrstuuIdQV69PAxWYTgmnoGpe0Sk47GNS/9ZW';
  93. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltBlowfish);
  94. $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class));
  95. }
  96. /**
  97. * @test
  98. */
  99. public function objectInstanceForPhpassSalts()
  100. {
  101. $saltPhpass = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
  102. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPhpass);
  103. $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class));
  104. }
  105. /**
  106. * @test
  107. */
  108. public function objectInstanceForPbkdf2Salts()
  109. {
  110. $saltPbkdf2 = '$pbkdf2-sha256$6400$0ZrzXitFSGltTQnBWOsdAw$Y11AchqV4b0sUisdZd0Xr97KWoymNE0LNNrnEgY4H9M';
  111. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltPbkdf2);
  112. $this->assertTrue(get_class($this->objectInstance) == \TYPO3\CMS\Saltedpasswords\Salt\Pbkdf2Salt::class || is_subclass_of($this->objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\Pbkdf2Salt::class));
  113. }
  114. /**
  115. * @test
  116. */
  117. public function resettingFactoryInstanceSucceeds()
  118. {
  119. $defaultClassNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod();
  120. if ($defaultClassNameToUse == \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class) {
  121. $saltedPW = '$P$CWF13LlG/0UcAQFUjnnS4LOqyRW43c.';
  122. } else {
  123. $saltedPW = '$1$rasmusle$rISCgZzpwk3UhDidwXvin0';
  124. }
  125. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance($saltedPW);
  126. // resetting
  127. $this->objectInstance = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null);
  128. $this->assertTrue(get_class($this->objectInstance) == $defaultClassNameToUse || is_subclass_of($this->objectInstance, $defaultClassNameToUse));
  129. }
  130. }