github.com/lbryio/lbcd@v0.22.119/wire/msgverack_test.go (about)

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