Выполнение работы на 4.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using Messenger.Api.Services;
|
||||
|
||||
namespace Messenger.Api.Tests;
|
||||
|
||||
public class PasswordHasherTests
|
||||
{
|
||||
private readonly Argon2PasswordHasher _hasher = new(new PasswordHashingSettings(4, TimeSpan.FromSeconds(30)));
|
||||
|
||||
[Fact]
|
||||
public async Task Hash_IsArgon2idPhcString_WithoutPlaintext()
|
||||
{
|
||||
var hash = await _hasher.HashAsync("correct horse battery staple", CancellationToken.None);
|
||||
|
||||
Assert.StartsWith("$argon2id$", hash);
|
||||
Assert.DoesNotContain("correct horse", hash);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hash_UsesRandomSalt()
|
||||
{
|
||||
var a = await _hasher.HashAsync("same-password-123", CancellationToken.None);
|
||||
var b = await _hasher.HashAsync("same-password-123", CancellationToken.None);
|
||||
|
||||
Assert.NotEqual(a, b);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("correct horse battery staple")]
|
||||
[InlineData("пароль-с-юникодом-и:двоеточием-12")]
|
||||
public async Task Verify_AcceptsCorrect_RejectsWrong(string password)
|
||||
{
|
||||
var hash = await _hasher.HashAsync(password, CancellationToken.None);
|
||||
|
||||
Assert.True(await _hasher.VerifyAsync(hash, password, CancellationToken.None));
|
||||
Assert.False(await _hasher.VerifyAsync(hash, password + "x", CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Verify_DoesNotTruncateLongPasswords()
|
||||
{
|
||||
// Это ограничение bcrypt (72 байта); Argon2id его не имеет. Пароли отличаются только хвостом.
|
||||
var prefix = new string('я', 60); // 120 байт в UTF-8
|
||||
var hash = await _hasher.HashAsync(prefix + "A", CancellationToken.None);
|
||||
|
||||
Assert.False(await _hasher.VerifyAsync(hash, prefix + "B", CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Hash_WhenAllSlotsAreBusy_FailsWithBusyException()
|
||||
{
|
||||
// Один слот и почти нулевое ожидание: параллельные вызовы не копят память, лишние получают исключение (→ 503).
|
||||
var hasher = new Argon2PasswordHasher(new PasswordHashingSettings(1, TimeSpan.FromMilliseconds(20)));
|
||||
|
||||
var attempts = await Task.WhenAll(Enumerable.Range(0, 6).Select(async _ =>
|
||||
{
|
||||
try { await hasher.HashAsync("some-long-enough-password", CancellationToken.None); return true; }
|
||||
catch (PasswordHasherBusyException) { return false; }
|
||||
}));
|
||||
|
||||
Assert.Contains(true, attempts); // кто-то получил слот
|
||||
Assert.Contains(false, attempts); // остальные получили контролируемый отказ
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Verify_UnknownUser_ReturnsFalse()
|
||||
{
|
||||
Assert.False(await _hasher.VerifyAsync(null, "any-password-123", CancellationToken.None));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user