From ce49f29c0839bfac3d801eee8eade7a7811040b5 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Fri, 22 Oct 2021 16:19:38 +0200 Subject: [PATCH] test: add test for relay protocol --- waku/v2/protocol/relay/waku_relay.go | 1 - waku/v2/protocol/relay/waku_relay_test.go | 112 ++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 waku/v2/protocol/relay/waku_relay_test.go diff --git a/waku/v2/protocol/relay/waku_relay.go b/waku/v2/protocol/relay/waku_relay.go index b70cf3f2..4b8bb82a 100644 --- a/waku/v2/protocol/relay/waku_relay.go +++ b/waku/v2/protocol/relay/waku_relay.go @@ -114,7 +114,6 @@ func (w *WakuRelay) upsertTopic(topic Topic) (*pubsub.Topic, error) { } func (w *WakuRelay) Subscribe(topic Topic) (subs *pubsub.Subscription, isNew bool, err error) { - sub, ok := w.relaySubs[topic] if !ok { pubSubTopic, err := w.upsertTopic(topic) diff --git a/waku/v2/protocol/relay/waku_relay_test.go b/waku/v2/protocol/relay/waku_relay_test.go new file mode 100644 index 00000000..e265d7fd --- /dev/null +++ b/waku/v2/protocol/relay/waku_relay_test.go @@ -0,0 +1,112 @@ +package relay + +import ( + "context" + "crypto/rand" + "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" + "github.com/status-im/go-waku/waku/v2/protocol/pb" + "github.com/stretchr/testify/require" +) + +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) + 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( + ctx, + libp2p.ListenAddrs(sourceMultiAddr), + libp2p.Identity(prvKey), + ) +} + +func TestWakuRelay(t *testing.T) { + var testTopic Topic = "/waku/2/go/relay/test" + + port, err := findFreePort(t, "", 5) + require.NoError(t, err) + + host, err := makeHost(context.Background(), port, rand.Reader) + require.NoError(t, err) + + relay, err := NewWakuRelay(context.Background(), host) + defer relay.Stop() + require.NoError(t, err) + + sub, isNew, err := relay.Subscribe(testTopic) + defer sub.Cancel() + require.NoError(t, err) + require.True(t, isNew) + + _, isNew, err = relay.Subscribe(testTopic) + require.NoError(t, err) + require.False(t, isNew) + + topics := relay.Topics() + require.Equal(t, 1, len(topics)) + require.Equal(t, testTopic, topics[0]) + + ctx, cancel := context.WithCancel(context.Background()) + go func() { + defer cancel() + + _, err := sub.Next(ctx) + require.NoError(t, err) + }() + + msg := &pb.WakuMessage{ + Payload: []byte{1}, + Version: 0, + ContentTopic: "test", + Timestamp: 0, + } + _, err = relay.Publish(context.Background(), msg, &testTopic) + require.NoError(t, err) + + <-ctx.Done() +}