Skip to content

Commit

Permalink
Extend processor SHA fetching tests
Browse files Browse the repository at this point in the history
Allow to forbid individual hashes from downloading. This allows to for
testing the behavior, if one of the hashes could not be downloaded.
  • Loading branch information
koplas committed Dec 16, 2024
1 parent 9dd4b7f commit b1a7620
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 65 deletions.
119 changes: 59 additions & 60 deletions cmd/csaf_checker/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ func getRequirementTestData(t *testing.T, params testutil.ProviderParams, direct
if params.EnableSha512 {
path += "sha512-"
}
if params.ForbidHashFetching {
path += "forbid-hash-fetching-"
if params.ForbidSha256 {
path += "forbid-sha256-"
}
if params.ForbidSha512 {
path += "forbid-sha512-"
}
if directoryProvider {
path += "directory"
Expand Down Expand Up @@ -64,74 +67,68 @@ func getRequirementTestData(t *testing.T, params testutil.ProviderParams, direct

func TestShaMarking(t *testing.T) {
tests := []struct {
name string
directoryProvider bool
enableSha256 bool
enableSha512 bool
forbidHashFetching bool
name string
directoryProvider bool
enableSha256 bool
enableSha512 bool
forbidSha256 bool
forbidSha512 bool
}{
{
name: "deliver sha256 and sha512",
directoryProvider: false,
enableSha256: true,
enableSha512: true,
forbidHashFetching: false,
name: "deliver sha256 and sha512",
directoryProvider: false,
enableSha256: true,
enableSha512: true,
},
{
name: "enable sha256 and sha512, forbid fetching",
directoryProvider: false,
enableSha256: true,
enableSha512: true,
forbidHashFetching: true,
name: "enable sha256 and sha512, forbid fetching",
directoryProvider: false,
enableSha256: true,
enableSha512: true,
forbidSha256: true,
forbidSha512: true,
},
{
name: "only deliver sha256",
directoryProvider: false,
enableSha256: true,
enableSha512: false,
forbidHashFetching: false,
name: "only deliver sha256",
directoryProvider: false,
enableSha256: true,
enableSha512: false,
},
{
name: "only deliver sha512",
directoryProvider: false,
enableSha256: false,
enableSha512: true,
forbidHashFetching: false,
name: "only deliver sha512",
directoryProvider: false,
enableSha256: false,
enableSha512: true,
},
{
name: "deliver sha256 and sha512, directory provider",
directoryProvider: true,
enableSha256: true,
enableSha512: true,
forbidHashFetching: false,
name: "deliver sha256 and sha512, directory provider",
directoryProvider: true,
enableSha256: true,
enableSha512: true,
},
{
name: "only deliver sha256, directory provider",
directoryProvider: true,
enableSha256: true,
enableSha512: false,
forbidHashFetching: false,
name: "only deliver sha256, directory provider",
directoryProvider: true,
enableSha256: true,
enableSha512: false,
},
{
name: "only deliver sha512, directory provider",
directoryProvider: true,
enableSha256: false,
enableSha512: true,
forbidHashFetching: false,
name: "only deliver sha512, directory provider",
directoryProvider: true,
enableSha256: false,
enableSha512: true,
},
{
name: "no hash",
directoryProvider: false,
enableSha256: false,
enableSha512: false,
forbidHashFetching: false,
name: "no hash",
directoryProvider: false,
enableSha256: false,
enableSha512: false,
},
{
name: "no hash, directory provider",
directoryProvider: true,
enableSha256: false,
enableSha512: false,
forbidHashFetching: false,
name: "no hash, directory provider",
directoryProvider: true,
enableSha256: false,
enableSha512: false,
},
}

Expand All @@ -142,10 +139,11 @@ func TestShaMarking(t *testing.T) {
tt.Parallel()
serverURL := ""
params := testutil.ProviderParams{
URL: "",
EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512,
ForbidHashFetching: test.forbidHashFetching,
URL: "",
EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512,
ForbidSha256: test.forbidSha256,
ForbidSha512: test.forbidSha512,
}
server := httptest.NewTLSServer(testutil.ProviderHandler(&params, test.directoryProvider))
defer server.Close()
Expand Down Expand Up @@ -173,10 +171,11 @@ func TestShaMarking(t *testing.T) {
}
expected := getRequirementTestData(t,
testutil.ProviderParams{
URL: serverURL,
EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512,
ForbidHashFetching: test.forbidHashFetching,
URL: serverURL,
EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512,
ForbidSha256: test.forbidSha256,
ForbidSha512: test.forbidSha512,
},
test.directoryProvider)
for i, got := range report.Domains[0].Requirements {
Expand Down
14 changes: 9 additions & 5 deletions internal/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ import (

// ProviderParams configures the test provider.
type ProviderParams struct {
URL string
EnableSha256 bool
EnableSha512 bool
ForbidHashFetching bool
URL string
EnableSha256 bool
EnableSha512 bool
ForbidSha256 bool
ForbidSha512 bool
}

// ProviderHandler returns a test provider handler with the specified configuration.
Expand Down Expand Up @@ -50,7 +51,10 @@ func ProviderHandler(params *ProviderParams, directoryProvider bool) http.Handle
w.Header().Add("Content-Type", "text/html")
case strings.HasSuffix(path, ".json"):
w.Header().Add("Content-Type", "application/json")
case (strings.HasSuffix(path, ".sha256") || strings.HasSuffix(path, ".sha512")) && params.ForbidHashFetching:
case (strings.HasSuffix(path, ".sha256")) && params.ForbidSha256:
w.WriteHeader(http.StatusForbidden)
return
case strings.HasSuffix(path, ".sha512") && params.ForbidSha512:
w.WriteHeader(http.StatusForbidden)
return
case strings.HasSuffix(path, ".sha256") && directoryProvider && !params.EnableSha256:
Expand Down

0 comments on commit b1a7620

Please sign in to comment.