1 <?php declare(strict_types=1);
4 require_once
'Services/Password/classes/encoders/class.ilBcryptPasswordEncoder.php';
5 require_once
'Services/Password/test/ilPasswordBaseTest.php';
74 if (version_compare(phpversion(),
'5.3.7',
'<')) {
75 $this->markTestSkipped(
'Requires PHP >= 5.3.7');
84 return class_exists(
'org\bovigo\vfs\vfsStreamWrapper');
93 $this->markTestSkipped(
'Skipped test, vfsStream (http://vfs.bovigo.org) required');
95 vfs\vfsStream::setup();
96 $this->
setTestDirectory(vfs\vfsStream::newDirectory(
'tests')->at(vfs\vfsStreamWrapper::getRoot()));
107 for (
$i = 4;
$i <= 31;
$i++) {
108 $data[sprintf(
"Costs: %s", (
string)
$i)] = [(string) $i];
136 'ignore_security_flaw' =>
true,
139 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
142 'ignore_security_flaw' =>
false,
145 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
148 'cost' => self::VALID_COSTS,
151 $this->assertInstanceOf(
'ilBcryptPasswordEncoder', $encoder);
152 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
153 $this->assertFalse($encoder->isSecurityFlawIgnored());
154 $encoder->setClientSalt(self::CLIENT_SALT);
169 $this->assertEquals($expected, $encoder->
getCosts());
179 $this->expectException(ilPasswordException::class);
190 $this->expectException(ilPasswordException::class);
216 $encoder->
setCosts(self::VALID_COSTS);
217 $encoded_password = $encoder->
encodePassword(self::PASSWORD, self::PASSWORD_SALT);
218 $this->assertTrue($encoder->
isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
219 $this->assertFalse($encoder->
isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
231 $this->expectException(ilPasswordException::class);
232 $encoder->
setCosts(self::VALID_COSTS);
233 $encoder->
encodePassword(str_repeat(
'a', 5000), self::PASSWORD_SALT);
244 $encoder->
setCosts(self::VALID_COSTS);
245 $this->assertFalse($encoder->
isPasswordValid(
'encoded', str_repeat(
'a', 5000), self::PASSWORD_SALT));
272 $this->assertEquals(
'bcrypt', $encoder->
getName());
282 $this->expectException(ilPasswordException::class);
284 $encoder->setClientSalt(null);
285 $encoder->setCosts(self::VALID_COSTS);
286 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
296 $this->expectException(ilPasswordException::class);
298 $encoder->setClientSalt(null);
299 $encoder->setCosts(self::VALID_COSTS);
300 $encoder->isPasswordValid(
'12121212', self::PASSWORD, self::PASSWORD_SALT);
314 $this->assertEquals(self::CLIENT_SALT, $encoder->getClientSalt());
327 $this->assertNotNull($encoder->getClientSalt());
337 $this->expectException(ilPasswordException::class);
351 $encoder->setBackwardCompatibility(
true);
352 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
353 $encoder->setBackwardCompatibility(
false);
354 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
366 $encoder->setClientSalt(self::CLIENT_SALT);
367 $encoder->setBackwardCompatibility(
true);
368 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
369 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
370 $this->assertEquals(
'$2a$', substr($encoded_password, 0, 4));
373 $another_encoder->setClientSalt(self::CLIENT_SALT);
374 $another_encoder->setBackwardCompatibility(
false);
375 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
376 $this->assertEquals(
'$2y$', substr($another_encoded_password, 0, 4));
377 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
387 $this->expectException(ilPasswordException::class);
389 $encoder->setClientSalt(self::CLIENT_SALT);
390 $encoder->setBackwardCompatibility(
true);
391 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
403 $encoder->setClientSalt(self::CLIENT_SALT);
404 $encoder->setBackwardCompatibility(
true);
405 $encoder->setIsSecurityFlawIgnored(
true);
406 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
const SALT_STORAGE_FILENAME
testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled()
testCostsCannotBeSetAboveRange(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
skipIfvfsStreamNotSupported()
getInstanceWithConfiguredDataDirectory()
testClientSaltIsGeneratedWhenNoClientSaltExistsYet()
encodePassword(string $raw, string $salt)
testEncoderDoesNotSupportReencoding(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
setTestDirectory(vfs\vfsStreamDirectory $testDirectory)
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
testInstanceCanBeCreatedAndInitializedWithClientSalt()
testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
requiresReencoding(string $encoded)
testBackwardCompatibility()
testExceptionIsRaisedIfSaltIsMissingIsOnVerification()
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()
testCostsCanBeSetInRange(string $costs, ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated costsProvider
testInstanceCanBeCreated()
skipIfPhpVersionIsNotSupported()
testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()
testCostsCannotBeSetBelowRange(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
testNameShouldBeBcrypt(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
testEncoderReliesOnSalts(ilBcryptPasswordEncoder $encoder)
testInstanceCanBeCreated
setTestDirectoryUrl(string $testDirectoryUrl)
isPasswordValid(string $encoded, string $raw, string $salt)
testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()