59 lines
1.6 KiB
Go
59 lines
1.6 KiB
Go
|
package model
|
||
|
|
||
|
import (
|
||
|
"git.mascherari.press/cwtch/protocol"
|
||
|
"github.com/golang/protobuf/proto"
|
||
|
"testing"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func TestMessageInsert(t *testing.T) {
|
||
|
timeline := new(Timeline)
|
||
|
|
||
|
// Setup the Group
|
||
|
sarah := GenerateNewProfile("Sarah")
|
||
|
alice := GenerateNewProfile("Alice")
|
||
|
sarah.AddContact(alice.Onion, alice.PublicProfile)
|
||
|
alice.AddContact(sarah.Onion, sarah.PublicProfile)
|
||
|
|
||
|
gid, invite := alice.StartGroup("aaa.onion")
|
||
|
gci := &protocol.CwtchPeerPacket{}
|
||
|
proto.Unmarshal(invite, gci)
|
||
|
sarah.ProcessInvite(gci.GetGroupChatInvite(), alice.Onion)
|
||
|
|
||
|
group := alice.GetGroupByGroupId(gid)
|
||
|
|
||
|
c1 := sarah.EncryptMessageToGroup("Hello World 1", group.GroupID)
|
||
|
alice.AttemptDecryption(c1)
|
||
|
|
||
|
c2 := alice.EncryptMessageToGroup("Hello World 2", group.GroupID)
|
||
|
alice.AttemptDecryption(c2)
|
||
|
|
||
|
c3 := alice.EncryptMessageToGroup("Hello World 3", group.GroupID)
|
||
|
alice.AttemptDecryption(c3)
|
||
|
|
||
|
time.Sleep(time.Second * 1)
|
||
|
|
||
|
c4 := alice.EncryptMessageToGroup("Hello World 4", group.GroupID)
|
||
|
alice.AttemptDecryption(c4)
|
||
|
|
||
|
c5 := alice.EncryptMessageToGroup("Hello World 5", group.GroupID)
|
||
|
|
||
|
_, m1 := sarah.AttemptDecryption(c1)
|
||
|
_, m2 := sarah.AttemptDecryption(c2)
|
||
|
_, m3 := sarah.AttemptDecryption(c3)
|
||
|
_, m4 := sarah.AttemptDecryption(c4)
|
||
|
_, m5 := sarah.AttemptDecryption(c5)
|
||
|
|
||
|
// Now we simulate a client receiving these messages completely out of order
|
||
|
timeline.Insert(m1)
|
||
|
timeline.Insert(m5)
|
||
|
timeline.Insert(m4)
|
||
|
timeline.Insert(m3)
|
||
|
timeline.Insert(m2)
|
||
|
|
||
|
for i, m := range timeline.Messages {
|
||
|
t.Logf("Messages %v: %v %x %x", i, m.Message, m.Signature, m.PreviousMessageSig)
|
||
|
}
|
||
|
}
|