mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 18:15:42 +01:00
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
138 lines
2.9 KiB
Go
138 lines
2.9 KiB
Go
// 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: 2022 German Federal Office for Information Security (BSI) <https://www.bsi.bund.de>
|
|
// Software-Engineering: 2022 Intevation GmbH <https://intevation.de>
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/csaf-poc/csaf_distribution/v3/util"
|
|
)
|
|
|
|
const page0 = `<html>
|
|
<body>
|
|
<a href="not-a-json">Not a JSON</a>
|
|
<a href="link0.json">link0</a>
|
|
<ol>
|
|
<li><a href="link1.json">link1</a></li>
|
|
<li><a href="link2.json">link1</a></li>
|
|
</ol>
|
|
<p>
|
|
<div>
|
|
<li><a href="link3.json">link1</a></li>
|
|
</div>
|
|
<p>
|
|
</body>
|
|
</html>`
|
|
|
|
func TestLinksOnPage(t *testing.T) {
|
|
var links []string
|
|
|
|
err := linksOnPage(
|
|
strings.NewReader(page0),
|
|
func(s string) error {
|
|
if strings.HasSuffix(s, ".json") {
|
|
links = append(links, s)
|
|
}
|
|
return nil
|
|
},
|
|
)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if l := len(links); l != 4 {
|
|
t.Fatalf("Expected 4 links, go %d\n", l)
|
|
}
|
|
|
|
for i, link := range links {
|
|
href := fmt.Sprintf("link%d.json", i)
|
|
if href != link {
|
|
t.Fatalf("Expected link '%s', got '%s'\n", href, link)
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|