status-go/protocol/messenger_delete_message_fo...

282 lines
8.3 KiB
Go
Raw Permalink Normal View History

2022-09-28 11:42:17 +00:00
package protocol
import (
"context"
"crypto/ecdsa"
"errors"
2022-09-28 11:42:17 +00:00
"testing"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
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/common"
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/protobuf"
2022-09-28 11:42:17 +00:00
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
func TestMessengerDeleteMessageForMeSuite(t *testing.T) {
suite.Run(t, new(MessengerDeleteMessageForMeSuite))
}
type MessengerDeleteMessageForMeSuite struct {
suite.Suite
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
alice1 *Messenger
alice2 *Messenger
// If one wants to send messages between different instances of Messenger,
// a single waku service should be shared.
shh types.Waku
logger *zap.Logger
}
func (s *MessengerDeleteMessageForMeSuite) newMessenger() *Messenger {
if s.privateKey == nil {
privateKey, err := crypto.GenerateKey()
s.Require().NoError(err)
s.privateKey = privateKey
}
messenger, err := newMessengerWithKey(s.shh, s.privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
}
func (s *MessengerDeleteMessageForMeSuite) otherNewMessenger() *Messenger {
privateKey, err := crypto.GenerateKey()
s.Require().NoError(err)
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
s.Require().NoError(err)
return messenger
}
func (s *MessengerDeleteMessageForMeSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
s.alice1 = s.newMessenger()
s.alice2 = s.newMessenger()
}
func (s *MessengerDeleteMessageForMeSuite) TearDownTest() {
TearDownMessenger(&s.Suite, s.alice1)
TearDownMessenger(&s.Suite, s.alice2)
2022-09-28 11:42:17 +00:00
_ = s.logger.Sync()
}
func (s *MessengerDeleteMessageForMeSuite) Pair() {
err := s.alice2.SetInstallationMetadata(s.alice2.installationID, &multidevice.InstallationMetadata{
Name: "alice2",
DeviceType: "alice2",
})
s.Require().NoError(err)
response, err := s.alice2.SendPairInstallation(context.Background(), "", nil)
2022-09-28 11:42:17 +00:00
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(
s.alice1,
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
func(r *MessengerResponse) bool { return len(r.Installations()) > 0 },
2022-09-28 11:42:17 +00:00
"installation not received",
)
s.Require().NoError(err)
feat: fallback pairing seed (#5614) * feat(pairing)!: Add extra parameters and remove v2 compatibility This commit includes the following changes: I have added a flag to maintain 2.29 compatibility. Breaking change in connection string The local pairing code that was parsing the connection string had a few non-upgradable features: It was strictly checking the number of parameters, throwing an error if the number was different. This made it impossible to add parameters to it without breaking. It was strictly checking the version number. This made increasing the version number impossible as older client would just refuse to connect. The code has been changed so that: Two parameters have been added, installation-id and key-uid. Those are needed for the fallback flow. I have also removed version from the payload, since it wasn't used. This means that we don't support v1 anymore. V2 parsing is supported . Going forward there's a clear strategy on how to update the protocol (append parameters, don't change existing one). https://www.youtube.com/watch?v=oyLBGkS5ICk Is a must watch video for understanding the strategy Changed MessengerResponse to use internally a map of installations rather than an array (minor) Just moving towards maps as arrays tend to lead to subtle bugs. Moved pairing methods to messenger_pairing.go Just moved some methods Added 2 new methods for the fallback flow FinishPairingThroughSeedPhraseProcess https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R29 EnableAndSyncInstallation https://github.com/status-im/status-go/pull/5567/files#diff-1ad620b07fa3bd5fbc96c9f459d88829938a162bf1aaf41c61dea6e38b488d54R18 Flow for clients Client A1 is logged in Client A2 is logged out Client A1 shows a QR code Client A2 scans a QR code If connection fails on A2, the user will be prompted to enter a seed phrase. If the generated account matches the key-uid from the QR code, A2 should call FinishPairingThroughSeedPhraseProcess with the installation id passed in the QR code. This will send installation information over waku. The user should be shown its own installation id and prompted to check the other device. Client A1 will receive new installation data through waku, if they are still on the qr code page, they should show a popup to the user showing the received installation id, and a way to Enable and Sync, which should call the EnableAndSyncInstallation endpoint. This should finish the fallback syncing flow. Current issues Currently I haven't tested that all the data is synced after finishing the flow. I see that the two devices are paired correctly, but for example the DisplayName is not changed on the receiving device. I haven't had time to look into it further. * test_: add more test for connection string parser * fix_: fix panic when parse old connection string * test_: add comments for TestMessengerPairAfterSeedPhrase * fix_: correct error description * feat_:rename FinishPairingThroughSeedPhraseProcess to EnableInstallationAndPair * fix_: delete leftover * fix_: add UniqueKey method * fix_: unify the response for InputConnectionStringForBootstrapping * fix_: remove fields installationID and keyUID in GethStatusBackend * fix_: rename messenger_pairing to messenger_pairing_and_syncing --------- Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2024-07-30 09:14:05 +00:00
actualInstallation := response.Installations()[0]
2022-09-28 11:42:17 +00:00
s.Require().Equal(s.alice2.installationID, actualInstallation.ID)
s.Require().NotNil(actualInstallation.InstallationMetadata)
s.Require().Equal("alice2", actualInstallation.InstallationMetadata.Name)
s.Require().Equal("alice2", actualInstallation.InstallationMetadata.DeviceType)
_, err = s.alice1.EnableInstallation(s.alice2.installationID)
2022-09-28 11:42:17 +00:00
s.Require().NoError(err)
}
func (s *MessengerDeleteMessageForMeSuite) TestDeleteMessageForMe() {
s.Pair()
chatID := "foobarsynctest"
_, err := s.alice1.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
_, err = s.alice2.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
otherMessenger := s.otherNewMessenger()
_, err = otherMessenger.createPublicChat(chatID, &MessengerResponse{})
s.Require().NoError(err)
chat := otherMessenger.Chat(chatID)
message := buildTestMessage(*chat)
response, err := otherMessenger.SendChatMessage(context.Background(), message)
s.Require().NoError(err)
messageID := response.Messages()[0].ID
2022-09-28 11:42:17 +00:00
var receivedPubChatMessage *common.Message
var alice1ReceivedMessage, alice2ReceivedMessage bool
var notReceivedMessageError = errors.New("not received all messages")
2022-09-28 11:42:17 +00:00
err = tt.RetryWithBackOff(func() error {
response, err = s.alice1.RetrieveAll()
2022-09-28 11:42:17 +00:00
if err != nil {
return err
}
if len(response.Messages()) > 0 {
alice1ReceivedMessage = true
}
2022-09-28 11:42:17 +00:00
response, err = s.alice2.RetrieveAll()
2022-09-28 11:42:17 +00:00
if err != nil {
return err
}
if len(response.Messages()) > 0 {
alice2ReceivedMessage = true
}
2022-09-28 11:42:17 +00:00
messages := response.Messages()
if len(messages) > 0 {
receivedPubChatMessage = messages[0]
if alice1ReceivedMessage && alice2ReceivedMessage {
return nil
}
2022-09-28 11:42:17 +00:00
}
return notReceivedMessageError
2022-09-28 11:42:17 +00:00
})
s.Require().NoError(err)
s.Require().Equal(receivedPubChatMessage.ChatId, chatID)
s.Require().Equal(receivedPubChatMessage.ID, messageID)
s.Require().False(receivedPubChatMessage.DeletedForMe)
// message synced to alice1
alice1Msg, err := s.alice1.MessageByID(messageID)
s.Require().NoError(err)
s.Require().False(alice1Msg.DeletedForMe)
response, err = s.alice1.DeleteMessageForMeAndSync(context.Background(), chatID, messageID)
s.Require().NoError(err)
s.Require().True(response.Messages()[0].DeletedForMe)
s.Require().Equal(response.Chats()[0].LastMessage.ID, messageID)
s.Require().Equal(response.Chats()[0].LastMessage.DeletedForMe, true)
2022-09-28 11:42:17 +00:00
err = tt.RetryWithBackOff(func() error {
response, err = s.alice2.RetrieveAll()
2022-09-28 11:42:17 +00:00
if err != nil {
return err
}
if len(response.messages) > 0 {
return nil
}
return notReceivedMessageError
2022-09-28 11:42:17 +00:00
})
s.Require().NoError(err)
deletedForMeMessage, err := s.alice2.MessageByID(messageID)
s.Require().NoError(err)
s.Require().True(deletedForMeMessage.DeletedForMe)
// no DeletedForMe in others' message
err = tt.RetryWithBackOff(func() error {
response, err = otherMessenger.RetrieveAll()
if err != nil {
return err
}
if len(response.messages) > 0 {
return nil
}
return notReceivedMessageError
})
s.Require().ErrorIs(err, notReceivedMessageError)
2022-09-28 11:42:17 +00:00
otherMessage, err := otherMessenger.MessageByID(messageID)
s.Require().NoError(err)
s.Require().False(otherMessage.DeletedForMe)
}
func (s *MessengerDeleteMessageForMeSuite) TestDeleteImageMessageFromReceiverSide() {
alice := s.otherNewMessenger()
defer TearDownMessenger(&s.Suite, alice)
bob := s.otherNewMessenger()
defer TearDownMessenger(&s.Suite, bob)
theirChat := CreateOneToOneChat("Their 1TO1", &s.privateKey.PublicKey, alice.transport)
err := alice.SaveChat(theirChat)
s.Require().NoError(err)
ourChat := CreateOneToOneChat("Our 1TO1", &alice.identity.PublicKey, alice.transport)
err = bob.SaveChat(ourChat)
s.Require().NoError(err)
messageCount := 3
var album []*common.Message
for i := 0; i < messageCount; i++ {
image, err := buildImageWithoutAlbumIDMessage(*ourChat)
s.NoError(err)
album = append(album, image)
}
response, err := bob.SendChatMessages(context.Background(), album)
s.NoError(err)
// Check that album count was the number of the images sent
imagesCount := uint32(0)
for _, message := range response.Messages() {
if message.ContentType == protobuf.ChatMessage_IMAGE {
imagesCount++
}
}
for _, message := range response.Messages() {
s.Require().NotNil(message.GetImage())
s.Require().Equal(message.GetImage().AlbumImagesCount, imagesCount)
}
s.Require().Equal(messageCount, len(response.Messages()), "it returns the messages")
s.Require().NoError(err)
s.Require().Len(response.Messages(), messageCount)
response, err = WaitOnMessengerResponse(
alice,
func(r *MessengerResponse) bool { return len(r.messages) == messageCount },
"no messages",
)
s.Require().NoError(err)
s.Require().Len(response.Chats(), 1)
s.Require().Len(response.Messages(), messageCount)
for _, message := range response.Messages() {
image := message.GetImage()
s.Require().NotNil(image, "Message.ID=%s", message.ID)
s.Require().Equal(image.AlbumImagesCount, imagesCount)
s.Require().NotEmpty(image.AlbumId, "Message.ID=%s", message.ID)
}
messages := response.Messages()
firstMessageID := messages[0].ID
localChatID := messages[0].LocalChatID
sendResponse, err := alice.DeleteMessageForMeAndSync(context.Background(), localChatID, firstMessageID)
s.Require().NoError(err)
s.Require().Len(sendResponse.Messages(), 3)
s.Require().Len(sendResponse.Chats(), 1)
// LastMessage marked as deleted
s.Require().Equal(sendResponse.Chats()[0].LastMessage.ID, album[2].ID)
s.Require().Equal(sendResponse.Chats()[0].LastMessage.DeletedForMe, true)
}