mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Add initial downloader tests
This commit is contained in:
parent
20bee797c6
commit
6ca6dfee25
2 changed files with 50 additions and 10 deletions
|
|
@ -39,6 +39,7 @@ import (
|
||||||
|
|
||||||
type downloader struct {
|
type downloader struct {
|
||||||
cfg *config
|
cfg *config
|
||||||
|
client *util.Client // Used for testing
|
||||||
keys *crypto.KeyRing
|
keys *crypto.KeyRing
|
||||||
validator csaf.RemoteValidator
|
validator csaf.RemoteValidator
|
||||||
forwarder *forwarder
|
forwarder *forwarder
|
||||||
|
|
@ -123,6 +124,11 @@ func (d *downloader) httpClient() util.Client {
|
||||||
|
|
||||||
client := util.Client(&hClient)
|
client := util.Client(&hClient)
|
||||||
|
|
||||||
|
// Overwrite for testing purposes
|
||||||
|
if client != nil {
|
||||||
|
client = *d.client
|
||||||
|
}
|
||||||
|
|
||||||
// Add extra headers.
|
// Add extra headers.
|
||||||
if len(d.cfg.ExtraHeader) > 0 {
|
if len(d.cfg.ExtraHeader) > 0 {
|
||||||
client = &util.HeaderClient{
|
client = &util.HeaderClient{
|
||||||
|
|
|
||||||
|
|
@ -11,22 +11,56 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/csaf-poc/csaf_distribution/v3/csaf"
|
|
||||||
"github.com/csaf-poc/csaf_distribution/v3/internal/options"
|
"github.com/csaf-poc/csaf_distribution/v3/internal/options"
|
||||||
|
"github.com/csaf-poc/csaf_distribution/v3/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestShaMarking(t *testing.T) {
|
func TestShaMarking(t *testing.T) {
|
||||||
cfg := config{LogLevel: &options.LogLevel{Level: slog.LevelDebug}}
|
tests := []struct {
|
||||||
cfg.prepare()
|
name string
|
||||||
d, err := newDownloader(&cfg)
|
wantSha256 bool
|
||||||
if err != nil {
|
wantSha512 bool
|
||||||
t.Fatalf("could not init downloader: %v", err)
|
}{
|
||||||
|
{
|
||||||
|
name: "want sha256 and sha512",
|
||||||
|
wantSha256: true,
|
||||||
|
wantSha512: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
defer d.close()
|
|
||||||
ctx := context.Background()
|
|
||||||
files := []csaf.AdvisoryFile{}
|
|
||||||
|
|
||||||
d.downloadFiles(ctx, csaf.TLPLabelWhite, files)
|
t.Parallel()
|
||||||
|
for _, testToRun := range tests {
|
||||||
|
test := testToRun
|
||||||
|
t.Run(test.name, func(tt *testing.T) {
|
||||||
|
tt.Parallel()
|
||||||
|
serverURL := ""
|
||||||
|
fs := http.FileServer(http.Dir("../../testdata/simple-rolie-provider"))
|
||||||
|
server := httptest.NewTLSServer(fs)
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
serverURL = server.URL
|
||||||
|
|
||||||
|
hClient := server.Client()
|
||||||
|
client := util.Client(hClient)
|
||||||
|
|
||||||
|
cfg := config{LogLevel: &options.LogLevel{Level: slog.LevelDebug}}
|
||||||
|
cfg.prepare()
|
||||||
|
d, err := newDownloader(&cfg)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("could not init downloader: %v", err)
|
||||||
|
}
|
||||||
|
defer d.close()
|
||||||
|
d.client = &client
|
||||||
|
|
||||||
|
ctx := context.Background()
|
||||||
|
err = d.run(ctx, []string{serverURL + "/provider-metadata.json"})
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("SHA marking: Expected no error, got: %v", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue