Skip to content

Commit

Permalink
tls: catch cases where Windows doesn't deliver the TLS error in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed May 28, 2022
1 parent dacdb5f commit ce5ff06
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions p2p/security/tls/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
mrand "math/rand"
"net"
"runtime"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -73,6 +74,13 @@ func connect(t *testing.T) (net.Conn, net.Conn) {
return conn, sconn
}

func isWindowsTCPCloseError(err error) bool {
if runtime.GOOS != "windows" {
return false
}
return strings.Contains(err.Error(), "wsarecv: An existing connection was forcibly closed by the remote host")
}

func TestHandshakeSucceeds(t *testing.T) {
clientID, clientKey := createPeer(t)
serverID, serverKey := createPeer(t)
Expand Down Expand Up @@ -498,8 +506,9 @@ func TestInvalidCerts(t *testing.T) {
case err := <-clientErrChan:
require.Error(t, err)
if err.Error() != "remote error: tls: error decrypting message" &&
err.Error() != "remote error: tls: bad certificate" {
t.Fatalf("unexpected error: %s", err.Error())
err.Error() != "remote error: tls: bad certificate" &&
!isWindowsTCPCloseError(err) {
t.Errorf("unexpected error: %s", err.Error())
}
case <-time.After(250 * time.Millisecond):
t.Fatal("expected the server handshake to return")
Expand Down Expand Up @@ -540,7 +549,9 @@ func TestInvalidCerts(t *testing.T) {
t.Fatal("expected the server handshake to return")
}
require.Error(t, serverErr)
require.Contains(t, serverErr.Error(), "remote error: tls:")
if !isWindowsTCPCloseError(serverErr) {
require.Contains(t, serverErr.Error(), "remote error: tls:")
}
})
}
}

0 comments on commit ce5ff06

Please sign in to comment.