-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth_test.go
115 lines (99 loc) · 2.43 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
package mailx
import (
"net/smtp"
"testing"
)
func TestLoginAuth(t *testing.T) {
smtpUser := "user"
smtpPass := "pass"
smtpHost := "smtp.example.com"
auth := &loginAuth{
username: smtpUser,
password: smtpPass,
host: smtpHost,
}
server := &smtp.ServerInfo{
Name: smtpHost,
TLS: true,
Auth: []string{"LOGIN", "PLAIN"},
}
proto, toServer, err := auth.Start(server)
if err != nil {
t.Fatalf("loginAuth Start(): %s", err.Error())
}
if proto != "LOGIN" {
t.Fatalf("invalid protocol, got '%s', want 'LOGIN'", proto)
}
if toServer != nil {
t.Fatalf("invalid response, got '%s', want 'nil'", toServer)
}
toServer, err = auth.Next([]byte("Username:"), true)
if err != nil {
t.Fatalf("loginAuth Next(): %s", err.Error())
}
if string(toServer) != smtpUser {
t.Fatalf("invalid username, got '%s', want '%s'", toServer, smtpUser)
}
toServer, err = auth.Next([]byte("Password:"), true)
if err != nil {
t.Fatalf("loginAuth Next(): %s", err.Error())
}
if string(toServer) != smtpPass {
t.Fatalf("invalid password, got '%s', want '%s'", toServer, smtpPass)
}
}
func TestLoginAuthStartErr(t *testing.T) {
smtpUser := "user"
smtpPass := "pass"
smtpHost := "smtp.example.com"
auth := &loginAuth{
username: smtpUser,
password: smtpPass,
host: smtpHost,
}
server := &smtp.ServerInfo{
Name: smtpHost,
TLS: false,
Auth: []string{"LOGIN", "PLAIN"},
}
proto, toServer, err := auth.Start(server)
if err == nil || proto != "" || toServer != nil {
t.Fatalf("invalid response")
}
server = &smtp.ServerInfo{
Name: "localhost",
TLS: true,
Auth: []string{"LOGIN", "PLAIN"},
}
proto, toServer, err = auth.Start(server)
if err == nil || proto != "" || toServer != nil {
t.Fatalf("invalid response")
}
server = &smtp.ServerInfo{
Name: "abcd.example.com",
TLS: true,
Auth: []string{"LOGIN", "PLAIN"},
}
proto, toServer, err = auth.Start(server)
if err == nil || proto != "" || toServer != nil {
t.Fatalf("invalid response")
}
}
func TestLoginAuthNextErr(t *testing.T) {
smtpUser := "user"
smtpPass := "pass"
smtpHost := "smtp.example.com"
auth := &loginAuth{
username: smtpUser,
password: smtpPass,
host: smtpHost,
}
toServer, err := auth.Next([]byte("everything"), false)
if err != nil || toServer != nil {
t.Fatalf("got err: %s", err.Error())
}
toServer, err = auth.Next([]byte("everything"), true)
if err == nil || toServer != nil {
t.Fatalf("invalid response")
}
}