aboutsummaryrefslogtreecommitdiff
path: root/internal/game/login_throttle_test.go
blob: b2565c4796a4b204e118c18fabe98a9e9bf51294 (plain)
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
package game

import (
	"testing"
	"time"
)

// TestLoginThrottleEscalatesAndResets pins the telnet login throttle: the
// first wrong password is free, then the delay escalates to a cap, and a
// successful login clears the state.
func TestLoginThrottleEscalatesAndResets(t *testing.T) {
	th := newLoginThrottle()
	key := loginThrottleKey("alice", "203.0.113.7")

	if d := th.delayFor(key); d != 0 {
		t.Fatalf("first failure delay = %v, want 0", d)
	}
	if d := th.delayFor(key); d != 250*time.Millisecond {
		t.Fatalf("second failure delay = %v, want 250ms", d)
	}
	if d := th.delayFor(key); d != 500*time.Millisecond {
		t.Fatalf("third failure delay = %v, want 500ms", d)
	}
	for i := 0; i < 10; i++ {
		th.delayFor(key)
	}
	if d := th.delayFor(key); d != loginThrottleMaxDelay {
		t.Fatalf("capped delay = %v, want %v", d, loginThrottleMaxDelay)
	}

	th.clear(key)
	if d := th.delayFor(key); d != 0 {
		t.Fatalf("delay after clear = %v, want 0", d)
	}
}