prysm-pulse/beacon-chain/p2p/utils_test.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

63 lines
1.8 KiB
Go

package p2p
import (
"fmt"
"testing"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/prysmaticlabs/prysm/v3/config/params"
"github.com/prysmaticlabs/prysm/v3/testing/assert"
"github.com/prysmaticlabs/prysm/v3/testing/require"
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) {
params.SetupTestConfigCleanup(t)
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 {
require.LogsDoNotContain(t, hook, logMessage)
} else {
require.LogsContain(t, hook, logMessage)
}
})
}
}
func TestSerializeENR(t *testing.T) {
params.SetupTestConfigCleanup(t)
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)
})
t.Run("Nil record", func(t *testing.T) {
_, err := SerializeENR(nil)
require.NotNil(t, err)
assert.ErrorContains(t, "could not serialize nil record", err)
})
}