diff options
Diffstat (limited to 'p2p/server_test.go')
-rw-r--r-- | p2p/server_test.go | 147 |
1 files changed, 142 insertions, 5 deletions
diff --git a/p2p/server_test.go b/p2p/server_test.go index 3f9db343c..bf9df31ab 100644 --- a/p2p/server_test.go +++ b/p2p/server_test.go @@ -22,8 +22,11 @@ func startTestServer(t *testing.T, pf newPeerHook) *Server { ListenAddr: "127.0.0.1:0", PrivateKey: newkey(), newPeerHook: pf, - setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trusted map[discover.NodeID]bool) (*conn, error) { + setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, keepconn func(discover.NodeID) bool) (*conn, error) { id := randomID() + if !keepconn(id) { + return nil, DiscAlreadyConnected + } rw := newRlpxFrameRW(fd, secrets{ MAC: zero16, AES: zero16, @@ -200,7 +203,7 @@ func TestServerDisconnectAtCap(t *testing.T) { // Run the handshakes just like a real peer would. key := newkey() hs := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)} - _, err = setupConn(conn, key, hs, srv.Self(), false, srv.trustedNodes) + _, err = setupConn(conn, key, hs, srv.Self(), keepalways) if i == nconns-1 { // When handling the last connection, the server should // disconnect immediately instead of running the protocol @@ -250,7 +253,7 @@ func TestServerStaticPeers(t *testing.T) { // Run the handshakes just like a real peer would, and wait for completion key := newkey() shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)} - if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil { + if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil { t.Fatalf("conn %d: unexpected error: %v", i, err) } <-started @@ -344,7 +347,7 @@ func TestServerTrustedPeers(t *testing.T) { // Run the handshakes just like a real peer would, and wait for completion key := newkey() shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)} - if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil { + if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil { t.Fatalf("conn %d: unexpected error: %v", i, err) } <-started @@ -357,7 +360,7 @@ func TestServerTrustedPeers(t *testing.T) { defer conn.Close() shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID} - if _, err = setupConn(conn, key, shake, server.Self(), false, server.trustedNodes); err != nil { + if _, err = setupConn(conn, key, shake, server.Self(), keepalways); err != nil { t.Fatalf("trusted node: unexpected error: %v", err) } select { @@ -369,6 +372,136 @@ func TestServerTrustedPeers(t *testing.T) { } } +// Tests that a failed dial will temporarily throttle a peer. +func TestServerMaxPendingDials(t *testing.T) { + defer testlog(t).detach() + + // Start a simple test server + server := &Server{ + ListenAddr: "127.0.0.1:0", + PrivateKey: newkey(), + MaxPeers: 10, + MaxPendingPeers: 1, + } + if err := server.Start(); err != nil { + t.Fatal("failed to start test server: %v", err) + } + defer server.Stop() + + // Simulate two separate remote peers + peers := make(chan *discover.Node, 2) + conns := make(chan net.Conn, 2) + for i := 0; i < 2; i++ { + listener, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatalf("listener %d: failed to setup: %v", i, err) + } + defer listener.Close() + + addr := listener.Addr().(*net.TCPAddr) + peers <- &discover.Node{ + ID: discover.PubkeyID(&newkey().PublicKey), + IP: addr.IP, + TCP: uint16(addr.Port), + } + go func() { + conn, err := listener.Accept() + if err == nil { + conns <- conn + } + }() + } + // Request a dial for both peers + go func() { + for i := 0; i < 2; i++ { + server.staticDial <- <-peers // hack piggybacking the static implementation + } + }() + + // Make sure only one outbound connection goes through + var conn net.Conn + + select { + case conn = <-conns: + case <-time.After(100 * time.Millisecond): + t.Fatalf("first dial timeout") + } + select { + case conn = <-conns: + t.Fatalf("second dial completed prematurely") + case <-time.After(100 * time.Millisecond): + } + // Finish the first dial, check the second + conn.Close() + select { + case conn = <-conns: + conn.Close() + + case <-time.After(100 * time.Millisecond): + t.Fatalf("second dial timeout") + } +} + +func TestServerMaxPendingAccepts(t *testing.T) { + defer testlog(t).detach() + + // Start a test server and a peer sink for synchronization + started := make(chan *Peer) + server := &Server{ + ListenAddr: "127.0.0.1:0", + PrivateKey: newkey(), + MaxPeers: 10, + MaxPendingPeers: 1, + NoDial: true, + newPeerHook: func(p *Peer) { started <- p }, + } + if err := server.Start(); err != nil { + t.Fatal("failed to start test server: %v", err) + } + defer server.Stop() + + // Try and connect to the server on multiple threads concurrently + conns := make([]net.Conn, 2) + for i := 0; i < 2; i++ { + dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)} + + conn, err := dialer.Dial("tcp", server.ListenAddr) + if err != nil { + t.Fatalf("failed to dial server: %v", err) + } + conns[i] = conn + } + // Check that a handshake on the second doesn't pass + go func() { + key := newkey() + shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)} + if _, err := setupConn(conns[1], key, shake, server.Self(), keepalways); err != nil { + t.Fatalf("failed to run handshake: %v", err) + } + }() + select { + case <-started: + t.Fatalf("handshake on second connection accepted") + + case <-time.After(time.Second): + } + // Shake on first, check that both go through + go func() { + key := newkey() + shake := &protoHandshake{Version: baseProtocolVersion, ID: discover.PubkeyID(&key.PublicKey)} + if _, err := setupConn(conns[0], key, shake, server.Self(), keepalways); err != nil { + t.Fatalf("failed to run handshake: %v", err) + } + }() + for i := 0; i < 2; i++ { + select { + case <-started: + case <-time.After(time.Second): + t.Fatalf("peer %d: handshake timeout", i) + } + } +} + func newkey() *ecdsa.PrivateKey { key, err := crypto.GenerateKey() if err != nil { @@ -383,3 +516,7 @@ func randomID() (id discover.NodeID) { } return id } + +func keepalways(id discover.NodeID) bool { + return true +} |