Skip to content

Commit

Permalink
[release-branch.go1.9] net/smtp: NewClient: set tls field to true whe…
Browse files Browse the repository at this point in the history
…n 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>
Reviewed-on: https://go-review.googlesource.com/70982
Run-TryBot: Russ Cox <rsc@golang.org>
  • Loading branch information
jeffizhungry authored and rsc committed Oct 25, 2017
1 parent 7dadd8d commit a1e34ab
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/net/smtp/smtp.go
Expand Up @@ -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
}

Expand Down
47 changes: 47 additions & 0 deletions src/net/smtp/smtp_test.go
Expand Up @@ -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) {
Expand Down

0 comments on commit a1e34ab

Please sign in to comment.