Skip to content

Commit

Permalink
brontide/noise_test: test parallel handshakes
Browse files Browse the repository at this point in the history
  • Loading branch information
cfromknecht committed Apr 1, 2018
1 parent 782a808 commit 9556674
Showing 1 changed file with 83 additions and 8 deletions.
91 changes: 83 additions & 8 deletions brontide/noise_test.go
Expand Up @@ -13,16 +13,12 @@ import (
"github.com/roasbeef/btcd/btcec"
)

func establishTestConnection() (net.Conn, net.Conn, func(), error) {
func makeListener() (*Listener, *lnwire.NetAddress, error) {
// First, generate the long-term private keys both ends of the
// connection within our test.
localPriv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, nil, nil, err
}
remotePriv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}

// Having a port of ":0" means a random port, and interface will be
Expand All @@ -32,15 +28,31 @@ func establishTestConnection() (net.Conn, net.Conn, func(), error) {
// Our listener will be local, and the connection remote.
listener, err := NewListener(localPriv, addr)
if err != nil {
return nil, nil, nil, err
return nil, nil, err
}
defer listener.Close()

netAddr := &lnwire.NetAddress{
IdentityKey: localPriv.PubKey(),
Address: listener.Addr().(*net.TCPAddr),
}

return listener, netAddr, nil
}

func establishTestConnection() (net.Conn, net.Conn, func(), error) {
listener, netAddr, err := makeListener()
if err != nil {
return nil, nil, nil, err
}
defer listener.Close()

// First, generate the long-term private keys both ends of the
// connection within our test.
remotePriv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
return nil, nil, nil, err
}

// Initiate a connection with a separate goroutine, and listen with our
// main one. If both errors are nil, then encryption+auth was
// successful.
Expand Down Expand Up @@ -134,6 +146,69 @@ func TestConnectionCorrectness(t *testing.T) {
}
}

// TestConecurrentHandshakes verifies the listener's ability to not be blocked
// by other pending handshakes. This is tested by opening multiple tcp
// connections with the listener, without completing any of the brontide acts.
// The test passes if real brontide dialer connects while the others are
// stalled.
func TestConcurrentHandshakes(t *testing.T) {
listener, netAddr, err := makeListener()
if err != nil {
t.Fatalf("unable to create listener connection: %v", err)
}
defer listener.Close()

const nblocking = 5

type maybeNetConn struct {
conn net.Conn
err error
}

// Open a handful of tcp connections, that do not complete any steps of
// the brontide handshake.
connChan := make(chan maybeNetConn)
for i := 0; i < nblocking; i++ {
go func() {
conn, err := net.Dial("tcp", listener.Addr().String())
connChan <- maybeNetConn{conn, err}
}()
}

for i := 0; i < nblocking; i++ {
result := <-connChan
if result.err != nil {
t.Fatalf("unable to tcp dial listener: %v", result.err)
}
defer result.conn.Close()
}

// Now, construct a new private key and use the brontide dialer to
// connect to the listener.
remotePriv, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("unable to generate private key: %v", err)
}

go func() {
remoteConn, err := Dial(remotePriv, netAddr, net.Dial)
connChan <- maybeNetConn{remoteConn, err}
}()

// This connection should be accepted without error, as the brontide
// connection should bypass stalled tcp connections.
_, err = listener.Accept()
if err != nil {
t.Fatalf("unable to accept dial: %v", err)
}

result := <-connChan
if result.err != nil {
t.Fatalf("unable to dial %v: %v", netAddr, result.err)
}
result.conn.Close()
}

func TestMaxPayloadLength(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 9556674

Please sign in to comment.