github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/dotnet/deps/deps_test.go (about) 1 package deps 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 "github.com/devseccon/trivy/pkg/fanal/types" 13 ) 14 15 func Test_depsLibraryAnalyzer_Analyze(t *testing.T) { 16 tests := []struct { 17 name string 18 inputFile string 19 want *analyzer.AnalysisResult 20 wantErr string 21 }{ 22 { 23 name: "happy path", 24 inputFile: "testdata/datacollector.deps.json", 25 want: &analyzer.AnalysisResult{ 26 Applications: []types.Application{ 27 { 28 Type: types.DotNetCore, 29 FilePath: "testdata/datacollector.deps.json", 30 Libraries: types.Packages{ 31 { 32 Name: "Newtonsoft.Json", 33 Version: "9.0.1", 34 Locations: []types.Location{ 35 { 36 StartLine: 8, 37 EndLine: 14, 38 }, 39 }, 40 }, 41 }, 42 }, 43 }, 44 }, 45 }, 46 { 47 name: "sad path", 48 inputFile: "testdata/invalid.txt", 49 wantErr: ".Net Core dependencies analysis error", 50 }, 51 } 52 for _, tt := range tests { 53 t.Run(tt.name, func(t *testing.T) { 54 f, err := os.Open(tt.inputFile) 55 require.NoError(t, err) 56 defer f.Close() 57 58 a := depsLibraryAnalyzer{} 59 ctx := context.Background() 60 got, err := a.Analyze(ctx, analyzer.AnalysisInput{ 61 FilePath: tt.inputFile, 62 Content: f, 63 }) 64 65 if tt.wantErr != "" { 66 require.NotNil(t, err) 67 assert.Contains(t, err.Error(), tt.wantErr) 68 return 69 } 70 71 assert.NoError(t, err) 72 assert.Equal(t, tt.want, got) 73 }) 74 } 75 } 76 77 func Test_depsLibraryAnalyzer_Required(t *testing.T) { 78 tests := []struct { 79 name string 80 filePath string 81 want bool 82 }{ 83 { 84 name: "config", 85 filePath: "test/datacollector.deps.json", 86 want: true, 87 }, 88 { 89 name: "zip", 90 filePath: "test.zip", 91 want: false, 92 }, 93 } 94 for _, tt := range tests { 95 t.Run(tt.name, func(t *testing.T) { 96 a := depsLibraryAnalyzer{} 97 got := a.Required(tt.filePath, nil) 98 assert.Equal(t, tt.want, got) 99 }) 100 } 101 }