2020-07-01 12:04:09 +00:00
|
|
|
package push_notification_server
|
2020-06-30 08:30:58 +00:00
|
|
|
|
|
|
|
import (
|
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 13:14:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
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
|
|
|
|
|
|
|
"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-06-30 08:30:58 +00:00
|
|
|
)
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
func TestServerSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(ServerSuite))
|
2020-07-01 10:09:40 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
type ServerSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
tmpFile *os.File
|
|
|
|
persistence Persistence
|
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
|
|
|
|
|
|
|
|
database, err := sqlite.Open(s.tmpFile.Name(), "")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.persistence = NewSQLitePersistence(database)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationServerValidateRegistration() {
|
2020-07-01 08:37:54 +00:00
|
|
|
accessToken := "b6ae4fde-bb65-11ea-b3de-0242ac130004"
|
|
|
|
installationID := "c6ae4fde-bb65-11ea-b3de-0242ac130004"
|
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-06-30 13:14:25 +00:00
|
|
|
|
|
|
|
config := &Config{
|
|
|
|
Identity: identity,
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:04:09 +00:00
|
|
|
server := New(config, s.persistence)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
key, err := crypto.GenerateKey()
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
sharedKey, err := server.generateSharedKey(&key.PublicKey)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
2020-06-30 13:14:25 +00:00
|
|
|
// Empty payload
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 10:09:40 +00:00
|
|
|
_, err = 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-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&key.PublicKey, []byte("too short"))
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().Equal(ErrInvalidCiphertextLength, err)
|
2020-06-30 13:14:25 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
// Invalid cyphertext length
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&key.PublicKey, []byte("too short"))
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().Equal(ErrInvalidCiphertextLength, err)
|
2020-06-30 08:30:58 +00:00
|
|
|
|
2020-06-30 14:55:24 +00:00
|
|
|
// Invalid ciphertext
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&key.PublicKey, []byte("not too short but invalid"))
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().Error(ErrInvalidCiphertextLength, err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
// Different key ciphertext
|
|
|
|
cyphertext, err := encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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
|
|
|
|
cyphertext, err = encrypt([]byte("plaintext"), sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: "abc",
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, 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
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationRegistration(&key.PublicKey, &protobuf.PushNotificationRegistration{
|
2020-07-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
2020-07-01 12:04:09 +00:00
|
|
|
Version: 2}))
|
2020-07-01 10:09:40 +00:00
|
|
|
|
|
|
|
_, err = server.ValidateRegistration(&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
|
|
|
|
s.Require().NoError(s.persistence.DeletePushNotificationRegistration(&key.PublicKey, 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-01 08:37:54 +00:00
|
|
|
InstallationId: installationID,
|
|
|
|
Unregister: true,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-06-30 14:55:24 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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",
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&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-01 08:37:54 +00:00
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 08:37:54 +00:00
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
2020-07-01 12:04:09 +00:00
|
|
|
s.Require().NoError(err)
|
2020-07-01 10:09:40 +00:00
|
|
|
_, err = server.ValidateRegistration(&key.PublicKey, cyphertext)
|
2020-07-02 08:08:19 +00:00
|
|
|
s.Require().Equal(ErrMalformedPushNotificationRegistrationDeviceToken, err)
|
|
|
|
|
|
|
|
// Successful
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
Token: "abc",
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
_, err = server.ValidateRegistration(&key.PublicKey, cyphertext)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServerSuite) TestPushNotificationHandleRegistration() {
|
|
|
|
accessToken := "b6ae4fde-bb65-11ea-b3de-0242ac130004"
|
|
|
|
installationID := "c6ae4fde-bb65-11ea-b3de-0242ac130004"
|
|
|
|
identity, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
config := &Config{
|
|
|
|
Identity: identity,
|
|
|
|
}
|
|
|
|
|
|
|
|
server := New(config, s.persistence)
|
|
|
|
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
sharedKey, err := server.generateSharedKey(&key.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Empty payload
|
|
|
|
response := server.HandlePushNotificationRegistration(&key.PublicKey, nil)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().False(response.Success)
|
|
|
|
s.Require().Equal(response.Error, protobuf.PushNotificationRegistrationResponse_MALFORMED_MESSAGE)
|
|
|
|
|
|
|
|
// Empty key
|
|
|
|
response = 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
|
|
|
|
response = server.HandlePushNotificationRegistration(&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
|
|
|
|
response = server.HandlePushNotificationRegistration(&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
|
|
|
|
response = server.HandlePushNotificationRegistration(&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 := encrypt([]byte("plaintext"), make([]byte, 32), rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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 = encrypt([]byte("plaintext"), sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: "abc",
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Setup persistence
|
|
|
|
s.Require().NoError(s.persistence.SavePushNotificationRegistration(&key.PublicKey, &protobuf.PushNotificationRegistration{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 2}))
|
|
|
|
|
|
|
|
response = server.HandlePushNotificationRegistration(&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(&key.PublicKey, installationID))
|
|
|
|
|
|
|
|
// Missing access token
|
|
|
|
payload, err = proto.Marshal(&protobuf.PushNotificationRegistration{
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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",
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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{
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&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",
|
|
|
|
AccessToken: accessToken,
|
|
|
|
InstallationId: installationID,
|
|
|
|
Version: 1,
|
|
|
|
}
|
|
|
|
payload, err = proto.Marshal(registration)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&key.PublicKey, cyphertext)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
// Pull from the db
|
|
|
|
retrievedRegistration, err := s.persistence.GetPushNotificationRegistration(&key.PublicKey, 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",
|
|
|
|
InstallationId: installationID,
|
|
|
|
Unregister: true,
|
|
|
|
Version: 2,
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
cyphertext, err = encrypt(payload, sharedKey, rand.Reader)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
response = server.HandlePushNotificationRegistration(&key.PublicKey, cyphertext)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
s.Require().True(response.Success)
|
|
|
|
|
|
|
|
// Check is gone from the db
|
|
|
|
retrievedRegistration, err = s.persistence.GetPushNotificationRegistration(&key.PublicKey, 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)
|
|
|
|
s.Require().Equal(installationID, retrievedRegistration.InstallationId)
|
|
|
|
s.Require().Equal(shake256(cyphertext), response.RequestId)
|
2020-06-30 08:30:58 +00:00
|
|
|
}
|