2017-10-11 14:20:51 +00:00
|
|
|
package jail
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-11-07 17:36:42 +00:00
|
|
|
"fmt"
|
2017-10-16 21:07:42 +00:00
|
|
|
"strconv"
|
2017-11-07 17:36:42 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2017-10-11 14:20:51 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
"github.com/status-im/status-go/e2e"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
|
|
|
"github.com/status-im/status-go/geth/jail"
|
2017-11-07 17:36:42 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
|
|
|
"github.com/status-im/status-go/static"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
testChatID = "testChat"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
baseStatusJSCode = string(static.MustAsset("testdata/jail/status.js"))
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJailTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(JailTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type JailTestSuite struct {
|
2017-11-07 17:36:42 +00:00
|
|
|
e2e.NodeManagerTestSuite
|
|
|
|
Jail common.JailManager
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailTestSuite) SetupTest() {
|
2017-11-07 17:36:42 +00:00
|
|
|
s.NodeManager = node.NewNodeManager()
|
|
|
|
s.Jail = jail.New(s.NodeManager)
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
func (s *JailTestSuite) TearDownTest() {
|
|
|
|
s.Jail.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailTestSuite) TestInitWithoutBaseJS() {
|
2017-10-11 14:20:51 +00:00
|
|
|
errorWrapper := func(err error) string {
|
|
|
|
return `{"error":"` + err.Error() + `"}`
|
|
|
|
}
|
|
|
|
|
|
|
|
// get cell VM w/o defining cell first
|
2017-11-07 17:36:42 +00:00
|
|
|
cell, err := s.Jail.Cell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
s.EqualError(err, "cell '"+testChatID+"' not found")
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Nil(cell)
|
|
|
|
|
|
|
|
// create VM (w/o properly initializing base JS script)
|
|
|
|
err = errors.New("ReferenceError: '_status_catalog' is not defined")
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Equal(errorWrapper(err), s.Jail.CreateAndInitCell(testChatID, ``))
|
2017-10-11 14:20:51 +00:00
|
|
|
err = errors.New("ReferenceError: 'call' is not defined")
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Equal(errorWrapper(err), s.Jail.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`))
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// get existing cell (even though we got errors, cell was still created)
|
2017-11-07 17:36:42 +00:00
|
|
|
cell, err = s.Jail.Cell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(cell)
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
func (s *JailTestSuite) TestInitWithBaseJS() {
|
2017-10-11 14:20:51 +00:00
|
|
|
statusJS := baseStatusJSCode + `;
|
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};`
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Jail.SetBaseJS(statusJS)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// now no error should occur
|
2017-11-07 17:36:42 +00:00
|
|
|
response := s.Jail.CreateAndInitCell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedResponse := `{"result": {"commands":{},"responses":{}}}`
|
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
|
|
|
|
// make sure that Call succeeds even w/o running node
|
2017-11-07 17:36:42 +00:00
|
|
|
response = s.Jail.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedResponse = `{"result": 144}`
|
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// @TODO(adam): finally, this test should pass as checking existence of `_status_catalog`
|
|
|
|
// should be done in status-react.
|
|
|
|
func (s *JailTestSuite) TestCreateAndInitCellWithoutStatusCatalog() {
|
|
|
|
response := s.Jail.CreateAndInitCell(testChatID)
|
|
|
|
s.Equal(`{"error":"ReferenceError: '_status_catalog' is not defined"}`, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO(adam): remove extra JS when checking `_status_catalog` is move to status-react.
|
|
|
|
func (s *JailTestSuite) TestMultipleInitError() {
|
|
|
|
response := s.Jail.CreateAndInitCell(testChatID, `var _status_catalog = {}`)
|
|
|
|
s.Equal(`{"result": {}}`, response)
|
|
|
|
|
2017-12-08 15:32:30 +00:00
|
|
|
// Shouldn't cause an error and reinitialize existing
|
2017-11-07 17:36:42 +00:00
|
|
|
response = s.Jail.CreateAndInitCell(testChatID)
|
2017-12-08 15:32:30 +00:00
|
|
|
s.Equal(`{"result": {}}`, response)
|
2017-11-07 17:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO(adam): remove extra JS when checking `_status_catalog` is moved to status-react.
|
|
|
|
func (s *JailTestSuite) TestCreateAndInitCellResponse() {
|
2017-10-11 14:20:51 +00:00
|
|
|
extraCode := `
|
|
|
|
var _status_catalog = {
|
|
|
|
foo: 'bar'
|
|
|
|
};`
|
2017-11-07 17:36:42 +00:00
|
|
|
response := s.Jail.CreateAndInitCell("newChat", extraCode)
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedResponse := `{"result": {"foo":"bar"}}`
|
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailTestSuite) TestFunctionCall() {
|
|
|
|
// load Status JS and add test command to it
|
|
|
|
statusJS := baseStatusJSCode + `;
|
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};`
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Jail.CreateAndInitCell(testChatID, statusJS)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// call with wrong chat id
|
2017-11-07 17:36:42 +00:00
|
|
|
response := s.Jail.Call("chatIDNonExistent", "", "")
|
|
|
|
expectedError := `{"error":"cell 'chatIDNonExistent' not found"}`
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Equal(expectedError, response)
|
|
|
|
|
|
|
|
// call extraFunc()
|
2017-11-07 17:36:42 +00:00
|
|
|
response = s.Jail.Call(testChatID, `["commands", "testCommand"]`, `{"val": 12}`)
|
2017-10-11 14:20:51 +00:00
|
|
|
expectedResponse := `{"result": 144}`
|
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *JailTestSuite) TestEventSignal() {
|
2017-11-07 17:36:42 +00:00
|
|
|
s.StartTestNode()
|
|
|
|
defer s.StopTestNode()
|
|
|
|
|
|
|
|
s.Jail.CreateAndInitCell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// obtain VM for a given chat (to send custom JS to jailed version of Send())
|
2017-11-07 17:36:42 +00:00
|
|
|
cell, err := s.Jail.Cell(testChatID)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
testData := "foobar"
|
|
|
|
opCompletedSuccessfully := make(chan struct{}, 1)
|
|
|
|
|
|
|
|
// replace transaction notification handler
|
2017-11-27 21:56:52 +00:00
|
|
|
defer signal.ResetDefaultNodeNotificationHandler()
|
2017-10-11 14:20:51 +00:00
|
|
|
signal.SetDefaultNodeNotificationHandler(func(jsonEvent string) {
|
|
|
|
var envelope signal.Envelope
|
2017-11-27 21:56:52 +00:00
|
|
|
unmarshalErr := json.Unmarshal([]byte(jsonEvent), &envelope)
|
|
|
|
s.NoError(unmarshalErr)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
if envelope.Type == jail.EventSignal {
|
|
|
|
event := envelope.Event.(map[string]interface{})
|
|
|
|
chatID, ok := event["chat_id"].(string)
|
|
|
|
s.True(ok, "chat id is required, but not found")
|
|
|
|
s.Equal(testChatID, chatID, "incorrect chat ID")
|
|
|
|
|
|
|
|
actualData, ok := event["data"].(string)
|
|
|
|
s.True(ok, "data field is required, but not found")
|
|
|
|
s.Equal(testData, actualData, "incorrect data")
|
|
|
|
|
|
|
|
close(opCompletedSuccessfully)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
var responseValue = statusSignals.sendSignal("` + testData + `");
|
|
|
|
responseValue = JSON.stringify(responseValue);
|
|
|
|
`)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that signal is sent (and its parameters are correct)
|
|
|
|
select {
|
|
|
|
case <-opCompletedSuccessfully:
|
|
|
|
// pass
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
s.FailNow("test timed out")
|
|
|
|
}
|
|
|
|
|
|
|
|
responseValue, err := cell.Get("responseValue")
|
|
|
|
s.NoError(err, "cannot obtain result of localStorage.set()")
|
|
|
|
|
|
|
|
response, err := responseValue.ToString()
|
|
|
|
s.NoError(err, "cannot parse result")
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
expectedResponse := `{"result":true}`
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
2017-10-16 21:07:42 +00:00
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
// TestCallResponseOrder tests exactly the problem from
|
|
|
|
// https://github.com/status-im/status-go/issues/372
|
|
|
|
func (s *JailTestSuite) TestSendSyncResponseOrder() {
|
|
|
|
s.StartTestNode()
|
|
|
|
defer s.StopTestNode()
|
|
|
|
|
|
|
|
// `testCommand` is a simple JS function. `calculateGasPrice` makes
|
|
|
|
// an implicit JSON-RPC call via `send` handler (it's a sync call).
|
|
|
|
// `web3.eth.gasPrice` is chosen to call `send` handler under the hood
|
|
|
|
// because it's a simple RPC method and does not require any params.
|
|
|
|
statusJS := baseStatusJSCode + `;
|
|
|
|
_status_catalog.commands["testCommand"] = function (params) {
|
|
|
|
return params.val * params.val;
|
|
|
|
};
|
|
|
|
_status_catalog.commands["calculateGasPrice"] = function (n) {
|
|
|
|
var gasMultiplicator = Math.pow(1.4, n).toFixed(3);
|
|
|
|
var price = 211000000000;
|
|
|
|
try {
|
|
|
|
price = web3.eth.gasPrice;
|
|
|
|
} catch (err) {}
|
|
|
|
|
|
|
|
return price * gasMultiplicator;
|
|
|
|
};
|
|
|
|
`
|
|
|
|
s.Jail.CreateAndInitCell(testChatID, statusJS)
|
|
|
|
|
|
|
|
// Concurrently call `testCommand` and `calculateGasPrice` and do some assertions.
|
|
|
|
// If the code executed in cell's VM is not thread-safe, this test will likely panic.
|
|
|
|
N := 10
|
|
|
|
errCh := make(chan error, N)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < N; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
res := s.Jail.Call(testChatID, `["commands", "testCommand"]`, fmt.Sprintf(`{"val": %d}`, i))
|
|
|
|
if !strings.Contains(string(res), fmt.Sprintf("result\": %d", i*i)) {
|
|
|
|
errCh <- fmt.Errorf("result should be '%d', got %s", i*i, res)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
res := s.Jail.Call(testChatID, `["commands", "calculateGasPrice"]`, fmt.Sprintf(`%d`, i))
|
|
|
|
if strings.Contains(string(res), "error") {
|
|
|
|
errCh <- fmt.Errorf("result should not contain 'error', got %s", res)
|
|
|
|
}
|
|
|
|
}(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
close(errCh)
|
|
|
|
for e := range errCh {
|
|
|
|
s.NoError(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-16 21:07:42 +00:00
|
|
|
func (s *JailTestSuite) TestJailCellsRemovedAfterStop() {
|
|
|
|
const loopLen = 5
|
|
|
|
|
|
|
|
getTestCellID := func(id int) string {
|
|
|
|
return testChatID + strconv.Itoa(id)
|
|
|
|
}
|
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
for i := 0; i < loopLen; i++ {
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Jail.CreateAndInitCell(getTestCellID(i))
|
|
|
|
cell, err := s.Jail.Cell(getTestCellID(i))
|
2017-10-16 21:07:42 +00:00
|
|
|
require.NoError(err)
|
|
|
|
_, err = cell.Run(`
|
|
|
|
var counter = 1;
|
|
|
|
setInterval(function(){
|
|
|
|
counter++;
|
|
|
|
}, 1000);
|
|
|
|
`)
|
2017-10-20 09:06:22 +00:00
|
|
|
require.NoError(err)
|
2017-10-16 21:07:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:36:42 +00:00
|
|
|
s.Jail.Stop()
|
2017-10-16 21:07:42 +00:00
|
|
|
|
|
|
|
for i := 0; i < loopLen; i++ {
|
2017-11-07 17:36:42 +00:00
|
|
|
_, err := s.Jail.Cell(getTestCellID(i))
|
2017-10-16 21:07:42 +00:00
|
|
|
require.Error(err, "Expected cells removing (from Jail) after stop")
|
|
|
|
}
|
|
|
|
}
|