-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkarma_memory_test.go
141 lines (135 loc) · 3.35 KB
/
karma_memory_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package karma
import (
"fmt"
"net/smtp"
"sync"
"testing"
"github.com/vodolaz095/msmtpd"
"github.com/vodolaz095/msmtpd/internal"
"github.com/vodolaz095/msmtpd/plugins/karma/storage/memory"
)
func TestKarmaPluginMemoryGood(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
memStorage := memory.Storage{Data: make(map[string]memory.Score, 0)}
memStorage.Data["127.0.0.1"] = memory.Score{
Good: 5,
Bad: 0,
Connections: 5,
}
kh := Handler{
InitialHate: 0,
HateLimit: 4,
KarmaLimit: 4, // > 5-0
Storage: &memStorage,
}
addr, closer := msmtpd.RunTestServerWithoutTLS(t, &msmtpd.Server{
ConnectionCheckers: []msmtpd.ConnectionChecker{
kh.ConnectionChecker,
},
CloseHandlers: []msmtpd.CloseHandler{
kh.CloseHandler,
func(transaction *msmtpd.Transaction) error {
wg.Done()
return nil
},
},
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.Hello("localhost"); err != nil {
t.Errorf("Helo failed: %v", err)
}
if err = c.Mail("sender@example.org"); err != nil {
t.Errorf("MAIL failed: %v", err)
}
if err = c.Rcpt("recipient@example.net"); err != nil {
t.Errorf("RCPT failed: %v", err)
}
wc, err := c.Data()
if err != nil {
t.Errorf("Data failed: %v", err)
}
_, err = fmt.Fprintf(wc, internal.MakeTestMessage("sender@example.org", "recipient@example.net"))
if err != nil {
t.Errorf("Data body failed: %v", err)
}
err = wc.Close()
if err != nil {
t.Error("while closing email body stream")
}
if err = c.Quit(); err != nil {
t.Errorf("QUIT failed: %v", err)
}
wg.Wait()
score, found := memStorage.Data["127.0.0.1"]
if !found {
t.Errorf("karma is nuked?")
}
t.Logf("Score: %v connections, %v good and %v bad", score.Connections, score.Good, score.Bad)
if score.Connections != 6 {
t.Errorf("wrong connections %v isntead 6", score.Connections)
}
if score.Good != 6 {
t.Errorf("wrong good connecetions %v isntead of 6", score.Good)
}
if score.Bad != 0 {
t.Errorf("wrong bad connecetions %v isntead of 0", score.Bad)
}
}
func TestKarmaPluginMemoryBad(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
memStorage := memory.Storage{Data: make(map[string]memory.Score, 0)}
memStorage.Data["127.0.0.1"] = memory.Score{
Good: 0,
Bad: 5,
Connections: 5,
}
kh := Handler{
InitialHate: 0,
HateLimit: 5,
KarmaLimit: -4,
Storage: &memStorage,
}
addr, closer := msmtpd.RunTestServerWithoutTLS(t, &msmtpd.Server{
ConnectionCheckers: []msmtpd.ConnectionChecker{
kh.ConnectionChecker,
},
CloseHandlers: []msmtpd.CloseHandler{
kh.CloseHandler,
func(transaction *msmtpd.Transaction) error {
wg.Done()
return nil
},
},
})
defer closer()
_, err := smtp.Dial(addr)
if err != nil {
if err.Error() != "521 FUCK OFF!" {
t.Errorf("wrong error %s", err)
}
} else {
t.Errorf("we are not banned???")
wg.Done()
}
wg.Wait()
score, found := memStorage.Data["127.0.0.1"]
if !found {
t.Errorf("karma is nuked?")
}
t.Logf("Score: %v connections, %v good and %v bad", score.Connections, score.Good, score.Bad)
if score.Connections != 6 {
t.Errorf("wrong connections %v isntead 6", score.Connections)
}
if score.Good != 0 {
t.Errorf("wrong good connecetions %v isntead of 0", score.Good)
}
if score.Bad != 6 {
t.Errorf("wrong bad connecetions %v isntead of 6", score.Bad)
}
}