2014-10-23 10:57:54 -05:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2014-11-21 14:48:49 -06:00
|
|
|
"bytes"
|
2015-02-04 20:07:58 -06:00
|
|
|
"crypto/ecdsa"
|
2014-11-04 06:21:44 -06:00
|
|
|
"io"
|
2015-02-04 20:07:58 -06:00
|
|
|
"math/rand"
|
2014-10-23 10:57:54 -05:00
|
|
|
"net"
|
2014-11-21 14:48:49 -06:00
|
|
|
"sync"
|
2014-10-23 10:57:54 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2015-02-04 20:07:58 -06:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2014-10-23 10:57:54 -05:00
|
|
|
)
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
func startTestServer(t *testing.T, pf newPeerHook) *Server {
|
2014-11-21 14:48:49 -06:00
|
|
|
server := &Server{
|
2015-02-04 20:07:58 -06:00
|
|
|
Name: "test",
|
2014-11-21 14:48:49 -06:00
|
|
|
MaxPeers: 10,
|
|
|
|
ListenAddr: "127.0.0.1:0",
|
2015-02-04 20:07:58 -06:00
|
|
|
PrivateKey: newkey(),
|
|
|
|
newPeerHook: pf,
|
|
|
|
handshakeFunc: func(io.ReadWriter, *ecdsa.PrivateKey, *discover.Node) (id discover.NodeID, st []byte, err error) {
|
|
|
|
return randomID(), nil, err
|
|
|
|
},
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
if err := server.Start(); err != nil {
|
|
|
|
t.Fatalf("Could not start server: %v", err)
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
return server
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
func TestServerListen(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
2014-11-04 06:21:44 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// start the test server
|
|
|
|
connected := make(chan *Peer)
|
2015-02-04 20:07:58 -06:00
|
|
|
srv := startTestServer(t, func(p *Peer) {
|
|
|
|
if p == nil {
|
2014-11-21 14:48:49 -06:00
|
|
|
t.Error("peer func called with nil conn")
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
connected <- p
|
2014-11-21 14:48:49 -06:00
|
|
|
})
|
|
|
|
defer close(connected)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
|
|
|
// dial the test server
|
|
|
|
conn, err := net.DialTimeout("tcp", srv.ListenAddr, 5*time.Second)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not dial: %v", err)
|
2014-11-04 06:21:44 -06:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
defer conn.Close()
|
2014-11-04 06:21:44 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
select {
|
|
|
|
case peer := <-connected:
|
2015-02-04 20:07:58 -06:00
|
|
|
if peer.LocalAddr().String() != conn.RemoteAddr().String() {
|
2014-11-21 14:48:49 -06:00
|
|
|
t.Errorf("peer started with wrong conn: got %v, want %v",
|
2015-02-04 20:07:58 -06:00
|
|
|
peer.LocalAddr(), conn.RemoteAddr())
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("server did not accept within one second")
|
2014-11-04 06:21:44 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
func TestServerDial(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// run a one-shot TCP server to handle the connection.
|
2014-11-21 14:48:49 -06:00
|
|
|
listener, err := net.Listen("tcp", "127.0.0.1:0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("could not setup listener: %v")
|
|
|
|
}
|
|
|
|
defer listener.Close()
|
|
|
|
accepted := make(chan net.Conn)
|
|
|
|
go func() {
|
|
|
|
conn, err := listener.Accept()
|
|
|
|
if err != nil {
|
2015-02-04 20:07:58 -06:00
|
|
|
t.Error("accept error:", err)
|
|
|
|
return
|
2014-11-21 14:48:49 -06:00
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
accepted <- conn
|
|
|
|
}()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// start the server
|
2014-11-21 14:48:49 -06:00
|
|
|
connected := make(chan *Peer)
|
2015-02-04 20:07:58 -06:00
|
|
|
srv := startTestServer(t, func(p *Peer) { connected <- p })
|
2014-11-21 14:48:49 -06:00
|
|
|
defer close(connected)
|
|
|
|
defer srv.Stop()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// tell the server to connect
|
|
|
|
tcpAddr := listener.Addr().(*net.TCPAddr)
|
2015-02-06 17:15:04 -06:00
|
|
|
srv.SuggestPeer(&discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port})
|
2014-11-21 14:48:49 -06:00
|
|
|
|
|
|
|
select {
|
|
|
|
case conn := <-accepted:
|
2014-11-04 06:21:44 -06:00
|
|
|
select {
|
2014-11-21 14:48:49 -06:00
|
|
|
case peer := <-connected:
|
2015-02-04 20:07:58 -06:00
|
|
|
if peer.RemoteAddr().String() != conn.LocalAddr().String() {
|
2014-11-21 14:48:49 -06:00
|
|
|
t.Errorf("peer started with wrong conn: got %v, want %v",
|
2015-02-04 20:07:58 -06:00
|
|
|
peer.RemoteAddr(), conn.LocalAddr())
|
2014-11-04 06:21:44 -06:00
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
// TODO: validate more fields
|
2014-11-21 14:48:49 -06:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("server did not launch peer within one second")
|
2014-11-04 06:21:44 -06:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
t.Error("server did not connect within one second")
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
func TestServerBroadcast(t *testing.T) {
|
|
|
|
defer testlog(t).detach()
|
2015-02-04 20:07:58 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
var connected sync.WaitGroup
|
2015-02-04 20:07:58 -06:00
|
|
|
srv := startTestServer(t, func(p *Peer) {
|
|
|
|
p.protocols = []Protocol{discard}
|
|
|
|
p.startSubprotocols([]Cap{discard.cap()})
|
2015-02-06 17:13:22 -06:00
|
|
|
p.noHandshake = true
|
2014-11-21 14:48:49 -06:00
|
|
|
connected.Done()
|
|
|
|
})
|
|
|
|
defer srv.Stop()
|
|
|
|
|
2015-02-04 20:07:58 -06:00
|
|
|
// create a few peers
|
2014-11-21 14:48:49 -06:00
|
|
|
var conns = make([]net.Conn, 8)
|
|
|
|
connected.Add(len(conns))
|
|
|
|
deadline := time.Now().Add(3 * time.Second)
|
|
|
|
dialer := &net.Dialer{Deadline: deadline}
|
|
|
|
for i := range conns {
|
|
|
|
conn, err := dialer.Dial("tcp", srv.ListenAddr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("conn %d: dial error: %v", i, err)
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
defer conn.Close()
|
|
|
|
conn.SetDeadline(deadline)
|
|
|
|
conns[i] = conn
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
2014-11-21 14:48:49 -06:00
|
|
|
connected.Wait()
|
|
|
|
|
|
|
|
// broadcast one message
|
|
|
|
srv.Broadcast("discard", 0, "foo")
|
|
|
|
goldbuf := new(bytes.Buffer)
|
|
|
|
writeMsg(goldbuf, NewMsg(16, "foo"))
|
|
|
|
golden := goldbuf.Bytes()
|
|
|
|
|
|
|
|
// check that the message has been written everywhere
|
|
|
|
for i, conn := range conns {
|
|
|
|
buf := make([]byte, len(golden))
|
|
|
|
if _, err := io.ReadFull(conn, buf); err != nil {
|
|
|
|
t.Errorf("conn %d: read error: %v", i, err)
|
|
|
|
} else if !bytes.Equal(buf, golden) {
|
|
|
|
t.Errorf("conn %d: msg mismatch\ngot: %x\nwant: %x", i, buf, golden)
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-04 20:07:58 -06:00
|
|
|
|
|
|
|
func newkey() *ecdsa.PrivateKey {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
panic("couldn't generate key: " + err.Error())
|
|
|
|
}
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomID() (id discover.NodeID) {
|
|
|
|
for i := range id {
|
|
|
|
id[i] = byte(rand.Intn(255))
|
|
|
|
}
|
|
|
|
return id
|
|
|
|
}
|