github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/p2p/netaddress_test.go (about)

     1  package p2p
     2  
     3  import (
     4  	"net"
     5  	"sync"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  func TestNetAddress_String(t *testing.T) {
    13  	tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
    14  	require.Nil(t, err)
    15  
    16  	netAddr := NewNetAddress("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", tcpAddr)
    17  
    18  	var wg sync.WaitGroup
    19  
    20  	for i := 0; i < 10; i++ {
    21  		wg.Add(1)
    22  		go func() {
    23  			defer wg.Done()
    24  			_ = netAddr.String()
    25  		}()
    26  	}
    27  
    28  	wg.Wait()
    29  
    30  	s := netAddr.String()
    31  	require.Equal(t, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", s)
    32  }
    33  
    34  func TestNewNetAddress(t *testing.T) {
    35  	tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:8080")
    36  	require.Nil(t, err)
    37  
    38  	assert.Panics(t, func() {
    39  		NewNetAddress("", tcpAddr)
    40  	})
    41  
    42  	addr := NewNetAddress("deadbeefdeadbeefdeadbeefdeadbeefdeadbeef", tcpAddr)
    43  	assert.Equal(t, "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", addr.String())
    44  
    45  	assert.NotPanics(t, func() {
    46  		NewNetAddress("", &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 8000})
    47  	}, "Calling NewNetAddress with UDPAddr should not panic in testing")
    48  }
    49  
    50  func TestNewNetAddressString(t *testing.T) {
    51  	testCases := []struct {
    52  		name     string
    53  		addr     string
    54  		expected string
    55  		correct  bool
    56  	}{
    57  		{"no node id and no protocol", "127.0.0.1:8080", "", false},
    58  		{"no node id w/ tcp input", "tcp://127.0.0.1:8080", "", false},
    59  		{"no node id w/ udp input", "udp://127.0.0.1:8080", "", false},
    60  
    61  		{
    62  			"no protocol",
    63  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    64  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    65  			true,
    66  		},
    67  		{
    68  			"tcp input",
    69  			"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    70  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    71  			true,
    72  		},
    73  		{
    74  			"udp input",
    75  			"udp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    76  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    77  			true,
    78  		},
    79  		{"malformed tcp input", "tcp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
    80  		{"malformed udp input", "udp//deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
    81  
    82  		// {"127.0.0:8080", false},
    83  		{"invalid host", "notahost", "", false},
    84  		{"invalid port", "127.0.0.1:notapath", "", false},
    85  		{"invalid host w/ port", "notahost:8080", "", false},
    86  		{"just a port", "8082", "", false},
    87  		{"non-existent port", "127.0.0:8080000", "", false},
    88  
    89  		{"too short nodeId", "deadbeef@127.0.0.1:8080", "", false},
    90  		{"too short, not hex nodeId", "this-isnot-hex@127.0.0.1:8080", "", false},
    91  		{"not hex nodeId", "xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
    92  
    93  		{"too short nodeId w/tcp", "tcp://deadbeef@127.0.0.1:8080", "", false},
    94  		{"too short notHex nodeId w/tcp", "tcp://this-isnot-hex@127.0.0.1:8080", "", false},
    95  		{"notHex nodeId w/tcp", "tcp://xxxxbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", "", false},
    96  		{
    97  			"correct nodeId w/tcp",
    98  			"tcp://deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
    99  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
   100  			true,
   101  		},
   102  
   103  		{"no node id", "tcp://@127.0.0.1:8080", "", false},
   104  		{"no node id or IP", "tcp://@", "", false},
   105  		{"tcp no host, w/ port", "tcp://:26656", "", false},
   106  		{"empty", "", "", false},
   107  		{"node id delimiter 1", "@", "", false},
   108  		{"node id delimiter 2", " @", "", false},
   109  		{"node id delimiter 3", " @ ", "", false},
   110  	}
   111  
   112  	for _, tc := range testCases {
   113  		tc := tc
   114  		t.Run(tc.name, func(t *testing.T) {
   115  			addr, err := NewNetAddressString(tc.addr)
   116  			if tc.correct {
   117  				if assert.Nil(t, err, tc.addr) {
   118  					assert.Equal(t, tc.expected, addr.String())
   119  				}
   120  			} else {
   121  				assert.NotNil(t, err, tc.addr)
   122  			}
   123  		})
   124  	}
   125  }
   126  
   127  func TestNewNetAddressStrings(t *testing.T) {
   128  	addrs, errs := NewNetAddressStrings([]string{
   129  		"127.0.0.1:8080",
   130  		"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
   131  		"deadbeefdeadbeefdeadbeefdeadbeefdeadbeed@127.0.0.2:8080"})
   132  	assert.Len(t, errs, 1)
   133  	assert.Equal(t, 2, len(addrs))
   134  }
   135  
   136  func TestNewNetAddressIPPort(t *testing.T) {
   137  	addr := NewNetAddressIPPort(net.ParseIP("127.0.0.1"), 8080)
   138  	assert.Equal(t, "127.0.0.1:8080", addr.String())
   139  }
   140  
   141  func TestNetAddressProperties(t *testing.T) {
   142  	// TODO add more test cases
   143  	testCases := []struct {
   144  		addr     string
   145  		valid    bool
   146  		local    bool
   147  		routable bool
   148  	}{
   149  		{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", true, true, false},
   150  		{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", true, false, true},
   151  	}
   152  
   153  	for _, tc := range testCases {
   154  		addr, err := NewNetAddressString(tc.addr)
   155  		require.Nil(t, err)
   156  
   157  		err = addr.Valid()
   158  		if tc.valid {
   159  			assert.NoError(t, err)
   160  		} else {
   161  			assert.Error(t, err)
   162  		}
   163  		assert.Equal(t, tc.local, addr.Local())
   164  		assert.Equal(t, tc.routable, addr.Routable())
   165  	}
   166  }
   167  
   168  func TestNetAddressReachabilityTo(t *testing.T) {
   169  	// TODO add more test cases
   170  	testCases := []struct {
   171  		addr         string
   172  		other        string
   173  		reachability int
   174  	}{
   175  		{
   176  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080",
   177  			"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8081",
   178  			0,
   179  		},
   180  		{"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@ya.ru:80", "deadbeefdeadbeefdeadbeefdeadbeefdeadbeef@127.0.0.1:8080", 1},
   181  	}
   182  
   183  	for _, tc := range testCases {
   184  		addr, err := NewNetAddressString(tc.addr)
   185  		require.Nil(t, err)
   186  
   187  		other, err := NewNetAddressString(tc.other)
   188  		require.Nil(t, err)
   189  
   190  		assert.Equal(t, tc.reachability, addr.ReachabilityTo(other))
   191  	}
   192  }