github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/cmd/syft/internal/test/integration/files_test.go (about) 1 package integration 2 3 import ( 4 "crypto" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/google/go-cmp/cmp" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 13 "github.com/anchore/clio" 14 stereoscopeFile "github.com/anchore/stereoscope/pkg/file" 15 "github.com/anchore/syft/cmd/syft/internal/options" 16 "github.com/anchore/syft/syft/cataloging/filecataloging" 17 "github.com/anchore/syft/syft/file" 18 "github.com/anchore/syft/syft/file/cataloger/filecontent" 19 "github.com/anchore/syft/syft/sbom" 20 ) 21 22 func TestFileCataloging_Default(t *testing.T) { 23 cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{}) 24 cfg = cfg.WithFilesConfig(filecataloging.DefaultConfig()) 25 sbom, _ := catalogDirectoryWithConfig(t, "test-fixtures/files", cfg) 26 27 var metadata map[file.Coordinates]file.Metadata 28 29 var digests map[file.Coordinates][]file.Digest 30 31 var contents map[file.Coordinates]string 32 33 assertFileData(t, metadata, digests, contents, sbom) 34 } 35 36 func TestFileCataloging_AllFiles(t *testing.T) { 37 cfg := options.DefaultCatalog().ToSBOMConfig(clio.Identification{}) 38 cfg = cfg.WithFilesConfig(filecataloging.Config{ 39 Selection: file.AllFilesSelection, 40 Hashers: []crypto.Hash{ 41 crypto.SHA256, 42 }, 43 Content: filecontent.Config{ 44 // this is enough to potentially capture a/file, a-small-file, a-symlink-to-a-small-file, and a-symlink-to-file 45 // but the size of a/file will cause it to be filtered, and the symlinks will not be included since 46 // they are not regular files 47 Globs: []string{"**/*file"}, 48 SkipFilesAboveSize: 30, 49 }, 50 }) 51 sbom, _ := catalogDirectoryWithConfig(t, "test-fixtures/files", cfg) 52 53 pwd, err := os.Getwd() 54 require.NoError(t, err) 55 56 testPath := func(path string) string { 57 return filepath.Join(pwd, "test-fixtures/files", path) 58 } 59 60 metadata := map[file.Coordinates]file.Metadata{ 61 {RealPath: ""}: { 62 Path: testPath(""), 63 Type: stereoscopeFile.TypeDirectory, 64 }, 65 {RealPath: "/somewhere"}: { 66 Path: testPath("/somewhere"), 67 Type: stereoscopeFile.TypeDirectory, 68 }, 69 {RealPath: "/somewhere/there"}: { 70 Path: testPath("/somewhere/there"), 71 Type: stereoscopeFile.TypeDirectory, 72 }, 73 {RealPath: "/somewhere/there/is"}: { 74 Path: testPath("/somewhere/there/is"), 75 Type: stereoscopeFile.TypeDirectory, 76 }, 77 {RealPath: "/somewhere/there/is/a"}: { 78 Path: testPath("/somewhere/there/is/a"), 79 Type: stereoscopeFile.TypeDirectory, 80 }, 81 {RealPath: "/somewhere/there/is/a-small-file"}: { 82 Path: testPath("/somewhere/there/is/a-small-file"), 83 Type: stereoscopeFile.TypeRegular, 84 MIMEType: "text/plain", 85 }, 86 {RealPath: "/somewhere/there/is/a-symlink-to-a-small-file"}: { 87 Path: testPath("/somewhere/there/is/a-symlink-to-a-small-file"), 88 LinkDestination: testPath("/somewhere/there/is/a-small-file"), 89 Type: stereoscopeFile.TypeSymLink, 90 }, 91 {RealPath: "/somewhere/there/is/a-symlink-to-file"}: { 92 Path: testPath("/somewhere/there/is/a-symlink-to-file"), 93 LinkDestination: testPath("/somewhere/there/is/a/file"), 94 Type: stereoscopeFile.TypeSymLink, 95 }, 96 {RealPath: "/somewhere/there/is/a/file"}: { 97 Path: testPath("/somewhere/there/is/a/file"), 98 Type: stereoscopeFile.TypeRegular, 99 MIMEType: "text/plain", 100 }, 101 } 102 103 digests := map[file.Coordinates][]file.Digest{ 104 {RealPath: "/somewhere/there/is/a-small-file"}: { 105 file.Digest{Algorithm: "sha256", Value: "672c23470e4ce99cf270bb63ae66ad2b8a80aa19090c40e59fbb1229a4ab661a"}, 106 }, 107 {RealPath: "/somewhere/there/is/a/file"}: { 108 file.Digest{Algorithm: "sha256", Value: "00dac26d6d94353ac0d92bb9640cba76f82f5ca8707bb845ecdc574bd002348e"}, 109 }, 110 } 111 112 contents := map[file.Coordinates]string{ 113 {RealPath: "/somewhere/there/is/a-small-file"}: "c29tZSBjb250ZW50cyE=", 114 } 115 116 assertFileData(t, metadata, digests, contents, sbom) 117 118 } 119 120 func assertFileData(t testing.TB, metadata map[file.Coordinates]file.Metadata, digests map[file.Coordinates][]file.Digest, contents map[file.Coordinates]string, sbom sbom.SBOM) { 121 metadataCompareOpts := cmp.Options{ 122 cmp.Comparer(func(x, y file.Metadata) bool { 123 if x.Path != y.Path { 124 t.Logf("path mismatch: %s != %s", x.Path, y.Path) 125 return false 126 } 127 128 if x.Type != y.Type { 129 t.Logf("type mismatch: %s != %s", x.Type, y.Type) 130 return false 131 } 132 133 if x.LinkDestination != y.LinkDestination { 134 t.Logf("link destination mismatch: %s != %s", x.LinkDestination, y.LinkDestination) 135 return false 136 } 137 138 if x.MIMEType != y.MIMEType { 139 t.Logf("mime type mismatch: %s != %s", x.MIMEType, y.MIMEType) 140 return false 141 } 142 143 return true 144 }), 145 } 146 147 if d := cmp.Diff(metadata, sbom.Artifacts.FileMetadata, metadataCompareOpts...); d != "" { 148 t.Errorf("unexpected metadata (-want +got):\n%s", d) 149 } 150 151 assert.Equal(t, digests, sbom.Artifacts.FileDigests, "different digests detected") 152 assert.Equal(t, contents, sbom.Artifacts.FileContents, "different contents detected") 153 154 }