2020-07-22 07:41:40 +00:00
|
|
|
package pushnotificationserver
|
2020-06-30 08:30:58 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-02 13:57:50 +00:00
|
|
|
"crypto/ecdsa"
|
2020-06-30 14:55:24 +00:00
|
|
|
"crypto/rand"
|
2020-07-01 12:04:09 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2020-06-30 08:30:58 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2020-07-01 12:04:09 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-06-30 14:55:24 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
"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-06-30 14:55:24 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2020-07-01 12:04:09 +00:00
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
2020-07-10 07:45:40 +00:00
|
|
|
"github.com/status-im/status-go/protocol/tt"
|
2020-06-30 08:30:58 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
func TestServerSuite(t *testing.T) {
|
2020-07-02 13:57:50 +00:00
|
|
|
s := new(ServerSuite)
|
|
|
|
s.accessToken = "b6ae4fde-bb65-11ea-b3de-0242ac130004"
|
|
|
|
s.installationID = "c6ae4fde-bb65-11ea-b3de-0242ac130004"
|
|
|
|
|
|
|
|
suite.Run(t, s)
|
2020-07-01 10:09:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
type ServerSuite struct {
|
|
|
|
suite.Suite
|
2020-07-02 13:57:50 +00:00
|
|
|
tmpFile *os.File
|
|
|
|
persistence Persistence
|
|
|
|
accessToken string
|
|
|
|
installationID string
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
key *ecdsa.PrivateKey
|
|
|
|
sharedKey []byte
|
2020-07-13 08:53:13 +00:00
|
|
|
grant []byte
|
2020-07-02 13:57:50 +00:00
|
|
|
server *Server
|
2020-07-01 10:09:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
func (s *ServerSuite) 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)
|
|
|
|
|
2020-06-30 13:14:25 +00:00
|
|
|
identity, err := crypto.GenerateKey()
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.identity = identity
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.key = key
|
2020-06-30 13:14:25 +00:00
|
|
|
|
|
|
|
config := &Config{
|
|
|
|
Identity: identity,
|
2020-07-10 07:45:40 +00:00
|
|
|
Logger: tt.MustCreateTestLogger(),
|
2020-06-30 13:14:25 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 08:54:22 +00:00
|
|
|
s.server = New(config, s.persistence, nil)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
2020-07-02 13:57:50 +00:00
|
|
|
sharedKey, err := s.server.generateSharedKey(&s.key.PublicKey)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.sharedKey = sharedKey
|
2020-07-13 08:53:13 +00:00
|
|
|
signatureMaterial := s.server.buildGrantSignatureMaterial(&s.key.PublicKey, &identity.PublicKey, s.accessToken)
|
|
|
|
grant, err := crypto.Sign(signatureMaterial, s.key)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.grant = grant
|
2020-06-30 14:55:24 +00:00
|
|
|
|
2020-07-02 13:57:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationServerValidateRegistration() {
|
2020-06-30 14:55:24 +00:00
|
|
|
|
2020-06-30 13:14:25 +00:00
|
|
|
// Empty payload
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err := s.server.validateRegistration(&s.key.PublicKey, nil)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrEmptyPushNotificationRegistrationPayload, err)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
|
|
|
// Empty key
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(nil, []byte("payload"))
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrEmptyPushNotificationRegistrationPublicKey, err)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
// Invalid cyphertext length
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, []byte("too short"))
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().Equal(common.ErrInvalidCiphertextLength, err)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
// Invalid cyphertext length
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, []byte("too short"))
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().Equal(common.ErrInvalidCiphertextLength, err)
|
2020-06-30 08:30:58 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
// Invalid ciphertext
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, []byte("not too short but invalid"))
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().Error(common.ErrInvalidCiphertextLength, err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
// Different key ciphertext
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err := common.Encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().Error(err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
// Right cyphertext but non unmarshable payload
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt([]byte("plaintext"), s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrCouldNotUnmarshalPushNotificationRegistration, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Missing installationID
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err := proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-01 08:37:54 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationInstallationID, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Malformed installationID
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-01 08:37:54 +00:00
|
|
|
InstallationId: "abc",
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-22 07:41:40 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationInstallationID, err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
// Version set to 0
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 08:37:54 +00:00
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrInvalidPushNotificationRegistrationVersion, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Version lower than previous one
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 08:37:54 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
|
2020-07-02 08:08:19 +00:00
|
|
|
// Setup persistence
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), &protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 12:04:09 +00:00
|
|
|
Version: 2}))
|
2020-07-01 10:09:40 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrInvalidPushNotificationRegistrationVersion, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-02 08:08:19 +00:00
|
|
|
// Cleanup persistence
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(s.persistence.DeletePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), s.installationID))
|
2020-07-01 10:09:40 +00:00
|
|
|
|
2020-07-01 08:37:54 +00:00
|
|
|
// Unregistering message
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 08:37:54 +00:00
|
|
|
Unregister: true,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().Nil(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Missing access token
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-01 08:37:54 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationAccessToken, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Invalid access token
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-01 08:37:54 +00:00
|
|
|
AccessToken: "bc",
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 08:37:54 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationAccessToken, err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
// Missing device token
|
2020-07-02 08:08:19 +00:00
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-01 08:37:54 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationDeviceToken, err)
|
|
|
|
|
2020-07-13 08:53:13 +00:00
|
|
|
// Missing grant
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: s.accessToken,
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "device-token",
|
2020-07-13 08:53:13 +00:00
|
|
|
InstallationId: s.installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-13 08:53:13 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationGrant, err)
|
|
|
|
|
|
|
|
// Invalid grant
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "device-token",
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: crypto.Keccak256([]byte("invalid")),
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-13 08:53:13 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationGrant, err)
|
|
|
|
|
2020-07-02 14:19:21 +00:00
|
|
|
// Missing token type
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: s.accessToken,
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "device-token",
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
InstallationId: s.installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 14:19:21 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 14:19:21 +00:00
|
|
|
s.Require().Equal(ErrUnknownPushNotificationRegistrationTokenType, err)
|
|
|
|
|
2020-07-02 08:08:19 +00:00
|
|
|
// Successful
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "abc",
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
_, err = s.server.validateRegistration(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationHandleRegistration() {
|
|
|
|
// Empty payload
|
2020-07-22 07:41:40 +00:00
|
|
|
response := s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, nil)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Empty key
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(nil, []byte("payload"))
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Invalid cyphertext length
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, []byte("too short"))
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Invalid cyphertext length
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, []byte("too short"))
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Invalid ciphertext
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, []byte("not too short but invalid"))
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Different key ciphertext
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err := common.Encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Right cyphertext but non unmarshable payload
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt([]byte("plaintext"), s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Missing installationID
|
|
|
|
payload, err := proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Malformed installationID
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-02 08:08:19 +00:00
|
|
|
InstallationId: "abc",
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-22 07:41:40 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Version set to 0
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_VERSION_MISMATCH)
|
|
|
|
|
|
|
|
// Version lower than previous one
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Setup persistence
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), &protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 2}))
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_VERSION_MISMATCH)
|
|
|
|
|
|
|
|
// Cleanup persistence
|
2020-07-07 09:00:04 +00:00
|
|
|
s.Require().NoError(s.persistence.DeletePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), s.installationID))
|
2020-07-02 08:08:19 +00:00
|
|
|
|
|
|
|
// Missing access token
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Invalid access token
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: "bc",
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Missing device token
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Successful
|
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "abc",
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
payload, err = proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
// Pull from the db
|
2020-07-07 09:00:04 +00:00
|
|
|
retrievedRegistration, err := s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(&s.key.PublicKey), s.installationID)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(retrievedRegistration)
|
|
|
|
s.Require().True(proto.Equal(retrievedRegistration, registration))
|
|
|
|
|
|
|
|
// Unregistering message
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "token",
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
2020-07-02 08:08:19 +00:00
|
|
|
Unregister: true,
|
|
|
|
Version: 2,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response = s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
2020-07-31 08:56:26 +00:00
|
|
|
s.Require().Equal(common.Shake256(cyphertext), response.RequestId)
|
2020-07-02 08:08:19 +00:00
|
|
|
|
|
|
|
// Check is gone from the db
|
2020-07-07 09:00:04 +00:00
|
|
|
retrievedRegistration, err = s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(&s.key.PublicKey), s.installationID)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-31 08:56:26 +00:00
|
|
|
s.Require().Nil(retrievedRegistration)
|
2020-07-31 12:22:05 +00:00
|
|
|
// Check version is maintained
|
2020-07-31 08:56:26 +00:00
|
|
|
version, err := s.persistence.GetPushNotificationRegistrationVersion(common.HashPublicKey(&s.key.PublicKey), s.installationID)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(uint64(2), version)
|
2020-06-30 08:30:58 +00:00
|
|
|
}
|
2020-07-02 13:57:50 +00:00
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
func (s *ServerSuite) TestbuildPushNotificationQueryResponseNoFiltering() {
|
2020-07-07 09:00:04 +00:00
|
|
|
hashedPublicKey := common.HashPublicKey(&s.key.PublicKey)
|
2020-07-02 13:57:50 +00:00
|
|
|
// Successful
|
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "abc",
|
2020-07-02 13:57:50 +00:00
|
|
|
AccessToken: s.accessToken,
|
2020-07-13 08:53:13 +00:00
|
|
|
Grant: s.grant,
|
2020-07-02 14:19:21 +00:00
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
2020-07-02 13:57:50 +00:00
|
|
|
InstallationId: s.installationID,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response := s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
query := &protobuf.PushNotificationQuery{
|
|
|
|
PublicKeys: [][]byte{[]byte("non-existing"), hashedPublicKey},
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
queryResponse := s.server.buildPushNotificationQueryResponse(query)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NotNil(queryResponse)
|
|
|
|
s.Require().True(queryResponse.Success)
|
|
|
|
s.Require().Len(queryResponse.Info, 1)
|
|
|
|
s.Require().Equal(s.accessToken, queryResponse.Info[0].AccessToken)
|
|
|
|
s.Require().Equal(hashedPublicKey, queryResponse.Info[0].PublicKey)
|
|
|
|
s.Require().Equal(s.installationID, queryResponse.Info[0].InstallationId)
|
2020-07-22 07:41:40 +00:00
|
|
|
s.Require().Nil(queryResponse.Info[0].AllowedKeyList)
|
2020-07-02 13:57:50 +00:00
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
func (s *ServerSuite) TestbuildPushNotificationQueryResponseWithFiltering() {
|
2020-07-07 09:00:04 +00:00
|
|
|
hashedPublicKey := common.HashPublicKey(&s.key.PublicKey)
|
2020-07-22 07:41:40 +00:00
|
|
|
allowedKeyList := [][]byte{[]byte("a")}
|
2020-07-02 13:57:50 +00:00
|
|
|
// Successful
|
|
|
|
|
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
2020-07-22 07:41:40 +00:00
|
|
|
DeviceToken: "abc",
|
2020-07-20 08:06:24 +00:00
|
|
|
AccessToken: s.accessToken,
|
|
|
|
Grant: s.grant,
|
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
AllowFromContactsOnly: true,
|
2020-07-22 07:41:40 +00:00
|
|
|
AllowedKeyList: allowedKeyList,
|
2020-07-20 08:06:24 +00:00
|
|
|
Version: 1,
|
2020-07-02 13:57:50 +00:00
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2020-07-07 09:00:04 +00:00
|
|
|
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-22 07:41:40 +00:00
|
|
|
response := s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
query := &protobuf.PushNotificationQuery{
|
|
|
|
PublicKeys: [][]byte{[]byte("non-existing"), hashedPublicKey},
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
queryResponse := s.server.buildPushNotificationQueryResponse(query)
|
2020-07-02 13:57:50 +00:00
|
|
|
s.Require().NotNil(queryResponse)
|
|
|
|
s.Require().True(queryResponse.Success)
|
|
|
|
s.Require().Len(queryResponse.Info, 1)
|
|
|
|
s.Require().Equal(hashedPublicKey, queryResponse.Info[0].PublicKey)
|
|
|
|
s.Require().Equal(s.installationID, queryResponse.Info[0].InstallationId)
|
2020-07-22 07:41:40 +00:00
|
|
|
s.Require().Equal(allowedKeyList, queryResponse.Info[0].AllowedKeyList)
|
2020-07-02 13:57:50 +00:00
|
|
|
}
|
2020-09-03 06:53:26 +00:00
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationMentions() {
|
|
|
|
existingChatID := []byte("existing-chat-id")
|
|
|
|
nonExistingChatID := []byte("non-existing-chat-id")
|
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
|
|
|
DeviceToken: "abc",
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
Grant: s.grant,
|
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
AllowedMentionsChatList: [][]byte{existingChatID},
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response := s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
pushNotificationRequest := &protobuf.PushNotificationRequest{
|
|
|
|
MessageId: []byte("message-id"),
|
|
|
|
Requests: []*protobuf.PushNotification{
|
|
|
|
{
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
PublicKey: common.HashPublicKey(&s.key.PublicKey),
|
|
|
|
ChatId: existingChatID,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
PublicKey: common.HashPublicKey(&s.key.PublicKey),
|
|
|
|
ChatId: nonExistingChatID,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushNotificationResponse, requestAndRegistrations := s.server.buildPushNotificationRequestResponse(pushNotificationRequest)
|
|
|
|
s.Require().NotNil(pushNotificationResponse)
|
|
|
|
s.Require().NotNil(requestAndRegistrations)
|
|
|
|
|
|
|
|
// only one should succeed
|
|
|
|
s.Require().Len(requestAndRegistrations, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationDisabledMentions() {
|
|
|
|
existingChatID := []byte("existing-chat-id")
|
|
|
|
registration := &protobuf.PushNotificationRegistration{
|
|
|
|
DeviceToken: "abc",
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
Grant: s.grant,
|
|
|
|
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
|
|
|
|
BlockMentions: true,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
AllowedMentionsChatList: [][]byte{existingChatID},
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
payload, err := proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response := s.server.buildPushNotificationRegistrationResponse(&s.key.PublicKey, cyphertext)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
pushNotificationRequest := &protobuf.PushNotificationRequest{
|
|
|
|
MessageId: []byte("message-id"),
|
|
|
|
Requests: []*protobuf.PushNotification{
|
|
|
|
{
|
|
|
|
AccessToken: s.accessToken,
|
|
|
|
PublicKey: common.HashPublicKey(&s.key.PublicKey),
|
|
|
|
ChatId: existingChatID,
|
|
|
|
InstallationId: s.installationID,
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
pushNotificationResponse, requestAndRegistrations := s.server.buildPushNotificationRequestResponse(pushNotificationRequest)
|
|
|
|
s.Require().NotNil(pushNotificationResponse)
|
|
|
|
s.Require().Nil(requestAndRegistrations)
|
|
|
|
}
|
2020-09-07 08:25:57 +00:00
|
|
|
|
|
|
|
func (s *ServerSuite) TestBuildPushNotificationReport() {
|
|
|
|
accessToken := "a"
|
|
|
|
chatID := []byte("chat-id")
|
|
|
|
author := []byte("author")
|
|
|
|
blockedAuthor := []byte("blocked-author")
|
|
|
|
blockedChatID := []byte("blocked-chat-id")
|
2023-06-22 05:06:32 +00:00
|
|
|
mutedChatID := []byte("muted-chat-id")
|
|
|
|
mutedChatList := [][]byte{mutedChatID, author}
|
2020-09-07 08:25:57 +00:00
|
|
|
blockedChatList := [][]byte{blockedChatID, blockedAuthor}
|
|
|
|
nonJoinedChatID := []byte("non-joined-chat-id")
|
|
|
|
allowedMentionsChatList := [][]byte{chatID}
|
|
|
|
validMessagePN := &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
ChatId: chatID,
|
|
|
|
Author: author,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
}
|
|
|
|
validMentionPN := &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
}
|
|
|
|
validRegistration := &protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
BlockedChatList: blockedChatList,
|
|
|
|
AllowedMentionsChatList: allowedMentionsChatList,
|
2023-06-22 05:06:32 +00:00
|
|
|
MutedChatList: mutedChatList,
|
2020-09-07 08:25:57 +00:00
|
|
|
}
|
|
|
|
blockedMentionsRegistration := &protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
BlockMentions: true,
|
|
|
|
BlockedChatList: blockedChatList,
|
|
|
|
AllowedMentionsChatList: allowedMentionsChatList,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
pn *protobuf.PushNotification
|
|
|
|
registration *protobuf.PushNotificationRegistration
|
|
|
|
expectedError error
|
|
|
|
expectedResponse *reportResult
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid message",
|
|
|
|
pn: validMessagePN,
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: true,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid mention",
|
|
|
|
pn: validMentionPN,
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: true,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unknow push notification",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedError: errUnhandledPushNotificationType,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty registration",
|
|
|
|
pn: validMessagePN,
|
|
|
|
registration: nil,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: false,
|
|
|
|
Error: protobuf.PushNotificationReport_NOT_REGISTERED,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid access token message",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
Author: author,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: "invalid",
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: false,
|
|
|
|
Error: protobuf.PushNotificationReport_WRONG_TOKEN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid access token mention",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
Author: author,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: "invalid",
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: false,
|
|
|
|
Error: protobuf.PushNotificationReport_WRONG_TOKEN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blocked chat list message",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
ChatId: blockedChatID,
|
|
|
|
Author: author,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
2023-06-22 05:06:32 +00:00
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "muted chat list message",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
ChatId: mutedChatID,
|
|
|
|
Author: author,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "unmuted chat and user muted author",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
ChatId: chatID,
|
|
|
|
Author: author,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: true,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "muted chat list mention",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
ChatId: mutedChatID,
|
|
|
|
Author: author,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
2020-09-07 08:25:57 +00:00
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blocked group chat message",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MESSAGE,
|
|
|
|
Author: blockedAuthor,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blocked chat list mention",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
Author: blockedAuthor,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "blocked mentions",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
Author: author,
|
|
|
|
ChatId: chatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: blockedMentionsRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not in allowed mention chat list",
|
|
|
|
pn: &protobuf.PushNotification{
|
|
|
|
Type: protobuf.PushNotification_MENTION,
|
|
|
|
Author: author,
|
|
|
|
ChatId: nonJoinedChatID,
|
|
|
|
AccessToken: accessToken,
|
|
|
|
},
|
|
|
|
registration: validRegistration,
|
|
|
|
expectedResponse: &reportResult{
|
|
|
|
sendNotification: false,
|
|
|
|
report: &protobuf.PushNotificationReport{
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.name, func() {
|
|
|
|
response, err := s.server.buildPushNotificationReport(tc.pn, tc.registration)
|
|
|
|
s.Require().Equal(tc.expectedError, err)
|
|
|
|
s.Require().Equal(tc.expectedResponse, response)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|