forked from DropMorePackets/berghain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
berghain_test.go
47 lines (41 loc) · 890 Bytes
/
berghain_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
47
package berghain
import (
"crypto/rand"
"net/netip"
"testing"
"time"
)
func generateSecret(tb testing.TB) []byte {
tb.Helper()
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
tb.Fatal(err)
}
return b
}
func TestBerghain(t *testing.T) {
bh := NewBerghain(generateSecret(t))
bh.Levels = []*LevelConfig{
{
Duration: time.Minute,
Type: ValidationTypeNone,
},
}
req := AcquireValidatorRequest()
req.Identifier = &RequestIdentifier{
SrcAddr: netip.MustParseAddr("1.2.3.4"),
Host: []byte("example.com"),
Level: 1,
}
req.Method = "GET"
resp := AcquireValidatorResponse()
err := bh.LevelConfig(req.Identifier.Level).Type.RunValidator(bh, req, resp)
if err != nil {
t.Errorf("validator failed: %v", err)
}
err = bh.IsValidCookie(*req.Identifier, resp.Token.ReadBytes())
if err != nil {
t.Errorf("invalid cookie: %v", err)
}
}