github.com/google/osv-scalibr@v0.4.1/detector/list/list_test.go (about) 1 // Copyright 2025 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package list_test 16 17 import ( 18 "regexp" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/google/go-cmp/cmp/cmpopts" 23 cpb "github.com/google/osv-scalibr/binary/proto/config_go_proto" 24 dl "github.com/google/osv-scalibr/detector/list" 25 ) 26 27 var ( 28 reValidName = regexp.MustCompile(`^[a-z0-9/-]+$`) 29 ) 30 31 func TestPluginNamesValid(t *testing.T) { 32 for _, initers := range dl.All { 33 for _, initer := range initers { 34 name := initer(&cpb.PluginConfig{}).Name() 35 if !reValidName.MatchString(name) { 36 t.Errorf("Invalid plugin name %q", name) 37 } 38 } 39 } 40 } 41 42 func TestDetectorsFromName(t *testing.T) { 43 testCases := []struct { 44 desc string 45 name string 46 wantDets []string 47 wantErr error 48 }{ 49 { 50 desc: "Find_all_detectors_of_a_type", 51 name: "cis", 52 wantDets: []string{ 53 "cis/generic-linux/etcpasswdpermissions", 54 }, 55 }, 56 { 57 desc: "Find_misc_detectors", 58 name: "misc", 59 wantDets: []string{ 60 "dockersocket", 61 }, 62 }, 63 { 64 desc: "Find_weak_credentials_detectors", 65 name: "weakcredentials", 66 wantDets: []string{ 67 "weakcredentials/codeserver", 68 "weakcredentials/etcshadow", 69 "weakcredentials/filebrowser", 70 "weakcredentials/winlocal", 71 }, 72 }, 73 { 74 desc: "Nonexistent plugin", 75 name: "nonexistent", 76 wantErr: cmpopts.AnyError, 77 wantDets: []string{}, 78 }, 79 } 80 81 for _, tc := range testCases { 82 t.Run(tc.desc, func(t *testing.T) { 83 got, err := dl.DetectorsFromName(tc.name, &cpb.PluginConfig{}) 84 if diff := cmp.Diff(tc.wantErr, err, cmpopts.EquateErrors()); diff != "" { 85 t.Errorf("dl.DetectorsFromName(%v) error got diff (-want +got):\n%s", tc.name, diff) 86 } 87 gotNames := []string{} 88 for _, d := range got { 89 gotNames = append(gotNames, d.Name()) 90 } 91 if diff := cmp.Diff(tc.wantDets, gotNames, cmpopts.SortSlices(func(a, b string) bool { return a < b })); diff != "" { 92 t.Errorf("dl.DetectorsFromName(%v): got diff (-want +got):\n%s", tc.name, diff) 93 } 94 }) 95 } 96 }