-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers_test.go
121 lines (115 loc) · 4.97 KB
/
helpers_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
package msmtpd
import (
"testing"
)
func TestWrap(t *testing.T) {
cases := map[string]string{
"foobar": "foobar",
"foobar quux": "foobar quux",
"foobar\r\n": "foobar\r\n",
"foobar\r\nquux": "foobar\r\nquux",
"foobar quux foobar quux foobar quux foobar quux foobar quux foobar quux foobar quux foobar quux": "foobar quux foobar quux foobar quux foobar quux foobar quux foobar quux foobar\r\n\tquux foobar quux",
"foobar quux foobar quux foobar quux foobar quux foobar quux foobar\r\n\tquux foobar quux foobar quux": "foobar quux foobar quux foobar quux foobar quux foobar quux foobar\r\n\tquux foobar quux foobar quux",
}
for k, v := range cases {
if string(wrap([]byte(k))) != v {
t.Fatal("Didn't wrap correctly.")
}
}
}
func TestParseLine(t *testing.T) {
commandForParseLine := parseLine("HELO hostname")
if commandForParseLine.action != "HELO" {
t.Fatalf("unexpected action: %s", commandForParseLine.action)
}
if len(commandForParseLine.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(commandForParseLine.fields))
}
if len(commandForParseLine.params) != 1 {
t.Fatalf("unexpected params length: %d", len(commandForParseLine.params))
}
if commandForParseLine.params[0] != "hostname" {
t.Fatalf("unexpected value for param 0: %v", commandForParseLine.params[0])
}
commandForParseLine = parseLine("DATA")
if commandForParseLine.action != "DATA" {
t.Fatalf("unexpected action: %s", commandForParseLine.action)
}
if len(commandForParseLine.fields) != 1 {
t.Fatalf("unexpected fields length: %d", len(commandForParseLine.fields))
}
if commandForParseLine.params != nil {
t.Fatalf("unexpected params: %v", commandForParseLine.params)
}
commandForParseLine = parseLine("MAIL FROM:<test@example.org>")
if commandForParseLine.action != "MAIL" {
t.Fatalf("unexpected action: %s", commandForParseLine.action)
}
if len(commandForParseLine.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(commandForParseLine.fields))
}
if len(commandForParseLine.params) != 2 {
t.Fatalf("unexpected params length: %d", len(commandForParseLine.params))
}
if commandForParseLine.params[0] != "FROM" {
t.Fatalf("unexpected value for param 0: %v", commandForParseLine.params[0])
}
if commandForParseLine.params[1] != "<test@example.org>" {
t.Fatalf("unexpected value for param 1: %v", commandForParseLine.params[1])
}
}
func TestParseLineMalformedMAILFROM(t *testing.T) {
commandForParseMalformedMailFrom := parseLine("MAIL FROM: <test@example.org>")
if commandForParseMalformedMailFrom.action != "MAIL" {
t.Fatalf("unexpected action: %s", commandForParseMalformedMailFrom.action)
}
if len(commandForParseMalformedMailFrom.fields) != 2 {
t.Fatalf("unexpected fields length: %d", len(commandForParseMalformedMailFrom.fields))
}
if len(commandForParseMalformedMailFrom.params) != 2 {
t.Fatalf("unexpected params length: %d", len(commandForParseMalformedMailFrom.params))
}
if commandForParseMalformedMailFrom.params[0] != "FROM" {
t.Fatalf("unexpected value for param 0: %v", commandForParseMalformedMailFrom.params[0])
}
if commandForParseMalformedMailFrom.params[1] != "<test@example.org>" {
t.Fatalf("unexpected value for param 1: %v", commandForParseMalformedMailFrom.params[1])
}
}
func TestMask(t *testing.T) {
masked := mask("thisIsNotAPassword")
if masked != "t****" {
t.Errorf("mask not works - %s", masked)
}
}
func TestDecodeBase64EncodedSubject(t *testing.T) {
var decoded string
var err error
cases := map[string]string{
"=?UTF-8?B?0YHQvtC9INCh0LLQtdGC0LvQsNC90Ys=?=": "сон Светланы",
"=?utf-8?b?RXh0ZXJuYWwgYW5zaWJsZSByZXBvcnQgLSDQstGB0ZEg0YDQsNCx0L7RgtCw0LXRgg==?=": "External ansible report - всё работает",
"=?UTF-8?b?RXh0ZXJuYWwgYW5zaWJsZSByZXBvcnQgLSDQstGB0ZEg0YDQsNCx0L7RgtCw0LXRgg==?=": "External ansible report - всё работает",
"=?utf-8?B?0JfQsNC00LDQudGC0LUg0LLQvtC/0YDQvtGBINC+INC/0YDQvtC40YE=?= =?utf-8?B?0YXQvtC20LTQtdC90LjQuCE=?=": "Задайте вопрос о происхождении!",
"pavelblossom =?UTF-8?B?0YPQv9C+0LzRj9C90YPQuyDQstCw0YEg0LI=?= =?UTF-8?B?INCQ0YHRgtGA0LDQuy7QrdCU0J4=?=": "pavelblossom упомянул вас в Астрал.ЭДО",
"=?utf-8?B?8J+TlSDQntGE0LvQsNC50L0t0LvQtdC60YbQuNGPINGN0LrRgdC/0LU=?= =?utf-8?B?0YDRgtCwIEdlbm90ZWsh?=": "📕 Офлайн-лекция эксперта Genotek!",
}
for k, v := range cases {
decoded, err = decodeBase64EncodedSubject(k)
if err != nil {
t.Errorf("%s : while decoding `%s` into `%s`", err, k, v)
} else {
if decoded != v {
t.Errorf("wrong data decoded - `%s` into `%s`", decoded, v)
}
}
}
erroneous, err := decodeBase64EncodedSubject("=?utf-8?B?этоНеbase64?=")
if err != nil {
if err.Error() != "illegal base64 data at input byte 0" {
t.Errorf("%s : while decoding", err)
}
}
if erroneous != "=?utf-8?B?этоНеbase64?=" {
t.Error("should not modify")
}
}