ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 153 of file ilBcryptPasswordEncoderTest.php.

154 {
155 $data = array();
156 for($i = 4; $i <= 31; $i++)
157 {
158 $data[] = array($i);
159 }
160 return $data;
161 }
$data

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 if(!defined('CLIENT_DATA_DIR'))
77 {
78 define('CLIENT_DATA_DIR', vfsStream::url('root/tests'));
79 }
80 }
81 else if(!defined('CLIENT_DATA_DIR'))
82 {
83 define('CLIENT_DATA_DIR', '/tmp');
84 }
85
86 parent::setUp();
87 }

References isVsfStreamInstalled(), and setTestDirectory().

+ Here is the call graph for this function:

◆ skipIfPhpVersionIsNotSupported()

ilBcryptPasswordEncoderTest::skipIfPhpVersionIsNotSupported ( )
private

Definition at line 267 of file ilBcryptPasswordEncoderTest.php.

268 {
269 if(version_compare(phpversion(), '5.3.7', '<'))
270 {
271 $this->markTestSkipped('Requires PHP >= 5.3.7');
272 }
273 }

Referenced by testBackwardCompatibility().

+ Here is the caller graph for this function:

◆ skipIfvfsStreamNotSupported()

ilBcryptPasswordEncoderTest::skipIfvfsStreamNotSupported ( )
private

Definition at line 278 of file ilBcryptPasswordEncoderTest.php.

279 {
280 if(!$this->isVsfStreamInstalled())
281 {
282 $this->markTestSkipped('Requires vfsStream (http://vfs.bovigo.org)');
283 }
284 }

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

246 {
248
249 $encoder = new ilBcryptPasswordEncoder();
250 $encoder->setClientSalt(self::CLIENT_SALT);
251 $encoder->setBackwardCompatibility(true);
252 $encoded_password = $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
253 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
254 $this->assertEquals('$2a$', substr($encoded_password, 0, 4));
255
256 $another_encoder = new ilBcryptPasswordEncoder();
257 $another_encoder->setClientSalt(self::CLIENT_SALT);
258 $another_encoder->setBackwardCompatibility(false);
259 $another_encoded_password = $another_encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
260 $this->assertEquals('$2y$', substr($another_encoded_password, 0, 4));
261 $this->assertTrue($another_encoder->isPasswordValid($encoded_password, self::PASSWORD, self::PASSWORD_SALT));
262 }

References skipIfPhpVersionIsNotSupported().

+ Here is the call graph for this function:

◆ testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet()

ilBcryptPasswordEncoderTest::testBackwardCompatibilityCanBeRetrievedWhenBackwardCompatibilityIsSet ( )

Definition at line 233 of file ilBcryptPasswordEncoderTest.php.

234 {
235 $encoder = new ilBcryptPasswordEncoder();
236 $encoder->setBackwardCompatibility(true);
237 $this->assertTrue($encoder->isBackwardCompatibilityEnabled());
238 $encoder->setBackwardCompatibility(false);
239 $this->assertFalse($encoder->isBackwardCompatibilityEnabled());
240 }

◆ testCostsCanBeRetrievedWhenCostsAreSet()

ilBcryptPasswordEncoderTest::testCostsCanBeRetrievedWhenCostsAreSet ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 117 of file ilBcryptPasswordEncoderTest.php.

118 {
119 $encoder->setCosts(4);
120 $this->assertEquals(4, $encoder->getCosts());
121 }

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

146 {
147 $encoder->setCosts($costs);
148 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetAboveRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetAboveRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 127 of file ilBcryptPasswordEncoderTest.php.

128 {
129 $encoder->setCosts(32);
130 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testCostsCannotBeSetBelowRange()

ilBcryptPasswordEncoderTest::testCostsCannotBeSetBelowRange ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 136 of file ilBcryptPasswordEncoderTest.php.

137 {
138 $encoder->setCosts(3);
139 }

References ilBcryptPasswordEncoder\setCosts().

+ Here is the call graph for this function:

◆ testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw()

ilBcryptPasswordEncoderTest::testExceptionIsNotRaisedIfTheRawPasswordContainsA8BitCharacterAndBackwardCompatibilityIsEnabledWithIgnoredSecurityFlaw ( )

Definition at line 300 of file ilBcryptPasswordEncoderTest.php.

301 {
302 $encoder = new ilBcryptPasswordEncoder();
303 $encoder->setClientSalt(self::CLIENT_SALT);
304 $encoder->setBackwardCompatibility(true);
305 $encoder->setIsSecurityFlawIgnored(true);
306 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
307 }

◆ testExceptionIsRaisedIfSaltIsMissingIsOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnEncoding ( )

@expectedException ilPasswordException

Definition at line 197 of file ilBcryptPasswordEncoderTest.php.

198 {
199 $encoder = new ilBcryptPasswordEncoder();
200 $encoder->setClientSalt(null);
201 $encoder->setCosts(self::VALID_COSTS);
202 $encoder->encodePassword(self::PASSWORD, self::PASSWORD_SALT);
203 }

◆ testExceptionIsRaisedIfSaltIsMissingIsOnVerification()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfSaltIsMissingIsOnVerification ( )

@expectedException ilPasswordException

Definition at line 208 of file ilBcryptPasswordEncoderTest.php.

209 {
210 $encoder = new ilBcryptPasswordEncoder();
211 $encoder->setClientSalt(null);
212 $encoder->setCosts(self::VALID_COSTS);
213 $encoder->isPasswordValid('12121212', self::PASSWORD, self::PASSWORD_SALT);
214 }

◆ testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding()

ilBcryptPasswordEncoderTest::testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated @expectedException ilPasswordException

Definition at line 179 of file ilBcryptPasswordEncoderTest.php.

180 {
181 $encoder->setCosts(self::VALID_COSTS);
182 $encoder->encodePassword(str_repeat('a', 5000), self::PASSWORD_SALT);
183 }
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 289 of file ilBcryptPasswordEncoderTest.php.

290 {
291 $encoder = new ilBcryptPasswordEncoder();
292 $encoder->setClientSalt(self::CLIENT_SALT);
293 $encoder->setBackwardCompatibility(true);
294 $encoder->encodePassword(self::PASSWORD . chr(195), self::PASSWORD_SALT);
295 }

◆ testInstanceCanBeCreated()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreated ( )
Returns
ilBcryptPasswordEncoder

Definition at line 92 of file ilBcryptPasswordEncoderTest.php.

93 {
94 $security_flaw_ignoring_encoder = new ilBcryptPasswordEncoder(array(
95 'ignore_security_flaw' => true
96 ));
97 $this->assertTrue($security_flaw_ignoring_encoder->isSecurityFlawIgnored());
98
99 $security_flaw_respecting_encoder = new ilBcryptPasswordEncoder(array(
100 'ignore_security_flaw' => false
101 ));
102 $this->assertFalse($security_flaw_respecting_encoder->isSecurityFlawIgnored());
103
104 $encoder = new ilBcryptPasswordEncoder(array(
105 'cost' => self::VALID_COSTS
106 ));
107 $this->assertInstanceOf('ilBcryptPasswordEncoder', $encoder);
108 $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
109 $this->assertFalse($encoder->isSecurityFlawIgnored());
110 $encoder->setClientSalt(self::CLIENT_SALT);
111 return $encoder;
112 }

◆ testInstanceCanBeCreatedAndInitializedWithClientSalt()

ilBcryptPasswordEncoderTest::testInstanceCanBeCreatedAndInitializedWithClientSalt ( )

Definition at line 219 of file ilBcryptPasswordEncoderTest.php.

220 {
222
223 $this->getTestDirectory()->chmod(0777);
224 vfsStream::newFile(ilBcryptPasswordEncoder::SALT_STORAGE_FILENAME)->withContent(self::CLIENT_SALT)->at($this->getTestDirectory());
225
226 $encoder = new ilBcryptPasswordEncoder();
227 $this->assertEquals(self::CLIENT_SALT, $encoder->getClientSalt());
228 }

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

+ Here is the call graph for this function:

◆ testNameShouldBeBcrypt()

ilBcryptPasswordEncoderTest::testNameShouldBeBcrypt ( )

Definition at line 312 of file ilBcryptPasswordEncoderTest.php.

313 {
314 $encoder = new ilBcryptPasswordEncoder();
315 $this->assertEquals('bcrypt', $encoder->getName());
316 }

◆ testPasswordShouldBeCorrectlyEncodedAndVerified()

ilBcryptPasswordEncoderTest::testPasswordShouldBeCorrectlyEncodedAndVerified ( ilBcryptPasswordEncoder  $encoder)

@depends testInstanceCanBeCreated

Definition at line 166 of file ilBcryptPasswordEncoderTest.php.

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

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

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: