ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
ilBcryptPhpPasswordEncoderTest.php
Go to the documentation of this file.
1 <?php declare(strict_types=1);
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 {
15  const VALID_COSTS = '08';
16 
18  const PASSWORD = 'password';
19 
21  const WRONG_PASSWORD = 'wrong_password';
22 
26  private function skipIfPhpVersionIsNotSupported() : void
27  {
28  if (version_compare(phpversion(), '5.5.0', '<')) {
29  $this->markTestSkipped('Requires PHP >= 5.5.0');
30  }
31  }
32 
36  public function costsProvider() : array
37  {
38  $data = [];
39  for ($i = 4; $i <= 31; $i++) {
40  $data[sprintf("Costs: %s", (string) $i)] = [(string) $i];
41  }
42 
43  return $data;
44  }
45 
51  {
53 
54  $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
55  $this->assertTrue((int) $default_costs_encoder->getCosts() > 4 && (int) $default_costs_encoder->getCosts() < 32);
56 
57  $encoder = new ilBcryptPhpPasswordEncoder([
58  'cost' => self::VALID_COSTS
59  ]);
60  $this->assertInstanceOf('ilBcryptPhpPasswordEncoder', $encoder);
61  $this->assertEquals(self::VALID_COSTS, $encoder->getCosts());
62  return $encoder;
63  }
64 
71  {
72  $expected = '04';
73 
74  $encoder->setCosts($expected);
75  $this->assertEquals($expected, $encoder->getCosts());
76  }
77 
84  {
85  $this->expectException(ilPasswordException::class);
86  $encoder->setCosts('32');
87  }
88 
95  {
96  $this->expectException(ilPasswordException::class);
97  $encoder->setCosts('3');
98  }
99 
108  public function testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder) : void
109  {
110  $encoder->setCosts($costs);
111  }
112 
122  $encoder->setCosts(self::VALID_COSTS);
123  $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
124  $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
125  $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
126  return $encoder;
127  }
128 
136  ) : void {
137  $this->expectException(ilPasswordException::class);
138  $encoder->setCosts(self::VALID_COSTS);
139  $encoder->encodePassword(str_repeat('a', 5000), '');
140  }
141 
149  ) : void {
150  $encoder->setCosts(self::VALID_COSTS);
151  $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
152  }
153 
158  public function testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder) : void
159  {
160  $this->assertEquals('bcryptphp', $encoder->getName());
161  }
162 
169  {
170  $costs_default = $encoder->benchmarkCost();
171  $costs_target = $encoder->benchmarkCost(0.5);
172 
173  $this->assertTrue($costs_default > 4 && $costs_default < 32);
174  $this->assertTrue($costs_target > 4 && $costs_target < 32);
175  $this->assertIsInt($costs_default);
176  $this->assertIsInt($costs_target);
177  $this->assertNotEquals($costs_default, $costs_target);
178  }
179 
185  {
186  $this->assertFalse($encoder->requiresSalt());
187  }
188 
195  {
196  $raw = self::PASSWORD;
197 
198  $encoder->setCosts('8');
199  $encoded = $encoder->encodePassword($raw, '');
200  $encoder->setCosts('8');
201  $this->assertFalse($encoder->requiresReencoding($encoded));
202 
203  $encoder->setCosts('9');
204  $this->assertTrue($encoder->requiresReencoding($encoded));
205  }
206 }
testCostsCanBeDeterminedDynamically(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCannotBeSetAboveRange(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated costsProvider
testEncoderDoesNotRelyOnSalts(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
$data
Definition: storeScorm.php:23
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
isPasswordValid(string $encoded, string $raw, string $salt)
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated
$i
Definition: metadata.php:24
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)
testInstanceCanBeCreated