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