|
9 | 9 | "crypto/tls" |
10 | 10 | "crypto/x509" |
11 | 11 | "fmt" |
| 12 | + "io" |
| 13 | + "io/ioutil" |
12 | 14 | "net" |
13 | 15 | "testing" |
14 | 16 | ) |
@@ -741,6 +743,61 @@ func Test_MisconfiguredTLSServerRespondsWithUnderlyingError(t *testing.T) { |
741 | 743 | } |
742 | 744 | } |
743 | 745 |
|
| 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 | + |
744 | 801 | // copied from src/net/http/internal/testcert.go |
745 | 802 |
|
746 | 803 | // Copyright 2015 The Go Authors. All rights reserved. |
|
0 commit comments