2019-12-02 15:34:05 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2020-09-25 08:12:02 +00:00
|
|
|
"strings"
|
2019-12-02 15:34:05 +00:00
|
|
|
"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
|
2023-08-18 11:39:59 +00:00
|
|
|
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,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.RequestAddressForTransaction{
|
2020-01-10 18:59:01 +00:00
|
|
|
Clock: 30,
|
|
|
|
Value: "0.34",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "missing clock value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.RequestAddressForTransaction{
|
2020-01-10 18:59:01 +00:00
|
|
|
Value: "0.34",
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "missing value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.RequestAddressForTransaction{
|
2020-01-10 18:59:01 +00:00
|
|
|
Clock: 30,
|
|
|
|
Contract: "some contract",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "non number value",
|
|
|
|
WhisperTimestamp: 30,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.RequestAddressForTransaction{
|
2020-01-10 18:59:01 +00:00
|
|
|
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,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.RequestAddressForTransaction{
|
2020-01-20 16:44:32 +00:00
|
|
|
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() {
|
2023-08-18 11:39:59 +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
|
2023-08-18 11:39:59 +00:00
|
|
|
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,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Clock: 1,
|
|
|
|
Timestamp: 2,
|
2020-09-25 08:12:02 +00:00
|
|
|
Text: strings.Repeat("É", maxChatMessageTextLength),
|
2019-12-02 15:34:05 +00:00
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing chatId",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
Clock: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing clock",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Timestamp: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Clock value too high",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-01-20 16:44:32 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Clock: 133000,
|
|
|
|
Timestamp: 1,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-01-20 16:44:32 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Missing timestamp",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Clock: 2,
|
|
|
|
Text: "some-text",
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Missing text",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Blank text",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: " \n \t \n ",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
2020-09-01 14:38:36 +00:00
|
|
|
{
|
|
|
|
Name: "Too long text",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-09-01 14:38:36 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Clock: 1,
|
|
|
|
Timestamp: 2,
|
2020-09-25 08:12:02 +00:00
|
|
|
Text: strings.Repeat("É", maxChatMessageTextLength+1),
|
2020-09-01 14:38:36 +00:00
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
2019-12-02 15:34:05 +00:00
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Unknown MessageType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_UNKNOWN_MESSAGE_TYPE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Unknown ContentType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "System message MessageType",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP,
|
2019-12-02 15:34:05 +00:00
|
|
|
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,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-01-10 18:59:01 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-01-10 18:59:01 +00:00
|
|
|
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,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: ":+1:",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
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: "",
|
2020-07-27 15:57:01 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_EMOJI,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
,*/
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Valid sticker message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Sticker{
|
|
|
|
Sticker: &protobuf.StickerMessage{
|
|
|
|
Pack: 1,
|
|
|
|
Hash: "some-hash",
|
|
|
|
},
|
|
|
|
},
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Invalid sticker message without Hash",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Sticker{
|
|
|
|
Sticker: &protobuf.StickerMessage{
|
|
|
|
Pack: 1,
|
|
|
|
},
|
|
|
|
},
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-01-20 16:44:32 +00:00
|
|
|
Name: "Invalid sticker message without any content",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2019-12-02 15:34:05 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2019-12-02 15:34:05 +00:00
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
2020-05-13 13:16:17 +00:00
|
|
|
{
|
|
|
|
Name: "Valid image message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-05-13 13:16:17 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
2024-01-24 20:09:28 +00:00
|
|
|
Format: 1,
|
2020-05-13 13:16:17 +00:00
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-05-13 13:16:17 +00:00
|
|
|
ContentType: protobuf.ChatMessage_IMAGE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid image message, type unknown",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-05-13 13:16:17 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
2024-01-24 20:09:28 +00:00
|
|
|
Format: protobuf.ImageFormat_UNKNOWN_IMAGE_FORMAT,
|
2020-05-13 13:16:17 +00:00
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-05-13 13:16:17 +00:00
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid image message, missing payload",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-05-13 13:16:17 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Image{
|
|
|
|
Image: &protobuf.ImageMessage{
|
2024-01-24 20:09:28 +00:00
|
|
|
Format: 1,
|
2020-05-13 13:16:17 +00:00
|
|
|
},
|
|
|
|
},
|
2020-07-25 11:46:43 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-05-13 13:16:17 +00:00
|
|
|
ContentType: protobuf.ChatMessage_IMAGE,
|
|
|
|
},
|
|
|
|
},
|
2020-06-17 18:55:49 +00:00
|
|
|
{
|
|
|
|
Name: "Valid audio message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-06-17 18:55:49 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{
|
|
|
|
Type: 1,
|
|
|
|
Payload: []byte("some-payload"),
|
|
|
|
},
|
|
|
|
},
|
2020-07-27 15:57:01 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-06-17 18:55:49 +00:00
|
|
|
ContentType: protobuf.ChatMessage_AUDIO,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid audio message, type unknown",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-06-17 18:55:49 +00:00
|
|
|
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"),
|
|
|
|
},
|
|
|
|
},
|
2020-07-27 15:57:01 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-06-17 18:55:49 +00:00
|
|
|
ContentType: protobuf.ChatMessage_STICKER,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid audio message, missing payload",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.ChatMessage{
|
2020-06-17 18:55:49 +00:00
|
|
|
ChatId: "a",
|
|
|
|
Text: "valid",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_Audio{
|
|
|
|
Audio: &protobuf.AudioMessage{
|
|
|
|
Type: 1,
|
|
|
|
},
|
|
|
|
},
|
2020-07-27 15:57:01 +00:00
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
2020-06-17 18:55:49 +00:00
|
|
|
ContentType: protobuf.ChatMessage_AUDIO,
|
|
|
|
},
|
|
|
|
},
|
2024-01-03 10:29:21 +00:00
|
|
|
{
|
|
|
|
Name: "Valid bridge message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: true,
|
|
|
|
Message: &protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_BridgeMessage{
|
|
|
|
BridgeMessage: &protobuf.BridgeMessage{
|
|
|
|
BridgeName: "discord",
|
|
|
|
UserName: "mike",
|
|
|
|
Content: "some text",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_BRIDGE_MESSAGE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "Invalid bridge message",
|
|
|
|
WhisperTimestamp: 2,
|
|
|
|
Valid: false,
|
|
|
|
Message: &protobuf.ChatMessage{
|
|
|
|
ChatId: "a",
|
|
|
|
Text: "",
|
|
|
|
Clock: 2,
|
|
|
|
Timestamp: 3,
|
|
|
|
ResponseTo: "",
|
|
|
|
EnsName: "",
|
|
|
|
Payload: &protobuf.ChatMessage_BridgeMessage{
|
|
|
|
BridgeMessage: &protobuf.BridgeMessage{
|
|
|
|
BridgeName: "",
|
|
|
|
UserName: "",
|
|
|
|
Content: "",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
ContentType: protobuf.ChatMessage_BRIDGE_MESSAGE,
|
|
|
|
},
|
|
|
|
},
|
2019-12-02 15:34:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.Name, func() {
|
2023-08-18 11:39:59 +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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 07:53:32 +00:00
|
|
|
|
|
|
|
func (s *MessageValidatorSuite) TestValidateEmojiReaction() {
|
|
|
|
testCases := []struct {
|
|
|
|
Name string
|
|
|
|
Valid bool
|
|
|
|
WhisperTimestamp uint64
|
2023-08-18 11:39:59 +00:00
|
|
|
Message *protobuf.EmojiReaction
|
2020-07-28 07:53:32 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "valid emoji reaction",
|
|
|
|
Valid: true,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
ChatId: "chat-id",
|
|
|
|
MessageId: "message-id",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "valid emoji retraction",
|
|
|
|
Valid: true,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
ChatId: "0.34",
|
|
|
|
MessageId: "message-id",
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
Retracted: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "missing chatID",
|
|
|
|
Valid: false,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
MessageId: "message-id",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "missing messageID",
|
|
|
|
Valid: false,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
ChatId: "chat-id",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "missing type",
|
|
|
|
Valid: false,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
ChatId: "chat-id",
|
|
|
|
MessageId: "message-id",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "missing message type",
|
|
|
|
Valid: false,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 30,
|
|
|
|
ChatId: "chat-id",
|
|
|
|
MessageId: "message-id",
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "clock value too high",
|
|
|
|
Valid: false,
|
|
|
|
WhisperTimestamp: 30,
|
2023-08-18 11:39:59 +00:00
|
|
|
Message: &protobuf.EmojiReaction{
|
2020-07-28 07:53:32 +00:00
|
|
|
Clock: 900000,
|
|
|
|
ChatId: "chat-id",
|
|
|
|
MessageId: "message-id",
|
|
|
|
MessageType: protobuf.MessageType_ONE_TO_ONE,
|
|
|
|
Type: protobuf.EmojiReaction_LOVE,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(tc.Name, func() {
|
2023-08-18 11:39:59 +00:00
|
|
|
err := ValidateReceivedEmojiReaction(tc.Message, tc.WhisperTimestamp)
|
2020-07-28 07:53:32 +00:00
|
|
|
if tc.Valid {
|
|
|
|
s.Nil(err)
|
|
|
|
} else {
|
|
|
|
s.NotNil(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|