github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/executable/executable_test.go (about)

     1  package executable
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    12  )
    13  
    14  func Test_executableAnalyzer_Analyze(t *testing.T) {
    15  	tests := []struct {
    16  		name     string
    17  		filePath string
    18  		want     *analyzer.AnalysisResult
    19  	}{
    20  		{
    21  			name:     "binary",
    22  			filePath: "testdata/binary",
    23  			want: &analyzer.AnalysisResult{
    24  				Digests: map[string]string{
    25  					"testdata/binary": "sha256:9f64a747e1b97f131fabb6b447296c9b6f0201e79fb3c5356e6c77e89b6a806a",
    26  				},
    27  			},
    28  		},
    29  		{
    30  			name:     "text",
    31  			filePath: "testdata/hello.txt",
    32  			want:     nil,
    33  		},
    34  	}
    35  	for _, tt := range tests {
    36  		t.Run(tt.name, func(t *testing.T) {
    37  			f, err := os.Open(tt.filePath)
    38  			require.NoError(t, err)
    39  			defer f.Close()
    40  
    41  			stat, err := f.Stat()
    42  			require.NoError(t, err)
    43  
    44  			a := executableAnalyzer{}
    45  			got, err := a.Analyze(context.Background(), analyzer.AnalysisInput{
    46  				FilePath: tt.filePath,
    47  				Content:  f,
    48  				Info:     stat,
    49  			})
    50  			assert.Equal(t, tt.want, got)
    51  		})
    52  	}
    53  }