ILIAS  release_8 Revision v8.24
ilBcryptPhpPasswordEncoderTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
29 private const VALID_COSTS = '08';
30
32 private const PASSWORD = 'password';
33
35 private const WRONG_PASSWORD = 'wrong_password';
36
40 public function costsProvider(): array
41 {
42 $data = [];
43 for ($i = 4; $i <= 31; ++$i) {
44 $data[sprintf('Costs: %s', $i)] = [(string) $i];
45 }
46
47 return $data;
48 }
49
51 {
52 $default_costs_encoder = new ilBcryptPhpPasswordEncoder();
53 $this->assertTrue((int) $default_costs_encoder->getCosts() > 4 && (int) $default_costs_encoder->getCosts() < 32);
54
55 $encoder = new ilBcryptPhpPasswordEncoder([
56 'cost' => self::VALID_COSTS
57 ]);
58 $this->assertInstanceOf(ilBcryptPhpPasswordEncoder::class, $encoder);
59 $this->assertSame(self::VALID_COSTS, $encoder->getCosts());
60
61 return $encoder;
62 }
63
69 {
70 $expected = '04';
71
72 $encoder->setCosts($expected);
73 $this->assertSame($expected, $encoder->getCosts());
74 }
75
81 {
82 $this->expectException(ilPasswordException::class);
83 $encoder->setCosts('32');
84 }
85
91 {
92 $this->expectException(ilPasswordException::class);
93 $encoder->setCosts('3');
94 }
95
102 public function testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder): void
103 {
104 $encoder->setCosts($costs);
105 }
106
114 $encoder->setCosts(self::VALID_COSTS);
115 $encoded_password = $encoder->encodePassword(self::PASSWORD, '');
116 $this->assertTrue($encoder->isPasswordValid($encoded_password, self::PASSWORD, ''));
117 $this->assertFalse($encoder->isPasswordValid($encoded_password, self::WRONG_PASSWORD, ''));
118
119 return $encoder;
120 }
121
128 ): void {
129 $this->expectException(ilPasswordException::class);
130 $encoder->setCosts(self::VALID_COSTS);
131 $encoder->encodePassword(str_repeat('a', 5000), '');
132 }
133
140 ): void {
141 $encoder->setCosts(self::VALID_COSTS);
142 $this->assertFalse($encoder->isPasswordValid('encoded', str_repeat('a', 5000), ''));
143 }
144
149 {
150 $this->assertSame('bcryptphp', $encoder->getName());
151 }
152
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->assertIsInt($costs_default);
165 $this->assertIsInt($costs_target);
166 $this->assertNotEquals($costs_default, $costs_target);
167 }
168
173 {
174 $this->assertFalse($encoder->requiresSalt());
175 }
176
182 {
183 $raw = self::PASSWORD;
184
185 $encoder->setCosts('8');
186 $encoded = $encoder->encodePassword($raw, '');
187 $encoder->setCosts('8');
188 $this->assertFalse($encoder->requiresReencoding($encoded));
189
190 $encoder->setCosts('9');
191 $this->assertTrue($encoder->requiresReencoding($encoded));
192 }
193}
requiresSalt()
Returns whether the encoder requires a salt.
testCostsCannotBeSetBelowRange(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testReencodingIsDetectedWhenNecessary(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCanBeDeterminedDynamically(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testExceptionIsRaisedIfThePasswordExceedsTheSupportedLengthOnEncoding(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testEncoderDoesNotRelyOnSalts(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCannotBeSetAboveRange(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testNameShouldBeBcryptPhp(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testCostsCanBeSetInRange(string $costs, ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated @dataProvider costsProvider @doesNotPerformAssertions
testCostsCanBeRetrievedWhenCostsAreSet(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testPasswordShouldBeCorrectlyEncodedAndVerified(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
testPasswordVerificationShouldFailIfTheRawPasswordExceedsTheSupportedLength(ilBcryptPhpPasswordEncoder $encoder)
@depends testInstanceCanBeCreated
requiresReencoding(string $encoded)
Returns whether the encoded password needs to be re-encoded.
getName()
Returns a unique name/id of the concrete password encoder.
encodePassword(string $raw, string $salt)
Encodes the raw password.
isPasswordValid(string $encoded, string $raw, string $salt)
Checks a raw password against an encoded password.
Class for user password exception handling in ILIAS.
$i
Definition: metadata.php:41