ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
ilBcryptPasswordEncoderTest Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoderTest:
+ Collaboration diagram for ilBcryptPasswordEncoderTest:

Public Member Functions

 getTestDirectory ()
 
 setTestDirectory (vfs\vfsStreamDirectory $testDirectory)
 
 getTestDirectoryUrl ()
 
 setTestDirectoryUrl (string $testDirectoryUrl)
 
 testInstanceCanBeCreated ()
 
 testCostsCanBeRetrievedWhenCostsAreSet (ilBcryptPasswordEncoder $encoder)
 
 testCostsCannotBeSetAboveRange (ilBcryptPasswordEncoder $encoder)
 
 testCostsCannotBeSetBelowRange (ilBcryptPasswordEncoder $encoder)
 
 testCostsCanBeSetInRange (string $costs, ilBcryptPasswordEncoder $encoder)
 
 testPasswordShouldBeCorrectlyEncodedAndVerified (ilBcryptPasswordEncoder $encoder)
 
 testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding (ilBcryptPasswordEncoder $encoder)
 
 testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength (ilBcryptPasswordEncoder $encoder)
 
 testEncoderReliesOnSalts (ilBcryptPasswordEncoder $encoder)
 
 testEncoderDoesNotSupportReencoding (ilBcryptPasswordEncoder $encoder)
 
 testNameShouldBeBcrypt (ilBcryptPasswordEncoder $encoder)
 
 testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ()
 
 testExceptionIsRaisedIfSaltIsMissingIsOnVerification ()
 
 testInstanceCanBeCreatedAndInitializedWithClientSalt ()
 
 testClientSaltIsGeneratedWhenNoClientSaltExistsYet ()
 
 testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ()
 
 testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ()
 
 testBackwardCompatibility ()
 
 testExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabled ()
 
 testNoExceptionIfPasswordsContainA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ()
 

Static Public Member Functions

static costsProvider ()
 

Private Member Functions

 isVsfStreamInstalled ()
 
 skipIfvfsStreamNotSupported ()
 
 getInstanceWithConfiguredDataDirectory ()
 

Private Attributes

const string VALID_COSTS = '08'
 
const string PASSWORD = 'password'
 
const string WRONG_PASSWORD = 'wrong_password'
 
const string CLIENT_SALT = 'homer!12345_/'
 
const string PASSWORD_SALT = 'salt'
 
vfs vfsStreamDirectory $testDirectory
 
string $testDirectoryUrl
 

Detailed Description

Definition at line 26 of file ilBcryptPasswordEncoderTest.php.

Member Function Documentation

◆ costsProvider()

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

Definition at line 76 of file ilBcryptPasswordEncoderTest.php.

76 : array
77 {
78 $data = [];
79 for ($i = 4; $i <= 31; ++$i) {
80 $data[sprintf('Costs: %s', $i)] = [(string) $i];
81 }
82
83 return $data;
84 }

References $data.

◆ getInstanceWithConfiguredDataDirectory()

ilBcryptPasswordEncoderTest::getInstanceWithConfiguredDataDirectory ( )
private

Definition at line 86 of file ilBcryptPasswordEncoderTest.php.

87 {
88 return new ilBcryptPasswordEncoder([
89 'data_directory' => $this->testDirectoryUrl
90 ]);
91 }

◆ getTestDirectory()

ilBcryptPasswordEncoderTest::getTestDirectory ( )

Definition at line 37 of file ilBcryptPasswordEncoderTest.php.

37 : vfs\vfsStreamDirectory
38 {
40 }

References $testDirectory.

◆ getTestDirectoryUrl()

ilBcryptPasswordEncoderTest::getTestDirectoryUrl ( )

Definition at line 47 of file ilBcryptPasswordEncoderTest.php.

47 : string
48 {
50 }

References $testDirectoryUrl.

◆ isVsfStreamInstalled()

ilBcryptPasswordEncoderTest::isVsfStreamInstalled ( )
private

Definition at line 57 of file ilBcryptPasswordEncoderTest.php.

57 : bool
58 {
59 return class_exists('org\bovigo\vfs\vfsStreamWrapper');
60 }

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ setTestDirectory()

ilBcryptPasswordEncoderTest::setTestDirectory ( vfs\vfsStreamDirectory  $testDirectory)

Definition at line 42 of file ilBcryptPasswordEncoderTest.php.

42 : void
43 {
44 $this->testDirectory = $testDirectory;
45 }

References $testDirectory.

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ setTestDirectoryUrl()

ilBcryptPasswordEncoderTest::setTestDirectoryUrl ( string  $testDirectoryUrl)

Definition at line 52 of file ilBcryptPasswordEncoderTest.php.

52 : void
53 {
54 $this->testDirectoryUrl = $testDirectoryUrl;
55 }

References $testDirectoryUrl.

Referenced by skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ skipIfvfsStreamNotSupported()

ilBcryptPasswordEncoderTest::skipIfvfsStreamNotSupported ( )
private

Definition at line 62 of file ilBcryptPasswordEncoderTest.php.

62 : void
63 {
64 if (!$this->isVsfStreamInstalled()) {
65 $this->markTestSkipped('Skipped test, vfsStream (https://github.com/bovigo/vfsStream) required');
66 } else {
67 vfs\vfsStream::setup();
68 $this->setTestDirectory(vfs\vfsStream::newDirectory('test')->at(vfs\vfsStreamWrapper::getRoot()));
69 $this->setTestDirectoryUrl(vfs\vfsStream::url('root/test'));
70 }
71 }
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 263 of file ilBcryptPasswordEncoderTest.php.

263 : void
264 {
266
267 $encoder = $this->getInstanceWithConfiguredDataDirectory();
268 $encoder->setClientSalt(self::CLIENT_SALT);
269 $encoder->setBackwardCompatibility(true);
270
271 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
272 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
273 $this->assertSame('$2a$', substr($encoded_password, 0, 4));
274
275 $another_encoder = $this->getInstanceWithConfiguredDataDirectory();
276 $another_encoder->setClientSalt(self::CLIENT_SALT);
277
278 $another_encoder->setBackwardCompatibility(false);
279 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
280 $this->assertSame('$2y$', substr($another_encoded_password, 0, 4));
281 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
282 }

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

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 252 of file ilBcryptPasswordEncoderTest.php.

252 : void
253 {
255
256 $encoder = $this->getInstanceWithConfiguredDataDirectory();
257 $encoder->setBackwardCompatibility(true);
258 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
259 $encoder->setBackwardCompatibility(false);
260 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
261 }

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

+ Here is the call graph for this function:

◆ testClientSaltIsGeneratedWhenNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testClientSaltIsGeneratedWhenNoClientSaltExistsYet ( )

Definition at line 232 of file ilBcryptPasswordEncoderTest.php.

232 : void
233 {
235
236 $this->testDirectory->chmod(0777);
237
238 $encoder = $this->getInstanceWithConfiguredDataDirectory();
239 $this->assertNotNull($encoder->getClientSalt());
240 }

References ilBcryptPasswordEncoder\getClientSalt().

+ Here is the call graph for this function:

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

Definition at line 122 of file ilBcryptPasswordEncoderTest.php.

122 : void
123 {
124 $expected = '04';
125
126 $encoder->setCosts($expected);
127 $this->assertSame($expected, $encoder->getCosts());
128 }

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

+ Here is the call graph for this function:

◆ testCostsCanBeSetInRange()

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

Definition at line 147 of file ilBcryptPasswordEncoderTest.php.

147 : void
148 {
149 $encoder->setCosts($costs);
150 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

Definition at line 131 of file ilBcryptPasswordEncoderTest.php.

131 : void
132 {
133 $this->expectException(ilPasswordException::class);
134 $encoder->setCosts('32');
135 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetBelowRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetBelowRange ( ilBcryptPasswordEncoder  $encoder)

Definition at line 138 of file ilBcryptPasswordEncoderTest.php.

138 : void
139 {
140 $this->expectException(ilPasswordException::class);
141 $encoder->setCosts('3');
142 }

References ilBcryptPhpPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testEncoderDoesNotSupportReencoding()

ilBcryptPasswordEncoderTest::testEncoderDoesNotSupportReencoding ( ilBcryptPasswordEncoder  $encoder)

Definition at line 188 of file ilBcryptPasswordEncoderTest.php.

188 : void
189 {
190 $this->assertFalse($encoder->requiresReencoding('hello'));
191 }
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)

Definition at line 182 of file ilBcryptPasswordEncoderTest.php.

182 : void
183 {
184 $this->assertTrue($encoder->requiresSalt());
185 }
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 284 of file ilBcryptPasswordEncoderTest.php.

284 : void
285 {
287
288 $this->expectException(ilPasswordException::class);
289 $encoder = $this->getInstanceWithConfiguredDataDirectory();
290 $encoder->setClientSalt(self::CLIENT_SALT);
291 $encoder->setBackwardCompatibility(true);
292 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
293 }

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

Definition at line 199 of file ilBcryptPasswordEncoderTest.php.

199 : void
200 {
202
203 $this->expectException(ilPasswordException::class);
204 $encoder = $this->getInstanceWithConfiguredDataDirectory();
205 $encoder->setClientSalt(null);
206 $encoder->setCosts(self::VALID_COSTS);
207 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
208 }

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

Definition at line 210 of file ilBcryptPasswordEncoderTest.php.

210 : void
211 {
213
214 $this->expectException(ilPasswordException::class);
215 $encoder = $this->getInstanceWithConfiguredDataDirectory();
216 $encoder->setClientSalt(null);
217 $encoder->setCosts(self::VALID_COSTS);
218 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
219 }

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

Definition at line 165 of file ilBcryptPasswordEncoderTest.php.

167 : void {
168 $this->expectException(ilPasswordException::class);
169 $encoder->setCosts(self::VALID_COSTS);
170 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
171 }
encodePassword(string $raw, string $salt)
Encodes the raw password.

◆ testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedWhenClientSaltCouldNotBeGeneratedInCaseNoClientSaltExistsYet ( )

Definition at line 242 of file ilBcryptPasswordEncoderTest.php.

242 : void
243 {
245
246 $this->expectException(ilPasswordException::class);
247 $this->testDirectory->chmod(0000);
248
250 }

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )

Definition at line 93 of file ilBcryptPasswordEncoderTest.php.

94 {
96
97 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder([
98 'ignore_security_flaw' => true,
99 'data_directory' => $this->testDirectoryUrl
100 ]);
101 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
102
103 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder([
104 'ignore_security_flaw' => false,
105 'data_directory' => $this->testDirectoryUrl
106 ]);
107 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
108
109 $encoder = new ilBcryptPasswordEncoder([
110 'cost' => self::VALID_COSTS,
111 'data_directory' => $this->testDirectoryUrl
112 ]);
113 $this->assertInstanceOf(ilBcryptPasswordEncoder::class, $encoder);
114 $this->assertSame(self::VALID_COSTS, $encoder->getCosts());
115 $this->assertFalse($encoder->isSecurityFlawIgnored());
116 $encoder->setClientSalt(self::CLIENT_SALT);
117
118 return $encoder;
119 }

References skipIfvfsStreamNotSupported().

+ Here is the call graph for this function:

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 221 of file ilBcryptPasswordEncoderTest.php.

221 : void
222 {
224
225 $this->testDirectory->chmod(0777);
226 vfs\vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->testDirectory);
227
228 $encoder = $this->getInstanceWithConfiguredDataDirectory();
229 $this->assertSame(self::CLIENT_SALT, $encoder->getClientSalt());
230 }

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

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( ilBcryptPasswordEncoder  $encoder)

Definition at line 194 of file ilBcryptPasswordEncoderTest.php.

194 : void
195 {
196 $this->assertSame('bcrypt', $encoder->getName());
197 }
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 ( )

Definition at line 296 of file ilBcryptPasswordEncoderTest.php.

296 : void
297 {
299
300 $encoder = $this->getInstanceWithConfiguredDataDirectory();
301 $encoder->setClientSalt(self::CLIENT_SALT);
302 $encoder->setBackwardCompatibility(true);
303 $encoder->setIsSecurityFlawIgnored(true);
304 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
305 }

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

+ Here is the call graph for this function:

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

Definition at line 153 of file ilBcryptPasswordEncoderTest.php.

156 $encoder->setCosts(self::VALID_COSTS);
157 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
158 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
159 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
160
161 return $encoder;
162 }
isPasswordValid(string $encoded, string $raw, string $salt)
Checks a raw password against an encoded password.

◆ testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength()

ilBcryptPasswordEncoderTest::testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength ( ilBcryptPasswordEncoder  $encoder)

Definition at line 174 of file ilBcryptPasswordEncoderTest.php.

176 : void {
177 $encoder->setCosts(self::VALID_COSTS);
178 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), self::PASSWORD_SALT));
179 }

Field Documentation

◆ $testDirectory

vfs vfsStreamDirectory ilBcryptPasswordEncoderTest::$testDirectory
private

Definition at line 34 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectory(), and setTestDirectory().

◆ $testDirectoryUrl

string ilBcryptPasswordEncoderTest::$testDirectoryUrl
private

Definition at line 35 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectoryUrl(), and setTestDirectoryUrl().

◆ CLIENT_SALT

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

Definition at line 31 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD

const string ilBcryptPasswordEncoderTest::PASSWORD = 'password'
private

Definition at line 29 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD_SALT

const string ilBcryptPasswordEncoderTest::PASSWORD_SALT = 'salt'
private

Definition at line 32 of file ilBcryptPasswordEncoderTest.php.

◆ VALID_COSTS

const string ilBcryptPasswordEncoderTest::VALID_COSTS = '08'
private

Definition at line 28 of file ilBcryptPasswordEncoderTest.php.

◆ WRONG_PASSWORD

const string ilBcryptPasswordEncoderTest::WRONG_PASSWORD = 'wrong_password'
private

Definition at line 30 of file ilBcryptPasswordEncoderTest.php.


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