Tests/Public/New-SecretSharingSecret.Tests.ps1

BeforeDiscovery {
    Import-Module (Join-Path $PSScriptRoot '../../Posh-SecretSharing.psd1') -Force
}

<#
Tests real (non-mocked) behavior rather than following the Mock -ModuleName pattern used for
typical API-wrapper cmdlets - for a pure-computation crypto module, testing the actual
computation is the point, matching how every Private/ layer in this suite is already tested.
#>

Describe 'New-SecretSharingSecret' -Tag Unit {
    It 'returns a SecureString' {
        (New-SecretSharingSecret -Entropy 128) | Should -BeOfType [System.Security.SecureString]
    }

    It 'returns a secret of the requested byte length' {
        (New-SecretSharingSecret -Entropy 128).Length | Should -Be 16
        (New-SecretSharingSecret -Entropy 256).Length | Should -Be 32
    }

    It 'throws for an unsupported entropy value' {
        { New-SecretSharingSecret -Entropy 64 } | Should -Throw
        { New-SecretSharingSecret -Entropy 512 } | Should -Throw
    }

    It 'produces different secrets on repeated calls' {
        $a = New-SecretSharingSecret -Entropy 128
        $b = New-SecretSharingSecret -Entropy 128
        $ptrA = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($a)
        $ptrB = [System.Runtime.InteropServices.Marshal]::SecureStringToGlobalAllocUnicode($b)
        try {
            $strA = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptrA, $a.Length)
            $strB = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($ptrB, $b.Length)
            $strA | Should -Not -Be $strB
        } finally {
            [System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($ptrA)
            [System.Runtime.InteropServices.Marshal]::ZeroFreeGlobalAllocUnicode($ptrB)
        }
    }
}