2020-07-22 07:41:40 +00:00
|
|
|
package pushnotificationserver
|
2020-07-01 12:04:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-07-07 09:00:04 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2020-07-01 12:04:09 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSQLitePersistenceSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(SQLitePersistenceSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type SQLitePersistenceSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
tmpFile *os.File
|
|
|
|
persistence Persistence
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) SetupTest() {
|
|
|
|
tmpFile, err := ioutil.TempFile("", "")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.tmpFile = tmpFile
|
|
|
|
|
2022-09-27 20:27:20 +00:00
|
|
|
database, err := sqlite.Open(s.tmpFile.Name(), "", sqlite.ReducedKDFIterationsNumber)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.persistence = NewSQLitePersistence(database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) TearDownTest() {
|
|
|
|
_ = os.Remove(s.tmpFile.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieve() {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
installationID := "54242d02-bb92-11ea-b3de-0242ac130004"
|
|
|
|
|
2020-07-02 08:08:19 +00:00
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
2020-07-01 12:04:09 +00:00
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 5,
|
|
|
|
}
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationRegistration(common.HashPublicKey(&key.PublicKey), registration))
|
2020-07-01 12:04:09 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
retrievedRegistration, err := s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(&key.PublicKey), installationID)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().True(proto.Equal(registration, retrievedRegistration))
|
2020-07-01 12:04:09 +00:00
|
|
|
}
|
2020-07-13 10:39:33 +00:00
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveIdentity() {
|
|
|
|
retrievedKey, err := s.persistence.GetIdentity()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Nil(retrievedKey)
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(s.persistence.SaveIdentity(key))
|
|
|
|
|
|
|
|
retrievedKey, err = s.persistence.GetIdentity()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal(key, retrievedKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveDifferentIdenities() {
|
|
|
|
key1, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
key2, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// First one should be successul, second should fail
|
|
|
|
s.Require().NoError(s.persistence.SaveIdentity(key1))
|
|
|
|
s.Require().Error(s.persistence.SaveIdentity(key2))
|
|
|
|
}
|
2020-08-26 05:54:00 +00:00
|
|
|
|
|
|
|
func (s *SQLitePersistenceSuite) TestExists() {
|
|
|
|
messageID1 := []byte("1")
|
|
|
|
messageID2 := []byte("2")
|
|
|
|
|
|
|
|
result, err := s.persistence.PushNotificationExists(messageID1)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().False(result)
|
|
|
|
|
|
|
|
result, err = s.persistence.PushNotificationExists(messageID1)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require()
|
|
|
|
s.Require().True(result)
|
|
|
|
|
|
|
|
result, err = s.persistence.PushNotificationExists(messageID2)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().False(result)
|
|
|
|
}
|