msgping_test.go raw

   1  package wire
   2  
   3  import (
   4  	"bytes"
   5  	"io"
   6  	"reflect"
   7  	"testing"
   8  	
   9  	"github.com/davecgh/go-spew/spew"
  10  )
  11  
  12  // TestPing tests the MsgPing API against the latest protocol version.
  13  func TestPing(t *testing.T) {
  14  	pver := ProtocolVersion
  15  	// Ensure we get the same nonce back out.
  16  	nonce, e := RandomUint64()
  17  	if e != nil {
  18  		t.Errorf("RandomUint64: Error generating nonce: %v", e)
  19  	}
  20  	msg := NewMsgPing(nonce)
  21  	if msg.Nonce != nonce {
  22  		t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
  23  			msg.Nonce, nonce,
  24  		)
  25  	}
  26  	// Ensure the command is expected value.
  27  	wantCmd := "ping"
  28  	if cmd := msg.Command(); cmd != wantCmd {
  29  		t.Errorf("NewMsgPing: wrong command - got %v want %v",
  30  			cmd, wantCmd,
  31  		)
  32  	}
  33  	// Ensure max payload is expected value for latest protocol version.
  34  	wantPayload := uint32(8)
  35  	maxPayload := msg.MaxPayloadLength(pver)
  36  	if maxPayload != wantPayload {
  37  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
  38  			"protocol version %d - got %v, want %v", pver,
  39  			maxPayload, wantPayload,
  40  		)
  41  	}
  42  }
  43  
  44  // TestPingBIP0031 tests the MsgPing API against the protocol version BIP0031Version.
  45  func TestPingBIP0031(t *testing.T) {
  46  	// Use the protocol version just prior to BIP0031Version changes.
  47  	pver := BIP0031Version
  48  	enc := BaseEncoding
  49  	nonce, e := RandomUint64()
  50  	if e != nil {
  51  		t.Errorf("RandomUint64: Error generating nonce: %v", e)
  52  	}
  53  	msg := NewMsgPing(nonce)
  54  	if msg.Nonce != nonce {
  55  		t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
  56  			msg.Nonce, nonce,
  57  		)
  58  	}
  59  	// Ensure max payload is expected value for old protocol version.
  60  	wantPayload := uint32(0)
  61  	maxPayload := msg.MaxPayloadLength(pver)
  62  	if maxPayload != wantPayload {
  63  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
  64  			"protocol version %d - got %v, want %v", pver,
  65  			maxPayload, wantPayload,
  66  		)
  67  	}
  68  	// Test encode with old protocol version.
  69  	var buf bytes.Buffer
  70  	e = msg.BtcEncode(&buf, pver, enc)
  71  	if e != nil {
  72  		t.Errorf("encode of MsgPing failed %v e <%v>", msg, e)
  73  	}
  74  	// Test decode with old protocol version.
  75  	readmsg := NewMsgPing(0)
  76  	e = readmsg.BtcDecode(&buf, pver, enc)
  77  	if e != nil {
  78  		t.Errorf("decode of MsgPing failed [%v] e <%v>", buf, e)
  79  	}
  80  	// Since this protocol version doesn't support the nonce, make sure it didn't get encoded and decoded back out.
  81  	if msg.Nonce == readmsg.Nonce {
  82  		t.Errorf("Should not get same nonce for protocol version %d", pver)
  83  	}
  84  }
  85  
  86  // TestPingCrossProtocol tests the MsgPing API when encoding with the latest protocol version and decoding with
  87  // BIP0031Version.
  88  func TestPingCrossProtocol(t *testing.T) {
  89  	nonce, e := RandomUint64()
  90  	if e != nil {
  91  		t.Errorf("RandomUint64: Error generating nonce: %v", e)
  92  	}
  93  	msg := NewMsgPing(nonce)
  94  	if msg.Nonce != nonce {
  95  		t.Errorf("NewMsgPing: wrong nonce - got %v, want %v",
  96  			msg.Nonce, nonce,
  97  		)
  98  	}
  99  	// Encode with latest protocol version.
 100  	var buf bytes.Buffer
 101  	e = msg.BtcEncode(&buf, ProtocolVersion, BaseEncoding)
 102  	if e != nil {
 103  		t.Errorf("encode of MsgPing failed %v e <%v>", msg, e)
 104  	}
 105  	// Decode with old protocol version.
 106  	readmsg := NewMsgPing(0)
 107  	e = readmsg.BtcDecode(&buf, BIP0031Version, BaseEncoding)
 108  	if e != nil {
 109  		t.Errorf("decode of MsgPing failed [%v] e <%v>", buf, e)
 110  	}
 111  	// Since one of the protocol versions doesn't support the nonce, make sure it didn't get encoded and decoded back
 112  	// out.
 113  	if msg.Nonce == readmsg.Nonce {
 114  		t.Error("Should not get same nonce for cross protocol")
 115  	}
 116  }
 117  
 118  // TestPingWire tests the MsgPing wire encode and decode for various protocol versions.
 119  func TestPingWire(t *testing.T) {
 120  	tests := []struct {
 121  		in   MsgPing         // Message to encode
 122  		out  MsgPing         // Expected decoded message
 123  		buf  []byte          // Wire encoding
 124  		pver uint32          // Protocol version for wire encoding
 125  		enc  MessageEncoding // Message encoding format
 126  	}{
 127  		// Latest protocol version.
 128  		{
 129  			MsgPing{Nonce: 123123}, // 0x1e0f3
 130  			MsgPing{Nonce: 123123}, // 0x1e0f3
 131  			[]byte{0xf3, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00},
 132  			ProtocolVersion,
 133  			BaseEncoding,
 134  		},
 135  		// Protocol version BIP0031Version+1
 136  		{
 137  			MsgPing{Nonce: 456456}, // 0x6f708
 138  			MsgPing{Nonce: 456456}, // 0x6f708
 139  			[]byte{0x08, 0xf7, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00},
 140  			BIP0031Version + 1,
 141  			BaseEncoding,
 142  		},
 143  		// Protocol version BIP0031Version
 144  		{
 145  			MsgPing{Nonce: 789789}, // 0xc0d1d
 146  			MsgPing{Nonce: 0},      // No nonce for pver
 147  			[]byte{},               // No nonce for pver
 148  			BIP0031Version,
 149  			BaseEncoding,
 150  		},
 151  	}
 152  	t.Logf("Running %d tests", len(tests))
 153  	for i, test := range tests {
 154  		// Encode the message to wire format.
 155  		var buf bytes.Buffer
 156  		e := test.in.BtcEncode(&buf, test.pver, test.enc)
 157  		if e != nil {
 158  			t.Errorf("BtcEncode #%d error %v", i, e)
 159  			continue
 160  		}
 161  		if !bytes.Equal(buf.Bytes(), test.buf) {
 162  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
 163  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf),
 164  			)
 165  			continue
 166  		}
 167  		// Decode the message from wire format.
 168  		var msg MsgPing
 169  		rbuf := bytes.NewReader(test.buf)
 170  		e = msg.BtcDecode(rbuf, test.pver, test.enc)
 171  		if e != nil {
 172  			t.Errorf("BtcDecode #%d error %v", i, e)
 173  			continue
 174  		}
 175  		if !reflect.DeepEqual(msg, test.out) {
 176  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
 177  				spew.Sdump(msg), spew.Sdump(test.out),
 178  			)
 179  			continue
 180  		}
 181  	}
 182  }
 183  
 184  // TestPingWireErrors performs negative tests against wire encode and decode of MsgPing to confirm error paths work
 185  // correctly.
 186  func TestPingWireErrors(t *testing.T) {
 187  	pver := ProtocolVersion
 188  	tests := []struct {
 189  		in       *MsgPing        // value to encode
 190  		buf      []byte          // Wire encoding
 191  		pver     uint32          // Protocol version for wire encoding
 192  		enc      MessageEncoding // Message encoding format
 193  		max      int             // Max size of fixed buffer to induce errors
 194  		writeErr error           // Expected write error
 195  		readErr  error           // Expected read error
 196  	}{
 197  		// Latest protocol version with intentional read/write errors.
 198  		{
 199  			&MsgPing{Nonce: 123123}, // 0x1e0f3
 200  			[]byte{0xf3, 0xe0, 0x01, 0x00},
 201  			pver,
 202  			BaseEncoding,
 203  			2,
 204  			io.ErrShortWrite,
 205  			io.ErrUnexpectedEOF,
 206  		},
 207  	}
 208  	t.Logf("Running %d tests", len(tests))
 209  	for i, test := range tests {
 210  		// Encode to wire format.
 211  		w := newFixedWriter(test.max)
 212  		e := test.in.BtcEncode(w, test.pver, test.enc)
 213  		if e != test.writeErr {
 214  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
 215  				i, e, test.writeErr,
 216  			)
 217  			continue
 218  		}
 219  		// Decode from wire format.
 220  		var msg MsgPing
 221  		r := newFixedReader(test.max, test.buf)
 222  		e = msg.BtcDecode(r, test.pver, test.enc)
 223  		if e != test.readErr {
 224  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
 225  				i, e, test.readErr,
 226  			)
 227  			continue
 228  		}
 229  	}
 230  }
 231