garbage_test.go raw

   1  package v2peer
   2  
   3  import (
   4  	"bytes"
   5  	"sync"
   6  	"testing"
   7  	"time"
   8  
   9  	"github.com/btcsuite/btcd/v2transport"
  10  )
  11  
  12  // testWire is a one-way pipe: writers append, the reader takes everything
  13  // queued at once.
  14  type testWire struct {
  15  	in    chan []byte
  16  	buf   bytes.Buffer
  17  	reads []int
  18  	mu    sync.Mutex
  19  }
  20  
  21  type peerConn struct {
  22  	r *testWire
  23  	w chan []byte
  24  }
  25  
  26  func (c *peerConn) Read(p []byte) (int, error) {
  27  	// Wait for junk to form: a socket does not hand a peer the wire one write
  28  	// at a time, it hands over whatever has arrived, so a reader that comes
  29  	// back for more than the current buffer gets the queued writes too.
  30  	time.Sleep(2 * time.Millisecond)
  31  	if c.r.buf.Len() == 0 {
  32  		select {
  33  		case chunk := <-c.r.in:
  34  			c.r.buf.Write(chunk)
  35  		case <-time.After(5 * time.Second):
  36  			return 0, bytes.ErrTooLarge
  37  		}
  38  		for {
  39  			select {
  40  			case more := <-c.r.in:
  41  				c.r.buf.Write(more)
  42  				continue
  43  			default:
  44  			}
  45  			break
  46  		}
  47  	}
  48  	n, err := c.r.buf.Read(p)
  49  	c.r.mu.Lock()
  50  	c.r.reads = append(c.r.reads, n)
  51  	c.r.mu.Unlock()
  52  	return n, err
  53  }
  54  
  55  func (c *peerConn) Write(p []byte) (int, error) {
  56  	c.w <- append([]byte(nil), p...)
  57  	return len(p), nil
  58  }
  59  
  60  func newPair() (*peerConn, *peerConn, chan []byte, chan []byte) {
  61  	x := make(chan []byte, 128)
  62  	y := make(chan []byte, 128)
  63  	initSide := &peerConn{r: &testWire{in: y}, w: x}
  64  	respSide := &peerConn{r: &testWire{in: x}, w: y}
  65  	return initSide, respSide, x, y
  66  }
  67  
  68  // TestBothPeersHandshakeCoalesced runs two of btcd's peers over a connection
  69  // whose reads coalesce, with and without decoy packets. If one of the pairs
  70  // hangs, this is the daemon gate's failure reproduced without a node.
  71  func TestBothPeersHandshakeCoalesced(t *testing.T) {
  72  	for _, decoys := range []int{0, 3} {
  73  		initSide, respSide, _, _ := newPair()
  74  		init := v2transport.NewPeer()
  75  		init.UseReadWriter(initSide)
  76  		resp := v2transport.NewPeer()
  77  		resp.UseReadWriter(respSide)
  78  		const net = v2transport.BitcoinNet(0xdab5bffa)
  79  		done := make(chan error, 2)
  80  		decoyLens := make([]int, decoys)
  81  		go func() {
  82  			if err := init.InitiateV2Handshake(0); err != nil {
  83  				done <- err
  84  				return
  85  			}
  86  			done <- init.CompleteHandshake(true, decoyLens, net)
  87  		}()
  88  		go func() {
  89  			if err := resp.RespondV2Handshake(0, net); err != nil {
  90  				done <- err
  91  				return
  92  			}
  93  			done <- resp.CompleteHandshake(false, decoyLens, net)
  94  		}()
  95  		ok := 0
  96  		for i := 0; i < 2; i++ {
  97  			select {
  98  			case err := <-done:
  99  				if err == nil {
 100  					ok++
 101  					continue
 102  				}
 103  				t.Fatalf("%d decoys: %v", decoys, err)
 104  			case <-time.After(5 * time.Second):
 105  				// Report the read pattern so the failure is the terminator's.
 106  				initSide.r.mu.Lock()
 107  				reads := append([]int(nil), initSide.r.reads...)
 108  				initSide.r.mu.Unlock()
 109  				t.Fatalf("%d decoys: the handshake did not finish; initiator reads %v", decoys, reads)
 110  			}
 111  		}
 112  		t.Logf("%d decoys: both sides done, initiator reads %v", decoys, readSizes(initSide.r))
 113  	}
 114  }
 115  
 116  func readSizes(w *testWire) []int {
 117  	w.mu.Lock()
 118  	defer w.mu.Unlock()
 119  	return append([]int(nil), w.reads...)
 120  }
 121