diff options
author | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-11-06 19:49:01 +0000 |
---|---|---|
committer | Ian Lance Taylor <ian@gcc.gnu.org> | 2013-11-06 19:49:01 +0000 |
commit | f038dae646bac2b31be98ab592c0e5206d2d96f5 (patch) | |
tree | 39530b071991b2326f881b2a30a2d82d6c133fd6 /libgo/go/net/smtp | |
parent | f20f261304993444741e0f0a14d3147e591bc660 (diff) | |
download | gcc-f038dae646bac2b31be98ab592c0e5206d2d96f5.zip gcc-f038dae646bac2b31be98ab592c0e5206d2d96f5.tar.gz gcc-f038dae646bac2b31be98ab592c0e5206d2d96f5.tar.bz2 |
libgo: Update to October 24 version of master library.
From-SVN: r204466
Diffstat (limited to 'libgo/go/net/smtp')
-rw-r--r-- | libgo/go/net/smtp/smtp.go | 22 | ||||
-rw-r--r-- | libgo/go/net/smtp/smtp_test.go | 47 |
2 files changed, 63 insertions, 6 deletions
diff --git a/libgo/go/net/smtp/smtp.go b/libgo/go/net/smtp/smtp.go index 4b91778..a0a478a 100644 --- a/libgo/go/net/smtp/smtp.go +++ b/libgo/go/net/smtp/smtp.go @@ -41,12 +41,13 @@ type Client struct { } // Dial returns a new Client connected to an SMTP server at addr. +// The addr must include a port number. func Dial(addr string) (*Client, error) { conn, err := net.Dial("tcp", addr) if err != nil { return nil, err } - host := addr[:strings.Index(addr, ":")] + host, _, _ := net.SplitHostPort(addr) return NewClient(conn, host) } @@ -63,6 +64,11 @@ func NewClient(conn net.Conn, host string) (*Client, error) { return c, nil } +// Close closes the connection. +func (c *Client) Close() error { + return c.Text.Close() +} + // hello runs a hello exchange if needed. func (c *Client) hello() error { if !c.didHello { @@ -190,7 +196,9 @@ func (c *Client) Auth(a Auth) error { default: err = &textproto.Error{Code: code, Msg: msg64} } - resp, err = a.Next(msg, code == 334) + if err == nil { + resp, err = a.Next(msg, code == 334) + } if err != nil { // abort the AUTH c.cmd(501, "*") @@ -256,15 +264,17 @@ func (c *Client) Data() (io.WriteCloser, error) { return &dataCloser{c, c.Text.DotWriter()}, nil } -// SendMail connects to the server at addr, switches to TLS if possible, -// authenticates with mechanism a if possible, and then sends an email from -// address from, to addresses to, with message msg. +// SendMail connects to the server at addr, switches to TLS if +// possible, authenticates with the optional mechanism a if possible, +// and then sends an email from address from, to addresses to, with +// message msg. func SendMail(addr string, a Auth, from string, to []string, msg []byte) error { c, err := Dial(addr) if err != nil { return err } - if err := c.hello(); err != nil { + defer c.Close() + if err = c.hello(); err != nil { return err } if ok, _ := c.Extension("STARTTLS"); ok { diff --git a/libgo/go/net/smtp/smtp_test.go b/libgo/go/net/smtp/smtp_test.go index c190b32..2133dc7 100644 --- a/libgo/go/net/smtp/smtp_test.go +++ b/libgo/go/net/smtp/smtp_test.go @@ -238,6 +238,7 @@ func TestNewClient(t *testing.T) { if err != nil { t.Fatalf("NewClient: %v\n(after %v)", err, out()) } + defer c.Close() if ok, args := c.Extension("aUtH"); !ok || args != "LOGIN PLAIN" { t.Fatalf("Expected AUTH supported") } @@ -278,6 +279,7 @@ func TestNewClient2(t *testing.T) { if err != nil { t.Fatalf("NewClient: %v", err) } + defer c.Close() if ok, _ := c.Extension("DSN"); ok { t.Fatalf("Shouldn't support DSN") } @@ -323,6 +325,7 @@ func TestHello(t *testing.T) { if err != nil { t.Fatalf("NewClient: %v", err) } + defer c.Close() c.localName = "customhost" err = nil @@ -501,3 +504,47 @@ SendMail is working for me. . QUIT ` + +func TestAuthFailed(t *testing.T) { + server := strings.Join(strings.Split(authFailedServer, "\n"), "\r\n") + client := strings.Join(strings.Split(authFailedClient, "\n"), "\r\n") + var cmdbuf bytes.Buffer + bcmdbuf := bufio.NewWriter(&cmdbuf) + var fake faker + fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf) + c, err := NewClient(fake, "fake.host") + if err != nil { + t.Fatalf("NewClient: %v", err) + } + defer c.Close() + + c.tls = true + c.serverName = "smtp.google.com" + err = c.Auth(PlainAuth("", "user", "pass", "smtp.google.com")) + + if err == nil { + t.Error("Auth: expected error; got none") + } else if err.Error() != "535 Invalid credentials\nplease see www.example.com" { + t.Errorf("Auth: got error: %v, want: %s", err, "535 Invalid credentials\nplease see www.example.com") + } + + bcmdbuf.Flush() + actualcmds := cmdbuf.String() + if client != actualcmds { + t.Errorf("Got:\n%s\nExpected:\n%s", actualcmds, client) + } +} + +var authFailedServer = `220 hello world +250-mx.google.com at your service +250 AUTH LOGIN PLAIN +535-Invalid credentials +535 please see www.example.com +221 Goodbye +` + +var authFailedClient = `EHLO localhost +AUTH PLAIN AHVzZXIAcGFzcw== +* +QUIT +` |