Files
integraci-dagger-modules/license-scan/scanofrecord_test.go
T

128 lines
3.6 KiB
Go

package main
// Written on the build VM, not run there (owner decision 2026-10-11). Runs
// with `go test ./...` after `dagger develop` has generated internal/dagger.
import (
"context"
"errors"
"path"
"reflect"
"sort"
"strings"
"testing"
)
type memTree struct {
files map[string]bool
globErr error
}
func newTree(paths ...string) memTree {
files := map[string]bool{}
for _, p := range paths {
files[p] = true
}
return memTree{files: files}
}
func (t memTree) Glob(_ context.Context, pattern string) ([]string, error) {
if t.globErr != nil {
return nil, t.globErr
}
var out []string
for p := range t.files {
if name, ok := strings.CutPrefix(pattern, "**/"); ok {
if strings.Contains(p, "/") && path.Base(p) == name {
out = append(out, p)
}
} else if p == pattern {
out = append(out, p)
}
}
sort.Strings(out)
return out, nil
}
func (t memTree) WithoutFiles(paths []string) memTree {
files := map[string]bool{}
for p := range t.files {
files[p] = true
}
for _, p := range paths {
delete(files, p)
}
return memTree{files: files}
}
func (t memTree) list() []string {
var out []string
for p := range t.files {
out = append(out, p)
}
sort.Strings(out)
return out
}
// A change that ships trivy.yaml / .trivyignore to hide its own GPL dependency
// is scanned WITHOUT them. Mutant: return `source, removed, nil` -> the tree
// still holds them and this fails.
func TestTheLicenceScanOfRecordTreeHasNoSuppressionFile(t *testing.T) {
tree := newTree("package.json", "trivy.yaml", ".trivyignore", "svc/.trivyignore.yaml", "trivy-secret.yaml")
got, removed, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles)
if err != nil {
t.Fatal(err)
}
if want := []string{"package.json"}; !reflect.DeepEqual(got.list(), want) {
t.Fatalf("scanned tree = %q, want %q", got.list(), want)
}
if len(removed) != 4 {
t.Fatalf("removed %q, want the four suppression files", removed)
}
}
// Legal direction: a tree without suppression is scanned whole.
func TestALicenceTreeWithoutSuppressionIsScannedWhole(t *testing.T) {
tree := newTree("package.json", "src/trivy.go")
got, removed, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles)
if err != nil || len(removed) != 0 || !reflect.DeepEqual(got.list(), tree.list()) {
t.Fatalf("got %q removed %q err %v", got.list(), removed, err)
}
}
func TestALicenceTreeThatCannotBeListedIsNotAScanOfRecord(t *testing.T) {
tree := memTree{globErr: errors.New("boom")}
if _, _, err := dropSuppression(context.Background(), tree, tree.Glob, memTree.WithoutFiles); err == nil {
t.Fatal("an unlistable tree must be an error, never a whole-tree scan")
}
}
// Mirrors security-scan's TestTrivyReadsNoConfigFromTheTreeInTheScanOfRecord.
// Mutant: drop trustedConfigArgs from licenceArgs -> fails; legal direction:
// add them outside the scan of record -> the second loop fails.
func TestLicenceTrivyReadsNoConfigFromTheTreeInTheScanOfRecord(t *testing.T) {
args := licenceArgs(true)
for _, flag := range []string{"--config=", "--ignorefile=", "--secret-config="} {
if !contains(args, flag) {
t.Errorf("licence scan of record lacks %s: %q", flag, args)
}
}
if args[len(args)-1] != "." {
t.Errorf("the target must stay last: %q", args)
}
for _, a := range licenceArgs(false) {
if strings.HasPrefix(a, "--config=") || strings.HasPrefix(a, "--ignorefile=") {
t.Errorf("outside the scan of record the argv must not change: %q", a)
}
}
}
func contains(xs []string, s string) bool {
for _, x := range xs {
if x == s {
return true
}
}
return false
}