-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransaction_auth_test.go
203 lines (195 loc) · 5.84 KB
/
transaction_auth_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
package msmtpd
import (
"crypto/tls"
"net/smtp"
"testing"
"github.com/vodolaz095/msmtpd/internal"
)
func TestAuthRejection(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
Authenticator: AuthenticatorForTestsThatAlwaysFails,
ForceTLS: true,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
err = internal.DoCommand(c.Text, 502, "AUTH")
if err != nil {
t.Errorf("%s : while making empty auth", err)
}
err = internal.DoCommand(c.Text, 502, "AUTH SOMETHING")
if err != nil {
t.Errorf("%s : while making malformed auth", err)
}
if err = c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err == nil {
t.Error("Auth worked despite rejection")
}
}
func TestAuthNotSupported(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
ForceTLS: true,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = c.Auth(smtp.PlainAuth("foo", "foo", "bar", "127.0.0.1")); err == nil {
t.Error("Auth worked despite no authenticator")
}
err = c.Close()
if err != nil {
if err.Error() != "use of closed network connection" {
t.Errorf("%s : while closing transaction", err)
}
} else {
t.Errorf("error `use of closed network connection` is not thrown - connection not closed!")
}
}
func TestAuthBypass(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
Authenticator: AuthenticatorForTestsThatAlwaysFails,
ForceTLS: true,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = c.Mail("sender@example.org"); err == nil {
t.Error("Unexpected MAIL success")
}
}
func TestAuthPlain(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
ForceTLS: true,
Authenticator: AuthenticatorForTestsThatAlwaysWorks,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
err = c.Auth(smtp.PlainAuth("", "mr.bubbles", "who cares", "127.0.0.1"))
if err != nil {
t.Errorf("%s : while performing plain authorization", err)
}
err = c.Quit()
if err != nil {
t.Errorf("%s : while performing plain authorization", err)
}
}
func TestLOGINAuth(t *testing.T) {
addr, closer := RunTestServerWithTLS(t, &Server{
Authenticator: AuthenticatorForTestsThatAlwaysWorks,
})
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "AUTH LOGIN"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "foo"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "AUTH LOGIN"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "Zm9v"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "foo"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "AUTH LOGIN"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "Zm9v"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 235, "Zm9v"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}
func TestAuthenticationErrors(t *testing.T) {
cert, err := tls.X509KeyPair(internal.LocalhostCert, internal.LocalhostKey)
if err != nil {
t.Errorf("Cert load failed: %v", err)
}
server := &Server{
Authenticator: AuthenticatorForTestsThatAlwaysWorks,
}
addr, closer := RunTestServerWithoutTLS(t, server)
defer closer()
c, err := smtp.Dial(addr)
if err != nil {
t.Errorf("Dial failed: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "AUTH PLAIN foobar"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = c.Hello("localhost"); err != nil {
t.Errorf("HELO failed: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "AUTH PLAIN foobar"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = c.Mail("sender@example.org"); err == nil {
t.Errorf("MAIL didn't fail")
}
if err = internal.DoCommand(c.Text, 502, "STARTTLS"); err != nil {
t.Errorf("STARTTLS didn't fail: %v", err)
}
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
}
if err = c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
t.Errorf("STARTTLS failed: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "AUTH UNKNOWN"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "AUTH PLAIN foobar"); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = internal.DoCommand(c.Text, 502, "AUTH PLAIN Zm9vAGJhcg=="); err != nil {
t.Errorf("AUTH didn't fail: %v", err)
}
if err = internal.DoCommand(c.Text, 334, "AUTH PLAIN"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = internal.DoCommand(c.Text, 235, "Zm9vAGJhcgBxdXV4"); err != nil {
t.Errorf("AUTH didn't work: %v", err)
}
if err = c.Mail("sender@example.org"); err != nil {
t.Errorf("MAIL failed: %v", err)
}
if err = c.Mail("sender@example.org"); err == nil {
t.Errorf("Duplicate MAIL didn't fail")
}
if err = c.Quit(); err != nil {
t.Errorf("Quit failed: %v", err)
}
}