HiddenString.Tests.ps1

#Requires -Modules @{ModuleName="Pester"; ModuleVersion="5.0.0"}

Describe 'Join-Object' {
    
    BeforeAll {
    
        Set-StrictMode -Version Latest

        . $PSScriptRoot\HiddenString.ps1
        
        # $Confidential = '***Confidential-Information***'
        
        # function Decrypt([SecureString]$SecureString) {
            # $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecureString)
            # $String = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
            # [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
            # $String
        # }
    }
        
    Context 'General usage' {

        It 'Hidden parameter' {

            $Actual = $Null
            
            function BadApp([String]$Username, [String]$Password) {
                ([ref]$Actual).Value = $Password
            }

            function MyScript {
                [CmdletBinding()] param(
                    [String]$Username,
                    [HiddenString]$Password
                )
                Write-Host "Credentials: $Username/$Password" # Write-Log ...
                BadApp $Username $Password.Reveal()
            }
            $Password = 'Unsecure plain text password'
            Start-Transcript -Path $PSScriptRoot\Transcript.txt
            MyScript JohnDoe $Password -WarningAction SilentlyContinue
            Stop-Transcript

            $Actual |Should -Be $Password
            Get-Content -Path $PSScriptRoot\Transcript.txt |Should -Not -Contain $Password
        }

    }
}


function Decrypt([SecureString]$SecureString) {
    $Ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToCoTaskMemUnicode($SecureString)
    $String = [System.Runtime.InteropServices.Marshal]::PtrToStringUni($Ptr)
    [System.Runtime.InteropServices.Marshal]::ZeroFreeCoTaskMemUnicode($Ptr)
    $String
}

# function SetScheduledTask([String]$TaskName, [String]$UserName, [String]$Password) { Write-Host $Password}

# function MyScript([String]$TaskName, [String]$UserName, [HiddenString]$Password) {

    # Start-Transcript -Path C:\Users\Gebruiker\Scripts\HiddenString\Transcript.txt
    
    # Write-Host "Scheduling $TaskName for $UserName/$Password" # Write-Log ...
    # SetScheduledTask -TaskName $TaskName -User $UserName -Password $Password.Reveal()

    # Stop-Transcript
# }

# function MyScript2([String]$TaskName, [String]$UserName, [HiddenString]$Password) {
    # MyScript -TaskName $TaskName -UserName $UserName -Password $Password
# }

# MyScript2 NotePad.Exe JohnDoe P@ssw0rd

# $HiddenString = [HiddenString]'P@ssw0rd'
# $HiddenString
# $HiddenString.Reveal()


# $SecureString = [SecureString]::new()
# foreach ($Character in [Char[]]'Test123') { $SecureString.AppendChar($Character) }

# $HiddenString = [HiddenString]$SecureString
# $HiddenString
# $HiddenString.Reveal()

# $Confidential = 'Confidential Information'
# $SecureString = [SecureString]::new()
# foreach ($Character in [Char[]]$Confidential) { $SecureString.AppendChar($Character) }
# $Base64 = [Convert]::ToBase64String(([regex]::matches(($SecureString |ConvertFrom-SecureString), '.{2}')).foreach{ [byte]"0x$_" })
# Write-Host 'Base64:' $Base64

# $HiddenString = [HiddenString]::New([System.Convert]::FromBase64String($Base64), $True)
# $HiddenString.Add([System.Environment]::NewLine)
# $HiddenString.Add('More Information')
# $HiddenString.Reveal()