2016-05-13 06:48:53 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-01-10 02:53:42 +03:00
|
|
|
|
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
2015-01-29 20:38:43 +03:00
|
|
|
"crypto/tls"
|
2016-01-07 08:59:38 +03:00
|
|
|
"net"
|
2015-01-10 02:53:42 +03:00
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestNewKeepAliveListener tests NewKeepAliveListener returns a listener
|
|
|
|
// that accepts connections.
|
|
|
|
// TODO: verify the keepalive option is set correctly
|
|
|
|
func TestNewKeepAliveListener(t *testing.T) {
|
2016-01-07 08:59:38 +03:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected listen error: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-22 07:01:21 +03:00
|
|
|
ln, err = NewKeepAliveListener(ln, "http", nil)
|
2015-01-10 02:53:42 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected NewKeepAliveListener error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go http.Get("http://" + ln.Addr().String())
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected Accept error: %v", err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
2015-01-29 20:38:43 +03:00
|
|
|
ln.Close()
|
|
|
|
|
2016-01-07 08:59:38 +03:00
|
|
|
ln, err = net.Listen("tcp", "127.0.0.1:0")
|
2017-03-07 01:08:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected Listen error: %v", err)
|
|
|
|
}
|
|
|
|
|
2015-01-29 20:38:43 +03:00
|
|
|
// tls
|
2017-04-07 23:29:54 +03:00
|
|
|
tlsinfo, del, err := createSelfCert()
|
2015-01-29 20:38:43 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create tmpfile: %v", err)
|
|
|
|
}
|
2017-04-07 23:29:54 +03:00
|
|
|
defer del()
|
|
|
|
tlsInfo := TLSInfo{CertFile: tlsinfo.CertFile, KeyFile: tlsinfo.KeyFile}
|
2015-01-29 20:38:43 +03:00
|
|
|
tlsInfo.parseFunc = fakeCertificateParserFunc(tls.Certificate{}, nil)
|
2016-03-22 07:01:21 +03:00
|
|
|
tlscfg, err := tlsInfo.ServerConfig()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected serverConfig error: %v", err)
|
|
|
|
}
|
|
|
|
tlsln, err := NewKeepAliveListener(ln, "https", tlscfg)
|
2015-01-29 20:38:43 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected NewKeepAliveListener error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go http.Get("https://" + tlsln.Addr().String())
|
|
|
|
conn, err = tlsln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected Accept error: %v", err)
|
|
|
|
}
|
|
|
|
if _, ok := conn.(*tls.Conn); !ok {
|
|
|
|
t.Errorf("failed to accept *tls.Conn")
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
tlsln.Close()
|
2015-01-10 02:53:42 +03:00
|
|
|
}
|
2015-07-14 22:18:15 +03:00
|
|
|
|
2016-03-22 07:01:21 +03:00
|
|
|
func TestNewKeepAliveListenerTLSEmptyConfig(t *testing.T) {
|
2016-01-07 08:59:38 +03:00
|
|
|
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected listen error: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-03-22 07:01:21 +03:00
|
|
|
_, err = NewKeepAliveListener(ln, "https", nil)
|
2015-07-14 22:18:15 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("err = nil, want not presented error")
|
|
|
|
}
|
|
|
|
}
|