1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Extend processor SHA fetching tests

Allow to forbid individual hashes from downloading. This allows to for
testing the behavior, if one of the hashes could not be downloaded.
This commit is contained in:
koplas 2024-12-16 12:23:10 +01:00
parent 9dd4b7fc8d
commit b1a7620763
No known key found for this signature in database
3 changed files with 68 additions and 65 deletions

View file

@ -29,8 +29,11 @@ func getRequirementTestData(t *testing.T, params testutil.ProviderParams, direct
if params.EnableSha512 { if params.EnableSha512 {
path += "sha512-" path += "sha512-"
} }
if params.ForbidHashFetching { if params.ForbidSha256 {
path += "forbid-hash-fetching-" path += "forbid-sha256-"
}
if params.ForbidSha512 {
path += "forbid-sha512-"
} }
if directoryProvider { if directoryProvider {
path += "directory" path += "directory"
@ -64,74 +67,68 @@ func getRequirementTestData(t *testing.T, params testutil.ProviderParams, direct
func TestShaMarking(t *testing.T) { func TestShaMarking(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
directoryProvider bool directoryProvider bool
enableSha256 bool enableSha256 bool
enableSha512 bool enableSha512 bool
forbidHashFetching bool forbidSha256 bool
forbidSha512 bool
}{ }{
{ {
name: "deliver sha256 and sha512", name: "deliver sha256 and sha512",
directoryProvider: false, directoryProvider: false,
enableSha256: true, enableSha256: true,
enableSha512: true, enableSha512: true,
forbidHashFetching: false,
}, },
{ {
name: "enable sha256 and sha512, forbid fetching", name: "enable sha256 and sha512, forbid fetching",
directoryProvider: false, directoryProvider: false,
enableSha256: true, enableSha256: true,
enableSha512: true, enableSha512: true,
forbidHashFetching: true, forbidSha256: true,
forbidSha512: true,
}, },
{ {
name: "only deliver sha256", name: "only deliver sha256",
directoryProvider: false, directoryProvider: false,
enableSha256: true, enableSha256: true,
enableSha512: false, enableSha512: false,
forbidHashFetching: false,
}, },
{ {
name: "only deliver sha512", name: "only deliver sha512",
directoryProvider: false, directoryProvider: false,
enableSha256: false, enableSha256: false,
enableSha512: true, enableSha512: true,
forbidHashFetching: false,
}, },
{ {
name: "deliver sha256 and sha512, directory provider", name: "deliver sha256 and sha512, directory provider",
directoryProvider: true, directoryProvider: true,
enableSha256: true, enableSha256: true,
enableSha512: true, enableSha512: true,
forbidHashFetching: false,
}, },
{ {
name: "only deliver sha256, directory provider", name: "only deliver sha256, directory provider",
directoryProvider: true, directoryProvider: true,
enableSha256: true, enableSha256: true,
enableSha512: false, enableSha512: false,
forbidHashFetching: false,
}, },
{ {
name: "only deliver sha512, directory provider", name: "only deliver sha512, directory provider",
directoryProvider: true, directoryProvider: true,
enableSha256: false, enableSha256: false,
enableSha512: true, enableSha512: true,
forbidHashFetching: false,
}, },
{ {
name: "no hash", name: "no hash",
directoryProvider: false, directoryProvider: false,
enableSha256: false, enableSha256: false,
enableSha512: false, enableSha512: false,
forbidHashFetching: false,
}, },
{ {
name: "no hash, directory provider", name: "no hash, directory provider",
directoryProvider: true, directoryProvider: true,
enableSha256: false, enableSha256: false,
enableSha512: false, enableSha512: false,
forbidHashFetching: false,
}, },
} }
@ -142,10 +139,11 @@ func TestShaMarking(t *testing.T) {
tt.Parallel() tt.Parallel()
serverURL := "" serverURL := ""
params := testutil.ProviderParams{ params := testutil.ProviderParams{
URL: "", URL: "",
EnableSha256: test.enableSha256, EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512, EnableSha512: test.enableSha512,
ForbidHashFetching: test.forbidHashFetching, ForbidSha256: test.forbidSha256,
ForbidSha512: test.forbidSha512,
} }
server := httptest.NewTLSServer(testutil.ProviderHandler(&params, test.directoryProvider)) server := httptest.NewTLSServer(testutil.ProviderHandler(&params, test.directoryProvider))
defer server.Close() defer server.Close()
@ -173,10 +171,11 @@ func TestShaMarking(t *testing.T) {
} }
expected := getRequirementTestData(t, expected := getRequirementTestData(t,
testutil.ProviderParams{ testutil.ProviderParams{
URL: serverURL, URL: serverURL,
EnableSha256: test.enableSha256, EnableSha256: test.enableSha256,
EnableSha512: test.enableSha512, EnableSha512: test.enableSha512,
ForbidHashFetching: test.forbidHashFetching, ForbidSha256: test.forbidSha256,
ForbidSha512: test.forbidSha512,
}, },
test.directoryProvider) test.directoryProvider)
for i, got := range report.Domains[0].Requirements { for i, got := range report.Domains[0].Requirements {

View file

@ -18,10 +18,11 @@ import (
// ProviderParams configures the test provider. // ProviderParams configures the test provider.
type ProviderParams struct { type ProviderParams struct {
URL string URL string
EnableSha256 bool EnableSha256 bool
EnableSha512 bool EnableSha512 bool
ForbidHashFetching bool ForbidSha256 bool
ForbidSha512 bool
} }
// ProviderHandler returns a test provider handler with the specified configuration. // ProviderHandler returns a test provider handler with the specified configuration.
@ -50,7 +51,10 @@ func ProviderHandler(params *ProviderParams, directoryProvider bool) http.Handle
w.Header().Add("Content-Type", "text/html") w.Header().Add("Content-Type", "text/html")
case strings.HasSuffix(path, ".json"): case strings.HasSuffix(path, ".json"):
w.Header().Add("Content-Type", "application/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) w.WriteHeader(http.StatusForbidden)
return return
case strings.HasSuffix(path, ".sha256") && directoryProvider && !params.EnableSha256: case strings.HasSuffix(path, ".sha256") && directoryProvider && !params.EnableSha256: