github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/rust/binary/binary_test.go (about)

     1  package binary
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/samber/lo"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/devseccon/trivy/pkg/fanal/analyzer"
    14  	"github.com/devseccon/trivy/pkg/fanal/types"
    15  )
    16  
    17  func Test_rustBinaryLibraryAnalyzer_Analyze(t *testing.T) {
    18  	tests := []struct {
    19  		name      string
    20  		inputFile string
    21  		want      *analyzer.AnalysisResult
    22  	}{
    23  		{
    24  			name:      "happy path",
    25  			inputFile: "testdata/executable_rust",
    26  			want: &analyzer.AnalysisResult{
    27  				Applications: []types.Application{
    28  					{
    29  						Type:     types.RustBinary,
    30  						FilePath: "testdata/executable_rust",
    31  						Libraries: types.Packages{
    32  							{
    33  								ID:        "crate_with_features@0.1.0",
    34  								Name:      "crate_with_features",
    35  								Version:   "0.1.0",
    36  								DependsOn: []string{"library_crate@0.1.0"},
    37  							},
    38  							{
    39  								ID:       "library_crate@0.1.0",
    40  								Name:     "library_crate",
    41  								Version:  "0.1.0",
    42  								Indirect: true,
    43  							},
    44  						},
    45  					},
    46  				},
    47  			},
    48  		},
    49  		{
    50  			name:      "not rust binary",
    51  			inputFile: "testdata/executable_bash",
    52  		},
    53  		{
    54  			name:      "broken elf",
    55  			inputFile: "testdata/broken_elf",
    56  		},
    57  	}
    58  	for _, tt := range tests {
    59  		t.Run(tt.name, func(t *testing.T) {
    60  			f, err := os.Open(tt.inputFile)
    61  			require.NoError(t, err)
    62  			defer f.Close()
    63  
    64  			a := rustBinaryLibraryAnalyzer{}
    65  			ctx := context.Background()
    66  			got, err := a.Analyze(ctx, analyzer.AnalysisInput{
    67  				FilePath: tt.inputFile,
    68  				Content:  f,
    69  			})
    70  
    71  			assert.NoError(t, err)
    72  			assert.Equal(t, tt.want, got)
    73  		})
    74  	}
    75  }
    76  
    77  func Test_rustBinaryLibraryAnalyzer_Required(t *testing.T) {
    78  	tests := []struct {
    79  		name     string
    80  		filePath string
    81  		want     bool
    82  	}{
    83  		{
    84  			name:     "executable file",
    85  			filePath: lo.Ternary(runtime.GOOS == "windows", "testdata/binary.exe", "testdata/0755"),
    86  			want:     true,
    87  		},
    88  		{
    89  			name:     "file perm 0644",
    90  			filePath: "testdata/0644",
    91  			want:     false,
    92  		},
    93  		{
    94  			name:     "symlink",
    95  			filePath: "testdata/symlink",
    96  			want:     false,
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			a := rustBinaryLibraryAnalyzer{}
   102  			fileInfo, err := os.Lstat(tt.filePath)
   103  			require.NoError(t, err)
   104  			got := a.Required(tt.filePath, fileInfo)
   105  			assert.Equal(t, tt.want, got, fileInfo.Mode().Perm())
   106  		})
   107  	}
   108  }