1
0
mirror of https://github.com/golang/go synced 2024-11-24 02:50:11 -07:00

net/smtp: NewClient: set tls field to true when already using a TLS connection

Change-Id: I34008f56c191df0edcaafc20d569bbc6184f89fc
Reviewed-on: https://go-review.googlesource.com/68470
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Jeff 2017-10-05 10:11:17 -07:00 committed by Ian Lance Taylor
parent d153df8e4b
commit 9005b220e4
2 changed files with 48 additions and 0 deletions

View File

@ -67,6 +67,7 @@ func NewClient(conn net.Conn, host string) (*Client, error) {
return nil, err return nil, err
} }
c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"} c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"}
_, c.tls = conn.(*tls.Conn)
return c, nil return c, nil
} }

View File

@ -373,6 +373,53 @@ HELO localhost
QUIT QUIT
` `
func TestNewClientWithTLS(t *testing.T) {
cert, err := tls.X509KeyPair(localhostCert, localhostKey)
if err != nil {
t.Fatalf("loadcert: %v", err)
}
config := tls.Config{Certificates: []tls.Certificate{cert}}
ln, err := tls.Listen("tcp", "127.0.0.1:0", &config)
if err != nil {
ln, err = tls.Listen("tcp", "[::1]:0", &config)
if err != nil {
t.Fatalf("server: listen: %s", err)
}
}
go func() {
conn, err := ln.Accept()
if err != nil {
t.Fatalf("server: accept: %s", err)
return
}
defer conn.Close()
_, err = conn.Write([]byte("220 SIGNS\r\n"))
if err != nil {
t.Fatalf("server: write: %s", err)
return
}
}()
config.InsecureSkipVerify = true
conn, err := tls.Dial("tcp", ln.Addr().String(), &config)
if err != nil {
t.Fatalf("client: dial: %s", err)
}
defer conn.Close()
client, err := NewClient(conn, ln.Addr().String())
if err != nil {
t.Fatalf("smtp: newclient: %s", err)
}
if !client.tls {
t.Errorf("client.tls Got: %t Expected: %t", client.tls, true)
}
}
func TestHello(t *testing.T) { func TestHello(t *testing.T) {
if len(helloServer) != len(helloClient) { if len(helloServer) != len(helloClient) {