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"
|
|
|
|
|
2019-01-17 12:56:22 +00:00
|
|
|
"github.com/status-im/status-go/t/utils"
|
2018-05-03 10:36:56 +00:00
|
|
|
)
|
|
|
|
|
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 {
|
2019-11-23 17:57:05 +00:00
|
|
|
s.Backend = api.NewGethStatusBackend()
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NotNil(s.Backend)
|
|
|
|
|
2019-01-17 12:56:22 +00:00
|
|
|
nodeConfig, err := utils.MakeTestNodeConfig(utils.GetNetworkID())
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NoError(err)
|
2019-08-20 15:38:40 +00:00
|
|
|
s.NoError(s.Backend.AccountManager().InitKeystore(nodeConfig.KeyStoreDir))
|
2018-05-03 10:36:56 +00:00
|
|
|
|
|
|
|
nodeConfig.IPCEnabled = false
|
2019-01-17 12:56:22 +00:00
|
|
|
nodeConfig.EnableStatusService = statusServiceEnabled
|
|
|
|
if debugAPIEnabled {
|
2018-06-25 13:27:17 +00:00
|
|
|
nodeConfig.AddAPIModule("debug")
|
|
|
|
}
|
2018-05-03 10:36:56 +00:00
|
|
|
nodeConfig.HTTPHost = "" // to make sure that no HTTP interface is started
|
|
|
|
|
|
|
|
if upstreamEnabled {
|
2019-01-17 12:56:22 +00:00
|
|
|
networkURL, err := utils.GetRemoteURL()
|
2018-05-03 10:36:56 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
nodeConfig.UpstreamConfig.Enabled = true
|
|
|
|
nodeConfig.UpstreamConfig.URL = networkURL
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.Backend.StartNode(nodeConfig)
|
|
|
|
}
|