|
| 1 | +// Copyright 2026 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 databrickspat_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/google/go-cmp/cmp" |
| 23 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 24 | + "github.com/google/osv-scalibr/veles" |
| 25 | + "github.com/google/osv-scalibr/veles/secrets/databrickspat" |
| 26 | + "github.com/google/osv-scalibr/veles/velestest" |
| 27 | +) |
| 28 | + |
| 29 | +const testPAT = `dapi0123456789abcdef0123456789abcdef` |
| 30 | +const testPATWithSuffix = `dapi0123456789abcdef0123456789abcdef-2` |
| 31 | + |
| 32 | +func TestDetectorAcceptance(t *testing.T) { |
| 33 | + velestest.AcceptDetector( |
| 34 | + t, |
| 35 | + databrickspat.NewDetector(), |
| 36 | + testPAT, |
| 37 | + databrickspat.UserAccountPAT{Token: testPAT}, |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +func TestDetector_truePositives(t *testing.T) { |
| 42 | + engine, err := veles.NewDetectionEngine([]veles.Detector{databrickspat.NewDetector()}) |
| 43 | + if err != nil { |
| 44 | + t.Fatal(err) |
| 45 | + } |
| 46 | + otherToken := testPAT[:len(testPAT)-1] + "0" |
| 47 | + cases := []struct { |
| 48 | + name string |
| 49 | + input string |
| 50 | + want []veles.Secret |
| 51 | + }{{ |
| 52 | + name: "simple matching string", |
| 53 | + input: testPAT, |
| 54 | + want: []veles.Secret{ |
| 55 | + databrickspat.UserAccountPAT{Token: testPAT}, |
| 56 | + }, |
| 57 | + }, { |
| 58 | + name: "optional single digit suffix", |
| 59 | + input: testPATWithSuffix, |
| 60 | + want: []veles.Secret{ |
| 61 | + databrickspat.UserAccountPAT{Token: testPATWithSuffix}, |
| 62 | + }, |
| 63 | + }, { |
| 64 | + name: "match at end of env assignment", |
| 65 | + input: `DATABRICKS_TOKEN=` + testPAT, |
| 66 | + want: []veles.Secret{ |
| 67 | + databrickspat.UserAccountPAT{Token: testPAT}, |
| 68 | + }, |
| 69 | + }, { |
| 70 | + name: "multiple distinct matches", |
| 71 | + input: testPAT + "\n" + otherToken, |
| 72 | + want: []veles.Secret{ |
| 73 | + databrickspat.UserAccountPAT{Token: testPAT}, |
| 74 | + databrickspat.UserAccountPAT{Token: otherToken}, |
| 75 | + }, |
| 76 | + }, { |
| 77 | + name: "larger input containing token", |
| 78 | + input: fmt.Sprintf(` |
| 79 | + [DEFAULT] |
| 80 | + token = "%s" |
| 81 | + `, testPAT), |
| 82 | + want: []veles.Secret{ |
| 83 | + databrickspat.UserAccountPAT{Token: testPAT}, |
| 84 | + }, |
| 85 | + }} |
| 86 | + for _, tc := range cases { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) |
| 89 | + if err != nil { |
| 90 | + t.Errorf("Detect() error: %v, want nil", err) |
| 91 | + } |
| 92 | + if diff := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); diff != "" { |
| 93 | + t.Errorf("Detect() diff (-want +got):\n%s", diff) |
| 94 | + } |
| 95 | + }) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func TestDetector_trueNegatives(t *testing.T) { |
| 100 | + engine, err := veles.NewDetectionEngine([]veles.Detector{databrickspat.NewDetector()}) |
| 101 | + if err != nil { |
| 102 | + t.Fatal(err) |
| 103 | + } |
| 104 | + cases := []struct { |
| 105 | + name string |
| 106 | + input string |
| 107 | + want []veles.Secret |
| 108 | + }{{ |
| 109 | + name: "empty input", |
| 110 | + input: "", |
| 111 | + }, { |
| 112 | + name: "short token should not match", |
| 113 | + input: testPAT[:len(testPAT)-1], |
| 114 | + }, { |
| 115 | + name: "long token should not match", |
| 116 | + input: testPAT + "0", |
| 117 | + }, { |
| 118 | + name: "invalid hex character should not match", |
| 119 | + input: testPAT[:len(testPAT)-1] + "g", |
| 120 | + }, { |
| 121 | + name: "uppercase hex should not match", |
| 122 | + input: strings.ToUpper(testPAT), |
| 123 | + }, { |
| 124 | + name: "incorrect prefix should not match", |
| 125 | + input: "dapo" + testPAT[len("dapi"):], |
| 126 | + }, { |
| 127 | + name: "two digit suffix should not match as full token", |
| 128 | + input: testPAT + "-20", |
| 129 | + }} |
| 130 | + for _, tc := range cases { |
| 131 | + t.Run(tc.name, func(t *testing.T) { |
| 132 | + got, err := engine.Detect(t.Context(), strings.NewReader(tc.input)) |
| 133 | + if err != nil { |
| 134 | + t.Errorf("Detect() error: %v, want nil", err) |
| 135 | + } |
| 136 | + if diff := cmp.Diff(tc.want, got, cmpopts.EquateEmpty()); diff != "" { |
| 137 | + t.Errorf("Detect() diff (-want +got):\n%s", diff) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
0 commit comments