-
Notifications
You must be signed in to change notification settings - Fork 0
/
anti-dos_test.go
46 lines (39 loc) · 1.01 KB
/
anti-dos_test.go
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
package main
import (
"github.com/patrickhaller/slog"
"net/http"
"testing"
"time"
)
func TestAntiDos(t *testing.T) {
// hasTooManyPasswordAttempts(username string, r *http.Request) bool {
username := "larry"
r, _ := http.NewRequest("GET", "/", nil)
r.Header.Add("X-Forwarded-For", "10.1.1.1")
cfg.AuthClientsWindow = 60
cfg.AuthFailWindow = 60
cfg.AuthFailMaxCount = 1
cfg.AuthFailLogPer = 1
slog.Init(slog.Config{
File: "STDERR",
Debug: true,
AuditFile: "STDERR",
Prefix: "WBDV",
})
// first attempt
if hasTooManyPasswordAttempts(username, r) == true {
t.Error("blocked on first attempt")
}
// ... they provide wrong passwd
lastFails[username] = append(lastFails[username], client{time: time.Now()})
if len(lastFails[username]) == 0 {
t.Error("lastFails is still zero-length")
}
if len(allClients[username]) == 0 {
t.Error("allClients is still zero-length")
}
// second attempt
if hasTooManyPasswordAttempts(username, r) == false {
t.Error("not blocked on second attempt")
}
}