-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create nettest.TestConn test for stcp
- Loading branch information
1 parent
d0ef5a3
commit 5a9c5be
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package stcp | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
"time" | ||
|
||
"github.com/SkycoinProject/dmsg" | ||
"github.com/SkycoinProject/dmsg/cipher" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/net/nettest" | ||
) | ||
|
||
func TestConn(t *testing.T) { | ||
mp := func() (c1, c2 net.Conn, stop func(), err error) { | ||
c1, c2, stop = prepareConns(t) | ||
return | ||
} | ||
nettest.TestConn(t, mp) | ||
} | ||
|
||
func prepareConns(t *testing.T) (*Conn, *Conn, func()) { | ||
aPK, aSK := cipher.GenerateKeyPair() | ||
bPK, _ := cipher.GenerateKeyPair() | ||
|
||
aConn, bConn := net.Pipe() | ||
|
||
ihs := InitiatorHandshake(aSK, dmsg.Addr{PK: aPK, Port: 1}, dmsg.Addr{PK: bPK, Port: 1}) | ||
|
||
rhs := ResponderHandshake(func(f2 Frame2) error { | ||
return nil | ||
}) | ||
|
||
var b *Conn | ||
var respErr error | ||
go func() { | ||
b, respErr = newConn(bConn, time.Now().Add(HandshakeTimeout), rhs, nil) | ||
}() | ||
|
||
a, err := newConn(aConn, time.Now().Add(HandshakeTimeout), ihs, nil) | ||
require.NoError(t, err) | ||
require.NoError(t, respErr) | ||
|
||
closeFunc := func() { | ||
require.NoError(t, a.Close()) | ||
require.NoError(t, b.Close()) | ||
} | ||
|
||
return a, b, closeFunc | ||
} |