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