github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/wire/netaddress_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/dashpay/godash/wire"
    17  	"github.com/davecgh/go-spew/spew"
    18  )
    19  
    20  // TestNetAddress tests the NetAddress API.
    21  func TestNetAddress(t *testing.T) {
    22  	ip := net.ParseIP("127.0.0.1")
    23  	port := 8333
    24  
    25  	// Test NewNetAddress.
    26  	tcpAddr := &net.TCPAddr{
    27  		IP:   ip,
    28  		Port: port,
    29  	}
    30  	na, err := wire.NewNetAddress(tcpAddr, 0)
    31  	if err != nil {
    32  		t.Errorf("NewNetAddress: %v", err)
    33  	}
    34  
    35  	// Ensure we get the same ip, port, and services back out.
    36  	if !na.IP.Equal(ip) {
    37  		t.Errorf("NetNetAddress: wrong ip - got %v, want %v", na.IP, ip)
    38  	}
    39  	if na.Port != uint16(port) {
    40  		t.Errorf("NetNetAddress: wrong port - got %v, want %v", na.Port,
    41  			port)
    42  	}
    43  	if na.Services != 0 {
    44  		t.Errorf("NetNetAddress: wrong services - got %v, want %v",
    45  			na.Services, 0)
    46  	}
    47  	if na.HasService(wire.SFNodeNetwork) {
    48  		t.Errorf("HasService: SFNodeNetwork service is set")
    49  	}
    50  
    51  	// Ensure adding the full service node flag works.
    52  	na.AddService(wire.SFNodeNetwork)
    53  	if na.Services != wire.SFNodeNetwork {
    54  		t.Errorf("AddService: wrong services - got %v, want %v",
    55  			na.Services, wire.SFNodeNetwork)
    56  	}
    57  	if !na.HasService(wire.SFNodeNetwork) {
    58  		t.Errorf("HasService: SFNodeNetwork service not set")
    59  	}
    60  
    61  	// Ensure max payload is expected value for latest protocol version.
    62  	pver := wire.ProtocolVersion
    63  	wantPayload := uint32(30)
    64  	maxPayload := wire.TstMaxNetAddressPayload(wire.ProtocolVersion)
    65  	if maxPayload != wantPayload {
    66  		t.Errorf("maxNetAddressPayload: wrong max payload length for "+
    67  			"protocol version %d - got %v, want %v", pver,
    68  			maxPayload, wantPayload)
    69  	}
    70  
    71  	// Protocol version before NetAddressTimeVersion when timestamp was
    72  	// added.  Ensure max payload is expected value for it.
    73  	pver = wire.NetAddressTimeVersion - 1
    74  	wantPayload = 26
    75  	maxPayload = wire.TstMaxNetAddressPayload(pver)
    76  	if maxPayload != wantPayload {
    77  		t.Errorf("maxNetAddressPayload: wrong max payload length for "+
    78  			"protocol version %d - got %v, want %v", pver,
    79  			maxPayload, wantPayload)
    80  	}
    81  
    82  	// Check for expected failure on wrong address type.
    83  	udpAddr := &net.UDPAddr{}
    84  	_, err = wire.NewNetAddress(udpAddr, 0)
    85  	if err != wire.ErrInvalidNetAddr {
    86  		t.Errorf("NewNetAddress: expected error not received - "+
    87  			"got %v, want %v", err, wire.ErrInvalidNetAddr)
    88  	}
    89  }
    90  
    91  // TestNetAddressWire tests the NetAddress wire encode and decode for various
    92  // protocol versions and timestamp flag combinations.
    93  func TestNetAddressWire(t *testing.T) {
    94  	// baseNetAddr is used in the various tests as a baseline NetAddress.
    95  	baseNetAddr := wire.NetAddress{
    96  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
    97  		Services:  wire.SFNodeNetwork,
    98  		IP:        net.ParseIP("127.0.0.1"),
    99  		Port:      8333,
   100  	}
   101  
   102  	// baseNetAddrNoTS is baseNetAddr with a zero value for the timestamp.
   103  	baseNetAddrNoTS := baseNetAddr
   104  	baseNetAddrNoTS.Timestamp = time.Time{}
   105  
   106  	// baseNetAddrEncoded is the wire encoded bytes of baseNetAddr.
   107  	baseNetAddrEncoded := []byte{
   108  		0x29, 0xab, 0x5f, 0x49, // Timestamp
   109  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   110  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   111  		0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
   112  		0x20, 0x8d, // Port 8333 in big-endian
   113  	}
   114  
   115  	// baseNetAddrNoTSEncoded is the wire encoded bytes of baseNetAddrNoTS.
   116  	baseNetAddrNoTSEncoded := []byte{
   117  		// No timestamp
   118  		0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // SFNodeNetwork
   119  		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
   120  		0x00, 0x00, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x01, // IP 127.0.0.1
   121  		0x20, 0x8d, // Port 8333 in big-endian
   122  	}
   123  
   124  	tests := []struct {
   125  		in   wire.NetAddress // NetAddress to encode
   126  		out  wire.NetAddress // Expected decoded NetAddress
   127  		ts   bool            // Include timestamp?
   128  		buf  []byte          // Wire encoding
   129  		pver uint32          // Protocol version for wire encoding
   130  	}{
   131  		// Latest protocol version without ts flag.
   132  		{
   133  			baseNetAddr,
   134  			baseNetAddrNoTS,
   135  			false,
   136  			baseNetAddrNoTSEncoded,
   137  			wire.ProtocolVersion,
   138  		},
   139  
   140  		// Latest protocol version with ts flag.
   141  		{
   142  			baseNetAddr,
   143  			baseNetAddr,
   144  			true,
   145  			baseNetAddrEncoded,
   146  			wire.ProtocolVersion,
   147  		},
   148  
   149  		// Protocol version NetAddressTimeVersion without ts flag.
   150  		{
   151  			baseNetAddr,
   152  			baseNetAddrNoTS,
   153  			false,
   154  			baseNetAddrNoTSEncoded,
   155  			wire.NetAddressTimeVersion,
   156  		},
   157  
   158  		// Protocol version NetAddressTimeVersion with ts flag.
   159  		{
   160  			baseNetAddr,
   161  			baseNetAddr,
   162  			true,
   163  			baseNetAddrEncoded,
   164  			wire.NetAddressTimeVersion,
   165  		},
   166  
   167  		// Protocol version NetAddressTimeVersion-1 without ts flag.
   168  		{
   169  			baseNetAddr,
   170  			baseNetAddrNoTS,
   171  			false,
   172  			baseNetAddrNoTSEncoded,
   173  			wire.NetAddressTimeVersion - 1,
   174  		},
   175  
   176  		// Protocol version NetAddressTimeVersion-1 with timestamp.
   177  		// Even though the timestamp flag is set, this shouldn't have a
   178  		// timestamp since it is a protocol version before it was
   179  		// added.
   180  		{
   181  			baseNetAddr,
   182  			baseNetAddrNoTS,
   183  			true,
   184  			baseNetAddrNoTSEncoded,
   185  			wire.NetAddressTimeVersion - 1,
   186  		},
   187  	}
   188  
   189  	t.Logf("Running %d tests", len(tests))
   190  	for i, test := range tests {
   191  		// Encode to wire format.
   192  		var buf bytes.Buffer
   193  		err := wire.TstWriteNetAddress(&buf, test.pver, &test.in, test.ts)
   194  		if err != nil {
   195  			t.Errorf("writeNetAddress #%d error %v", i, err)
   196  			continue
   197  		}
   198  		if !bytes.Equal(buf.Bytes(), test.buf) {
   199  			t.Errorf("writeNetAddress #%d\n got: %s want: %s", i,
   200  				spew.Sdump(buf.Bytes()), spew.Sdump(test.buf))
   201  			continue
   202  		}
   203  
   204  		// Decode the message from wire format.
   205  		var na wire.NetAddress
   206  		rbuf := bytes.NewReader(test.buf)
   207  		err = wire.TstReadNetAddress(rbuf, test.pver, &na, test.ts)
   208  		if err != nil {
   209  			t.Errorf("readNetAddress #%d error %v", i, err)
   210  			continue
   211  		}
   212  		if !reflect.DeepEqual(na, test.out) {
   213  			t.Errorf("readNetAddress #%d\n got: %s want: %s", i,
   214  				spew.Sdump(na), spew.Sdump(test.out))
   215  			continue
   216  		}
   217  	}
   218  }
   219  
   220  // TestNetAddressWireErrors performs negative tests against wire encode and
   221  // decode NetAddress to confirm error paths work correctly.
   222  func TestNetAddressWireErrors(t *testing.T) {
   223  	pver := wire.ProtocolVersion
   224  	pverNAT := wire.NetAddressTimeVersion - 1
   225  
   226  	// baseNetAddr is used in the various tests as a baseline NetAddress.
   227  	baseNetAddr := wire.NetAddress{
   228  		Timestamp: time.Unix(0x495fab29, 0), // 2009-01-03 12:15:05 -0600 CST
   229  		Services:  wire.SFNodeNetwork,
   230  		IP:        net.ParseIP("127.0.0.1"),
   231  		Port:      8333,
   232  	}
   233  
   234  	tests := []struct {
   235  		in       *wire.NetAddress // Value to encode
   236  		buf      []byte           // Wire encoding
   237  		pver     uint32           // Protocol version for wire encoding
   238  		ts       bool             // Include timestamp flag
   239  		max      int              // Max size of fixed buffer to induce errors
   240  		writeErr error            // Expected write error
   241  		readErr  error            // Expected read error
   242  	}{
   243  		// Latest protocol version with timestamp and intentional
   244  		// read/write errors.
   245  		// Force errors on timestamp.
   246  		{&baseNetAddr, []byte{}, pver, true, 0, io.ErrShortWrite, io.EOF},
   247  		// Force errors on services.
   248  		{&baseNetAddr, []byte{}, pver, true, 4, io.ErrShortWrite, io.EOF},
   249  		// Force errors on ip.
   250  		{&baseNetAddr, []byte{}, pver, true, 12, io.ErrShortWrite, io.EOF},
   251  		// Force errors on port.
   252  		{&baseNetAddr, []byte{}, pver, true, 28, io.ErrShortWrite, io.EOF},
   253  
   254  		// Latest protocol version with no timestamp and intentional
   255  		// read/write errors.
   256  		// Force errors on services.
   257  		{&baseNetAddr, []byte{}, pver, false, 0, io.ErrShortWrite, io.EOF},
   258  		// Force errors on ip.
   259  		{&baseNetAddr, []byte{}, pver, false, 8, io.ErrShortWrite, io.EOF},
   260  		// Force errors on port.
   261  		{&baseNetAddr, []byte{}, pver, false, 24, io.ErrShortWrite, io.EOF},
   262  
   263  		// Protocol version before NetAddressTimeVersion with timestamp
   264  		// flag set (should not have timestamp due to old protocol
   265  		// version) and  intentional read/write errors.
   266  		// Force errors on services.
   267  		{&baseNetAddr, []byte{}, pverNAT, true, 0, io.ErrShortWrite, io.EOF},
   268  		// Force errors on ip.
   269  		{&baseNetAddr, []byte{}, pverNAT, true, 8, io.ErrShortWrite, io.EOF},
   270  		// Force errors on port.
   271  		{&baseNetAddr, []byte{}, pverNAT, true, 24, io.ErrShortWrite, io.EOF},
   272  	}
   273  
   274  	t.Logf("Running %d tests", len(tests))
   275  	for i, test := range tests {
   276  		// Encode to wire format.
   277  		w := newFixedWriter(test.max)
   278  		err := wire.TstWriteNetAddress(w, test.pver, test.in, test.ts)
   279  		if err != test.writeErr {
   280  			t.Errorf("writeNetAddress #%d wrong error got: %v, want: %v",
   281  				i, err, test.writeErr)
   282  			continue
   283  		}
   284  
   285  		// Decode from wire format.
   286  		var na wire.NetAddress
   287  		r := newFixedReader(test.max, test.buf)
   288  		err = wire.TstReadNetAddress(r, test.pver, &na, test.ts)
   289  		if err != test.readErr {
   290  			t.Errorf("readNetAddress #%d wrong error got: %v, want: %v",
   291  				i, err, test.readErr)
   292  			continue
   293  		}
   294  	}
   295  }