Skip to content

Commit 6acabf7

Browse files
alexandre-dauboisAlexandre Daubois
authored andcommitted
[ExpressionLanguage][PasswordHasher][Stopwatch] Improve overall coverage
1 parent 2ecc809 commit 6acabf7

File tree

4 files changed

+69
-0
lines changed

4 files changed

+69
-0
lines changed

Tests/Hasher/MessageDigestPasswordHasherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ public function testCheckPasswordLength()
6060

6161
$this->assertFalse($hasher->verify('encoded', str_repeat('a', 5000), 'salt'));
6262
}
63+
64+
public function testUsingBracketInSaltThrows()
65+
{
66+
$hasher = new MessageDigestPasswordHasher('sha256', false, 1);
67+
68+
$this->expectException(\LogicException::class);
69+
$hasher->hash('password', '{');
70+
}
6371
}

Tests/Hasher/NativePasswordHasherTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\PasswordHasher\Tests\Hasher;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
1516
use Symfony\Component\PasswordHasher\Hasher\NativePasswordHasher;
1617

1718
/**
@@ -119,4 +120,40 @@ public function testNeedsRehash()
119120
$hasher = new NativePasswordHasher(5, 11000, 5);
120121
$this->assertTrue($hasher->needsRehash($hash));
121122
}
123+
124+
public function testLowOpsLimitsThrows()
125+
{
126+
$this->expectException(\InvalidArgumentException::class);
127+
$this->expectExceptionMessageMatches('/\$opsLimit must be (\d+) or greater\./');
128+
129+
new NativePasswordHasher(2);
130+
}
131+
132+
public function testLowMemLimitThrows()
133+
{
134+
$this->expectException(\InvalidArgumentException::class);
135+
$this->expectExceptionMessageMatches('/\$memLimit must be (\d+)(.?) or greater\./');
136+
137+
new NativePasswordHasher(3, 9999);
138+
}
139+
140+
/**
141+
* @testWith [1]
142+
* [40]
143+
*/
144+
public function testInvalidCostThrows(int $cost)
145+
{
146+
$this->expectException(\InvalidArgumentException::class);
147+
$this->expectExceptionMessageMatches('/\$cost must be in the range of (\d+)-(\d+)\./');
148+
149+
new NativePasswordHasher(4, 11000, $cost);
150+
}
151+
152+
public function testHashTooLongPasswordThrows()
153+
{
154+
$this->expectException(InvalidPasswordException::class);
155+
156+
$hasher = new NativePasswordHasher(4, 11000, 4);
157+
$hasher->hash(str_repeat('a', 5000));
158+
}
122159
}

Tests/Hasher/PasswordHasherFactoryTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,22 @@ public function testMigrateFrom()
175175
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
176176
}
177177

178+
public function testMissingClass()
179+
{
180+
$this->expectException(\InvalidArgumentException::class);
181+
$this->expectExceptionMessage('"class" must be set in {"arguments":[]}');
182+
183+
(new PasswordHasherFactory([SomeUser::class => ['arguments' => []]]))->getPasswordHasher(SomeUser::class);
184+
}
185+
186+
public function testMissingArguments()
187+
{
188+
$this->expectException(\InvalidArgumentException::class);
189+
$this->expectExceptionMessage('"arguments" must be set in {"class":"stdClass"}');
190+
191+
(new PasswordHasherFactory([SomeUser::class => ['class' => \stdClass::class]]))->getPasswordHasher(SomeUser::class);
192+
}
193+
178194
public function testDefaultMigratingHashers()
179195
{
180196
$this->assertInstanceOf(

Tests/Hasher/PlaintextPasswordHasherTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ public function testCheckPasswordLength()
5353

5454
$this->assertFalse($hasher->verify('encoded', str_repeat('a', 5000), 'salt'));
5555
}
56+
57+
public function testUsingBracketInSaltThrows()
58+
{
59+
$hasher = new PlaintextPasswordHasher();
60+
61+
$this->expectException(\LogicException::class);
62+
$hasher->hash('password', '{');
63+
}
5664
}

0 commit comments

Comments
 (0)