github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/fanal/analyzer/language/java/pom/pom_test.go (about) 1 package pom 2 3 import ( 4 "os" 5 "path/filepath" 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_pomAnalyzer_Analyze(t *testing.T) { 16 tests := []struct { 17 name string 18 inputDir string 19 inputFile string 20 want *analyzer.AnalysisResult 21 wantErr string 22 }{ 23 { 24 name: "happy path", 25 inputFile: "testdata/happy/pom.xml", 26 want: &analyzer.AnalysisResult{ 27 Applications: []types.Application{ 28 { 29 Type: types.Pom, 30 FilePath: "testdata/happy/pom.xml", 31 Libraries: types.Packages{ 32 { 33 ID: "com.example:example:1.0.0", 34 Name: "com.example:example", 35 Version: "1.0.0", 36 Licenses: []string{"Apache-2.0"}, 37 }, 38 }, 39 }, 40 }, 41 }, 42 }, 43 { 44 name: "happy dir path", 45 inputDir: "testdata/happy", 46 inputFile: "pom.xml", 47 want: &analyzer.AnalysisResult{ 48 Applications: []types.Application{ 49 { 50 Type: types.Pom, 51 FilePath: "pom.xml", 52 Libraries: types.Packages{ 53 { 54 ID: "com.example:example:1.0.0", 55 Name: "com.example:example", 56 Version: "1.0.0", 57 Licenses: []string{"Apache-2.0"}, 58 }, 59 }, 60 }, 61 }, 62 }, 63 }, 64 { 65 name: "unsupported requirement", 66 inputFile: "testdata/requirements/pom.xml", 67 want: &analyzer.AnalysisResult{ 68 Applications: []types.Application{ 69 { 70 Type: types.Pom, 71 FilePath: "testdata/requirements/pom.xml", 72 Libraries: types.Packages{ 73 { 74 ID: "com.example:example:2.0.0", 75 Name: "com.example:example", 76 Version: "2.0.0", 77 Licenses: []string{"Apache-2.0"}, 78 }, 79 }, 80 }, 81 }, 82 }, 83 }, 84 { 85 name: "sad path", 86 inputFile: "testdata/broken/pom.xml", 87 wantErr: "xml decode error", 88 }, 89 { 90 name: "sad dir path", 91 inputDir: "testdata/broken", 92 inputFile: "pom.xml", 93 wantErr: "xml decode error", 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 f, err := os.Open(filepath.Join(tt.inputDir, tt.inputFile)) 99 require.NoError(t, err) 100 defer f.Close() 101 102 a := pomAnalyzer{} 103 got, err := a.Analyze(nil, analyzer.AnalysisInput{ 104 Dir: tt.inputDir, 105 FilePath: tt.inputFile, 106 Content: f, 107 }) 108 if tt.wantErr != "" { 109 require.NotNil(t, err) 110 assert.Contains(t, err.Error(), tt.wantErr) 111 return 112 } 113 assert.NoError(t, err) 114 assert.Equal(t, tt.want, got) 115 }) 116 } 117 } 118 119 func Test_pomAnalyzer_Required(t *testing.T) { 120 tests := []struct { 121 name string 122 filePath string 123 want bool 124 }{ 125 { 126 name: "happy", 127 filePath: "test/pom.xml", 128 want: true, 129 }, 130 { 131 name: "no extension", 132 filePath: "test/pom", 133 want: false, 134 }, 135 { 136 name: "json", 137 filePath: "test/pom.json", 138 want: false, 139 }, 140 } 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 a := pomAnalyzer{} 144 got := a.Required(tt.filePath, nil) 145 assert.Equal(t, tt.want, got) 146 }) 147 } 148 }