2021-11-10 13:36:51 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2021-11-17 16:19:42 +00:00
|
|
|
"time"
|
2021-11-10 13:36:51 +00:00
|
|
|
|
|
|
|
"github.com/multiformats/go-multiaddr"
|
|
|
|
|
|
|
|
"github.com/status-im/go-waku/tests"
|
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2021-11-17 16:19:42 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/relay"
|
2022-05-06 19:29:31 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-11-10 13:36:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makeAdminService(t *testing.T) *AdminService {
|
|
|
|
options := node.WithWakuRelay()
|
|
|
|
n, err := node.New(context.Background(), options)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = n.Start()
|
|
|
|
require.NoError(t, err)
|
2022-05-30 15:55:30 +00:00
|
|
|
return &AdminService{n, utils.Logger()}
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestV1Peers(t *testing.T) {
|
|
|
|
port, err := tests.FindFreePort(t, "", 5)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
host, err := tests.MakeHost(context.Background(), port, rand.Reader)
|
|
|
|
require.NoError(t, err)
|
2022-05-30 15:55:30 +00:00
|
|
|
relay, err := relay.NewWakuRelay(context.Background(), host, nil, 0, utils.Logger())
|
2021-11-17 16:19:42 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer relay.Stop()
|
2021-11-10 13:36:51 +00:00
|
|
|
|
|
|
|
var reply PeersReply
|
|
|
|
|
|
|
|
request, err := http.NewRequest(http.MethodPost, "url", bytes.NewReader([]byte("")))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
a := makeAdminService(t)
|
|
|
|
|
|
|
|
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
|
|
|
|
require.NoError(t, err)
|
2022-07-28 18:29:35 +00:00
|
|
|
require.Len(t, reply, 0)
|
2021-11-10 13:36:51 +00:00
|
|
|
|
|
|
|
var reply2 SuccessReply
|
|
|
|
|
|
|
|
hostInfo, err := multiaddr.NewMultiaddr(fmt.Sprintf("/p2p/%s", host.ID().Pretty()))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var addr multiaddr.Multiaddr
|
|
|
|
for _, a := range host.Addrs() {
|
|
|
|
addr = a.Encapsulate(hostInfo)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
err = a.PostV1Peers(request, &PeersArgs{Peers: []string{addr.String()}}, &reply2)
|
|
|
|
require.NoError(t, err)
|
2022-06-14 15:36:34 +00:00
|
|
|
require.True(t, reply2)
|
2021-11-10 13:36:51 +00:00
|
|
|
|
2021-11-17 16:19:42 +00:00
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2021-11-10 13:36:51 +00:00
|
|
|
err = a.GetV1Peers(request, &GetPeersArgs{}, &reply)
|
|
|
|
require.NoError(t, err)
|
2022-07-28 18:29:35 +00:00
|
|
|
require.Len(t, reply, 2)
|
2021-11-10 13:36:51 +00:00
|
|
|
}
|