Skip to content

Commit

Permalink
small bugfix in filecrlloader and added tests for filecrlloader
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRoettges committed Sep 27, 2023
1 parent af30393 commit 5d85587
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 1 deletion.
2 changes: 1 addition & 1 deletion crl/crlloader/filecrlloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (f FileLoader) copyToTargetFile(filePath string) error {
if stat.IsDir() {
return fmt.Errorf("CRL File %s is a directory", f.FileName)
}
crlFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_EXCL, 0600)
crlFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0600)
if err != nil {
return err
}
Expand Down
189 changes: 189 additions & 0 deletions crl/crlloader/filecrlloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package crlloader

import (
"github.com/gr33nbl00d/caddy-revocation-validator/core/utils"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
)

type FileLoaderSuite struct {
suite.Suite
logger *zap.Logger
tmpDir string // Store the path to the temporary directory
}

func (suite *FileLoaderSuite) SetupTest() {
// Initialize the logger before each test
logger, _ := zap.NewDevelopment()
suite.logger = logger

// Create a temporary directory
tmpDir, err := ioutil.TempDir("", "test-crl-dir-")
assert.NoError(suite.T(), err, "Error creating temporary directory")
suite.tmpDir = tmpDir
}

func (suite *FileLoaderSuite) TearDownTest() {
// Remove the temporary directory after each test

utils.Retry(10, 3*time.Second, zap.NewExample(), func() error {
err := os.RemoveAll(suite.tmpDir)
assert.NoError(suite.T(), err, "Error removing temporary directory")
return err
})

}

func (suite *FileLoaderSuite) TestLoadCRL() {
// Create a temporary CRL file within the temporary directory
crlFile, err := ioutil.TempFile(suite.tmpDir, "test-crl-*.crl")
// Write some data to the temporary file
_, err = crlFile.WriteString("Test CRL Data")
assert.NoError(suite.T(), err)
err = crlFile.Close()
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
defer os.Remove(crlFile.Name())

// Create a FileLoader instance
fileLoader := FileLoader{
FileName: crlFile.Name(),
Logger: suite.logger,
}

// Define the path to copy the CRL to
copyToPath := filepath.Join(suite.tmpDir, "copied-crl.crl")

// Call the LoadCRL method
err = fileLoader.LoadCRL(copyToPath)

assert.NoError(suite.T(), err, "Error copying CRL")

// Check if the CRL was copied correctly
copiedCRLData, err := ioutil.ReadFile(copyToPath)

assert.NoError(suite.T(), err, "Error reading copied CRL")
assert.Equal(suite.T(), "Test CRL Data", string(copiedCRLData), "Copied CRL data doesn't match")
}

func (suite *FileLoaderSuite) TestLoadCRLWhereCRLIsDirectory() {
// Create a temporary CRL file within the temporary directory
crlFile, err := ioutil.TempFile(suite.tmpDir, "test-crl-*.crl")

// Write some data to the temporary file
_, err = crlFile.WriteString("Test CRL Data")
assert.NoError(suite.T(), err)
err = crlFile.Close()
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
defer os.Remove(crlFile.Name())

// Create a FileLoader instance
fileLoader := FileLoader{
FileName: suite.tmpDir,
Logger: suite.logger,
}

// Define the path to copy the CRL to
copyToPath := filepath.Join(suite.tmpDir, "copied-crl.crl")

// Call the LoadCRL method
err = fileLoader.LoadCRL(copyToPath)
assert.Error(suite.T(), err, "should return an error")
assert.Contains(suite.T(), err.Error(), "after 5 attempts, last error: CRL File")
assert.Contains(suite.T(), err.Error(), "is a directory")
}

func (suite *FileLoaderSuite) TestLoadCRLWhereCRLPathDoesNotExist() {

// Create a temporary CRL file within the temporary directory
crlFile, err := ioutil.TempFile(suite.tmpDir, "test-crl-*.crl")

// Write some data to the temporary file
_, err = crlFile.WriteString("Test CRL Data")
assert.NoError(suite.T(), err)
err = crlFile.Close()
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
defer os.Remove(crlFile.Name())

// Create a FileLoader instance
fileLoader := FileLoader{
FileName: crlFile.Name(),
Logger: suite.logger,
}

// Define the path to copy the CRL to
copyToPath := filepath.Join(suite.tmpDir, "nonexistent/nonexsistent.crl")

// Call the LoadCRL method
err = fileLoader.LoadCRL(copyToPath)
assert.Error(suite.T(), err, "should return an error")
assert.Contains(suite.T(), err.Error(), "The system cannot find the path specified")
}

func (suite *FileLoaderSuite) TestLoadCRLWhereCRLTargetPathDoesNotExist() {

invalidTargetPath := filepath.Join(suite.tmpDir, "nonexistent/nonexsistent.crl")
// Create a FileLoader instance
fileLoader := FileLoader{
FileName: invalidTargetPath,
Logger: suite.logger,
}

// Define the path to copy the CRL to
copyToPath := filepath.Join(suite.tmpDir, "copied-crl.crl")

// Call the LoadCRL method
err := fileLoader.LoadCRL(copyToPath)
assert.Error(suite.T(), err, "should return an error")
assert.Contains(suite.T(), err.Error(), "The system cannot find the path specified")
}

func (suite *FileLoaderSuite) TestGetCRLLocationIdentifier() {
// Create a temporary CRL file within the temporary directory
crlFile, err := ioutil.TempFile(suite.tmpDir, "test-crl-*.crl")
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
err = crlFile.Close()
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
defer os.Remove(crlFile.Name())

// Create a FileLoader instance
fileLoader := FileLoader{
FileName: crlFile.Name(),
Logger: suite.logger,
}

// Call the GetCRLLocationIdentifier method
identifier, err := fileLoader.GetCRLLocationIdentifier()
assert.NoError(suite.T(), err, "Error getting CRL location identifier")
assert.NotEmpty(suite.T(), identifier, "CRL location identifier is empty")
}

func (suite *FileLoaderSuite) TestGetDescription() {
// Create a temporary CRL file within the temporary directory
crlFile, err := ioutil.TempFile(suite.tmpDir, "test-crl-*.crl")
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
err = crlFile.Close()
assert.NoError(suite.T(), err, "Error creating temporary CRL file")
defer os.Remove(crlFile.Name())

// Create a FileLoader instance
fileLoader := FileLoader{
FileName: crlFile.Name(),
Logger: suite.logger,
}

// Call the GetDescription method
description := fileLoader.GetDescription()
assert.NotEmpty(suite.T(), description, "Description is empty")
}

func TestFileLoaderSuite(t *testing.T) {
// Run the test suite
suite.Run(t, new(FileLoaderSuite))
}

0 comments on commit 5d85587

Please sign in to comment.