ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
ilBcryptPhpPasswordEncoderTest.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/Password/classes/encoders/class.ilBcryptPhpPasswordEncoder.php';
5 require_once 'Services/Password/test/ilPasswordBaseTest.php';
6 
13 {
17  const VALID_COSTS = '08';
18 
22  const PASSWORD = 'password';
23 
27  const WRONG_PASSWORD = 'wrong_password';
28 
32  protected function setUp()
33  {
34  }
35 
39  private function skipIfPhpVersionIsNotSupported()
40  {
41  if (version_compare(phpversion(), '5.5.0', '<')) {
42  $this->markTestSkipped('Requires PHP >= 5.5.0');
43  }
44  }
45 
49  public function costsProvider()
50  {
51  $data = array();
52  for ($i = 4; $i <= 31; $i++) {
53  $data[] = array($i);
54  }
55  return $data;
56  }
57 
61  public function testInstanceCanBeCreated()
62  {
64 
65  $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
66  $this->assertTrue((int) $default_costs_encoder->getCosts() > 4 && (int) $default_costs_encoder->getCosts() < 32);
67 
68  $encoder = new ilBcryptPhpPasswordEncoder(array(
69  'cost' => self::VALID_COSTS
70  ));
71  $this->assertInstanceOf('ilBcryptPhpPasswordEncoder', $encoder);
72  $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
73  return $encoder;
74  }
75 
80  {
81  $encoder->setCosts(4);
82  $this->assertEquals(4, $encoder->getCosts());
83  }
84 
90  {
91  $this->assertException(ilPasswordException::class);
92  $encoder->setCosts(32);
93  }
94 
100  {
101  $this->assertException(ilPasswordException::class);
102  $encoder->setCosts(3);
103  }
104 
109  public function testCostsCanBeSetInRange($costs, ilBcryptPhpPasswordEncoder $encoder)
110  {
111  $encoder->setCosts($costs);
112  }
113 
118  {
119  $encoder->setCosts(self::VALID_COSTS);
120  $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
121  $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
122  $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
123  return $encoder;
124  }
125 
131  {
132  $this->assertException(ilPasswordException::class);
133  $encoder->setCosts(self::VALID_COSTS);
134  $encoder->encodePassword(str_repeat('a', 5000), '');
135  }
136 
141  {
142  $encoder->setCosts(self::VALID_COSTS);
143  $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
144  }
145 
150  {
151  $this->assertEquals('bcryptphp', $encoder->getName());
152  }
153 
158  {
159  $costs_default = $encoder->benchmarkCost();
160  $costs_target = $encoder->benchmarkCost(0.5);
161 
162  $this->assertTrue($costs_default > 4 && $costs_default < 32);
163  $this->assertTrue($costs_target > 4 && $costs_target < 32);
164  $this->assertInternalType('int', $costs_default);
165  $this->assertInternalType('int', $costs_target);
166  $this->assertNotEquals($costs_default, $costs_target);
167  }
168 
173  {
174  $this->assertFalse($encoder->requiresSalt());
175  }
176 
181  {
182  $raw = self::PASSWORD;
183 
184  $encoder->setCosts(8);
185  $encoded = $encoder->encodePassword($raw, '');
186  $encoder->setCosts(8);
187  $this->assertFalse($encoder->requiresReencoding($encoded));
188 
189  $encoder->setCosts(9);
190  $this->assertTrue($encoder->requiresReencoding($encoded));
191  }
192 }
testCostsCanBeDeterminedDynamically(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCannotBeSetAboveRange(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated ilPasswordException
testEncoderDoesNotRelyOnSalts(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
requiresSalt()
{Returns whether or not the encoder requires a salt.boolean}
testCostsCanBeSetInRange($costs, ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated costsProvider
isPasswordValid($encoded, $raw, $salt)
{Checks a raw password against an encoded password.The raw password has to be injected into the encod...
requiresReencoding($encoded)
{Returns whether or not the a encoded password needs to be re-encoded.string boolean} ...
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated ilPasswordException
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated ilPasswordException
$i
Definition: disco.tpl.php:19
encodePassword($raw, $salt)
{Encodes the raw password.The password to encode The salt string The encoded password} ...
assertException($exception_class)
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
$data
Definition: bench.php:6
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated