-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker_geoip_test.go
73 lines (70 loc) · 1.62 KB
/
checker_geoip_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
_ "embed"
"net"
"testing"
)
func Test_geoIPChecker_Check(t *testing.T) {
tests := []struct {
name string
cfg geoIPConfig
logLine logLine
wantScore harmScore
wantDecision instantDecision
}{
{
name: "local db RU allow",
cfg: geoIPConfig{
Path: "test-data/geoip2-country.mmdb",
AllowedCountries: []string{"RU"},
},
logLine: logLine{
ip: net.IPv4(95, 173, 136, 72),
fields: map[string]string{},
},
wantScore: 0,
wantDecision: decisionWhitelist,
},
{
name: "local db RU ban",
cfg: geoIPConfig{
Path: "test-data/geoip2-country.mmdb",
AllowedCountries: []string{"FR"},
},
logLine: logLine{
ip: net.IPv4(95, 173, 136, 72),
fields: map[string]string{},
},
wantScore: 0,
wantDecision: decisionBan,
},
{
name: "local db unknown ban",
cfg: geoIPConfig{
Path: "test-data/geoip2-country.mmdb",
AllowedCountries: []string{"FR", "RU", "CN"},
},
logLine: logLine{
ip: net.IPv4(1, 2, 3, 4),
fields: map[string]string{},
},
wantScore: 0,
wantDecision: decisionBan,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gi, err := newGeoIPChecker(tt.cfg)
if err != nil {
t.Fatal(err)
}
gotScore, gotDecision := gi.Check(&tt.logLine)
if gotScore != tt.wantScore {
t.Errorf("geoIPChecker.Check() gotScore = %v, want %v", gotScore, tt.wantScore)
}
if gotDecision != tt.wantDecision {
t.Errorf("geoIPChecker.Check() gotDecision = %v, want %v", gotDecision, tt.wantDecision)
}
})
}
}