From 5f4f6fafc193b99dfdc574064bef1e9042e3cff8 Mon Sep 17 00:00:00 2001 From: Ahrav Dutta Date: Tue, 30 Jan 2024 18:20:49 -0800 Subject: [PATCH] fix test --- pkg/engine/engine.go | 13 +++++++++---- pkg/engine/engine_test.go | 5 +++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/engine/engine.go b/pkg/engine/engine.go index 201186e41e64..cc733911a896 100644 --- a/pkg/engine/engine.go +++ b/pkg/engine/engine.go @@ -595,16 +595,21 @@ func likelyDuplicate(ctx context.Context, val []byte, dupes map[string]struct{}) if _, ok := dupes[string(val)]; ok { return true } + + // The string conversion is purposefully placed after the dupes check to avoid the allocation. + // []byte -> string conversion within a map lookup does not allocate. (due to compiler optimizations) + valStr := string(val) + const similarityThreshold = 0.9 for k := range dupes { // Avoid comparing strings of vastly different lengths. - if len(k)*10 < len(val)*9 || len(k)*10 > len(val)*11 { + if len(k)*10 < len(valStr)*9 || len(k)*10 > len(valStr)*11 { continue } - similarity := strutil.Similarity(string(val), k, metrics.NewLevenshtein()) + similarity := strutil.Similarity(valStr, k, metrics.NewLevenshtein()) // close enough - if similarity > 0.9 { + if similarity > similarityThreshold { ctx.Logger().V(2).Info("found similar duplicate", "val", val, "k", k, "similarity", similarity) return true } @@ -660,7 +665,7 @@ func (e *Engine) reverifierWorker(ctx context.Context) { wgDoneFn: wgDetect.Done, }, res) - // Remove the detector from the map as we have already processed the result. + // Remove the detector and secret from the maps delete(detectorsWithResult, detector) } chunkSecrets[string(val)] = struct{}{} diff --git a/pkg/engine/engine_test.go b/pkg/engine/engine_test.go index db4f066052ce..8f67c11229fc 100644 --- a/pkg/engine/engine_test.go +++ b/pkg/engine/engine_test.go @@ -233,7 +233,7 @@ func TestReverifcationChunk(t *testing.T) { WithConcurrency(1), WithDecoders(decoders.DefaultDecoders()...), WithDetectors(conf.Detectors...), - WithVerify(true), + WithVerify(false), WithPrinter(new(discardPrinter)), withReverificationTracking(), ) @@ -246,7 +246,8 @@ func TestReverifcationChunk(t *testing.T) { // Wait for all the chunks to be processed. assert.Nil(t, e.Finish(ctx)) - want := uint64(1) + // We want TWO secrets that match both the custom regexes. + want := uint64(2) assert.Equal(t, want, e.GetMetrics().UnverifiedSecretsFound) wantDupe := 1