mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Merge unittest into sha-handling
commit990c74a1a6Merge:86d7ce17824f3bAuthor: koplas <pschwabauer@intevation.de> Date: Fri Nov 22 16:58:46 2024 +0100 Merge branch 'sha-handling' into unittest commit86d7ce13dcMerge:a6807d279b8900Author: koplas <pschwabauer@intevation.de> Date: Fri Nov 22 16:54:45 2024 +0100 Merge branch 'sha-handling' into unittest commit79b89009ddAuthor: koplas <pschwabauer@intevation.de> Date: Fri Nov 22 16:31:56 2024 +0100 Improve hash fetching and logging commita6807d24d6Merge:ddb5518d18d2c3Author: koplas <pschwabauer@intevation.de> Date: Fri Nov 22 16:51:55 2024 +0100 Merge branch 'sha-handling' into unittest commitd18d2c3bf1Author: koplas <pschwabauer@intevation.de> Date: Fri Nov 22 16:31:56 2024 +0100 Improve hash fetching and logging commitddb5518c6dAuthor: koplas <54645365+koplas@users.noreply.github.com> Date: Tue Sep 17 10:45:25 2024 +0200 Extend SHA marking tests commit13c94f4fa0Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 20:46:31 2024 +0200 Use temp directory for downloads commit1819b4896bAuthor: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 20:37:55 2024 +0200 Fix rolie feed commit989e3667baAuthor: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 20:23:22 2024 +0200 Fix provider-metadata.json commit714735d74aAuthor: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 20:08:21 2024 +0200 Implement provider handler commitd488e39947Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 16:26:37 2024 +0200 Add info about gpg key commita9bf9da130Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 16:12:49 2024 +0200 Rename directory testdata commit6ca6dfee25Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 16:01:41 2024 +0200 Add initial downloader tests commit20bee797c6Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 15:58:31 2024 +0200 Fix: Remove unecessary error print commit8e4e508073Author: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 14:50:48 2024 +0200 Extend links test commit3ba29f94deAuthor: koplas <pschwabauer@intevation.de> Date: Mon Sep 16 14:11:14 2024 +0200 Add initial directory feed testdata commitdee55aafd9Author: koplas <54645365+koplas@users.noreply.github.com> Date: Mon Sep 16 10:47:32 2024 +0200 Add initial testdata commitcd9338ae72Author: koplas <54645365+koplas@users.noreply.github.com> Date: Thu Sep 12 15:54:42 2024 +0200 Add initial download unittests
This commit is contained in:
parent
7824f3b48d
commit
ffb4eff933
30 changed files with 1115 additions and 4 deletions
218
cmd/csaf_downloader/downloader_test.go
Normal file
218
cmd/csaf_downloader/downloader_test.go
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
// This file is Free Software under the Apache-2.0 License
|
||||
// without warranty, see README.md and LICENSES/Apache-2.0.txt for details.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
||||
// Software-Engineering: 2023 Intevation GmbH <https://intevation.de>
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"html/template"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/csaf-poc/csaf_distribution/v3/internal/options"
|
||||
"github.com/csaf-poc/csaf_distribution/v3/util"
|
||||
)
|
||||
|
||||
type ProviderParams struct {
|
||||
URL string
|
||||
EnableSha256 bool
|
||||
EnableSha512 bool
|
||||
}
|
||||
|
||||
func ProviderHandler(params *ProviderParams, directoryProvider bool) http.HandlerFunc {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
path := "../../testdata/"
|
||||
if directoryProvider {
|
||||
path += "simple-directory-provider"
|
||||
} else {
|
||||
path += "simple-rolie-provider"
|
||||
}
|
||||
|
||||
path += r.URL.Path
|
||||
|
||||
if strings.HasSuffix(r.URL.Path, "/") {
|
||||
path += "index.html"
|
||||
}
|
||||
|
||||
content, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case strings.HasSuffix(path, ".html"):
|
||||
w.Header().Add("Content-Type", "text/html")
|
||||
case strings.HasSuffix(path, ".json"):
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
case strings.HasSuffix(path, ".sha256") && directoryProvider && !params.EnableSha256:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
case strings.HasSuffix(path, ".sha512") && directoryProvider && !params.EnableSha512:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
default:
|
||||
w.Header().Add("Content-Type", "text/plain")
|
||||
}
|
||||
|
||||
tmplt, err := template.New("base").Parse(string(content))
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
err = tmplt.Execute(w, params)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func checkIfFileExists(path string, t *testing.T) bool {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return true
|
||||
} else if errors.Is(err, os.ErrNotExist) {
|
||||
return false
|
||||
} else {
|
||||
t.Fatalf("Failed to check if file exists: %v", err)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func TestShaMarking(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
directoryProvider bool
|
||||
wantSha256 bool
|
||||
wantSha512 bool
|
||||
enableSha256 bool
|
||||
enableSha512 bool
|
||||
preferredHash hashAlgorithm
|
||||
}{
|
||||
{
|
||||
name: "want sha256 and sha512",
|
||||
directoryProvider: false,
|
||||
wantSha256: true,
|
||||
wantSha512: true,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
},
|
||||
{
|
||||
name: "only want sha256",
|
||||
directoryProvider: false,
|
||||
wantSha256: true,
|
||||
wantSha512: false,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
preferredHash: algSha256,
|
||||
},
|
||||
{
|
||||
name: "only want sha512",
|
||||
directoryProvider: false,
|
||||
wantSha256: false,
|
||||
wantSha512: true,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
preferredHash: algSha512,
|
||||
},
|
||||
{
|
||||
name: "only want sha512",
|
||||
directoryProvider: false,
|
||||
wantSha256: false,
|
||||
wantSha512: true,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
preferredHash: algSha512,
|
||||
},
|
||||
|
||||
{
|
||||
name: "only deliver sha256",
|
||||
directoryProvider: false,
|
||||
wantSha256: true,
|
||||
wantSha512: false,
|
||||
enableSha256: true,
|
||||
enableSha512: false,
|
||||
preferredHash: algSha512,
|
||||
},
|
||||
{
|
||||
name: "only want sha256, directory provider",
|
||||
directoryProvider: true,
|
||||
wantSha256: true,
|
||||
wantSha512: false,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
preferredHash: algSha256,
|
||||
},
|
||||
{
|
||||
name: "only want sha512, directory provider",
|
||||
directoryProvider: true,
|
||||
wantSha256: false,
|
||||
wantSha512: true,
|
||||
enableSha256: true,
|
||||
enableSha512: true,
|
||||
preferredHash: algSha512,
|
||||
},
|
||||
}
|
||||
|
||||
t.Parallel()
|
||||
for _, testToRun := range tests {
|
||||
test := testToRun
|
||||
t.Run(test.name, func(tt *testing.T) {
|
||||
tt.Parallel()
|
||||
serverURL := ""
|
||||
params := ProviderParams{
|
||||
URL: "",
|
||||
EnableSha256: test.enableSha256,
|
||||
EnableSha512: test.enableSha512,
|
||||
}
|
||||
server := httptest.NewTLSServer(ProviderHandler(¶ms, test.directoryProvider))
|
||||
defer server.Close()
|
||||
|
||||
serverURL = server.URL
|
||||
params.URL = server.URL
|
||||
|
||||
hClient := server.Client()
|
||||
client := util.Client(hClient)
|
||||
|
||||
tempDir := t.TempDir()
|
||||
cfg := config{LogLevel: &options.LogLevel{Level: slog.LevelDebug}, Directory: tempDir, PreferredHash: test.preferredHash}
|
||||
err := cfg.prepare()
|
||||
if err != nil {
|
||||
t.Fatalf("SHA marking config failed: %v", err)
|
||||
}
|
||||
d, err := newDownloader(&cfg)
|
||||
if err != nil {
|
||||
t.Fatalf("could not init downloader: %v", err)
|
||||
}
|
||||
d.client = &client
|
||||
|
||||
ctx := context.Background()
|
||||
err = d.run(ctx, []string{serverURL + "/provider-metadata.json"})
|
||||
if err != nil {
|
||||
t.Errorf("SHA marking %v: Expected no error, got: %v", test.name, err)
|
||||
}
|
||||
d.close()
|
||||
|
||||
// Check for downloaded hashes
|
||||
sha256Exists := checkIfFileExists(tempDir+"/white/2020/avendor-advisory-0004.json.sha256", t)
|
||||
sha512Exists := checkIfFileExists(tempDir+"/white/2020/avendor-advisory-0004.json.sha512", t)
|
||||
|
||||
if sha256Exists != test.wantSha256 {
|
||||
t.Errorf("%v: expected sha256 hash present to be %v, got: %v", test.name, test.wantSha256, sha256Exists)
|
||||
}
|
||||
|
||||
if sha512Exists != test.wantSha512 {
|
||||
t.Errorf("%v: expected sha512 hash present to be %v, got: %v", test.name, test.wantSha512, sha512Exists)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue