github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/wire/msgaddr_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  	"net"
    12  	"reflect"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/BlockABC/godash/wire"
    17  	"github.com/davecgh/go-spew/spew"
    18  )
    19  
    20  // TestAddr tests the MsgAddr API.
    21  func TestAddr(t *testing.T) {
    22  	pver := wire.ProtocolVersion
    23  
    24  	// Ensure the command is expected value.
    25  	wantCmd := "addr"
    26  	msg := wire.NewMsgAddr()
    27  	if cmd := msg.Command(); cmd != wantCmd {
    28  		t.Errorf("NewMsgAddr: wrong command - got %v want %v",
    29  			cmd, wantCmd)
    30  	}
    31  
    32  	// Ensure max payload is expected value for latest protocol version.
    33  	// Num addresses (varInt) + max allowed addresses.
    34  	wantPayload := uint32(30009)
    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  	// Ensure NetAddresses are added properly.
    43  	tcpAddr := &net.TCPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8333}
    44  	na, err := wire.NewNetAddress(tcpAddr, wire.SFNodeNetwork)
    45  	if err != nil {
    46  		t.Errorf("NewNetAddress: %v", err)
    47  	}
    48  	err = msg.AddAddress(na)
    49  	if err != nil {
    50  		t.Errorf("AddAddress: %v", err)
    51  	}
    52  	if msg.AddrList[0] != na {
    53  		t.Errorf("AddAddress: wrong address added - got %v, want %v",
    54  			spew.Sprint(msg.AddrList[0]), spew.Sprint(na))
    55  	}
    56  
    57  	// Ensure the address list is cleared properly.
    58  	msg.ClearAddresses()
    59  	if len(msg.AddrList) != 0 {
    60  		t.Errorf("ClearAddresses: address list is not empty - "+
    61  			"got %v [%v], want %v", len(msg.AddrList),
    62  			spew.Sprint(msg.AddrList[0]), 0)
    63  	}
    64  
    65  	// Ensure adding more than the max allowed addresses per message returns
    66  	// error.
    67  	for i := 0; i < wire.MaxAddrPerMsg+1; i++ {
    68  		err = msg.AddAddress(na)
    69  	}
    70  	if err == nil {
    71  		t.Errorf("AddAddress: expected error on too many addresses " +
    72  			"not received")
    73  	}
    74  	err = msg.AddAddresses(na)
    75  	if err == nil {
    76  		t.Errorf("AddAddresses: expected error on too many addresses " +
    77  			"not received")
    78  	}
    79  
    80  	// Ensure max payload is expected value for protocol versions before
    81  	// timestamp was added to NetAddress.
    82  	// Num addresses (varInt) + max allowed addresses.
    83  	pver = wire.NetAddressTimeVersion - 1
    84  	wantPayload = uint32(26009)
    85  	maxPayload = msg.MaxPayloadLength(pver)
    86  	if maxPayload != wantPayload {
    87  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
    88  			"protocol version %d - got %v, want %v", pver,
    89  			maxPayload, wantPayload)
    90  	}
    91  
    92  	// Ensure max payload is expected value for protocol versions before
    93  	// multiple addresses were allowed.
    94  	// Num addresses (varInt) + a single net addresses.
    95  	pver = wire.MultipleAddressVersion - 1
    96  	wantPayload = uint32(35)
    97  	maxPayload = msg.MaxPayloadLength(pver)
    98  	if maxPayload != wantPayload {
    99  		t.Errorf("MaxPayloadLength: wrong max payload length for "+
   100  			"protocol version %d - got %v, want %v", pver,
   101  			maxPayload, wantPayload)
   102  	}
   103  
   104  	return
   105  }
   106  
   107  // TestAddrWire tests the MsgAddr wire encode and decode for various numbers
   108  // of addreses and protocol versions.
   109  func TestAddrWire(t *testing.T) {
   110  	// A couple of NetAddresses to use for testing.
   111  	na := &wire.NetAddress{
   112  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
   113  		Services:  wire.SFNodeNetwork,
   114  		IP:        net.ParseIP("127.0.0.1"),
   115  		Port:      8333,
   116  	}
   117  	na2 := &wire.NetAddress{
   118  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
   119  		Services:  wire.SFNodeNetwork,
   120  		IP:        net.ParseIP("192.168.0.1"),
   121  		Port:      8334,
   122  	}
   123  
   124  	// Empty address message.
   125  	noAddr := wire.NewMsgAddr()
   126  	noAddrEncoded := []byte{
   127  		0x00, // Varint for number of addresses
   128  	}
   129  
   130  	// Address message with multiple addresses.
   131  	multiAddr := wire.NewMsgAddr()
   132  	multiAddr.AddAddresses(na, na2)
   133  	multiAddrEncoded := []byte{
   134  		0x02,                   // Varint for number of addresses
   135  		0x29, 0xab, 0x5f, 0x49, // Timestamp
   136  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   137  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   138  		0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
   139  		0x20, 0x8d, // Port 8333 in big-endian
   140  		0x29, 0xab, 0x5f, 0x49, // Timestamp
   141  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   142  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   143  		0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01, // IP 192.168.0.1
   144  		0x20, 0x8e, // Port 8334 in big-endian
   145  
   146  	}
   147  
   148  	tests := []struct {
   149  		in   *wire.MsgAddr // Message to encode
   150  		out  *wire.MsgAddr // Expected decoded message
   151  		buf  []byte        // Wire encoding
   152  		pver uint32        // Protocol version for wire encoding
   153  	}{
   154  		// Latest protocol version with no addresses.
   155  		{
   156  			noAddr,
   157  			noAddr,
   158  			noAddrEncoded,
   159  			wire.ProtocolVersion,
   160  		},
   161  
   162  		// Latest protocol version with multiple addresses.
   163  		{
   164  			multiAddr,
   165  			multiAddr,
   166  			multiAddrEncoded,
   167  			wire.ProtocolVersion,
   168  		},
   169  
   170  		// Protocol version MultipleAddressVersion-1 with no addresses.
   171  		{
   172  			noAddr,
   173  			noAddr,
   174  			noAddrEncoded,
   175  			wire.MultipleAddressVersion - 1,
   176  		},
   177  	}
   178  
   179  	t.Logf("Running %d tests", len(tests))
   180  	for i, test := range tests {
   181  		// Encode the message to wire format.
   182  		var buf bytes.Buffer
   183  		err := test.in.BtcEncode(&buf, test.pver)
   184  		if err != nil {
   185  			t.Errorf("BtcEncode #%d error %v", i, err)
   186  			continue
   187  		}
   188  		if !bytes.Equal(buf.Bytes(), test.buf) {
   189  			t.Errorf("BtcEncode #%d\n got: %s want: %s", i,
   190  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   191  			continue
   192  		}
   193  
   194  		// Decode the message from wire format.
   195  		var msg wire.MsgAddr
   196  		rbuf := bytes.NewReader(test.buf)
   197  		err = msg.BtcDecode(rbuf, test.pver)
   198  		if err != nil {
   199  			t.Errorf("BtcDecode #%d error %v", i, err)
   200  			continue
   201  		}
   202  		if !reflect.DeepEqual(&msg, test.out) {
   203  			t.Errorf("BtcDecode #%d\n got: %s want: %s", i,
   204  				spew.Sdump(msg), spew.Sdump(test.out))
   205  			continue
   206  		}
   207  	}
   208  }
   209  
   210  // TestAddrWireErrors performs negative tests against wire encode and decode
   211  // of MsgAddr to confirm error paths work correctly.
   212  func TestAddrWireErrors(t *testing.T) {
   213  	pver := wire.ProtocolVersion
   214  	pverMA := wire.MultipleAddressVersion
   215  	wireErr := &wire.MessageError{}
   216  
   217  	// A couple of NetAddresses to use for testing.
   218  	na := &wire.NetAddress{
   219  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
   220  		Services:  wire.SFNodeNetwork,
   221  		IP:        net.ParseIP("127.0.0.1"),
   222  		Port:      8333,
   223  	}
   224  	na2 := &wire.NetAddress{
   225  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
   226  		Services:  wire.SFNodeNetwork,
   227  		IP:        net.ParseIP("192.168.0.1"),
   228  		Port:      8334,
   229  	}
   230  
   231  	// Address message with multiple addresses.
   232  	baseAddr := wire.NewMsgAddr()
   233  	baseAddr.AddAddresses(na, na2)
   234  	baseAddrEncoded := []byte{
   235  		0x02,                   // Varint for number of addresses
   236  		0x29, 0xab, 0x5f, 0x49, // Timestamp
   237  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   238  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   239  		0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
   240  		0x20, 0x8d, // Port 8333 in big-endian
   241  		0x29, 0xab, 0x5f, 0x49, // Timestamp
   242  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   243  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   244  		0x00, 0x00, 0xff, 0xff, 0xc0, 0xa8, 0x00, 0x01, // IP 192.168.0.1
   245  		0x20, 0x8e, // Port 8334 in big-endian
   246  
   247  	}
   248  
   249  	// Message that forces an error by having more than the max allowed
   250  	// addresses.
   251  	maxAddr := wire.NewMsgAddr()
   252  	for i := 0; i < wire.MaxAddrPerMsg; i++ {
   253  		maxAddr.AddAddress(na)
   254  	}
   255  	maxAddr.AddrList = append(maxAddr.AddrList, na)
   256  	maxAddrEncoded := []byte{
   257  		0xfd, 0x03, 0xe9, // Varint for number of addresses (1001)
   258  	}
   259  
   260  	tests := []struct {
   261  		in       *wire.MsgAddr // Value to encode
   262  		buf      []byte        // Wire encoding
   263  		pver     uint32        // Protocol version for wire encoding
   264  		max      int           // Max size of fixed buffer to induce errors
   265  		writeErr error         // Expected write error
   266  		readErr  error         // Expected read error
   267  	}{
   268  		// Latest protocol version with intentional read/write errors.
   269  		// Force error in addresses count
   270  		{baseAddr, baseAddrEncoded, pver, 0, io.ErrShortWrite, io.EOF},
   271  		// Force error in address list.
   272  		{baseAddr, baseAddrEncoded, pver, 1, io.ErrShortWrite, io.EOF},
   273  		// Force error with greater than max inventory vectors.
   274  		{maxAddr, maxAddrEncoded, pver, 3, wireErr, wireErr},
   275  		// Force error with greater than max inventory vectors for
   276  		// protocol versions before multiple addresses were allowed.
   277  		{maxAddr, maxAddrEncoded, pverMA - 1, 3, wireErr, wireErr},
   278  	}
   279  
   280  	t.Logf("Running %d tests", len(tests))
   281  	for i, test := range tests {
   282  		// Encode to wire format.
   283  		w := newFixedWriter(test.max)
   284  		err := test.in.BtcEncode(w, test.pver)
   285  		if reflect.TypeOf(err) != reflect.TypeOf(test.writeErr) {
   286  			t.Errorf("BtcEncode #%d wrong error got: %v, want: %v",
   287  				i, err, test.writeErr)
   288  			continue
   289  		}
   290  
   291  		// For errors which are not of type wire.MessageError, check
   292  		// them for equality.
   293  		if _, ok := err.(*wire.MessageError); !ok {
   294  			if err != test.writeErr {
   295  				t.Errorf("BtcEncode #%d wrong error got: %v, "+
   296  					"want: %v", i, err, test.writeErr)
   297  				continue
   298  			}
   299  		}
   300  
   301  		// Decode from wire format.
   302  		var msg wire.MsgAddr
   303  		r := newFixedReader(test.max, test.buf)
   304  		err = msg.BtcDecode(r, test.pver)
   305  		if reflect.TypeOf(err) != reflect.TypeOf(test.readErr) {
   306  			t.Errorf("BtcDecode #%d wrong error got: %v, want: %v",
   307  				i, err, test.readErr)
   308  			continue
   309  		}
   310  
   311  		// For errors which are not of type wire.MessageError, check
   312  		// them for equality.
   313  		if _, ok := err.(*wire.MessageError); !ok {
   314  			if err != test.readErr {
   315  				t.Errorf("BtcDecode #%d wrong error got: %v, "+
   316  					"want: %v", i, err, test.readErr)
   317  				continue
   318  			}
   319  		}
   320  
   321  	}
   322  }