2019-12-02 15:34:05 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-12-02 15:34:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MessageValidatorSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMessageValidatorSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(MessageValidatorSuite))
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:59:01 +00:00
|
|
|
func (s *MessageValidatorSuite) TestValidateRequestAddressForTransaction() {
|
|
|
|
testCases := []struct {
|
2020-01-20 16:44:32 +00:00
|
|
|
Name string
|
|
|
|
WhisperTimestamp uint64
|
|
|
|
Valid bool
|
|
|
|
Message protobuf.RequestAddressForTransaction
|
2020-01-10 18:59:01 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "valid message",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: true,
|
2020-01-10 18:59:01 +00:00
|
|
|
Message: protobuf.RequestAddressForTransaction{
|
|
|
|
Clock: 30,
|
|
|
|
Value: "0.34",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "missing clock value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2020-01-10 18:59:01 +00:00
|
|
|
Message: protobuf.RequestAddressForTransaction{
|
|
|
|
Value: "0.34",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "missing value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2020-01-10 18:59:01 +00:00
|
|
|
Message: protobuf.RequestAddressForTransaction{
|
|
|
|
Clock: 30,
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "non number value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2020-01-10 18:59:01 +00:00
|
|
|
Message: protobuf.RequestAddressForTransaction{
|
|
|
|
Clock: 30,
|
|
|
|
Value: "most definitely not a number",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
2020-01-20 16:44:32 +00:00
|
|
|
{
|
|
|
|
Name: "Clock value too high",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.RequestAddressForTransaction{
|
|
|
|
Clock: 151000,
|
|
|
|
Value: "0.34",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
2020-01-10 18:59:01 +00:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.Name, func() {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedRequestAddressForTransaction(&tc.Message, tc.WhisperTimestamp)
|
2020-01-10 18:59:01 +00:00
|
|
|
if tc.Valid {
|
|
|
|
s.Nil(err)
|
|
|
|
} else {
|
|
|
|
s.NotNil(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-02 15:34:05 +00:00
|
|
|
func (s *MessageValidatorSuite) TestValidatePlainTextMessage() {
|
|
|
|
testCases := []struct {
|
2020-01-20 16:44:32 +00:00
|
|
|
Name string
|
|
|
|
WhisperTimestamp uint64
|
|
|
|
Valid bool
|
|
|
|
Message protobuf.ChatMessage
|
2019-12-02 15:34:05 +00:00
|
|
|
}{
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "A valid message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Clock: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing chatId",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
Clock: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing clock",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Timestamp: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Clock value too high",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Clock: 133000,
|
|
|
|
Timestamp: 1,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Missing timestamp",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Clock: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing text",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Blank text",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: " \n \t \n ",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Unknown MessageType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_UNKNOWN_MESSAGE_TYPE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Unknown ContentType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "System message MessageType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_SYSTEM_MESSAGE_PRIVATE_GROUP,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
2020-01-10 18:59:01 +00:00
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Request address for transaction message type",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2020-01-10 18:59:01 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TRANSACTION_COMMAND,
|
|
|
|
},
|
|
|
|
},
|
2019-12-02 15:34:05 +00:00
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Valid emoji only emssage",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: ":+1:",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_EMOJI,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
// TODO: FIX ME
|
|
|
|
/* {
|
|
|
|
Name: "Invalid emoji only emssage",
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
2020-02-10 11:22:37 +00:00
|
|
|
ChatID: "a",
|
2019-12-02 15:34:05 +00:00
|
|
|
Text: ":+1: not valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_EMOJI,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,*/
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Valid sticker message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Sticker{
|
|
|
|
Sticker: &protobuf.StickerMessage{
|
|
|
|
Pack: 1,
|
|
|
|
Hash: "some-hash",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Invalid sticker message without Hash",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Sticker{
|
|
|
|
Sticker: &protobuf.StickerMessage{
|
|
|
|
Pack: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Invalid sticker message without any content",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2019-12-02 15:34:05 +00:00
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
2020-05-13 13:16:17 +00:00
|
|
|
{
|
|
|
|
Name: "Valid image message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Type: 1,
|
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_IMAGE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid image message, type unknown",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Type: protobuf.ImageMessage_UNKNOWN_IMAGE_TYPE,
|
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid image message, missing payload",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
|
|
|
Type: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_IMAGE,
|
|
|
|
},
|
|
|
|
},
|
2020-06-17 18:55:49 +00:00
|
|
|
{
|
|
|
|
Name: "Valid audio message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{
|
|
|
|
Type: 1,
|
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_AUDIO,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid audio message, type unknown",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{
|
|
|
|
Type: protobuf.AudioMessage_UNKNOWN_AUDIO_TYPE,
|
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid audio message, missing payload",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{
|
|
|
|
Type: 1,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.ChatMessage_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_AUDIO,
|
|
|
|
},
|
|
|
|
},
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.Name, func() {
|
2020-01-20 16:44:32 +00:00
|
|
|
err := ValidateReceivedChatMessage(&tc.Message, tc.WhisperTimestamp)
|
2019-12-02 15:34:05 +00:00
|
|
|
if tc.Valid {
|
|
|
|
s.Nil(err)
|
|
|
|
} else {
|
|
|
|
s.NotNil(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|