go-waku/tests/connection_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

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"
"google.golang.org/protobuf/proto"
2021-08-13 11:56:09 +00:00
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/payload"
"github.com/waku-org/go-waku/waku/v2/protocol"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
"github.com/waku-org/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()
2023-01-06 22:37:57 +00:00
wakuNode, err := node.New(
2021-08-13 11:56:09 +00:00
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)
2023-01-06 22:37:57 +00:00
err = wakuNode.Start(ctx)
2021-10-06 15:42:57 +00:00
require.NoError(t, err)
2021-08-13 11:56:09 +00:00
require.NoError(t, write(ctx, wakuNode, "test"))
sub, err := wakuNode.Relay().Subscribe(ctx, protocol.NewContentFilter(relay.DefaultWakuTopic))
2021-08-13 11:56:09 +00:00
require.NoError(t, err)
value := <-sub[0].Ch
payload, err := payload.DecodePayload(value.Message(), &payload.KeyInfo{Kind: payload.None})
2021-08-13 11:56:09 +00:00
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"
version := uint32(0)
timestamp := utils.GetUnixEpoch()
2021-08-13 11:56:09 +00:00
p := new(payload.Payload)
2021-08-13 11:56:09 +00:00
p.Data = []byte(wakuNode.ID() + ": " + msgContent)
p.Key = &payload.KeyInfo{Kind: payload.None}
2021-08-13 11:56:09 +00:00
payload, err := p.Encode(version)
if err != nil {
return err
}
msg := &pb.WakuMessage{
Payload: payload,
Version: proto.Uint32(version),
2021-08-13 11:56:09 +00:00
ContentTopic: contentTopic,
Timestamp: timestamp,
}
_, err = wakuNode.Relay().Publish(ctx, msg, relay.WithDefaultPubsubTopic())
2021-08-13 11:56:09 +00:00
return err
}