Debian Codegraph @

pkg/detectors/tests/helpers.go

raw symbols SCIP
1 package tests
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/DefangLabs/secret-detector/pkg/secrets"
8 "github.com/stretchr/testify/assert"
9 )
10
11 type TestCase struct {
12 Name, Input string
13 ExpectDetection bool
14 }
15
16 func TestScan(t *testing.T, detector secrets.Detector, testCases []TestCase) {
17 t.Helper()
18
19 for _, test := range testCases {
20 t.Run(test.Name, func(t *testing.T) {
21 detectedSecrets, err := detector.Scan(test.Input)
22
23 assert.NoError(t, err)
24 if test.ExpectDetection {
25 if assert.Len(t, detectedSecrets, 1) {
26 assert.Equal(t, detector.SecretType(), detectedSecrets[0].Type)
27 assert.NotEmpty(t, detectedSecrets[0].Value)
28 }
29 } else {
30 assert.Len(t, detectedSecrets, 0)
31 }
32 })
33 }
34 }
35
36 func TestScanWithKey(t *testing.T, detector secrets.Detector, testCases []TestCase) {
37 t.Helper()
38
39 for i, test := range testCases {
40 t.Run(test.Name+" (with key)", func(t *testing.T) {
41 key := fmt.Sprintf("key%v", i)
42 input := fmt.Sprintf("%v=%v", key, test.Input)
43 detectedSecrets, err := detector.Scan(input)
44
45 assert.NoError(t, err)
46 if test.ExpectDetection {
47 if assert.Len(t, detectedSecrets, 1) {
48 assert.Equal(t, key, detectedSecrets[0].Key)
49 assert.Equal(t, detector.SecretType(), detectedSecrets[0].Type)
50 assert.NotEmpty(t, detectedSecrets[0].Value)
51 }
52 } else {
53 assert.Len(t, detectedSecrets, 0)
54 }
55 })
56 }
57 }
58
59 func TestScanWithMultipleMatches(t *testing.T, detector secrets.Detector, testCases []TestCase) {
60 t.Helper()
61 TestScanWithMultipleMatchesWithoutKey(t, detector, testCases)
62 TestScanWithMultipleMatchesWithKey(t, detector, testCases)
63 }
64
65 func TestScanWithMultipleMatchesWithoutKey(t *testing.T, detector secrets.Detector, testCases []TestCase) {
66 t.Helper()
67
68 t.Run("Test Scan with multiple matches", func(t *testing.T) {
69 var input string
70 expectedDetections := 0
71 for _, test := range testCases {
72 input += test.Input + "\n"
73 if test.ExpectDetection {
74 expectedDetections++
75 }
76 }
77
78 detectedSecrets, err := detector.Scan(input)
79
80 assert.NoError(t, err)
81 assert.Len(t, detectedSecrets, expectedDetections)
82 })
83 }
84
85 func TestScanWithMultipleMatchesWithKey(t *testing.T, detector secrets.Detector, testCases []TestCase) {
86 t.Helper()
87
88 t.Run("Test Scan with multiple matches (with keys)", func(t *testing.T) {
89 var input string
90 expectedKeys := make([]string, 0)
91 for i, test := range testCases {
92 expectedKey := fmt.Sprintf("key%v", i)
93 input += fmt.Sprintf("%v: %v\n", expectedKey, test.Input)
94 if test.ExpectDetection {
95 expectedKeys = append(expectedKeys, expectedKey)
96 }
97 }
98
99 detectedSecrets, err := detector.Scan(input)
100
101 assert.NoError(t, err)
102 AssertKeysDetected(t, expectedKeys, detector.SecretType(), detectedSecrets)
103 })
104 }
105
106 func TestScanMap(t *testing.T, detector secrets.Detector, testCases []TestCase) {
107 t.Helper()
108
109 t.Run("Test ScanMap", func(t *testing.T) {
110 input := make(map[string]string, len(testCases))
111 expectedKeys := make([]string, 0)
112 for i, test := range testCases {
113 expectedKey := fmt.Sprintf("key%v", i)
114 input[expectedKey] = test.Input
115 if test.ExpectDetection {
116 expectedKeys = append(expectedKeys, expectedKey)
117 }
118 }
119
120 detectedSecrets, err := detector.ScanMap(input)
121
122 assert.NoError(t, err)
123 AssertKeysDetected(t, expectedKeys, detector.SecretType(), detectedSecrets)
124 })
125
126 t.Run("Test ScanMap with no detections", func(t *testing.T) {
127 input := map[string]string{
128 "key1": "this is not a secret",
129 "key2": "nothing interesting here",
130 }
131 detectedSecrets, err := detector.ScanMap(input)
132
133 assert.NoError(t, err)
134 assert.Len(t, detectedSecrets, 0)
135 })
136 }
137
138 func AssertKeysDetected(t *testing.T, expectedKeys []string, expectedType string, detectedSecrets []secrets.DetectedSecret) {
139 t.Helper()
140
141 if !assert.Len(t, detectedSecrets, len(expectedKeys)) {
142 return
143 }
144
145 detectedSecretsMap := make(map[string]secrets.DetectedSecret, len(detectedSecrets))
146 for _, secret := range detectedSecrets {
147 detectedSecretsMap[secret.Key] = secret
148 }
149
150 for _, expectedKey := range expectedKeys {
151 secret, found := detectedSecretsMap[expectedKey]
152 if assert.True(t, found) {
153 assert.Equal(t, expectedKey, secret.Key)
154 assert.Equal(t, expectedType, secret.Type)
155 assert.NotEmpty(t, secret.Value)
156 }
157 }
158 }