ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
ilBcryptPasswordEncoderTest Class Reference
+ Inheritance diagram for ilBcryptPasswordEncoderTest:
+ Collaboration diagram for ilBcryptPasswordEncoderTest:

Public Member Functions

 getTestDirectory ()
 
 setTestDirectory ($test_directory)
 
 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...
 
 costsProvider ()
 
 testPasswordShouldBeCorrectlyEncodedAndVerified (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated @expectedException ilPasswordException More...
 
 testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength (ilBcryptPasswordEncoder $encoder)
 @depends testInstanceCanBeCreated More...
 
 testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ()
 @expectedException ilPasswordException More...
 
 testExceptionIsRaisedIfSaltIsMissingIsOnVerification ()
 @expectedException ilPasswordException More...
 
 testInstanceCanBeCreatedAndInitializedWithClientSalt ()
 
 testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ()
 
 testBackwardCompatibility ()
 
 testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled ()
 @expectedException ilPasswordException More...
 
 testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ()
 
 testNameShouldBeBcrypt ()
 

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 Attributes

 $test_directory
 

Private Member Functions

 isVsfStreamInstalled ()
 
 skipIfPhpVersionIsNotSupported ()
 
 skipIfvfsStreamNotSupported ()
 

Detailed Description

Definition at line 11 of file ilBcryptPasswordEncoderTest.php.

Member Function Documentation

◆ costsProvider()

ilBcryptPasswordEncoderTest::costsProvider ( )
Returns
array

Definition at line 145 of file ilBcryptPasswordEncoderTest.php.

146 {
147 $data = array();
148 for($i = 4; $i <= 31; $i++)
149 {
150 $data[] = array($i);
151 }
152 return $data;
153 }

References $data.

◆ getTestDirectory()

ilBcryptPasswordEncoderTest::getTestDirectory ( )
Returns
vfsStreamDirectory

Definition at line 46 of file ilBcryptPasswordEncoderTest.php.

References $test_directory.

Referenced by testInstanceCanBeCreatedAndInitializedWithClientSalt().

+ Here is the caller graph for this function:

◆ isVsfStreamInstalled()

ilBcryptPasswordEncoderTest::isVsfStreamInstalled ( )
private
Returns
bool

Definition at line 62 of file ilBcryptPasswordEncoderTest.php.

63 {
64 return @include_once('vfsStream.php');
65 }

Referenced by setUp(), and skipIfvfsStreamNotSupported().

+ Here is the caller graph for this function:

◆ setTestDirectory()

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

Definition at line 54 of file ilBcryptPasswordEncoderTest.php.

55 {
56 $this->test_directory = $test_directory;
57 }

References $test_directory.

Referenced by setUp().

+ Here is the caller graph for this function:

◆ setUp()

ilBcryptPasswordEncoderTest::setUp ( )
protected

Setup.

Definition at line 70 of file ilBcryptPasswordEncoderTest.php.

71 {
72 if($this->isVsfStreamInstalled())
73 {
74 vfsStream::setup();
75 $this->setTestDirectory(vfsStream::newDirectory('tests')->at(vfsStreamWrapper::getRoot()));
76 define('CLIENT_DATA_DIR', vfsStream::url('root/tests'));
77 }
78 parent::setUp();
79 }

References isVsfStreamInstalled(), and setTestDirectory().

+ Here is the call graph for this function:

◆ skipIfPhpVersionIsNotSupported()

ilBcryptPasswordEncoderTest::skipIfPhpVersionIsNotSupported ( )
private

Definition at line 259 of file ilBcryptPasswordEncoderTest.php.

260 {
261 if(version_compare(phpversion(), '5.3.7', '<'))
262 {
263 $this->markTestSkipped('Requires PHP >= 5.3.7');
264 }
265 }

Referenced by testBackwardCompatibility().

+ Here is the caller graph for this function:

◆ skipIfvfsStreamNotSupported()

ilBcryptPasswordEncoderTest::skipIfvfsStreamNotSupported ( )
private

Definition at line 270 of file ilBcryptPasswordEncoderTest.php.

271 {
272 if(!$this->isVsfStreamInstalled())
273 {
274 $this->markTestSkipped('Requires vfsStream (http://vfs.bovigo.org)');
275 }
276 }

References isVsfStreamInstalled().

Referenced by testInstanceCanBeCreatedAndInitializedWithClientSalt().

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

◆ testBackwardCompatibility()

ilBcryptPasswordEncoderTest::testBackwardCompatibility ( )

Definition at line 237 of file ilBcryptPasswordEncoderTest.php.

238 {
240
241 $encoder = new ilBcryptPasswordEncoder();
242 $encoder->setClientSalt(self::CLIENT_SALT);
243 $encoder->setBackwardCompatibility(true);
244 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
245 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
246 $this->assertEquals('$2a$', substr($encoded_password, 0, 4));
247
248 $another_encoder = new ilBcryptPasswordEncoder();
249 $another_encoder->setClientSalt(self::CLIENT_SALT);
250 $another_encoder->setBackwardCompatibility(false);
251 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
252 $this->assertEquals('$2y$', substr($another_encoded_password, 0, 4));
253 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
254 }

References skipIfPhpVersionIsNotSupported().

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 225 of file ilBcryptPasswordEncoderTest.php.

226 {
227 $encoder = new ilBcryptPasswordEncoder();
228 $encoder->setBackwardCompatibility(true);
229 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
230 $encoder->setBackwardCompatibility(false);
231 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
232 }

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 109 of file ilBcryptPasswordEncoderTest.php.

110 {
111 $encoder->setCosts(4);
112 $this->assertEquals(4, $encoder->getCosts());
113 }

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

+ Here is the call graph for this function:

◆ testCostsCanBeSetInRange()

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

@depends testInstanceCanBeCreated @dataProvider costsProvider

Definition at line 137 of file ilBcryptPasswordEncoderTest.php.

138 {
139 $encoder->setCosts($costs);
140 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 119 of file ilBcryptPasswordEncoderTest.php.

120 {
121 $encoder->setCosts(32);
122 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetBelowRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetBelowRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 128 of file ilBcryptPasswordEncoderTest.php.

129 {
130 $encoder->setCosts(3);
131 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()

ilBcryptPasswordEncoderTest::testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ( )

Definition at line 292 of file ilBcryptPasswordEncoderTest.php.

293 {
294 $encoder = new ilBcryptPasswordEncoder();
295 $encoder->setClientSalt(self::CLIENT_SALT);
296 $encoder->setBackwardCompatibility(true);
297 $encoder->setIsSecurityFlawIgnored(true);
298 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
299 }

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

@expectedException ilPasswordException

Definition at line 189 of file ilBcryptPasswordEncoderTest.php.

190 {
191 $encoder = new ilBcryptPasswordEncoder();
192 $encoder->setClientSalt(null);
193 $encoder->setCosts(self::VALID_COSTS);
194 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
195 }

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

@expectedException ilPasswordException

Definition at line 200 of file ilBcryptPasswordEncoderTest.php.

201 {
202 $encoder = new ilBcryptPasswordEncoder();
203 $encoder->setClientSalt(null);
204 $encoder->setCosts(self::VALID_COSTS);
205 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
206 }

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 171 of file ilBcryptPasswordEncoderTest.php.

172 {
173 $encoder->setCosts(self::VALID_COSTS);
174 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
175 }
encodePassword($raw, $salt)
{Encodes the raw password.string The encoded password}

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

+ Here is the call graph for this function:

◆ testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabled ( )

@expectedException ilPasswordException

Definition at line 281 of file ilBcryptPasswordEncoderTest.php.

282 {
283 $encoder = new ilBcryptPasswordEncoder();
284 $encoder->setClientSalt(self::CLIENT_SALT);
285 $encoder->setBackwardCompatibility(true);
286 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
287 }

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )
Returns
ilBcryptPasswordEncoder

Definition at line 84 of file ilBcryptPasswordEncoderTest.php.

85 {
86 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder(array(
87 'ignore_security_flaw' => true
88 ));
89 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
90
91 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder(array(
92 'ignore_security_flaw' => false
93 ));
94 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
95
96 $encoder = new ilBcryptPasswordEncoder(array(
97 'cost' => self::VALID_COSTS
98 ));
99 $this->assertInstanceOf('ilBcryptPasswordEncoder', $encoder);
100 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
101 $this->assertFalse($encoder->isSecurityFlawIgnored());
102 $encoder->setClientSalt(self::CLIENT_SALT);
103 return $encoder;
104 }

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 211 of file ilBcryptPasswordEncoderTest.php.

212 {
214
215 $this->getTestDirectory()->chmod(0777);
216 vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->getTestDirectory());
217
218 $encoder = new ilBcryptPasswordEncoder();
219 $this->assertEquals(self::CLIENT_SALT, $encoder->getClientSalt());
220 }

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

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( )

Definition at line 304 of file ilBcryptPasswordEncoderTest.php.

305 {
306 $encoder = new ilBcryptPasswordEncoder();
307 $this->assertEquals('bcrypt', $encoder->getName());
308 }

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 158 of file ilBcryptPasswordEncoderTest.php.

159 {
160 $encoder->setCosts(self::VALID_COSTS);
161 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
162 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
163 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, self::PASSWORD_SALT));
164 return $encoder;
165 }
isPasswordValid($encoded, $raw, $salt)
{Checks a raw password against an encoded password.The raw password has to be injected into the encod...

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

+ Here is the call graph for this function:

◆ testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength()

ilBcryptPasswordEncoderTest::testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 180 of file ilBcryptPasswordEncoderTest.php.

181 {
182 $encoder->setCosts(self::VALID_COSTS);
183 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), self::PASSWORD_SALT));
184 }

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

+ Here is the call graph for this function:

Field Documentation

◆ $test_directory

ilBcryptPasswordEncoderTest::$test_directory
protected

Definition at line 41 of file ilBcryptPasswordEncoderTest.php.

Referenced by getTestDirectory(), and setTestDirectory().

◆ CLIENT_SALT

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

Definition at line 31 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD

const ilBcryptPasswordEncoderTest::PASSWORD = 'password'

Definition at line 21 of file ilBcryptPasswordEncoderTest.php.

◆ PASSWORD_SALT

const ilBcryptPasswordEncoderTest::PASSWORD_SALT = 'salt'

Definition at line 36 of file ilBcryptPasswordEncoderTest.php.

◆ VALID_COSTS

const ilBcryptPasswordEncoderTest::VALID_COSTS = '08'

Definition at line 16 of file ilBcryptPasswordEncoderTest.php.

◆ WRONG_PASSWORD

const ilBcryptPasswordEncoderTest::WRONG_PASSWORD = 'wrong_password'

Definition at line 26 of file ilBcryptPasswordEncoderTest.php.


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