Skip to content

Commit

Permalink
Add savetopemfile test (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
thom-at-redhat authored Apr 29, 2024
1 parent e66433b commit 4b61542
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 18 deletions.
5 changes: 3 additions & 2 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ flag_management:
statuses:
- type: project
target: auto
threshold: 1%
threshold: 5%
- type: patch
target: 90%
target: auto
threshold: 5%
12 changes: 10 additions & 2 deletions pkg/certificates/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"encoding/pem"
"fmt"
"io"
"io/fs"
"math/big"
"net"
"os"
Expand All @@ -23,6 +24,7 @@ import (

type Oser interface {
ReadFile(name string) ([]byte, error)
WriteFile(name string, data []byte, perm fs.FileMode) error
}

type OsWrapper struct{}
Expand All @@ -31,6 +33,10 @@ func (ow *OsWrapper) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}

func (ow *OsWrapper) WriteFile(name string, data []byte, perm fs.FileMode) error {
return os.WriteFile(name, data, perm)
}

type Rsaer interface {
GenerateKey(random io.Reader, bits int) (*rsa.PrivateKey, error)
}
Expand Down Expand Up @@ -113,7 +119,7 @@ func LoadFromPEMFile(filename string, osWrapper Oser) ([]interface{}, error) {
}

// SaveToPEMFile saves certificate data to a PEM file.
func SaveToPEMFile(filename string, data []interface{}) error {
func SaveToPEMFile(filename string, data []interface{}, osWrapper Oser) error {
var err error
var ok bool
content := make([]string, 0)
Expand Down Expand Up @@ -182,9 +188,11 @@ func SaveToPEMFile(filename string, data []interface{}) error {

continue
}

return fmt.Errorf("unknown block type %s", elem)
}

return os.WriteFile(filename, []byte(strings.Join(content, "\n")), 0o600)
return Oser.WriteFile(osWrapper, filename, []byte(strings.Join(content, "\n")), 0o600)
}

// LoadCertificate loads a single certificate from a file.
Expand Down
204 changes: 204 additions & 0 deletions pkg/certificates/ca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1558,3 +1558,207 @@ func TestRsaWrapper_GenerateKey(t *testing.T) {
})
}
}

func TestSaveToPEMFile(t *testing.T) {
type args struct {
filename string
data []interface{}
}

errorSettingUpTypeFormatString := "Error setting up %s: %v"

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

certificateTestFilename := "certificate_test_filename"
goodCaCertificate, err := setupGoodCertificate()
if err != nil {
t.Errorf("Error setting up certificate: %v", err)
}

failedToEncodeTestFilename := "failed_to_encode_test_filename"

privateKeyTestFilename := "private_key_test_filename"
goodPrivateKey, err := setupGoodPrivateKey()
if err != nil {
t.Errorf(errorSettingUpTypeFormatString, "private key", err)
}

publicKeyTestFilename := "public_key_test_filename"
goodPublicKey, err := setupGoodPublicKey()
if err != nil {
t.Errorf(errorSettingUpTypeFormatString, "public key", err)
}

rsaPrivateKeyTestFilename := "rsa_private_key_test_filename"
goodCaPrivateKey, err := setupGoodRSAPrivateKey()
if err != nil {
t.Fatal(err)
}

unknownBlockTypeTestFilename := "unknown_block_type_test_filename"

ctrl := gomock.NewController(t)
defer ctrl.Finish()

o := mock_certificates.NewMockOser(ctrl)

tests := []struct {
name string
args args
wantOserWritefileCalls func()
want []interface{}
wantErr bool
}{
{
name: "Certificate",
args: args{
filename: certificateTestFilename,
data: []interface{}{
goodCaCertificate,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(1)
},
want: []interface{}{
nil,
},
wantErr: false,
},
{
name: "Certificate Request",
args: args{
filename: certificateRequestTestFilename,
data: []interface{}{
goodRequest,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(1)
},
want: []interface{}{
nil,
},
wantErr: false,
},
{
name: "RSA Private Key",
args: args{
filename: rsaPrivateKeyTestFilename,
data: []interface{}{
goodCaPrivateKey,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(1)
},
want: []interface{}{
nil,
},
wantErr: false,
},
{
name: "Private Key",
args: args{
filename: privateKeyTestFilename,
data: []interface{}{
goodPrivateKey,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(1)
},
want: []interface{}{
nil,
},
wantErr: false,
},
{
name: "Public Key",
args: args{
filename: publicKeyTestFilename,
data: []interface{}{
goodPublicKey,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(1)
},
want: []interface{}{
nil,
},
wantErr: false,
},
{
name: "Failed to encode",
args: args{
filename: failedToEncodeTestFilename,
data: []interface{}{
[]byte{
0, 0, 0, 0,
},
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(0)
},
want: nil,
wantErr: true,
},
{
name: "Unknown block type",
args: args{
filename: unknownBlockTypeTestFilename,
data: []interface{}{
nil,
},
},
wantOserWritefileCalls: func() {
o.
EXPECT().
WriteFile(gomock.Any(), gomock.Any(), gomock.Any()).
Return(interface{}(nil)).
Times(0)
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.wantOserWritefileCalls()

if err := certificates.SaveToPEMFile(tt.args.filename, tt.args.data, o); (err != nil) != tt.wantErr {
t.Errorf("SaveToPEMFile() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
10 changes: 5 additions & 5 deletions pkg/certificates/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
func InitCA(opts *CertOptions, certOut, keyOut string) error {
ca, err := CreateCA(opts, &RsaWrapper{})
if err == nil {
err = SaveToPEMFile(certOut, []interface{}{ca.Certificate})
err = SaveToPEMFile(certOut, []interface{}{ca.Certificate}, &OsWrapper{})
}
if err == nil {
err = SaveToPEMFile(keyOut, []interface{}{ca.PrivateKey})
err = SaveToPEMFile(keyOut, []interface{}{ca.PrivateKey}, &OsWrapper{})
}

return err
Expand Down Expand Up @@ -88,12 +88,12 @@ func MakeReq(opts *CertOptions, keyIn, keyOut, reqOut string) error {
return err
}
}
err := SaveToPEMFile(reqOut, []interface{}{req})
err := SaveToPEMFile(reqOut, []interface{}{req}, &OsWrapper{})
if err != nil {
return err
}
if keyOut != "" {
err = SaveToPEMFile(keyOut, []interface{}{key})
err = SaveToPEMFile(keyOut, []interface{}{key}, &OsWrapper{})
if err != nil {
return err
}
Expand Down Expand Up @@ -212,7 +212,7 @@ func SignReq(opts *CertOptions, caCrtPath, caKeyPath, reqPath, certOut string, v
return err
}

return SaveToPEMFile(certOut, []interface{}{cert})
return SaveToPEMFile(certOut, []interface{}{cert}, &OsWrapper{})
}

type signReq struct {
Expand Down
17 changes: 16 additions & 1 deletion pkg/certificates/mock_certificates/Oser.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions tests/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func GenerateCA(name, commonName string) (string, string, error) {
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(crtPath, []interface{}{CA.Certificate})
err = certificates.SaveToPEMFile(crtPath, []interface{}{CA.Certificate}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{CA.PrivateKey})
err = certificates.SaveToPEMFile(keyPath, []interface{}{CA.PrivateKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -71,11 +71,11 @@ func GenerateCert(name, commonName string, dnsNames, nodeIDs []string) (string,
return "", "", err
}
// Save cert and key to files
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert})
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{key})
err = certificates.SaveToPEMFile(keyPath, []interface{}{key}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -119,11 +119,11 @@ func GenerateCertWithCA(name, caKeyPath, caCrtPath, commonName string, dnsNames,
return "", "", err
}
// Save cert and key to files
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert})
err = certificates.SaveToPEMFile(crtPath, []interface{}{cert}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
err = certificates.SaveToPEMFile(keyPath, []interface{}{key})
err = certificates.SaveToPEMFile(keyPath, []interface{}{key}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
Expand All @@ -143,13 +143,13 @@ func GenerateRSAPair() (string, string, error) {
publicKey := &privateKey.PublicKey

privateKeyPath := filepath.Join(dir, "private.pem")
err = certificates.SaveToPEMFile(privateKeyPath, []interface{}{privateKey})
err = certificates.SaveToPEMFile(privateKeyPath, []interface{}{privateKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}

publicKeyPath := filepath.Join(dir, "public.pem")
err = certificates.SaveToPEMFile(publicKeyPath, []interface{}{publicKey})
err = certificates.SaveToPEMFile(publicKeyPath, []interface{}{publicKey}, &certificates.OsWrapper{})
if err != nil {
return "", "", err
}
Expand Down

0 comments on commit 4b61542

Please sign in to comment.