Skip to content

Commit e84e764

Browse files
leodidoona-agent
andcommitted
Add SLSA verifier interface and mock implementation
- Define VerifierInterface for testable SLSA verification - Implement MockVerifier with configurable behavior for testing - Add helper functions for simulating verification scenarios - Enable comprehensive testing of SLSA verification logic Co-authored-by: Ona <[email protected]>
1 parent 3a706eb commit e84e764

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package slsa
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
// MockVerifier implements VerifierInterface for testing
10+
type MockVerifier struct {
11+
// VerifyFunc allows customizing verification behavior in tests
12+
VerifyFunc func(ctx context.Context, artifactPath, attestationPath string) error
13+
14+
// CallLog tracks verification calls for test assertions
15+
CallLog []VerifyCall
16+
17+
// DefaultResult controls the default verification result when VerifyFunc is nil
18+
DefaultResult error
19+
}
20+
21+
// VerifyCall represents a single verification call for testing
22+
type VerifyCall struct {
23+
ArtifactPath string
24+
AttestationPath string
25+
Context context.Context
26+
}
27+
28+
// NewMockVerifier creates a new mock verifier for testing
29+
func NewMockVerifier() *MockVerifier {
30+
return &MockVerifier{
31+
CallLog: make([]VerifyCall, 0),
32+
}
33+
}
34+
35+
// VerifyArtifact implements the VerifierInterface
36+
func (m *MockVerifier) VerifyArtifact(ctx context.Context, artifactPath, attestationPath string) error {
37+
// Record the call for test assertions
38+
m.CallLog = append(m.CallLog, VerifyCall{
39+
ArtifactPath: artifactPath,
40+
AttestationPath: attestationPath,
41+
Context: ctx,
42+
})
43+
44+
// Use custom verification function if provided
45+
if m.VerifyFunc != nil {
46+
return m.VerifyFunc(ctx, artifactPath, attestationPath)
47+
}
48+
49+
// Return default result
50+
return m.DefaultResult
51+
}
52+
53+
// Reset clears the call log and resets the mock state
54+
func (m *MockVerifier) Reset() {
55+
m.CallLog = make([]VerifyCall, 0)
56+
m.VerifyFunc = nil
57+
m.DefaultResult = nil
58+
}
59+
60+
// SetVerifyResult sets a simple success/failure result for all verifications
61+
func (m *MockVerifier) SetVerifyResult(err error) {
62+
m.DefaultResult = err
63+
}
64+
65+
// SetVerifyFunc sets a custom verification function
66+
func (m *MockVerifier) SetVerifyFunc(fn func(ctx context.Context, artifactPath, attestationPath string) error) {
67+
m.VerifyFunc = fn
68+
}
69+
70+
// GetCallCount returns the number of verification calls made
71+
func (m *MockVerifier) GetCallCount() int {
72+
return len(m.CallLog)
73+
}
74+
75+
// GetLastCall returns the most recent verification call, or nil if no calls were made
76+
func (m *MockVerifier) GetLastCall() *VerifyCall {
77+
if len(m.CallLog) == 0 {
78+
return nil
79+
}
80+
return &m.CallLog[len(m.CallLog)-1]
81+
}
82+
83+
// WasCalledWith checks if the verifier was called with specific paths
84+
func (m *MockVerifier) WasCalledWith(artifactPath, attestationPath string) bool {
85+
for _, call := range m.CallLog {
86+
if call.ArtifactPath == artifactPath && call.AttestationPath == attestationPath {
87+
return true
88+
}
89+
}
90+
return false
91+
}
92+
93+
// SimulateVerificationFailure creates a verification function that fails for specific artifacts
94+
func SimulateVerificationFailure(failingArtifacts ...string) func(ctx context.Context, artifactPath, attestationPath string) error {
95+
return func(ctx context.Context, artifactPath, attestationPath string) error {
96+
for _, failing := range failingArtifacts {
97+
if strings.Contains(artifactPath, failing) {
98+
return fmt.Errorf("mock verification failed for artifact: %s", artifactPath)
99+
}
100+
}
101+
return nil
102+
}
103+
}
104+
105+
// SimulateContextCancellation creates a verification function that respects context cancellation
106+
func SimulateContextCancellation() func(ctx context.Context, artifactPath, attestationPath string) error {
107+
return func(ctx context.Context, artifactPath, attestationPath string) error {
108+
select {
109+
case <-ctx.Done():
110+
return ctx.Err()
111+
default:
112+
return nil
113+
}
114+
}
115+
}

pkg/leeway/cache/slsa/verifier.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ import (
1111
"github.com/slsa-framework/slsa-verifier/v2/options"
1212
)
1313

14+
// VerifierInterface defines the interface for SLSA verification
15+
type VerifierInterface interface {
16+
VerifyArtifact(ctx context.Context, artifactPath, attestationPath string) error
17+
}
18+
1419
// Verifier handles SLSA attestation verification using Go API
1520
type Verifier struct {
1621
sourceURI string

0 commit comments

Comments
 (0)