-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2010 mistakenly removed detector version tracking from the Aho Corasick wrapper. This PR re-adds it.
- Loading branch information
Showing
2 changed files
with
120 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package engine | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/detectors" | ||
"github.com/trufflesecurity/trufflehog/v3/pkg/pb/detectorspb" | ||
) | ||
|
||
const TestDetectorType = -1 | ||
|
||
type testDetectorV1 struct { | ||
} | ||
|
||
func (d testDetectorV1) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) { | ||
return make([]detectors.Result, 0), nil | ||
} | ||
|
||
func (d testDetectorV1) Keywords() []string { | ||
return []string{"a"} | ||
} | ||
|
||
func (d testDetectorV1) Type() detectorspb.DetectorType { | ||
return TestDetectorType | ||
} | ||
|
||
func (d testDetectorV1) Version() int { | ||
return 1 | ||
} | ||
|
||
type testDetectorV2 struct { | ||
} | ||
|
||
func (d testDetectorV2) FromData(ctx context.Context, verify bool, data []byte) ([]detectors.Result, error) { | ||
return make([]detectors.Result, 0), nil | ||
} | ||
|
||
func (d testDetectorV2) Keywords() []string { | ||
return []string{"b"} | ||
} | ||
|
||
func (d testDetectorV2) Type() detectorspb.DetectorType { | ||
return TestDetectorType | ||
} | ||
|
||
func (d testDetectorV2) Version() int { | ||
return 2 | ||
} | ||
|
||
var _ detectors.Detector = (*testDetectorV1)(nil) | ||
var _ detectors.Detector = (*testDetectorV2)(nil) | ||
var _ detectors.Versioner = (*testDetectorV1)(nil) | ||
var _ detectors.Versioner = (*testDetectorV2)(nil) | ||
|
||
func TestAhoCorasickCore_MultipleDetectorVersionsMatchable(t *testing.T) { | ||
testCases := []struct { | ||
matchString string | ||
detector detectors.Detector | ||
}{ | ||
{ | ||
matchString: "a", | ||
detector: testDetectorV1{}, | ||
}, | ||
{ | ||
matchString: "b", | ||
detector: testDetectorV2{}, | ||
}, | ||
} | ||
|
||
var allDetectors []detectors.Detector | ||
for _, tt := range testCases { | ||
allDetectors = append(allDetectors, tt.detector) | ||
} | ||
|
||
ac := NewAhoCorasickCore(allDetectors) | ||
|
||
for _, tt := range testCases { | ||
matches := ac.MatchString(tt.matchString) | ||
assert.Equal(t, 1, len(matches)) | ||
|
||
matchingDetectors := make(map[detectorspb.DetectorType]detectors.Detector) | ||
ac.PopulateDetectorsByMatch(matches[0], matchingDetectors) | ||
assert.Equal(t, 1, len(matchingDetectors)) | ||
assert.Equal(t, tt.detector, matchingDetectors[TestDetectorType]) | ||
} | ||
} |