Files
elmprodvpn/selective-vpn-api/app/resolver_cache_test.go
beckline 10a10f44a8 baseline: api+gui traffic mode + candidates picker
Snapshot before app-launcher (cgroup/mark) work; ignore binaries/backups.
2026-02-14 15:52:20 +03:00

71 lines
2.3 KiB
Go

package app
import (
"os"
"path/filepath"
"testing"
)
func TestDomainCacheLegacyMigrationToDirectBucket(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "domain-cache.json")
legacy := `{
"example.com": {"ips": ["1.1.1.1", "10.0.0.1"], "last_resolved": 100},
"bad.com": {"ips": [], "last_resolved": 100}
}`
if err := os.WriteFile(path, []byte(legacy), 0o644); err != nil {
t.Fatalf("write legacy cache: %v", err)
}
st := loadDomainCacheState(path, nil)
if _, ok := st.get("example.com", domainCacheSourceDirect, 150, 100); !ok {
t.Fatalf("expected direct cache hit after migration")
}
if _, ok := st.get("example.com", domainCacheSourceWildcard, 150, 100); ok {
t.Fatalf("did not expect wildcard cache hit for migrated legacy entry")
}
if ips, ok := st.get("example.com", domainCacheSourceDirect, 150, 100); !ok || len(ips) != 1 || ips[0] != "1.1.1.1" {
t.Fatalf("unexpected migrated ips: ok=%v ips=%v", ok, ips)
}
}
func TestDomainCacheSplitBucketsAreIndependent(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "domain-cache.json")
v2 := `{
"version": 2,
"domains": {
"example.com": {
"direct": {"ips": ["1.1.1.1"], "last_resolved": 100},
"wildcard": {"ips": ["2.2.2.2"], "last_resolved": 100}
}
}
}`
if err := os.WriteFile(path, []byte(v2), 0o644); err != nil {
t.Fatalf("write v2 cache: %v", err)
}
st := loadDomainCacheState(path, nil)
direct, ok := st.get("example.com", domainCacheSourceDirect, 150, 100)
if !ok || len(direct) != 1 || direct[0] != "1.1.1.1" {
t.Fatalf("unexpected direct lookup: ok=%v ips=%v", ok, direct)
}
wild, ok := st.get("example.com", domainCacheSourceWildcard, 150, 100)
if !ok || len(wild) != 1 || wild[0] != "2.2.2.2" {
t.Fatalf("unexpected wildcard lookup: ok=%v ips=%v", ok, wild)
}
}
func TestDomainCacheSetAndTTL(t *testing.T) {
st := newDomainCacheState()
st.set("example.com", domainCacheSourceDirect, []string{"1.1.1.1", "1.1.1.1", "10.0.0.1"}, 100)
if _, ok := st.get("example.com", domainCacheSourceDirect, 201, 100); ok {
t.Fatalf("expected cache miss due ttl expiry")
}
ips, ok := st.get("example.com", domainCacheSourceDirect, 200, 100)
if !ok || len(ips) != 1 || ips[0] != "1.1.1.1" {
t.Fatalf("unexpected ttl hit result: ok=%v ips=%v", ok, ips)
}
}