2015-06-30 22:41:55 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/smtp"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testUser = "user"
|
|
|
|
testPwd = "pwd"
|
|
|
|
testHost = "smtp.example.com"
|
|
|
|
)
|
|
|
|
|
2016-03-07 02:17:01 +08:00
|
|
|
type authTest struct {
|
2015-06-30 22:41:55 +08:00
|
|
|
auths []string
|
|
|
|
challenges []string
|
|
|
|
tls bool
|
|
|
|
wantData []string
|
|
|
|
wantError bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoAdvertisement(t *testing.T) {
|
2016-03-07 02:17:01 +08:00
|
|
|
testLoginAuth(t, &authTest{
|
|
|
|
auths: []string{},
|
|
|
|
tls: false,
|
|
|
|
wantError: true,
|
2015-06-30 22:41:55 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNoAdvertisementTLS(t *testing.T) {
|
2016-03-07 02:17:01 +08:00
|
|
|
testLoginAuth(t, &authTest{
|
2015-06-30 22:41:55 +08:00
|
|
|
auths: []string{},
|
|
|
|
challenges: []string{"Username:", "Password:"},
|
|
|
|
tls: true,
|
2016-03-07 02:17:01 +08:00
|
|
|
wantData: []string{"", testUser, testPwd},
|
2015-06-30 22:41:55 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLogin(t *testing.T) {
|
2016-03-07 02:17:01 +08:00
|
|
|
testLoginAuth(t, &authTest{
|
|
|
|
auths: []string{"PLAIN", "LOGIN"},
|
2015-06-30 22:41:55 +08:00
|
|
|
challenges: []string{"Username:", "Password:"},
|
|
|
|
tls: false,
|
|
|
|
wantData: []string{"", testUser, testPwd},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLoginTLS(t *testing.T) {
|
2016-03-07 02:17:01 +08:00
|
|
|
testLoginAuth(t, &authTest{
|
2015-06-30 22:41:55 +08:00
|
|
|
auths: []string{"LOGIN"},
|
|
|
|
challenges: []string{"Username:", "Password:"},
|
|
|
|
tls: true,
|
|
|
|
wantData: []string{"", testUser, testPwd},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-03-07 02:17:01 +08:00
|
|
|
func testLoginAuth(t *testing.T, test *authTest) {
|
|
|
|
auth := &loginAuth{
|
2015-06-30 22:41:55 +08:00
|
|
|
username: testUser,
|
|
|
|
password: testPwd,
|
|
|
|
host: testHost,
|
|
|
|
}
|
|
|
|
server := &smtp.ServerInfo{
|
|
|
|
Name: testHost,
|
|
|
|
TLS: test.tls,
|
|
|
|
Auth: test.auths,
|
|
|
|
}
|
|
|
|
proto, toServer, err := auth.Start(server)
|
|
|
|
if err != nil && !test.wantError {
|
2016-03-07 02:17:01 +08:00
|
|
|
t.Fatalf("loginAuth.Start(): %v", err)
|
2015-06-30 22:41:55 +08:00
|
|
|
}
|
|
|
|
if err != nil && test.wantError {
|
|
|
|
return
|
|
|
|
}
|
2016-03-07 02:17:01 +08:00
|
|
|
if proto != "LOGIN" {
|
|
|
|
t.Errorf("invalid protocol, got %q, want LOGIN", proto)
|
2015-06-30 22:41:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
got := string(toServer)
|
|
|
|
if got != test.wantData[i] {
|
|
|
|
t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, challenge := range test.challenges {
|
|
|
|
i++
|
|
|
|
if i >= len(test.wantData) {
|
|
|
|
t.Fatalf("unexpected challenge: %q", challenge)
|
|
|
|
}
|
|
|
|
|
|
|
|
toServer, err = auth.Next([]byte(challenge), true)
|
|
|
|
if err != nil {
|
2016-03-07 02:17:01 +08:00
|
|
|
t.Fatalf("loginAuth.Auth(): %v", err)
|
2015-06-30 22:41:55 +08:00
|
|
|
}
|
|
|
|
got = string(toServer)
|
|
|
|
if got != test.wantData[i] {
|
|
|
|
t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|