< Summary - Kestrun — Combined Coverage

Information
Class: Kestrun.Utilities.SecureStringUtils
Assembly: Kestrun
File(s): /home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/SecureString.cs
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
89%
Covered lines: 26
Uncovered lines: 3
Coverable lines: 29
Total lines: 94
Line coverage: 89.6%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ToSecureSpan(...)100%6686.36%
ToSecureString(...)100%44100%

File(s)

/home/runner/work/Kestrun/Kestrun/src/CSharp/Kestrun/Utilities/SecureString.cs

#LineLine coverage
 1
 2using System.Runtime.InteropServices;
 3using System.Security;
 4using Serilog;
 5
 6namespace Kestrun.Utilities;
 7
 8
 9/// <summary>
 10/// Provides utility methods for working with SecureString and ReadOnlySpan&lt;char&gt;.
 11/// </summary>
 12public static class SecureStringUtils
 13{
 14    /// <summary>
 15    /// Represents a delegate that handles a ReadOnlySpan&lt;char&gt;.
 16    /// </summary>
 17    public delegate void SpanHandler(ReadOnlySpan<char> span);
 18
 19
 20    /// <summary>
 21    /// Converts a SecureString to a ReadOnlySpan&lt;char&gt; and passes it to the specified handler.
 22    /// The unmanaged memory is zeroed and freed after the handler executes.
 23    /// </summary>
 24    /// <param name="secureString">The SecureString to convert.</param>
 25    /// <param name="handler">The delegate to handle the ReadOnlySpan&lt;char&gt;.</param>
 26    public static unsafe void ToSecureSpan(this SecureString secureString, SpanHandler handler)
 27    {
 428        Log.Debug("Converting SecureString to ReadOnlySpan<char> for handler {Handler}", handler.Method.Name);
 29
 430        ArgumentNullException.ThrowIfNull(secureString);
 431        ArgumentNullException.ThrowIfNull(handler);
 432        if (secureString.Length == 0)
 33        {
 134            throw new ArgumentException("SecureString is empty", nameof(secureString));
 35        }
 36        // Convert SecureString to a ReadOnlySpan<char> using a pointer
 37        // This is safe because SecureString guarantees that the memory is zeroed after use.
 338        var ptr = IntPtr.Zero;
 39        try
 40        {
 41            // Convert SecureString to a pointer
 42            // Marshal.SecureStringToCoTaskMemUnicode returns a pointer to the unmanaged memory
 43            // that contains the characters of the SecureString.
 44            // This memory must be freed after use to avoid memory leaks.
 345            Log.Debug("Marshalling SecureString to unmanaged memory");
 346            ptr = Marshal.SecureStringToCoTaskMemUnicode(secureString);
 347            var span = new ReadOnlySpan<char>((char*)ptr, secureString.Length);
 348            handler(span);
 349            Log.Debug("Handler executed successfully with SecureString span");
 350        }
 051        catch (Exception ex)
 52        {
 053            Log.Error(ex, "Error while converting SecureString to ReadOnlySpan<char>");
 054            throw; // rethrow the exception for further handling
 55        }
 56        finally
 57        {
 58            // Ensure the unmanaged memory is zeroed and freed
 359            Log.Debug("Zeroing and freeing unmanaged memory for SecureString");
 360            if (ptr != IntPtr.Zero)
 61            {
 62                // zero & free
 6063                for (var i = 0; i < secureString.Length; i++)
 64                {
 2765                    Marshal.WriteInt16(ptr, i * 2, 0);
 66                }
 67
 368                Marshal.ZeroFreeCoTaskMemUnicode(ptr);
 69            }
 370        }
 371    }
 72    /// <summary>
 73    /// Converts a <see cref="ReadOnlySpan{Char}"/> to a <see cref="SecureString"/>.
 74    /// </summary>
 75    /// <param name="span">The character span to convert.</param>
 76    /// <returns>A read-only <see cref="SecureString"/> containing the characters from the span.</returns>
 77    /// <exception cref="ArgumentException">Thrown if the span is empty.</exception>
 78    public static SecureString ToSecureString(this ReadOnlySpan<char> span)
 79    {
 580        if (span.Length == 0)
 81        {
 182            throw new ArgumentException("Span is empty", nameof(span));
 83        }
 84
 485        var secure = new SecureString();
 7486        foreach (var c in span)
 87        {
 3388            secure.AppendChar(c);
 89        }
 90
 491        secure.MakeReadOnly();
 492        return secure;
 93    }
 94}