go-waku/waku/v2/utils/enr_test.go

67 lines
2.3 KiB
Go
Raw Normal View History

package utils
import (
"fmt"
"net"
"testing"
gcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode"
2022-10-19 19:39:32 +00:00
"github.com/libp2p/go-libp2p/core/peer"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"
)
func TestEnodeToMultiAddr(t *testing.T) {
enr := "enr:-IS4QAmC_o1PMi5DbR4Bh4oHVyQunZblg4bTaottPtBodAhJZvxVlWW-4rXITPNg4mwJ8cW__D9FBDc9N4mdhyMqB-EBgmlkgnY0gmlwhIbRi9KJc2VjcDI1NmsxoQOevTdO6jvv3fRruxguKR-3Ge4bcFsLeAIWEDjrfaigNoN0Y3CCdl8"
parsedNode := enode.MustParse(enr)
expectedMultiAddr := "/ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ"
actualMultiAddr, err := enodeToMultiAddr(parsedNode)
require.NoError(t, err)
require.Equal(t, expectedMultiAddr, actualMultiAddr.String())
}
func TestGetENRandIP(t *testing.T) {
key, _ := gcrypto.GenerateKey()
2022-05-27 19:55:35 +00:00
pubKey := EcdsaPubKeyToSecp256k1PublicKey(&key.PublicKey)
id, _ := peer.IDFromPublicKey(pubKey)
hostAddr := &net.TCPAddr{IP: net.ParseIP("192.168.0.1"), Port: 9999}
hostMultiAddr, _ := manet.FromNetAddr(hostAddr)
hostInfo, _ := ma.NewMultiaddr(fmt.Sprintf("/p2p/%s", id.Pretty()))
ogMultiaddress := hostMultiAddr.Encapsulate(hostInfo)
wakuFlag := NewWakuEnrBitfield(true, true, true, true)
node, err := GetENRandIP([]ma.Multiaddr{ogMultiaddress}, wakuFlag, key)
require.NoError(t, err)
parsedNode := enode.MustParse(node.String())
resMultiaddress, err := enodeToMultiAddr(parsedNode)
require.NoError(t, err)
require.Equal(t, ogMultiaddress.String(), resMultiaddress.String())
}
2021-12-10 14:26:38 +00:00
func TestMultiaddr(t *testing.T) {
key, _ := gcrypto.GenerateKey()
2022-05-27 19:55:35 +00:00
pubKey := EcdsaPubKeyToSecp256k1PublicKey(&key.PublicKey)
id, _ := peer.IDFromPublicKey(pubKey)
2021-12-10 14:26:38 +00:00
wakuFlag := NewWakuEnrBitfield(true, true, true, true)
normalMultiaddr, _ := ma.NewMultiaddr("/ip4/192.1.168.241/tcp/60000/p2p/" + id.Pretty())
wsMultiaddress, _ := ma.NewMultiaddr("/ip4/10.0.0.241/tcp/60001/ws/p2p/" + id.Pretty())
node, err := GetENRandIP([]ma.Multiaddr{normalMultiaddr, wsMultiaddress}, wakuFlag, key)
2021-12-10 14:26:38 +00:00
require.NoError(t, err)
multiaddresses, err := Multiaddress(node)
fmt.Println(multiaddresses)
2021-12-10 14:26:38 +00:00
require.NoError(t, err)
require.Len(t, multiaddresses, 2)
require.True(t, normalMultiaddr.Equal(multiaddresses[0]))
require.True(t, wsMultiaddress.Equal(multiaddresses[1]))
2021-12-10 14:26:38 +00:00
}