-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathself_test.go
79 lines (67 loc) · 1.4 KB
/
self_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
package dovecotsasl
import (
"errors"
"io/ioutil"
"net"
"os"
"testing"
"github.com/emersion/go-sasl"
)
func testDir(t *testing.T) string {
t.Helper()
dir, err := ioutil.TempDir("", "dovecot-sasl-tests-")
if err != nil {
t.Fatal(err)
}
return dir
}
func testListener(t *testing.T) net.Listener {
t.Helper()
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
return l
}
func testDial(t *testing.T, l net.Listener) net.Conn {
t.Helper()
conn, err := net.Dial(l.Addr().Network(), l.Addr().String())
if err != nil {
t.Fatal(err)
}
return conn
}
func TestSaslPlain(t *testing.T) {
dir := testDir(t)
defer os.RemoveAll(dir)
authenticator := func(_, user, pass string) error {
if user == "foxcpp" && pass == "1234" {
return nil
}
return errors.New("nope")
}
s := NewServer()
s.AddMechanism("PLAIN", Mechanism{Plaintext: true}, func(req *AuthReq) sasl.Server {
return sasl.NewPlainServer(authenticator)
})
defer s.Close()
l := testListener(t)
go s.Serve(l)
conn := testDial(t, l)
cl, err := NewClient(conn)
if err != nil {
t.Fatal(err)
}
err = cl.Do("smtp", sasl.NewPlainClient("", "foxcpp", "1234"), Secured)
if err != nil {
t.Fatal(err)
}
err = cl.Do("smtp", sasl.NewPlainClient("", "foxcpp", "5678"), Secured)
if err == nil {
t.Fatal("Expected an error")
}
_, ok := err.(AuthFail)
if !ok {
t.Fatal("Error is not an auth fail:", err)
}
}