Skip to content

Commit 394c64c

Browse files
committed
Add tests
Signed-off-by: Fabian Ruff <[email protected]>
1 parent 1994c14 commit 394c64c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

protocol_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"crypto/tls"
1010
"crypto/x509"
1111
"fmt"
12+
"io"
13+
"io/ioutil"
1214
"net"
1315
"testing"
1416
)
@@ -741,6 +743,61 @@ func Test_MisconfiguredTLSServerRespondsWithUnderlyingError(t *testing.T) {
741743
}
742744
}
743745

746+
type testConn struct {
747+
readFromCalledWith io.Reader
748+
reads int
749+
net.Conn // nil; crash on any unexpected use
750+
}
751+
752+
func (c *testConn) ReadFrom(r io.Reader) (int64, error) {
753+
c.readFromCalledWith = r
754+
b, err := ioutil.ReadAll(r)
755+
return int64(len(b)), err
756+
}
757+
func (c *testConn) Write(p []byte) (int, error) {
758+
return len(p), nil
759+
}
760+
func (c *testConn) Read(p []byte) (int, error) {
761+
if c.reads == 0 {
762+
return 0, io.EOF
763+
}
764+
c.reads--
765+
return 1, nil
766+
}
767+
768+
func TestCopyToWrappedConnection(t *testing.T) {
769+
innerConn := &testConn{}
770+
wrappedConn := NewConn(innerConn)
771+
dummySrc := &testConn{reads: 1}
772+
773+
io.Copy(wrappedConn, dummySrc)
774+
if innerConn.readFromCalledWith != dummySrc {
775+
t.Error("Expected io.Copy to delegate to ReadFrom function of inner destination connection")
776+
}
777+
}
778+
779+
func TestCopyFromWrappedConnection(t *testing.T) {
780+
wrappedConn := NewConn(&testConn{reads: 1})
781+
dummyDst := &testConn{}
782+
783+
io.Copy(dummyDst, wrappedConn)
784+
if dummyDst.readFromCalledWith != wrappedConn.conn {
785+
t.Errorf("Expected io.Copy to pass inner source connection to ReadFrom method of destination")
786+
}
787+
}
788+
789+
func TestCopyFromWrappedConnectionToWrappedConnection(t *testing.T) {
790+
innerConn1 := &testConn{reads: 1}
791+
wrappedConn1 := NewConn(innerConn1)
792+
innerConn2 := &testConn{}
793+
wrappedConn2 := NewConn(innerConn2)
794+
795+
io.Copy(wrappedConn1, wrappedConn2)
796+
if innerConn1.readFromCalledWith != innerConn2 {
797+
t.Errorf("Expected io.Copy to pass inner source connection to ReadFrom of inner destination connection")
798+
}
799+
}
800+
744801
// copied from src/net/http/internal/testcert.go
745802

746803
// Copyright 2015 The Go Authors. All rights reserved.

0 commit comments

Comments
 (0)