ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilBcryptPasswordEncoderTest Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoderTest:
+ Collaboration diagram for ilBcryptPasswordEncoderTest:

Public Member Functions

 getTestDirectory ()
 
 setTestDirectory ($test_directory)
 
 getTestDirectoryUrl ()
 
 setTestDirectoryUrl ($test_directory_url)
 
 costsProvider ()
 
 testInstanceCanBeCreated ()
 
 testCostsCanBeRetrievedWhenCostsAreSet (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testCostsCannotBeSetAboveRange (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated @expectedException ilPasswordException More...
 
 testCostsCannotBeSetBelowRange (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated @expectedException ilPasswordException More...
 
 testCostsCanBeSetInRange ($costs, ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated @dataProvider costsProvider More...
 
 testPasswordShouldBeCorrectlyEncodedAndVerified (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated @expectedException ilPasswordException More...
 
 testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testEncoderReliesOnSalts (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testEncoderDoesNotSupportReencoding (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testNameShouldBeBcrypt (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ()
 @expectedException ilPasswordException More...
 
 testExceptionIsRaisedIfSaltIsMissingIsOnVerification ()
 @expectedException ilPasswordException More...
 
 testInstanceCanBeCreatedAndInitializedWithClientSalt ()
 
 testClientSaltIsGeneratedWhenNoClientSaltExistsYet ()
 
 testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ()
 @expectedException ilPasswordException More...
 
 testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ()
 
 testBackwardCompatibility ()
 
 testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled ()
 @expectedException ilPasswordException More...
 
 testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ()
 

Data Fields

const VALID_COSTS = '08'
 
const PASSWORD = 'password'
 
const WRONG_PASSWORD = 'wrong_password'
 
const CLIENT_SALT = 'homer!12345_/'
 
const PASSWORD_SALT = 'salt'
 

Protected Member Functions

 setUp ()
 Setup. More...
 
- Protected Member Functions inherited from ilPasswordBaseTest
 assertException ($exception_class)
 

Protected Attributes

 $test_directory
 
 $test_directory_url
 

Private Member Functions

 skipIfPhpVersionIsNotSupported ()
 
 getInstanceWithConfiguredDataDirectory ()
 

Detailed Description

Definition at line 14 of file ilBcryptPasswordEncoderTest.php.

Member Function Documentation

◆ costsProvider()

ilBcryptPasswordEncoderTest::costsProvider ( )
Returns
array

Definition at line 108 of file ilBcryptPasswordEncoderTest.php.

109 {
110 $data = array();
111 for ($i = 4; $i <= 31; $i++) {
112 $data[] = array($i);
113 }
114 return $data;
115 }
$i
Definition: disco.tpl.php:19
$data
Definition: bench.php:6

References $data, and $i.

◆ getInstanceWithConfiguredDataDirectory()

◆ getTestDirectory()

ilBcryptPasswordEncoderTest::getTestDirectory ( )

◆ getTestDirectoryUrl()

ilBcryptPasswordEncoderTest::getTestDirectoryUrl ( )
Returns
string

Definition at line 70 of file ilBcryptPasswordEncoderTest.php.

References $test_directory_url.

Referenced by getInstanceWithConfiguredDataDirectory(), and testInstanceCanBeCreated().

+ Here is the caller graph for this function:

◆ setTestDirectory()

ilBcryptPasswordEncoderTest::setTestDirectory (   $test_directory)
Parameters
vfs\vfsStreamDirectory$test_directory

Definition at line 62 of file ilBcryptPasswordEncoderTest.php.

63 {
64 $this->test_directory = $test_directory;
65 }

References $test_directory.

Referenced by setUp().

+ Here is the caller graph for this function:

◆ setTestDirectoryUrl()

ilBcryptPasswordEncoderTest::setTestDirectoryUrl (   $test_directory_url)
Parameters
string$test_directory_url

Definition at line 78 of file ilBcryptPasswordEncoderTest.php.

79 {
80 $this->test_directory_url = $test_directory_url;
81 }

References $test_directory_url.

Referenced by setUp().

+ Here is the caller graph for this function:

◆ setUp()

ilBcryptPasswordEncoderTest::setUp ( )
protected

Setup.

Definition at line 86 of file ilBcryptPasswordEncoderTest.php.

87 {
88 vfs\vfsStream::setup();
89 $this->setTestDirectory(vfs\vfsStream::newDirectory('tests')->at(vfs\vfsStreamWrapper::getRoot()));
90 $this->setTestDirectoryUrl(vfs\vfsStream::url('root/tests'));
91
92 parent::setUp();
93 }

References setTestDirectory(), and setTestDirectoryUrl().

+ Here is the call graph for this function:

◆ skipIfPhpVersionIsNotSupported()

ilBcryptPasswordEncoderTest::skipIfPhpVersionIsNotSupported ( )
private

Definition at line 98 of file ilBcryptPasswordEncoderTest.php.

99 {
100 if (version_compare(phpversion(), '5.3.7', '<')) {
101 $this->markTestSkipped('Requires PHP >= 5.3.7');
102 }
103 }

Referenced by testBackwardCompatibility().

+ Here is the caller graph for this function:

◆ testBackwardCompatibility()

ilBcryptPasswordEncoderTest::testBackwardCompatibility ( )

Definition at line 325 of file ilBcryptPasswordEncoderTest.php.

326 {
328
329 $encoder = $this->getInstanceWithConfiguredDataDirectory();
330 $encoder->setClientSalt(self::CLIENT_SALT);
331 $encoder->setBackwardCompatibility(true);
332 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
333 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
334 $this->assertEquals('$2a$', substr($encoded_password, 0, 4));
335
336 $another_encoder = $this->getInstanceWithConfiguredDataDirectory();
337 $another_encoder->setClientSalt(self::CLIENT_SALT);
338 $another_encoder->setBackwardCompatibility(false);
339 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
340 $this->assertEquals('$2y$', substr($another_encoded_password, 0, 4));
341 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
342 }

References getInstanceWithConfiguredDataDirectory(), and skipIfPhpVersionIsNotSupported().

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 313 of file ilBcryptPasswordEncoderTest.php.

314 {
315 $encoder = $this->getInstanceWithConfiguredDataDirectory();
316 $encoder->setBackwardCompatibility(true);
317 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
318 $encoder->setBackwardCompatibility(false);
319 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
320 }

References getInstanceWithConfiguredDataDirectory().

+ Here is the call graph for this function:

◆ testClientSaltIsGeneratedWhenNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testClientSaltIsGeneratedWhenNoClientSaltExistsYet ( )

Definition at line 291 of file ilBcryptPasswordEncoderTest.php.

292 {
293 $this->getTestDirectory()->chmod(0777);
294
295 $encoder = $this->getInstanceWithConfiguredDataDirectory();
296 $this->assertNotNull($encoder->getClientSalt());
297 }

References getInstanceWithConfiguredDataDirectory(), and getTestDirectory().

+ Here is the call graph for this function:

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 161 of file ilBcryptPasswordEncoderTest.php.

162 {
163 $encoder->setCosts(4);
164 $this->assertEquals(4, $encoder->getCosts());
165 }

References ilBcryptPhpPasswordEncoder\getCosts(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCanBeSetInRange()

ilBcryptPasswordEncoderTest::testCostsCanBeSetInRange (   $costs,
ilBcryptPasswordEncoder  $encoder 
)

@depends testInstanceCanBeCreated @dataProvider costsProvider

Definition at line 191 of file ilBcryptPasswordEncoderTest.php.

192 {
193 $encoder->setCosts($costs);
194 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 171 of file ilBcryptPasswordEncoderTest.php.

172 {
173 $this->assertException(ilPasswordException::class);
174 $encoder->setCosts(32);
175 }
assertException($exception_class)

References ilPasswordBaseTest\assertException(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetBelowRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetBelowRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 181 of file ilBcryptPasswordEncoderTest.php.

182 {
183 $this->assertException(ilPasswordException::class);
184 $encoder->setCosts(3);
185 }

References ilPasswordBaseTest\assertException(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testEncoderDoesNotSupportReencoding()

ilBcryptPasswordEncoderTest::testEncoderDoesNotSupportReencoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 239 of file ilBcryptPasswordEncoderTest.php.

240 {
241 $this->assertFalse($encoder->requiresReencoding('hello'));
242 }
requiresReencoding($encoded)
{{{Returns whether or not the a encoded password needs to be re-encoded.boolean}}}

References ilBcryptPasswordEncoder\requiresReencoding().

+ Here is the call graph for this function:

◆ testEncoderReliesOnSalts()

ilBcryptPasswordEncoderTest::testEncoderReliesOnSalts ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 231 of file ilBcryptPasswordEncoderTest.php.

232 {
233 $this->assertTrue($encoder->requiresSalt());
234 }
requiresSalt()
{{Returns whether or not the encoder requires a salt.boolean}}

References ilBcryptPasswordEncoder\requiresSalt().

+ Here is the call graph for this function:

◆ testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()

ilBcryptPasswordEncoderTest::testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ( )

Definition at line 359 of file ilBcryptPasswordEncoderTest.php.

360 {
361 $encoder = $this->getInstanceWithConfiguredDataDirectory();
362 $encoder->setClientSalt(self::CLIENT_SALT);
363 $encoder->setBackwardCompatibility(true);
364 $encoder->setIsSecurityFlawIgnored(true);
365 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
366 }

References getInstanceWithConfiguredDataDirectory().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

@expectedException ilPasswordException

Definition at line 255 of file ilBcryptPasswordEncoderTest.php.

256 {
257 $this->assertException(ilPasswordException::class);
258 $encoder = $this->getInstanceWithConfiguredDataDirectory();
259 $encoder->setClientSalt(null);
260 $encoder->setCosts(self::VALID_COSTS);
261 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
262 }

References ilPasswordBaseTest\assertException(), and getInstanceWithConfiguredDataDirectory().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

@expectedException ilPasswordException

Definition at line 267 of file ilBcryptPasswordEncoderTest.php.

268 {
269 $this->assertException(ilPasswordException::class);
270 $encoder = $this->getInstanceWithConfiguredDataDirectory();
271 $encoder->setClientSalt(null);
272 $encoder->setCosts(self::VALID_COSTS);
273 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
274 }

References ilPasswordBaseTest\assertException(), and getInstanceWithConfiguredDataDirectory().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 212 of file ilBcryptPasswordEncoderTest.php.

213 {
214 $this->assertException(ilPasswordException::class);
215 $encoder->setCosts(self::VALID_COSTS);
216 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
217 }
encodePassword($raw, $salt)
{{Encodes the raw password.string The encoded password}}

References ilPasswordBaseTest\assertException(), ilBcryptPasswordEncoder\encodePassword(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled ( )

@expectedException ilPasswordException

Definition at line 347 of file ilBcryptPasswordEncoderTest.php.

348 {
349 $this->assertException(ilPasswordException::class);
350 $encoder = $this->getInstanceWithConfiguredDataDirectory();
351 $encoder->setClientSalt(self::CLIENT_SALT);
352 $encoder->setBackwardCompatibility(true);
353 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
354 }

References ilPasswordBaseTest\assertException(), and getInstanceWithConfiguredDataDirectory().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ( )

@expectedException ilPasswordException

Definition at line 302 of file ilBcryptPasswordEncoderTest.php.

303 {
304 $this->assertException(ilPasswordException::class);
305 $this->getTestDirectory()->chmod(0000);
306
307 $encoder = $this->getInstanceWithConfiguredDataDirectory();
308 }

References ilPasswordBaseTest\assertException(), getInstanceWithConfiguredDataDirectory(), and getTestDirectory().

+ Here is the call graph for this function:

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )
Returns
ilBcryptPasswordEncoder

Definition at line 132 of file ilBcryptPasswordEncoderTest.php.

133 {
134 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder(array(
135 'ignore_security_flaw' => true,
136 'data_directory' => $this->getTestDirectoryUrl()
137 ));
138 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
139
140 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder(array(
141 'ignore_security_flaw' => false,
142 'data_directory' => $this->getTestDirectoryUrl()
143 ));
144 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
145
146 $encoder = new ilBcryptPasswordEncoder(array(
147 'cost' => self::VALID_COSTS,
148 'data_directory' => $this->getTestDirectoryUrl()
149 ));
150 $this->assertInstanceOf('ilBcryptPasswordEncoder', $encoder);
151 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
152 $this->assertFalse($encoder->isSecurityFlawIgnored());
153 $encoder->setClientSalt(self::CLIENT_SALT);
154
155 return $encoder;
156 }

References getTestDirectoryUrl().

+ Here is the call graph for this function:

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 279 of file ilBcryptPasswordEncoderTest.php.

280 {
281 $this->getTestDirectory()->chmod(0777);
282 vfs\vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->getTestDirectory());
283
284 $encoder = $this->getInstanceWithConfiguredDataDirectory();
285 $this->assertEquals(self::CLIENT_SALT, $encoder->getClientSalt());
286 }

References getInstanceWithConfiguredDataDirectory(), getTestDirectory(), and ilBcryptPasswordEncoder\SALT_STORAGE_FILENAME.

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 247 of file ilBcryptPasswordEncoderTest.php.

248 {
249 $this->assertEquals('bcrypt', $encoder->getName());
250 }

References ilBcryptPasswordEncoder\getName().

+ Here is the call graph for this function:

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 199 of file ilBcryptPasswordEncoderTest.php.

200 {
201 $encoder->setCosts(self::VALID_COSTS);
202 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
203 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
204 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
205 return $encoder;
206 }
isPasswordValid($encoded, $raw, $salt)
{{Checks a raw password against an encoded password.The raw password has to be injected into the enco...

References ilBcryptPasswordEncoder\encodePassword(), ilBcryptPasswordEncoder\isPasswordValid(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength()

ilBcryptPasswordEncoderTest::testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 222 of file ilBcryptPasswordEncoderTest.php.

223 {
224 $encoder->setCosts(self::VALID_COSTS);
225 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), self::PASSWORD_SALT));
226 }

References ilBcryptPasswordEncoder\isPasswordValid(), and ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

Field Documentation

◆ $test_directory

ilBcryptPasswordEncoderTest::$test_directory
protected

Definition at line 44 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectory(), and setTestDirectory().

◆ $test_directory_url

ilBcryptPasswordEncoderTest::$test_directory_url
protected

Definition at line 49 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectoryUrl(), and setTestDirectoryUrl().

◆ CLIENT_SALT

const ilBcryptPasswordEncoderTest::CLIENT_SALT = 'homer!12345_/'

Definition at line 34 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD

const ilBcryptPasswordEncoderTest::PASSWORD = 'password'

Definition at line 24 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD_SALT

const ilBcryptPasswordEncoderTest::PASSWORD_SALT = 'salt'

Definition at line 39 of file ilBcryptPasswordEncoderTest.php.

◆ VALID_COSTS

const ilBcryptPasswordEncoderTest::VALID_COSTS = '08'

Definition at line 19 of file ilBcryptPasswordEncoderTest.php.

◆ WRONG_PASSWORD

const ilBcryptPasswordEncoderTest::WRONG_PASSWORD = 'wrong_password'

Definition at line 29 of file ilBcryptPasswordEncoderTest.php.


The documentation for this class was generated from the following file: