crypto/tls: add Dialer

Fixes #18482

Change-Id: I99d65dc5d824c00093ea61e7445fc121314af87f
Reviewed-on: https://go-review.googlesource.com/c/go/+/214977
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Brad Fitzpatrick 2020-01-15 19:27:32 +00:00
parent 2fcb91d134
commit 2f2a543ff4
3 changed files with 126 additions and 13 deletions

View file

@ -6,6 +6,7 @@ package tls
import (
"bytes"
"context"
"crypto"
"crypto/x509"
"encoding/json"
@ -272,6 +273,47 @@ func TestDeadlineOnWrite(t *testing.T) {
}
}
type readerFunc func([]byte) (int, error)
func (f readerFunc) Read(b []byte) (int, error) { return f(b) }
// TestDialer tests that tls.Dialer.DialContext can abort in the middle of a handshake.
// (The other cases are all handled by the existing dial tests in this package, which
// all also flow through the same code shared code paths)
func TestDialer(t *testing.T) {
ln := newLocalListener(t)
defer ln.Close()
unblockServer := make(chan struct{}) // close-only
defer close(unblockServer)
go func() {
conn, err := ln.Accept()
if err != nil {
return
}
defer conn.Close()
<-unblockServer
}()
ctx, cancel := context.WithCancel(context.Background())
d := Dialer{Config: &Config{
Rand: readerFunc(func(b []byte) (n int, err error) {
// By the time crypto/tls wants randomness, that means it has a TCP
// connection, so we're past the Dialer's dial and now blocked
// in a handshake. Cancel our context and see if we get unstuck.
// (Our TCP listener above never reads or writes, so the Handshake
// would otherwise be stuck forever)
cancel()
return len(b), nil
}),
ServerName: "foo",
}}
_, err := d.DialContext(ctx, "tcp", ln.Addr().String())
if err != context.Canceled {
t.Errorf("err = %v; want context.Canceled", err)
}
}
func isTimeoutError(err error) bool {
if ne, ok := err.(net.Error); ok {
return ne.Timeout()