Skip to content

Commit

Permalink
added more testes for loaders
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRoettges committed Sep 27, 2023
1 parent cae0444 commit ec2d9a2
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 5 deletions.
8 changes: 3 additions & 5 deletions crl/crlloader/multischemescrlloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,9 @@ func (f MultiSchemesCRLLoader) GetCRLLocationIdentifier() (string, error) {
}

func (f MultiSchemesCRLLoader) GetDescription() string {
builder := strings.Builder{}
var descriptions []string
for _, loader := range f.Loaders {
description := loader.GetDescription()
builder.WriteString(description)
builder.WriteString(", ")
descriptions = append(descriptions, loader.GetDescription())
}
return builder.String()
return strings.Join(descriptions, ", ")
}
146 changes: 146 additions & 0 deletions crl/crlloader/multischemescrlloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package crlloader

import (
"fmt"
"testing"

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

// MockCRLLoader is a mock implementation of the CRLLoader interface
type MockCRLLoader struct {
mock.Mock
}

func (m *MockCRLLoader) LoadCRL(filePath string) error {
args := m.Called(filePath)
return args.Error(0)
}

func (m *MockCRLLoader) GetCRLLocationIdentifier() (string, error) {
args := m.Called()
return args.String(0), args.Error(1)
}

func (m *MockCRLLoader) GetDescription() string {
args := m.Called()
return args.String(0)
}

type MultiSchemesCRLLoaderSuite struct {
suite.Suite
logger *zap.Logger
}

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

func (suite *MultiSchemesCRLLoaderSuite) TestLoadCRL_Success() {
// Create two mock loaders
mockLoader1 := new(MockCRLLoader)
mockLoader2 := new(MockCRLLoader)

// Configure the first loader to succeed
mockLoader1.On("LoadCRL", mock.Anything).Return(nil)

// Create a MultiSchemesCRLLoader with the two loaders
loader := MultiSchemesCRLLoader{
Loaders: []CRLLoader{mockLoader1, mockLoader2},
Logger: suite.logger,
}

// Call the LoadCRL method
err := loader.LoadCRL("test.crl")

// Assert that the first loader was called and no error occurred
assert.NoError(suite.T(), err)

// Verify that the second loader was not called
mockLoader2.AssertNotCalled(suite.T(), "LoadCRL", mock.Anything)
}

func (suite *MultiSchemesCRLLoaderSuite) TestLoadCRL_Failure() {
// Create two mock loaders
mockLoader1 := new(MockCRLLoader)
mockLoader2 := new(MockCRLLoader)

// Configure both loaders to fail
mockLoader1.On("LoadCRL", mock.Anything).Return(fmt.Errorf("Loader 1 failed"))
mockLoader2.On("LoadCRL", mock.Anything).Return(fmt.Errorf("Loader 2 failed"))
// Configure both loaders to provide descriptions
mockLoader1.On("GetDescription").Return("Loader 1")
mockLoader2.On("GetDescription").Return("Loader 2")

// Create a MultiSchemesCRLLoader with the two loaders
loader := MultiSchemesCRLLoader{
Loaders: []CRLLoader{mockLoader1, mockLoader2},
Logger: suite.logger,
}

// Call the LoadCRL method
err := loader.LoadCRL("test.crl")

// Assert that an error occurred and no loader was successful
assert.Error(suite.T(), err)
assert.Contains(suite.T(), err.Error(), "failed to load CRL from all loaders")

// Verify that both loaders were called
mockLoader1.AssertCalled(suite.T(), "LoadCRL", "test.crl")
mockLoader2.AssertCalled(suite.T(), "LoadCRL", "test.crl")
}

func (suite *MultiSchemesCRLLoaderSuite) TestGetCRLLocationIdentifier() {
// Create two mock loaders
mockLoader1 := new(MockCRLLoader)
mockLoader2 := new(MockCRLLoader)

// Configure the loaders to return identifiers
mockLoader1.On("GetCRLLocationIdentifier").Return("Loader1Identifier", nil)
mockLoader2.On("GetCRLLocationIdentifier").Return("Loader2Identifier", nil)

// Create a MultiSchemesCRLLoader with the two loaders
loader := MultiSchemesCRLLoader{
Loaders: []CRLLoader{mockLoader1, mockLoader2},
Logger: suite.logger,
}

// Call the GetCRLLocationIdentifier method
identifier, err := loader.GetCRLLocationIdentifier()

// Assert that no error occurred and the identifiers are concatenated
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "5c489d06cdb283469537650b99d9aebcbb96685df2ec3003c95704cf2d45924f", identifier)
}

func (suite *MultiSchemesCRLLoaderSuite) TestGetDescription() {
// Create two mock loaders
mockLoader1 := new(MockCRLLoader)
mockLoader2 := new(MockCRLLoader)

// Configure the loaders to return descriptions
mockLoader1.On("GetDescription").Return("Loader 1")
mockLoader2.On("GetDescription").Return("Loader 2")

// Create a MultiSchemesCRLLoader with the two loaders
loader := MultiSchemesCRLLoader{
Loaders: []CRLLoader{mockLoader1, mockLoader2},
Logger: suite.logger,
}

// Call the GetDescription method
description := loader.GetDescription()

// Assert that the descriptions are concatenated
assert.Equal(suite.T(), "Loader 1, Loader 2", description)
}

func TestMultiSchemesCRLLoaderSuite(t *testing.T) {
// Run the test suite
suite.Run(t, new(MultiSchemesCRLLoaderSuite))
}
39 changes: 39 additions & 0 deletions crl/crlloader/urlcrlloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,33 @@ func (suite *URLLoaderSuite) TestLoadCRLWithInvalidUrl() {
assert.Contains(suite.T(), err.Error(), "unsupported protocol")
}

func (suite *URLLoaderSuite) TestLoadCRLWhereCRLTargetPathDoesNotExist() {
// Create a temporary directory to store the downloaded CRL
tmpDir, err := ioutil.TempDir("", "test-crl-dir-")
assert.NoError(suite.T(), err, "Error creating temporary directory")
defer os.RemoveAll(tmpDir)

// Create a mock HTTP server that serves the CRL content
mockCRLContent := "Mock CRL Data"
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(mockCRLContent))
}))
defer mockServer.Close()

// Create a URLLoader instance
urlLoader := URLLoader{
UrlString: mockServer.URL,
Logger: suite.logger,
}

// Define the path to save the downloaded CRL
downloadFilePath := filepath.Join(tmpDir, "notexist/downloaded-crl.crl")

// Call the LoadCRL method
err = urlLoader.LoadCRL(downloadFilePath)
assert.Error(suite.T(), err)
}

func (suite *URLLoaderSuite) TestGetCRLLocationIdentifier() {
// Create a URLLoader instance
urlLoader := URLLoader{
Expand All @@ -97,6 +124,18 @@ func (suite *URLLoaderSuite) TestGetCRLLocationIdentifier() {
assert.NotEmpty(suite.T(), identifier, "CRL location identifier is empty")
}

func (suite *URLLoaderSuite) TestGetCRLLocationIdentifierWithInvalidUrl() {
// Create a URLLoader instance
urlLoader := URLLoader{
UrlString: "bongo://:dsdsd",
Logger: suite.logger,
}

// Call the GetCRLLocationIdentifier method
_, err := urlLoader.GetCRLLocationIdentifier()
assert.Error(suite.T(), err)
}

func (suite *URLLoaderSuite) TestGetDescription() {
// Create a URLLoader instance
urlLoader := URLLoader{
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ require (
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tailscale/tscert v0.0.0-20230124224810-c6dc1f4049b2 // indirect
github.com/urfave/cli v1.22.12 // indirect
go.etcd.io/bbolt v1.3.7 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down

0 comments on commit ec2d9a2

Please sign in to comment.