Skip to content

Commit

Permalink
Merge pull request gocsaf#554 from gocsaf/sha-handling
Browse files Browse the repository at this point in the history
Improve SHA* marking
  • Loading branch information
koplas authored Mar 10, 2025
2 parents ec0c3f9 + 1d1c569 commit 3e16741
Show file tree
Hide file tree
Showing 46 changed files with 3,835 additions and 98 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/gocsaf/csaf/v3/util"
)

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/gocsaf/csaf/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)
}
})
}
}
Loading

0 comments on commit 3e16741

Please sign in to comment.