-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: return error from SendMail when required AUTH not available
Return an error if an Auth is passed to SendMail but the server does not support authentication.
- Loading branch information
Showing
2 changed files
with
51 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"net" | ||
"net/textproto" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -376,7 +377,7 @@ var helloServer = []string{ | |
"250 Reset ok\n", | ||
"221 Goodbye\n", | ||
"250 Sender ok\n", | ||
"250 ok\n" | ||
"250 ok\n", | ||
} | ||
|
||
var baseHelloClient = `EHLO customhost | ||
|
@@ -498,6 +499,49 @@ SendMail is working for me. | |
QUIT | ||
` | ||
|
||
func TestSendMailWithAuth(t *testing.T) { | ||
l, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
t.Fatalf("Unable to to create listener: %v", err) | ||
} | ||
defer l.Close() | ||
wg := sync.WaitGroup{} | ||
var done = make(chan struct{}) | ||
go func() { | ||
defer wg.Done() | ||
conn, err := l.Accept() | ||
if err != nil { | ||
t.Errorf("Accept error: %v", err) | ||
return | ||
} | ||
defer conn.Close() | ||
|
||
tc := textproto.NewConn(conn) | ||
tc.PrintfLine("220 hello world") | ||
msg, err := tc.ReadLine() | ||
if msg == "EHLO localhost" { | ||
tc.PrintfLine("250 mx.google.com at your service") | ||
} | ||
// for this test case, there should have no more traffic | ||
<-done | ||
}() | ||
wg.Add(1) | ||
|
||
err = SendMail(l.Addr().String(), sasl.NewPlainClient("", "user", "pass"), "[email protected]", []string{"[email protected]"}, strings.NewReader(strings.Replace(`From: [email protected] | ||
To: [email protected] | ||
Subject: SendMail test | ||
SendMail is working for me. | ||
`, "\n", "\r\n", -1))) | ||
if err == nil { | ||
t.Error("SendMail: Server doesn't support AUTH, expected to get an error, but got none ") | ||
} | ||
if err.Error() != "smtp: server doesn't support AUTH" { | ||
t.Errorf("Expected: smtp: server doesn't support AUTH, got: %s", err) | ||
} | ||
close(done) | ||
wg.Wait() | ||
} | ||
|
||
func TestAuthFailed(t *testing.T) { | ||
server := strings.Join(strings.Split(authFailedServer, "\n"), "\r\n") | ||
client := strings.Join(strings.Split(authFailedClient, "\n"), "\r\n") | ||
|
@@ -690,10 +734,9 @@ func init() { | |
} | ||
|
||
func sendMail(hostPort string) error { | ||
auth := sasl.NewPlainClient("", "", "") | ||
from := "[email protected]" | ||
to := []string{"[email protected]"} | ||
return SendMail(hostPort, auth, from, to, strings.NewReader("Subject: test\n\nhowdy!")) | ||
return SendMail(hostPort, nil, from, to, strings.NewReader("Subject: test\n\nhowdy!")) | ||
} | ||
|
||
// (copied from net/http/httptest) | ||
|