From a2e6d33d251cae25d052dce8cfb6ef93bef4e638 Mon Sep 17 00:00:00 2001 From: Benjamin Wenger <ben@elixxir.ioo> Date: Wed, 31 Aug 2022 08:12:13 -0700 Subject: [PATCH] fixed bugs in the event model where it wasnt handling errors correctly and tests were broken --- channels/emoji.go | 3 +- channels/emoji_test.go | 10 ++++-- channels/eventModel.go | 5 +++ channels/eventModel_test.go | 72 ++++++++++++++++++++++++++++++++++++- 4 files changed, 84 insertions(+), 6 deletions(-) diff --git a/channels/emoji.go b/channels/emoji.go index 4e1e77d4d..899bedbd4 100644 --- a/channels/emoji.go +++ b/channels/emoji.go @@ -8,7 +8,6 @@ package channels import ( - "bufio" "bytes" jww "github.com/spf13/jwalterweatherman" "regexp" @@ -39,7 +38,7 @@ func ValidateReaction(reaction string) error { return InvalidReaction } - reader := bufio.NewReader(bytes.NewReader([]byte(reaction))) + reader := bytes.NewReader([]byte(reaction)) // make sure it has emojis if !compiledRegex.MatchReader(reader) { diff --git a/channels/emoji_test.go b/channels/emoji_test.go index f453cab5f..d320b05a6 100644 --- a/channels/emoji_test.go +++ b/channels/emoji_test.go @@ -13,13 +13,17 @@ import ( func TestValidateReaction(t *testing.T) { - testReactions := []string{"🍆", "😂", "❤", "🤣", "👍", "😭", "🙏", "😘", "🥰", "😍", - "😊", "☺", "A", "b", "AA", "1", "🍆🍆", "🍆A", "👍👍👍", "👍😘A", "O"} + testReactions := []string{"🍆", "😂", "❤", "🤣", "👍", "😭", "🙏", "😘", "🥰", + "😍", "😊", "☺", "A", "b", "AA", "1", "🍆🍆", "🍆A", "👍👍👍", "👍😘A", + "O", "\u0000", "\u0011", "\u001F", "\u007F", "\u0080", "\u008A", + "\u009F"} + expected := []error{ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, - InvalidReaction} + InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction, + InvalidReaction, InvalidReaction, InvalidReaction, InvalidReaction} for i, r := range testReactions { err := ValidateReaction(r) diff --git a/channels/eventModel.go b/channels/eventModel.go index 23db8e082..44af3a0c7 100644 --- a/channels/eventModel.go +++ b/channels/eventModel.go @@ -139,6 +139,7 @@ func (e *events) triggerEvent(chID *id.ID, umi *userMessageInternal, "round %d which could not be handled due to unregistered message "+ "type %s; Contents: %v", um.Username, chID, round.ID, messageType, cm.Payload) + return } //modify the timestamp to reduce the chance message order will be ambiguous @@ -170,6 +171,7 @@ func (e *events) triggerAdminEvent(chID *id.ID, cm *ChannelMessage, "round %d which could not be handled due to unregistered message "+ "type %s; Contents: %v", AdminUsername, chID, round.ID, messageType, cm.Payload) + return } //modify the timestamp to reduce the chance message order will be ambiguous @@ -213,6 +215,8 @@ func (e *events) receiveTextMessage(channelID *id.ID, "without reply", messageID, senderUsername, channelID, messageType, timestamp, lease, round.ID) + // Still process the message, but drop the reply because it is + // malformed } } @@ -246,6 +250,7 @@ func (e *events) receiveReaction(channelID *id.ID, "reaction (%s), ignoring reaction", messageID, senderUsername, channelID, messageType, timestamp, lease, round.ID, err) + return } if react.ReactionMessageID != nil && len(react.ReactionMessageID) == cryptoChannel.MessageIDLen { diff --git a/channels/eventModel_test.go b/channels/eventModel_test.go index a8513489d..866e2b761 100644 --- a/channels/eventModel_test.go +++ b/channels/eventModel_test.go @@ -173,6 +173,7 @@ func TestEvents_RegisterReceiveHandler(t *testing.T) { } type dummyMessageTypeHandler struct { + triggered bool channelID *id.ID messageID cryptoChannel.MessageID messageType MessageType @@ -187,6 +188,7 @@ func (dmth *dummyMessageTypeHandler) dummyMessageTypeReceiveMessage( channelID *id.ID, messageID cryptoChannel.MessageID, messageType MessageType, senderUsername string, content []byte, timestamp time.Time, lease time.Duration, round rounds.Round) { + dmth.triggered = true dmth.channelID = channelID dmth.messageID = messageID dmth.messageType = messageType @@ -224,6 +226,11 @@ func TestEvents_triggerEvents(t *testing.T) { //call the trigger e.triggerEvent(chID, umi, receptionID.EphemeralIdentity{}, r) + //check that the event was triggered + if !dummy.triggered { + t.Errorf("The event was not triggered") + } + //check the data is stored in the dummy if !dummy.channelID.Cmp(chID) { t.Errorf("The channel IDs do not match %s vs %s", @@ -266,6 +273,34 @@ func TestEvents_triggerEvents(t *testing.T) { } } +func TestEvents_triggerEvents_noChannel(t *testing.T) { + me := &MockEvent{} + + e := initEvents(me) + + dummy := &dummyMessageTypeHandler{} + + //skip handler registration + mt := MessageType(42) + + //craft the input for the event + chID := &id.ID{} + chID[0] = 1 + + umi, _, _ := builtTestUMI(t, mt) + + r := rounds.Round{ID: 420, Timestamps: make(map[states.Round]time.Time)} + r.Timestamps[states.QUEUED] = time.Now() + + //call the trigger + e.triggerEvent(chID, umi, receptionID.EphemeralIdentity{}, r) + + //check that the event was triggered + if dummy.triggered { + t.Errorf("The event was triggered when it is unregistered") + } +} + func TestEvents_triggerAdminEvents(t *testing.T) { me := &MockEvent{} @@ -295,6 +330,11 @@ func TestEvents_triggerAdminEvents(t *testing.T) { //call the trigger e.triggerAdminEvent(chID, cm, msgID, receptionID.EphemeralIdentity{}, r) + //check that the event was triggered + if !dummy.triggered { + t.Errorf("The admin event was not triggered") + } + //check the data is stored in the dummy if !dummy.channelID.Cmp(chID) { t.Errorf("The channel IDs do not match %s vs %s", @@ -337,6 +377,36 @@ func TestEvents_triggerAdminEvents(t *testing.T) { } } +func TestEvents_triggerAdminEvents_noChannel(t *testing.T) { + me := &MockEvent{} + + e := initEvents(me) + + dummy := &dummyMessageTypeHandler{} + + mt := MessageType(42) + //skip handler registration + + //craft the input for the event + chID := &id.ID{} + chID[0] = 1 + + u, _, cm := builtTestUMI(t, mt) + + r := rounds.Round{ID: 420, Timestamps: make(map[states.Round]time.Time)} + r.Timestamps[states.QUEUED] = time.Now() + + msgID := cryptoChannel.MakeMessageID(u.userMessage.Message) + + //call the trigger + e.triggerAdminEvent(chID, cm, msgID, receptionID.EphemeralIdentity{}, r) + + //check that the event was triggered + if dummy.triggered { + t.Errorf("The admin event was triggered when unregistered") + } +} + func TestEvents_receiveTextMessage_Message(t *testing.T) { me := &MockEvent{} @@ -699,7 +769,7 @@ func TestEvents_receiveReaction_InvalidReactionContent(t *testing.T) { chID := &id.ID{} chID[0] = 1 - replyMsgId := []byte("blarg") + replyMsgId := cryptoChannel.MakeMessageID([]byte("blarg")) textPayload := &CMIXChannelReaction{ Version: 0, -- GitLab