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
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-01-09 08:47:06 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
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"
|
2019-12-09 10:36:14 +00:00
|
|
|
"github.com/status-im/status-go/whisper/v6"
|
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
|
|
|
}
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
func buildLoginParams(mainAccountAddress, chatAddress, password string) account.LoginParams {
|
|
|
|
return account.LoginParams{
|
|
|
|
ChatAddress: common.HexToAddress(chatAddress),
|
|
|
|
Password: password,
|
|
|
|
MainAccount: common.HexToAddress(mainAccountAddress),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
accountManager := s.Backend.AccountManager()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(accountManager)
|
|
|
|
|
|
|
|
whisperAPI := whisper.NewPublicWhisperAPI(whisperService)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
// account1
|
2019-01-18 09:01:14 +00:00
|
|
|
_, accountKey1, err := accountManager.AddressToDecryptedAccount(TestConfig.Account1.ChatAddress, 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
|
2019-01-18 09:01:14 +00:00
|
|
|
_, accountKey2, err := accountManager.AddressToDecryptedAccount(TestConfig.Account2.ChatAddress, 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)
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// create account 1
|
|
|
|
accountInfo1, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// create account 2
|
|
|
|
accountInfo2, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account2.Password)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identities are not injected yet
|
|
|
|
s.False(whisperService.HasKeyPair(accountInfo1.ChatPubKey), "identity already present in whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(accountInfo2.ChatPubKey), "identity already present in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// try selecting with wrong password
|
2019-07-26 14:45:10 +00:00
|
|
|
err = s.Backend.SelectAccount(buildLoginParams(accountInfo1.WalletAddress, accountInfo1.ChatAddress, "wrongpassword"))
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NotNil(err)
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// select account 1
|
2019-07-26 14:45:10 +00:00
|
|
|
err = s.Backend.SelectAccount(buildLoginParams(accountInfo1.WalletAddress, accountInfo1.ChatAddress, TestConfig.Account1.Password))
|
2019-01-18 09:01:14 +00:00
|
|
|
s.NoError(err)
|
|
|
|
s.True(whisperService.HasKeyPair(accountInfo1.ChatPubKey), "identity not injected in whisper")
|
|
|
|
|
|
|
|
// select account 2, make sure that previous account is wiped out from Whisper cache
|
|
|
|
s.False(whisperService.HasKeyPair(accountInfo2.ChatPubKey), "identity already present in whisper")
|
2019-07-26 14:45:10 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(buildLoginParams(accountInfo2.WalletAddress, accountInfo2.ChatAddress, TestConfig.Account2.Password)))
|
2019-01-18 09:01:14 +00:00
|
|
|
s.True(whisperService.HasKeyPair(accountInfo2.ChatPubKey), "identity not injected into whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identity doesn't exist (yet) in Whisper
|
2019-01-18 09:01:14 +00:00
|
|
|
s.False(whisperService.HasKeyPair(accountInfo.ChatPubKey), "identity already present in whisper")
|
2019-07-26 14:45:10 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(buildLoginParams(accountInfo.WalletAddress, accountInfo.ChatAddress, TestConfig.Account1.Password)))
|
2019-01-18 09:01:14 +00:00
|
|
|
s.True(whisperService.HasKeyPair(accountInfo.ChatPubKey), "identity not injected into whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
s.NoError(s.Backend.Logout())
|
2019-01-18 09:01:14 +00:00
|
|
|
s.False(whisperService.HasKeyPair(accountInfo.ChatPubKey), "identity not cleared from whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo1, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo2, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account2.Password)
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// make sure that identity is not (yet injected)
|
2019-01-18 09:01:14 +00:00
|
|
|
s.False(whisperService.HasKeyPair(accountInfo1.ChatPubKey), "identity already present in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
// make sure that no wallet account is selected by default
|
2019-07-26 14:45:10 +00:00
|
|
|
selectedWalletAccount, err := s.Backend.AccountManager().MainAccountAddress()
|
2018-03-28 08:24:55 +00:00
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
|
2019-07-26 14:45:10 +00:00
|
|
|
s.Equal(common.Address{}, selectedWalletAccount)
|
2018-03-28 08:24:55 +00:00
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
// make sure that no chat account is selected by default
|
|
|
|
selectedChatAccount, err := s.Backend.AccountManager().SelectedChatAccount()
|
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error(), "account selected, but should not be")
|
|
|
|
s.Nil(selectedChatAccount)
|
|
|
|
|
|
|
|
// select account with wrong password
|
2019-07-26 14:45:10 +00:00
|
|
|
err = s.Backend.SelectAccount(buildLoginParams(accountInfo1.WalletAddress, accountInfo1.ChatAddress, "wrongPassword"))
|
2019-10-04 15:21:24 +00:00
|
|
|
expectedErr := errors.New("cannot retrieve a valid key for a given account: could not decrypt key with given password")
|
2018-03-28 08:24:55 +00:00
|
|
|
s.EqualError(expectedErr, err.Error())
|
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
// select account with right password
|
2019-07-26 14:45:10 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(buildLoginParams(accountInfo1.WalletAddress, accountInfo1.ChatAddress, TestConfig.Account1.Password)))
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedChatAccount1, err := s.Backend.AccountManager().SelectedChatAccount()
|
|
|
|
s.NoError(err)
|
|
|
|
selectedChatPubKey1 := hexutil.Encode(crypto.FromECDSAPub(&selectedChatAccount1.AccountKey.PrivateKey.PublicKey))
|
2019-01-18 09:01:14 +00:00
|
|
|
s.Equal(selectedChatPubKey1, accountInfo1.ChatPubKey)
|
2019-01-09 08:47:06 +00:00
|
|
|
s.True(whisperService.HasKeyPair(selectedChatPubKey1), "identity not injected into whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// select another account, make sure that previous account is wiped out from Whisper cache
|
2019-01-18 09:01:14 +00:00
|
|
|
s.False(whisperService.HasKeyPair(accountInfo2.ChatPubKey), "identity already present in whisper")
|
2019-07-26 14:45:10 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(buildLoginParams(accountInfo2.WalletAddress, accountInfo2.ChatAddress, TestConfig.Account2.Password)))
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedChatAccount2, err := s.Backend.AccountManager().SelectedChatAccount()
|
|
|
|
s.NoError(err)
|
|
|
|
selectedChatPubKey2 := hexutil.Encode(crypto.FromECDSAPub(&selectedChatAccount2.AccountKey.PrivateKey.PublicKey))
|
2019-01-18 09:01:14 +00:00
|
|
|
s.Equal(selectedChatPubKey2, accountInfo2.ChatPubKey)
|
2019-01-09 08:47:06 +00:00
|
|
|
s.True(whisperService.HasKeyPair(selectedChatPubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(selectedChatPubKey1), "identity should be removed, but it is still present in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// 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
|
2018-09-13 16:31:29 +00:00
|
|
|
s.Require().NoError(s.Backend.StartNode(&preservedNodeConfig))
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// re-check selected account (account2 MUST be selected)
|
2019-07-26 14:45:10 +00:00
|
|
|
selectedWalletAccount, err = s.Backend.AccountManager().MainAccountAddress()
|
2018-03-28 08:24:55 +00:00
|
|
|
s.NoError(err)
|
2019-01-09 08:47:06 +00:00
|
|
|
s.NotNil(selectedWalletAccount)
|
2019-07-26 14:45:10 +00:00
|
|
|
s.Equal(selectedWalletAccount.String(), accountInfo2.WalletAddress, "incorrect wallet address selected")
|
2019-01-18 09:01:14 +00:00
|
|
|
selectedChatAccount, err = s.Backend.AccountManager().SelectedChatAccount()
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(selectedChatAccount)
|
|
|
|
s.Equal(selectedChatAccount.Address.Hex(), accountInfo2.ChatAddress, "incorrect chat address selected")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// make sure that Whisper gets identity re-injected
|
|
|
|
whisperService = s.WhisperService()
|
2019-01-09 08:47:06 +00:00
|
|
|
s.True(whisperService.HasKeyPair(selectedChatPubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(selectedChatPubKey1), "identity should not be present, but it is still present in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// now restart node using RestartNode() method, and make sure that account is still available
|
|
|
|
s.RestartTestNode()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
whisperService = s.WhisperService()
|
2019-01-09 08:47:06 +00:00
|
|
|
s.True(whisperService.HasKeyPair(selectedChatPubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(selectedChatPubKey1), "identity should not be present, but it is still present in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
|
|
|
|
// 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()
|
2019-01-09 08:47:06 +00:00
|
|
|
s.False(whisperService.HasKeyPair(selectedChatPubKey2), "identity not injected into whisper")
|
|
|
|
s.False(whisperService.HasKeyPair(selectedChatPubKey1), "identity should not be present, but it is still present in whisper")
|
|
|
|
|
2019-07-26 14:45:10 +00:00
|
|
|
selectedWalletAccount, err = s.Backend.AccountManager().MainAccountAddress()
|
2019-01-09 08:47:06 +00:00
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error())
|
2019-07-26 14:45:10 +00:00
|
|
|
s.Equal(common.Address{}, selectedWalletAccount)
|
2018-03-28 08:24:55 +00:00
|
|
|
|
2019-01-09 08:47:06 +00:00
|
|
|
selectedChatAccount, err = s.Backend.AccountManager().SelectedChatAccount()
|
2018-03-28 08:24:55 +00:00
|
|
|
s.EqualError(account.ErrNoAccountSelected, err.Error())
|
2019-01-09 08:47:06 +00:00
|
|
|
s.Nil(selectedChatAccount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperTestSuite) TestSelectedChatKeyIsUsedInWhisper() {
|
|
|
|
s.StartTestBackend()
|
|
|
|
defer s.StopTestBackend()
|
|
|
|
|
|
|
|
whisperService, err := s.Backend.StatusNode().WhisperService()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// create an account
|
2019-01-18 09:01:14 +00:00
|
|
|
accountInfo, _, err := s.Backend.AccountManager().CreateAccount(TestConfig.Account1.Password)
|
2019-01-09 08:47:06 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// select account
|
2019-07-26 14:45:10 +00:00
|
|
|
s.NoError(s.Backend.SelectAccount(buildLoginParams(accountInfo.WalletAddress, accountInfo.ChatAddress, TestConfig.Account1.Password)))
|
2019-01-09 08:47:06 +00:00
|
|
|
|
|
|
|
// Get the chat account
|
|
|
|
selectedChatAccount, err := s.Backend.AccountManager().SelectedChatAccount()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// chat key should be injected in whisper
|
|
|
|
selectedChatPubKey := hexutil.Encode(crypto.FromECDSAPub(&selectedChatAccount.AccountKey.PrivateKey.PublicKey))
|
|
|
|
s.True(whisperService.HasKeyPair(selectedChatPubKey), "identity not injected in whisper")
|
2018-03-28 08:24:55 +00:00
|
|
|
}
|