91 lines
2.3 KiB
Go
91 lines
2.3 KiB
Go
// ©AngelaMos | 2026
|
|
// cache_test.go
|
|
|
|
package vuln
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/CarterPerez-dev/bomber/pkg/types"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestCachePutGet(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
cache, err := NewCache(dbPath, 24*time.Hour)
|
|
require.NoError(t, err)
|
|
defer cache.Close()
|
|
|
|
matches := []types.VulnMatch{
|
|
{
|
|
Package: types.Package{PURL: "pkg:golang/example@v1.0.0"},
|
|
Vulnerability: types.Vulnerability{ID: "CVE-2024-1234", Source: "osv"},
|
|
},
|
|
}
|
|
|
|
require.NoError(t, cache.Put("pkg:golang/example@v1.0.0", "osv", matches))
|
|
|
|
got, ok, err := cache.Get("pkg:golang/example@v1.0.0", "osv")
|
|
require.NoError(t, err)
|
|
assert.True(t, ok)
|
|
assert.Len(t, got, 1)
|
|
assert.Equal(t, "CVE-2024-1234", got[0].Vulnerability.ID)
|
|
}
|
|
|
|
func TestCacheExpiry(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
cache, err := NewCache(dbPath, 1*time.Millisecond)
|
|
require.NoError(t, err)
|
|
defer cache.Close()
|
|
|
|
matches := []types.VulnMatch{
|
|
{
|
|
Package: types.Package{PURL: "pkg:golang/example@v1.0.0"},
|
|
Vulnerability: types.Vulnerability{ID: "CVE-2024-1234", Source: "osv"},
|
|
},
|
|
}
|
|
|
|
require.NoError(t, cache.Put("pkg:golang/example@v1.0.0", "osv", matches))
|
|
time.Sleep(5 * time.Millisecond)
|
|
|
|
_, ok, err := cache.Get("pkg:golang/example@v1.0.0", "osv")
|
|
require.NoError(t, err)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestCacheMiss(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
cache, err := NewCache(dbPath, 24*time.Hour)
|
|
require.NoError(t, err)
|
|
defer cache.Close()
|
|
|
|
_, ok, err := cache.Get("pkg:golang/nonexistent@v1.0.0", "osv")
|
|
require.NoError(t, err)
|
|
assert.False(t, ok)
|
|
}
|
|
|
|
func TestCacheOverwrite(t *testing.T) {
|
|
dbPath := filepath.Join(t.TempDir(), "test.db")
|
|
cache, err := NewCache(dbPath, 24*time.Hour)
|
|
require.NoError(t, err)
|
|
defer cache.Close()
|
|
|
|
old := []types.VulnMatch{
|
|
{Vulnerability: types.Vulnerability{ID: "CVE-OLD"}},
|
|
}
|
|
require.NoError(t, cache.Put("pkg:test@1.0.0", "osv", old))
|
|
|
|
updated := []types.VulnMatch{
|
|
{Vulnerability: types.Vulnerability{ID: "CVE-NEW"}},
|
|
}
|
|
require.NoError(t, cache.Put("pkg:test@1.0.0", "osv", updated))
|
|
|
|
got, ok, err := cache.Get("pkg:test@1.0.0", "osv")
|
|
require.NoError(t, err)
|
|
assert.True(t, ok)
|
|
assert.Equal(t, "CVE-NEW", got[0].Vulnerability.ID)
|
|
}
|