ILIAS  release_8 Revision v8.24
ilBcryptPasswordEncoderTest Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoderTest:
+ Collaboration diagram for ilBcryptPasswordEncoderTest:

Public Member Functions

 getTestDirectory ()
 
 setTestDirectory (vfs\vfsStreamDirectory $testDirectory)
 
 getTestDirectoryUrl ()
 
 setTestDirectoryUrl (string $testDirectoryUrl)
 
 costsProvider ()
 
 testInstanceCanBeCreated ()
 
 testCostsCanBeRetrievedWhenCostsAreSet (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testCostsCannotBeSetAboveRange (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testCostsCannotBeSetBelowRange (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testCostsCanBeSetInRange (string $costs, ilBcryptPasswordEncoder $encoder)
 @doesNotPerformAssertions @depends testInstanceCanBeCreated @dataProvider costsProvider More...
 
 testPasswordShouldBeCorrectlyEncodedAndVerified (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated 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 ()
 
 testExceptionIsRaisedIfSaltIsMissingIsOnVerification ()
 
 testInstanceCanBeCreatedAndInitializedWithClientSalt ()
 
 testClientSaltIsGeneratedWhenNoClientSaltExistsYet ()
 
 testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ()
 
 testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ()
 
 testBackwardCompatibility ()
 
 testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled ()
 
 testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ()
 @doesNotPerformAssertions More...
 

Protected Attributes

vfs vfsStreamDirectory $testDirectory
 
string $testDirectoryUrl
 

Private Member Functions

 isVsfStreamInstalled ()
 
 skipIfvfsStreamNotSupported ()
 
 getInstanceWithConfiguredDataDirectory ()
 

Private Attributes

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

Detailed Description

Definition at line 28 of file ilBcryptPasswordEncoderTest.php.

Member Function Documentation

◆ costsProvider()

ilBcryptPasswordEncoderTest::costsProvider ( )
Returns
array<string, string[]>

Definition at line 87 of file ilBcryptPasswordEncoderTest.php.

87 : array
88 {
89 $data = [];
90 for ($i = 4; $i <= 31; ++$i) {
91 $data[sprintf('Costs: %s', $i)] = [(string) $i];
92 }
93
94 return $data;
95 }
$i
Definition: metadata.php:41

References $data, and $i.

◆ getInstanceWithConfiguredDataDirectory()

ilBcryptPasswordEncoderTest::getInstanceWithConfiguredDataDirectory ( )
private

Definition at line 97 of file ilBcryptPasswordEncoderTest.php.

References getTestDirectoryUrl().

+ Here is the call graph for this function:

◆ getTestDirectory()

ilBcryptPasswordEncoderTest::getTestDirectory ( )

Definition at line 48 of file ilBcryptPasswordEncoderTest.php.

48 : vfs\vfsStreamDirectory
49 {
51 }

References $testDirectory.

◆ getTestDirectoryUrl()

ilBcryptPasswordEncoderTest::getTestDirectoryUrl ( )

Definition at line 58 of file ilBcryptPasswordEncoderTest.php.

58 : string
59 {
61 }

References $testDirectoryUrl.

Referenced by getInstanceWithConfiguredDataDirectory(), and testInstanceCanBeCreated().

+ Here is the caller graph for this function:

◆ isVsfStreamInstalled()

ilBcryptPasswordEncoderTest::isVsfStreamInstalled ( )
private

Definition at line 68 of file ilBcryptPasswordEncoderTest.php.

68 : bool
69 {
70 return class_exists('org\bovigo\vfs\vfsStreamWrapper');
71 }

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ setTestDirectory()

ilBcryptPasswordEncoderTest::setTestDirectory ( vfs\vfsStreamDirectory  $testDirectory)

Definition at line 53 of file ilBcryptPasswordEncoderTest.php.

53 : void
54 {
55 $this->testDirectory = $testDirectory;
56 }

References $testDirectory.

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ setTestDirectoryUrl()

ilBcryptPasswordEncoderTest::setTestDirectoryUrl ( string  $testDirectoryUrl)

Definition at line 63 of file ilBcryptPasswordEncoderTest.php.

63 : void
64 {
65 $this->testDirectoryUrl = $testDirectoryUrl;
66 }

References $testDirectoryUrl.

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ skipIfvfsStreamNotSupported()

ilBcryptPasswordEncoderTest::skipIfvfsStreamNotSupported ( )
private

Definition at line 73 of file ilBcryptPasswordEncoderTest.php.

73 : void
74 {
75 if (!$this->isVsfStreamInstalled()) {
76 $this->markTestSkipped('Skipped test, vfsStream (https://github.com/bovigo/vfsStream) required');
77 } else {
78 vfs\vfsStream::setup();
79 $this->setTestDirectory(vfs\vfsStream::newDirectory('tests')->at(vfs\vfsStreamWrapper::getRoot()));
80 $this->setTestDirectoryUrl(vfs\vfsStream::url('root/tests'));
81 }
82 }
setTestDirectory(vfs\vfsStreamDirectory $testDirectory)
setTestDirectoryUrl(string $testDirectoryUrl)

References isVsfStreamInstalled(), setTestDirectory(), and setTestDirectoryUrl().

Referenced by testInstanceCanBeCreated().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ testBackwardCompatibility()

ilBcryptPasswordEncoderTest::testBackwardCompatibility ( )

Definition at line 301 of file ilBcryptPasswordEncoderTest.php.

301 : void
302 {
304
305 $encoder = $this->getInstanceWithConfiguredDataDirectory();
306 $encoder->setClientSalt(self::CLIENT_SALT);
307 $encoder->setBackwardCompatibility(true);
308
309 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
310 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
311 $this->assertSame('$2a$', substr($encoded_password, 0, 4));
312
313 $another_encoder = $this->getInstanceWithConfiguredDataDirectory();
314 $another_encoder->setClientSalt(self::CLIENT_SALT);
315
316 $another_encoder->setBackwardCompatibility(false);
317 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
318 $this->assertSame('$2y$', substr($another_encoded_password, 0, 4));
319 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
320 }

References ilBcryptPasswordEncoder\encodePassword(), ilBcryptPasswordEncoder\isPasswordValid(), ilBcryptPasswordEncoder\setBackwardCompatibility(), and ilBcryptPasswordEncoder\setClientSalt().

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 290 of file ilBcryptPasswordEncoderTest.php.

290 : void
291 {
293
294 $encoder = $this->getInstanceWithConfiguredDataDirectory();
295 $encoder->setBackwardCompatibility(true);
296 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
297 $encoder->setBackwardCompatibility(false);
298 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
299 }

References ilBcryptPasswordEncoder\isBackwardCompatibilityEnabled(), and ilBcryptPasswordEncoder\setBackwardCompatibility().

+ Here is the call graph for this function:

◆ testClientSaltIsGeneratedWhenNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testClientSaltIsGeneratedWhenNoClientSaltExistsYet ( )

Definition at line 270 of file ilBcryptPasswordEncoderTest.php.

270 : void
271 {
273
274 $this->getTestDirectory()->chmod(0777);
275
276 $encoder = $this->getInstanceWithConfiguredDataDirectory();
277 $this->assertNotNull($encoder->getClientSalt());
278 }

References ilBcryptPasswordEncoder\getClientSalt().

+ Here is the call graph for this function:

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 136 of file ilBcryptPasswordEncoderTest.php.

136 : void
137 {
138 $expected = '04';
139
140 $encoder->setCosts($expected);
141 $this->assertSame($expected, $encoder->getCosts());
142 }

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

+ Here is the call graph for this function:

◆ testCostsCanBeSetInRange()

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

@doesNotPerformAssertions @depends testInstanceCanBeCreated @dataProvider costsProvider

Exceptions
ilPasswordException

Definition at line 170 of file ilBcryptPasswordEncoderTest.php.

170 : void
171 {
172 $encoder->setCosts($costs);
173 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 148 of file ilBcryptPasswordEncoderTest.php.

148 : void
149 {
150 $this->expectException(ilPasswordException::class);
151 $encoder->setCosts('32');
152 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetBelowRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetBelowRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 158 of file ilBcryptPasswordEncoderTest.php.

158 : void
159 {
160 $this->expectException(ilPasswordException::class);
161 $encoder->setCosts('3');
162 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testEncoderDoesNotSupportReencoding()

ilBcryptPasswordEncoderTest::testEncoderDoesNotSupportReencoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 224 of file ilBcryptPasswordEncoderTest.php.

224 : void
225 {
226 $this->assertFalse($encoder->requiresReencoding('hello'));
227 }
requiresReencoding(string $encoded)
Returns whether the encoded password needs to be re-encoded.

References ilBcryptPasswordEncoder\requiresReencoding().

+ Here is the call graph for this function:

◆ testEncoderReliesOnSalts()

ilBcryptPasswordEncoderTest::testEncoderReliesOnSalts ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 216 of file ilBcryptPasswordEncoderTest.php.

216 : void
217 {
218 $this->assertTrue($encoder->requiresSalt());
219 }
requiresSalt()
Returns whether the encoder requires a salt.

References ilBcryptPasswordEncoder\requiresSalt().

+ Here is the call graph for this function:

◆ testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled()

ilBcryptPasswordEncoderTest::testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled ( )

Definition at line 322 of file ilBcryptPasswordEncoderTest.php.

322 : void
323 {
325
326 $this->expectException(ilPasswordException::class);
327 $encoder = $this->getInstanceWithConfiguredDataDirectory();
328 $encoder->setClientSalt(self::CLIENT_SALT);
329 $encoder->setBackwardCompatibility(true);
330 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
331 }

References ilBcryptPasswordEncoder\encodePassword(), ilBcryptPasswordEncoder\setBackwardCompatibility(), and ilBcryptPasswordEncoder\setClientSalt().

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

Definition at line 237 of file ilBcryptPasswordEncoderTest.php.

237 : void
238 {
240
241 $this->expectException(ilPasswordException::class);
242 $encoder = $this->getInstanceWithConfiguredDataDirectory();
243 $encoder->setClientSalt(null);
244 $encoder->setCosts(self::VALID_COSTS);
245 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
246 }

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

Definition at line 248 of file ilBcryptPasswordEncoderTest.php.

248 : void
249 {
251
252 $this->expectException(ilPasswordException::class);
253 $encoder = $this->getInstanceWithConfiguredDataDirectory();
254 $encoder->setClientSalt(null);
255 $encoder->setCosts(self::VALID_COSTS);
256 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
257 }

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 194 of file ilBcryptPasswordEncoderTest.php.

196 : void {
197 $this->expectException(ilPasswordException::class);
198 $encoder->setCosts(self::VALID_COSTS);
199 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
200 }
encodePassword(string $raw, string $salt)
Encodes the raw password.

◆ testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ( )

Definition at line 280 of file ilBcryptPasswordEncoderTest.php.

280 : void
281 {
283
284 $this->expectException(ilPasswordException::class);
285 $this->getTestDirectory()->chmod(0000);
286
288 }

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )

Definition at line 104 of file ilBcryptPasswordEncoderTest.php.

105 {
107
108 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder([
109 'ignore_security_flaw' => true,
110 'data_directory' => $this->getTestDirectoryUrl()
111 ]);
112 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
113
114 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder([
115 'ignore_security_flaw' => false,
116 'data_directory' => $this->getTestDirectoryUrl()
117 ]);
118 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
119
120 $encoder = new ilBcryptPasswordEncoder([
121 'cost' => self::VALID_COSTS,
122 'data_directory' => $this->getTestDirectoryUrl()
123 ]);
124 $this->assertInstanceOf(ilBcryptPasswordEncoder::class, $encoder);
125 $this->assertSame(self::VALID_COSTS, $encoder->getCosts());
126 $this->assertFalse($encoder->isSecurityFlawIgnored());
127 $encoder->setClientSalt(self::CLIENT_SALT);
128
129 return $encoder;
130 }

References getTestDirectoryUrl(), and skipIfvfsStreamNotSupported().

+ Here is the call graph for this function:

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 259 of file ilBcryptPasswordEncoderTest.php.

259 : void
260 {
262
263 $this->getTestDirectory()->chmod(0777);
264 vfs\vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->getTestDirectory());
265
266 $encoder = $this->getInstanceWithConfiguredDataDirectory();
267 $this->assertSame(self::CLIENT_SALT, $encoder->getClientSalt());
268 }

References ilBcryptPasswordEncoder\getClientSalt(), and ilBcryptPasswordEncoder\SALT_STORAGE_FILENAME.

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 232 of file ilBcryptPasswordEncoderTest.php.

232 : void
233 {
234 $this->assertSame('bcrypt', $encoder->getName());
235 }
getName()
Returns a unique name/id of the concrete password encoder.

References ilBcryptPasswordEncoder\getName().

+ Here is the call graph for this function:

◆ testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()

ilBcryptPasswordEncoderTest::testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ( )

@doesNotPerformAssertions

Exceptions
ilPasswordException

Definition at line 337 of file ilBcryptPasswordEncoderTest.php.

337 : void
338 {
340
341 $encoder = $this->getInstanceWithConfiguredDataDirectory();
342 $encoder->setClientSalt(self::CLIENT_SALT);
343 $encoder->setBackwardCompatibility(true);
344 $encoder->setIsSecurityFlawIgnored(true);
345 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
346 }

References ilBcryptPasswordEncoder\encodePassword(), ilBcryptPasswordEncoder\setBackwardCompatibility(), ilBcryptPasswordEncoder\setClientSalt(), and ilBcryptPasswordEncoder\setIsSecurityFlawIgnored().

+ Here is the call graph for this function:

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 179 of file ilBcryptPasswordEncoderTest.php.

182 $encoder->setCosts(self::VALID_COSTS);
183 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
184 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
185 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
186
187 return $encoder;
188 }
isPasswordValid(string $encoded, string $raw, string $salt)
Checks a raw password against an encoded password.

◆ testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength()

ilBcryptPasswordEncoderTest::testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Exceptions
ilPasswordException

Definition at line 206 of file ilBcryptPasswordEncoderTest.php.

208 : void {
209 $encoder->setCosts(self::VALID_COSTS);
210 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), self::PASSWORD_SALT));
211 }

Field Documentation

◆ $testDirectory

vfs vfsStreamDirectory ilBcryptPasswordEncoderTest::$testDirectory
protected

Definition at line 45 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectory(), and setTestDirectory().

◆ $testDirectoryUrl

string ilBcryptPasswordEncoderTest::$testDirectoryUrl
protected

Definition at line 46 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectoryUrl(), and setTestDirectoryUrl().

◆ CLIENT_SALT

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

Definition at line 40 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD

const ilBcryptPasswordEncoderTest::PASSWORD = 'password'
private

Definition at line 34 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD_SALT

const ilBcryptPasswordEncoderTest::PASSWORD_SALT = 'salt'
private

Definition at line 43 of file ilBcryptPasswordEncoderTest.php.

◆ VALID_COSTS

const ilBcryptPasswordEncoderTest::VALID_COSTS = '08'
private

Definition at line 31 of file ilBcryptPasswordEncoderTest.php.

◆ WRONG_PASSWORD

const ilBcryptPasswordEncoderTest::WRONG_PASSWORD = 'wrong_password'
private

Definition at line 37 of file ilBcryptPasswordEncoderTest.php.


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