2020-01-10 18:59:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2020-01-15 11:36:49 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
2020-10-08 14:05:49 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
2020-07-21 15:41:10 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
2020-01-10 18:59:01 +00:00
|
|
|
)
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
const statusChatID = "status"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func TestMessengerInstallationSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerInstallationSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerInstallationSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
m *Messenger // main instance of Messenger
|
|
|
|
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
|
2020-02-10 11:22:37 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// If one wants to send messages between different instances of Messenger,
|
2020-07-21 15:41:10 +00:00
|
|
|
// a single Waku service should be shared.
|
|
|
|
shh types.Waku
|
2020-02-10 11:22:37 +00:00
|
|
|
|
2020-10-08 14:05:49 +00:00
|
|
|
logger *zap.Logger
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) SetupTest() {
|
|
|
|
s.logger = tt.MustCreateTestLogger()
|
|
|
|
|
2020-07-21 15:41:10 +00:00
|
|
|
config := waku.DefaultConfig
|
|
|
|
config.MinimumAcceptedPoW = 0
|
|
|
|
shh := waku.New(&config, s.logger)
|
|
|
|
s.shh = gethbridge.NewGethWakuWrapper(shh)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(shh.Start(nil))
|
|
|
|
|
|
|
|
s.m = s.newMessenger(s.shh)
|
|
|
|
s.privateKey = s.m.identity
|
2020-07-31 09:08:09 +00:00
|
|
|
// We start the messenger in order to receive installations
|
|
|
|
s.Require().NoError(s.m.Start())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) TearDownTest() {
|
|
|
|
s.Require().NoError(s.m.Shutdown())
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 15:41:10 +00:00
|
|
|
func (s *MessengerInstallationSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
|
2020-01-10 18:59:01 +00:00
|
|
|
options := []Option{
|
|
|
|
WithCustomLogger(s.logger),
|
|
|
|
WithMessagesPersistenceEnabled(),
|
2020-10-08 14:05:49 +00:00
|
|
|
WithDatabaseConfig(sqlite.InMemoryPath, "some-key"),
|
2020-01-10 18:59:01 +00:00
|
|
|
WithDatasync(),
|
|
|
|
}
|
|
|
|
installationID := uuid.New().String()
|
|
|
|
m, err := NewMessenger(
|
|
|
|
privateKey,
|
|
|
|
&testNode{shh: shh},
|
|
|
|
installationID,
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = m.Init()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2020-07-21 15:41:10 +00:00
|
|
|
func (s *MessengerInstallationSuite) newMessenger(shh types.Waku) *Messenger {
|
2020-01-10 18:59:01 +00:00
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
return s.newMessengerWithKey(s.shh, privateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) TestReceiveInstallation() {
|
|
|
|
theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Start())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
err := theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{
|
|
|
|
Name: "their-name",
|
|
|
|
DeviceType: "their-device-type",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response, err := theirMessenger.SendPairInstallation(context.Background())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.Chats, 1)
|
|
|
|
s.Require().False(response.Chats[0].Active)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
|
|
|
|
"installation not received",
|
|
|
|
)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
actualInstallation := response.Installations[0]
|
|
|
|
s.Require().Equal(theirMessenger.installationID, actualInstallation.ID)
|
|
|
|
s.Require().NotNil(actualInstallation.InstallationMetadata)
|
|
|
|
s.Require().Equal("their-name", actualInstallation.InstallationMetadata.Name)
|
|
|
|
s.Require().Equal("their-device-type", actualInstallation.InstallationMetadata.DeviceType)
|
|
|
|
|
|
|
|
err = s.m.EnableInstallation(theirMessenger.installationID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contactKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contact, err := buildContact(&contactKey.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
contact.SystemTags = append(contact.SystemTags, contactAdded)
|
|
|
|
err = s.m.SaveContact(contact)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
theirMessenger,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 && r.Contacts[0].ID == contact.ID },
|
|
|
|
"contact not received",
|
|
|
|
)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
actualContact := response.Contacts[0]
|
|
|
|
s.Require().Equal(contact.ID, actualContact.ID)
|
|
|
|
s.Require().True(actualContact.IsAdded())
|
2020-01-15 07:25:09 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
chat := CreatePublicChat(statusChatID, s.m.transport)
|
2020-01-15 07:25:09 +00:00
|
|
|
err = s.m.SaveChat(&chat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
theirMessenger,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Chats) > 0 },
|
|
|
|
"sync chat not received",
|
|
|
|
)
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
actualChat := response.Chats[0]
|
2020-07-22 07:41:40 +00:00
|
|
|
s.Require().Equal(statusChatID, actualChat.ID)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().True(actualChat.Active)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Shutdown())
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
func (s *MessengerInstallationSuite) TestSyncInstallation() {
|
|
|
|
|
|
|
|
// add contact
|
|
|
|
contactKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
contact, err := buildContact(&contactKey.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
contact.SystemTags = append(contact.SystemTags, contactAdded)
|
2020-08-20 14:06:38 +00:00
|
|
|
contact.LocalNickname = "Test Nickname"
|
2020-01-15 07:25:09 +00:00
|
|
|
err = s.m.SaveContact(contact)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// add chat
|
2020-07-22 07:41:40 +00:00
|
|
|
chat := CreatePublicChat(statusChatID, s.m.transport)
|
2020-01-15 07:25:09 +00:00
|
|
|
err = s.m.SaveChat(&chat)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// pair
|
|
|
|
theirMessenger := s.newMessengerWithKey(s.shh, s.privateKey)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Start())
|
2020-01-15 07:25:09 +00:00
|
|
|
|
|
|
|
err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{
|
|
|
|
Name: "their-name",
|
|
|
|
DeviceType: "their-device-type",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response, err := theirMessenger.SendPairInstallation(context.Background())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.Chats, 1)
|
|
|
|
s.Require().False(response.Chats[0].Active)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
|
|
|
|
"installation not received",
|
|
|
|
)
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
actualInstallation := response.Installations[0]
|
|
|
|
s.Require().Equal(theirMessenger.installationID, actualInstallation.ID)
|
|
|
|
s.Require().NotNil(actualInstallation.InstallationMetadata)
|
|
|
|
s.Require().Equal("their-name", actualInstallation.InstallationMetadata.Name)
|
|
|
|
s.Require().Equal("their-device-type", actualInstallation.InstallationMetadata.DeviceType)
|
|
|
|
|
|
|
|
err = s.m.EnableInstallation(theirMessenger.installationID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// sync
|
|
|
|
err = s.m.SyncDevices(context.Background(), "ens-name", "profile-image")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var allChats []*Chat
|
2020-04-17 11:22:38 +00:00
|
|
|
var actualContact *Contact
|
2020-01-15 07:25:09 +00:00
|
|
|
// Wait for the message to reach its destination
|
|
|
|
err = tt.RetryWithBackOff(func() error {
|
|
|
|
var err error
|
|
|
|
response, err = theirMessenger.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
allChats = append(allChats, response.Chats...)
|
|
|
|
|
2020-04-17 11:22:38 +00:00
|
|
|
if len(allChats) >= 2 && len(response.Contacts) == 1 {
|
|
|
|
actualContact = response.Contacts[0]
|
2020-01-15 07:25:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Not received all chats & contacts")
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var statusChat *Chat
|
|
|
|
for _, c := range allChats {
|
2020-07-22 07:41:40 +00:00
|
|
|
if c.ID == statusChatID {
|
2020-01-15 07:25:09 +00:00
|
|
|
statusChat = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NotNil(statusChat)
|
|
|
|
|
|
|
|
s.Require().True(actualContact.IsAdded())
|
2020-08-20 14:06:38 +00:00
|
|
|
s.Require().Equal("Test Nickname", actualContact.LocalNickname)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Shutdown())
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2020-07-28 13:22:22 +00:00
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() {
|
|
|
|
|
|
|
|
bob1 := s.m
|
|
|
|
// pair
|
|
|
|
bob2 := s.newMessengerWithKey(s.shh, s.privateKey)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(bob2.Start())
|
2020-07-28 13:22:22 +00:00
|
|
|
alice := s.newMessenger(s.shh)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(alice.Start())
|
2020-07-28 13:22:22 +00:00
|
|
|
|
|
|
|
err := bob2.SetInstallationMetadata(bob2.installationID, &multidevice.InstallationMetadata{
|
|
|
|
Name: "their-name",
|
|
|
|
DeviceType: "their-device-type",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response, err := bob2.SendPairInstallation(context.Background())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.Chats, 1)
|
|
|
|
s.Require().False(response.Chats[0].Active)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
bob1,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Installations) > 0 },
|
|
|
|
"installation not received",
|
|
|
|
)
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
actualInstallation := response.Installations[0]
|
|
|
|
s.Require().Equal(bob2.installationID, actualInstallation.ID)
|
|
|
|
err = bob1.EnableInstallation(bob2.installationID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// send a message from bob1 to alice, it should be received on both bob1 and bob2
|
|
|
|
|
|
|
|
alicePkString := types.EncodeHex(crypto.FromECDSAPub(&alice.identity.PublicKey))
|
|
|
|
chat := CreateOneToOneChat(alicePkString, &alice.identity.PublicKey, bob1.transport)
|
|
|
|
s.Require().NoError(bob1.SaveChat(&chat))
|
|
|
|
|
|
|
|
inputMessage := buildTestMessage(chat)
|
|
|
|
_, err = s.m.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
_, err = WaitOnMessengerResponse(
|
|
|
|
bob2,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Messages) > 0 },
|
|
|
|
"message not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(bob2.Shutdown())
|
|
|
|
s.Require().NoError(alice.Shutdown())
|
2020-07-28 13:22:22 +00:00
|
|
|
}
|