-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_test.go
218 lines (211 loc) · 5.25 KB
/
transaction_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package msmtpd
import (
"fmt"
"net/mail"
"net/smtp"
"sync"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestKarma(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
SenderCheckers: []SenderChecker{
func(transaction *Transaction) error {
karma := transaction.Karma()
if karma != commandExecutedProperly { // because HELO passed
t.Errorf("wrong initial karma %v", karma)
}
if transaction.MailFrom.Address == "scuba@vodolaz095.ru" {
transaction.Love(1000)
}
return nil
},
},
DataHandlers: []DataHandler{
func(tr *Transaction) error {
if tr.Karma() < 1000 {
t.Errorf("not enough karma. Required at least 1000. Actual: %v", tr.Karma())
}
err := ErrorSMTP{
Code: 555,
Message: "karma",
}
if err.Error() != "555 karma" {
t.Errorf("wrong error")
}
return err
},
},
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = c.Hello("mx.example.org")
if err != nil {
t.Errorf("sending helo command failed with %s", err)
}
err = c.Mail("scuba@vodolaz095.ru")
if err != nil {
t.Errorf("sending mail from command failed with %s", err)
}
err = c.Rcpt("example@example.org")
if err != nil {
t.Errorf("RCPT TO command command failed with %s", err)
}
wc, err := c.Data()
if err != nil {
t.Errorf("Data failed: %v", err)
}
_, err = fmt.Fprint(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 {
if err.Error() != "555 karma" {
t.Errorf("wrong error returned")
}
}
err = c.Quit()
if err != nil {
t.Errorf("sending quit command failed with %s", err)
}
}
func TestContext(t *testing.T) {
wg := sync.WaitGroup{}
wg.Add(1)
addr, closer := RunTestServerWithoutTLS(t, &Server{
ConnectionCheckers: []ConnectionChecker{
func(transaction *Transaction) error {
ctx := transaction.Context()
t.Logf("context is extracted!")
go func() {
t.Logf("starting background goroutine being terminated with context")
<-ctx.Done()
wg.Done()
t.Logf("context is terminated")
}()
return nil
},
},
})
defer closer()
cm, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
t.Logf("closing connection, so context should be closed too...")
err = cm.Close()
if err != nil {
t.Error(err)
}
t.Logf("waiting for context to be closed...")
wg.Wait()
t.Logf("context is closed")
}
func TestMeta(t *testing.T) {
addr, closer := RunTestServerWithoutTLS(t, &Server{
MaxConnections: 1,
HeloCheckers: []HelloChecker{
func(transaction *Transaction) error {
name := transaction.HeloName
transaction.SetFact("something", name)
transaction.Incr("int64", 1)
transaction.Incr("float64", 1.1)
transaction.LogWarn("something")
transaction.SetFlag("heloCheckerFired")
return nil
},
},
SenderCheckers: []SenderChecker{
func(transaction *Transaction) error {
var found bool
_, found = transaction.GetFact("nothing")
if found {
t.Error("fact `nothing` is found?")
}
something, found := transaction.GetFact("something")
if !found {
t.Errorf("fact `something` is not set!")
}
if something != "localhost" {
t.Errorf("wrong meta `something` %s instead of `localhost`", something)
}
integerValue, found := transaction.GetCounter("int64")
if !found {
t.Errorf("counter `int64` is not set!")
}
if integerValue != 1 {
t.Errorf("wrong value for `int64`")
}
floatValue, found := transaction.GetCounter("float64")
if !found {
t.Errorf("counter `float64` is not set!")
}
if floatValue != 1.1 {
t.Errorf("wrong value for `float64` - %v", floatValue)
}
_, found = transaction.GetCounter("lalala")
if found {
t.Errorf("unexistend counter returned value")
}
transaction.Incr("int64", 1)
transaction.Incr("float64", 1.1)
if !transaction.IsFlagSet("heloCheckerFired") {
t.Errorf("flag heloCheckerFired is not set")
}
transaction.UnsetFlag("heloCheckerFired")
return nil
},
},
RecipientCheckers: []RecipientChecker{
func(transaction *Transaction, _ *mail.Address) error {
var found bool
a, found := transaction.GetCounter("int64")
if !found {
t.Errorf("counter `int64` is not set!")
}
b, found := transaction.GetCounter("float64")
if !found {
t.Errorf("counter `float64` is not set!")
}
c, found := transaction.GetFact("something")
if !found {
t.Errorf("fact `something` is not set!")
}
if transaction.IsFlagSet("heloCheckerFired") {
t.Errorf("flag heloCheckerFired is set")
}
return ErrorSMTP{
Code: 451,
Message: fmt.Sprintf("%v %v %s", a, b, c),
}
},
},
})
defer closer()
cm, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
err = cm.Hello("localhost")
if err != nil {
t.Error(err)
}
err = cm.Mail("somebody@localhost")
if err != nil {
t.Error(err)
}
err = cm.Rcpt("scuba@example.org")
if err != nil {
if err.Error() != "451 2 2.2 localhost" {
t.Errorf("wrong error `%s` instead `451 2 2.2 localhost`", err)
}
}
err = cm.Close()
if err != nil {
t.Error(err)
}
}