Simple test of Dialing TLS
package tcp
import (
+ "crypto/tls"
"net"
+ "os"
"testing"
"time"
+
+ "github.com/rqlite/rqlite/testdata/x509"
)
func Test_NewDialer(t *testing.T) {
@@ -24,7 +28,6 @@ func Test_DialerNoConnect(t *testing.T) {
func Test_DialerHeader(t *testing.T) {
s := mustNewEchoServer()
defer s.Close()
-
go s.MustStart()
d := NewDialer(64, false, false)
@@ -43,6 +46,29 @@ func Test_DialerHeader(t *testing.T) {
}
}
+func Test_DialerHeaderTLS(t *testing.T) {
+ s, cert, key := mustNewEchoServerTLS()
+ defer s.Close()
+ defer os.Remove(cert)
+ defer os.Remove(key)
+ go s.MustStart()
+
+ d := NewDialer(23, true, true)
+ conn, err := d.Dial(s.Addr(), 10*time.Second)
+ if err != nil {
+ t.Fatalf("failed to dial TLS echo server: %s", err.Error())
+ }
+
+ buf := make([]byte, 1)
+ _, err = conn.Read(buf)
+ if err != nil {
+ t.Fatalf("failed to read from TLS echo server: %s", err.Error())
+ }
+ if exp, got := buf[0], byte(23); exp != got {
+ t.Fatalf("got wrong response from TLS echo server, exp %d, got %d", exp, got)
+ }
+}
+
type echoServer struct {
ln net.Listener
}
@@ -84,3 +110,18 @@ func mustNewEchoServer() *echoServer {
ln: mustTCPListener("127.0.0.1:0"),
}
}
+
+func mustNewEchoServerTLS() (*echoServer, string, string) {
+ ln := mustTCPListener("127.0.0.1:0")
+ cert := x509.CertFile("")
+ key := x509.KeyFile("")
+
+ tlsConfig, err := createTLSConfig(cert, key, "")
+ if err != nil {
+ panic("failed to create TLS config")
+ }
+
+ return &echoServer{
+ ln: tls.NewListener(ln, tlsConfig),
+ }, cert, key
+}
