platform: modularize api/gui, add docs-tests-web foundation, and refresh root config
This commit is contained in:
69
selective-vpn-api/app/resolver_policy_test.go
Normal file
69
selective-vpn-api/app/resolver_policy_test.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package app
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDomainStateFromScore(t *testing.T) {
|
||||
tests := []struct {
|
||||
score int
|
||||
want string
|
||||
}{
|
||||
{25, domainStateActive},
|
||||
{10, domainStateStable},
|
||||
{0, domainStateSuspect},
|
||||
{-12, domainStateQuarantine},
|
||||
{-40, domainStateHardQuar},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
if got := domainStateFromScore(tc.score); got != tc.want {
|
||||
t.Fatalf("domainStateFromScore(%d)=%q want=%q", tc.score, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainScorePenalty(t *testing.T) {
|
||||
if got := domainScorePenalty(dnsMetrics{NXDomain: 2}); got >= 0 {
|
||||
t.Fatalf("expected negative penalty for confirmed nxdomain, got %d", got)
|
||||
}
|
||||
if got := domainScorePenalty(dnsMetrics{NXDomain: 1}); got >= 0 {
|
||||
t.Fatalf("expected negative penalty for single nxdomain, got %d", got)
|
||||
}
|
||||
if got := domainScorePenalty(dnsMetrics{Timeout: 1}); got >= 0 {
|
||||
t.Fatalf("expected negative penalty for timeout, got %d", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDomainCacheGetStale(t *testing.T) {
|
||||
s := newDomainCacheState()
|
||||
now := 2000
|
||||
s.set("example.com", domainCacheSourceDirect, []string{"1.2.3.4"}, now-100)
|
||||
ips, age, ok := s.getStale("example.com", domainCacheSourceDirect, now, 300)
|
||||
if !ok {
|
||||
t.Fatalf("expected stale entry")
|
||||
}
|
||||
if age != 100 {
|
||||
t.Fatalf("age=%d want=100", age)
|
||||
}
|
||||
if len(ips) != 1 || ips[0] != "1.2.3.4" {
|
||||
t.Fatalf("unexpected ips: %v", ips)
|
||||
}
|
||||
if _, _, ok := s.getStale("example.com", domainCacheSourceDirect, now, 50); ok {
|
||||
t.Fatalf("expected stale miss when maxAge is too small")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetErrorWithStatsSetsQuarantine(t *testing.T) {
|
||||
s := newDomainCacheState()
|
||||
now := 3000
|
||||
s.set("bad.example", domainCacheSourceDirect, []string{"2.2.2.2"}, now-10)
|
||||
for i := 0; i < 5; i++ {
|
||||
s.setErrorWithStats("bad.example", domainCacheSourceDirect, dnsMetrics{NXDomain: 2}, now+i)
|
||||
}
|
||||
state, _, ok := s.getQuarantine("bad.example", domainCacheSourceDirect, now+5)
|
||||
if !ok {
|
||||
t.Fatalf("expected quarantine to be set")
|
||||
}
|
||||
if state != domainStateQuarantine && state != domainStateHardQuar {
|
||||
t.Fatalf("unexpected quarantine state: %s", state)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user