status-go/protocol/push_notification_server/server_test.go

581 lines
21 KiB
Go
Raw Normal View History

package push_notification_server
import (
2020-07-02 13:57:50 +00:00
"crypto/ecdsa"
2020-06-30 14:55:24 +00:00
"crypto/rand"
"io/ioutil"
"os"
"testing"
"github.com/ethereum/go-ethereum/crypto"
2020-06-30 14:55:24 +00:00
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/suite"
2020-06-30 14:55:24 +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"
"github.com/status-im/status-go/protocol/sqlite"
2020-07-10 07:45:40 +00:00
"github.com/status-im/status-go/protocol/tt"
)
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)
}
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
}
func (s *ServerSuite) SetupTest() {
tmpFile, err := ioutil.TempFile("", "")
s.Require().NoError(err)
s.tmpFile = tmpFile
database, err := sqlite.Open(s.tmpFile.Name(), "")
s.Require().NoError(err)
s.persistence = NewSQLitePersistence(database)
identity, err := crypto.GenerateKey()
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
config := &Config{
Identity: identity,
2020-07-10 07:45:40 +00:00
Logger: tt.MustCreateTestLogger(),
}
s.server = New(config, s.persistence, nil)
2020-07-02 13:57:50 +00:00
sharedKey, err := s.server.generateSharedKey(&s.key.PublicKey)
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
// Empty payload
2020-07-02 13:57:50 +00:00
_, err := s.server.ValidateRegistration(&s.key.PublicKey, nil)
s.Require().Equal(ErrEmptyPushNotificationRegistrationPayload, err)
// Empty key
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(nil, []byte("payload"))
s.Require().Equal(ErrEmptyPushNotificationRegistrationPublicKey, err)
2020-06-30 14:55:24 +00:00
// Invalid cyphertext length
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, []byte("too short"))
s.Require().Equal(common.ErrInvalidCiphertextLength, err)
2020-06-30 14:55:24 +00:00
// Invalid cyphertext length
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, []byte("too short"))
s.Require().Equal(common.ErrInvalidCiphertextLength, err)
2020-06-30 14:55:24 +00:00
// Invalid ciphertext
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, []byte("not too short but invalid"))
s.Require().Error(common.ErrInvalidCiphertextLength, err)
2020-06-30 14:55:24 +00:00
// Different key ciphertext
cyphertext, err := common.Encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Error(err)
2020-06-30 14:55:24 +00:00
// Right cyphertext but non unmarshable payload
cyphertext, err = common.Encrypt([]byte("plaintext"), s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrCouldNotUnmarshalPushNotificationRegistration, err)
2020-07-01 08:37:54 +00:00
// 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 14:19:21 +00:00
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
2020-07-01 08:37:54 +00:00
Version: 1,
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationInstallationID, err)
2020-07-01 08:37:54 +00:00
// Malformed installationID
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,
})
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationInstallationID, err)
2020-06-30 14:55:24 +00:00
// Version set to 0
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
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrInvalidPushNotificationRegistrationVersion, err)
2020-07-01 08:37:54 +00:00
// 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 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,
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
// Setup persistence
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,
Version: 2}))
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrInvalidPushNotificationRegistrationVersion, err)
2020-07-01 08:37:54 +00:00
// Cleanup persistence
s.Require().NoError(s.persistence.DeletePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), s.installationID))
2020-07-01 08:37:54 +00:00
// Unregistering message
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,
})
s.Require().NoError(err)
2020-06-30 14:55:24 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Nil(err)
2020-07-01 08:37:54 +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 14:19:21 +00:00
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
2020-07-01 08:37:54 +00:00
Version: 1,
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationAccessToken, err)
2020-07-01 08:37:54 +00:00
// Invalid access token
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,
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationAccessToken, err)
2020-07-01 08:37:54 +00:00
// Missing device token
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,
})
s.Require().NoError(err)
2020-07-01 08:37:54 +00:00
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationDeviceToken, err)
2020-07-13 08:53:13 +00:00
// Missing grant
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
AccessToken: s.accessToken,
Token: "device-token",
InstallationId: s.installationID,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrMalformedPushNotificationRegistrationGrant, err)
// Invalid grant
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
AccessToken: s.accessToken,
TokenType: protobuf.PushNotificationRegistration_APN_TOKEN,
Token: "device-token",
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)
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
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,
Token: "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)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
2020-07-02 14:19:21 +00:00
s.Require().NoError(err)
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().Equal(ErrUnknownPushNotificationRegistrationTokenType, err)
// Successful
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
Token: "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,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
_, err = s.server.ValidateRegistration(&s.key.PublicKey, cyphertext)
s.Require().NoError(err)
}
func (s *ServerSuite) TestPushNotificationHandleRegistration() {
// Empty payload
2020-07-02 13:57:50 +00:00
response := s.server.HandlePushNotificationRegistration(&s.key.PublicKey, nil)
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Empty key
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(nil, []byte("payload"))
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Invalid cyphertext length
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, []byte("too short"))
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Invalid cyphertext length
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, []byte("too short"))
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Invalid ciphertext
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, []byte("not too short but invalid"))
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Different key ciphertext
cyphertext, err := common.Encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Right cyphertext but non unmarshable payload
cyphertext, err = common.Encrypt([]byte("plaintext"), s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
InstallationId: "abc",
2020-07-13 08:53:13 +00:00
Grant: s.grant,
Version: 1,
})
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
// Setup persistence
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,
Version: 2}))
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_VERSION_MISMATCH)
// Cleanup persistence
s.Require().NoError(s.persistence.DeletePushNotificationRegistration(common.HashPublicKey(&s.key.PublicKey), s.installationID))
// 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,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
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,
Version: 1,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().False(response.Success)
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
// Successful
registration := &protobuf.PushNotificationRegistration{
Token: "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)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().True(response.Success)
// Pull from the db
retrievedRegistration, err := s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(&s.key.PublicKey), s.installationID)
s.Require().NoError(err)
s.Require().NotNil(retrievedRegistration)
s.Require().True(proto.Equal(retrievedRegistration, registration))
// Unregistering message
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
Token: "token",
2020-07-02 13:57:50 +00:00
InstallationId: s.installationID,
Unregister: true,
Version: 2,
})
s.Require().NoError(err)
cyphertext, err = common.Encrypt(payload, s.sharedKey, rand.Reader)
s.Require().NoError(err)
2020-07-02 13:57:50 +00:00
response = s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().True(response.Success)
// Check is gone from the db
retrievedRegistration, err = s.persistence.GetPushNotificationRegistrationByPublicKeyAndInstallationID(common.HashPublicKey(&s.key.PublicKey), s.installationID)
s.Require().NoError(err)
s.Require().NotNil(retrievedRegistration)
s.Require().Empty(retrievedRegistration.AccessToken)
s.Require().Empty(retrievedRegistration.Token)
s.Require().Equal(uint64(2), retrievedRegistration.Version)
2020-07-02 13:57:50 +00:00
s.Require().Equal(s.installationID, retrievedRegistration.InstallationId)
s.Require().Equal(common.Shake256(cyphertext), response.RequestId)
}
2020-07-02 13:57:50 +00:00
func (s *ServerSuite) TestHandlePushNotificationQueryNoFiltering() {
hashedPublicKey := common.HashPublicKey(&s.key.PublicKey)
2020-07-02 13:57:50 +00:00
// Successful
registration := &protobuf.PushNotificationRegistration{
Token: "abc",
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)
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
2020-07-02 13:57:50 +00:00
s.Require().NoError(err)
response := s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().True(response.Success)
query := &protobuf.PushNotificationQuery{
PublicKeys: [][]byte{[]byte("non-existing"), hashedPublicKey},
}
queryResponse := s.server.HandlePushNotificationQuery(query)
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)
s.Require().Nil(queryResponse.Info[0].AllowedUserList)
}
func (s *ServerSuite) TestHandlePushNotificationQueryWithFiltering() {
hashedPublicKey := common.HashPublicKey(&s.key.PublicKey)
2020-07-02 13:57:50 +00:00
allowedUserList := [][]byte{[]byte("a")}
// Successful
registration := &protobuf.PushNotificationRegistration{
Token: "abc",
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,
AllowedUserList: allowedUserList,
Version: 1,
}
payload, err := proto.Marshal(registration)
s.Require().NoError(err)
cyphertext, err := common.Encrypt(payload, s.sharedKey, rand.Reader)
2020-07-02 13:57:50 +00:00
s.Require().NoError(err)
response := s.server.HandlePushNotificationRegistration(&s.key.PublicKey, cyphertext)
s.Require().NotNil(response)
s.Require().True(response.Success)
query := &protobuf.PushNotificationQuery{
PublicKeys: [][]byte{[]byte("non-existing"), hashedPublicKey},
}
queryResponse := s.server.HandlePushNotificationQuery(query)
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)
s.Require().Equal(allowedUserList, queryResponse.Info[0].AllowedUserList)
}