2020-06-10 23:41:11 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-11 08:31:43 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2022-08-16 12:20:13 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v3/config/params"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
2020-06-10 23:41:11 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Test `verifyConnectivity` function by trying to connect to google.com (successfully)
|
|
|
|
// and then by connecting to an unreachable IP and ensuring that a log is emitted
|
|
|
|
func TestVerifyConnectivity(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2020-06-10 23:41:11 +00:00
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
cases := []struct {
|
|
|
|
address string
|
|
|
|
port uint
|
|
|
|
expectedConnectivity bool
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{"142.250.68.46", 80, true, "Dialing a reachable IP: 142.250.68.46:80"}, // google.com
|
|
|
|
{"123.123.123.123", 19000, false, "Dialing an unreachable IP: 123.123.123.123:19000"},
|
|
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(fmt.Sprintf(tc.name),
|
|
|
|
func(t *testing.T) {
|
|
|
|
verifyConnectivity(tc.address, tc.port, "tcp")
|
|
|
|
logMessage := "IP address is not accessible"
|
|
|
|
if tc.expectedConnectivity {
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsDoNotContain(t, hook, logMessage)
|
2020-06-10 23:41:11 +00:00
|
|
|
} else {
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, logMessage)
|
2020-06-10 23:41:11 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 08:31:43 +00:00
|
|
|
|
|
|
|
func TestSerializeENR(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2021-05-11 08:31:43 +00:00
|
|
|
t.Run("Ok", func(t *testing.T) {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
db, err := enode.OpenDB("")
|
|
|
|
require.NoError(t, err)
|
|
|
|
lNode := enode.NewLocalNode(db, key)
|
|
|
|
record := lNode.Node().Record()
|
|
|
|
s, err := SerializeENR(record)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.NotEqual(t, "", s)
|
2022-11-10 12:03:55 +00:00
|
|
|
s = "enr:" + s
|
|
|
|
newRec, err := enode.Parse(enode.ValidSchemes, s)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, s, newRec.String())
|
2021-05-11 08:31:43 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Nil record", func(t *testing.T) {
|
|
|
|
_, err := SerializeENR(nil)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
assert.ErrorContains(t, "could not serialize nil record", err)
|
|
|
|
})
|
|
|
|
}
|