mirror of
https://github.com/anchore/syft.git
synced 2026-11-01 23:16:30 +01:00
syft-json output wasn't reproducible for nested archives. `AllCoordinates()` comes from map iteration, and the coordinate and location sorters ignored `ArchivePath`, so the same path in two archives (e.g. `META-INF/MANIFEST.MF` in two jars in a war) compared equal and came out in random order. Package locations had the same problem. - `ArchivePath` is now the final tiebreak in `LocationSorter`, `CoordinatesSorter` and the `CoordinateSet.ToSlice` fallback - `Location.Equals` and `Location.String` include `ArchivePath`, which matches `Coordinates`, since the same path in different archives is a different file Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
Go
package file
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/anchore/stereoscope/pkg/file"
|
|
)
|
|
|
|
func TestLocation_ID(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
coordinates Coordinates
|
|
virtualPath string
|
|
ref file.Reference
|
|
}{
|
|
{
|
|
name: "coordinates should match location hash",
|
|
coordinates: Coordinates{
|
|
RealPath: "path!",
|
|
FileSystemID: "filesystem!",
|
|
},
|
|
},
|
|
{
|
|
name: "coordinates should match location hash (with extra fields)",
|
|
coordinates: Coordinates{
|
|
RealPath: "path!",
|
|
FileSystemID: "filesystem!",
|
|
},
|
|
virtualPath: "virtualPath!",
|
|
ref: file.Reference{
|
|
RealPath: "other-real-path!",
|
|
},
|
|
},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
l := Location{
|
|
LocationData: LocationData{
|
|
Coordinates: test.coordinates,
|
|
AccessPath: test.virtualPath,
|
|
ref: test.ref,
|
|
},
|
|
}
|
|
assert.Equal(t, l.ID(), test.coordinates.ID())
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
func TestLocation_archivePathIsIdentity(t *testing.T) {
|
|
a := NewLocationFromCoordinates(Coordinates{RealPath: "META-INF/MANIFEST.MF", ArchivePath: "/a.jar"})
|
|
b := NewLocationFromCoordinates(Coordinates{RealPath: "META-INF/MANIFEST.MF", ArchivePath: "/b.jar"})
|
|
|
|
assert.False(t, a.Equals(b))
|
|
assert.NotEqual(t, a.String(), b.String())
|
|
assert.Contains(t, a.String(), `ArchivePath="/a.jar"`)
|
|
}
|