ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ilBcryptPasswordEncoderTest Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoderTest:
+ Collaboration diagram for ilBcryptPasswordEncoderTest:

Public Member Functions

 getTestDirectory ()
 
 setTestDirectory ($test_directory)
 
 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
 

Private Member Functions

 skipIfPhpVersionIsNotSupported ()
 

Detailed Description

Definition at line 14 of file ilBcryptPasswordEncoderTest.php.

Member Function Documentation

◆ costsProvider()

ilBcryptPasswordEncoderTest::costsProvider ( )
Returns
array

Definition at line 92 of file ilBcryptPasswordEncoderTest.php.

93 {
94 $data = array();
95 for($i = 4; $i <= 31; $i++)
96 {
97 $data[] = array($i);
98 }
99 return $data;
100 }

References $data.

◆ getTestDirectory()

ilBcryptPasswordEncoderTest::getTestDirectory ( )

◆ setTestDirectory()

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

Definition at line 57 of file ilBcryptPasswordEncoderTest.php.

58 {
59 $this->test_directory = $test_directory;
60 }

References $test_directory.

Referenced by setUp().

+ Here is the caller graph for this function:

◆ setUp()

ilBcryptPasswordEncoderTest::setUp ( )
protected

Setup.

Definition at line 65 of file ilBcryptPasswordEncoderTest.php.

66 {
67 vfs\vfsStream::setup();
68 $this->setTestDirectory(vfs\vfsStream::newDirectory('tests')->at(vfs\vfsStreamWrapper::getRoot()));
69
70 if(!defined('CLIENT_DATA_DIR'))
71 {
72 define('CLIENT_DATA_DIR', vfs\vfsStream::url('root/tests'));
73 }
74
75 parent::setUp();
76 }
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27

References defined, and setTestDirectory().

+ Here is the call graph for this function:

◆ skipIfPhpVersionIsNotSupported()

ilBcryptPasswordEncoderTest::skipIfPhpVersionIsNotSupported ( )
private

Definition at line 81 of file ilBcryptPasswordEncoderTest.php.

82 {
83 if(version_compare(phpversion(), '5.3.7', '<'))
84 {
85 $this->markTestSkipped('Requires PHP >= 5.3.7');
86 }
87 }

Referenced by testBackwardCompatibility().

+ Here is the caller graph for this function:

◆ testBackwardCompatibility()

ilBcryptPasswordEncoderTest::testBackwardCompatibility ( )

Definition at line 294 of file ilBcryptPasswordEncoderTest.php.

295 {
297
298 $encoder = new ilBcryptPasswordEncoder();
299 $encoder->setClientSalt(self::CLIENT_SALT);
300 $encoder->setBackwardCompatibility(true);
301 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
302 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
303 $this->assertEquals('$2a$', substr($encoded_password, 0, 4));
304
305 $another_encoder = new ilBcryptPasswordEncoder();
306 $another_encoder->setClientSalt(self::CLIENT_SALT);
307 $another_encoder->setBackwardCompatibility(false);
308 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
309 $this->assertEquals('$2y$', substr($another_encoded_password, 0, 4));
310 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
311 }

References skipIfPhpVersionIsNotSupported().

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 282 of file ilBcryptPasswordEncoderTest.php.

283 {
284 $encoder = new ilBcryptPasswordEncoder();
285 $encoder->setBackwardCompatibility(true);
286 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
287 $encoder->setBackwardCompatibility(false);
288 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
289 }

◆ testClientSaltIsGeneratedWhenNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testClientSaltIsGeneratedWhenNoClientSaltExistsYet ( )

Definition at line 260 of file ilBcryptPasswordEncoderTest.php.

261 {
262 $this->getTestDirectory()->chmod(0777);
263
264 $encoder = new ilBcryptPasswordEncoder();
265 $this->assertNotNull($encoder->getClientSalt());
266 }

References getTestDirectory().

+ Here is the call graph for this function:

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 130 of file ilBcryptPasswordEncoderTest.php.

131 {
132 $encoder->setCosts(4);
133 $this->assertEquals(4, $encoder->getCosts());
134 }

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 160 of file ilBcryptPasswordEncoderTest.php.

161 {
162 $encoder->setCosts($costs);
163 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 140 of file ilBcryptPasswordEncoderTest.php.

141 {
142 $this->assertException(ilPasswordException::class);
143 $encoder->setCosts(32);
144 }
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 150 of file ilBcryptPasswordEncoderTest.php.

151 {
152 $this->assertException(ilPasswordException::class);
153 $encoder->setCosts(3);
154 }

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

+ Here is the call graph for this function:

◆ testEncoderDoesNotSupportReencoding()

ilBcryptPasswordEncoderTest::testEncoderDoesNotSupportReencoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 208 of file ilBcryptPasswordEncoderTest.php.

209 {
210 $this->assertFalse($encoder->requiresReencoding('hello'));
211 }
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 200 of file ilBcryptPasswordEncoderTest.php.

201 {
202 $this->assertTrue($encoder->requiresSalt());
203 }
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 328 of file ilBcryptPasswordEncoderTest.php.

329 {
330 $encoder = new ilBcryptPasswordEncoder();
331 $encoder->setClientSalt(self::CLIENT_SALT);
332 $encoder->setBackwardCompatibility(true);
333 $encoder->setIsSecurityFlawIgnored(true);
334 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
335 }

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

@expectedException ilPasswordException

Definition at line 224 of file ilBcryptPasswordEncoderTest.php.

225 {
226 $this->assertException(ilPasswordException::class);
227 $encoder = new ilBcryptPasswordEncoder();
228 $encoder->setClientSalt(null);
229 $encoder->setCosts(self::VALID_COSTS);
230 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
231 }

References ilPasswordBaseTest\assertException().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

@expectedException ilPasswordException

Definition at line 236 of file ilBcryptPasswordEncoderTest.php.

237 {
238 $this->assertException(ilPasswordException::class);
239 $encoder = new ilBcryptPasswordEncoder();
240 $encoder->setClientSalt(null);
241 $encoder->setCosts(self::VALID_COSTS);
242 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
243 }

References ilPasswordBaseTest\assertException().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 181 of file ilBcryptPasswordEncoderTest.php.

182 {
183 $this->assertException(ilPasswordException::class);
184 $encoder->setCosts(self::VALID_COSTS);
185 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
186 }
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 316 of file ilBcryptPasswordEncoderTest.php.

317 {
318 $this->assertException(ilPasswordException::class);
319 $encoder = new ilBcryptPasswordEncoder();
320 $encoder->setClientSalt(self::CLIENT_SALT);
321 $encoder->setBackwardCompatibility(true);
322 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
323 }

References ilPasswordBaseTest\assertException().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ( )

@expectedException ilPasswordException

Definition at line 271 of file ilBcryptPasswordEncoderTest.php.

272 {
273 $this->assertException(ilPasswordException::class);
274 $this->getTestDirectory()->chmod(0000);
275
276 $encoder = new ilBcryptPasswordEncoder();
277 }

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

+ Here is the call graph for this function:

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )
Returns
ilBcryptPasswordEncoder

Definition at line 105 of file ilBcryptPasswordEncoderTest.php.

106 {
107 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder(array(
108 'ignore_security_flaw' => true
109 ));
110 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
111
112 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder(array(
113 'ignore_security_flaw' => false
114 ));
115 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
116
117 $encoder = new ilBcryptPasswordEncoder(array(
118 'cost' => self::VALID_COSTS
119 ));
120 $this->assertInstanceOf('ilBcryptPasswordEncoder', $encoder);
121 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
122 $this->assertFalse($encoder->isSecurityFlawIgnored());
123 $encoder->setClientSalt(self::CLIENT_SALT);
124 return $encoder;
125 }

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 248 of file ilBcryptPasswordEncoderTest.php.

249 {
250 $this->getTestDirectory()->chmod(0777);
251 vfs\vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->getTestDirectory());
252
253 $encoder = new ilBcryptPasswordEncoder();
254 $this->assertEquals(self::CLIENT_SALT, $encoder->getClientSalt());
255 }

References getTestDirectory(), and ilBcryptPasswordEncoder\SALT_STORAGE_FILENAME.

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 216 of file ilBcryptPasswordEncoderTest.php.

217 {
218 $this->assertEquals('bcrypt', $encoder->getName());
219 }

References ilBcryptPasswordEncoder\getName().

+ Here is the call graph for this function:

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 168 of file ilBcryptPasswordEncoderTest.php.

169 {
170 $encoder->setCosts(self::VALID_COSTS);
171 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
172 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
173 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
174 return $encoder;
175 }
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 191 of file ilBcryptPasswordEncoderTest.php.

192 {
193 $encoder->setCosts(self::VALID_COSTS);
194 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), self::PASSWORD_SALT));
195 }

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().

◆ 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: