github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/p2p/node_info_test.go (about) 1 package p2p 2 3 import ( 4 "fmt" 5 "net" 6 "testing" 7 8 "github.com/gnolang/gno/tm2/pkg/crypto/ed25519" 9 "github.com/gnolang/gno/tm2/pkg/versionset" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestNodeInfoValidate(t *testing.T) { 14 t.Parallel() 15 16 // empty fails 17 ni := NodeInfo{} 18 assert.Error(t, ni.Validate()) 19 20 channels := make([]byte, maxNumChannels) 21 for i := 0; i < maxNumChannels; i++ { 22 channels[i] = byte(i) 23 } 24 dupChannels := make([]byte, 5) 25 copy(dupChannels, channels[:5]) 26 dupChannels = append(dupChannels, testCh) 27 28 nonAscii := "¢§µ" 29 emptyTab := fmt.Sprintf("\t") 30 emptySpace := fmt.Sprintf(" ") 31 32 testCases := []struct { 33 testName string 34 malleateNodeInfo func(*NodeInfo) 35 expectErr bool 36 }{ 37 {"Too Many Channels", func(ni *NodeInfo) { ni.Channels = append(channels, byte(maxNumChannels)) }, true}, //nolint: gocritic 38 {"Duplicate Channel", func(ni *NodeInfo) { ni.Channels = dupChannels }, true}, 39 {"Good Channels", func(ni *NodeInfo) { ni.Channels = ni.Channels[:5] }, false}, 40 41 {"Nil NetAddress", func(ni *NodeInfo) { ni.NetAddress = nil }, true}, 42 {"Zero NetAddress ID", func(ni *NodeInfo) { ni.NetAddress.ID = "" }, true}, 43 {"Invalid NetAddress IP", func(ni *NodeInfo) { ni.NetAddress.IP = net.IP([]byte{0x00}) }, true}, 44 45 {"Non-ASCII Version", func(ni *NodeInfo) { ni.Version = nonAscii }, true}, 46 {"Empty tab Version", func(ni *NodeInfo) { ni.Version = emptyTab }, true}, 47 {"Empty space Version", func(ni *NodeInfo) { ni.Version = emptySpace }, true}, 48 {"Empty Version", func(ni *NodeInfo) { ni.Version = "" }, false}, 49 50 {"Non-ASCII Moniker", func(ni *NodeInfo) { ni.Moniker = nonAscii }, true}, 51 {"Empty tab Moniker", func(ni *NodeInfo) { ni.Moniker = emptyTab }, true}, 52 {"Empty space Moniker", func(ni *NodeInfo) { ni.Moniker = emptySpace }, true}, 53 {"Empty Moniker", func(ni *NodeInfo) { ni.Moniker = "" }, true}, 54 {"Good Moniker", func(ni *NodeInfo) { ni.Moniker = "hey its me" }, false}, 55 56 {"Non-ASCII TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = nonAscii }, true}, 57 {"Empty tab TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = emptyTab }, true}, 58 {"Empty space TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = emptySpace }, true}, 59 {"Empty TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = "" }, false}, 60 {"Off TxIndex", func(ni *NodeInfo) { ni.Other.TxIndex = "off" }, false}, 61 62 {"Non-ASCII RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = nonAscii }, true}, 63 {"Empty tab RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = emptyTab }, true}, 64 {"Empty space RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = emptySpace }, true}, 65 {"Empty RPCAddress", func(ni *NodeInfo) { ni.Other.RPCAddress = "" }, false}, 66 {"Good RPCAddress", func(ni *NodeInfo) { 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) 74 ni.Channels = channels 75 assert.NoError(t, ni.Validate()) 76 77 for _, tc := range testCases { 78 ni := testNodeInfo(nodeKey.ID(), name) 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 func TestNodeInfoCompatible(t *testing.T) { 91 t.Parallel() 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) 101 ni2 := testNodeInfo(nodeKey2.ID(), name) 102 assert.NoError(t, ni1.CompatibleWith(ni2)) 103 104 // add another channel; still compatible 105 ni2.Channels = []byte{newTestChannel, testCh} 106 assert.NoError(t, ni1.CompatibleWith(ni2)) 107 108 // wrong NodeInfo type is not compatible 109 _, netAddr := CreateRoutableAddr() 110 ni3 := NodeInfo{NetAddress: netAddr} 111 assert.Error(t, ni1.CompatibleWith(ni3)) 112 113 testCases := []struct { 114 testName string 115 malleateNodeInfo func(*NodeInfo) 116 }{ 117 {"Bad block version", func(ni *NodeInfo) { 118 ni.VersionSet.Set(versionset.VersionInfo{Name: "Block", Version: "badversion"}) 119 }}, 120 {"Wrong block version", func(ni *NodeInfo) { 121 ni.VersionSet.Set(versionset.VersionInfo{Name: "Block", Version: "v999.999.999-wrong"}) 122 }}, 123 {"Wrong network", func(ni *NodeInfo) { ni.Network += "-wrong" }}, 124 {"No common channels", func(ni *NodeInfo) { ni.Channels = []byte{newTestChannel} }}, 125 } 126 127 for i, tc := range testCases { 128 t.Logf("case #%v", i) 129 ni := testNodeInfo(nodeKey2.ID(), name) 130 tc.malleateNodeInfo(&ni) 131 fmt.Printf("case #%v\n", i) 132 assert.Error(t, ni1.CompatibleWith(ni)) 133 } 134 }