1<?
php declare(strict_types=1);
4require_once
'Services/Password/classes/encoders/class.ilBcryptPasswordEncoder.php';
5require_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);
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));
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());
280 $this->skipIfvfsStreamNotSupported();
282 $this->expectException(ilPasswordException::class);
283 $encoder = $this->getInstanceWithConfiguredDataDirectory();
285 $encoder->
setCosts(self::VALID_COSTS);
294 $this->skipIfvfsStreamNotSupported();
296 $this->expectException(ilPasswordException::class);
297 $encoder = $this->getInstanceWithConfiguredDataDirectory();
299 $encoder->
setCosts(self::VALID_COSTS);
300 $encoder->
isPasswordValid(
'12121212', self::PASSWORD, self::PASSWORD_SALT);
308 $this->skipIfvfsStreamNotSupported();
310 $this->getTestDirectory()->chmod(0777);
313 $encoder = $this->getInstanceWithConfiguredDataDirectory();
314 $this->assertEquals(self::CLIENT_SALT, $encoder->
getClientSalt());
322 $this->skipIfvfsStreamNotSupported();
324 $this->getTestDirectory()->chmod(0777);
326 $encoder = $this->getInstanceWithConfiguredDataDirectory();
335 $this->skipIfvfsStreamNotSupported();
337 $this->expectException(ilPasswordException::class);
338 $this->getTestDirectory()->chmod(0000);
340 $this->getInstanceWithConfiguredDataDirectory();
348 $this->skipIfvfsStreamNotSupported();
350 $encoder = $this->getInstanceWithConfiguredDataDirectory();
362 $this->skipIfPhpVersionIsNotSupported();
363 $this->skipIfvfsStreamNotSupported();
365 $encoder = $this->getInstanceWithConfiguredDataDirectory();
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));
372 $another_encoder = $this->getInstanceWithConfiguredDataDirectory();
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));
385 $this->skipIfvfsStreamNotSupported();
387 $this->expectException(ilPasswordException::class);
388 $encoder = $this->getInstanceWithConfiguredDataDirectory();
391 $encoder->
encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
400 $this->skipIfvfsStreamNotSupported();
402 $encoder = $this->getInstanceWithConfiguredDataDirectory();
406 $encoder->
encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
An exception for terminatinating execution or to throw for unit testing.
testClientSaltIsGeneratedWhenNoClientSaltExistsYet()
testCostsCanBeSetInRange(string $costs, ilBcryptPasswordEncoder $encoder)
@doesNotPerformAssertions @depends testInstanceCanBeCreated @dataProvider costsProvider
testInstanceCanBeCreatedAndInitializedWithClientSalt()
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
setTestDirectory(vfs\vfsStreamDirectory $testDirectory)
getInstanceWithConfiguredDataDirectory()
skipIfPhpVersionIsNotSupported()
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()
testCostsCannotBeSetBelowRange(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled()
testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()
@doesNotPerformAssertions
setTestDirectoryUrl(string $testDirectoryUrl)
testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()
testInstanceCanBeCreated()
testExceptionIsRaisedIfSaltIsMissingIsOnVerification()
testEncoderReliesOnSalts(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testBackwardCompatibility()
testEncoderDoesNotSupportReencoding(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCannotBeSetAboveRange(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
skipIfvfsStreamNotSupported()
testNameShouldBeBcrypt(ilBcryptPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()
encodePassword(string $raw, string $salt)
@inheritDoc
setBackwardCompatibility(bool $backward_compatibility)
Set the backward compatibility $2a$ instead of $2y$ for PHP 5.3.7+.
const SALT_STORAGE_FILENAME
isBackwardCompatibilityEnabled()
requiresSalt()
@inheritDoc
isPasswordValid(string $encoded, string $raw, string $salt)
@inheritDoc
setClientSalt(?string $client_salt)
requiresReencoding(string $encoded)
@inheritDoc
setIsSecurityFlawIgnored(bool $is_security_flaw_ignored)
Class for user password exception handling in ILIAS.