forked from saintienn/go-spamc
-
Notifications
You must be signed in to change notification settings - Fork 9
/
spamc_test.go
398 lines (359 loc) · 9.33 KB
/
spamc_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
package spamc
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/textproto"
"os"
"reflect"
"strings"
"testing"
"github.com/teamwork/test"
"github.com/teamwork/test/diff"
"github.com/teamwork/test/fakeconn"
)
func TestWrite(t *testing.T) {
cases := []struct {
inCmd string
inMsg io.Reader
inHeader Header
want string
wantErr string
}{
{ // testing bytes.NewReader
"CMD", bytes.NewReader([]byte("Message")), nil,
"CMD SPAMC/1.5\r\nContent-length: 7\r\n\r\nMessage",
"",
},
{"", strings.NewReader("Message"), nil, "", "empty command"},
{"CMD", strings.NewReader(""), nil, "CMD SPAMC/1.5\r\nContent-length: 0\r\n\r\n", ""},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
conn := fakeconn.New()
c := Client{conn: conn}
err := c.write(conn, tc.inCmd, tc.inMsg, tc.inHeader)
out := conn.Written.String()
if out != tc.want {
t.Errorf("wrong data written\nout: %#v\nwant: %#v\n",
out, tc.want)
}
if !test.ErrorContains(err, tc.wantErr) {
t.Errorf("wrong error\nout: %#v\nwant: %#v\n", out, tc.want)
}
})
}
}
func TestWriteDefaultUser(t *testing.T) {
cases := []struct {
inCmd string
inMsg string
inHeader Header
want string
wantErr string
}{
{
"CMD", "Message", Header{"User": "xx"},
"CMD SPAMC/1.5\r\nContent-length: 7\r\nUser: xx\r\n\r\nMessage",
"",
},
{
"CMD", "Message", nil,
"CMD SPAMC/1.5\r\nContent-length: 7\r\nUser: default\r\n\r\nMessage",
"",
},
{"", "Message", nil, "", "empty command"},
{"CMD", "", nil, "CMD SPAMC/1.5\r\nContent-length: 0\r\nUser: default\r\n\r\n", ""},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
conn := fakeconn.New()
c := Client{conn: conn}
c.DefaultUser = "default"
err := c.write(conn, tc.inCmd, strings.NewReader(tc.inMsg), tc.inHeader)
out := conn.Written.String()
if out != tc.want {
t.Errorf("wrong data written\nout: %#v\nwant: %#v\n", out, tc.want)
}
if !test.ErrorContains(err, tc.wantErr) {
t.Errorf("wrong error\nout: %#v\nwant: %#v\n", out, tc.want)
}
})
}
}
func TestReadResponse(t *testing.T) {
cases := []struct {
in string
expectedHeader Header
expectedBody string
expectedErr string
}{
{
in: "SPAMD/1.1 0 EX_OK\r\n" +
"Header: value\r\n\r\n" +
"THE BODY",
expectedHeader: Header{"Header": "value"},
expectedBody: "THE BODY\r\n",
expectedErr: "",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
headers, tp, err := readResponse(strings.NewReader(tc.in))
if !test.ErrorContains(err, tc.expectedErr) {
t.Errorf("wrong error; want «%v», got «%v»", tc.expectedErr, err)
}
body, err := readBody(tp)
if err != nil {
t.Fatal(err)
}
if body != tc.expectedBody {
t.Errorf("wrong body\nout: %#v\nexpected: %#v\n", body, tc.expectedBody)
}
if !reflect.DeepEqual(headers, tc.expectedHeader) {
t.Errorf("\nout: %#v\nexpected: %#v\n", headers, tc.expectedHeader)
}
})
}
}
func TestParseCodeLine(t *testing.T) {
cases := []struct {
in string
expected string
isPing bool
}{
{"SPAMD/1.1 0 EX_OK", "", false},
{"SPAMD/1.1 0", "", false},
{"SPAMD/1.0 0 EX_OK", "", false},
{"", "EOF", false},
{"SPAMD/", "short response", false},
{"SPAMD/1.", "short response", false},
{"SPAMD/1.1", "short response", false},
{"SPAMD/1.2 0 EX_OK", "unknown server protocol", false},
{"SPAMD/1 0 EX_OK", "unknown server protocol", false},
{"SPAMD/1.1 a EX_OK", "could not parse return code", false},
{"SPAMD/1.1 EX_OK", "could not parse return code", false},
{"SPAMD/1.1 65 EX_OK", "65: Data format error", false},
{"SPAMD/1.1 99 A message", "99: A message", false},
{"SPAMD/1.5 0 PONG", "", true},
{"SPAMD/1.1 0 PONG", "unexpected", true},
{"SPAMD/1.5 65 PONG", "code 65", true},
}
for _, tc := range cases {
t.Run(tc.in, func(t *testing.T) {
out := parseCodeLine(textproto.NewReader(bufio.NewReader(strings.NewReader(tc.in))), tc.isPing)
if !test.ErrorContains(out, tc.expected) {
t.Errorf("wrong error; want «%v», got «%v»", tc.expected, out)
}
})
}
}
func TestParseSpamHeader(t *testing.T) {
cases := []struct {
in Header
wantIsSpam bool
wantScore, wantBaseScore float64
wantErr string
}{
// Invalid data
{Header{}, false, 0, 0, "header missing"},
{Header{"Spam": ""}, false, 0, 0, "header missing"},
{
Header{"Spam": "clearly incorrect"},
false, 0, 0, "unexpected data",
},
{
Header{"Spam": "bacon ; 0 / 0"},
false, 0, 0, "unknown spam status",
},
{
Header{"Spam": "no ; 0 "},
false, 0, 0, "unexpected data",
},
{
Header{"Spam": "no ; 0 / "},
false, 0, 0, "could not parse",
},
{
Header{"Spam": "no ; 0 / asd"},
false, 0, 0, "could not parse",
},
{
Header{"Spam": "no ; asd / 0"},
false, 0, 0, "could not parse",
},
// Valid data
{
Header{"Spam": "no ; 0.1 / 5.0"},
false, .1, 5.0, "",
},
{
Header{"Spam": "no;0.1 / 5.0"},
false, .1, 5.0, "",
},
{
Header{"Spam": "no;0.1/5.0"},
false, .1, 5.0, "",
},
{
Header{"Spam": "no;-0.1/5.0"},
false, -.1, 5.0, "",
},
{
Header{"Spam": "TRUe ; 4 / 7.0"},
true, 4.0, 7.0, "",
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
isSpam, score, baseScore, err := parseSpamHeader(tc.in)
if isSpam != tc.wantIsSpam {
t.Errorf("isSpam wrong\nout: %#v\nwant: %#v\n",
isSpam, tc.wantIsSpam)
}
if score != tc.wantScore {
t.Errorf("score wrong\nout: %#v\nwant: %#v\n",
score, tc.wantScore)
}
if baseScore != tc.wantBaseScore {
t.Errorf("baseScore wrong\nout: %#v\nwant: %#v\n",
baseScore, tc.wantBaseScore)
}
if !test.ErrorContains(err, tc.wantErr) {
t.Errorf("error wrong\nout: %#v\nwant: %#v\n",
err, tc.wantErr)
}
})
}
}
func TestParseReport(t *testing.T) {
cases := []struct {
in string
want Report
}{
{
normalizeSpace(`
Spam detection software, running on the system "d311d8df23f8",
has NOT identified this incoming email as spam.
Content preview: the body [...]
Content analysis details: (1.6 points, 5.0 required)
pts rule name description
---- ---------------------- --------------------------------------------------
0.4 INVALID_DATE Invalid Date: header (not RFC 2822)
-0.0 NO_RELAYS Informational: message was not relayed via SMTP
-1.2 MISSING_HEADERS Missing To: header
0.0 HEADER_FROM_DIFFERENT_DOMAINS From and EnvelopeFrom 2nd level mail are different
`),
Report{
Intro: normalizeSpace(`
Spam detection software, running on the system "d311d8df23f8",
has NOT identified this incoming email as spam.
Content preview: the body [...]
Content analysis details: (1.6 points, 5.0 required)
`),
Table: []struct {
Points float64
Rule string
Description string
}{
{
Points: 0.4,
Rule: "INVALID_DATE",
Description: "Invalid Date: header (not RFC 2822)",
},
{
Points: -0.0,
Rule: "NO_RELAYS",
Description: "Informational: message was not relayed via SMTP",
},
{
Points: -1.2,
Rule: "MISSING_HEADERS",
Description: "Missing To: header",
},
{
Points: 0.0,
Rule: "HEADER_FROM_DIFFERENT_DOMAINS",
Description: "From and EnvelopeFrom 2nd level mail are different",
},
},
},
},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
tp := textproto.NewReader(bufio.NewReader(strings.NewReader(tc.in)))
out, err := parseReport(tp)
if err != nil {
t.Fatal(err)
}
if d := diff.TextDiff(tc.want.Intro, out.Intro); d != "" {
t.Errorf("intro wrong\n%v", d)
}
if !reflect.DeepEqual(out.Table, tc.want.Table) {
t.Errorf("wrong table\nout: %#v\nwant: %#v\n",
out.Table, tc.want.Table)
}
if !t.Failed() {
tc.in += "\n"
s := out.String()
if d := diff.TextDiff(s, tc.in); d != "" {
t.Errorf("String() not the same\n%v", d)
}
}
})
}
}
type tr struct{}
func (t tr) Read([]byte) (int, error) { return 0, nil }
func TestSizeFromReader(t *testing.T) {
err := ioutil.WriteFile("/tmp/xxx", []byte("xxx"), 0777)
if err != nil {
t.Fatal(err)
}
fp, err := os.Open("/tmp/xxx")
if err != nil {
t.Fatal(err)
}
cases := []struct {
in io.Reader
want int64
wantErr string
}{
{strings.NewReader("xx"), 2, ""},
{bytes.NewReader([]byte("xx")), 2, ""},
{fp, 3, ""},
{tr{}, 0, "unknown type: spamc.tr"},
}
for i, tc := range cases {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
out, err := sizeFromReader(tc.in)
if !test.ErrorContains(err, tc.wantErr) {
t.Errorf("wrong err\nout: %#v\nwant: %#v\n", err, tc.wantErr)
}
if out != tc.want {
t.Errorf("\nout: %#v\nwant: %#v\n", out, tc.want)
}
})
}
}
func normalizeSpace(in string) string {
indent := 0
for i := 0; i < len(in); i++ {
switch in[i] {
case '\n':
// Do nothing
case '\t':
indent++
default:
break
}
}
r := ""
for _, line := range strings.Split(in, "\n") {
r += strings.Replace(line, "\t", "", indent) + "\n"
}
return strings.TrimSpace(r)
}