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)
|
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
response := s.Jail.CreateAndInitCell(testChatID)
|
|
|
|
s.Equal(jail.EmptyResponse, response)
|
|
|
|
|
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)
|
2018-01-09 16:53:33 +00:00
|
|
|
s.Equal(jail.EmptyResponse, response)
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// 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}`)
|
2018-01-09 16:53:33 +00:00
|
|
|
expectedResponse := `{"result": 144}`
|
2017-10-11 14:20:51 +00:00
|
|
|
s.Equal(expectedResponse, response)
|
|
|
|
}
|
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
func (s *JailTestSuite) TestCreateAndInitCell() {
|
|
|
|
// If no custom JS provided -- the default response is returned
|
|
|
|
response := s.Jail.CreateAndInitCell("newChat1")
|
|
|
|
expectedResponse := jail.EmptyResponse
|
|
|
|
s.Equal(expectedResponse, response)
|
2017-11-07 17:36:42 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// If any custom JS provided -- the result of the last op is returned
|
|
|
|
response = s.Jail.CreateAndInitCell("newChat2", "var a = 2", "a")
|
|
|
|
expectedResponse = `{"result": 2}`
|
|
|
|
s.Equal(expectedResponse, response)
|
2017-11-07 17:36:42 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// Reinitialization preserves the JS environment, so the 'test' variable exists
|
|
|
|
// even though we didn't initialize it here (in the second room).
|
|
|
|
response = s.Jail.CreateAndInitCell("newChat2", "a")
|
|
|
|
expectedResponse = `{"result": 2}`
|
|
|
|
s.Equal(expectedResponse, response)
|
2017-11-07 17:36:42 +00:00
|
|
|
|
2018-01-09 16:53:33 +00:00
|
|
|
// But this variable doesn't leak into other rooms.
|
|
|
|
response = s.Jail.CreateAndInitCell("newChat1", "a")
|
|
|
|
expectedResponse = `{"error":"ReferenceError: 'a' is not defined"}`
|
2017-10-11 14:20:51 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|