github.com/pure-x-eth/consensus_tm@v0.0.0-20230502163723-e3c2ff987250/p2p/node_info_test.go (about) 1 package p2p 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 8 "github.com/pure-x-eth/consensus_tm/crypto/ed25519" 9 ) 10 11 func TestNodeInfoValidate(t *testing.T) { 12 13 // empty fails 14 ni := DefaultNodeInfo{} 15 assert.Error(t, ni.Validate()) 16 17 channels := make([]byte, maxNumChannels) 18 for i := 0; i < maxNumChannels; i++ { 19 channels[i] = byte(i) 20 } 21 dupChannels := make([]byte, 5) 22 copy(dupChannels, channels[:5]) 23 dupChannels = append(dupChannels, testCh) 24 25 nonASCII := "¢§µ" 26 emptyTab := "\t" 27 emptySpace := " " 28 29 testCases := []struct { 30 testName string 31 malleateNodeInfo func(*DefaultNodeInfo) 32 expectErr bool 33 }{ 34 { 35 "Too Many Channels", 36 func(ni *DefaultNodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, //nolint: gocritic 37 true, 38 }, 39 {"Duplicate Channel", func(ni *DefaultNodeInfo) { ni.Channels = dupChannels }, true}, 40 {"Good Channels", func(ni *DefaultNodeInfo) { ni.Channels = ni.Channels[:5] }, false}, 41 42 {"Invalid NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "not-an-address" }, true}, 43 {"Good NetAddress", func(ni *DefaultNodeInfo) { ni.ListenAddr = "0.0.0.0:26656" }, false}, 44 45 {"Non-ASCII Version", func(ni *DefaultNodeInfo) { ni.Version = nonASCII }, true}, 46 {"Empty tab Version", func(ni *DefaultNodeInfo) { ni.Version = emptyTab }, true}, 47 {"Empty space Version", func(ni *DefaultNodeInfo) { ni.Version = emptySpace }, true}, 48 {"Empty Version", func(ni *DefaultNodeInfo) { ni.Version = "" }, false}, 49 50 {"Non-ASCII Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = nonASCII }, true}, 51 {"Empty tab Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptyTab }, true}, 52 {"Empty space Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = emptySpace }, true}, 53 {"Empty Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "" }, true}, 54 {"Good Moniker", func(ni *DefaultNodeInfo) { ni.Moniker = "hey its me" }, false}, 55 56 {"Non-ASCII TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = nonASCII }, true}, 57 {"Empty tab TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptyTab }, true}, 58 {"Empty space TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = emptySpace }, true}, 59 {"Empty TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "" }, false}, 60 {"Off TxIndex", func(ni *DefaultNodeInfo) { ni.Other.TxIndex = "off" }, false}, 61 62 {"Non-ASCII RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = nonASCII }, true}, 63 {"Empty tab RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptyTab }, true}, 64 {"Empty space RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = emptySpace }, true}, 65 {"Empty RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "" }, false}, 66 {"Good RPCAddress", func(ni *DefaultNodeInfo) { ni.Other.RPCAddress = "0.0.0.0:26657" }, false}, 67 } 68 69 nodeKey := NodeKey{PrivKey: ed25519.GenPrivKey()} 70 name := "testing" 71 72 // test case passes 73 ni = testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo) 74 ni.Channels = channels 75 assert.NoError(t, ni.Validate()) 76 77 for _, tc := range testCases { 78 ni := testNodeInfo(nodeKey.ID(), name).(DefaultNodeInfo) 79 ni.Channels = channels 80 tc.malleateNodeInfo(&ni) 81 err := ni.Validate() 82 if tc.expectErr { 83 assert.Error(t, err, tc.testName) 84 } else { 85 assert.NoError(t, err, tc.testName) 86 } 87 } 88 89 } 90 91 func TestNodeInfoCompatible(t *testing.T) { 92 93 nodeKey1 := NodeKey{PrivKey: ed25519.GenPrivKey()} 94 nodeKey2 := NodeKey{PrivKey: ed25519.GenPrivKey()} 95 name := "testing" 96 97 var newTestChannel byte = 0x2 98 99 // test NodeInfo is compatible 100 ni1 := testNodeInfo(nodeKey1.ID(), name).(DefaultNodeInfo) 101 ni2 := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo) 102 assert.NoError(t, ni1.CompatibleWith(ni2)) 103 104 // add another channel; still compatible 105 ni2.Channels = append(ni2.Channels, newTestChannel) 106 assert.True(t, ni2.HasChannel(newTestChannel)) 107 assert.NoError(t, ni1.CompatibleWith(ni2)) 108 109 // wrong NodeInfo type is not compatible 110 _, netAddr := CreateRoutableAddr() 111 ni3 := mockNodeInfo{netAddr} 112 assert.Error(t, ni1.CompatibleWith(ni3)) 113 114 testCases := []struct { 115 testName string 116 malleateNodeInfo func(*DefaultNodeInfo) 117 }{ 118 {"Wrong block version", func(ni *DefaultNodeInfo) { ni.ProtocolVersion.Block++ }}, 119 {"Wrong network", func(ni *DefaultNodeInfo) { ni.Network += "-wrong" }}, 120 {"No common channels", func(ni *DefaultNodeInfo) { ni.Channels = []byte{newTestChannel} }}, 121 } 122 123 for _, tc := range testCases { 124 ni := testNodeInfo(nodeKey2.ID(), name).(DefaultNodeInfo) 125 tc.malleateNodeInfo(&ni) 126 assert.Error(t, ni1.CompatibleWith(ni)) 127 } 128 }