2017-05-25 11:23:01 +00:00
|
|
|
package api_test
|
|
|
|
|
|
|
|
import (
|
2017-05-27 20:26:07 +00:00
|
|
|
"io/ioutil"
|
2017-05-25 13:14:52 +00:00
|
|
|
"math/rand"
|
2017-05-27 20:26:07 +00:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2017-05-25 11:23:01 +00:00
|
|
|
"testing"
|
2017-05-25 12:34:13 +00:00
|
|
|
"time"
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/e2e"
|
2017-05-25 11:23:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/api"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-05-25 11:23:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-10-16 21:07:42 +00:00
|
|
|
. "github.com/status-im/status-go/testing"
|
2017-05-25 11:23:01 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
2017-10-16 21:07:42 +00:00
|
|
|
const (
|
|
|
|
testChatID = "testChat"
|
|
|
|
)
|
|
|
|
|
2017-05-25 11:23:01 +00:00
|
|
|
func TestAPI(t *testing.T) {
|
|
|
|
suite.Run(t, new(APITestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type APITestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
api *api.StatusAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *APITestSuite) SetupTest() {
|
2017-10-11 14:20:51 +00:00
|
|
|
s.api = api.NewStatusAPI()
|
|
|
|
s.NotNil(s.api)
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-05-27 20:26:07 +00:00
|
|
|
|
|
|
|
func (s *APITestSuite) TestCHTUpdate() {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "cht-updates")
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-10-20 09:06:22 +00:00
|
|
|
defer os.RemoveAll(tmpDir) //nolint: errcheck
|
2017-05-27 20:26:07 +00:00
|
|
|
|
|
|
|
configJSON := `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(params.RopstenNetworkID) + `,
|
|
|
|
"DataDir": "` + tmpDir + `",
|
|
|
|
"LogLevel": "INFO",
|
2017-08-25 09:56:54 +00:00
|
|
|
"RPCEnabled": true
|
2017-05-27 20:26:07 +00:00
|
|
|
}`
|
2017-10-20 09:06:22 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
_, err = params.LoadNodeConfig(configJSON)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-04 16:14:17 +00:00
|
|
|
// TODO(tiabc): Test that CHT is really updated.
|
2017-05-27 20:26:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
func (s *APITestSuite) TestRaceConditions() {
|
|
|
|
cnt := 25
|
|
|
|
progress := make(chan struct{}, cnt)
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
nodeConfig1, err := e2e.MakeTestNodeConfig(params.RopstenNetworkID)
|
|
|
|
s.NoError(err)
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
nodeConfig2, err := e2e.MakeTestNodeConfig(params.RinkebyNetworkID)
|
|
|
|
s.NoError(err)
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
nodeConfigs := []*params.NodeConfig{nodeConfig1, nodeConfig2}
|
2017-05-25 11:23:01 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
var funcsToTest = []func(*params.NodeConfig){
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("StartNodeAsync()")
|
|
|
|
_, err := s.api.StartNodeAsync(config)
|
|
|
|
s.T().Logf("StartNodeAsync() for network: %d, error: %v", config.NetworkID, err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("StopNodeAsync()")
|
|
|
|
_, err := s.api.StopNodeAsync()
|
|
|
|
s.T().Logf("StopNodeAsync(), error: %v", err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
|
|
|
func(config *params.NodeConfig) {
|
|
|
|
log.Info("RestartNodeAsync()")
|
|
|
|
_, err := s.api.RestartNodeAsync()
|
|
|
|
s.T().Logf("RestartNodeAsync(), error: %v", err)
|
|
|
|
progress <- struct{}{}
|
|
|
|
},
|
2017-10-11 14:20:51 +00:00
|
|
|
// TODO(adam): quarantined until it uses a different datadir
|
|
|
|
// as otherwise it wipes out cached blockchain data.
|
|
|
|
// func(config *params.NodeConfig) {
|
|
|
|
// log.Info("ResetChainDataAsync()")
|
|
|
|
// _, err := s.api.ResetChainDataAsync()
|
|
|
|
// s.T().Logf("ResetChainDataAsync(), error: %v", err)
|
|
|
|
// progress <- struct{}{}
|
|
|
|
// },
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
|
|
|
// increase StartNode()/StopNode() population
|
|
|
|
for i := 0; i < 5; i++ {
|
|
|
|
funcsToTest = append(funcsToTest, funcsToTest[0], funcsToTest[1])
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
for i := 0; i < cnt; i++ {
|
|
|
|
randConfig := nodeConfigs[rnd.Intn(len(nodeConfigs))]
|
|
|
|
randFunc := funcsToTest[rnd.Intn(len(funcsToTest))]
|
2017-05-25 12:34:13 +00:00
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
if rnd.Intn(100) > 75 { // introduce random delays
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
go randFunc(randConfig)
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for range progress {
|
2017-05-25 13:14:52 +00:00
|
|
|
cnt -= 1
|
|
|
|
if cnt <= 0 {
|
2017-05-25 11:23:01 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second) // so that we see some logs
|
2017-10-20 09:06:22 +00:00
|
|
|
// just in case we have a node running
|
|
|
|
s.api.StopNode() //nolint: errcheck
|
2017-05-25 11:23:01 +00:00
|
|
|
}
|
2017-10-16 21:07:42 +00:00
|
|
|
|
|
|
|
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)
|
2017-10-20 09:06:22 +00:00
|
|
|
defer s.api.StopNode() //nolint: errcheck
|
2017-10-16 21:07:42 +00:00
|
|
|
|
|
|
|
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++ {
|
2017-10-20 09:06:22 +00:00
|
|
|
_, e := s.api.JailManager().NewCell(getChatId(i))
|
|
|
|
require.NoError(e)
|
2017-10-16 21:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.api.SelectAccount(address2, TestConfig.Account2.Password)
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < itersCount; i++ {
|
2017-10-20 09:06:22 +00:00
|
|
|
_, e := s.api.JailManager().Cell(getChatId(i))
|
|
|
|
require.Error(e)
|
2017-10-16 21:07:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2017-10-20 09:06:22 +00:00
|
|
|
defer s.api.StopNode() //nolint: errcheck
|
2017-10-16 21:07:42 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|