2021-11-18 13:21:36 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
2022-06-13 18:30:35 +00:00
|
|
|
"time"
|
2021-11-18 13:21:36 +00:00
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2021-11-18 13:21:36 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/node"
|
2022-05-06 19:29:31 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/utils"
|
2021-11-18 13:21:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func makePrivateService(t *testing.T) *PrivateService {
|
2021-12-09 09:07:08 +00:00
|
|
|
n, err := node.New(context.Background(), node.WithWakuRelayAndMinPeers(0))
|
2021-11-18 13:21:36 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
err = n.Start()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-30 15:55:30 +00:00
|
|
|
return NewPrivateService(n, utils.Logger())
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetV1SymmetricKey(t *testing.T) {
|
|
|
|
d := makePrivateService(t)
|
|
|
|
defer d.node.Stop()
|
|
|
|
|
|
|
|
var reply SymmetricKeyReply
|
|
|
|
err := d.GetV1SymmetricKey(
|
|
|
|
makeRequest(t),
|
|
|
|
&Empty{},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.NotEmpty(t, reply)
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 08:52:27 +00:00
|
|
|
func TestGetV1AsymmetricKey(t *testing.T) {
|
2021-11-18 13:21:36 +00:00
|
|
|
d := makePrivateService(t)
|
|
|
|
defer d.node.Stop()
|
|
|
|
|
|
|
|
var reply KeyPairReply
|
|
|
|
err := d.GetV1AsymmetricKeypair(
|
|
|
|
makeRequest(t),
|
|
|
|
&Empty{},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.NotEmpty(t, reply.PublicKey)
|
2021-12-08 08:52:27 +00:00
|
|
|
require.NotEmpty(t, reply.PrivateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostV1SymmetricMessage(t *testing.T) {
|
|
|
|
d := makePrivateService(t)
|
|
|
|
defer d.node.Stop()
|
|
|
|
|
|
|
|
var reply SuccessReply
|
|
|
|
err := d.PostV1SymmetricMessage(
|
|
|
|
makeRequest(t),
|
|
|
|
&SymmetricMessageArgs{
|
|
|
|
Topic: "test",
|
2022-06-13 18:30:35 +00:00
|
|
|
Message: RPCWakuMessage{Payload: []byte("test")},
|
|
|
|
SymKey: "0x1122334455667788991011223344556677889910112233445566778899101122",
|
2021-12-08 08:52:27 +00:00
|
|
|
},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-14 15:36:34 +00:00
|
|
|
require.True(t, reply)
|
2021-12-08 08:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostV1AsymmetricMessage(t *testing.T) {
|
|
|
|
d := makePrivateService(t)
|
|
|
|
defer d.node.Stop()
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
var reply bool
|
2021-12-08 08:52:27 +00:00
|
|
|
err := d.PostV1AsymmetricMessage(
|
|
|
|
makeRequest(t),
|
|
|
|
&AsymmetricMessageArgs{
|
|
|
|
Topic: "test",
|
2022-06-13 18:30:35 +00:00
|
|
|
Message: RPCWakuMessage{Payload: []byte("test")},
|
|
|
|
PublicKey: "0x045ded6a56c88173e87a88c55b96956964b1bd3351b5fcb70950a4902fbc1bc0ceabb0ac846c3a4b8f2f6024c0e19f0a7f6a4865035187de5463f34012304fc7c5",
|
2021-12-08 08:52:27 +00:00
|
|
|
},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.True(t, reply)
|
2021-11-18 13:21:36 +00:00
|
|
|
}
|
2021-12-09 09:07:08 +00:00
|
|
|
|
|
|
|
func TestGetV1SymmetricMessages(t *testing.T) {
|
|
|
|
d := makePrivateService(t)
|
2022-06-13 18:30:35 +00:00
|
|
|
go d.Start()
|
2021-12-09 09:07:08 +00:00
|
|
|
defer d.node.Stop()
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
// Subscribing topic to test getter
|
|
|
|
_, err := d.node.Relay().SubscribeToTopic(context.TODO(), "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-09 09:07:08 +00:00
|
|
|
var reply SuccessReply
|
2022-06-13 18:30:35 +00:00
|
|
|
err = d.PostV1SymmetricMessage(
|
2021-12-09 09:07:08 +00:00
|
|
|
makeRequest(t),
|
|
|
|
&SymmetricMessageArgs{
|
|
|
|
Topic: "test",
|
2022-06-13 18:30:35 +00:00
|
|
|
Message: RPCWakuMessage{Payload: []byte("test")},
|
|
|
|
SymKey: "0x1122334455667788991011223344556677889910112233445566778899101122",
|
2021-12-09 09:07:08 +00:00
|
|
|
},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-14 15:36:34 +00:00
|
|
|
require.True(t, reply)
|
2021-12-09 09:07:08 +00:00
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
2021-12-09 09:07:08 +00:00
|
|
|
var getReply MessagesReply
|
|
|
|
err = d.GetV1SymmetricMessages(
|
|
|
|
makeRequest(t),
|
2022-06-13 18:30:35 +00:00
|
|
|
&SymmetricMessagesArgs{
|
|
|
|
Topic: "test",
|
|
|
|
SymKey: "0x1122334455667788991011223344556677889910112233445566778899101122",
|
|
|
|
},
|
2021-12-09 09:07:08 +00:00
|
|
|
&getReply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.Len(t, getReply, 1)
|
2021-12-09 09:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetV1AsymmetricMessages(t *testing.T) {
|
|
|
|
d := makePrivateService(t)
|
2022-06-13 18:30:35 +00:00
|
|
|
go d.Start()
|
2021-12-09 09:07:08 +00:00
|
|
|
defer d.node.Stop()
|
|
|
|
|
2022-06-13 18:30:35 +00:00
|
|
|
// Subscribing topic to test getter
|
|
|
|
_, err := d.node.Relay().SubscribeToTopic(context.TODO(), "test")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
prvKey, err := crypto.GenerateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var reply bool
|
|
|
|
err = d.PostV1AsymmetricMessage(
|
2021-12-09 09:07:08 +00:00
|
|
|
makeRequest(t),
|
|
|
|
&AsymmetricMessageArgs{
|
|
|
|
Topic: "test",
|
2022-06-13 18:30:35 +00:00
|
|
|
Message: RPCWakuMessage{Payload: []byte("test")},
|
|
|
|
PublicKey: hexutil.Encode(crypto.FromECDSAPub(&prvKey.PublicKey)),
|
2021-12-09 09:07:08 +00:00
|
|
|
},
|
|
|
|
&reply,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.True(t, reply)
|
|
|
|
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
2021-12-09 09:07:08 +00:00
|
|
|
|
|
|
|
var getReply MessagesReply
|
|
|
|
err = d.GetV1AsymmetricMessages(
|
|
|
|
makeRequest(t),
|
|
|
|
&AsymmetricMessagesArgs{
|
|
|
|
Topic: "test",
|
2022-06-13 18:30:35 +00:00
|
|
|
PrivateKey: hexutil.Encode(crypto.FromECDSA(prvKey)),
|
2021-12-09 09:07:08 +00:00
|
|
|
},
|
|
|
|
&getReply,
|
|
|
|
)
|
2022-06-13 18:30:35 +00:00
|
|
|
|
2021-12-09 09:07:08 +00:00
|
|
|
require.NoError(t, err)
|
2022-06-13 18:30:35 +00:00
|
|
|
require.Len(t, getReply, 1)
|
2021-12-09 09:07:08 +00:00
|
|
|
}
|