mirror of
https://github.com/status-im/status-go.git
synced 2026-08-31 00:51:12 +00:00
Part of the Go project layout migration, item 27. Pure move plus import-path rewrite across 502 files. No API or behaviour change. `internal/` keeps the messaging application logic unimportable from outside the module, which is what the issue asks for -- status-go is consumed through the C-bindings in mobile/, not as a Go library. Things that had to follow the move, beyond the Go imports: - tools/generate-handlers/template.txt. messenger_handlers.go is generated, and the template hard-codes the imports it emits, so the generated file kept importing protocol/common and failed typecheck. - .gitignore. The ignore rule for that generated file was pinned to the old path; without moving it, a 1486-line generated file starts being tracked. - Makefile: the logosstorage and torrent test targets (both the archive packages and ./protocol itself), the archive README, migration-protocol. - scripts/run_unit_tests.sh, which names the protocol package explicitly to shard its tests. - scripts/cleanup_generated_files.sh and .golangci.yml. scripts/migration_check.sh also needed a fix that is not specific to this move: it validated every file the branch touched under a migration dir against the timestamp naming rule, and a directory rename makes every migration in it look newly added. It now excludes renames, so moving a migration is not mistaken for adding one. refs #7067
328 lines
10 KiB
Go
328 lines
10 KiB
Go
package pushnotificationclient
|
|
|
|
import (
|
|
"crypto/ecdsa"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/status-im/status-go/internal/crypto"
|
|
"github.com/status-im/status-go/internal/db/appdatabase"
|
|
"github.com/status-im/status-go/internal/protocol/protobuf"
|
|
"github.com/status-im/status-go/internal/protocol/sqlite"
|
|
"github.com/status-im/status-go/internal/testutils"
|
|
)
|
|
|
|
const (
|
|
testAccessToken = "token"
|
|
installationID1 = "installation-id-1"
|
|
installationID2 = "installation-id-2"
|
|
installationID3 = "installation-id-3"
|
|
)
|
|
|
|
func TestSQLitePersistenceSuite(t *testing.T) {
|
|
suite.Run(t, new(SQLitePersistenceSuite))
|
|
}
|
|
|
|
type SQLitePersistenceSuite struct {
|
|
suite.Suite
|
|
persistence *Persistence
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) SetupTest() {
|
|
db, err := testutils.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
|
s.Require().NoError(err)
|
|
err = sqlite.Migrate(db)
|
|
s.Require().NoError(err)
|
|
|
|
s.persistence = NewPersistence(db)
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveServer() {
|
|
key, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
server := &PushNotificationServer{
|
|
PublicKey: &key.PublicKey,
|
|
Registered: true,
|
|
RegisteredAt: 1,
|
|
AccessToken: testAccessToken,
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.UpsertServer(server))
|
|
|
|
retrievedServers, err := s.persistence.GetServers()
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(retrievedServers, 1)
|
|
s.Require().True(retrievedServers[0].Registered)
|
|
s.Require().Equal(int64(1), retrievedServers[0].RegisteredAt)
|
|
s.Require().True(crypto.IsPubKeyEqual(retrievedServers[0].PublicKey, &key.PublicKey))
|
|
s.Require().Equal(testAccessToken, retrievedServers[0].AccessToken)
|
|
|
|
server.Registered = false
|
|
server.RegisteredAt = 2
|
|
|
|
s.Require().NoError(s.persistence.UpsertServer(server))
|
|
|
|
retrievedServers, err = s.persistence.GetServers()
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(retrievedServers, 1)
|
|
s.Require().False(retrievedServers[0].Registered)
|
|
s.Require().Equal(int64(2), retrievedServers[0].RegisteredAt)
|
|
s.Require().True(crypto.IsPubKeyEqual(retrievedServers[0].PublicKey, &key.PublicKey))
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveInfo() {
|
|
key1, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
key2, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
serverKey, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
infos := []*PushNotificationInfo{
|
|
{
|
|
PublicKey: &key1.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID1,
|
|
},
|
|
{
|
|
PublicKey: &key1.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID2,
|
|
},
|
|
{
|
|
PublicKey: &key1.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID3,
|
|
},
|
|
{
|
|
PublicKey: &key2.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID1,
|
|
},
|
|
{
|
|
PublicKey: &key2.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID2,
|
|
},
|
|
{
|
|
PublicKey: &key2.PublicKey,
|
|
ServerPublicKey: &serverKey.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID3,
|
|
},
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationInfo(infos))
|
|
|
|
retrievedInfos, err := s.persistence.GetPushNotificationInfo(&key1.PublicKey, []string{installationID1, installationID2})
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(retrievedInfos, 2)
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveInfoWithVersion() {
|
|
installationID := "installation-id-1"
|
|
key, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
serverKey1, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
serverKey2, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
infos := []*PushNotificationInfo{
|
|
{
|
|
PublicKey: &key.PublicKey,
|
|
ServerPublicKey: &serverKey1.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID,
|
|
},
|
|
{
|
|
PublicKey: &key.PublicKey,
|
|
ServerPublicKey: &serverKey2.PublicKey,
|
|
RetrievedAt: 1,
|
|
Version: 1,
|
|
AccessToken: testAccessToken,
|
|
InstallationID: installationID,
|
|
},
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationInfo(infos))
|
|
|
|
retrievedInfos, err := s.persistence.GetPushNotificationInfo(&key.PublicKey, []string{installationID})
|
|
s.Require().NoError(err)
|
|
|
|
// We should retrieve both
|
|
s.Require().Len(retrievedInfos, 2)
|
|
s.Require().Equal(uint64(1), retrievedInfos[0].Version)
|
|
|
|
// Bump version
|
|
infos[0].Version = 2
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationInfo(infos))
|
|
|
|
retrievedInfos, err = s.persistence.GetPushNotificationInfo(&key.PublicKey, []string{installationID})
|
|
s.Require().NoError(err)
|
|
|
|
// Only one should be retrieved now
|
|
s.Require().Len(retrievedInfos, 1)
|
|
s.Require().Equal(uint64(2), retrievedInfos[0].Version)
|
|
|
|
// Lower version
|
|
infos[0].Version = 1
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationInfo(infos))
|
|
|
|
retrievedInfos, err = s.persistence.GetPushNotificationInfo(&key.PublicKey, []string{installationID})
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(retrievedInfos, 1)
|
|
s.Require().Equal(uint64(2), retrievedInfos[0].Version)
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) TestNotifiedOnAndUpdateNotificationResponse() {
|
|
key, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
installationID := "installation-id"
|
|
messageID := []byte("message-id")
|
|
|
|
sentNotification := &SentNotification{
|
|
PublicKey: &key.PublicKey,
|
|
InstallationID: installationID,
|
|
MessageID: messageID,
|
|
LastTriedAt: time.Now().Unix(),
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.UpsertSentNotification(sentNotification))
|
|
|
|
retrievedNotification, err := s.persistence.GetSentNotification(sentNotification.HashedPublicKey(), installationID, messageID)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(sentNotification, retrievedNotification)
|
|
|
|
retriableNotifications, err := s.persistence.GetRetriablePushNotifications()
|
|
s.Require().NoError(err)
|
|
s.Require().Len(retriableNotifications, 0)
|
|
|
|
response := &protobuf.PushNotificationReport{
|
|
Success: false,
|
|
Error: protobuf.PushNotificationReport_WRONG_TOKEN,
|
|
PublicKey: sentNotification.HashedPublicKey(),
|
|
InstallationId: installationID,
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.UpdateNotificationResponse(messageID, response))
|
|
// This notification should be retriable
|
|
retriableNotifications, err = s.persistence.GetRetriablePushNotifications()
|
|
s.Require().NoError(err)
|
|
s.Require().Len(retriableNotifications, 1)
|
|
|
|
sentNotification.Error = protobuf.PushNotificationReport_WRONG_TOKEN
|
|
|
|
retrievedNotification, err = s.persistence.GetSentNotification(sentNotification.HashedPublicKey(), installationID, messageID)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(sentNotification, retrievedNotification)
|
|
|
|
// Update with a successful notification
|
|
response = &protobuf.PushNotificationReport{
|
|
Success: true,
|
|
PublicKey: sentNotification.HashedPublicKey(),
|
|
InstallationId: installationID,
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.UpdateNotificationResponse(messageID, response))
|
|
|
|
sentNotification.Success = true
|
|
sentNotification.Error = protobuf.PushNotificationReport_UNKNOWN_ERROR_TYPE
|
|
|
|
retrievedNotification, err = s.persistence.GetSentNotification(sentNotification.HashedPublicKey(), installationID, messageID)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(sentNotification, retrievedNotification)
|
|
|
|
// This notification should not be retriable
|
|
retriableNotifications, err = s.persistence.GetRetriablePushNotifications()
|
|
s.Require().NoError(err)
|
|
s.Require().Len(retriableNotifications, 0)
|
|
|
|
// Update with a unsuccessful notification, it should be ignored
|
|
response = &protobuf.PushNotificationReport{
|
|
Success: false,
|
|
Error: protobuf.PushNotificationReport_WRONG_TOKEN,
|
|
PublicKey: sentNotification.HashedPublicKey(),
|
|
InstallationId: installationID,
|
|
}
|
|
|
|
s.Require().NoError(s.persistence.UpdateNotificationResponse(messageID, response))
|
|
|
|
sentNotification.Success = true
|
|
sentNotification.Error = protobuf.PushNotificationReport_UNKNOWN_ERROR_TYPE
|
|
|
|
retrievedNotification, err = s.persistence.GetSentNotification(sentNotification.HashedPublicKey(), installationID, messageID)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(sentNotification, retrievedNotification)
|
|
}
|
|
|
|
func (s *SQLitePersistenceSuite) TestSaveAndRetrieveRegistration() {
|
|
// Try with nil first
|
|
retrievedRegistration, retrievedContactIDs, err := s.persistence.GetLastPushNotificationRegistration()
|
|
s.Require().NoError(err)
|
|
s.Require().Nil(retrievedRegistration)
|
|
s.Require().Nil(retrievedContactIDs)
|
|
|
|
// Save & retrieve registration
|
|
registration := &protobuf.PushNotificationRegistration{
|
|
AccessToken: "test",
|
|
Version: 3,
|
|
}
|
|
|
|
key1, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
key2, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
key3, err := crypto.GenerateKey()
|
|
s.Require().NoError(err)
|
|
|
|
publicKeys := []*ecdsa.PublicKey{&key1.PublicKey, &key2.PublicKey}
|
|
|
|
s.Require().NoError(s.persistence.SaveLastPushNotificationRegistration(registration, publicKeys))
|
|
retrievedRegistration, retrievedContactIDs, err = s.persistence.GetLastPushNotificationRegistration()
|
|
s.Require().NoError(err)
|
|
s.Require().True(proto.Equal(registration, retrievedRegistration))
|
|
s.Require().Equal(publicKeys, retrievedContactIDs)
|
|
|
|
// Override and retrieve
|
|
|
|
registration.Version = 5
|
|
publicKeys = append(publicKeys, &key3.PublicKey)
|
|
s.Require().NoError(s.persistence.SaveLastPushNotificationRegistration(registration, publicKeys))
|
|
retrievedRegistration, retrievedContactIDs, err = s.persistence.GetLastPushNotificationRegistration()
|
|
s.Require().NoError(err)
|
|
s.Require().True(proto.Equal(registration, retrievedRegistration))
|
|
s.Require().Equal(publicKeys, retrievedContactIDs)
|
|
}
|