2019-02-01 05:16:02 +03:00
|
|
|
package wtmock
|
2018-10-24 04:28:42 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
2019-02-01 05:16:02 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtserver"
|
2018-10-24 04:28:42 +03:00
|
|
|
)
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// MockPeer emulates a single endpoint of brontide transport.
|
2018-10-24 04:28:42 +03:00
|
|
|
type MockPeer struct {
|
|
|
|
remotePub *btcec.PublicKey
|
|
|
|
remoteAddr net.Addr
|
2019-03-15 12:32:53 +03:00
|
|
|
localPub *btcec.PublicKey
|
|
|
|
localAddr net.Addr
|
2018-10-24 04:28:42 +03:00
|
|
|
|
|
|
|
IncomingMsgs chan []byte
|
|
|
|
OutgoingMsgs chan []byte
|
|
|
|
|
|
|
|
writeDeadline <-chan time.Time
|
|
|
|
readDeadline <-chan time.Time
|
|
|
|
|
2019-03-15 12:32:53 +03:00
|
|
|
RemoteQuit chan struct{}
|
|
|
|
Quit chan struct{}
|
2018-10-24 04:28:42 +03:00
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// NewMockPeer returns a fresh MockPeer.
|
2019-03-15 12:34:00 +03:00
|
|
|
func NewMockPeer(lpk, rpk *btcec.PublicKey, addr net.Addr,
|
|
|
|
bufferSize int) *MockPeer {
|
|
|
|
|
2018-10-24 04:28:42 +03:00
|
|
|
return &MockPeer{
|
2019-03-15 12:34:00 +03:00
|
|
|
remotePub: rpk,
|
2019-03-15 12:32:53 +03:00
|
|
|
remoteAddr: addr,
|
|
|
|
localAddr: &net.TCPAddr{
|
|
|
|
IP: net.IP{0x32, 0x31, 0x30, 0x29},
|
|
|
|
Port: 36723,
|
|
|
|
},
|
2019-03-15 12:34:00 +03:00
|
|
|
localPub: lpk,
|
2018-10-24 04:28:42 +03:00
|
|
|
IncomingMsgs: make(chan []byte, bufferSize),
|
|
|
|
OutgoingMsgs: make(chan []byte, bufferSize),
|
|
|
|
Quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 12:32:53 +03:00
|
|
|
// NewMockConn establishes a bidirectional connection between two MockPeers.
|
|
|
|
func NewMockConn(localPk, remotePk *btcec.PublicKey,
|
|
|
|
localAddr, remoteAddr net.Addr,
|
|
|
|
bufferSize int) (*MockPeer, *MockPeer) {
|
|
|
|
|
|
|
|
localPeer := &MockPeer{
|
|
|
|
remotePub: remotePk,
|
|
|
|
remoteAddr: remoteAddr,
|
|
|
|
localPub: localPk,
|
|
|
|
localAddr: localAddr,
|
|
|
|
IncomingMsgs: make(chan []byte, bufferSize),
|
|
|
|
OutgoingMsgs: make(chan []byte, bufferSize),
|
|
|
|
Quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
remotePeer := &MockPeer{
|
|
|
|
remotePub: localPk,
|
|
|
|
remoteAddr: localAddr,
|
|
|
|
localPub: remotePk,
|
|
|
|
localAddr: remoteAddr,
|
|
|
|
IncomingMsgs: localPeer.OutgoingMsgs,
|
|
|
|
OutgoingMsgs: localPeer.IncomingMsgs,
|
|
|
|
Quit: make(chan struct{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
localPeer.RemoteQuit = remotePeer.Quit
|
|
|
|
remotePeer.RemoteQuit = localPeer.Quit
|
|
|
|
|
|
|
|
return localPeer, remotePeer
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// Write sends the raw bytes as the next full message read to the remote peer.
|
|
|
|
// The write will fail if either party closes the connection or the write
|
|
|
|
// deadline expires. The passed bytes slice is copied before sending, thus the
|
|
|
|
// bytes may be reused once the method returns.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) Write(b []byte) (n int, err error) {
|
2019-03-15 12:32:53 +03:00
|
|
|
bb := make([]byte, len(b))
|
|
|
|
copy(bb, b)
|
|
|
|
|
2018-10-24 04:28:42 +03:00
|
|
|
select {
|
2019-03-15 12:32:53 +03:00
|
|
|
case p.OutgoingMsgs <- bb:
|
2018-10-24 04:28:42 +03:00
|
|
|
return len(b), nil
|
|
|
|
case <-p.writeDeadline:
|
|
|
|
return 0, fmt.Errorf("write timeout expired")
|
2019-03-15 12:32:53 +03:00
|
|
|
case <-p.RemoteQuit:
|
|
|
|
return 0, fmt.Errorf("remote closed connected")
|
2018-10-24 04:28:42 +03:00
|
|
|
case <-p.Quit:
|
|
|
|
return 0, fmt.Errorf("connection closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// Close tearsdown the connection, and fails any pending reads or writes.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) Close() error {
|
|
|
|
select {
|
|
|
|
case <-p.Quit:
|
|
|
|
return fmt.Errorf("connection already closed")
|
|
|
|
default:
|
|
|
|
close(p.Quit)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// ReadNextMessage returns the raw bytes of the next full message read from the
|
|
|
|
// remote peer. The read will fail if either party closes the connection or the
|
|
|
|
// read deadline expires.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) ReadNextMessage() ([]byte, error) {
|
|
|
|
select {
|
|
|
|
case b := <-p.IncomingMsgs:
|
|
|
|
return b, nil
|
|
|
|
case <-p.readDeadline:
|
|
|
|
return nil, fmt.Errorf("read timeout expired")
|
2019-03-15 12:32:53 +03:00
|
|
|
case <-p.RemoteQuit:
|
|
|
|
return nil, fmt.Errorf("remote closed connected")
|
2018-10-24 04:28:42 +03:00
|
|
|
case <-p.Quit:
|
|
|
|
return nil, fmt.Errorf("connection closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// SetWriteDeadline initializes a timer that will cause any pending writes to
|
|
|
|
// fail at time t. If t is zero, the deadline is infinite.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) SetWriteDeadline(t time.Time) error {
|
|
|
|
if t.IsZero() {
|
|
|
|
p.writeDeadline = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Until(t)
|
|
|
|
p.writeDeadline = time.After(duration)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// SetReadDeadline initializes a timer that will cause any pending reads to fail
|
|
|
|
// at time t. If t is zero, the deadline is infinite.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) SetReadDeadline(t time.Time) error {
|
|
|
|
if t.IsZero() {
|
|
|
|
p.readDeadline = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
duration := time.Until(t)
|
|
|
|
p.readDeadline = time.After(duration)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// RemotePub returns the public key of the remote peer.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) RemotePub() *btcec.PublicKey {
|
|
|
|
return p.remotePub
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// RemoteAddr returns the net address of the remote peer.
|
2018-10-24 04:28:42 +03:00
|
|
|
func (p *MockPeer) RemoteAddr() net.Addr {
|
|
|
|
return p.remoteAddr
|
|
|
|
}
|
2019-02-01 05:16:02 +03:00
|
|
|
|
2019-03-15 12:32:53 +03:00
|
|
|
// LocalAddr returns the local net address of the peer.
|
|
|
|
func (p *MockPeer) LocalAddr() net.Addr {
|
|
|
|
return p.localAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read is not implemented.
|
|
|
|
func (p *MockPeer) Read(dst []byte) (int, error) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDeadline is not implemented.
|
|
|
|
func (p *MockPeer) SetDeadline(t time.Time) error {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
2019-02-01 05:16:02 +03:00
|
|
|
// Compile-time constraint ensuring the MockPeer implements the wserver.Peer
|
|
|
|
// interface.
|
|
|
|
var _ wtserver.Peer = (*MockPeer)(nil)
|
2019-03-15 12:32:53 +03:00
|
|
|
|
|
|
|
// Compile-time constraint ensuring the MockPeer implements the net.Conn
|
|
|
|
// interface.
|
|
|
|
var _ net.Conn = (*MockPeer)(nil)
|