From e688471c286e6cd4248b3a885febd732e1d6e991 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 9 Jan 2015 13:22:28 -0800 Subject: [PATCH 1/4] pkg/transport: add NewTimeoutListener test --- pkg/transport/timeout_listener_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/pkg/transport/timeout_listener_test.go b/pkg/transport/timeout_listener_test.go index 32a2f3e29..a4696cc7d 100644 --- a/pkg/transport/timeout_listener_test.go +++ b/pkg/transport/timeout_listener_test.go @@ -22,6 +22,23 @@ import ( "time" ) +// TestNewTimeoutListener tests that NewTimeoutListener returns a +// rwTimeoutListener struct with timeouts set. +func TestNewTimeoutListener(t *testing.T) { + l, err := NewTimeoutListener(":0", "http", TLSInfo{}, time.Hour, time.Hour) + if err != nil { + t.Fatalf("unexpected NewTimeoutListener error: %v", err) + } + defer l.Close() + tln := l.(*rwTimeoutListener) + if tln.rdtimeoutd != time.Hour { + t.Errorf("read timeout = %s, want %s", tln.rdtimeoutd, time.Hour) + } + if tln.wtimeoutd != time.Hour { + t.Errorf("write timeout = %s, want %s", tln.wtimeoutd, time.Hour) + } +} + func TestWriteReadTimeoutListener(t *testing.T) { ln, err := net.Listen("tcp", "127.0.0.1:0") if err != nil { From 3577ed69a2a2a76f234d03accecc122d99b2e0a6 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 9 Jan 2015 13:42:41 -0800 Subject: [PATCH 2/4] pkg/transport: add NewTimeoutTransport test --- pkg/transport/timeout_transport_test.go | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pkg/transport/timeout_transport_test.go diff --git a/pkg/transport/timeout_transport_test.go b/pkg/transport/timeout_transport_test.go new file mode 100644 index 000000000..b72ceceed --- /dev/null +++ b/pkg/transport/timeout_transport_test.go @@ -0,0 +1,51 @@ +/* + Copyright 2014 CoreOS, Inc. + + 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. +*/ + +package transport + +import ( + "net/http" + "net/http/httptest" + "testing" + "time" +) + +// TestNewTimeoutTransport tests that NewTimeoutTransport returns a transport +// that can dial out timeout connections. +func TestNewTimeoutTransport(t *testing.T) { + tr, err := NewTimeoutTransport(TLSInfo{}, time.Hour, time.Hour) + if err != nil { + t.Fatalf("unexpected NewTimeoutTransport error: %v", err) + } + srv := httptest.NewServer(http.NotFoundHandler()) + defer srv.Close() + conn, err := tr.Dial("tcp", srv.Listener.Addr().String()) + if err != nil { + t.Fatalf("unexpected dial error: %v", err) + } + defer conn.Close() + + tconn, ok := conn.(*timeoutConn) + if !ok { + t.Fatalf("failed to dial out *timeoutConn") + } + if tconn.rdtimeoutd != time.Hour { + t.Errorf("read timeout = %s, want %s", tconn.rdtimeoutd, time.Hour) + } + if tconn.wtimeoutd != time.Hour { + t.Errorf("write timeout = %s, want %s", tconn.wtimeoutd, time.Hour) + } +} From f1368a00fbb654d6c5c506373bd12c8a7d0d5688 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 9 Jan 2015 15:43:25 -0800 Subject: [PATCH 3/4] pkg/transport: add NewListener test --- pkg/transport/listener_test.go | 41 ++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/pkg/transport/listener_test.go b/pkg/transport/listener_test.go index 8d18460b1..b2c6ba9d5 100644 --- a/pkg/transport/listener_test.go +++ b/pkg/transport/listener_test.go @@ -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 { From dfb66ab8ce00386aed40e2b716351a88863e4ba2 Mon Sep 17 00:00:00 2001 From: Yicheng Qin Date: Fri, 9 Jan 2015 15:53:42 -0800 Subject: [PATCH 4/4] pkg/transport: add NewKeepAliveListener test --- pkg/transport/keepalive_listener_test.go | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 pkg/transport/keepalive_listener_test.go diff --git a/pkg/transport/keepalive_listener_test.go b/pkg/transport/keepalive_listener_test.go new file mode 100644 index 000000000..a3586556a --- /dev/null +++ b/pkg/transport/keepalive_listener_test.go @@ -0,0 +1,39 @@ +/* + Copyright 2015 CoreOS, Inc. + + 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. +*/ + +package transport + +import ( + "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) { + ln, err := NewKeepAliveListener(":0", "http", TLSInfo{}) + 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() +}