mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
pkg/transport: add NewListener test
This commit is contained in:
parent
3577ed69a2
commit
f1368a00fb
@ -20,6 +20,7 @@ import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
@ -44,6 +45,46 @@ func fakeCertificateParserFunc(cert tls.Certificate, err error) func(certPEMBloc
|
||||
}
|
||||
}
|
||||
|
||||
// TestNewListenerTLSInfo tests that NewListener with valid TLSInfo returns
|
||||
// a TLS listerner that accepts TLS connections.
|
||||
func TestNewListenerTLSInfo(t *testing.T) {
|
||||
tmp, err := createTempFile([]byte("XXX"))
|
||||
if err != nil {
|
||||
t.Fatalf("unable to create tmpfile: %v", err)
|
||||
}
|
||||
defer os.Remove(tmp)
|
||||
tlsInfo := TLSInfo{CertFile: tmp, KeyFile: tmp}
|
||||
tlsInfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
|
||||
ln, err := NewListener(":0", "https", tlsInfo)
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected NewListener error: %v", err)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
go http.Get("https://" + ln.Addr().String())
|
||||
conn, err := ln.Accept()
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected Accept error: %v", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
if _, ok := conn.(*tls.Conn); !ok {
|
||||
t.Errorf("failed to accept *tls.Conn")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewListenerTLSInfoNonexist(t *testing.T) {
|
||||
tlsInfo := TLSInfo{CertFile: "@badname", KeyFile: "@badname"}
|
||||
_, err := NewListener(":0", "https", tlsInfo)
|
||||
werr := &os.PathError{
|
||||
Op: "open",
|
||||
Path: "@badname",
|
||||
Err: errors.New("no such file or directory"),
|
||||
}
|
||||
if err.Error() != werr.Error() {
|
||||
t.Errorf("err = %v, want %v", err, werr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTransportTLSInfo(t *testing.T) {
|
||||
tmp, err := createTempFile([]byte("XXX"))
|
||||
if err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user