Files
status-go/internal/protocol/message_validator_test.go
Igor Sirotin f3e4363808 refactor: move protocol to internal/protocol
Part of the Go project layout migration, item 27.

Pure move plus import-path rewrite across 502 files. No API or behaviour
change. `internal/` keeps the messaging application logic unimportable
from outside the module, which is what the issue asks for -- status-go is
consumed through the C-bindings in mobile/, not as a Go library.

Things that had to follow the move, beyond the Go imports:

- tools/generate-handlers/template.txt. messenger_handlers.go is
  generated, and the template hard-codes the imports it emits, so the
  generated file kept importing protocol/common and failed typecheck.
- .gitignore. The ignore rule for that generated file was pinned to the
  old path; without moving it, a 1486-line generated file starts being
  tracked.
- Makefile: the logosstorage and torrent test targets (both the archive
  packages and ./protocol itself), the archive README, migration-protocol.
- scripts/run_unit_tests.sh, which names the protocol package explicitly
  to shard its tests.
- scripts/cleanup_generated_files.sh and .golangci.yml.

scripts/migration_check.sh also needed a fix that is not specific to this
move: it validated every file the branch touched under a migration dir
against the timestamp naming rule, and a directory rename makes every
migration in it look newly added. It now excludes renames, so moving a
migration is not mistaken for adding one.

refs #7067
2026-08-21 10:11:05 +02:00

584 lines
14 KiB
Go

package protocol
import (
"strings"
"testing"
"github.com/stretchr/testify/suite"
"github.com/status-im/status-go/internal/protocol/protobuf"
)
type MessageValidatorSuite struct {
suite.Suite
}
func TestMessageValidatorSuite(t *testing.T) {
suite.Run(t, new(MessageValidatorSuite))
}
func (s *MessageValidatorSuite) TestValidatePlainTextMessage() {
testCases := []struct {
Name string
WhisperTimestamp uint64
Valid bool
Message *protobuf.ChatMessage
}{
{
Name: "A valid message",
WhisperTimestamp: 2,
Valid: true,
Message: &protobuf.ChatMessage{
ChatId: "a",
Clock: 1,
Timestamp: 2,
Text: strings.Repeat("É", maxChatMessageTextLength),
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Missing chatId",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
Clock: 1,
Timestamp: 2,
Text: "some-text",
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Missing clock",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Timestamp: 2,
Text: "some-text",
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
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.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Missing timestamp",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Clock: 2,
Text: "some-text",
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Missing text",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Blank text",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: " \n \t \n ",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Too long text",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Clock: 1,
Timestamp: 2,
Text: strings.Repeat("É", maxChatMessageTextLength+1),
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Unknown MessageType",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_UNKNOWN_MESSAGE_TYPE,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Unknown ContentType",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_UNKNOWN_CONTENT_TYPE,
},
},
{
Name: "System message MessageType",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_SYSTEM_MESSAGE_PRIVATE_GROUP,
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
},
},
{
Name: "Valid emoji only emssage",
WhisperTimestamp: 2,
Valid: true,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: ":+1:",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_EMOJI,
},
},
// TODO: FIX ME
/* {
Name: "Invalid emoji only emssage",
Valid: false,
Message: protobuf.ChatMessage{
ChatID: "a",
Text: ":+1: not valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_EMOJI,
},
}
,*/
{
Name: "Valid sticker message",
WhisperTimestamp: 2,
Valid: true,
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.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_STICKER,
},
},
{
Name: "Invalid sticker message without Hash",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
Payload: &protobuf.ChatMessage_Sticker{
Sticker: &protobuf.StickerMessage{
Pack: 1,
},
},
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_STICKER,
},
},
{
Name: "Invalid sticker message without any content",
WhisperTimestamp: 2,
Valid: false,
Message: &protobuf.ChatMessage{
ChatId: "a",
Text: "valid",
Clock: 2,
Timestamp: 3,
ResponseTo: "",
EnsName: "",
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_STICKER,
},
},
{
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{
Format: 1,
Payload: []byte("some-payload"),
},
},
MessageType: protobuf.MessageType_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{
Format: protobuf.ImageFormat_UNKNOWN_IMAGE_FORMAT,
Payload: []byte("some-payload"),
},
},
MessageType: protobuf.MessageType_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{
Format: 1,
},
},
MessageType: protobuf.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_IMAGE,
},
},
{
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.MessageType_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.MessageType_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.MessageType_ONE_TO_ONE,
ContentType: protobuf.ChatMessage_AUDIO,
},
},
{
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,
},
},
}
for _, tc := range testCases {
s.Run(tc.Name, func() {
err := ValidateReceivedChatMessage(tc.Message, tc.WhisperTimestamp)
if tc.Valid {
s.Nil(err)
} else {
s.NotNil(err)
}
})
}
}
func (s *MessageValidatorSuite) TestValidateEmojiReaction() {
testCases := []struct {
Name string
Valid bool
WhisperTimestamp uint64
Message *protobuf.EmojiReaction
}{
{
Name: "valid emoji reaction",
Valid: true,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
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,
Message: &protobuf.EmojiReaction{
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,
Message: &protobuf.EmojiReaction{
Clock: 30,
MessageId: "message-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
Type: protobuf.EmojiReaction_LOVE,
},
},
{
Name: "missing messageID",
Valid: false,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 30,
ChatId: "chat-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
Type: protobuf.EmojiReaction_LOVE,
},
},
{
Name: "missing type",
Valid: false,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 30,
ChatId: "chat-id",
MessageId: "message-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
},
},
{
Name: "missing message type",
Valid: false,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 30,
ChatId: "chat-id",
MessageId: "message-id",
Type: protobuf.EmojiReaction_LOVE,
},
},
{
Name: "clock value too high",
Valid: false,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 900000,
ChatId: "chat-id",
MessageId: "message-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
Type: protobuf.EmojiReaction_LOVE,
},
},
{
Name: "no emoji and unknown type",
Valid: false,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 30,
ChatId: "chat-id",
MessageId: "message-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
Type: protobuf.EmojiReaction_UNKNOWN_EMOJI_REACTION_TYPE,
Emoji: "",
},
},
{
Name: "unknown type but has an emoji",
Valid: true,
WhisperTimestamp: 30,
Message: &protobuf.EmojiReaction{
Clock: 30,
ChatId: "chat-id",
MessageId: "message-id",
MessageType: protobuf.MessageType_ONE_TO_ONE,
Type: protobuf.EmojiReaction_UNKNOWN_EMOJI_REACTION_TYPE,
Emoji: "🏁",
},
},
}
for _, tc := range testCases {
s.Run(tc.Name, func() {
err := ValidateReceivedEmojiReaction(tc.Message, tc.WhisperTimestamp)
if tc.Valid {
s.Nil(err)
} else {
s.NotNil(err)
}
})
}
}