2020-01-10 18:59:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
"github.com/status-im/status-go/services/browsers"
|
|
|
|
|
2020-01-15 11:36:49 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
"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"
|
2021-10-26 10:48:02 +00:00
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2020-01-10 18:59:01 +00:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
|
|
|
)
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
const statusChatID = "status"
|
2021-10-05 17:26:02 +00:00
|
|
|
const removedChatID = "deactivated"
|
2020-07-22 07:41:40 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func TestMessengerInstallationSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerInstallationSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerInstallationSuite struct {
|
2023-07-13 10:28:34 +00:00
|
|
|
MessengerBaseTestSuite
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) TestReceiveInstallation() {
|
2020-12-21 11:57:47 +00:00
|
|
|
theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
|
|
|
|
s.Require().NoError(err)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
2020-12-21 11:57:47 +00:00
|
|
|
err = theirMessenger.SetInstallationMetadata(theirMessenger.installationID, &multidevice.InstallationMetadata{
|
2020-01-10 18:59:01 +00:00
|
|
|
Name: "their-name",
|
|
|
|
DeviceType: "their-device-type",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
2023-02-28 12:32:45 +00:00
|
|
|
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().False(response.Chats()[0].Active)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
contact, err := BuildContactFromPublicKey(&contactKey.PublicKey)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
2022-10-14 08:50:36 +00:00
|
|
|
response, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: contact.ID})
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-02-14 17:44:00 +00:00
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
s.Require().Equal(response.Contacts[0].ID, contact.ID)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
// Wait for the message to reach its destination
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
theirMessenger,
|
2023-02-14 17:44:00 +00:00
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) == 1 && r.Contacts[0].ID == contact.ID },
|
2020-04-22 12:58:28 +00:00
|
|
|
"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)
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(actualContact.added())
|
2020-01-15 07:25:09 +00:00
|
|
|
|
2023-02-14 17:44:00 +00:00
|
|
|
// Simulate update from contact
|
|
|
|
contact.LastUpdated = 10
|
|
|
|
contact.DisplayName = "display-name"
|
|
|
|
|
|
|
|
s.Require().NoError(s.m.persistence.SaveContacts([]*Contact{contact}))
|
|
|
|
// Trigger syncing of contact
|
|
|
|
err = s.m.syncContact(context.Background(), contact, s.m.dispatchMessage)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
2023-04-27 16:39:59 +00:00
|
|
|
_, err = WaitOnMessengerResponse(
|
2023-02-14 17:44:00 +00:00
|
|
|
theirMessenger,
|
2023-04-27 16:39:59 +00:00
|
|
|
func(r *MessengerResponse) bool {
|
|
|
|
return len(r.Contacts) == 1 &&
|
|
|
|
r.Contacts[0].ID == contact.ID &&
|
|
|
|
// Make sure lastupdated is **not** synced
|
|
|
|
actualContact.LastUpdated == 0 &&
|
|
|
|
r.Contacts[0].DisplayName == "display-name"
|
|
|
|
},
|
2023-02-14 17:44:00 +00:00
|
|
|
"contact not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
chat := CreatePublicChat(statusChatID, s.m.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
err = s.m.SaveChat(chat)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-04-22 12:58:28 +00:00
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
theirMessenger,
|
2021-01-11 10:32:51 +00:00
|
|
|
func(r *MessengerResponse) bool { return len(r.Chats()) > 0 },
|
2020-04-22 12:58:28 +00:00
|
|
|
"sync chat not received",
|
|
|
|
)
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
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)
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
contact, err := BuildContactFromPublicKey(&contactKey.PublicKey)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
2023-01-18 08:12:27 +00:00
|
|
|
|
|
|
|
// mock added as mutual contact
|
|
|
|
contact.LastUpdated = 1
|
2023-01-20 14:28:30 +00:00
|
|
|
contact.ContactRequestReceived(1)
|
2023-01-18 08:12:27 +00:00
|
|
|
s.m.allContacts.Store(contact.ID, contact)
|
|
|
|
|
2020-08-20 14:06:38 +00:00
|
|
|
contact.LocalNickname = "Test Nickname"
|
2022-10-14 08:50:36 +00:00
|
|
|
_, err = s.m.AddContact(context.Background(), &requests.AddContact{ID: contact.ID})
|
2021-10-22 14:20:42 +00:00
|
|
|
s.Require().NoError(err)
|
2021-10-27 10:59:43 +00:00
|
|
|
_, err = s.m.SetContactLocalNickname(&requests.SetContactLocalNickname{ID: types.Hex2Bytes(contact.ID), Nickname: contact.LocalNickname})
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2022-01-17 03:42:11 +00:00
|
|
|
//add bookmark
|
|
|
|
bookmark := browsers.Bookmark{
|
|
|
|
Name: "status official site",
|
|
|
|
URL: "https://status.im",
|
|
|
|
Removed: false,
|
|
|
|
}
|
|
|
|
_, err = s.m.browserDatabase.StoreBookmark(bookmark)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
// add chat
|
2020-07-22 07:41:40 +00:00
|
|
|
chat := CreatePublicChat(statusChatID, s.m.transport)
|
2021-01-11 10:32:51 +00:00
|
|
|
err = s.m.SaveChat(chat)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-10-05 17:26:02 +00:00
|
|
|
// add and deactivate chat
|
|
|
|
chat2 := CreatePublicChat(removedChatID, s.m.transport)
|
2022-08-24 11:31:04 +00:00
|
|
|
chat2.DeletedAtClockValue = 1
|
2021-10-05 17:26:02 +00:00
|
|
|
err = s.m.SaveChat(chat2)
|
|
|
|
s.Require().NoError(err)
|
2022-12-07 19:34:48 +00:00
|
|
|
_, err = s.m.deactivateChat(removedChatID, 0, true, true)
|
2021-10-05 17:26:02 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
// pair
|
2020-12-21 11:57:47 +00:00
|
|
|
theirMessenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
|
|
|
|
s.Require().NoError(err)
|
2021-10-05 17:26:02 +00:00
|
|
|
err = theirMessenger.SaveChat(chat2)
|
|
|
|
s.Require().NoError(err)
|
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)
|
2023-02-28 12:32:45 +00:00
|
|
|
response, err := theirMessenger.SendPairInstallation(context.Background(), nil)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().False(response.Chats()[0].Active)
|
2020-01-15 07:25:09 +00:00
|
|
|
|
|
|
|
// 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
|
2023-01-06 12:21:14 +00:00
|
|
|
err = s.m.SyncDevices(context.Background(), "ens-name", "profile-image", nil)
|
2020-01-15 07:25:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var allChats []*Chat
|
2020-04-17 11:22:38 +00:00
|
|
|
var actualContact *Contact
|
2023-05-05 06:44:26 +00:00
|
|
|
var bookmarks []*browsers.Bookmark
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
allChats = append(allChats, response.Chats()...)
|
2021-09-03 08:26:05 +00:00
|
|
|
if len(response.Contacts) == 1 {
|
2020-04-17 11:22:38 +00:00
|
|
|
actualContact = response.Contacts[0]
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
2023-05-05 06:44:26 +00:00
|
|
|
bookmarks = append(bookmarks, response.GetBookmarks()...)
|
2021-09-03 08:26:05 +00:00
|
|
|
|
2023-05-05 06:44:26 +00:00
|
|
|
if len(allChats) >= 2 && actualContact != nil && len(bookmarks) >= 1 {
|
2020-01-15 07:25:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-05 06:44:26 +00:00
|
|
|
return errors.New("not received all chats & contacts & bookmarks yet")
|
2020-01-15 07:25:09 +00:00
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
var statusChat *Chat
|
2021-10-05 17:26:02 +00:00
|
|
|
var removedChat *Chat
|
2020-01-15 07:25:09 +00:00
|
|
|
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
|
|
|
|
}
|
2021-10-05 17:26:02 +00:00
|
|
|
if c.ID == removedChatID {
|
|
|
|
removedChat = c
|
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Require().NotNil(statusChat)
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(actualContact.added())
|
2020-08-20 14:06:38 +00:00
|
|
|
s.Require().Equal("Test Nickname", actualContact.LocalNickname)
|
2023-01-20 14:28:30 +00:00
|
|
|
s.Require().True(actualContact.hasAddedUs())
|
|
|
|
s.Require().True(actualContact.mutual())
|
2022-01-17 03:42:11 +00:00
|
|
|
|
2023-05-05 06:44:26 +00:00
|
|
|
bookmarks, err = theirMessenger.browserDatabase.GetBookmarks()
|
2022-01-17 03:42:11 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(1, len(bookmarks))
|
|
|
|
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Shutdown())
|
2021-10-05 17:26:02 +00:00
|
|
|
|
|
|
|
s.Require().NotNil(removedChat)
|
|
|
|
s.Require().False(removedChat.Active)
|
2022-01-17 03:42:11 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2020-07-28 13:22:22 +00:00
|
|
|
|
|
|
|
func (s *MessengerInstallationSuite) TestSyncInstallationNewMessages() {
|
|
|
|
|
|
|
|
bob1 := s.m
|
|
|
|
// pair
|
2020-12-21 11:57:47 +00:00
|
|
|
bob2, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
|
|
|
|
s.Require().NoError(err)
|
2023-07-13 10:44:09 +00:00
|
|
|
alice := s.newMessenger()
|
2020-07-28 13:22:22 +00:00
|
|
|
|
2020-12-21 11:57:47 +00:00
|
|
|
err = bob2.SetInstallationMetadata(bob2.installationID, &multidevice.InstallationMetadata{
|
2020-07-28 13:22:22 +00:00
|
|
|
Name: "their-name",
|
|
|
|
DeviceType: "their-device-type",
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
2023-02-28 12:32:45 +00:00
|
|
|
response, err := bob2.SendPairInstallation(context.Background(), nil)
|
2020-07-28 13:22:22 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().Len(response.Chats(), 1)
|
|
|
|
s.Require().False(response.Chats()[0].Active)
|
2020-07-28 13:22:22 +00:00
|
|
|
|
|
|
|
// 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)
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().NoError(bob1.SaveChat(chat))
|
2020-07-28 13:22:22 +00:00
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
inputMessage := buildTestMessage(*chat)
|
2020-07-28 13:22:22 +00:00
|
|
|
_, err = s.m.SendChatMessage(context.Background(), inputMessage)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
_, err = WaitOnMessengerResponse(
|
|
|
|
bob2,
|
2021-06-03 13:11:55 +00:00
|
|
|
func(r *MessengerResponse) bool { return len(r.Messages()) > 0 },
|
2020-07-28 13:22:22 +00:00
|
|
|
"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
|
|
|
}
|