2021-10-28 12:41:17 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-12 09:19:42 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
2021-10-28 12:41:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2023-01-13 23:58:22 +00:00
|
|
|
"sync"
|
2021-10-28 12:41:17 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2021-10-28 12:41:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
2023-01-13 23:58:22 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2023-06-05 14:39:38 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
|
2021-10-28 12:41:17 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2023-06-05 14:39:38 +00:00
|
|
|
v2 "github.com/waku-org/go-waku/waku/v2"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/peers"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2021-10-28 12:41:17 +00:00
|
|
|
)
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// GetHostAddress returns the first listen address used by a host
|
2022-10-19 19:39:32 +00:00
|
|
|
func GetHostAddress(ha host.Host) multiaddr.Multiaddr {
|
2021-10-28 12:41:17 +00:00
|
|
|
return ha.Addrs()[0]
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// FindFreePort returns an available port number
|
2021-10-28 12:41:17 +00:00
|
|
|
func FindFreePort(t *testing.T, host string, maxAttempts int) (int, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if host == "" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < maxAttempts; i++ {
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(host, "0"))
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("unable to resolve tcp addr: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l, err := net.ListenTCP("tcp", addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Close()
|
|
|
|
t.Logf("unable to listen on addr %q: %v", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
port := l.Addr().(*net.TCPAddr).Port
|
|
|
|
l.Close()
|
|
|
|
return port, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("no free port found")
|
|
|
|
}
|
|
|
|
|
2023-01-12 02:20:23 +00:00
|
|
|
// FindFreePort returns an available port number
|
|
|
|
func FindFreeUDPPort(t *testing.T, host string, maxAttempts int) (int, error) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
if host == "" {
|
|
|
|
host = "localhost"
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < maxAttempts; i++ {
|
|
|
|
addr, err := net.ResolveUDPAddr("udp", net.JoinHostPort(host, "0"))
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("unable to resolve tcp addr: %v", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
l, err := net.ListenUDP("udp", addr)
|
|
|
|
if err != nil {
|
|
|
|
l.Close()
|
|
|
|
t.Logf("unable to listen on addr %q: %v", addr, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
port := l.LocalAddr().(*net.UDPAddr).Port
|
|
|
|
l.Close()
|
|
|
|
return port, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fmt.Errorf("no free port found")
|
|
|
|
}
|
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// MakeHost creates a Libp2p host with a random key on a specific port
|
2021-10-28 12:41:17 +00:00
|
|
|
func MakeHost(ctx context.Context, port int, randomness io.Reader) (host.Host, error) {
|
|
|
|
// Creates a new RSA key pair for this host.
|
|
|
|
prvKey, _, err := crypto.GenerateKeyPairWithReader(crypto.RSA, 2048, randomness)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 0.0.0.0 will listen on any interface device.
|
2023-06-05 14:39:38 +00:00
|
|
|
sourceMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
psWrapper := peers.NewWakuPeerstore(ps)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-10-28 12:41:17 +00:00
|
|
|
|
|
|
|
// libp2p.New constructs a new libp2p Host.
|
|
|
|
// Other options can be added here.
|
|
|
|
return libp2p.New(
|
2023-06-05 14:39:38 +00:00
|
|
|
libp2p.Peerstore(psWrapper),
|
2021-10-28 12:41:17 +00:00
|
|
|
libp2p.ListenAddrs(sourceMultiAddr),
|
|
|
|
libp2p.Identity(prvKey),
|
|
|
|
)
|
|
|
|
}
|
2021-10-28 13:03:23 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// CreateWakuMessage creates a WakuMessage protobuffer with default values and a custom contenttopic and timestamp
|
2022-02-23 15:01:53 +00:00
|
|
|
func CreateWakuMessage(contentTopic string, timestamp int64) *pb.WakuMessage {
|
2021-10-28 13:03:23 +00:00
|
|
|
return &pb.WakuMessage{Payload: []byte{1, 2, 3}, ContentTopic: contentTopic, Version: 0, Timestamp: timestamp}
|
|
|
|
}
|
2021-11-12 09:19:42 +00:00
|
|
|
|
2022-04-25 19:31:26 +00:00
|
|
|
// RandomHex returns a random hex string of n bytes
|
2021-11-12 09:19:42 +00:00
|
|
|
func RandomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
2023-01-13 23:58:22 +00:00
|
|
|
|
|
|
|
type TestPeerDiscoverer struct {
|
|
|
|
sync.RWMutex
|
|
|
|
peerMap map[peer.ID]struct{}
|
2023-06-05 14:39:38 +00:00
|
|
|
peerCh chan v2.PeerData
|
2023-01-13 23:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewTestPeerDiscoverer() *TestPeerDiscoverer {
|
|
|
|
result := &TestPeerDiscoverer{
|
|
|
|
peerMap: make(map[peer.ID]struct{}),
|
2023-06-05 14:39:38 +00:00
|
|
|
peerCh: make(chan v2.PeerData, 10),
|
2023-01-13 23:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for p := range result.peerCh {
|
|
|
|
result.Lock()
|
2023-06-05 14:39:38 +00:00
|
|
|
result.peerMap[p.AddrInfo.ID] = struct{}{}
|
2023-01-13 23:58:22 +00:00
|
|
|
result.Unlock()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2023-06-05 14:39:38 +00:00
|
|
|
func (t *TestPeerDiscoverer) PeerChannel() chan<- v2.PeerData {
|
2023-01-13 23:58:22 +00:00
|
|
|
return t.peerCh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestPeerDiscoverer) HasPeer(p peer.ID) bool {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
_, ok := t.peerMap[p]
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestPeerDiscoverer) PeerCount() int {
|
|
|
|
t.RLock()
|
|
|
|
defer t.RUnlock()
|
|
|
|
return len(t.peerMap)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestPeerDiscoverer) Clear() {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
t.peerMap = make(map[peer.ID]struct{})
|
|
|
|
}
|