ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
ilBcryptPhpPasswordEncoderTest.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
24 
26 {
27  private const string VALID_COSTS = '08';
28  private const string PASSWORD = 'password';
29  private const string WRONG_PASSWORD = 'wrong_password';
30 
34  public static function costsProvider(): array
35  {
36  $data = [];
37  for ($i = 4; $i <= 31; ++$i) {
38  $data[sprintf('Costs: %s', $i)] = [(string) $i];
39  }
40 
41  return $data;
42  }
43 
45  {
46  $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
47  $this->assertTrue((int) $default_costs_encoder->getCosts() > 4 && (int) $default_costs_encoder->getCosts() < 32);
48 
49  $encoder = new ilBcryptPhpPasswordEncoder([
50  'cost' => self::VALID_COSTS
51  ]);
52  $this->assertInstanceOf(ilBcryptPhpPasswordEncoder::class, $encoder);
53  $this->assertSame(self::VALID_COSTS, $encoder->getCosts());
54 
55  return $encoder;
56  }
57 
58  #[Depends('testInstanceCanBeCreated')]
60  {
61  $expected = '04';
62 
63  $encoder->setCosts($expected);
64  $this->assertSame($expected, $encoder->getCosts());
65  }
66 
67  #[Depends('testInstanceCanBeCreated')]
69  {
70  $this->expectException(ilPasswordException::class);
71  $encoder->setCosts('32');
72  }
73 
74  #[Depends('testInstanceCanBeCreated')]
76  {
77  $this->expectException(ilPasswordException::class);
78  $encoder->setCosts('3');
79  }
80 
81  #[Depends('testInstanceCanBeCreated')]
82  #[DataProvider('costsProvider')]
83  #[DoesNotPerformAssertions]
84  public function testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder): void
85  {
86  $encoder->setCosts($costs);
87  }
88 
89  #[Depends('testInstanceCanBeCreated')]
93  $encoder->setCosts(self::VALID_COSTS);
94  $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
95  $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
96  $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
97 
98  return $encoder;
99  }
100 
101  #[Depends('testInstanceCanBeCreated')]
104  ): void {
105  $this->expectException(ilPasswordException::class);
106  $encoder->setCosts(self::VALID_COSTS);
107  $encoder->encodePassword(str_repeat('a', 5000), '');
108  }
109 
110  #[Depends('testInstanceCanBeCreated')]
113  ): void {
114  $encoder->setCosts(self::VALID_COSTS);
115  $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
116  }
117 
118  #[Depends('testInstanceCanBeCreated')]
120  {
121  $this->assertSame('bcryptphp', $encoder->getName());
122  }
123 
124  #[Depends('testInstanceCanBeCreated')]
126  {
127  $costs_default = $encoder->benchmarkCost();
128  $costs_target = $encoder->benchmarkCost(0.5);
129 
130  $this->assertTrue($costs_default > 4 && $costs_default < 32);
131  $this->assertTrue($costs_target > 4 && $costs_target < 32);
132  $this->assertIsInt($costs_default);
133  $this->assertIsInt($costs_target);
134  $this->assertNotEquals($costs_default, $costs_target);
135  }
136 
137  #[Depends('testInstanceCanBeCreated')]
139  {
140  $this->assertFalse($encoder->requiresSalt());
141  }
142 
143  #[Depends('testInstanceCanBeCreated')]
145  {
146  $raw = self::PASSWORD;
147 
148  $encoder->setCosts('8');
149  $encoded = $encoder->encodePassword($raw, '');
150  $encoder->setCosts('8');
151  $this->assertFalse($encoder->requiresReencoding($encoded));
152 
153  $encoder->setCosts('9');
154  $this->assertTrue($encoder->requiresReencoding($encoded));
155  }
156 }
testCostsCanBeDeterminedDynamically(ilBcryptPhpPasswordEncoder $encoder)
testCostsCannotBeSetAboveRange(ilBcryptPhpPasswordEncoder $encoder)
testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder)
testEncoderDoesNotRelyOnSalts(ilBcryptPhpPasswordEncoder $encoder)
getName()
Returns a unique name/id of the concrete password encoder.
requiresSalt()
Returns whether the encoder requires a salt.
encodePassword(string $raw, string $salt)
Encodes the raw password.
requiresReencoding(string $encoded)
Returns whether the encoded password needs to be re-encoded.
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
isPasswordValid(string $encoded, string $raw, string $salt)
Checks a raw password against an encoded password.
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)