Skip to content

Commit

Permalink
Add Makereq cli test (#1050)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored May 22, 2024
1 parent ca48d6d commit ee4fa78
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
3 changes: 3 additions & 0 deletions pkg/certificates/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/ghjm/cmdline"
)

// InitCA Initialize Certificate Authority.
func InitCA(opts *CertOptions, certOut, keyOut string) error {
ca, err := CreateCA(opts, &RsaWrapper{})
if err == nil {
Expand Down Expand Up @@ -56,6 +57,7 @@ func (ica initCA) Run() (err error) {
return InitCA(opts, ica.OutCert, ica.OutKey)
}

// MakeReq Create Certificate Request.
func MakeReq(opts *CertOptions, keyIn, keyOut, reqOut string) error {
var req *x509.CertificateRequest
var key *rsa.PrivateKey
Expand Down Expand Up @@ -151,6 +153,7 @@ func (mr makeReq) Run() error {
return MakeReq(opts, mr.InKey, mr.OutKey, mr.OutReq)
}

// SignReq Sign Certificate Request.
func SignReq(opts *CertOptions, caCrtPath, caKeyPath, reqPath, certOut string, verify bool) error {
ca := &CA{}
var err error
Expand Down
58 changes: 53 additions & 5 deletions pkg/certificates/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func TestInitCA(t *testing.T) {
name: "Positive test",
args: args{
opts: &certificates.CertOptions{
CommonName: "Ansible Automation Controller Nodes Mesh",
Bits: 8192,
CommonName: "Ansible Automation Controller Nodes Mesh",
},
certOut: "receptor_cert.pem",
keyOut: "receptor_key.pem",
certOut: "/tmp/receptor_cert.pem",
keyOut: "/tmp/receptor_key.pem",
},
wantErr: false,
},
Expand All @@ -39,8 +39,8 @@ func TestInitCA(t *testing.T) {
Bits: -1,
CommonName: "Ansible Automation Controller Nodes Mesh",
},
certOut: "receptor_cert.pem",
keyOut: "receptor_key.pem",
certOut: "/tmp/receptor_cert.pem",
keyOut: "/tmp/receptor_key.pem",
},
wantErr: true,
},
Expand All @@ -54,3 +54,51 @@ func TestInitCA(t *testing.T) {
})
}
}

func TestMakeReq(t *testing.T) {
type args struct {
opts *certificates.CertOptions
keyIn string
keyOut string
reqOut string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Positive test",
args: args{
opts: &certificates.CertOptions{
Bits: 8192,
CommonName: "Ansible Automation Controller Nodes Mesh",
},
keyIn: "/tmp/receptor_key.pem",
keyOut: "/tmp/receptor_key_out.pem",
reqOut: "/tmp/receptor_request_out.pem",
},
wantErr: false,
},
{
name: "Negative test",
args: args{
opts: &certificates.CertOptions{
Bits: -1,
CommonName: "Ansible Automation Controller Nodes Mesh",
},
keyIn: "/tmp/",
keyOut: "/tmp/receptor_key_out.pem",
reqOut: "/tmp/receptor_request_out.pem",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := certificates.MakeReq(tt.args.opts, tt.args.keyIn, tt.args.keyOut, tt.args.reqOut); (err != nil) != tt.wantErr {
t.Errorf("MakeReq() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit ee4fa78

Please sign in to comment.