Skip to content

Commit

Permalink
Merge unittest into sha-handling
Browse files Browse the repository at this point in the history
commit 990c74a
Merge: 86d7ce1 7824f3b
Author: koplas <[email protected]>
Date:   Fri Nov 22 16:58:46 2024 +0100

    Merge branch 'sha-handling' into unittest

commit 86d7ce1
Merge: a6807d2 79b8900
Author: koplas <[email protected]>
Date:   Fri Nov 22 16:54:45 2024 +0100

    Merge branch 'sha-handling' into unittest

commit 79b8900
Author: koplas <[email protected]>
Date:   Fri Nov 22 16:31:56 2024 +0100

    Improve hash fetching and logging

commit a6807d2
Merge: ddb5518 d18d2c3
Author: koplas <[email protected]>
Date:   Fri Nov 22 16:51:55 2024 +0100

    Merge branch 'sha-handling' into unittest

commit d18d2c3
Author: koplas <[email protected]>
Date:   Fri Nov 22 16:31:56 2024 +0100

    Improve hash fetching and logging

commit ddb5518
Author: koplas <[email protected]>
Date:   Tue Sep 17 10:45:25 2024 +0200

    Extend SHA marking tests

commit 13c94f4
Author: koplas <[email protected]>
Date:   Mon Sep 16 20:46:31 2024 +0200

    Use temp directory for downloads

commit 1819b48
Author: koplas <[email protected]>
Date:   Mon Sep 16 20:37:55 2024 +0200

    Fix rolie feed

commit 989e366
Author: koplas <[email protected]>
Date:   Mon Sep 16 20:23:22 2024 +0200

    Fix provider-metadata.json

commit 714735d
Author: koplas <[email protected]>
Date:   Mon Sep 16 20:08:21 2024 +0200

    Implement provider handler

commit d488e39
Author: koplas <[email protected]>
Date:   Mon Sep 16 16:26:37 2024 +0200

    Add info about gpg key

commit a9bf9da
Author: koplas <[email protected]>
Date:   Mon Sep 16 16:12:49 2024 +0200

    Rename directory testdata

commit 6ca6dfe
Author: koplas <[email protected]>
Date:   Mon Sep 16 16:01:41 2024 +0200

    Add initial downloader tests

commit 20bee79
Author: koplas <[email protected]>
Date:   Mon Sep 16 15:58:31 2024 +0200

    Fix: Remove unecessary error print

commit 8e4e508
Author: koplas <[email protected]>
Date:   Mon Sep 16 14:50:48 2024 +0200

    Extend links test

commit 3ba29f9
Author: koplas <[email protected]>
Date:   Mon Sep 16 14:11:14 2024 +0200

    Add initial directory feed testdata

commit dee55aa
Author: koplas <[email protected]>
Date:   Mon Sep 16 10:47:32 2024 +0200

    Add initial testdata

commit cd9338a
Author: koplas <[email protected]>
Date:   Thu Sep 12 15:54:42 2024 +0200

    Add initial download unittests
  • Loading branch information
koplas committed Nov 27, 2024
1 parent 7824f3b commit ffb4eff
Show file tree
Hide file tree
Showing 30 changed files with 1,115 additions and 4 deletions.
67 changes: 67 additions & 0 deletions cmd/csaf_aggregator/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// This file is Free Software under the MIT License
// without warranty, see README.md and LICENSES/MIT.txt for details.
//
// SPDX-License-Identifier: MIT
//
// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>

package main

import (
"io"
"net/http"
"net/http/httptest"
"testing"

"github.com/csaf-poc/csaf_distribution/v3/util"

Check failure on line 17 in cmd/csaf_aggregator/client_test.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/csaf-poc/csaf_distribution/v3/util; to add it:

Check failure on line 17 in cmd/csaf_aggregator/client_test.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/csaf-poc/csaf_distribution/v3/util; to add it:

Check failure on line 17 in cmd/csaf_aggregator/client_test.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/csaf-poc/csaf_distribution/v3/util; to add it:

Check failure on line 17 in cmd/csaf_aggregator/client_test.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/csaf-poc/csaf_distribution/v3/util; to add it:
)

func Test_downloadJSON(t *testing.T) {
tests := []struct {
name string
statusCode int
contentType string
wantErr error
}{
{
name: "status ok, application/json",
statusCode: http.StatusOK,
contentType: "application/json",
wantErr: nil,
},
{
name: "status found, application/json",
statusCode: http.StatusFound,
contentType: "application/json",
wantErr: errNotFound,
},
{
name: "status ok, application/xml",
statusCode: http.StatusOK,
contentType: "application/xml",
wantErr: errNotFound,
},
}

t.Parallel()
for _, testToRun := range tests {
test := testToRun
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
found := func(r io.Reader) error {
return nil
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", test.contentType)
w.WriteHeader(test.statusCode)
}))
defer server.Close()
hClient := http.Client{}
client := util.Client(&hClient)
if gotErr := downloadJSON(client, server.URL, found); gotErr != test.wantErr {
t.Errorf("downloadJSON: Expected %q but got %q.", test.wantErr, gotErr)
}
})
}
}
80 changes: 79 additions & 1 deletion cmd/csaf_checker/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ package main

import (
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/csaf-poc/csaf_distribution/v3/util"
)

const page0 = `<html>
Expand All @@ -31,7 +35,6 @@ const page0 = `<html>
</html>`

func TestLinksOnPage(t *testing.T) {

var links []string

err := linksOnPage(
Expand All @@ -58,3 +61,78 @@ func TestLinksOnPage(t *testing.T) {
}
}
}

func Test_listed(t *testing.T) {
tests := []struct {
name string
badDirs util.Set[string]
path string
want bool
}{
{
name: "listed path",
badDirs: util.Set[string]{},
path: "/white/avendor-advisory-0004.json",
want: true,
},
{
name: "badDirs contains path",
badDirs: util.Set[string]{"/white/": {}},
path: "/white/avendor-advisory-0004.json",
want: false,
},
{
name: "not found",
badDirs: util.Set[string]{},
path: "/not-found/resource.json",
want: false,
},
{
name: "badDirs does not contain path",
badDirs: util.Set[string]{"/bad-dir/": {}},
path: "/white/avendor-advisory-0004.json",
want: true,
},
{
name: "unlisted path",
badDirs: util.Set[string]{},
path: "/white/avendor-advisory-0004-not-listed.json",
want: false,
},
}

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-directory-provider"))
server := httptest.NewTLSServer(fs)
defer server.Close()

serverURL = server.URL

hClient := server.Client()
client := util.Client(hClient)

pgs := pages{}
cfg := config{RemoteValidator: "", RemoteValidatorCache: ""}
p, err := newProcessor(&cfg)
if err != nil {
t.Error(err)
}
p.client = client

badDirs := util.Set[string]{}
for dir := range test.badDirs {
badDirs.Add(serverURL + dir)
}

got, _ := pgs.listed(serverURL+test.path, p, badDirs)
if got != test.want {
t.Errorf("%q: Expected %t but got %t.", test.name, test.want, got)
}
})
}
}
4 changes: 2 additions & 2 deletions cmd/csaf_downloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const (
type hashAlgorithm string

const (
algSha256 = hashAlgorithm("SHA256")
algSha512 = hashAlgorithm("SHA512")
algSha256 = hashAlgorithm("sha256")
algSha512 = hashAlgorithm("sha512")
)

type config struct {
Expand Down
6 changes: 6 additions & 0 deletions cmd/csaf_downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type hashFetchInfo struct {

type downloader struct {
cfg *config
client *util.Client // Used for testing
keys *crypto.KeyRing
validator csaf.RemoteValidator
forwarder *forwarder
Expand Down Expand Up @@ -131,6 +132,11 @@ func (d *downloader) httpClient() util.Client {

client := util.Client(&hClient)

// Overwrite for testing purposes
if client != nil {
client = *d.client
}

// Add extra headers.
if len(d.cfg.ExtraHeader) > 0 {
client = &util.HeaderClient{
Expand Down
Loading

0 comments on commit ffb4eff

Please sign in to comment.