Repository: PowerShell/PowerShell
File: test/powershell/Modules/Microsoft.PowerShell.Security/CmsMessage2.Tests.ps1 (PowerShell)

Code before:
            -----END CERTIFICATE-----
            "
  }

  It " Encrypting with X509Cert" {
    "test" | Protect-CmsMessage -to $vc1 | Should -BeLike '-----BEGIN CMS*'
  }

  It " Encrypting with base64 string" {
    "test" | Protect-CmsMessage -to $certContent | Should -BeLike '-----BEGIN CMS*'
  }

  It " Encrypting with multiple X509Cert" {
    $msg = "test" | Protect-CmsMessage -to $vc1, $vc2 | Should -BeLike '-----BEGIN CMS*'
  }

  It " Decrypt with X509Cert" {
    "test" | Protect-CmsMessage -to $vc1 | Unprotect-CmsMessage -to $vc1 | Should -BeExactly "test"
  }

  It " Decrypt with multiple X509Cert" {
    "test" | Protect-CmsMessage -to $vc1, $vc2 | Unprotect-CmsMessage -to $vc1, $vc2 | Should -BeExactly "test"
  }

  It " Encrypt with invalid cert" {
    {"test" | Protect-CmsMessage -to $ic -ErrorAction Stop } | Should -Throw -ErrorId 'CertificateCannotBeUsedForEncryption'
  }

  It "Encrypt with valid and invalid" {
    { "test" | Protect-CmsMessage -to $vc1, $vc2, $ic  -ErrorAction Stop } | Should -Throw -ErrorId 'CertificateCannotBeUsedForEncryption'
  }

  It "Encrypt/Decrypt from file" {
    "test" | Protect-CmsMessage -To $vc1 -OutFile $tmpfile
    $msg = Unprotect-CmsMessage -to $vc1 -Path $tmpfile
    $msg | Should -BeExactly "test"
  }

  It "Get-CmsMessage from content" {
    ("test" | Protect-CmsMessage -to $vc1 | Get-CmsMessage).Content | Should -BeLike '-----BEGIN CMS*'
  }

  It "Get-CmsMessage from file" {
    (Get-CmsMessage -Path $tmpfile).Content | Should -BeLike '-----BEGIN CMS*'
  }
}

Describe "CmsMessage cmdlets using files" -Tags "CI" {

  BeforeAll {
    Write-Verbose  "generating temp cert files"

Review comment (suggestion):
Indentation should be 4.
```suggestion
    { "test" | Protect-CmsMessage -To $ic -ErrorAction Stop } | Should -Throw -ErrorId 'CertificateCannotBeUsedForEncryption'
```

Code after:
            bnRDZXJ0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx3SuShUvnRqn
            tYOIouJdP3wPZ5rtDi2KYPurpngGNZjM0EGDTrnhmEAI8DL4Kp6n/zz1mYVoX73+
            6uCpZX/13VDXg1neebJ261XpBX6FzxtclIQr8ywdUtrEgCnUAhgqgvO1Wwm4ogNR
            tWGCGkmlnqyaoV1j/V4KSn4WvKqSUIOZm0umGCTtNAJ6VtdpYO+uxxnRAapPUCY+
            qQ7DFzTUECIo1lMlBcuMiXj6NSFr4/D7ltkZ27jCdsZmzI7ZvRnDlfSYTPQnAO/E
            0uYn9uyKY/xfngWkUX/pe+j+10Lm1ypbASrj2Ezgf0KeZRXBwqKUOLhKheEmBJ18
            rLV27qwHeQIDAQABo4GOMIGLMA4GA1UdDwEB/wQEAwIEMDAUBgNVHSUEDTALBgkr
            BgEEAYI3UAEwRAYJKoZIhvcNAQkPBDcwNTAOBggqhkiG9w0DAgICAIAwDgYIKoZI
            hvcNAwQCAgCAMAcGBSsOAwIHMAoGCCqGSIb3DQMHMB0GA1UdDgQWBBRIyIzwInLJ
            3B+FajVUFMACf1hrxjANBgkqhkiG9w0BAQUFAAOCAQEAfFt4rmUmWfCbbwi2mCrZ
            Osq0lfVNUiZ+iLlEKga4VAI3sJZRtErnVM70eXUt7XpRaOdIfxjuXFpsgc37KyLi
            ByCORLuRC0itZVs3aba48opfMDXivxBy0ngqCPPLQsyaN9K7WnpvYV1QxiudYwwU
            8U5rFmzlwNLvc3XiyoGWaVZluk2DIJawQ5QYAU9/NMBBCbPHjTG7k0l4cpcEC+Ex
            od3RlO6/MOYuK2WB4VTxKsV80EdA3ljlu7Td8P4movnrbB4rG4wpCpk05eREkg/5
            Y54Ilo9m5OSAWtdx4yfS779eebLgUs3P+dk6EKwovXMokVveZA8cenIp3QkqSpeT
            cQ==
            -----END CERTIFICATE-----
            "
    }

    It "Cert Store: Encrypt/Decrypt using Subject" {
        "test" | Protect-CmsMessage -To $vc1.Subject | Unprotect-CmsMessage | Should -BeExactly "test"
        "test" | Protect-CmsMessage -To $vc1.Subject, $vc2.Subject | Unprotect-CmsMessage | Should -BeExactly "test"
    }

    It "Cert Store: Subject with wildcard (returns single cert)" {
        "test" | Protect-CmsMessage -To "*dCms1" | Unprotect-CmsMessage | Should -BeExactly "test"
    }

    It "Cert Store: Subject with wrong wildcard (returns multiple certs)" {
        { "test" | Protect-CmsMessage -To "*ValidCms*" -ErrorAction Stop } | Should -Throw -ErrorId 'IdentifierMustReferenceSingleCertificate'
    }

    It "Cert Store: Encrypt/Decrypt using Thumbprint" {
        "test" | Protect-CmsMessage -To $vc1.Thumbprint | Unprotect-CmsMessage | Should -BeExactly "test"
        "test" | Protect-CmsMessage -To $vc1.Thumbprint, $vc2.Thumbprint | Unprotect-CmsMessage | Should -BeExactly "test"
    }

    It "Cert Store: Encrypt/Decrypt subject and thumbprint" {
        "test" | Protect-CmsMessage -To $vc1.Thumbprint, $vc2.Subject | Unprotect-CmsMessage | Should -BeExactly "test"
    }

    It "Cert Store: removing test certificates" {
        $store.Remove($vc1)
        $store.Remove($vc2)
        $store.Remove($vc3)
        if ($IsMacOS) {
            $store.Remove($ic)
        }

        $store.Certificates.Find("FindByThumbprint", $vc1.Thumbprint, $false).Count | Should -BeExactly 0