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"
|
2017-09-02 17:04:23 +00:00
|
|
|
"testing"
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/e2e"
|
2017-09-26 13:44:26 +00:00
|
|
|
"github.com/status-im/status-go/geth/account"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-10-11 14:20:51 +00:00
|
|
|
. "github.com/status-im/status-go/testing"
|
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 {
|
2017-10-11 14:20:51 +00:00
|
|
|
e2e.NodeManagerTestSuite
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *WhisperTestSuite) SetupTest() {
|
|
|
|
s.NodeManager = node.NewNodeManager()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NotNil(s.NodeManager)
|
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() {
|
2017-09-02 17:04:23 +00:00
|
|
|
s.StartTestNode(params.RinkebyNetworkID)
|
2017-05-16 12:09:52 +00:00
|
|
|
defer s.StopTestNode()
|
|
|
|
|
|
|
|
whisperService, err := s.NodeManager.WhisperService()
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-26 13:44:26 +00:00
|
|
|
accountManager := account.NewManager(s.NodeManager)
|
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
|
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
func (s *WhisperTestSuite) TestLogout() {
|
|
|
|
s.StartTestNode(params.RinkebyNetworkID)
|
|
|
|
defer s.StopTestNode()
|
|
|
|
|
|
|
|
whisperService, err := s.NodeManager.WhisperService()
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
accountManager := account.NewManager(s.NodeManager)
|
|
|
|
s.NotNil(accountManager)
|
|
|
|
|
|
|
|
// create an account
|
|
|
|
address, pubKey, _, err := 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(accountManager.SelectAccount(address, TestConfig.Account1.Password))
|
|
|
|
s.True(whisperService.HasKeyPair(pubKey), "identity not injected into whisper")
|
|
|
|
|
|
|
|
s.NoError(accountManager.Logout())
|
|
|
|
s.False(whisperService.HasKeyPair(pubKey), "identity not cleared from whisper")
|
|
|
|
}
|