-
Notifications
You must be signed in to change notification settings - Fork 45
/
plain_test.go
71 lines (61 loc) · 1.74 KB
/
plain_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
package sasl_test
import (
"bytes"
"errors"
"testing"
"github.com/emersion/go-sasl"
)
func TestNewPlainClient(t *testing.T) {
c := sasl.NewPlainClient("identity", "username", "password")
mech, ir, err := c.Start()
if err != nil {
t.Fatal("Error while starting client:", err)
}
if mech != "PLAIN" {
t.Error("Invalid mechanism name:", mech)
}
expected := []byte{105, 100, 101, 110, 116, 105, 116, 121, 0, 117, 115, 101, 114, 110, 97, 109, 101, 0, 112, 97, 115, 115, 119, 111, 114, 100}
if bytes.Compare(ir, expected) != 0 {
t.Error("Invalid initial response:", ir)
}
}
func TestNewPlainServer(t *testing.T) {
var authenticated = false
s := sasl.NewPlainServer(func(identity, username, password string) error {
if username != "username" {
return errors.New("Invalid username: " + username)
}
if password != "password" {
return errors.New("Invalid password: " + password)
}
if identity != "identity" {
return errors.New("Invalid identity: " + identity)
}
authenticated = true
return nil
})
challenge, done, err := s.Next(nil)
if err != nil {
t.Fatal("Error while starting server:", err)
}
if done {
t.Fatal("Done after starting server")
}
if len(challenge) > 0 {
t.Error("Invalid non-empty initial challenge:", challenge)
}
response := []byte{105, 100, 101, 110, 116, 105, 116, 121, 0, 117, 115, 101, 114, 110, 97, 109, 101, 0, 112, 97, 115, 115, 119, 111, 114, 100}
challenge, done, err = s.Next(response)
if err != nil {
t.Fatal("Error while finishing authentication:", err)
}
if !done {
t.Fatal("Authentication not finished after sending PLAIN credentials")
}
if len(challenge) > 0 {
t.Error("Invalid non-empty final challenge:", challenge)
}
if !authenticated {
t.Error("Not authenticated")
}
}