Files
godoxy/internal/acl/config_test.go
yusing c8d7d4f7d3 refactor(acl): memoize IPAllowed with goutils keyed TTL cache
Replace the xsync map plus manual expiry on checkCache with
cache.NewKeyFunc(evaluateIP).WithTTL. Move deny/allow/default logic into
evaluateIP; wire getCachedCity and IPAllowed through the cache API.

Refresh README security notes and add tests showing cached decisions persist
across in-memory rule changes until TTL expires.
2026-04-19 15:15:28 +08:00

62 lines
1.2 KiB
Go

package acl
import (
"net"
"testing"
"github.com/stretchr/testify/require"
)
func TestIPAllowedCachesDecision(t *testing.T) {
t.Parallel()
testIP := net.ParseIP("8.8.8.8")
require.NotNil(t, testIP)
t.Run("cached allow survives rule changes", func(t *testing.T) {
t.Parallel()
cfg := &Config{
Default: ACLDeny,
AllowLocal: new(false),
Allow: mustMatchers(t, "ip:8.8.8.8"),
}
require.NoError(t, cfg.Validate())
require.True(t, cfg.IPAllowed(testIP))
cfg.Allow = nil
cfg.Deny = mustMatchers(t, "ip:8.8.8.8")
require.True(t, cfg.IPAllowed(testIP))
})
t.Run("cached deny survives rule changes", func(t *testing.T) {
t.Parallel()
cfg := &Config{
Default: ACLAllow,
AllowLocal: new(false),
Deny: mustMatchers(t, "ip:8.8.8.8"),
}
require.NoError(t, cfg.Validate())
require.False(t, cfg.IPAllowed(testIP))
cfg.Deny = nil
cfg.Allow = mustMatchers(t, "ip:8.8.8.8")
require.False(t, cfg.IPAllowed(testIP))
})
}
func mustMatchers(t *testing.T, rules ...string) Matchers {
t.Helper()
matchers := make(Matchers, len(rules))
for i, rule := range rules {
require.NoError(t, matchers[i].Parse(rule))
}
return matchers
}