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