From: Jeff Date: Thu, 5 Oct 2017 17:11:17 +0000 (-0700) Subject: [release-branch.go1.9] net/smtp: NewClient: set tls field to true when already using... X-Git-Tag: go1.9.2~17 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a1e34abfb388237c46eaa133e2737a72f5693e24;p=gostls13.git [release-branch.go1.9] 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 TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor Reviewed-on: https://go-review.googlesource.com/70982 Run-TryBot: Russ Cox --- diff --git a/src/net/smtp/smtp.go b/src/net/smtp/smtp.go index 28472e447b..767b077fe0 100644 --- a/src/net/smtp/smtp.go +++ b/src/net/smtp/smtp.go @@ -67,6 +67,7 @@ func NewClient(conn net.Conn, host string) (*Client, error) { return nil, err } c := &Client{Text: text, conn: conn, serverName: host, localName: "localhost"} + _, c.tls = conn.(*tls.Conn) return c, nil } diff --git a/src/net/smtp/smtp_test.go b/src/net/smtp/smtp_test.go index ff6585e69b..606a715ce4 100644 --- a/src/net/smtp/smtp_test.go +++ b/src/net/smtp/smtp_test.go @@ -364,6 +364,53 @@ HELO localhost 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) { if len(helloServer) != len(helloClient) {