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 misconfiguration // is scanned WITHOUT them. Mutant: return `source, removed, nil` -> the tree // still holds them and this fails. func TestTheIacScanOfRecordTreeHasNoSuppressionFile(t *testing.T) { tree := newTree("main.tf", "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{"main.tf"}; !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 TestAnIacTreeWithoutSuppressionIsScannedWhole(t *testing.T) { tree := newTree("main.tf", "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 TestAnIacTreeThatCannotBeListedIsNotAScanOfRecord(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 iacTrivyArgs -> fails; legal direction: // add them outside the scan of record -> the second loop fails. func TestIacTrivyReadsNoConfigFromTheTreeInTheScanOfRecord(t *testing.T) { args := iacTrivyArgs(true) for _, flag := range []string{"--config=", "--ignorefile=", "--secret-config="} { if !contains(args, flag) { t.Errorf("iac 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 iacTrivyArgs(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 }