-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_test.go
More file actions
130 lines (99 loc) · 2.89 KB
/
Copy pathcache_test.go
File metadata and controls
130 lines (99 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"sync"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCache_GetPut(t *testing.T) {
cache := NewCache()
content := "const key = \"AKIATESTKEYEXAMPLE7A\""
findings := []Finding{
{RuleID: "aws-access-key", Description: "AWS Access Key"},
}
// Initially empty
_, ok := cache.Get(content)
assert.False(t, ok, "Cache should be empty initially")
// Put and get
cache.Put(content, findings)
result, ok := cache.Get(content)
assert.True(t, ok, "Should find cached entry")
assert.Len(t, result, 1)
assert.Equal(t, "aws-access-key", result[0].RuleID)
}
func TestCache_DifferentContent(t *testing.T) {
cache := NewCache()
content1 := "secret1"
content2 := "secret2"
findings1 := []Finding{{RuleID: "rule1"}}
findings2 := []Finding{{RuleID: "rule2"}}
cache.Put(content1, findings1)
cache.Put(content2, findings2)
result1, ok := cache.Get(content1)
assert.True(t, ok)
assert.Equal(t, "rule1", result1[0].RuleID)
result2, ok := cache.Get(content2)
assert.True(t, ok)
assert.Equal(t, "rule2", result2[0].RuleID)
}
func TestCache_Clear(t *testing.T) {
cache := NewCache()
cache.Put("content1", []Finding{{RuleID: "rule1"}})
cache.Put("content2", []Finding{{RuleID: "rule2"}})
assert.Equal(t, 2, cache.Size())
cache.Clear()
assert.Equal(t, 0, cache.Size())
_, ok := cache.Get("content1")
assert.False(t, ok, "Cache should be empty after clear")
}
func TestCache_Size(t *testing.T) {
cache := NewCache()
assert.Equal(t, 0, cache.Size())
cache.Put("a", []Finding{})
assert.Equal(t, 1, cache.Size())
cache.Put("b", []Finding{})
assert.Equal(t, 2, cache.Size())
// Same content doesn't increase size
cache.Put("a", []Finding{{RuleID: "updated"}})
assert.Equal(t, 2, cache.Size())
}
func TestCache_EmptyFindings(t *testing.T) {
cache := NewCache()
content := "clean code with no secrets"
cache.Put(content, []Finding{})
result, ok := cache.Get(content)
assert.True(t, ok, "Should cache empty findings too")
assert.Empty(t, result)
}
func TestCache_Concurrent(t *testing.T) {
cache := NewCache()
var wg sync.WaitGroup
// Concurrent writes
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
content := string(rune('a' + n%26))
cache.Put(content, []Finding{{RuleID: "rule"}})
}(i)
}
// Concurrent reads
for i := 0; i < 100; i++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
content := string(rune('a' + n%26))
cache.Get(content)
}(i)
}
wg.Wait()
// Verify postconditions: cache should have entries and return correct data
assert.Greater(t, cache.Size(), 0, "Cache should have entries after concurrent writes")
assert.LessOrEqual(t, cache.Size(), 26, "Cache should have at most 26 entries (a-z)")
// Verify all cached entries return valid data
for i := 0; i < 26; i++ {
content := string(rune('a' + i))
if result, ok := cache.Get(content); ok {
assert.Equal(t, "rule", result[0].RuleID)
}
}
}