ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
43  $this->markTestSkipped('Requires PHP >= 5.5.0');
44  }
45  }
46 
50  public function costsProvider()
51  {
52  $data = array();
53  for($i = 4; $i <= 31; $i++)
54  {
55  $data[] = array($i);
56  }
57  return $data;
58  }
59 
63  public function testInstanceCanBeCreated()
64  {
66 
67  $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
68  $this->assertTrue((int)$default_costs_encoder->getCosts() > 4 && (int)$default_costs_encoder->getCosts() < 32);
69 
70  $encoder = new ilBcryptPhpPasswordEncoder(array(
71  'cost' => self::VALID_COSTS
72  ));
73  $this->assertInstanceOf('ilBcryptPhpPasswordEncoder', $encoder);
74  $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
75  return $encoder;
76  }
77 
82  {
83  $encoder->setCosts(4);
84  $this->assertEquals(4, $encoder->getCosts());
85  }
86 
92  {
93  $this->assertException(ilPasswordException::class);
94  $encoder->setCosts(32);
95  }
96 
102  {
103  $this->assertException(ilPasswordException::class);
104  $encoder->setCosts(3);
105  }
106 
111  public function testCostsCanBeSetInRange($costs, ilBcryptPhpPasswordEncoder $encoder)
112  {
113  $encoder->setCosts($costs);
114  }
115 
120  {
121  $encoder->setCosts(self::VALID_COSTS);
122  $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
123  $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
124  $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
125  return $encoder;
126  }
127 
133  {
134  $this->assertException(ilPasswordException::class);
135  $encoder->setCosts(self::VALID_COSTS);
136  $encoder->encodePassword(str_repeat('a', 5000), '');
137  }
138 
143  {
144  $encoder->setCosts(self::VALID_COSTS);
145  $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
146  }
147 
152  {
153  $this->assertEquals('bcryptphp', $encoder->getName());
154  }
155 
160  {
161  $costs_default = $encoder->benchmarkCost();
162  $costs_target = $encoder->benchmarkCost(0.5);
163 
164  $this->assertTrue($costs_default > 4 && $costs_default < 32);
165  $this->assertTrue($costs_target > 4 && $costs_target < 32);
166  $this->assertInternalType('int', $costs_default);
167  $this->assertInternalType('int', $costs_target);
168  $this->assertNotEquals($costs_default, $costs_target);
169  }
170 
175  {
176  $this->assertFalse($encoder->requiresSalt());
177  }
178 
183  {
184  $raw = self::PASSWORD;
185 
186  $encoder->setCosts(8);
187  $encoded = $encoder->encodePassword($raw, '');
188  $encoder->setCosts(8);
189  $this->assertFalse($encoder->requiresReencoding($encoded));
190 
191  $encoder->setCosts(9);
192  $this->assertTrue($encoder->requiresReencoding($encoded));
193  }
194 }
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} ...
Create styles array
The data for the language used.
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated ilPasswordException
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated ilPasswordException
encodePassword($raw, $salt)
{Encodes the raw password.The password to encode The salt string The encoded password} ...
assertException($exception_class)
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated