github.com/status-im/status-go@v1.1.0/protocol/encryption/sharedsecret/service_test.go (about)

     1  package sharedsecret
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/status-im/status-go/appdatabase"
     7  	"github.com/status-im/status-go/protocol/sqlite"
     8  	"github.com/status-im/status-go/protocol/tt"
     9  	"github.com/status-im/status-go/t/helpers"
    10  
    11  	"github.com/stretchr/testify/suite"
    12  	"go.uber.org/zap"
    13  
    14  	"github.com/status-im/status-go/eth-node/crypto"
    15  )
    16  
    17  func TestServiceTestSuite(t *testing.T) {
    18  	suite.Run(t, new(SharedSecretTestSuite))
    19  }
    20  
    21  type SharedSecretTestSuite struct {
    22  	suite.Suite
    23  	service *SharedSecret
    24  	logger  *zap.Logger
    25  }
    26  
    27  func (s *SharedSecretTestSuite) SetupTest() {
    28  	s.logger = tt.MustCreateTestLogger()
    29  
    30  	db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
    31  	s.Require().NoError(err)
    32  	err = sqlite.Migrate(db)
    33  	s.Require().NoError(err)
    34  
    35  	s.service = New(db, s.logger)
    36  }
    37  
    38  func (s *SharedSecretTestSuite) TearDownTest() {
    39  	_ = s.logger.Sync()
    40  }
    41  
    42  func (s *SharedSecretTestSuite) TestSingleInstallationID() {
    43  	ourInstallationID := "our"
    44  	installationID1 := "1"
    45  	installationID2 := "2"
    46  
    47  	myKey, err := crypto.GenerateKey()
    48  	s.Require().NoError(err)
    49  
    50  	theirKey, err := crypto.GenerateKey()
    51  	s.Require().NoError(err)
    52  
    53  	// We receive a message from installationID1
    54  	sharedKey1, err := s.service.Generate(myKey, &theirKey.PublicKey, installationID1)
    55  	s.Require().NoError(err)
    56  	s.Require().NotNil(sharedKey1, "it generates a shared key")
    57  
    58  	// We want to send a message to installationID1
    59  	sharedKey2, agreed2, err := s.service.Agreed(myKey, ourInstallationID, &theirKey.PublicKey, []string{installationID1})
    60  	s.Require().NoError(err)
    61  	s.Require().True(agreed2)
    62  	s.Require().NotNil(sharedKey2, "We can retrieve a shared secret")
    63  	s.Require().Equal(sharedKey1, sharedKey2, "The shared secret is the same as the one stored")
    64  
    65  	// We want to send a message to multiple installationIDs, one of which we haven't never communicated with
    66  	sharedKey3, agreed3, err := s.service.Agreed(myKey, ourInstallationID, &theirKey.PublicKey, []string{installationID1, installationID2})
    67  	s.Require().NoError(err)
    68  	s.Require().NotNil(sharedKey3, "A shared key is returned")
    69  	s.Require().False(agreed3)
    70  
    71  	// We receive a message from installationID2
    72  	sharedKey4, err := s.service.Generate(myKey, &theirKey.PublicKey, installationID2)
    73  	s.Require().NoError(err)
    74  	s.Require().NotNil(sharedKey4, "it generates a shared key")
    75  	s.Require().Equal(sharedKey1, sharedKey4, "It generates the same key")
    76  
    77  	// We want to send a message to installationID 1 & 2, both have been
    78  	sharedKey5, agreed5, err := s.service.Agreed(myKey, ourInstallationID, &theirKey.PublicKey, []string{installationID1, installationID2})
    79  	s.Require().NoError(err)
    80  	s.Require().NotNil(sharedKey5, "We can retrieve a shared secret")
    81  	s.Require().True(agreed5)
    82  	s.Require().Equal(sharedKey1, sharedKey5, "The shared secret is the same as the one stored")
    83  
    84  }
    85  
    86  func (s *SharedSecretTestSuite) TestAll() {
    87  	installationID1 := "1"
    88  	installationID2 := "2"
    89  
    90  	myKey, err := crypto.GenerateKey()
    91  	s.Require().NoError(err)
    92  
    93  	theirKey1, err := crypto.GenerateKey()
    94  	s.Require().NoError(err)
    95  
    96  	theirKey2, err := crypto.GenerateKey()
    97  	s.Require().NoError(err)
    98  
    99  	// We receive a message from user 1
   100  	sharedKey1, err := s.service.Generate(myKey, &theirKey1.PublicKey, installationID1)
   101  	s.Require().NoError(err)
   102  	s.Require().NotNil(sharedKey1, "it generates a shared key")
   103  
   104  	// We receive a message from user 2
   105  	sharedKey2, err := s.service.Generate(myKey, &theirKey2.PublicKey, installationID2)
   106  	s.Require().NoError(err)
   107  	s.Require().NotNil(sharedKey2, "it generates a shared key")
   108  
   109  	// All the secrets are there
   110  	secrets, err := s.service.All()
   111  	s.Require().NoError(err)
   112  	expected := []*Secret{
   113  		sharedKey1,
   114  		sharedKey2,
   115  	}
   116  	s.Require().Equal(expected, secrets)
   117  }