2020-01-10 18:59:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
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-21 15:41:10 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
2020-01-10 18:59:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMessengerContactUpdateSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessengerContactUpdateSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type MessengerContactUpdateSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
m *Messenger // main instance of Messenger
|
|
|
|
privateKey *ecdsa.PrivateKey // private key for the main instance of Messenger
|
|
|
|
// 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.
|
2020-10-08 14:05:49 +00:00
|
|
|
shh types.Waku
|
|
|
|
logger *zap.Logger
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) 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)
|
2021-07-09 13:19:33 +00:00
|
|
|
s.Require().NoError(shh.Start())
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
s.m = s.newMessenger(s.shh)
|
|
|
|
s.privateKey = s.m.identity
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err := s.m.Start()
|
|
|
|
s.Require().NoError(err)
|
2020-07-31 09:46:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) 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 *MessengerContactUpdateSuite) newMessenger(shh types.Waku) *Messenger {
|
2020-01-10 18:59:01 +00:00
|
|
|
privateKey, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-12-21 11:57:47 +00:00
|
|
|
messenger, err := newMessengerWithKey(s.shh, privateKey, s.logger, nil)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
return messenger
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
|
|
|
|
theirName := "ens-name.stateofus.eth"
|
2021-10-22 14:20:42 +00:00
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
|
|
|
theirMessenger := s.newMessenger(s.shh)
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2021-10-22 14:20:42 +00:00
|
|
|
|
|
|
|
// Set ENS name
|
|
|
|
err = theirMessenger.settings.SaveSetting("preferred-name", theirName)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
theirContactID := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
|
|
|
|
|
2021-10-26 10:48:02 +00:00
|
|
|
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
2021-10-22 14:20:42 +00:00
|
|
|
// It should add the contact
|
|
|
|
s.Require().True(contact.Added)
|
|
|
|
|
|
|
|
// It should create a profile chat & a one to one chat
|
|
|
|
s.Require().Len(response.Chats(), 2)
|
|
|
|
chats := response.Chats()
|
|
|
|
if chats[0].ChatType == ChatTypeOneToOne {
|
|
|
|
s.Require().False(chats[0].Active)
|
|
|
|
} else {
|
|
|
|
s.Require().False(chats[1].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.Contacts) > 0 },
|
|
|
|
"contact request not received",
|
|
|
|
)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
receivedContact := response.Contacts[0]
|
2022-02-17 15:13:10 +00:00
|
|
|
s.Require().Equal(theirName, receivedContact.EnsName)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
2021-10-01 14:50:16 +00:00
|
|
|
s.Require().True(receivedContact.HasAddedUs)
|
2020-01-10 18:59:01 +00:00
|
|
|
|
|
|
|
newPicture := "new-picture"
|
2022-02-17 15:13:10 +00:00
|
|
|
err = theirMessenger.SendContactUpdates(context.Background(), newEnsName, newPicture)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// 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.Contacts) > 0 && response.Contacts[0].ID == theirContactID
|
|
|
|
},
|
|
|
|
"contact request not received",
|
|
|
|
)
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
receivedContact = response.Contacts[0]
|
|
|
|
s.Require().Equal(theirContactID, receivedContact.ID)
|
2022-02-17 15:13:10 +00:00
|
|
|
s.Require().Equal(newEnsName, receivedContact.EnsName)
|
2020-01-10 18:59:01 +00:00
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
2020-07-31 09:46:38 +00:00
|
|
|
s.Require().NoError(theirMessenger.Shutdown())
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestAddContact() {
|
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
|
|
|
theirMessenger := s.newMessenger(s.shh)
|
2021-01-14 22:15:13 +00:00
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
2021-10-26 10:48:02 +00:00
|
|
|
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
|
2020-12-22 10:49:25 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
|
|
|
|
// It adds the profile chat and the one to one chat
|
2021-01-11 10:32:51 +00:00
|
|
|
s.Require().Len(response.Chats(), 2)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// It should add the contact
|
2021-10-01 14:50:16 +00:00
|
|
|
s.Require().True(contact.Added)
|
2020-12-22 10:49:25 +00:00
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
|
|
|
|
"contact request not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
receivedContact := response.Contacts[0]
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
|
|
|
}
|
2021-10-26 10:48:02 +00:00
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestAddContactWithENS() {
|
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
ensName := "blah.stateofus.eth"
|
|
|
|
|
|
|
|
theirMessenger := s.newMessenger(s.shh)
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().NoError(theirMessenger.ENSVerified(contactID, ensName))
|
|
|
|
|
|
|
|
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
2022-02-17 15:13:10 +00:00
|
|
|
s.Require().Equal(ensName, response.Contacts[0].EnsName)
|
2021-10-26 10:48:02 +00:00
|
|
|
s.Require().True(response.Contacts[0].ENSVerified)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
|
|
|
|
// It adds the profile chat and the one to one chat
|
|
|
|
s.Require().Len(response.Chats(), 2)
|
|
|
|
|
|
|
|
// It should add the contact
|
|
|
|
s.Require().True(contact.Added)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
|
|
|
|
"contact request not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
receivedContact := response.Contacts[0]
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
|
|
|
}
|
2021-11-22 17:21:27 +00:00
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestRejectContactRequest() {
|
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
|
|
|
theirMessenger := s.newMessenger(s.shh)
|
|
|
|
_, err := theirMessenger.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
response, err := theirMessenger.AddContact(context.Background(), &requests.AddContact{ID: types.Hex2Bytes(contactID)})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
// It should add the contact
|
|
|
|
s.Require().True(contact.Added)
|
|
|
|
|
|
|
|
// Wait for the message to reach its destination
|
|
|
|
response, err = WaitOnMessengerResponse(
|
|
|
|
s.m,
|
|
|
|
func(r *MessengerResponse) bool { return len(r.Contacts) > 0 },
|
|
|
|
"contact request not received",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Make sure HasAddedUs is set
|
|
|
|
receivedContact := response.Contacts[0]
|
|
|
|
s.Require().True(receivedContact.HasAddedUs)
|
|
|
|
|
|
|
|
response, err = s.m.RejectContactRequest(context.Background(), &requests.RejectContactRequest{ID: types.Hex2Bytes(contactID)})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
s.Require().Equal(response.Contacts[0].ID, contactID)
|
|
|
|
s.Require().False(response.Contacts[0].HasAddedUs)
|
|
|
|
}
|