2020-01-10 18:59:01 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"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/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.
|
|
|
|
shh types.Waku
|
2020-01-10 18:59:01 +00:00
|
|
|
tmpFiles []*os.File // files to clean up
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
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-21 15:41:10 +00:00
|
|
|
func (s *MessengerContactUpdateSuite) newMessengerWithKey(shh types.Waku, privateKey *ecdsa.PrivateKey) *Messenger {
|
2020-01-10 18:59:01 +00:00
|
|
|
tmpFile, err := ioutil.TempFile("", "")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
options := []Option{
|
|
|
|
WithCustomLogger(s.logger),
|
|
|
|
WithMessagesPersistenceEnabled(),
|
|
|
|
WithDatabaseConfig(tmpFile.Name(), "some-key"),
|
|
|
|
WithDatasync(),
|
|
|
|
}
|
|
|
|
m, err := NewMessenger(
|
|
|
|
privateKey,
|
|
|
|
&testNode{shh: shh},
|
|
|
|
uuid.New().String(),
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
err = m.Init()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.tmpFiles = append(s.tmpFiles, tmpFile)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
return s.newMessengerWithKey(s.shh, privateKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MessengerContactUpdateSuite) TestReceiveContactUpdate() {
|
|
|
|
theirName := "ens-name.stateofus.eth"
|
|
|
|
theirPicture := "their-picture"
|
|
|
|
contactID := types.EncodeHex(crypto.FromECDSAPub(&s.m.identity.PublicKey))
|
|
|
|
|
|
|
|
theirMessenger := s.newMessenger(s.shh)
|
|
|
|
theirContactID := types.EncodeHex(crypto.FromECDSAPub(&theirMessenger.identity.PublicKey))
|
|
|
|
|
|
|
|
response, err := theirMessenger.SendContactUpdate(context.Background(), contactID, theirName, theirPicture)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
s.Require().Len(response.Contacts, 1)
|
|
|
|
contact := response.Contacts[0]
|
|
|
|
s.Require().True(contact.IsAdded())
|
|
|
|
|
|
|
|
s.Require().Len(response.Chats, 1)
|
|
|
|
chat := response.Chats[0]
|
|
|
|
s.Require().False(chat.Active, "It does not create an active chat")
|
|
|
|
|
|
|
|
// 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]
|
|
|
|
s.Require().Equal(theirName, receivedContact.Name)
|
|
|
|
s.Require().Equal(theirPicture, receivedContact.Photo)
|
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
|
|
|
s.Require().True(receivedContact.HasBeenAdded())
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
|
|
|
|
|
|
|
newPicture := "new-picture"
|
|
|
|
err = theirMessenger.SendContactUpdates(context.Background(), newName, newPicture)
|
|
|
|
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)
|
|
|
|
s.Require().Equal(newName, receivedContact.Name)
|
|
|
|
s.Require().Equal(newPicture, receivedContact.Photo)
|
|
|
|
s.Require().False(receivedContact.ENSVerified)
|
|
|
|
s.Require().True(receivedContact.HasBeenAdded())
|
|
|
|
s.Require().NotEmpty(receivedContact.LastUpdated)
|
|
|
|
}
|