2018-04-12 16:17:10 +00:00
|
|
|
package node_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
gethnode "github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
type testService struct{}
|
|
|
|
|
|
|
|
func (s *testService) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testService) APIs() []rpc.API {
|
|
|
|
return []rpc.API{
|
|
|
|
{
|
|
|
|
Namespace: "pri",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: &TestServiceAPI{},
|
|
|
|
Public: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Namespace: "pub",
|
|
|
|
Version: "1.0",
|
|
|
|
Service: &TestServiceAPI{},
|
|
|
|
Public: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testService) Start(server *p2p.Server) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *testService) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
func createAndStartStatusNode(config *params.NodeConfig) (*node.StatusNode, error) {
|
2018-04-12 16:17:10 +00:00
|
|
|
services := []gethnode.ServiceConstructor{
|
|
|
|
func(_ *gethnode.ServiceContext) (gethnode.Service, error) {
|
|
|
|
return &testService{}, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
statusNode := node.New()
|
2019-08-20 15:38:40 +00:00
|
|
|
return statusNode, statusNode.Start(config, nil, services...)
|
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
|
|
|
|
})
|
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)
|
|
|
|
|
|
|
|
var result string
|
|
|
|
|
|
|
|
// call public API
|
|
|
|
err = client.Call(&result, "pub_someMethod")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "some method result", result)
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// call private API with public RPC client
|
2018-04-12 16:17:10 +00:00
|
|
|
err = client.Call(&result, "pri_someMethod")
|
|
|
|
require.EqualError(t, err, "The method pri_someMethod does not exist/is not available")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeRPCClientCallWhitelistedPrivateService(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{
|
|
|
|
APIModules: "pri",
|
|
|
|
})
|
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)
|
|
|
|
|
|
|
|
// call private API
|
|
|
|
var result string
|
|
|
|
err = client.Call(&result, "pri_someMethod")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "some method result", result)
|
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
|
|
|
func TestNodeRPCPrivateClientCallPrivateService(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
statusNode, err := createAndStartStatusNode(¶ms.NodeConfig{})
|
2018-04-16 08:01:37 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
err := statusNode.Stop()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
client := statusNode.RPCPrivateClient()
|
|
|
|
require.NotNil(t, client)
|
|
|
|
|
|
|
|
// call private API with private RPC client
|
|
|
|
var result string
|
|
|
|
err = client.Call(&result, "pri_someMethod")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "some method result", result)
|
|
|
|
}
|