p2p_unavailable_test.go raw

   1  package gate
   2  
   3  import (
   4  	"net"
   5  	"testing"
   6  	"time"
   7  )
   8  
   9  // TestPeerAcceptsWhatWeDoNot covers the last part of the handshake question
  10  // that a client-only test cannot: whether a node this machine runs ever writes
  11  // a frame without one being sent first.
  12  //
  13  // A node presents itself as soon as it accepts a connection. The nodes on this
  14  // machine write nothing after accepting, which means they are either not
  15  // running the peer listener this connection reached or are answering only
  16  // after something the client has not done. Either way, the observation is the
  17  // same on a fresh datadir and on the rig's, and it is recorded here so that a
  18  // later run can tell whether the behaviour has changed.
  19  func TestPeerAcceptsWhatWeDoNot(t *testing.T) {
  20  	addr := p2pAddr(t)
  21  	nc, err := net.Dial("tcp", addr)
  22  	if err != nil {
  23  		t.Skipf("no listener at %s: %v", addr, err)
  24  	}
  25  	defer nc.Close()
  26  	_ = nc.SetReadDeadline(time.Now().Add(3 * time.Second))
  27  	buf := make([]byte, 1024)
  28  	n, rerr := nc.Read(buf)
  29  	if n > 0 {
  30  		t.Logf("the node at %s presented itself with %d bytes before any was sent", addr, n)
  31  		return
  32  	}
  33  	t.Logf("the node at %s wrote nothing after accepting a connection: %v", addr, rerr)
  34  }
  35