Skip to content

Commit

Permalink
feat: add more testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashwat-K committed Mar 28, 2024
1 parent 674ab90 commit f8d9d20
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 0 deletions.
122 changes: 122 additions & 0 deletions datasource/datasource_contract_signingcert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package datasource

import (
"encoding/json"
"fmt"
"io"
"os"
"testing"

"github.com/ibm-hyper-protect/terraform-provider-hpcr/data"
"github.com/stretchr/testify/assert"
)

func Commoner() (string, string, string, string, string, error) {
contractPath, err := os.Open("../samples/contracts/simple.yaml")
if err != nil {
fmt.Println("Error parsing contract - ", err)
}
defer contractPath.Close()

contract, err := io.ReadAll(contractPath)
if err != nil {
fmt.Println(err)
return "", "", "", "", "", err
}

encryptCert := data.DefaultCertificate

privateKeyPath, err := os.Open("../samples/contract-expiry/private.pem")
if err != nil {
fmt.Println("Error parsing Private Key - ", err)
return "", "", "", "", "", err
}
defer privateKeyPath.Close()

privateKey, err := io.ReadAll(privateKeyPath)
if err != nil {
fmt.Println(err)
return "", "", "", "", "", err
}

caCertPath, err := os.Open("../samples/contract-expiry/personal_ca.crt")
if err != nil {
fmt.Println("Error parsing CA certificate - ", err)
return "", "", "", "", "", err
}
defer caCertPath.Close()

caCert, err := io.ReadAll(caCertPath)
if err != nil {
fmt.Println(err)
return "", "", "", "", "", err
}

caKeyPath, err := os.Open("../samples/contract-expiry/personal_ca.key")
if err != nil {
fmt.Println("Error parsing CA certificate - ", err)
return "", "", "", "", "", err
}
defer caCertPath.Close()

caKey, err := io.ReadAll(caKeyPath)
if err != nil {
fmt.Println(err)
return "", "", "", "", "", err
}

return string(contract), encryptCert, string(privateKey), string(caCert), string(caKey), nil
}

func TestEncryptAndSign(t *testing.T) {

contract, encryptCert, privateKey, caCert, caKey, err := Commoner()
if err != nil {
fmt.Println(err)
}

csrDataMap := map[string]interface{}{
"country": "IN",
"state": "Karnataka",
"location": "Bangalore",
"org": "IBM",
"unit": "ISDL",
"domain": "HPVS",
"mail": "sashwat.k@ibm.com",
}
csrDataStr, err := json.Marshal(csrDataMap)
if err != nil {
fmt.Println(err)
}

expiryDays := 365

_, err = EncryptAndSign(contract, encryptCert, privateKey, caCert, caKey, string(csrDataStr), "", expiryDays)

assert.NoError(t, err)
}

func TestEncryptAndSignCsrFile(t *testing.T) {

contract, encryptCert, privateKey, caCert, caKey, err := Commoner()
if err != nil {
fmt.Println(err)
}

csrPath, err := os.Open("../samples/contract-expiry/csr.pem")
if err != nil {
fmt.Println("Error parsing CSR - ", err)
}
defer csrPath.Close()

csr, err := io.ReadAll(csrPath)
if err != nil {
fmt.Println(err)
}

expiryDays := 365

_, err = EncryptAndSign(contract, encryptCert, privateKey, caCert, caKey, "", string(csr), expiryDays)

assert.NoError(t, err)
}
28 changes: 28 additions & 0 deletions samples/contract-expiry/csr.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN CERTIFICATE REQUEST-----
MIIEyTCCArECAQAwgYMxCzAJBgNVBAYTAklOMRIwEAYDVQQIDAlLYXJuYXRha2Ex
EjAQBgNVBAcMCUJhbmdhbG9yZTEMMAoGA1UECgwDSUJNMQ0wCwYDVQQLDARJU0RM
MQ0wCwYDVQQDDARIUFZTMSAwHgYJKoZIhvcNAQkBFhFTYXNod2F0LktAaWJtLmNv
bTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBALWpHedRZqz8DtYSfnzr
QXPI9i8lzTvHhP0+cZAfO2qi59ZyTa+j5zoGMDLrF2k1DpOfRIRmwJHW8LNmj6N8
qzOCisFM0O6Rb1ZIh8He/0vtYmucvKlXau+t8BEcTdM53w8kcaTwHCcepoP7sy+q
fjQSfCj7vCTKOO0tx3LcLvDX0Hlp1pkkai/yuF830I3eQgV7tUuDzdjfo4Nuxb1X
mEYWmazbKihQOW3ut9K/RpN/vem9A77b4cWCYFr6E6/XbPjgtdQpXbl2dU/E/9W/
F48NoHUpjxt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