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