2017-08-04 16:14:17 +00:00
|
|
|
package jail_test
|
|
|
|
|
|
|
|
import (
|
2017-09-08 11:55:17 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2017-10-16 21:07:42 +00:00
|
|
|
"testing"
|
2017-08-04 16:14:17 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/robertkrimen/otto"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/jail"
|
|
|
|
"github.com/status-im/status-go/static"
|
|
|
|
"github.com/stretchr/testify/suite"
|
2017-08-04 16:14:17 +00:00
|
|
|
)
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
const (
|
|
|
|
testChatID = "testChat"
|
|
|
|
)
|
2017-09-01 20:17:34 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
var (
|
|
|
|
baseStatusJSCode = string(static.MustAsset("testdata/jail/status.js"))
|
|
|
|
)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-10-16 21:07:42 +00:00
|
|
|
func TestCellTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(CellTestSuite))
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
type CellTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
jail *jail.Jail
|
|
|
|
}
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) SetupTest() {
|
|
|
|
s.jail = jail.New(nil)
|
|
|
|
s.NotNil(s.jail)
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailTimeout() {
|
2017-08-04 16:14:17 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := s.jail.NewCell(testChatID)
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
// Attempt to run a timeout string against a Cell.
|
2017-09-01 20:17:34 +00:00
|
|
|
_, err = cell.Run(`
|
|
|
|
var timerCounts = 0;
|
2017-10-11 14:20:51 +00:00
|
|
|
setTimeout(function(n){
|
2017-09-01 20:17:34 +00:00
|
|
|
if (Date.now() - n < 50) {
|
|
|
|
throw new Error("Timed out");
|
|
|
|
}
|
|
|
|
|
|
|
|
timerCounts++;
|
|
|
|
}, 50, Date.now());
|
|
|
|
`)
|
|
|
|
require.NoError(err)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
// wait at least 10x longer to decrease probability
|
|
|
|
// of false negatives as we using real clock here
|
|
|
|
time.Sleep(300 * time.Millisecond)
|
2017-08-04 16:14:17 +00:00
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
value, err := cell.Get("timerCounts")
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
2017-09-01 20:17:34 +00:00
|
|
|
require.True(value.IsNumber())
|
|
|
|
require.Equal("1", value.String())
|
2017-08-04 16:14:17 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailLoopInCall() {
|
2017-08-04 16:14:17 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
// load Status JS and add test command to it
|
|
|
|
s.jail.BaseJS(baseStatusJSCode)
|
|
|
|
s.jail.Parse(testChatID, ``)
|
|
|
|
|
2017-09-02 17:04:23 +00:00
|
|
|
cell, err := s.jail.Cell(testChatID)
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-08-04 16:14:17 +00:00
|
|
|
|
|
|
|
items := make(chan string)
|
|
|
|
|
|
|
|
err = cell.Set("__captureResponse", func(val string) otto.Value {
|
|
|
|
go func() { items <- val }()
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
function callRunner(namespace){
|
|
|
|
console.log("Initiating callRunner for: ", namespace)
|
|
|
|
return setTimeout(function(){
|
|
|
|
__captureResponse(namespace);
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2017-09-01 20:17:34 +00:00
|
|
|
_, err = cell.Call("callRunner", nil, "softball")
|
2017-08-04 16:14:17 +00:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case received := <-items:
|
|
|
|
require.Equal(received, "softball")
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
require.Fail("Failed to received event response")
|
|
|
|
}
|
|
|
|
}
|
2017-09-08 11:55:17 +00:00
|
|
|
|
|
|
|
// TestJailLoopRace tests multiple setTimeout callbacks,
|
|
|
|
// supposed to be run with '-race' flag.
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailLoopRace() {
|
2017-09-08 11:55:17 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
cell, err := s.jail.NewCell(testChatID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-09-08 11:55:17 +00:00
|
|
|
|
|
|
|
items := make(chan struct{})
|
|
|
|
|
|
|
|
err = cell.Set("__captureResponse", func() otto.Value {
|
|
|
|
go func() { items <- struct{}{} }()
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
function callRunner(){
|
|
|
|
return setTimeout(function(){
|
|
|
|
__captureResponse();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
_, err = cell.Call("callRunner", nil)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
select {
|
|
|
|
case <-items:
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
require.Fail("test timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailFetchPromise() {
|
2017-09-08 11:55:17 +00:00
|
|
|
body := `{"key": "value"}`
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2017-10-20 09:06:22 +00:00
|
|
|
w.Write([]byte(body)) //nolint: errcheck
|
2017-09-08 11:55:17 +00:00
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
cell, err := s.jail.NewCell(testChatID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-09-08 11:55:17 +00:00
|
|
|
|
|
|
|
dataCh := make(chan otto.Value, 1)
|
|
|
|
errCh := make(chan otto.Value, 1)
|
|
|
|
|
|
|
|
err = cell.Set("__captureSuccess", func(res otto.Value) { dataCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
err = cell.Set("__captureError", func(res otto.Value) { errCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// run JS code for fetching valid URL
|
|
|
|
_, err = cell.Run(`fetch('` + server.URL + `').then(function(r) {
|
|
|
|
return r.text()
|
|
|
|
}).then(function(data) {
|
|
|
|
__captureSuccess(data)
|
|
|
|
}).catch(function (e) {
|
|
|
|
__captureError(e)
|
|
|
|
})`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case data := <-dataCh:
|
|
|
|
require.True(data.IsString())
|
|
|
|
require.Equal(body, data.String())
|
|
|
|
case err := <-errCh:
|
|
|
|
require.Fail("request failed", err)
|
|
|
|
case <-time.After(1 * time.Second):
|
|
|
|
require.Fail("test timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailFetchCatch() {
|
2017-09-08 11:55:17 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
cell, err := s.jail.NewCell(testChatID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-09-08 11:55:17 +00:00
|
|
|
|
|
|
|
dataCh := make(chan otto.Value, 1)
|
|
|
|
errCh := make(chan otto.Value, 1)
|
|
|
|
|
|
|
|
err = cell.Set("__captureSuccess", func(res otto.Value) { dataCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
err = cell.Set("__captureError", func(res otto.Value) { errCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// run JS code for fetching invalid URL
|
|
|
|
_, err = cell.Run(`fetch('http://👽/nonexistent').then(function(r) {
|
|
|
|
return r.text()
|
|
|
|
}).then(function(data) {
|
|
|
|
__captureSuccess(data)
|
|
|
|
}).catch(function (e) {
|
|
|
|
__captureError(e)
|
|
|
|
})`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case data := <-dataCh:
|
|
|
|
require.Fail("request should have failed, but returned", data)
|
|
|
|
case e := <-errCh:
|
|
|
|
require.True(e.IsObject())
|
|
|
|
name, err := e.Object().Get("name")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal("Error", name.String())
|
|
|
|
_, err = e.Object().Get("message")
|
|
|
|
require.NoError(err)
|
2017-10-20 09:06:22 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2017-09-08 11:55:17 +00:00
|
|
|
require.Fail("test timed out")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestJailFetchRace tests multiple fetch callbacks,
|
|
|
|
// supposed to be run with '-race' flag.
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailFetchRace() {
|
2017-09-08 11:55:17 +00:00
|
|
|
body := `{"key": "value"}`
|
|
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
2017-10-20 09:06:22 +00:00
|
|
|
w.Write([]byte(body)) //nolint: errcheck
|
2017-09-08 11:55:17 +00:00
|
|
|
}))
|
|
|
|
defer server.Close()
|
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
cell, err := s.jail.NewCell(testChatID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
2017-10-06 16:52:26 +00:00
|
|
|
defer cell.Stop()
|
2017-09-08 11:55:17 +00:00
|
|
|
|
|
|
|
dataCh := make(chan otto.Value, 1)
|
|
|
|
errCh := make(chan otto.Value, 1)
|
|
|
|
|
|
|
|
err = cell.Set("__captureSuccess", func(res otto.Value) { dataCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
err = cell.Set("__captureError", func(res otto.Value) { errCh <- res })
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// run JS code for fetching valid URL
|
|
|
|
_, err = cell.Run(`fetch('` + server.URL + `').then(function(r) {
|
|
|
|
return r.text()
|
|
|
|
}).then(function(data) {
|
|
|
|
__captureSuccess(data)
|
|
|
|
}).catch(function (e) {
|
|
|
|
__captureError(e)
|
|
|
|
})`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// run JS code for fetching invalid URL
|
|
|
|
_, err = cell.Run(`fetch('http://👽/nonexistent').then(function(r) {
|
|
|
|
return r.text()
|
|
|
|
}).then(function(data) {
|
|
|
|
__captureSuccess(data)
|
|
|
|
}).catch(function (e) {
|
|
|
|
__captureError(e)
|
|
|
|
})`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case data := <-dataCh:
|
|
|
|
require.True(data.IsString())
|
|
|
|
require.Equal(body, data.String())
|
|
|
|
case e := <-errCh:
|
|
|
|
require.True(e.IsObject())
|
|
|
|
name, err := e.Object().Get("name")
|
|
|
|
require.NoError(err)
|
|
|
|
require.Equal("Error", name.String())
|
|
|
|
_, err = e.Object().Get("message")
|
|
|
|
require.NoError(err)
|
2017-10-20 09:06:22 +00:00
|
|
|
case <-time.After(3 * time.Second):
|
2017-09-08 11:55:17 +00:00
|
|
|
require.Fail("test timed out")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 16:52:26 +00:00
|
|
|
|
|
|
|
// TestJailLoopCancel tests that cell.Stop() really cancels event
|
|
|
|
// loop and pending tasks.
|
2017-10-11 14:20:51 +00:00
|
|
|
func (s *CellTestSuite) TestJailLoopCancel() {
|
2017-10-06 16:52:26 +00:00
|
|
|
require := s.Require()
|
|
|
|
|
|
|
|
// load Status JS and add test command to it
|
|
|
|
s.jail.BaseJS(baseStatusJSCode)
|
|
|
|
s.jail.Parse(testChatID, ``)
|
|
|
|
|
|
|
|
cell, err := s.jail.Cell(testChatID)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(cell)
|
|
|
|
|
|
|
|
var count int
|
2017-10-20 09:06:22 +00:00
|
|
|
err = cell.Set("__captureResponse", func(val string) otto.Value { //nolint: unparam
|
2017-10-06 16:52:26 +00:00
|
|
|
count++
|
|
|
|
return otto.UndefinedValue()
|
|
|
|
})
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
_, err = cell.Run(`
|
|
|
|
function callRunner(val, delay){
|
|
|
|
return setTimeout(function(){
|
|
|
|
__captureResponse(val);
|
|
|
|
}, delay);
|
|
|
|
}
|
|
|
|
`)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// Run 5 timeout tasks to be executed in: 1, 2, 3, 4 and 5 secs
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
_, err = cell.Call("callRunner", nil, "value", i*1000)
|
|
|
|
require.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 1.5 second (so only one task executed) so far
|
|
|
|
// and stop the cell (event loop should die)
|
|
|
|
time.Sleep(1500 * time.Millisecond)
|
|
|
|
cell.Stop()
|
|
|
|
|
|
|
|
// check that only 1 task has increased counter
|
|
|
|
require.Equal(1, count)
|
|
|
|
|
|
|
|
// wait 2 seconds more (so at least two more tasks would
|
|
|
|
// have been executed if event loop is still running)
|
|
|
|
<-time.After(2 * time.Second)
|
|
|
|
|
|
|
|
// check that counter hasn't increased
|
|
|
|
require.Equal(1, count)
|
|
|
|
}
|