mirror of
https://github.com/securego/gosec.git
synced 2026-06-20 00:15:59 +02:00
* feat: implement global cache usage in rules * refactor: make global cache generic with local key types - Remove GlobalKey struct from gosec_cache.go - Each use case now defines its own key type (type safety via Go's type system) - Move RegexMatchWithCache to separate regex_cache.go file - Move cache kind constants to rules/hardcoded_credentials.go as local types - Add documentation for cache key requirements
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package gosec
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestLRUCache_AddGet(t *testing.T) {
|
|
cache := NewLRUCache[string, int](2)
|
|
|
|
cache.Add("one", 1)
|
|
val, ok := cache.Get("one")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, 1, val)
|
|
|
|
cache.Add("two", 2)
|
|
val, ok = cache.Get("two")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, 2, val)
|
|
}
|
|
|
|
func TestLRUCache_Miss(t *testing.T) {
|
|
cache := NewLRUCache[string, int](2)
|
|
|
|
val, ok := cache.Get("missing")
|
|
assert.False(t, ok)
|
|
assert.Equal(t, 0, val)
|
|
}
|
|
|
|
func TestLRUCache_Eviction(t *testing.T) {
|
|
cache := NewLRUCache[string, int](2)
|
|
|
|
cache.Add("one", 1)
|
|
cache.Add("two", 2)
|
|
|
|
// Cache is full: [two, one]
|
|
|
|
// Access "one" to make it most recently used
|
|
// Cache: [one, two]
|
|
_, ok := cache.Get("one")
|
|
assert.True(t, ok)
|
|
|
|
// Add "three", should evict "two" (LRU)
|
|
cache.Add("three", 3)
|
|
// Cache: [three, one]
|
|
|
|
val, ok := cache.Get("two")
|
|
assert.False(t, ok, "Expected 'two' to be evicted")
|
|
assert.Equal(t, 0, val)
|
|
|
|
val, ok = cache.Get("one")
|
|
assert.True(t, ok, "Expected 'one' to remain")
|
|
assert.Equal(t, 1, val)
|
|
|
|
val, ok = cache.Get("three")
|
|
assert.True(t, ok, "Expected 'three' to exist")
|
|
assert.Equal(t, 3, val)
|
|
}
|
|
|
|
func TestLRUCache_UpdateExisting(t *testing.T) {
|
|
cache := NewLRUCache[string, int](2)
|
|
|
|
cache.Add("one", 1)
|
|
cache.Add("two", 2)
|
|
|
|
// Update "one"
|
|
cache.Add("one", 10)
|
|
|
|
val, ok := cache.Get("one")
|
|
assert.True(t, ok)
|
|
assert.Equal(t, 10, val)
|
|
|
|
// Ensure updating didn't change size unexpectedly or eviction order incorrectly
|
|
// Cache should be: [one, two] (because "one" was just added/updated)
|
|
|
|
// Add "three", should evict "two"
|
|
cache.Add("three", 3)
|
|
|
|
_, ok = cache.Get("two")
|
|
assert.False(t, ok, "Expected 'two' to be evicted")
|
|
|
|
_, ok = cache.Get("one")
|
|
assert.True(t, ok)
|
|
}
|