Skip to content

Commit

Permalink
Added more tests and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRoettges committed Jul 24, 2024
1 parent c3974c8 commit 9a97a44
Show file tree
Hide file tree
Showing 8 changed files with 528 additions and 344 deletions.
74 changes: 74 additions & 0 deletions core/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
"log"
"strings"
"testing"
"time"
)
Expand Down Expand Up @@ -72,3 +74,75 @@ func TestRetryWith2Retries(t *testing.T) {
assert.Equals(t, 3, retryMessageCount)
assert.Nil(t, err)
}

// Mocking log output
type LogCapture struct {
Messages []string
}

func (lc *LogCapture) Write(p []byte) (n int, err error) {
lc.Messages = append(lc.Messages, strings.TrimSpace(string(p)))
return len(p), nil
}

// Helper function to reset the log capture
func captureLogs() (*LogCapture, func()) {
logCapture := &LogCapture{}
log.SetOutput(logCapture)
return logCapture, func() {
log.SetOutput(nil) // Restore default output after test
}
}

func TestCloseWithErrorHandling_AllSuccessful(t *testing.T) {
logCapture, reset := captureLogs()
defer reset()

CloseWithErrorHandling(
func() error { return nil },
func() error { return nil },
)

assert.Equals(t, 0, len(logCapture.Messages), "expected no log messages")
}

func TestCloseWithErrorHandling_OneError(t *testing.T) {
logCapture, reset := captureLogs()
defer reset()

CloseWithErrorHandling(
func() error { return errors.New("close error 1") },
func() error { return nil },
)

assert.Equals(t, 1, len(logCapture.Messages), "expected a log message")
assert.True(t, strings.Contains(logCapture.Messages[0], "error(s) occurred while closing files: close error 1"))
}

func TestCloseWithErrorHandling_MultipleErrors(t *testing.T) {
logCapture, reset := captureLogs()
defer reset()

CloseWithErrorHandling(
func() error { return errors.New("close error 1") },
func() error { return errors.New("close error 2") },
)

assert.Equals(t, 1, len(logCapture.Messages), "expected a log message")
assert.True(t, strings.Contains(logCapture.Messages[0], "error(s) occurred while closing files: close error 1; close error 2"))
}

func TestCloseWithErrorHandling_MixedSuccessAndErrors(t *testing.T) {
logCapture, reset := captureLogs()
defer reset()

CloseWithErrorHandling(
func() error { return nil },
func() error { return errors.New("close error 1") },
func() error { return nil },
func() error { return errors.New("close error 2") },
)

assert.Equals(t, 1, len(logCapture.Messages), "expected a log message")
assert.True(t, strings.Contains(logCapture.Messages[0], "error(s) occurred while closing files: close error 1; close error 2"))
}
10 changes: 10 additions & 0 deletions crl/crlreader/crlprocessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package crlreader

import "github.com/gr33nbl00d/caddy-revocation-validator/core"

type CRLProcessor interface {
StartUpdateCrl(crlMetaInfo *CRLMetaInfo) error
InsertRevokedCertificate(entry *CRLEntry) error
UpdateExtendedMetaInfo(info *ExtendedCRLMetaInfo) error
UpdateSignatureCertificate(entry *core.CertificateChainEntry) error
}
Loading

0 comments on commit 9a97a44

Please sign in to comment.