2016-10-18 05:41:20 +03:00
|
|
|
package brontide
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-11-08 06:45:00 +03:00
|
|
|
"io"
|
2016-11-08 05:50:18 +03:00
|
|
|
"math"
|
2016-10-18 05:41:20 +03:00
|
|
|
"net"
|
2016-11-08 06:45:00 +03:00
|
|
|
"sync"
|
2016-10-18 05:41:20 +03:00
|
|
|
"testing"
|
|
|
|
|
2016-10-27 05:04:19 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2016-10-18 05:41:20 +03:00
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
)
|
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
func establishTestConnection() (net.Conn, net.Conn, error) {
|
2016-10-18 05:41:20 +03:00
|
|
|
// First, generate the long-term private keys both ends of the connection
|
|
|
|
// within our test.
|
|
|
|
localPriv, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
2016-11-08 06:45:00 +03:00
|
|
|
return nil, nil, err
|
2016-10-18 05:41:20 +03:00
|
|
|
}
|
|
|
|
remotePriv, err := btcec.NewPrivateKey(btcec.S256())
|
|
|
|
if err != nil {
|
2016-11-08 06:45:00 +03:00
|
|
|
return nil, nil, err
|
2016-10-18 05:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Having a port of ":0" means a random port, and interface will be
|
|
|
|
// chosen for our listener.
|
|
|
|
addr := ":0"
|
|
|
|
|
|
|
|
// Our listener will be local, and the connection remote.
|
|
|
|
listener, err := NewListener(localPriv, addr)
|
|
|
|
if err != nil {
|
2016-11-08 06:45:00 +03:00
|
|
|
return nil, nil, err
|
2016-10-18 05:41:20 +03:00
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
|
2016-10-27 05:04:19 +03:00
|
|
|
netAddr := &lnwire.NetAddress{
|
|
|
|
IdentityKey: localPriv.PubKey(),
|
|
|
|
Address: listener.Addr().(*net.TCPAddr),
|
|
|
|
}
|
2016-10-18 05:41:20 +03:00
|
|
|
|
|
|
|
// Initiate a connection with a separate goroutine, and listen with our
|
|
|
|
// main one. If both errors are nil, then encryption+auth was succesful.
|
|
|
|
errChan := make(chan error)
|
|
|
|
connChan := make(chan net.Conn)
|
|
|
|
go func() {
|
2016-10-27 05:04:19 +03:00
|
|
|
conn, err := Dial(remotePriv, netAddr)
|
2016-12-01 06:11:49 +03:00
|
|
|
|
2016-10-18 05:41:20 +03:00
|
|
|
errChan <- err
|
|
|
|
connChan <- conn
|
|
|
|
}()
|
|
|
|
|
|
|
|
localConn, listenErr := listener.Accept()
|
|
|
|
if listenErr != nil {
|
2016-12-01 06:11:49 +03:00
|
|
|
return nil, nil, listenErr
|
2016-10-18 05:41:20 +03:00
|
|
|
}
|
|
|
|
|
2016-12-01 06:11:49 +03:00
|
|
|
if dialErr := <-errChan; dialErr != nil {
|
2016-11-08 06:45:00 +03:00
|
|
|
return nil, nil, dialErr
|
|
|
|
}
|
|
|
|
remoteConn := <-connChan
|
|
|
|
|
|
|
|
return localConn, remoteConn, nil
|
|
|
|
}
|
2017-01-08 06:15:58 +03:00
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
func TestConnectionCorrectness(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
// Create a test connection, grabbing either side of the connection
|
|
|
|
// into local variables. If the initial crypto handshake fails, then
|
|
|
|
// we'll get a non-nil error here.
|
|
|
|
localConn, remoteConn, err := establishTestConnection()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to establish test connection: %v", err)
|
2016-10-18 05:41:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test out some message full-message reads.
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
msg := []byte("hello" + string(i))
|
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
if _, err := localConn.Write(msg); err != nil {
|
2016-10-18 05:41:20 +03:00
|
|
|
t.Fatalf("remote conn failed to write: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readBuf := make([]byte, len(msg))
|
2016-11-08 06:45:00 +03:00
|
|
|
if _, err := remoteConn.Read(readBuf); err != nil {
|
2016-10-18 05:41:20 +03:00
|
|
|
t.Fatalf("local conn failed to read: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(readBuf, msg) {
|
|
|
|
t.Fatalf("messages don't match, %v vs %v",
|
|
|
|
string(readBuf), string(msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now try incremental message reads. This simulates first writing a
|
|
|
|
// message header, then a message body.
|
|
|
|
outMsg := []byte("hello world")
|
2016-11-08 06:45:00 +03:00
|
|
|
if _, err := localConn.Write(outMsg); err != nil {
|
2016-10-18 05:41:20 +03:00
|
|
|
t.Fatalf("remote conn failed to write: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
readBuf := make([]byte, len(outMsg))
|
2016-11-08 06:45:00 +03:00
|
|
|
if _, err := remoteConn.Read(readBuf[:len(outMsg)/2]); err != nil {
|
2016-10-18 05:41:20 +03:00
|
|
|
t.Fatalf("local conn failed to read: %v", err)
|
|
|
|
}
|
2016-11-08 06:45:00 +03:00
|
|
|
if _, err := remoteConn.Read(readBuf[len(outMsg)/2:]); err != nil {
|
2016-10-18 05:41:20 +03:00
|
|
|
t.Fatalf("local conn failed to read: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bytes.Equal(outMsg, readBuf) {
|
|
|
|
t.Fatalf("messages don't match, %v vs %v",
|
|
|
|
string(readBuf), string(outMsg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 05:50:18 +03:00
|
|
|
func TestMaxPayloadLength(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2017-02-23 22:56:47 +03:00
|
|
|
b := Machine{}
|
2016-11-08 05:50:18 +03:00
|
|
|
b.split()
|
|
|
|
|
|
|
|
// Create a payload that's juust over the maximum alloted payload
|
|
|
|
// length.
|
|
|
|
payloadToReject := make([]byte, math.MaxUint16+1)
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// A write of the payload generated above to the state machine should
|
|
|
|
// be rejected as it's over the max payload length.
|
|
|
|
err := b.WriteMessage(&buf, payloadToReject)
|
|
|
|
if err != ErrMaxMessageLengthExceeded {
|
|
|
|
t.Fatalf("payload is over the max allowed length, the write " +
|
|
|
|
"should have been rejected")
|
|
|
|
}
|
|
|
|
|
2017-01-08 06:15:58 +03:00
|
|
|
// Generate another payload which should be accepted as a valid
|
|
|
|
// payload.
|
|
|
|
payloadToAccept := make([]byte, math.MaxUint16-1)
|
2016-11-08 05:50:18 +03:00
|
|
|
if err := b.WriteMessage(&buf, payloadToAccept); err != nil {
|
|
|
|
t.Fatalf("write for payload was rejected, should have been " +
|
|
|
|
"accepted")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate a final payload which is juuust over the max payload length
|
|
|
|
// when the MAC is accounted for.
|
2017-01-08 06:15:58 +03:00
|
|
|
payloadToReject = make([]byte, math.MaxUint16+1)
|
2016-11-08 05:50:18 +03:00
|
|
|
|
|
|
|
// This payload should be rejected.
|
|
|
|
err = b.WriteMessage(&buf, payloadToReject)
|
|
|
|
if err != ErrMaxMessageLengthExceeded {
|
|
|
|
t.Fatalf("payload is over the max allowed length, the write " +
|
|
|
|
"should have been rejected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
func TestWriteMessageChunking(t *testing.T) {
|
2017-06-17 01:59:20 +03:00
|
|
|
t.Parallel()
|
|
|
|
|
2016-11-08 06:45:00 +03:00
|
|
|
// Create a test connection, grabbing either side of the connection
|
|
|
|
// into local variables. If the initial crypto handshake fails, then
|
|
|
|
// we'll get a non-nil error here.
|
|
|
|
localConn, remoteConn, err := establishTestConnection()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to establish test connection: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to write a message which is over 3x the max allowed payload
|
|
|
|
// size.
|
|
|
|
largeMessage := bytes.Repeat([]byte("kek"), math.MaxUint16*3)
|
|
|
|
|
2016-12-13 22:31:47 +03:00
|
|
|
// Launch a new goroutine to write the large message generated above in
|
2016-11-08 06:45:00 +03:00
|
|
|
// chunks. We spawn a new goroutine because otherwise, we may block as
|
|
|
|
// the kernal waits for the buffer to flush.
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
bytesWritten, err := localConn.Write(largeMessage)
|
|
|
|
if err != nil {
|
2017-01-08 06:15:58 +03:00
|
|
|
t.Fatalf("unable to write message: %v", err)
|
2016-11-08 06:45:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// The entire message should have been written out to the remote
|
|
|
|
// connection.
|
|
|
|
if bytesWritten != len(largeMessage) {
|
|
|
|
t.Fatalf("bytes not fully written!")
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Attempt to read the entirety of the message generated above.
|
|
|
|
buf := make([]byte, len(largeMessage))
|
|
|
|
if _, err := io.ReadFull(remoteConn, buf); err != nil {
|
2017-01-08 06:15:58 +03:00
|
|
|
t.Fatalf("unable to read message: %v", err)
|
2016-11-08 06:45:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Finally, the message the remote end of the connection received
|
|
|
|
// should be identical to what we sent from the local connection.
|
|
|
|
if !bytes.Equal(buf, largeMessage) {
|
|
|
|
t.Fatalf("bytes don't match")
|
|
|
|
}
|
|
|
|
}
|