peer_test.go raw

   1  package v2peer
   2  
   3  import (
   4  	"context"
   5  	"io"
   6  	"net"
   7  	"os"
   8  	"testing"
   9  	"time"
  10  
  11  	"github.com/btcsuite/btcd/wire/v2"
  12  )
  13  
  14  // TestHandshakeV2WithARealNode is the measurement the whole connection exists
  15  // for: the node completes the BIP324 handshake and answers a getheaders.
  16  //
  17  // The node is the one BTCD_LITE_P2P_TEST names. Without it the test fails
  18  // rather than skipping, because a passed handshake is the claim this package
  19  // makes and a skip would hide the claim never being made.
  20  func TestHandshakeV2WithARealNode(t *testing.T) {
  21  	addr := os.Getenv("BTCD_LITE_P2P_TEST")
  22  	if addr == "" {
  23  		addr = "127.0.0.1:18444"
  24  	}
  25  	nc, err := net.DialTimeout("tcp", addr, 5*time.Second)
  26  	if err != nil {
  27  		t.Fatalf("no node at %s: %v", addr, err)
  28  	}
  29  	defer nc.Close()
  30  	p, err := Dial(Config{
  31  		Conn: nc, Net: wire.TestNet, UserAgent: "/btcd-lite:0.1.0/",
  32  		Services: wire.SFNodeNetwork, V2: true, Timeout: 20 * time.Second,
  33  		Logf: t.Logf,
  34  	})
  35  	if err != nil {
  36  		t.Fatalf("dial and handshake: %v", err)
  37  	}
  38  	defer p.Close()
  39  	if !p.V2() {
  40  		t.Fatal("the node fell back to v1; BIP324 is the transport this test is for")
  41  	}
  42  	t.Logf("negotiated protocol %d over the v2 transport, user agent %q", p.Protocol(), "btcd messages")
  43  	nonce := uint64(time.Now().UnixNano())
  44  	vtrace = func(format string, args ...any) { t.Logf(format, args...) }
  45  	if err := p.Write(wire.NewMsgPing(nonce)); err != nil {
  46  		t.Fatal(err)
  47  	}
  48  	ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
  49  	defer cancel()
  50  	for {
  51  		select {
  52  		case <-ctx.Done():
  53  			t.Fatal("the node never answered the ping")
  54  		case m, ok := <-p.Messages():
  55  			if !ok {
  56  				t.Fatalf("the connection ended: %v", p.Err())
  57  			}
  58  			if pong, ok := m.Message.(*wire.MsgPong); ok && pong.Nonce == nonce {
  59  				return
  60  			}
  61  		}
  62  	}
  63  }
  64  
  65  var _ = io.EOF
  66  
  67  // TestNodeFirstBytes reports what the node sends before its version, which is
  68  // where a handshake that decrypts but does not decode goes wrong.
  69  func TestNodeFirstBytes(t *testing.T) {
  70  	addr := os.Getenv("BTCD_LITE_P2P_TEST")
  71  	if addr == "" {
  72  		addr = "127.0.0.1:18444"
  73  	}
  74  	nc, err := net.DialTimeout("tcp", addr, 5*time.Second)
  75  	if err != nil {
  76  		t.Fatalf("no node at %s: %v", addr, err)
  77  	}
  78  	defer nc.Close()
  79  	vtrace = func(format string, args ...any) { t.Logf(format, args...) }
  80  	defer func() { vtrace = nil }()
  81  	_, err = Dial(Config{
  82  		Conn: nc, Net: wire.TestNet, UserAgent: "/btcd-lite:0.1.0/",
  83  		Services: wire.SFNodeNetwork, V2: true, Timeout: 10 * time.Second,
  84  	})
  85  	t.Logf("dial: %v", err)
  86  }
  87