github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/msgping_test.go (about)

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