github.com/anchore/syft@v1.4.2-0.20240516191711-1bec1fc5d397/syft/file/cataloger/executable/macho_test.go (about) 1 package executable 2 3 import ( 4 "debug/macho" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 "github.com/stretchr/testify/require" 11 12 "github.com/anchore/syft/syft/internal/unionreader" 13 ) 14 15 func Test_machoHasEntrypoint(t *testing.T) { 16 17 readerForFixture := func(t *testing.T, fixture string) unionreader.UnionReader { 18 t.Helper() 19 f, err := os.Open(filepath.Join("test-fixtures/shared-info", fixture)) 20 require.NoError(t, err) 21 return f 22 } 23 24 tests := []struct { 25 name string 26 fixture string 27 want bool 28 }{ 29 { 30 name: "shared lib", 31 fixture: "bin/libhello.dylib", 32 want: false, 33 }, 34 { 35 name: "application", 36 fixture: "bin/hello_mac", 37 want: true, 38 }, 39 } 40 for _, tt := range tests { 41 t.Run(tt.name, func(t *testing.T) { 42 f, err := macho.NewFile(readerForFixture(t, tt.fixture)) 43 require.NoError(t, err) 44 assert.Equal(t, tt.want, machoHasEntrypoint(f)) 45 }) 46 } 47 } 48 49 func Test_machoHasExports(t *testing.T) { 50 readerForFixture := func(t *testing.T, fixture string) unionreader.UnionReader { 51 t.Helper() 52 f, err := os.Open(filepath.Join("test-fixtures/shared-info", fixture)) 53 require.NoError(t, err) 54 return f 55 } 56 57 tests := []struct { 58 name string 59 fixture string 60 want bool 61 }{ 62 { 63 name: "shared lib", 64 fixture: "bin/libhello.dylib", 65 want: true, 66 }, 67 { 68 name: "application", 69 fixture: "bin/hello_mac", 70 want: false, 71 }, 72 { 73 name: "gcc-amd64-darwin-exec-debug", 74 fixture: "bin/gcc-amd64-darwin-exec-debug", 75 want: false, 76 }, 77 } 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 f, err := macho.NewFile(readerForFixture(t, tt.fixture)) 81 require.NoError(t, err) 82 assert.Equal(t, tt.want, machoHasExports(f)) 83 }) 84 } 85 }