github.com/anchore/syft@v1.38.2/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/file" 13 "github.com/anchore/syft/syft/internal/unionreader" 14 ) 15 16 func Test_machoHasEntrypoint(t *testing.T) { 17 18 readerForFixture := func(t *testing.T, fixture string) unionreader.UnionReader { 19 t.Helper() 20 f, err := os.Open(filepath.Join("test-fixtures/shared-info", fixture)) 21 require.NoError(t, err) 22 return f 23 } 24 25 tests := []struct { 26 name string 27 fixture string 28 want bool 29 }{ 30 { 31 name: "shared lib", 32 fixture: "bin/libhello.dylib", 33 want: false, 34 }, 35 { 36 name: "application", 37 fixture: "bin/hello_mac", 38 want: true, 39 }, 40 } 41 for _, tt := range tests { 42 t.Run(tt.name, func(t *testing.T) { 43 f, err := macho.NewFile(readerForFixture(t, tt.fixture)) 44 require.NoError(t, err) 45 assert.Equal(t, tt.want, machoHasEntrypoint(f)) 46 }) 47 } 48 } 49 50 func Test_machoHasExports(t *testing.T) { 51 readerForFixture := func(t *testing.T, fixture string) unionreader.UnionReader { 52 t.Helper() 53 f, err := os.Open(filepath.Join("test-fixtures/shared-info", fixture)) 54 require.NoError(t, err) 55 return f 56 } 57 58 tests := []struct { 59 name string 60 fixture string 61 want bool 62 }{ 63 { 64 name: "shared lib", 65 fixture: "bin/libhello.dylib", 66 want: true, 67 }, 68 { 69 name: "application", 70 fixture: "bin/hello_mac", 71 want: false, 72 }, 73 { 74 name: "gcc-amd64-darwin-exec-debug", 75 fixture: "bin/gcc-amd64-darwin-exec-debug", 76 want: false, 77 }, 78 } 79 for _, tt := range tests { 80 t.Run(tt.name, func(t *testing.T) { 81 f, err := macho.NewFile(readerForFixture(t, tt.fixture)) 82 require.NoError(t, err) 83 assert.Equal(t, tt.want, machoHasExports(f)) 84 }) 85 } 86 } 87 88 func Test_machoUniversal(t *testing.T) { 89 readerForFixture := func(t *testing.T, fixture string) unionreader.UnionReader { 90 t.Helper() 91 f, err := os.Open(filepath.Join("test-fixtures/shared-info", fixture)) 92 require.NoError(t, err) 93 return f 94 } 95 96 tests := []struct { 97 name string 98 fixture string 99 want file.Executable 100 }{ 101 { 102 name: "universal lib", 103 fixture: "bin/libhello_universal.dylib", 104 want: file.Executable{HasExports: true, HasEntrypoint: false}, 105 }, 106 { 107 name: "universal application", 108 fixture: "bin/hello_mac_universal", 109 want: file.Executable{HasExports: false, HasEntrypoint: true}, 110 }, 111 } 112 for _, tt := range tests { 113 t.Run(tt.name, func(t *testing.T) { 114 var data file.Executable 115 err := findMachoFeatures(&data, readerForFixture(t, tt.fixture)) 116 require.NoError(t, err) 117 118 assert.Equal(t, tt.want.HasEntrypoint, data.HasEntrypoint) 119 assert.Equal(t, tt.want.HasExports, data.HasExports) 120 }) 121 } 122 }