mirror of
https://github.com/anchore/syft.git
synced 2026-10-31 14:36:33 +01:00
account for repoDigests data in power-user json testing
Signed-off-by: Alex Goodman <alex.goodman@anchore.com>
This commit is contained in:
@@ -10,33 +10,11 @@ import (
|
||||
"github.com/anchore/syft/syft/source"
|
||||
)
|
||||
|
||||
var supportedHashAlgorithms = make(map[string]crypto.Hash)
|
||||
|
||||
type DigestsCataloger struct {
|
||||
hashes []crypto.Hash
|
||||
}
|
||||
|
||||
func init() {
|
||||
for _, h := range []crypto.Hash{
|
||||
crypto.MD5,
|
||||
crypto.SHA1,
|
||||
crypto.SHA256,
|
||||
} {
|
||||
supportedHashAlgorithms[cleanAlgorithmName(h.String())] = h
|
||||
}
|
||||
}
|
||||
|
||||
func NewDigestsCataloger(hashAlgorithms []string) (*DigestsCataloger, error) {
|
||||
var hashes []crypto.Hash
|
||||
for _, hashStr := range hashAlgorithms {
|
||||
name := cleanAlgorithmName(hashStr)
|
||||
hashObj, ok := supportedHashAlgorithms[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unsupported hash algorithm: %s", hashStr)
|
||||
}
|
||||
hashes = append(hashes, hashObj)
|
||||
}
|
||||
|
||||
func NewDigestsCataloger(hashes []crypto.Hash) (*DigestsCataloger, error) {
|
||||
return &DigestsCataloger{
|
||||
hashes: hashes,
|
||||
}, nil
|
||||
@@ -84,7 +62,7 @@ func (i *DigestsCataloger) catalogLocation(resolver source.FileResolver, locatio
|
||||
// file type but a body is still allowed.
|
||||
for idx, hasher := range hashers {
|
||||
result[idx] = Digest{
|
||||
Algorithm: cleanAlgorithmName(i.hashes[idx].String()),
|
||||
Algorithm: DigestAlgorithmName(i.hashes[idx]),
|
||||
Value: fmt.Sprintf("%+x", hasher.Sum(nil)),
|
||||
}
|
||||
}
|
||||
@@ -92,7 +70,11 @@ func (i *DigestsCataloger) catalogLocation(resolver source.FileResolver, locatio
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func cleanAlgorithmName(name string) string {
|
||||
func DigestAlgorithmName(hash crypto.Hash) string {
|
||||
return CleanDigestAlgorithmName(hash.String())
|
||||
}
|
||||
|
||||
func CleanDigestAlgorithmName(name string) string {
|
||||
lower := strings.ToLower(name)
|
||||
return strings.Replace(lower, "-", "", -1)
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ func testDigests(t testing.TB, files []string, hashes ...crypto.Hash) map[source
|
||||
h := hash.New()
|
||||
h.Write(b)
|
||||
digests[source.NewLocation(f)] = append(digests[source.NewLocation(f)], Digest{
|
||||
Algorithm: cleanAlgorithmName(hash.String()),
|
||||
Algorithm: CleanDigestAlgorithmName(hash.String()),
|
||||
Value: fmt.Sprintf("%x", h.Sum(nil)),
|
||||
})
|
||||
}
|
||||
@@ -46,40 +46,27 @@ 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
|
||||
name string
|
||||
digests []crypto.Hash
|
||||
files []string
|
||||
expected map[source.Location][]Digest
|
||||
catalogErr bool
|
||||
}{
|
||||
{
|
||||
name: "bad algorithm",
|
||||
algorithms: []string{"sha-nothing"},
|
||||
files: regularFiles,
|
||||
constructorErr: true,
|
||||
name: "md5",
|
||||
digests: []crypto.Hash{crypto.MD5},
|
||||
files: regularFiles,
|
||||
expected: testDigests(t, regularFiles, crypto.MD5),
|
||||
},
|
||||
{
|
||||
name: "unsupported algorithm",
|
||||
algorithms: []string{"sha512"},
|
||||
files: regularFiles,
|
||||
constructorErr: true,
|
||||
},
|
||||
{
|
||||
name: "md5",
|
||||
algorithms: []string{"md5"},
|
||||
files: regularFiles,
|
||||
expected: testDigests(t, regularFiles, crypto.MD5),
|
||||
},
|
||||
{
|
||||
name: "md5-sha1-sha256",
|
||||
algorithms: []string{"md5", "sha1", "sha256"},
|
||||
files: regularFiles,
|
||||
expected: testDigests(t, regularFiles, crypto.MD5, crypto.SHA1, crypto.SHA256),
|
||||
name: "md5-sha1-sha256",
|
||||
digests: []crypto.Hash{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"},
|
||||
digests: []crypto.Hash{crypto.MD5},
|
||||
files: []string{"test-fixtures/last"},
|
||||
catalogErr: true,
|
||||
},
|
||||
@@ -87,13 +74,9 @@ func TestDigestsCataloger_SimpleContents(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
c, err := NewDigestsCataloger(test.algorithms)
|
||||
if err != nil && !test.constructorErr {
|
||||
t.Fatalf("could not create cataloger (but should have been able to): %+v", err)
|
||||
} else if err == nil && test.constructorErr {
|
||||
t.Fatalf("expected constructor error but did not get one")
|
||||
} else if test.constructorErr && err != nil {
|
||||
return
|
||||
c, err := NewDigestsCataloger(test.digests)
|
||||
if err != nil {
|
||||
t.Fatalf("could not create cataloger: %+v", err)
|
||||
}
|
||||
|
||||
resolver := source.NewMockResolverForPaths(test.files...)
|
||||
@@ -161,7 +144,7 @@ func TestDigestsCataloger_MixFileTypes(t *testing.T) {
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.path, func(t *testing.T) {
|
||||
c, err := NewDigestsCataloger([]string{"md5"})
|
||||
c, err := NewDigestsCataloger([]crypto.Hash{crypto.MD5})
|
||||
if err != nil {
|
||||
t.Fatalf("unable to get cataloger: %+v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user