< Summary

Line coverage
100%
Covered lines: 60
Uncovered lines: 0
Coverable lines: 60
Total lines: 113
Line coverage: 100%
Branch coverage
100%
Covered branches: 31
Total branches: 31
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: SpecialCharacterRegex()100%11100%
File 2: Evaluate(...)100%3131100%

File(s)

/home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/obj/Debug/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs

File '/home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/obj/Debug/net9.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs' does not exist (any more).

/home/runner/work/LOCKnet/LOCKnet/src/LOCKnet.Core/Services/PasswordStrengthService.cs

#LineLine coverage
 1using System.Text.RegularExpressions;
 2
 3namespace LOCKnet.Core.Services;
 4
 5/// <summary>
 6/// Standardimplementierung fuer <see cref="IPasswordStrengthService"/>.
 7/// </summary>
 8public sealed partial class PasswordStrengthService : IPasswordStrengthService
 9{
 10  private const string LabelVeryWeak = "Sehr schwach";
 11  private const string LabelWeak = "Schwach";
 12  private const string LabelMedium = "Mittel";
 13  private const string LabelStrong = "Stark";
 14  private const string LabelVeryStrong = "Sehr stark";
 15
 16  /// <inheritdoc/>
 17  public PasswordStrength Evaluate(string password)
 1718  {
 1719    if (string.IsNullOrEmpty(password))
 720    {
 721      return new PasswordStrength(0, LabelVeryWeak, "#FF4757");
 22    }
 23
 1024    var hasUpper = password.Any(char.IsUpper);
 1025    var hasLower = password.Any(char.IsLower);
 1026    var hasDigit = password.Any(char.IsDigit);
 1027    var hasSpecial = SpecialCharacterRegex().IsMatch(password);
 5028    var variety = new[] { hasUpper, hasLower, hasDigit, hasSpecial }.Count(v => v);
 29
 1030    var points = 0;
 1031    if (password.Length >= 8)
 932    {
 933      points++;
 934    }
 35
 1036    if (password.Length >= 12)
 437    {
 438      points++;
 439    }
 40
 1041    if (hasUpper)
 542    {
 543      points++;
 544    }
 45
 1046    if (hasLower)
 1047    {
 1048      points++;
 1049    }
 50
 1051    if (hasDigit)
 752    {
 753      points++;
 754    }
 55
 1056    if (hasSpecial)
 657    {
 658      points++;
 659    }
 60
 1061    if (variety >= 3)
 562    {
 563      points++;
 564    }
 65
 1066    if (variety == 4)
 467    {
 468      points++;
 469    }
 70
 1071    var score = points switch
 1072    {
 173      <= 1 => 0,
 374      <= 3 => 1,
 275      <= 5 => 2,
 176      <= 7 => 3,
 377      _ => 4
 1078    };
 79
 1080    return score switch
 1081    {
 182      0 => new PasswordStrength(0, LabelVeryWeak, "#FF4757"),
 383      1 => new PasswordStrength(1, LabelWeak, "#FF6B35"),
 284      2 => new PasswordStrength(2, LabelMedium, "#FFB347"),
 185      3 => new PasswordStrength(3, LabelStrong, "#2ED573B3"),
 386      _ => new PasswordStrength(4, LabelVeryStrong, "#2ED573")
 1087    };
 1788  }
 89
 90  [GeneratedRegex("[^a-zA-Z0-9]")]
 91  private static partial Regex SpecialCharacterRegex();
 92}