2017-10-11 14:20:51 +00:00
|
|
|
package whisper
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
import (
|
2017-08-07 10:48:14 +00:00
|
|
|
"context"
|
2018-03-28 08:24:55 +00:00
|
|
|
"errors"
|
2017-09-02 17:04:23 +00:00
|
|
|
"testing"
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-03-02 09:25:30 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2018-02-08 12:52:47 +00:00
|
|
|
e2e "github.com/status-im/status-go/t/e2e"
|
|
|
|
. "github.com/status-im/status-go/t/utils"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWhisperTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(WhisperTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperTestSuite struct {
|
2018-03-28 08:24:55 +00:00
|
|
|
e2e.BackendTestSuite
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// TODO(adam): can anyone explain what this test is testing?
|
|
|
|
// I don't see any race condition testing here.
|
2017-05-16 12:09:52 +00:00
|
|
|
func (s *WhisperTestSuite) TestWhisperFilterRace() {
|
2018-03-28 08:24:55 +00:00
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := s.Backend.StatusNode().WhisperService()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
accountManager := account.NewManager(s.Backend.StatusNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(accountManager)
|
|
|
|
|
|
|
|
whisperAPI := whisper.NewPublicWhisperAPI(whisperService)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// account1
|
|
|
|
_, accountKey1, err := accountManager.AddressToDecryptedAccount(TestConfig.Account1.Address, TestConfig.Account1.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-07 10:48:14 +00:00
|
|
|
accountKey1Byte := crypto.FromECDSAPub(&accountKey1.PrivateKey.PublicKey)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-08-07 10:48:14 +00:00
|
|
|
key1ID, err := whisperService.AddKeyPair(accountKey1.PrivateKey)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-07 10:48:14 +00:00
|
|
|
ok := whisperAPI.HasKeyPair(context.Background(), key1ID)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(ok, "identity not injected")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// account2
|
|
|
|
_, accountKey2, err := accountManager.AddressToDecryptedAccount(TestConfig.Account2.Address, TestConfig.Account2.Password)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-07 10:48:14 +00:00
|
|
|
key2ID, err := whisperService.AddKeyPair(accountKey2.PrivateKey)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-08-07 10:48:14 +00:00
|
|
|
ok = whisperAPI.HasKeyPair(context.Background(), key2ID)
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(ok, "identity not injected")
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// race filter addition
|
|
|
|
filterAdded := make(chan struct{})
|
|
|
|
allFiltersAdded := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
counter := 10
|
|
|
|
for range filterAdded {
|
|
|
|
counter--
|
|
|
|
if counter <= 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(allFiltersAdded)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
go func() {
|
|
|
|
// nolint: errcheck
|
2017-08-07 10:48:14 +00:00
|
|
|
whisperAPI.NewMessageFilter(whisper.Criteria{
|
|
|
|
Sig: accountKey1Byte,
|
|
|
|
PrivateKeyID: key2ID,
|
|
|
|
Topics: []whisper.TopicType{
|
|
|
|
{0x4e, 0x03, 0x65, 0x7a},
|
|
|
|
{0x34, 0x60, 0x7c, 0x9b},
|
|
|
|
{0x21, 0x41, 0x7d, 0xf9},
|
2017-05-16 12:09:52 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
filterAdded <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
<-allFiltersAdded
|
|
|
|
}
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
func (s *WhisperTestSuite) TestSelectAccount() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := s.Backend.StatusNode().WhisperService()
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// create an acc
|
|
|
|
address, pubKey, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey), "identity already present in whisper")
|
|
|
|
|
|
|
|
// try selecting with wrong password
|
|
|
|
err = s.Backend.SelectAccount(address, "wrongPassword")
|
|
|
|
s.NotNil(err)
|
|
|
|
|
|
|
|
// select another account, make sure that previous account is wiped out from Whisper cache
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey), "identity already present in whisper")
|
|
|
|
s.NoError(s.Backend.SelectAccount(address, TestConfig.Account1.Password))
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey), "identity not injected into whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperTestSuite) TestLogout() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
whisperService, err := s.Backend.StatusNode().WhisperService()
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// create an account
|
|
|
|
address, pubKey, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identity doesn't exist (yet) in Whisper
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey), "identity already present in whisper")
|
|
|
|
s.NoError(s.Backend.SelectAccount(address, TestConfig.Account1.Password))
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey), "identity not injected into whisper")
|
|
|
|
|
|
|
|
s.NoError(s.Backend.Logout())
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey), "identity not cleared from whisper")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperTestSuite) TestSelectedAccountOnRestart() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
|
|
|
|
// we need to make sure that selected account is injected as identity into Whisper
|
|
|
|
whisperService := s.WhisperService()
|
|
|
|
|
|
|
|
// create test accounts
|
|
|
|
address1, pubKey1, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
address2, pubKey2, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey1), "identity already present in whisper")
|
|
|
|
|
|
|
|
// make sure that no account is selected by default
|
|
|
|
selectedAccount, err := s.Backend.AccountManager().SelectedAccount()
|
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
|
|
|
|
s.Nil(selectedAccount)
|
|
|
|
|
|
|
|
// select account
|
|
|
|
err = s.Backend.SelectAccount(address1, "wrongPassword")
|
|
|
|
expectedErr := errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given passphrase")
|
|
|
|
s.EqualError(expectedErr, err.Error())
|
|
|
|
|
|
|
|
s.NoError(s.Backend.SelectAccount(address1, TestConfig.Account1.Password))
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey1), "identity not injected into whisper")
|
|
|
|
|
|
|
|
// select another account, make sure that previous account is wiped out from Whisper cache
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey2), "identity already present in whisper")
|
|
|
|
s.NoError(s.Backend.SelectAccount(address2, TestConfig.Account1.Password))
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey1), "identity should be removed, but it is still present in whisper")
|
|
|
|
|
|
|
|
// stop node (and all of its sub-protocols)
|
2018-04-16 12:36:09 +00:00
|
|
|
nodeConfig := s.Backend.StatusNode().Config()
|
|
|
|
s.NotNil(nodeConfig)
|
2018-03-28 08:24:55 +00:00
|
|
|
preservedNodeConfig := *nodeConfig
|
|
|
|
s.NoError(s.Backend.StopNode())
|
|
|
|
|
|
|
|
// resume node
|
|
|
|
s.NoError(s.Backend.StartNode(&preservedNodeConfig))
|
|
|
|
|
|
|
|
// re-check selected account (account2 MUST be selected)
|
|
|
|
selectedAccount, err = s.Backend.AccountManager().SelectedAccount()
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(selectedAccount)
|
|
|
|
s.Equal(selectedAccount.Address.Hex(), address2, "incorrect address selected")
|
|
|
|
|
|
|
|
// make sure that Whisper gets identity re-injected
|
|
|
|
whisperService = s.WhisperService()
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey1), "identity should not be present, but it is still present in whisper")
|
|
|
|
|
|
|
|
// now restart node using RestartNode() method, and make sure that account is still available
|
|
|
|
s.RestartTestNode()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
whisperService = s.WhisperService()
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey1), "identity should not be present, but it is still present in whisper")
|
|
|
|
|
|
|
|
// now logout, and make sure that on restart no account is selected (i.e. logout works properly)
|
2018-04-20 15:39:53 +00:00
|
|
|
s.NoError(s.Backend.Logout())
|
2018-03-28 08:24:55 +00:00
|
|
|
s.RestartTestNode()
|
|
|
|
whisperService = s.WhisperService()
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey1), "identity should not be present, but it is still present in whisper")
|
|
|
|
|
|
|
|
selectedAccount, err = s.Backend.AccountManager().SelectedAccount()
|
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error())
|
|
|
|
s.Nil(selectedAccount)
|
|
|
|
}
|