2021-08-13 11:56:09 +00:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"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)
|
|
|
|
|
2021-11-12 09:19:42 +00:00
|
|
|
key, err := RandomHex(32)
|
2021-08-13 11:56:09 +00:00
|
|
|
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-11-17 16:19:42 +00:00
|
|
|
node.WithHostAddress(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-11-19 20:01:52 +00:00
|
|
|
sub, err := wakuNode.Relay().Subscribe(ctx)
|
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 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,
|
|
|
|
}
|
|
|
|
|
2021-11-19 20:01:52 +00:00
|
|
|
_, err = wakuNode.Relay().Publish(ctx, msg)
|
2021-08-13 11:56:09 +00:00
|
|
|
return err
|
|
|
|
}
|