Skip to content

Commit

Permalink
Add load certificate request test (#989)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Apr 11, 2024
1 parent 8ab3350 commit 97235ff
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pkg/certificates/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ func LoadCertificate(filename string, osWrapper Oser) (*x509.Certificate, error)
}

// LoadRequest loads a single certificate request from a file.
func LoadRequest(filename string) (*x509.CertificateRequest, error) {
data, err := LoadFromPEMFile(filename, &OsWrapper{})
func LoadRequest(filename string, osWrapper Oser) (*x509.CertificateRequest, error) {
data, err := LoadFromPEMFile(filename, osWrapper)
if err != nil {
return nil, err
}
Expand Down
89 changes: 87 additions & 2 deletions pkg/certificates/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ func TestLoadPublicKey(t *testing.T) {
wantErr bool
}{
{
name: "Positive Private Key",
name: "Positive Public Key",
args: args{
filename: positivePublicKeyFilename,
},
Expand All @@ -1361,7 +1361,7 @@ func TestLoadPublicKey(t *testing.T) {
wantErr: true,
},
{
name: "Negative no private key test",
name: "Negative no public key test",
args: args{
filename: negativeNoPublicKeyFilename,
},
Expand Down Expand Up @@ -1396,3 +1396,88 @@ func TestLoadPublicKey(t *testing.T) {
})
}
}

func TestLoadRequest(t *testing.T) {
type args struct {
filename string
}
errorSettingUpTypeFormatString := "Error setting up %s: %v"

positiveRequestFilename := "request_test_filename"
goodRequest, err := setupGoodCertificateRequest()
if err != nil {
t.Errorf(errorSettingUpTypeFormatString, "request", err)
}

negativeMultipleItemFilename := "negative_multiple_item_test"
multipleRequests := setupGoodCertificateRequestPEMData()
multipleRequests = append(multipleRequests, multipleRequests[0])

negativeNoRequestFilename := "negative_no_request_test"
noRequest := []byte{
0, 0, 0, 0,
}

tests := []struct {
name string
args args
wantOserReadfileArg string
wantOserReadfileResult []byte
want *x509.CertificateRequest
wantErr bool
}{
{
name: "Positive Request",
args: args{
filename: positiveRequestFilename,
},
wantOserReadfileArg: positiveRequestFilename,
wantOserReadfileResult: setupGoodCertificateRequestPEMData(),
want: goodRequest,
wantErr: false,
},
{
name: "Negative multi item test",
args: args{
filename: negativeMultipleItemFilename,
},
wantOserReadfileArg: negativeMultipleItemFilename,
wantOserReadfileResult: multipleRequests,
want: nil,
wantErr: true,
},
{
name: "Negative no request test",
args: args{
filename: negativeNoRequestFilename,
},
wantOserReadfileArg: negativeNoRequestFilename,
wantOserReadfileResult: noRequest,
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

o := mock_certificates.NewMockOser(ctrl)
o.
EXPECT().
ReadFile(gomock.Eq(tt.wantOserReadfileArg)).
Return(tt.wantOserReadfileResult, nil).
Times(1)

got, err := certificates.LoadRequest(tt.args.filename, o)
if (err != nil) != tt.wantErr {
t.Errorf("LoadRequest() error = %v, wantErr %v", err, tt.wantErr)

return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("LoadRequest() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/certificates/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func SignReq(opts *CertOptions, caCrtPath, caKeyPath, reqPath, certOut string, v
return err
}
var req *x509.CertificateRequest
req, err = LoadRequest(reqPath)
req, err = LoadRequest(reqPath, &OsWrapper{})
if err != nil {
return err
}
Expand Down

0 comments on commit 97235ff

Please sign in to comment.