Skip to content

Commit

Permalink
Added certreq test
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored and AaronH88 committed Feb 2, 2024
1 parent 94cf499 commit 9f05dd2
Showing 1 changed file with 219 additions and 0 deletions.
219 changes: 219 additions & 0 deletions pkg/certificates/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package certificates_test
import (
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io"
Expand Down Expand Up @@ -284,3 +285,221 @@ func TestCreateCANegative(t *testing.T) {
})
}
}

func TestCreateCertReqValid(t *testing.T) {
type args struct {
opts *certificates.CertOptions
privateKey *rsa.PrivateKey
}

goodCaTimeAfterString := "2032-01-07T00:03:51Z"
goodCaTimeAfter, err := time.Parse(time.RFC3339, goodCaTimeAfterString)
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeAfterString, err)
}

goodCaTimeBeforeString := "2022-01-07T00:03:51Z"
goodCaTimeBefore, err := time.Parse(time.RFC3339, goodCaTimeBeforeString)
if err != nil {
t.Errorf("Error parsing time %s: %v", goodCaTimeBeforeString, err)
}

goodCertOptions := certificates.CertOptions{
Bits: 4096,
CommonName: "Ansible Automation Controller Nodes Mesh",
NotAfter: goodCaTimeAfter,
NotBefore: goodCaTimeBefore,
}

goodCertificateRequest := &x509.CertificateRequest{
Attributes: nil,
DNSNames: nil,
EmailAddresses: nil,
Extensions: nil,
ExtraExtensions: nil,
IPAddresses: nil,
PublicKeyAlgorithm: x509.RSA,
SignatureAlgorithm: x509.SHA256WithRSA,
Subject: pkix.Name{
CommonName: goodCertOptions.CommonName,
Country: nil,
ExtraNames: []pkix.AttributeTypeAndValue{},
Locality: nil,
Names: []pkix.AttributeTypeAndValue{},
Organization: nil,
OrganizationalUnit: nil,
PostalCode: nil,
Province: nil,
SerialNumber: "",
StreetAddress: nil,
},
URIs: nil,
Version: 0,
}

goodPrivateKeyBlock, _ := pem.Decode([]byte(`-----BEGIN RS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S1PrivateKey(goodPrivateKeyBlock.Bytes)
if err != nil {
t.Errorf("Error parsing Private Key: %v", err)
}

tests := []struct {
name string
args args
want *x509.CertificateRequest
wantErr bool
}{
{
name: "Positive test",
args: args{
opts: &goodCertOptions,
privateKey: goodPrivateKey,
},
want: goodCertificateRequest,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := certificates.CreateCertReq(tt.args.opts, tt.args.privateKey)
if (err != nil) != tt.wantErr {
t.Errorf("CreateCertReq() error = %v, wantErr %v", err, tt.wantErr)

return
}

if !reflect.DeepEqual(got.DNSNames, tt.want.DNSNames) {
t.Errorf("CreateCertReq() DNSNames got = %v, want = %v", got.DNSNames, tt.want.DNSNames)

return
}

if !reflect.DeepEqual(got.EmailAddresses, tt.want.EmailAddresses) {
t.Errorf("CreateCertReq() EmailAddresses got = %v, want = %v", got.EmailAddresses, tt.want.EmailAddresses)

return
}

if !reflect.DeepEqual(got.ExtraExtensions, tt.want.ExtraExtensions) {
t.Errorf("CreateCertReq() ExtraExtensions got = %v, want = %v", got.ExtraExtensions, tt.want.ExtraExtensions)

return
}

if !reflect.DeepEqual(got.IPAddresses, tt.want.IPAddresses) {
t.Errorf("CreateCertReq() IPAddresses got = %v, want = %v", got.IPAddresses, tt.want.IPAddresses)

return
}

if !reflect.DeepEqual(got.PublicKeyAlgorithm, tt.want.PublicKeyAlgorithm) {
t.Errorf("CreateCertReq() PublicKeyAlgorithm = %v, want = %v", got.PublicKeyAlgorithm, tt.want.PublicKeyAlgorithm)

return
}

if !reflect.DeepEqual(got.SignatureAlgorithm, tt.want.SignatureAlgorithm) {
t.Errorf("CreateCertReq() SignatureAlgorithm got = %v, want = %v", got.SignatureAlgorithm, tt.want.SignatureAlgorithm)

return
}

if !reflect.DeepEqual(got.URIs, tt.want.URIs) {
t.Errorf("CreateCertReq() URIs got = %v, want = %v", got.URIs, tt.want.URIs)

return
}

if !reflect.DeepEqual(got.Version, tt.want.Version) {
t.Errorf("CreateCertReq() Version got = %v, want = %v", got.Version, tt.want.Version)

return
}
})
}
}

func TestCreateCertReqNegative(t *testing.T) {
type args struct {
opts *certificates.CertOptions
privateKey *rsa.PrivateKey
}

badCertOptions := certificates.CertOptions{
Bits: -1,
}
tests := []struct {
name string
args args
want *x509.CertificateRequest
wantErr error
}{
{
name: "Negative test for Common Name",
args: args{
opts: &badCertOptions,
privateKey: nil,
},
want: nil,
wantErr: fmt.Errorf("must provide CommonName"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, gotErr := certificates.CreateCertReq(tt.args.opts, tt.args.privateKey)
if gotErr == nil || gotErr.Error() != tt.wantErr.Error() {
t.Errorf("CreateCA() error = %v, wantErr = %v", gotErr, tt.wantErr)
}
})
}
}

0 comments on commit 9f05dd2

Please sign in to comment.