mirror of
https://github.com/anchore/syft.git
synced 2026-10-11 21:57:22 +02:00
* ci: run the full unit suite on windows via make unit Drops the curated `unit:windows` subset; the windows job now bootstraps like linux and runs `make unit`. Exceptions get added as failures show up. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * ci: skip fixture refresh for windows unit tests binny has no windows release, so the bootstrap action can't install the tools the fixture refresh needs. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * test: skip linux container fixtures when docker can't run them Windows CI runners only run windows containers, so image fixtures go through `testutils.SkipWithoutLinuxContainers`. No behavior change on linux/mac. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: close leaked file handles and posix squashfs paths on windows - output writers are now closed on early returns in `scan`/`convert` (open handles block cleanup on windows) - `newSBOMMultiWriter` used `path.Dir` on host paths, so output subdirectories were never created on windows - squashfs walk paths were built with `filepath.Join` - skip a few posix-only test cases (dir perms, xdg fallback, go-rpmdb ndb handle leak upstream) Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix(golang): resolve go source module dir natively on windows - `parseGoModFile` passed the resolver's posix (`/c/...`) path to the go toolchain, so go source resolution silently found nothing on windows - skip container-built fixtures (go archs, java builds) and golden image tarballs on windows; stereoscope's layer cache filenames contain `:`, which windows rejects Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: keep posix paths posix on windows - `ChrootContext.ToChrootPath` nested volumes (`D:\cwd\C:\root`) when the scan root was on another drive, so the prefix was never trimmed - bitnami, python wheel/egg, and alpm parsers used `filepath` on posix location paths - alpm backup-file lookups silently missed on windows Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * ci: pull the test fixture cache for windows unit tests Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: don't map virtual file tree paths through the host on windows `FiletreeResolver` without a chroot (snaps) ran request paths through `filepath.Abs`, rooting them on the current volume (`/payload.txt` -> `/d/payload.txt`), so snap scans on windows found nothing. Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: match tar entry globs as posix on windows - `matchesAnyGlob` used `doublestar.PathMatch` (host separator) on tar entry names, so tar-wrapped java archives silently produced nothing on windows - test helpers now treat binary snippet keys and `FromFile` locations as posix Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate windows path fix Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * incorporate go-rpmdb close fix Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * close squashfs fixture writers before finalizing - windows dir listings can report a zero size for files still open, so the manifest was packed empty (also adds a temporary windows diagnostic for the binary cataloger) Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * temp: surface windows binary cataloger diagnostics Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * fix: keep directory symlink targets posix on windows - the directory indexer joined symlink targets with native separators, so links in the posix file tree pointed nowhere and globs on link names found nothing - binary fixture logical keys used native separators, so nested snippets were picked as the fixture dir on windows Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * keep addSymlinkToIndex under the funlen limit Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> * drop windows skips covered by the stereoscope and go-rpmdb bumps Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
391 lines
8.5 KiB
Go
391 lines
8.5 KiB
Go
package main
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestReturnsPackageCataloger(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "returns pkg.Cataloger",
|
|
code: `func NewFoo() pkg.Cataloger { return nil }`,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "returns bare Cataloger",
|
|
code: `func NewFoo() Cataloger { return nil }`,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "returns multiple values",
|
|
code: `func NewFoo() (pkg.Cataloger, error) { return nil, nil }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "returns error",
|
|
code: `func NewFoo() error { return nil }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "returns pointer to Cataloger",
|
|
code: `func NewFoo() *pkg.Cataloger { return nil }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "returns string",
|
|
code: `func NewFoo() string { return "" }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "no return type",
|
|
code: `func NewFoo() { }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "returns wrong package Cataloger",
|
|
code: `func NewFoo() other.Cataloger { return nil }`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "returns pkg.OtherType",
|
|
code: `func NewFoo() pkg.OtherType { return nil }`,
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
funcDecl := parseFuncDecl(t, tt.code)
|
|
got := returnsPackageCataloger(funcDecl)
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsGenericNewCatalogerCall(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
want bool
|
|
}{
|
|
{
|
|
name: "generic.NewCataloger call",
|
|
code: `generic.NewCataloger("foo")`,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "generic.NewCataloger with no args",
|
|
code: `generic.NewCataloger()`,
|
|
want: true,
|
|
},
|
|
{
|
|
name: "other.NewCataloger call",
|
|
code: `other.NewCataloger("foo")`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "generic.OtherMethod call",
|
|
code: `generic.OtherMethod("foo")`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "bare NewCataloger call",
|
|
code: `NewCataloger("foo")`,
|
|
want: false,
|
|
},
|
|
{
|
|
name: "nested call",
|
|
code: `foo(generic.NewCataloger("bar"))`,
|
|
want: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
callExpr := parseCallExpr(t, tt.code)
|
|
got := isGenericNewCatalogerCall(callExpr)
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestExtractStringSliceFromExpr(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
want []string
|
|
}{
|
|
{
|
|
name: "strset.New with strings",
|
|
code: `strset.New([]string{"foo", "bar", "baz"})`,
|
|
want: []string{"foo", "bar", "baz"},
|
|
},
|
|
{
|
|
name: "strset.New with single string",
|
|
code: `strset.New([]string{"single"})`,
|
|
want: []string{"single"},
|
|
},
|
|
{
|
|
name: "strset.New with empty slice",
|
|
code: `strset.New([]string{})`,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "other.New with strings",
|
|
code: `other.New([]string{"x", "y"})`,
|
|
want: []string{"x", "y"},
|
|
},
|
|
{
|
|
name: "call with no args",
|
|
code: `strset.New()`,
|
|
want: nil,
|
|
},
|
|
{
|
|
name: "call with non-composite-literal arg",
|
|
code: `strset.New("not a slice")`,
|
|
want: nil,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
callExpr := parseCallExpr(t, tt.code)
|
|
got := extractStringSliceFromExpr(callExpr)
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestSearchConstInDecl(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
constName string
|
|
want string
|
|
}{
|
|
{
|
|
name: "single const",
|
|
code: `const Foo = "bar"`,
|
|
constName: "Foo",
|
|
want: "bar",
|
|
},
|
|
{
|
|
name: "grouped consts - first",
|
|
code: `const (
|
|
Foo = "bar"
|
|
Baz = "qux"
|
|
)`,
|
|
constName: "Foo",
|
|
want: "bar",
|
|
},
|
|
{
|
|
name: "grouped consts - second",
|
|
code: `const (
|
|
Foo = "bar"
|
|
Baz = "qux"
|
|
)`,
|
|
constName: "Baz",
|
|
want: "qux",
|
|
},
|
|
{
|
|
name: "const not found",
|
|
code: `const Foo = "bar"`,
|
|
constName: "Missing",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "var declaration instead of const",
|
|
code: `var Foo = "bar"`,
|
|
constName: "Foo",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "const with non-string value",
|
|
code: `const Foo = 42`,
|
|
constName: "Foo",
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
genDecl := parseConstDecl(t, tt.code)
|
|
got := searchConstInDecl(genDecl, tt.constName)
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetConstValue(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
code string
|
|
constName string
|
|
want string
|
|
}{
|
|
{
|
|
name: "single const match",
|
|
code: `const Foo = "bar"`,
|
|
constName: "Foo",
|
|
want: "bar",
|
|
},
|
|
{
|
|
name: "no match",
|
|
code: `const Foo = "bar"`,
|
|
constName: "NotFoo",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "non-string literal",
|
|
code: `const Foo = 123`,
|
|
constName: "Foo",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "const with complex value",
|
|
code: `const Foo = Bar + "suffix"`,
|
|
constName: "Foo",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "first of multiple in same spec",
|
|
code: `const Foo, Bar = "baz", "qux"`,
|
|
constName: "Foo",
|
|
want: "baz",
|
|
},
|
|
{
|
|
name: "second of multiple in same spec",
|
|
code: `const Foo, Bar = "baz", "qux"`,
|
|
constName: "Bar",
|
|
want: "qux",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
genDecl := parseConstDecl(t, tt.code)
|
|
require.Equal(t, token.CONST, genDecl.Tok)
|
|
require.NotEmpty(t, genDecl.Specs)
|
|
|
|
// getConstValue works on a single ValueSpec, so we need to find the right one
|
|
// in case of grouped constants, each const is its own spec
|
|
var got string
|
|
for _, spec := range genDecl.Specs {
|
|
valueSpec, ok := spec.(*ast.ValueSpec)
|
|
require.True(t, ok)
|
|
|
|
got = getConstValue(valueSpec, tt.constName)
|
|
if got != "" {
|
|
break
|
|
}
|
|
}
|
|
|
|
require.Equal(t, tt.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestResolveImportPath(t *testing.T) {
|
|
const testRepoRoot = "/repo/root"
|
|
|
|
tests := []struct {
|
|
name string
|
|
importPath string
|
|
want string
|
|
}{
|
|
{
|
|
name: "syft pkg cataloger golang",
|
|
importPath: "github.com/anchore/syft/syft/pkg/cataloger/golang",
|
|
want: "/repo/root/syft/pkg/cataloger/golang",
|
|
},
|
|
{
|
|
name: "syft internal capabilities",
|
|
importPath: "github.com/anchore/syft/internal/capabilities",
|
|
want: "/repo/root/internal/capabilities",
|
|
},
|
|
{
|
|
name: "syft root package",
|
|
importPath: "github.com/anchore/syft/syft",
|
|
want: "/repo/root/syft",
|
|
},
|
|
{
|
|
name: "external package",
|
|
importPath: "github.com/other/repo/pkg",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "standard library",
|
|
importPath: "fmt",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "empty import path",
|
|
importPath: "",
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
got := resolveImportPath(tt.importPath, testRepoRoot)
|
|
require.Equal(t, filepath.FromSlash(tt.want), got)
|
|
})
|
|
}
|
|
}
|
|
|
|
// test helper functions
|
|
|
|
// parseFuncDecl parses a function declaration from a code string
|
|
func parseFuncDecl(t *testing.T, code string) *ast.FuncDecl {
|
|
t.Helper()
|
|
fset := token.NewFileSet()
|
|
file, err := parser.ParseFile(fset, "", "package test\n"+code, 0)
|
|
require.NoError(t, err)
|
|
require.Len(t, file.Decls, 1, "expected exactly one declaration")
|
|
funcDecl, ok := file.Decls[0].(*ast.FuncDecl)
|
|
require.True(t, ok, "expected declaration to be a function")
|
|
return funcDecl
|
|
}
|
|
|
|
// parseCallExpr parses a call expression from a code string
|
|
func parseCallExpr(t *testing.T, code string) *ast.CallExpr {
|
|
t.Helper()
|
|
expr, err := parser.ParseExpr(code)
|
|
require.NoError(t, err)
|
|
callExpr, ok := expr.(*ast.CallExpr)
|
|
require.True(t, ok, "expected expression to be a call expression")
|
|
return callExpr
|
|
}
|
|
|
|
// parseCompositeLit parses a composite literal from a code string
|
|
func parseCompositeLit(t *testing.T, code string) *ast.CompositeLit {
|
|
t.Helper()
|
|
expr, err := parser.ParseExpr(code)
|
|
require.NoError(t, err)
|
|
lit, ok := expr.(*ast.CompositeLit)
|
|
require.True(t, ok, "expected expression to be a composite literal")
|
|
return lit
|
|
}
|
|
|
|
// parseConstDecl parses a const declaration from a code string and returns the GenDecl
|
|
func parseConstDecl(t *testing.T, code string) *ast.GenDecl {
|
|
t.Helper()
|
|
fset := token.NewFileSet()
|
|
file, err := parser.ParseFile(fset, "", "package test\n"+code, 0)
|
|
require.NoError(t, err)
|
|
require.Len(t, file.Decls, 1, "expected exactly one declaration")
|
|
genDecl, ok := file.Decls[0].(*ast.GenDecl)
|
|
require.True(t, ok, "expected declaration to be a general declaration")
|
|
return genDecl
|
|
}
|