github.com/google/osv-scalibr@v0.4.1/veles/secrets/anthropicapikey/detector_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 anthropicapikey_test 16 17 import ( 18 "strings" 19 "testing" 20 21 "github.com/google/go-cmp/cmp" 22 "github.com/google/go-cmp/cmp/cmpopts" 23 "github.com/google/osv-scalibr/veles" 24 "github.com/google/osv-scalibr/veles/secrets/anthropicapikey" 25 ) 26 27 const ( 28 testKey = "sk-ant-api03-test123456789012345678901234567890123456789012345678" 29 adminKey = "sk-ant-admin01-test123456789012345678901234567890123456789012345678" 30 ) 31 32 func TestDetector(t *testing.T) { 33 engine, err := veles.NewDetectionEngine([]veles.Detector{anthropicapikey.NewDetector()}) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 cases := []struct { 39 name string 40 input string 41 want []veles.Secret 42 }{{ 43 name: "model_key", 44 input: testKey, 45 want: []veles.Secret{ 46 anthropicapikey.ModelAPIKey{Key: testKey}, 47 }, 48 }, { 49 name: "workspace_key", 50 input: adminKey, 51 want: []veles.Secret{ 52 anthropicapikey.WorkspaceAPIKey{Key: adminKey}, 53 }, 54 }, { 55 name: "multiple_keys", 56 input: testKey + " " + adminKey, 57 want: []veles.Secret{ 58 anthropicapikey.ModelAPIKey{Key: testKey}, 59 anthropicapikey.WorkspaceAPIKey{Key: adminKey}, 60 }, 61 }} 62 63 for _, tc := range cases { 64 t.Run(tc.name, func(t *testing.T) { 65 got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) 66 if err != nil { 67 t.Errorf("Detect() error: %v, want nil", err) 68 } 69 if diff := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); diff != "" { 70 t.Errorf("Detect() diff (-want +got):\n%s", diff) 71 } 72 }) 73 } 74 } 75 76 func TestDetector_NoMatches(t *testing.T) { 77 engine, err := veles.NewDetectionEngine([]veles.Detector{anthropicapikey.NewDetector()}) 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 cases := []struct { 83 name string 84 input string 85 }{{ 86 name: "empty_input", 87 input: "", 88 }, { 89 name: "wrong_prefix", 90 input: "sk-openai-api03-test123", 91 }, { 92 name: "too_short", 93 input: "sk-ant-api03", 94 }} 95 96 for _, tc := range cases { 97 t.Run(tc.name, func(t *testing.T) { 98 got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) 99 if err != nil { 100 t.Errorf("Detect() error: %v, want nil", err) 101 } 102 if len(got) != 0 { 103 t.Errorf("Detect() found %d secrets, want 0", len(got)) 104 } 105 }) 106 } 107 }