github.com/google/osv-scalibr@v0.4.1/veles/velestest/fakedetector_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 velestest_test 16 17 import ( 18 "testing" 19 20 "github.com/google/go-cmp/cmp" 21 "github.com/google/go-cmp/cmp/cmpopts" 22 "github.com/google/osv-scalibr/veles/velestest" 23 ) 24 25 func TestFakeDetector(t *testing.T) { 26 d := velestest.NewFakeDetector("FOO") 27 cases := []struct { 28 name string 29 input string 30 wantPositions []int 31 }{ 32 { 33 name: "empty input", 34 input: "", 35 wantPositions: nil, 36 }, 37 { 38 name: "no match", 39 input: "afjsdfjadlsk;fjasd;kfj;lkaeruyaeiru32489304jlkadsf;348730940347", 40 wantPositions: nil, 41 }, 42 { 43 name: "match at start", 44 input: "FOOaaaaaaaaaaaaaaaaaaaa", 45 wantPositions: []int{0}, 46 }, 47 { 48 name: "match at end", 49 input: "aaaaaaaaFOO", 50 wantPositions: []int{8}, 51 }, 52 { 53 name: "match in middle", 54 input: "aaaaFOOaaaa", 55 wantPositions: []int{4}, 56 }, 57 { 58 name: "multiple matches", 59 input: "aFOOaaaFOOaaa", 60 wantPositions: []int{1, 7}, 61 }, 62 { 63 name: "case sensitive", 64 input: "aaafooaaa", 65 wantPositions: nil, 66 }, 67 { 68 name: "specific", 69 input: "FFFFOOOOOO", 70 wantPositions: []int{3}, 71 }, 72 } 73 for _, tc := range cases { 74 t.Run(tc.name, func(t *testing.T) { 75 t.Parallel() 76 gotSecrets, gotPositions := d.Detect([]byte(tc.input)) 77 if len(gotSecrets) != len(tc.wantPositions) { 78 t.Errorf("Detect() len(secrets)=%d, want %d", len(gotSecrets), len(tc.wantPositions)) 79 } 80 if diff := cmp.Diff(tc.wantPositions, gotPositions, cmpopts.EquateEmpty()); diff != "" { 81 t.Errorf("Detect() positions diff (-want +got):\n%s", diff) 82 } 83 }) 84 } 85 } 86 87 func TestFakeDetector_returnsHotword(t *testing.T) { 88 d := velestest.NewFakeDetector("bar") 89 secrets, _ := d.Detect([]byte("abara")) 90 if secrets[0] != velestest.NewFakeStringSecret("bar") { 91 t.Errorf("Detect() secret=%v, want FakeStringSecret(bar)", secrets[0]) 92 } 93 }