mirror of
https://github.com/anchore/syft.git
synced 2026-11-04 08:26:38 +01:00
allow file metadata digests to be optional + add link destination
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -7,6 +7,10 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/file"
|
||||
|
||||
"github.com/anchore/stereoscope/pkg/imagetest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/anchore/syft/syft/source"
|
||||
@@ -38,12 +42,13 @@ func testDigests(t testing.TB, files []string, hashes ...crypto.Hash) map[source
|
||||
return digests
|
||||
}
|
||||
|
||||
func TestDigestsCataloger(t *testing.T) {
|
||||
files := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
|
||||
func TestDigestsCataloger_SimpleContents(t *testing.T) {
|
||||
regularFiles := []string{"test-fixtures/last/path.txt", "test-fixtures/another-path.txt", "test-fixtures/a-path.txt"}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
algorithms []string
|
||||
files []string
|
||||
expected map[source.Location][]Digest
|
||||
constructorErr bool
|
||||
catalogErr bool
|
||||
@@ -51,22 +56,32 @@ func TestDigestsCataloger(t *testing.T) {
|
||||
{
|
||||
name: "bad algorithm",
|
||||
algorithms: []string{"sha-nothing"},
|
||||
files: regularFiles,
|
||||
constructorErr: true,
|
||||
},
|
||||
{
|
||||
name: "unsupported algorithm",
|
||||
algorithms: []string{"sha512"},
|
||||
files: regularFiles,
|
||||
constructorErr: true,
|
||||
},
|
||||
{
|
||||
name: "md5-sha1-sha256",
|
||||
name: "md5",
|
||||
algorithms: []string{"md5"},
|
||||
expected: testDigests(t, files, crypto.MD5),
|
||||
files: regularFiles,
|
||||
expected: testDigests(t, regularFiles, crypto.MD5),
|
||||
},
|
||||
{
|
||||
name: "md5-sha1-sha256",
|
||||
algorithms: []string{"md5", "sha1", "sha256"},
|
||||
expected: testDigests(t, files, crypto.MD5, crypto.SHA1, crypto.SHA256),
|
||||
files: regularFiles,
|
||||
expected: testDigests(t, regularFiles, crypto.MD5, crypto.SHA1, crypto.SHA256),
|
||||
},
|
||||
{
|
||||
name: "directory returns error",
|
||||
algorithms: []string{"md5"},
|
||||
files: []string{"test-fixtures/last"},
|
||||
catalogErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -81,7 +96,7 @@ func TestDigestsCataloger(t *testing.T) {
|
||||
return
|
||||
}
|
||||
|
||||
resolver := source.NewMockResolverForPaths(files...)
|
||||
resolver := source.NewMockResolverForPaths(test.files...)
|
||||
actual, err := c.Catalog(resolver)
|
||||
if err != nil && !test.catalogErr {
|
||||
t.Fatalf("could not catalog (but should have been able to): %+v", err)
|
||||
@@ -96,3 +111,80 @@ func TestDigestsCataloger(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDigestsCataloger_MixFileTypes(t *testing.T) {
|
||||
testImage := "image-file-type-mix"
|
||||
|
||||
if *updateImageGoldenFiles {
|
||||
imagetest.UpdateGoldenFixtureImage(t, testImage)
|
||||
}
|
||||
|
||||
img := imagetest.GetGoldenFixtureImage(t, testImage)
|
||||
|
||||
src, err := source.NewFromImage(img, "---")
|
||||
if err != nil {
|
||||
t.Fatalf("could not create source: %+v", err)
|
||||
}
|
||||
|
||||
resolver, err := src.FileResolver(source.SquashedScope)
|
||||
if err != nil {
|
||||
t.Fatalf("could not create resolver: %+v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
path string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
path: "/file-1.txt",
|
||||
expected: "888c139e550867814eb7c33b84d76e4d",
|
||||
},
|
||||
{
|
||||
path: "/hardlink-1",
|
||||
},
|
||||
{
|
||||
path: "/symlink-1",
|
||||
},
|
||||
{
|
||||
path: "/char-device-1",
|
||||
},
|
||||
{
|
||||
path: "/block-device-1",
|
||||
},
|
||||
{
|
||||
path: "/fifo-1",
|
||||
},
|
||||
{
|
||||
path: "/bin",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.path, func(t *testing.T) {
|
||||
c, err := NewDigestsCataloger([]string{"md5"})
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get cataloger: %+v", err)
|
||||
}
|
||||
|
||||
actual, err := c.Catalog(resolver)
|
||||
if err != nil {
|
||||
t.Fatalf("could not catalog: %+v", err)
|
||||
}
|
||||
|
||||
_, ref, err := img.SquashedTree().File(file.Path(test.path))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get file=%q : %+v", test.path, err)
|
||||
}
|
||||
l := source.NewLocationFromImage(test.path, *ref, img)
|
||||
|
||||
if len(actual[l]) == 0 {
|
||||
if test.expected != "" {
|
||||
t.Fatalf("no digest found, but expected one")
|
||||
}
|
||||
|
||||
} else {
|
||||
assert.Equal(t, actual[l][0].Value, test.expected, "mismatched digests")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user