github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/msgverack_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  	"reflect"
    11  	"testing"
    12  
    13  	"github.com/BlockABC/godash/wire"
    14  	"github.com/davecgh/go-spew/spew"
    15  )
    16  
    17  // TestVerAck tests the MsgVerAck API.
    18  func TestVerAck(t *testing.T) {
    19  	pver := wire.ProtocolVersion
    20  
    21  	// Ensure the command is expected value.
    22  	wantCmd := "verack"
    23  	msg := wire.NewMsgVerAck()
    24  	if cmd := msg.Command(); cmd != wantCmd {
    25  		t.Errorf("NewMsgVerAck: wrong command - got %v want %v",
    26  			cmd, wantCmd)
    27  	}
    28  
    29  	// Ensure max payload is expected value.
    30  	wantPayload := uint32(0)
    31  	maxPayload := msg.MaxPayloadLength(pver)
    32  	if maxPayload != wantPayload {
    33  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
    34  			"protocol version %d - got %v, want %v", pver,
    35  			maxPayload, wantPayload)
    36  	}
    37  
    38  	return
    39  }
    40  
    41  // TestVerAckWire tests the MsgVerAck wire encode and decode for various
    42  // protocol versions.
    43  func TestVerAckWire(t *testing.T) {
    44  	msgVerAck := wire.NewMsgVerAck()
    45  	msgVerAckEncoded := []byte{}
    46  
    47  	tests := []struct {
    48  		in   *wire.MsgVerAck // Message to encode
    49  		out  *wire.MsgVerAck // Expected decoded message
    50  		buf  []byte          // Wire encoding
    51  		pver uint32          // Protocol version for wire encoding
    52  	}{
    53  		// Latest protocol version.
    54  		{
    55  			msgVerAck,
    56  			msgVerAck,
    57  			msgVerAckEncoded,
    58  			wire.ProtocolVersion,
    59  		},
    60  
    61  		// Protocol version BIP0035Version.
    62  		{
    63  			msgVerAck,
    64  			msgVerAck,
    65  			msgVerAckEncoded,
    66  			wire.BIP0035Version,
    67  		},
    68  
    69  		// Protocol version BIP0031Version.
    70  		{
    71  			msgVerAck,
    72  			msgVerAck,
    73  			msgVerAckEncoded,
    74  			wire.BIP0031Version,
    75  		},
    76  
    77  		// Protocol version NetAddressTimeVersion.
    78  		{
    79  			msgVerAck,
    80  			msgVerAck,
    81  			msgVerAckEncoded,
    82  			wire.NetAddressTimeVersion,
    83  		},
    84  
    85  		// Protocol version MultipleAddressVersion.
    86  		{
    87  			msgVerAck,
    88  			msgVerAck,
    89  			msgVerAckEncoded,
    90  			wire.MultipleAddressVersion,
    91  		},
    92  	}
    93  
    94  	t.Logf("Running %d tests", len(tests))
    95  	for i, test := range tests {
    96  		// Encode the message to wire format.
    97  		var buf bytes.Buffer
    98  		err := test.in.BtcEncode(&buf, test.pver)
    99  		if err != nil {
   100  			t.Errorf("BtcEncode #%d error %v", i, err)
   101  			continue
   102  		}
   103  		if !bytes.Equal(buf.Bytes(), test.buf) {
   104  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
   105  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   106  			continue
   107  		}
   108  
   109  		// Decode the message from wire format.
   110  		var msg wire.MsgVerAck
   111  		rbuf := bytes.NewReader(test.buf)
   112  		err = msg.BtcDecode(rbuf, test.pver)
   113  		if err != nil {
   114  			t.Errorf("BtcDecode #%d error %v", i, err)
   115  			continue
   116  		}
   117  		if !reflect.DeepEqual(&msg, test.out) {
   118  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
   119  				spew.Sdump(msg), spew.Sdump(test.out))
   120  			continue
   121  		}
   122  	}
   123  }