github.com/devseccon/trivy@v0.47.1-0.20231123133102-bd902a0bd996/pkg/licensing/scanner_test.go (about) 1 package licensing_test 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/devseccon/trivy/pkg/fanal/types" 9 "github.com/devseccon/trivy/pkg/licensing" 10 ) 11 12 func TestScanner_Scan(t *testing.T) { 13 tests := []struct { 14 name string 15 categories map[types.LicenseCategory][]string 16 licenseName string 17 wantCategory types.LicenseCategory 18 wantSeverity string 19 }{ 20 { 21 name: "forbidden", 22 categories: map[types.LicenseCategory][]string{ 23 types.CategoryForbidden: { 24 licensing.BSD3Clause, 25 licensing.Apache20, 26 }, 27 }, 28 licenseName: licensing.Apache20, 29 wantCategory: types.CategoryForbidden, 30 wantSeverity: "CRITICAL", 31 }, 32 { 33 name: "restricted", 34 categories: map[types.LicenseCategory][]string{ 35 types.CategoryForbidden: { 36 licensing.GPL30, 37 }, 38 types.CategoryRestricted: { 39 licensing.BSD3Clause, 40 licensing.Apache20, 41 }, 42 }, 43 licenseName: licensing.BSD3Clause, 44 wantCategory: types.CategoryRestricted, 45 wantSeverity: "HIGH", 46 }, 47 { 48 name: "unknown", 49 categories: map[types.LicenseCategory][]string{}, 50 licenseName: licensing.BSD3Clause, 51 wantCategory: types.CategoryUnknown, 52 wantSeverity: "UNKNOWN", 53 }, 54 } 55 for _, tt := range tests { 56 t.Run(tt.name, func(t *testing.T) { 57 s := licensing.NewScanner(tt.categories) 58 gotCategory, gotSeverity := s.Scan(tt.licenseName) 59 assert.Equalf(t, tt.wantCategory, gotCategory, "Scan(%v)", tt.licenseName) 60 assert.Equalf(t, tt.wantSeverity, gotSeverity, "Scan(%v)", tt.licenseName) 61 }) 62 } 63 }