fix: Use vectorized search for username-password validation. (#2152)

### Summary

Optimizes account validation:

```cs
| Method              | Mean       | Error     | StdDev    |
|-------------------- |-----------:|----------:|----------:|
| ForLoopUsernameSafe | 158.536 ns | 1.6247 ns | 1.5198 ns |
| SVUsernameSafe      |   2.859 ns | 0.0581 ns | 0.0796 ns |
```
This commit is contained in:
Kamron Batman 2025-04-11 22:40:06 -07:00 • committed by GitHub
parent fdf8c5cf23
commit 0ce2a62a76
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 105 additions and 87 deletions

View file

@ -0,0 +1,35 @@
using Server.Misc;
using Xunit;
namespace Server.Tests.Accounting;
public class AccountHandlerTests
{
[Theory]
[InlineData("", false)] // Empty username
[InlineData(" ", false)] // Single space
[InlineData(".", false)] // Single period
[InlineData("Invalid<Char", false)] // Contains forbidden character
[InlineData("EndsWithSpace ", false)] // Ends with space
[InlineData("EndsWithPeriod.", false)] // Ends with period
[InlineData(" StartsWithSpace", false)] // Starts with space
[InlineData("ValidUser123", true)] // Standard Username
[InlineData("Valid.User", true)] // Valid with period
[InlineData("ValidUser!@#", true)] // Contains valid special characters
public void IsValidUsername_ValidatesCorrectly(string username, bool expected)
{
Assert.Equal(expected, AccountHandler.IsValidUsername(username));
}
[Theory]
[InlineData("", false)] // Empty password
[InlineData("Invalid\x01Char", false)] // Contains invalid ASCII character
[InlineData("ValidPass123!", true)] // Standard Password
[InlineData(" ", true)] // Single space
[InlineData("ValidPass!@#", true)] // Valid special characters
[InlineData("ValidPassWithLength1234567890", true)] // Long valid password
public void IsValidPassword_ValidatesCorrectly(string password, bool expected)
{
Assert.Equal(expected, AccountHandler.IsValidPassword(password));
}
}

View file

@ -17,30 +17,30 @@ public class PasswordProtectionTest
[InlineData(typeof(HashAlgorithmPasswordProtection), "SHA2")]
public void TestValidates(Type protectionType, string algorithmType)
{
IPasswordProtection passwordProtection;
if (protectionType == typeof(HashAlgorithmPasswordProtection))
IPasswordProtection passwordProtection;
if (protectionType == typeof(HashAlgorithmPasswordProtection))
{
passwordProtection = algorithmType switch
{
passwordProtection = algorithmType switch
{
"SHA1" => HashAlgorithmPasswordProtection.SHA1Instance,
"SHA2" => HashAlgorithmPasswordProtection.SHA2Instance,
_ => HashAlgorithmPasswordProtection.MD5Instance,
};
}
else
{
passwordProtection = Activator.CreateInstance(protectionType) as IPasswordProtection;
}
if (passwordProtection == null)
{
Assert.Fail($"{protectionType.Name} is not an IPasswordProtection.");
}
var encryptedPassword = passwordProtection.EncryptPassword(plainPassword);
Assert.True(passwordProtection.ValidatePassword(encryptedPassword, plainPassword));
"SHA1" => HashAlgorithmPasswordProtection.SHA1Instance,
"SHA2" => HashAlgorithmPasswordProtection.SHA2Instance,
_ => HashAlgorithmPasswordProtection.MD5Instance,
};
}
else
{
passwordProtection = Activator.CreateInstance(protectionType) as IPasswordProtection;
}
if (passwordProtection == null)
{
Assert.Fail($"{protectionType.Name} is not an IPasswordProtection.");
}
var encryptedPassword = passwordProtection.EncryptPassword(plainPassword);
Assert.True(passwordProtection.ValidatePassword(encryptedPassword, plainPassword));
}
[Theory]
[InlineData(typeof(Argon2PasswordProtection), null)]
@ -50,28 +50,28 @@ public class PasswordProtectionTest
[InlineData(typeof(HashAlgorithmPasswordProtection), "SHA2")]
public void TestPasswordDoesNotValidate(Type protectionType, string algorithmType)
{
IPasswordProtection passwordProtection;
if (protectionType == typeof(HashAlgorithmPasswordProtection))
IPasswordProtection passwordProtection;
if (protectionType == typeof(HashAlgorithmPasswordProtection))
{
passwordProtection = algorithmType switch
{
passwordProtection = algorithmType switch
{
"SHA1" => HashAlgorithmPasswordProtection.SHA1Instance,
"SHA2" => HashAlgorithmPasswordProtection.SHA2Instance,
_ => HashAlgorithmPasswordProtection.MD5Instance,
};
}
else
{
passwordProtection = Activator.CreateInstance(protectionType) as IPasswordProtection;
}
if (passwordProtection == null)
{
Assert.Fail($"{protectionType.Name} is not an IPasswordProtection.");
}
var encryptedPassword = passwordProtection.EncryptPassword(plainPassword);
Assert.False(passwordProtection.ValidatePassword(encryptedPassword, "Not the same password"));
"SHA1" => HashAlgorithmPasswordProtection.SHA1Instance,
"SHA2" => HashAlgorithmPasswordProtection.SHA2Instance,
_ => HashAlgorithmPasswordProtection.MD5Instance,
};
}
}
else
{
passwordProtection = Activator.CreateInstance(protectionType) as IPasswordProtection;
}
if (passwordProtection == null)
{
Assert.Fail($"{protectionType.Name} is not an IPasswordProtection.");
}
var encryptedPassword = passwordProtection.EncryptPassword(plainPassword);
Assert.False(passwordProtection.ValidatePassword(encryptedPassword, "Not the same password"));
}
}