2021-08-13 11:56:09 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/hex"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/pb"
|
2021-10-12 13:12:54 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-08-13 11:56:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBasicSendingReceiving(t *testing.T) {
|
|
|
|
hostAddr, err := net.ResolveTCPAddr("tcp", "0.0.0.0:0")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
key, err := randomHex(32)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
prvKey, err := crypto.HexToECDSA(key)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
wakuNode, err := node.New(ctx,
|
|
|
|
node.WithPrivateKey(prvKey),
|
2021-10-18 12:38:01 +00:00
|
|
|
node.WithHostAddress([]*net.TCPAddr{hostAddr}),
|
2021-08-13 11:56:09 +00:00
|
|
|
node.WithWakuRelay(),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, wakuNode)
|
|
|
|
|
2021-10-06 15:42:57 +00:00
|
|
|
err = wakuNode.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-08-13 11:56:09 +00:00
|
|
|
require.NoError(t, write(ctx, wakuNode, "test"))
|
|
|
|
|
2021-10-11 22:45:54 +00:00
|
|
|
sub, err := wakuNode.Subscribe(ctx, nil)
|
2021-08-13 11:56:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := <-sub.C
|
|
|
|
payload, err := node.DecodePayload(value.Message(), &node.KeyInfo{Kind: node.None})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Contains(t, string(payload.Data), "test")
|
|
|
|
}
|
|
|
|
|
|
|
|
func randomHex(n int) (string, error) {
|
|
|
|
bytes := make([]byte, n)
|
|
|
|
if _, err := rand.Read(bytes); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return hex.EncodeToString(bytes), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func write(ctx context.Context, wakuNode *node.WakuNode, msgContent string) error {
|
|
|
|
var contentTopic string = "test"
|
|
|
|
var version uint32 = 0
|
2021-10-12 13:12:54 +00:00
|
|
|
var timestamp float64 = utils.GetUnixEpoch()
|
2021-08-13 11:56:09 +00:00
|
|
|
|
|
|
|
p := new(node.Payload)
|
|
|
|
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
|
|
|
|
p.Key = &node.KeyInfo{Kind: node.None}
|
|
|
|
|
|
|
|
payload, err := p.Encode(version)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &pb.WakuMessage{
|
|
|
|
Payload: payload,
|
|
|
|
Version: version,
|
|
|
|
ContentTopic: contentTopic,
|
|
|
|
Timestamp: timestamp,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = wakuNode.Publish(ctx, msg, nil)
|
|
|
|
return err
|
|
|
|
}
|