-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth_test.go
215 lines (162 loc) · 5.58 KB
/
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
204
205
206
207
208
209
210
211
212
213
214
215
package smtpd_test
import (
"crypto/tls"
"net/smtp"
"testing"
"time"
"github.com/mailproto/smtpd"
)
type TestUser struct {
username string
password string
}
func (t *TestUser) IsUser(ident string) bool {
return true
}
func (t *TestUser) Password() string {
return t.password
}
func TestSMTPAuthPlain(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
serverAuth := smtpd.NewAuth()
serverAuth.Extend("PLAIN", &smtpd.AuthPlain{
Auth: func(username, password string) (smtpd.AuthUser, bool) {
return &TestUser{}, true
},
})
server.Auth = serverAuth
server.TLSConfig = TestingTLSConfig()
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
if err := c.StartTLS(&tls.Config{ServerName: server.Name, InsecureSkipVerify: true}); err != nil {
t.Errorf("Should be able to negotiate some TLS? %v", err)
}
auth := smtp.PlainAuth("", "user@example.com", "password", "127.0.0.1")
if err := c.Auth(auth); err != nil {
t.Errorf("Auth should have succeeded: %v", err)
}
}
func TestSMTPAuthPlainRejection(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
passwd := map[string]string{
"user@example.com": "password",
"user@example.ca": "canadian-password",
}
serverAuth := smtpd.NewAuth()
serverAuth.Extend("PLAIN", &smtpd.AuthPlain{
Auth: func(username, password string) (smtpd.AuthUser, bool) {
if passwd[username] == password {
return &TestUser{username, password}, true
}
return nil, false
},
})
server.Auth = serverAuth
server.TLSConfig = TestingTLSConfig()
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
return
}
c.StartTLS(&tls.Config{ServerName: server.Name, InsecureSkipVerify: true})
auth := smtp.PlainAuth("", "user@example.com", "password", "127.0.0.1")
if err := c.Auth(auth); err != nil {
t.Errorf("Auth should have succeded! %v", err)
}
// Connect to the remote SMTP server.
c, err = smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
return
}
c.StartTLS(&tls.Config{ServerName: server.Name, InsecureSkipVerify: true})
auth = smtp.PlainAuth("", "user@example.ca", "password", "127.0.0.1")
if err := c.Auth(auth); err == nil {
t.Errorf("Auth should have failed!")
}
}
func TestSMTPAuthLocking(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
serverAuth := smtpd.NewAuth()
serverAuth.Extend("PLAIN", &smtpd.AuthPlain{
Auth: func(username, password string) (smtpd.AuthUser, bool) {
return &TestUser{}, true
},
})
server.Auth = serverAuth
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
if err := c.Mail("sender@example.org"); err == nil {
t.Errorf("Should not be able to set a sender before Authenticating")
}
}
func TestSMTPAuthPlainEncryption(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
serverAuth := smtpd.NewAuth()
serverAuth.Extend("PLAIN", &smtpd.AuthPlain{
Auth: func(username, password string) (smtpd.AuthUser, bool) {
return &TestUser{}, true
},
})
server.Auth = serverAuth
server.TLSConfig = TestingTLSConfig()
go server.ListenAndServe("localhost:0")
defer server.Close()
time.Sleep(time.Second)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
auth := smtp.PlainAuth("", "user@example.com", "password", "127.0.0.1")
if err := c.Auth(auth); err == nil {
t.Errorf("Should not be able to do PLAIN auth on an unencrypted connection")
}
}
func TestSMTPAuthCramMd5(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
serverAuth := smtpd.NewAuth()
serverAuth.Extend("CRAM-MD5", &smtpd.AuthCramMd5{
FindUser: func(username string) (smtpd.AuthUser, error) {
return &TestUser{"user@test.com", "password"}, nil
},
})
server.Auth = serverAuth
server.TLSConfig = TestingTLSConfig()
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
if err := c.StartTLS(&tls.Config{ServerName: server.Name, InsecureSkipVerify: true}); err != nil {
t.Errorf("Should be able to negotiate some TLS? %v", err)
}
auth := smtp.CRAMMD5Auth("user@test.com", "password")
if err := c.Auth(auth); err != nil {
t.Errorf("Auth should have succeeded: %v", err)
}
}