Skip to content

Commit 6765a43

Browse files
minor symfony#52421 [PasswordHasher] [Tests] Do not invoke methods with additional arguments in tests (OskarStark)
This PR was merged into the 5.4 branch. Discussion ---------- [PasswordHasher] [Tests] Do not invoke methods with additional arguments in tests | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | --- | License | MIT Extracted from symfony#52402 I think its worth to merge this in 5.4 Commits ------- fdaefc4 [PasswordHasher][Tests] Do not invoke methods with additional arguments in tests
2 parents fc22954 + fdaefc4 commit 6765a43

File tree

3 files changed

+48
-48
lines changed

3 files changed

+48
-48
lines changed

src/Symfony/Component/PasswordHasher/Tests/Hasher/NativePasswordHasherTest.php

+16-16
Original file line numberDiff line numberDiff line change
@@ -51,25 +51,25 @@ public function testValidation()
5151
{
5252
$hasher = new NativePasswordHasher();
5353
$result = $hasher->hash('password', null);
54-
$this->assertTrue($hasher->verify($result, 'password', null));
55-
$this->assertFalse($hasher->verify($result, 'anotherPassword', null));
56-
$this->assertFalse($hasher->verify($result, '', null));
54+
$this->assertTrue($hasher->verify($result, 'password'));
55+
$this->assertFalse($hasher->verify($result, 'anotherPassword'));
56+
$this->assertFalse($hasher->verify($result, ''));
5757
}
5858

5959
public function testNonArgonValidation()
6060
{
6161
$hasher = new NativePasswordHasher();
62-
$this->assertTrue($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'password', null));
63-
$this->assertFalse($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'anotherPassword', null));
64-
$this->assertTrue($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'password', null));
65-
$this->assertFalse($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'anotherPassword', null));
62+
$this->assertTrue($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'password'));
63+
$this->assertFalse($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'anotherPassword'));
64+
$this->assertTrue($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'password'));
65+
$this->assertFalse($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'anotherPassword'));
6666
}
6767

6868
public function testConfiguredAlgorithm()
6969
{
7070
$hasher = new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT);
71-
$result = $hasher->hash('password', null);
72-
$this->assertTrue($hasher->verify($result, 'password', null));
71+
$result = $hasher->hash('password');
72+
$this->assertTrue($hasher->verify($result, 'password'));
7373
$this->assertStringStartsWith('$2', $result);
7474
}
7575

@@ -84,8 +84,8 @@ public function testDefaultAlgorithm()
8484
public function testConfiguredAlgorithmWithLegacyConstValue()
8585
{
8686
$hasher = new NativePasswordHasher(null, null, null, '1');
87-
$result = $hasher->hash('password', null);
88-
$this->assertTrue($hasher->verify($result, 'password', null));
87+
$result = $hasher->hash('password');
88+
$this->assertTrue($hasher->verify($result, 'password'));
8989
$this->assertStringStartsWith('$2', $result);
9090
}
9191

@@ -94,17 +94,17 @@ public function testBcryptWithLongPassword()
9494
$hasher = new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT);
9595
$plainPassword = str_repeat('a', 100);
9696

97-
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword, 'salt'));
98-
$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword, 'salt'));
97+
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
98+
$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword));
9999
}
100100

101101
public function testBcryptWithNulByte()
102102
{
103103
$hasher = new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT);
104104
$plainPassword = "a\0b";
105105

106-
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword, 'salt'));
107-
$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword, 'salt'));
106+
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
107+
$this->assertTrue($hasher->verify($hasher->hash($plainPassword), $plainPassword));
108108
}
109109

110110
public function testNeedsRehash()
@@ -113,7 +113,7 @@ public function testNeedsRehash()
113113

114114
$this->assertTrue($hasher->needsRehash('dummyhash'));
115115

116-
$hash = $hasher->hash('foo', 'salt');
116+
$hash = $hasher->hash('foo');
117117
$this->assertFalse($hasher->needsRehash($hash));
118118

119119
$hasher = new NativePasswordHasher(5, 11000, 5);

src/Symfony/Component/PasswordHasher/Tests/Hasher/PasswordHasherFactoryTest.php

+10-10
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public function testGetNamedHasherForHasherAware()
110110
'hasher_name' => new MessageDigestPasswordHasher('sha1'),
111111
]);
112112

113-
$hasher = $factory->getPasswordHasher(new HasherAwareUser('user', 'pass'));
113+
$hasher = $factory->getPasswordHasher(new HasherAwareUser());
114114
$expectedHasher = new MessageDigestPasswordHasher('sha1');
115115
$this->assertEquals($expectedHasher->hash('foo', ''), $hasher->hash('foo', ''));
116116
}
@@ -122,7 +122,7 @@ public function testGetNullNamedHasherForHasherAware()
122122
'hasher_name' => new MessageDigestPasswordHasher('sha256'),
123123
]);
124124

125-
$user = new HasherAwareUser('mathilde', 'krogulec');
125+
$user = new HasherAwareUser();
126126
$user->hasherName = null;
127127
$hasher = $factory->getPasswordHasher($user);
128128
$expectedHasher = new MessageDigestPasswordHasher('sha1');
@@ -137,7 +137,7 @@ public function testGetInvalidNamedHasherForHasherAware()
137137
'hasher_name' => new MessageDigestPasswordHasher('sha256'),
138138
]);
139139

140-
$user = new HasherAwareUser('user', 'pass');
140+
$user = new HasherAwareUser();
141141
$user->hasherName = 'invalid_hasher_name';
142142
$factory->getPasswordHasher($user);
143143
}
@@ -168,9 +168,9 @@ public function testMigrateFrom()
168168
$hasher = $factory->getPasswordHasher(SomeUser::class);
169169
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);
170170

171-
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
172-
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
173-
$this->assertTrue($hasher->verify($digest->hash('foo', null), 'foo', null));
171+
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo'), 'foo', null));
172+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo'), 'foo', null));
173+
$this->assertTrue($hasher->verify($digest->hash('foo'), 'foo', null));
174174
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
175175
}
176176

@@ -190,8 +190,8 @@ public function testMigrateFromWithCustomInstance()
190190
$hasher = $factory->getPasswordHasher(SomeUser::class);
191191
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);
192192

193-
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
194-
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
193+
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo'), 'foo', null));
194+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo'), 'foo', null));
195195
$this->assertTrue($hasher->verify($digest->hash('foo', null), 'foo', null));
196196
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
197197
}
@@ -213,8 +213,8 @@ public function testMigrateFromLegacy()
213213
$hasher = $factory->getPasswordHasher(SomeUser::class);
214214
$this->assertInstanceOf(MigratingPasswordHasher::class, $hasher);
215215

216-
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo', null), 'foo', null));
217-
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo', null), 'foo', null));
216+
$this->assertTrue($hasher->verify((new SodiumPasswordHasher())->hash('foo'), 'foo', null));
217+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, null, \PASSWORD_BCRYPT))->hash('foo'), 'foo', null));
218218
$this->assertTrue($hasher->verify($plaintext->encodePassword('foo', null), 'foo', null));
219219
$this->assertStringStartsWith(\SODIUM_CRYPTO_PWHASH_STRPREFIX, $hasher->hash('foo', null));
220220
}

src/Symfony/Component/PasswordHasher/Tests/Hasher/SodiumPasswordHasherTest.php

+22-22
Original file line numberDiff line numberDiff line change
@@ -28,65 +28,65 @@ protected function setUp(): void
2828
public function testValidation()
2929
{
3030
$hasher = new SodiumPasswordHasher();
31-
$result = $hasher->hash('password', null);
32-
$this->assertTrue($hasher->verify($result, 'password', null));
33-
$this->assertFalse($hasher->verify($result, 'anotherPassword', null));
34-
$this->assertFalse($hasher->verify($result, '', null));
31+
$result = $hasher->hash('password');
32+
$this->assertTrue($hasher->verify($result, 'password'));
33+
$this->assertFalse($hasher->verify($result, 'anotherPassword'));
34+
$this->assertFalse($hasher->verify($result, ''));
3535
}
3636

3737
public function testBcryptValidation()
3838
{
3939
$hasher = new SodiumPasswordHasher();
40-
$this->assertTrue($hasher->verify('$2y$04$M8GDODMoGQLQRpkYCdoJh.lbiZPee3SZI32RcYK49XYTolDGwoRMm', 'abc', null));
40+
$this->assertTrue($hasher->verify('$2y$04$M8GDODMoGQLQRpkYCdoJh.lbiZPee3SZI32RcYK49XYTolDGwoRMm', 'abc'));
4141
}
4242

4343
public function testNonArgonValidation()
4444
{
4545
$hasher = new SodiumPasswordHasher();
46-
$this->assertTrue($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'password', null));
47-
$this->assertFalse($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'anotherPassword', null));
48-
$this->assertTrue($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'password', null));
49-
$this->assertFalse($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'anotherPassword', null));
46+
$this->assertTrue($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'password'));
47+
$this->assertFalse($hasher->verify('$5$abcdefgh$ZLdkj8mkc2XVSrPVjskDAgZPGjtj1VGVaa1aUkrMTU/', 'anotherPassword'));
48+
$this->assertTrue($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'password'));
49+
$this->assertFalse($hasher->verify('$6$abcdefgh$yVfUwsw5T.JApa8POvClA1pQ5peiq97DUNyXCZN5IrF.BMSkiaLQ5kvpuEm/VQ1Tvh/KV2TcaWh8qinoW5dhA1', 'anotherPassword'));
5050
}
5151

5252
public function testHashLength()
5353
{
5454
$this->expectException(InvalidPasswordException::class);
5555
$hasher = new SodiumPasswordHasher();
56-
$hasher->hash(str_repeat('a', 4097), 'salt');
56+
$hasher->hash(str_repeat('a', 4097));
5757
}
5858

5959
public function testCheckPasswordLength()
6060
{
6161
$hasher = new SodiumPasswordHasher();
62-
$result = $hasher->hash(str_repeat('a', 4096), null);
63-
$this->assertFalse($hasher->verify($result, str_repeat('a', 4097), null));
64-
$this->assertTrue($hasher->verify($result, str_repeat('a', 4096), null));
62+
$result = $hasher->hash(str_repeat('a', 4096));
63+
$this->assertFalse($hasher->verify($result, str_repeat('a', 4097)));
64+
$this->assertTrue($hasher->verify($result, str_repeat('a', 4096)));
6565
}
6666

6767
public function testBcryptWithLongPassword()
6868
{
69-
$hasher = new SodiumPasswordHasher(null, null, 4);
69+
$hasher = new SodiumPasswordHasher(null, null);
7070
$plainPassword = str_repeat('a', 100);
7171

72-
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword, 'salt'));
73-
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword, 'salt'));
72+
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
73+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword));
7474
}
7575

7676
public function testBcryptWithNulByte()
7777
{
78-
$hasher = new SodiumPasswordHasher(null, null, 4);
78+
$hasher = new SodiumPasswordHasher(null, null);
7979
$plainPassword = "a\0b";
8080

81-
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword, 'salt'));
82-
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword, 'salt'));
81+
$this->assertFalse($hasher->verify(password_hash($plainPassword, \PASSWORD_BCRYPT, ['cost' => 4]), $plainPassword));
82+
$this->assertTrue($hasher->verify((new NativePasswordHasher(null, null, 4, \PASSWORD_BCRYPT))->hash($plainPassword), $plainPassword));
8383
}
8484

8585
public function testUserProvidedSaltIsNotUsed()
8686
{
8787
$hasher = new SodiumPasswordHasher();
88-
$result = $hasher->hash('password', 'salt');
89-
$this->assertTrue($hasher->verify($result, 'password', 'anotherSalt'));
88+
$result = $hasher->hash('password');
89+
$this->assertTrue($hasher->verify($result, 'password'));
9090
}
9191

9292
public function testNeedsRehash()
@@ -95,7 +95,7 @@ public function testNeedsRehash()
9595

9696
$this->assertTrue($hasher->needsRehash('dummyhash'));
9797

98-
$hash = $hasher->hash('foo', 'salt');
98+
$hash = $hasher->hash('foo');
9999
$this->assertFalse($hasher->needsRehash($hash));
100100

101101
$hasher = new SodiumPasswordHasher(5, 11000);

0 commit comments

Comments
 (0)