aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/server_test.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2015-04-30 17:41:27 +0800
committerPéter Szilágyi <peterke@gmail.com>2015-04-30 21:06:47 +0800
commit1528dbc17101597348eefe3f3fb8d4f0d5c54b3c (patch)
tree20f7fb8fa6d850ebc1b72740c7f9abaf548c16d5 /p2p/server_test.go
parent14f32a0c3a30c172c62272aa93f97e8a3d72ddcb (diff)
downloaddexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar.gz
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar.bz2
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar.lz
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar.xz
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.tar.zst
dexon-1528dbc17101597348eefe3f3fb8d4f0d5c54b3c.zip
p2p: add trust check to handshake, test privileged connectivity
Conflicts: p2p/server_test.go
Diffstat (limited to 'p2p/server_test.go')
-rw-r--r--p2p/server_test.go68
1 files changed, 65 insertions, 3 deletions
diff --git a/p2p/server_test.go b/p2p/server_test.go
index e99d37ed0..a79679ac1 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -22,7 +22,7 @@ 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) (*conn, error) {
+ setupFunc: func(fd net.Conn, prv *ecdsa.PrivateKey, our *protoHandshake, dial *discover.Node, atcap bool, trust map[discover.NodeID]bool) (*conn, error) {
id := randomID()
rw := newRlpxFrameRW(fd, secrets{
MAC: zero16,
@@ -102,7 +102,7 @@ func TestServerDial(t *testing.T) {
// tell the server to connect
tcpAddr := listener.Addr().(*net.TCPAddr)
- srv.trustDial <-&discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port}
+ srv.trustDial <- &discover.Node{IP: tcpAddr.IP, TCPPort: tcpAddr.Port}
select {
case conn := <-accepted:
@@ -200,7 +200,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)
+ _, err = setupConn(conn, key, hs, srv.Self(), false, nil)
if i == nconns-1 {
// When handling the last connection, the server should
// disconnect immediately instead of running the protocol
@@ -219,6 +219,68 @@ func TestServerDisconnectAtCap(t *testing.T) {
}
}
+// Tests that trusted peers and can connect above max peer caps.
+func TestServerTrustedPeers(t *testing.T) {
+ defer testlog(t).detach()
+
+ // Create a test server with limited connection slots
+ started := make(chan *Peer)
+ server := &Server{
+ ListenAddr: "127.0.0.1:0",
+ PrivateKey: newkey(),
+ MaxPeers: 3,
+ NoDial: true,
+ newPeerHook: func(p *Peer) { started <- p },
+ }
+ if err := server.Start(); err != nil {
+ t.Fatal(err)
+ }
+ defer server.Stop()
+
+ // Fill up all the slots on the server
+ dialer := &net.Dialer{Deadline: time.Now().Add(3 * time.Second)}
+ for i := 0; i < server.MaxPeers; i++ {
+ // Establish a new connection
+ conn, err := dialer.Dial("tcp", server.ListenAddr)
+ if err != nil {
+ t.Fatalf("conn %d: dial error: %v", i, err)
+ }
+ defer conn.Close()
+
+ // 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, nil); err != nil {
+ t.Fatalf("conn %d: unexpected error: %v", i, err)
+ }
+ <-started
+ }
+ // Inject a trusted node and dial that (we'll connect from this end, don't need IP setup)
+ key := newkey()
+ trusted := &discover.Node{
+ ID: discover.PubkeyID(&key.PublicKey),
+ }
+ server.TrustPeer(trusted)
+
+ conn, err := dialer.Dial("tcp", server.ListenAddr)
+ if err != nil {
+ t.Fatalf("trusted node: dial error: %v", err)
+ }
+ defer conn.Close()
+
+ shake := &protoHandshake{Version: baseProtocolVersion, ID: trusted.ID}
+ if _, err = setupConn(conn, key, shake, server.Self(), false, nil); err != nil {
+ t.Fatalf("trusted node: unexpected error: %v", err)
+ }
+ select {
+ case <-started:
+ // Ok, trusted peer accepted
+
+ case <-time.After(100 * time.Millisecond):
+ t.Fatalf("trusted node timeout")
+ }
+}
+
func newkey() *ecdsa.PrivateKey {
key, err := crypto.GenerateKey()
if err != nil {