mirror of https://github.com/status-im/go-waku.git
94 lines
2.1 KiB
Go
94 lines
2.1 KiB
Go
package tests
|
|
|
|
import (
|
|
"context"
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"testing"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
"github.com/libp2p/go-libp2p-core/host"
|
|
"github.com/multiformats/go-multiaddr"
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var log *zap.SugaredLogger = nil
|
|
|
|
func Logger() *zap.SugaredLogger {
|
|
if log == nil {
|
|
l, _ := zap.NewDevelopment()
|
|
log = l.Sugar()
|
|
}
|
|
return log
|
|
}
|
|
|
|
func GetHostAddress(ha host.Host) ma.Multiaddr {
|
|
return ha.Addrs()[0]
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
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.
|
|
sourceMultiAddr, _ := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port))
|
|
|
|
// libp2p.New constructs a new libp2p Host.
|
|
// Other options can be added here.
|
|
return libp2p.New(
|
|
libp2p.ListenAddrs(sourceMultiAddr),
|
|
libp2p.Identity(prvKey),
|
|
)
|
|
}
|
|
|
|
func CreateWakuMessage(contentTopic string, timestamp int64) *pb.WakuMessage {
|
|
return &pb.WakuMessage{Payload: []byte{1, 2, 3}, ContentTopic: contentTopic, Version: 0, Timestamp: timestamp}
|
|
}
|
|
|
|
func RandomHex(n int) (string, error) {
|
|
bytes := make([]byte, n)
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(bytes), nil
|
|
}
|