2021-10-28 12:41:17 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
2023-11-10 05:14:03 +00:00
|
|
|
"bytes"
|
2021-10-28 12:41:17 +00:00
|
|
|
"context"
|
2023-11-07 17:13:19 +00:00
|
|
|
"crypto/ecdsa"
|
2021-11-12 09:19:42 +00:00
|
|
|
"crypto/rand"
|
2023-11-10 05:14:03 +00:00
|
|
|
"encoding/base64"
|
2021-11-12 09:19:42 +00:00
|
|
|
"encoding/hex"
|
2023-11-10 05:14:03 +00:00
|
|
|
"encoding/json"
|
2021-10-28 12:41:17 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2023-11-07 17:13:19 +00:00
|
|
|
"math"
|
2023-11-10 05:14:03 +00:00
|
|
|
"math/big"
|
2021-10-28 12:41:17 +00:00
|
|
|
"net"
|
2023-11-10 05:14:03 +00:00
|
|
|
"net/url"
|
2023-11-07 17:13:19 +00:00
|
|
|
"strconv"
|
2023-11-10 05:14:03 +00:00
|
|
|
"strings"
|
2021-10-28 12:41:17 +00:00
|
|
|
"testing"
|
2023-11-10 05:14:03 +00:00
|
|
|
"unicode/utf8"
|
2021-10-28 12:41:17 +00:00
|
|
|
|
2023-11-07 17:13:19 +00:00
|
|
|
gcrypto "github.com/ethereum/go-ethereum/crypto"
|
2022-05-30 15:55:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2021-10-28 12:41:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p"
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/libp2p/go-libp2p/config"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
2023-11-07 17:13:19 +00:00
|
|
|
libp2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
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-11-07 17:13:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-08-03 16:21:15 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peerstore"
|
2023-11-07 17:13:19 +00:00
|
|
|
wenr "github.com/waku-org/go-waku/waku/v2/protocol/enr"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2023-11-07 17:13:19 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
|
|
|
"go.uber.org/zap"
|
2021-10-28 12:41:17 +00:00
|
|
|
)
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
type StringGenerator func(maxLength int) (string, error)
|
|
|
|
|
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-07 20:18:37 +00:00
|
|
|
sourceMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
|
2023-06-05 14:39:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := pstoremem.NewPeerstore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-08-03 16:21:15 +00:00
|
|
|
psWrapper := peerstore.NewWakuPeerstore(ps)
|
2023-06-05 14:39:38 +00:00
|
|
|
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
|
2023-11-07 19:48:43 +00:00
|
|
|
func CreateWakuMessage(contentTopic string, timestamp *int64, optionalPayload ...string) *pb.WakuMessage {
|
2023-08-14 20:29:00 +00:00
|
|
|
var payload []byte
|
|
|
|
if len(optionalPayload) > 0 {
|
|
|
|
payload = []byte(optionalPayload[0])
|
|
|
|
} else {
|
|
|
|
payload = []byte{1, 2, 3}
|
|
|
|
}
|
2023-11-07 19:48:43 +00:00
|
|
|
return &pb.WakuMessage{Payload: payload, ContentTopic: contentTopic, Timestamp: timestamp}
|
2021-10-28 13:03:23 +00:00
|
|
|
}
|
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-11-07 17:13:19 +00:00
|
|
|
|
|
|
|
func NewLocalnode(priv *ecdsa.PrivateKey, ipAddr *net.TCPAddr, udpPort int, wakuFlags wenr.WakuEnrBitfield, advertiseAddr *net.IP, log *zap.Logger) (*enode.LocalNode, error) {
|
|
|
|
db, err := enode.OpenDB("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
localnode := enode.NewLocalNode(db, priv)
|
|
|
|
localnode.SetFallbackUDP(udpPort)
|
|
|
|
localnode.Set(enr.WithEntry(wenr.WakuENRField, wakuFlags))
|
|
|
|
localnode.SetFallbackIP(net.IP{127, 0, 0, 1})
|
|
|
|
localnode.SetStaticIP(ipAddr.IP)
|
|
|
|
|
|
|
|
if udpPort > 0 && udpPort <= math.MaxUint16 {
|
|
|
|
localnode.Set(enr.UDP(uint16(udpPort))) // lgtm [go/incorrect-integer-conversion]
|
|
|
|
} else {
|
|
|
|
log.Error("setting udpPort", zap.Int("port", udpPort))
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipAddr.Port > 0 && ipAddr.Port <= math.MaxUint16 {
|
|
|
|
localnode.Set(enr.TCP(uint16(ipAddr.Port))) // lgtm [go/incorrect-integer-conversion]
|
|
|
|
} else {
|
|
|
|
log.Error("setting tcpPort", zap.Int("port", ipAddr.Port))
|
|
|
|
}
|
|
|
|
|
|
|
|
if advertiseAddr != nil {
|
|
|
|
localnode.SetStaticIP(*advertiseAddr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return localnode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateHost(t *testing.T, opts ...config.Option) (host.Host, int, *ecdsa.PrivateKey) {
|
|
|
|
privKey, err := gcrypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sPrivKey := libp2pcrypto.PrivKey(utils.EcdsaPrivKeyToSecp256k1PrivKey(privKey))
|
|
|
|
|
|
|
|
port, err := FindFreePort(t, "127.0.0.1", 3)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
sourceMultiAddr, err := multiaddr.NewMultiaddr(fmt.Sprintf("/ip4/127.0.0.1/tcp/%d", port))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
opts = append(opts, libp2p.ListenAddrs(sourceMultiAddr),
|
|
|
|
libp2p.Identity(sPrivKey))
|
|
|
|
|
|
|
|
host, err := libp2p.New(opts...)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return host, port, privKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func ExtractIP(addr multiaddr.Multiaddr) (*net.TCPAddr, error) {
|
|
|
|
ipStr, err := addr.ValueForProtocol(multiaddr.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
portStr, err := addr.ValueForProtocol(multiaddr.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
port, err := strconv.Atoi(portStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &net.TCPAddr{
|
|
|
|
IP: net.ParseIP(ipStr),
|
|
|
|
Port: port,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-11-10 05:14:03 +00:00
|
|
|
|
|
|
|
func RandomInt(min, max int) (int, error) {
|
|
|
|
n, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1)))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return min + int(n.Int64()), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func RandomBytes(n int) ([]byte, error) {
|
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := rand.Read(b)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomASCIIString(maxLength int) (string, error) {
|
|
|
|
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength)))
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-15 13:38:08 +00:00
|
|
|
length.SetInt64(length.Int64() + 1)
|
2023-11-10 05:14:03 +00:00
|
|
|
|
|
|
|
const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
|
|
result := make([]byte, length.Int64())
|
|
|
|
for i := range result {
|
|
|
|
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(chars))))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
result[i] = chars[num.Int64()]
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(result), nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomUTF8String(maxLength int) (string, error) {
|
|
|
|
length, err := rand.Int(rand.Reader, big.NewInt(int64(maxLength)))
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-15 13:38:08 +00:00
|
|
|
length.SetInt64(length.Int64() + 1)
|
2023-11-10 05:14:03 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
runes []rune
|
|
|
|
start, end int
|
|
|
|
)
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
// Define unicode range
|
|
|
|
start = 0x0020 // Space character
|
|
|
|
end = 0x007F // Tilde (~)
|
2023-11-10 05:14:03 +00:00
|
|
|
|
|
|
|
for i := 0; int64(i) < length.Int64(); i++ {
|
|
|
|
randNum, err := rand.Int(rand.Reader, big.NewInt(int64(end-start+1)))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
char := rune(start + int(randNum.Int64()))
|
|
|
|
if !utf8.ValidRune(char) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
runes = append(runes, char)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(runes), nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomJSONString(maxLength int) (string, error) {
|
2023-11-10 05:14:03 +00:00
|
|
|
// With 5 key-value pairs
|
|
|
|
m := make(map[string]interface{})
|
|
|
|
for i := 0; i < 5; i++ {
|
2023-11-15 13:38:08 +00:00
|
|
|
key, err := GenerateRandomASCIIString(20)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-11-15 13:38:08 +00:00
|
|
|
value, err := GenerateRandomASCIIString(maxLength)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
m[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal the map into a JSON string
|
|
|
|
var buf bytes.Buffer
|
|
|
|
encoder := json.NewEncoder(&buf)
|
|
|
|
encoder.SetEscapeHTML(false)
|
|
|
|
err := encoder.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomBase64String(maxLength int) (string, error) {
|
|
|
|
bytes, err := RandomBytes(maxLength)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomURLEncodedString(maxLength int) (string, error) {
|
|
|
|
randomString, err := GenerateRandomASCIIString(maxLength)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// URL-encode the random string
|
|
|
|
return url.QueryEscape(randomString), nil
|
|
|
|
}
|
|
|
|
|
2023-11-15 13:38:08 +00:00
|
|
|
func GenerateRandomSQLInsert(maxLength int) (string, error) {
|
2023-11-10 05:14:03 +00:00
|
|
|
// Random table name
|
2023-11-15 13:38:08 +00:00
|
|
|
tableName, err := GenerateRandomASCIIString(10)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random column names
|
|
|
|
columnCount, err := RandomInt(3, 6)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
columnNames := make([]string, columnCount)
|
|
|
|
for i := 0; i < columnCount; i++ {
|
2023-11-15 13:38:08 +00:00
|
|
|
columnName, err := GenerateRandomASCIIString(maxLength)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
columnNames[i] = columnName
|
|
|
|
}
|
|
|
|
|
|
|
|
// Random values
|
|
|
|
values := make([]string, columnCount)
|
|
|
|
for i := 0; i < columnCount; i++ {
|
2023-11-15 13:38:08 +00:00
|
|
|
value, err := GenerateRandomASCIIString(maxLength)
|
2023-11-10 05:14:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
values[i] = "'" + value + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
query := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s);",
|
|
|
|
tableName,
|
|
|
|
strings.Join(columnNames, ", "),
|
|
|
|
strings.Join(values, ", "))
|
|
|
|
|
|
|
|
return query, nil
|
|
|
|
}
|