Jail.RemoveCells before logout and switching account (#382)

Called jail.Stop upon SwitchAccount and Logout to ensure all jail cells have been terminated.
This commit is contained in:
Oleg Raev 2017-10-17 04:07:42 +07:00 committed by Ivan Tomilov
parent 16741f9517
commit 240149786f
4 changed files with 110 additions and 0 deletions

View File

@ -12,9 +12,14 @@ import (
"github.com/status-im/status-go/geth/api"
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/params"
. "github.com/status-im/status-go/testing"
"github.com/stretchr/testify/suite"
)
const (
testChatID = "testChat"
)
func TestAPI(t *testing.T) {
suite.Run(t, new(APITestSuite))
}
@ -118,3 +123,70 @@ func (s *APITestSuite) TestRaceConditions() {
time.Sleep(2 * time.Second) // so that we see some logs
s.api.StopNode() // just in case we have a node running
}
func (s *APITestSuite) TestCellsRemovedAfterSwitchAccount() {
const itersCount = 5
var (
require = s.Require()
getChatId = func(id int) string {
return testChatID + strconv.Itoa(id)
}
)
config, err := e2e.MakeTestNodeConfig(params.RopstenNetworkID)
require.NoError(err)
err = s.api.StartNode(config)
require.NoError(err)
defer s.api.StopNode()
address1, _, _, err := s.api.AccountManager().CreateAccount(TestConfig.Account1.Password)
require.NoError(err)
address2, _, _, err := s.api.AccountManager().CreateAccount(TestConfig.Account2.Password)
require.NoError(err)
err = s.api.SelectAccount(address1, TestConfig.Account1.Password)
require.NoError(err)
for i := 0; i < itersCount; i++ {
_, err := s.api.JailManager().NewCell(getChatId(i))
require.NoError(err)
}
err = s.api.SelectAccount(address2, TestConfig.Account2.Password)
require.NoError(err)
for i := 0; i < itersCount; i++ {
_, err := s.api.JailManager().Cell(getChatId(i))
require.Error(err)
}
}
// TestLogoutRemovesCells we want be sure that
// cells will be removed after the API call "Logout"
func (s *APITestSuite) TestLogoutRemovesCells() {
var (
err error
require = s.Require()
)
config, err := e2e.MakeTestNodeConfig(params.RopstenNetworkID)
require.NoError(err)
err = s.api.StartNode(config)
require.NoError(err)
defer s.api.StopNode()
address1, _, _, err := s.api.AccountManager().CreateAccount(TestConfig.Account1.Password)
require.NoError(err)
err = s.api.SelectAccount(address1, TestConfig.Account1.Password)
require.NoError(err)
s.api.JailManager().Parse(testChatID, ``)
err = s.api.Logout()
require.NoError(err)
_, err = s.api.JailManager().Cell(testChatID)
require.Error(err, "Expected that cells was removed")
}

View File

@ -3,6 +3,7 @@ package jail
import (
"encoding/json"
"errors"
"strconv"
"testing"
"time"
@ -157,3 +158,31 @@ func (s *JailTestSuite) TestEventSignal() {
expectedResponse := `{"jsonrpc":"2.0","result":true}`
s.Equal(expectedResponse, response)
}
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++ {
s.jail.Parse(getTestCellID(i), "")
cell, err := s.jail.Cell(getTestCellID(i))
require.NoError(err)
_, err = cell.Run(`
var counter = 1;
setInterval(function(){
counter++;
}, 1000);
`)
}
s.jail.Stop()
for i := 0; i < loopLen; i++ {
_, err := s.jail.Cell(getTestCellID(i))
require.Error(err, "Expected cells removing (from Jail) after stop")
}
}

View File

@ -146,11 +146,15 @@ func (api *StatusAPI) VerifyAccountPassword(keyStoreDir, address, password strin
// using provided password. Once verification is done, decrypted key is injected into Whisper (as a single identity,
// all previous identities are removed).
func (api *StatusAPI) SelectAccount(address, password string) error {
// FIXME(oleg-raev): This method doesn't make stop, it rather resets its cells to an initial state
// and should be properly renamed, for example: ResetCells
api.b.jailManager.Stop()
return api.b.AccountManager().SelectAccount(address, password)
}
// Logout clears whisper identities
func (api *StatusAPI) Logout() error {
api.b.jailManager.Stop()
return api.b.AccountManager().Logout()
}

View File

@ -3,6 +3,7 @@ package jail_test
import (
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/robertkrimen/otto"
@ -19,6 +20,10 @@ var (
baseStatusJSCode = string(static.MustAsset("testdata/jail/status.js"))
)
func TestCellTestSuite(t *testing.T) {
suite.Run(t, new(CellTestSuite))
}
type CellTestSuite struct {
suite.Suite
jail *jail.Jail