2021-07-09 13:19:33 +00:00
|
|
|
package node
|
2018-04-12 16:17:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-09 13:19:33 +00:00
|
|
|
"strings"
|
2018-04-12 16:17:10 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2018-04-12 16:17:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type TestServiceAPI struct{}
|
|
|
|
|
|
|
|
func (api *TestServiceAPI) SomeMethod(_ context.Context) (string, error) {
|
|
|
|
return "some method result", nil
|
|
|
|
}
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
func createAndStartStatusNode(config *params.NodeConfig) (*StatusNode, error) {
|
2021-12-21 15:44:37 +00:00
|
|
|
statusNode := New(nil)
|
2021-07-09 13:19:33 +00:00
|
|
|
err := statusNode.Start(config, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-04-12 16:17:10 +00:00
|
|
|
}
|
2021-07-09 13:19:33 +00:00
|
|
|
return statusNode, nil
|
2018-04-12 16:17:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRPCClientCallOnlyPublicAPIs(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{
|
|
|
|
APIModules: "", // no whitelisted API modules; use only public APIs
|
2021-07-09 13:19:33 +00:00
|
|
|
UpstreamConfig: params.UpstreamRPCConfig{
|
|
|
|
URL: "https://infura.io",
|
|
|
|
Enabled: true},
|
|
|
|
WakuConfig: params.WakuConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
2018-04-16 12:36:09 +00:00
|
|
|
})
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err := statusNode.Stop()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
client := statusNode.RPCClient()
|
|
|
|
require.NotNil(t, client)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// call public API with public RPC Client
|
|
|
|
result, err := statusNode.CallRPC(`{"jsonrpc": "2.0", "id": 1, "method": "eth_uninstallFilter", "params": ["id"]}`)
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// the call is successful
|
|
|
|
require.False(t, strings.Contains(result, "error"))
|
2018-04-12 16:17:10 +00:00
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
result, err = statusNode.CallRPC(`{"jsonrpc": "2.0", "id": 1, "method": "waku_info"}`)
|
2018-04-12 16:17:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
// call private API with public RPC client
|
|
|
|
require.Equal(t, ErrRPCMethodUnavailable, result)
|
2018-04-12 16:17:10 +00:00
|
|
|
|
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
|
|
|
func TestNodeRPCPrivateClientCallPrivateService(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2021-07-12 14:16:33 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{
|
|
|
|
WakuConfig: params.WakuConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
|
|
|
})
|
2018-04-16 08:01:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err := statusNode.Stop()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
result, err := statusNode.CallPrivateRPC(`{"jsonrpc": "2.0", "id": 1, "method": "waku_info"}`)
|
2018-04-16 08:01:37 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-09 13:19:33 +00:00
|
|
|
|
|
|
|
// the call is successful
|
|
|
|
require.False(t, strings.Contains(result, "error"))
|
2018-04-16 08:01:37 +00:00
|
|
|
}
|