2018-05-03 10:36:56 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2018-05-03 10:36:56 +00:00
|
|
|
"github.com/status-im/status-go/t/e2e"
|
|
|
|
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
|
|
|
)
|
|
|
|
|
2018-08-16 11:37:53 +00:00
|
|
|
const (
|
|
|
|
// see vendor/github.com/ethereum/go-ethereum/rpc/errors.go:L27
|
|
|
|
methodNotFoundErrorCode = -32601
|
|
|
|
)
|
|
|
|
|
|
|
|
type rpcError struct {
|
|
|
|
Code int `json:"code"`
|
|
|
|
}
|
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
type BaseJSONRPCSuite struct {
|
|
|
|
e2e.BackendTestSuite
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseJSONRPCSuite) AssertAPIMethodUnexported(method string) {
|
|
|
|
exported := s.isMethodExported(method, false)
|
|
|
|
s.False(exported,
|
|
|
|
"method %s should be hidden, but it isn't",
|
|
|
|
method)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseJSONRPCSuite) AssertAPIMethodExported(method string) {
|
|
|
|
exported := s.isMethodExported(method, false)
|
|
|
|
s.True(exported,
|
|
|
|
"method %s should be exported, but it isn't",
|
|
|
|
method)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseJSONRPCSuite) AssertAPIMethodExportedPrivately(method string) {
|
|
|
|
exported := s.isMethodExported(method, true)
|
|
|
|
s.True(exported,
|
|
|
|
"method %s should be exported, but it isn't",
|
|
|
|
method)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *BaseJSONRPCSuite) isMethodExported(method string, private bool) bool {
|
2018-12-20 08:31:17 +00:00
|
|
|
var (
|
|
|
|
result string
|
|
|
|
err error
|
|
|
|
)
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
cmd := fmt.Sprintf(`{"jsonrpc":"2.0", "method": "%s", "params": []}`, method)
|
|
|
|
if private {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err = s.Backend.CallPrivateRPC(cmd)
|
2018-05-03 10:36:56 +00:00
|
|
|
} else {
|
2018-12-20 08:31:17 +00:00
|
|
|
result, err = s.Backend.CallRPC(cmd)
|
2018-05-03 10:36:56 +00:00
|
|
|
}
|
2018-12-20 08:31:17 +00:00
|
|
|
s.NoError(err)
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
var response struct {
|
|
|
|
Error *rpcError `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
s.NoError(json.Unmarshal([]byte(result), &response))
|
|
|
|
|
|
|
|
return !(response.Error != nil && response.Error.Code == methodNotFoundErrorCode)
|
|
|
|
}
|
|
|
|
|
2018-06-25 13:27:17 +00:00
|
|
|
func (s *BaseJSONRPCSuite) SetupTest(upstreamEnabled, statusServiceEnabled, debugAPIEnabled bool) error {
|
2018-05-03 10:36:56 +00:00
|
|
|
s.Backend = api.NewStatusBackend()
|
|
|
|
s.NotNil(s.Backend)
|
|
|
|
|
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
nodeConfig.IPCEnabled = false
|
|
|
|
nodeConfig.StatusServiceEnabled = statusServiceEnabled
|
2018-06-25 13:27:17 +00:00
|
|
|
nodeConfig.DebugAPIEnabled = debugAPIEnabled
|
|
|
|
if nodeConfig.DebugAPIEnabled {
|
|
|
|
nodeConfig.AddAPIModule("debug")
|
|
|
|
}
|
2018-05-03 10:36:56 +00:00
|
|
|
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
|
|
|
|
|
|
|
|
if upstreamEnabled {
|
|
|
|
networkURL, err := GetRemoteURL()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
nodeConfig.UpstreamConfig.Enabled = true
|
|
|
|
nodeConfig.UpstreamConfig.URL = networkURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Backend.StartNode(nodeConfig)
|
|
|
|
}
|