mirror of
https://github.com/golang/go
synced 2024-11-24 02:30:12 -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:
parent
d153df8e4b
commit
9005b220e4
@ -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
|
||||
}
|
||||
|
||||
|
@ -373,6 +373,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) {
|
||||
|
Loading…
Reference in New Issue
Block a user